all_simple_paths(G, source, target, cutoff=None)
A simple path is a path with no repeated nodes.
This algorithm uses a modified depth-first search to generate the paths . A single path can be found in $O(V+E)$ time but the number of simple paths in a graph can be very large, e.g. $O(n!)$ in the complete graph of order $n$.
This function does not check that a path exists between :None:None:`source`
and :None:None:`target`
. For large graphs, this may result in very long runtimes. Consider using has_path
to check that a path exists between :None:None:`source`
and :None:None:`target`
before calling this function on large graphs.
Starting node for path
Single node or iterable of nodes at which to end path
Depth to stop the search. Only paths of length <= cutoff are returned.
A generator that produces lists of simple paths. If there are no paths between the source and target within the given cutoff the generator produces no output.
Generate all simple paths in the graph G from source to target.
>>> G = nx.complete_graph(4) >>> for path in nx.all_simple_paths(G, source=0, target=3): ... print(path) ... [0, 1, 2, 3] [0, 1, 3] [0, 2, 1, 3] [0, 2, 3] [0, 3]
>>> paths = nx.all_simple_paths(G, source=0, target=3, cutoff=2) >>> print(list(paths)) [[0, 1, 3], [0, 2, 3], [0, 3]]
>>> paths = nx.all_simple_paths(G, source=0, target=3) >>> for path in map(nx.utils.pairwise, paths): ... print(list(path)) [(0, 1), (1, 2), (2, 3)] [(0, 1), (1, 3)] [(0, 2), (2, 1), (1, 3)] [(0, 2), (2, 3)] [(0, 3)]
>>> G = nx.complete_graph(4) >>> for path in nx.all_simple_paths(G, source=0, target=[3, 2]): ... print(path) ... [0, 1, 2] [0, 1, 2, 3] [0, 1, 3] [0, 1, 3, 2] [0, 2] [0, 2, 1, 3] [0, 2, 3] [0, 3] [0, 3, 1, 2] [0, 3, 2]
>>> from itertools import chain >>> from itertools import product >>> from itertools import starmap >>> from functools import partial >>> >>> chaini = chain.from_iterable >>> >>> G = nx.DiGraph([(0, 1), (1, 2), (0, 3), (3, 2)]) >>> roots = (v for v, d in G.in_degree() if d == 0) >>> leaves = (v for v, d in G.out_degree() if d == 0) >>> all_paths = partial(nx.all_simple_paths, G) >>> list(chaini(starmap(all_paths, product(roots, leaves)))) [[0, 1, 2], [0, 3, 2]]
>>> G = nx.DiGraph([(0, 1), (1, 2), (0, 3), (3, 2)]) >>> roots = (v for v, d in G.in_degree() if d == 0) >>> leaves = (v for v, d in G.out_degree() if d == 0) >>> all_paths = [] >>> for root in roots: ... for leaf in leaves: ... paths = nx.all_simple_paths(G, root, leaf) ... all_paths.extend(paths) >>> all_paths [[0, 1, 2], [0, 3, 2]]
See :>>> G = nx.DiGraph([(0, 1), (2, 1), (1, 3), (1, 4)]) >>> roots = (v for v, d in G.in_degree() if d == 0) >>> leaves = [v for v, d in G.out_degree() if d == 0] >>> all_paths = [] >>> for root in roots: ... paths = nx.all_simple_paths(G, root, leaves) ... all_paths.extend(paths) >>> all_paths [[0, 1, 3], [0, 1, 4], [2, 1, 3], [2, 1, 4]]
The following pages refer to to this document either explicitly or contain code examples using this.
networkx.algorithms.dag.dag_longest_path_length
networkx.algorithms.dag.dag_longest_path
networkx.algorithms.simple_paths.shortest_simple_paths
networkx.algorithms.simple_paths.all_simple_edge_paths
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them