Python networkx.all_simple_paths() Examples
The following are 30
code examples of networkx.all_simple_paths().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
networkx
, or try the search function
.
Example #1
Source File: data.py From ektelo with Apache License 2.0 | 6 votes |
def materialize(self, X, prng, node): if self._dgraph.root.id == node.id: return X, None paths = [path for path in nx.all_simple_paths(self._dgraph.graph, self._dgraph.root.id, node.id)] assert len(paths) == 1 path = paths[0] for i in range(len(path)): node = self._dgraph.id_node_map[path[i]] if util.contains_superclass(node.operator.__class__, 'SplitByPartition'): id_node_map = self._dgraph.id_node_map Xs = node.operator.transform(X) X = Xs[node.idx] else: X = node.operator.transform(X) return X, path
Example #2
Source File: pathfinding.py From indra with BSD 2-Clause "Simplified" License | 6 votes |
def get_path_iter(graph, source, target, path_length, loop): """Return a generator of paths with path_length cutoff from source to target.""" path_iter = nx.all_simple_paths(graph, source, target, path_length) try: for p in path_iter: path = deepcopy(p) # Remove common target from a path. path.remove(target) if loop: path.append(path[0]) # A path should contain at least one edge if len(path) < 2: continue yield path except nx.NetworkXNoPath: pass
Example #3
Source File: test_simple_paths.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_all_simple_paths_cutoff(): G = nx.complete_graph(4) paths = nx.all_simple_paths(G, 0, 1, cutoff=1) assert_equal(set(tuple(p) for p in paths), {(0, 1)}) paths = nx.all_simple_paths(G, 0, 1, cutoff=2) assert_equal(set(tuple(p) for p in paths), {(0, 1), (0, 2, 1), (0, 3, 1)})
Example #4
Source File: test_simple_paths.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_cutoff_zero(): G = nx.complete_graph(4) paths = nx.all_simple_paths(G, 0, 3, cutoff=0) assert_equal(list(list(p) for p in paths), []) paths = nx.all_simple_paths(nx.MultiGraph(G), 0, 3, cutoff=0) assert_equal(list(list(p) for p in paths), [])
Example #5
Source File: test_simple_paths.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_all_simple_paths_corner_cases(): assert_equal(list(nx.all_simple_paths(nx.empty_graph(2), 0, 0)), []) assert_equal(list(nx.all_simple_paths(nx.empty_graph(2), 0, 1)), []) assert_equal(list(nx.all_simple_paths(nx.path_graph(9), 0, 8, 0)), [])
Example #6
Source File: test_simple_paths.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_all_simple_paths_multigraph(): G = nx.MultiGraph([(1, 2), (1, 2)]) paths = nx.all_simple_paths(G, 1, 2) assert_equal(set(tuple(p) for p in paths), {(1, 2), (1, 2)})
Example #7
Source File: test_simple_paths.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_all_simple_paths_multigraph_with_cutoff(): G = nx.MultiGraph([(1, 2), (1, 2), (1, 10), (10, 2)]) paths = nx.all_simple_paths(G, 1, 2, cutoff=1) assert_equal(set(tuple(p) for p in paths), {(1, 2), (1, 2)})
Example #8
Source File: test_simple_paths.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_all_simple_paths_directed(): G = nx.DiGraph() nx.add_path(G, [1, 2, 3]) nx.add_path(G, [3, 2, 1]) paths = nx.all_simple_paths(G, 1, 3) assert_equal(set(tuple(p) for p in paths), {(1, 2, 3)})
Example #9
Source File: test_simple_paths.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def hamiltonian_path(G, source): source = arbitrary_element(G) neighbors = set(G[source]) - set([source]) n = len(G) for target in neighbors: for path in nx.all_simple_paths(G, source, target): if len(path) == n: yield path
Example #10
Source File: test_simple_paths.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_cutoff_zero(): G = nx.complete_graph(4) paths = nx.all_simple_paths(G, 0, 3, cutoff=0) assert_equal(list(list(p) for p in paths), []) paths = nx.all_simple_paths(nx.MultiGraph(G), 0, 3, cutoff=0) assert_equal(list(list(p) for p in paths), [])
Example #11
Source File: test_simple_paths.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_source_missing(): G = nx.Graph() nx.add_path(G, [1, 2, 3]) paths = list(nx.all_simple_paths(nx.MultiGraph(G), 0, 3))
Example #12
Source File: test_simple_paths.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_target_missing(): G = nx.Graph() nx.add_path(G, [1, 2, 3]) paths = list(nx.all_simple_paths(nx.MultiGraph(G), 1, 4)) # Tests for shortest_simple_paths
Example #13
Source File: test_simple_paths.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_shortest_simple_paths(): G = cnlti(nx.grid_2d_graph(4, 4), first_label=1, ordering="sorted") paths = nx.shortest_simple_paths(G, 1, 12) assert_equal(next(paths), [1, 2, 3, 4, 8, 12]) assert_equal(next(paths), [1, 5, 6, 7, 8, 12]) assert_equal([len(path) for path in nx.shortest_simple_paths(G, 1, 12)], sorted([len(path) for path in nx.all_simple_paths(G, 1, 12)]))
Example #14
Source File: test_simple_paths.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_all_simple_paths_cutoff(): G = nx.complete_graph(4) paths = nx.all_simple_paths(G, 0, 1, cutoff=1) assert_equal(set(tuple(p) for p in paths), {(0, 1)}) paths = nx.all_simple_paths(G, 0, 1, cutoff=2) assert_equal(set(tuple(p) for p in paths), {(0, 1), (0, 2, 1), (0, 3, 1)})
Example #15
Source File: test_simple_paths.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_all_simple_paths_multigraph_with_cutoff(): G = nx.MultiGraph([(1, 2), (1, 2), (1, 10), (10, 2)]) paths = list(nx.all_simple_paths(G, 1, 2, cutoff=1)) assert_equal(len(paths), 2) assert_equal(set(tuple(p) for p in paths), {(1, 2), (1, 2)})
Example #16
Source File: test_simple_paths.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_all_simple_paths_multigraph(): G = nx.MultiGraph([(1, 2), (1, 2)]) paths = nx.all_simple_paths(G, 1, 1) assert_equal(paths, []) nx.add_path(G, [3, 1, 10, 2]) paths = list(nx.all_simple_paths(G, 1, 2)) assert_equal(len(paths), 3) assert_equal(set(tuple(p) for p in paths), {(1, 2), (1, 2), (1, 10, 2)})
Example #17
Source File: test_simple_paths.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_all_simple_paths_directed(): G = nx.DiGraph() nx.add_path(G, [1, 2, 3]) nx.add_path(G, [3, 2, 1]) paths = nx.all_simple_paths(G, 1, 3) assert_equal(set(tuple(p) for p in paths), {(1, 2, 3)})
Example #18
Source File: test_simple_paths.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_all_simple_paths_source_target(): G = nx.path_graph(4) paths = nx.all_simple_paths(G, 1, 1) assert_equal(paths, [])
Example #19
Source File: test_simple_paths.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_all_simple_paths_with_two_targets_inside_cycle_emits_two_paths(): G = nx.cycle_graph(3, create_using=nx.DiGraph()) G.add_edge(1, 3) paths = nx.all_simple_paths(G, 0, [2, 3]) assert_equal(set(tuple(p) for p in paths), {(0, 1, 2), (0, 1, 3)})
Example #20
Source File: test_simple_paths.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_all_simple_paths_ignores_cycle(): G = nx.cycle_graph(3, create_using=nx.DiGraph()) G.add_edge(1, 3) paths = nx.all_simple_paths(G, 0, 3) assert_equal(set(tuple(p) for p in paths), {(0, 1, 3)})
Example #21
Source File: test_simple_paths.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_all_simple_paths_with_two_targets_in_line_emits_two_paths(): G = nx.path_graph(4) paths = nx.all_simple_paths(G, 0, [2, 3]) assert_equal(set(tuple(p) for p in paths), {(0, 1, 2), (0, 1, 2, 3)})
Example #22
Source File: test_simple_paths.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_all_simple_paths_with_two_targets_cutoff(): G = nx.path_graph(4) G.add_edge(2, 4) paths = nx.all_simple_paths(G, 0, [3, 4], cutoff=3) assert_equal(set(tuple(p) for p in paths), {(0, 1, 2, 3), (0, 1, 2, 4)})
Example #23
Source File: test_simple_paths.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_digraph_all_simple_paths_with_two_targets_emits_two_paths(): G = nx.path_graph(4, create_using=nx.DiGraph()) G.add_edge(2, 4) paths = nx.all_simple_paths(G, 0, [3, 4]) assert_equal(set(tuple(p) for p in paths), {(0, 1, 2, 3), (0, 1, 2, 4)})
Example #24
Source File: test_simple_paths.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_all_simple_paths_with_two_targets_emits_two_paths(): G = nx.path_graph(4) G.add_edge(2, 4) paths = nx.all_simple_paths(G, 0, [3, 4]) assert_equal(set(tuple(p) for p in paths), {(0, 1, 2, 3), (0, 1, 2, 4)})
Example #25
Source File: test_simple_paths.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_all_simple_paths(): G = nx.path_graph(4) paths = nx.all_simple_paths(G, 0, 3) assert_equal(set(tuple(p) for p in paths), {(0, 1, 2, 3)})
Example #26
Source File: dag.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def root_to_leaf_paths(G): """Yields root-to-leaf paths in a directed acyclic graph. `G` must be a directed acyclic graph. If not, the behavior of this function is undefined. A "root" in this graph is a node of in-degree zero and a "leaf" a node of out-degree zero. When invoked, this function iterates over each path from any root to any leaf. A path is a list of nodes. """ 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) # TODO In Python 3, this would be better as `yield from ...`. return chaini(starmap(all_paths, product(roots, leaves)))
Example #27
Source File: test_simple_paths.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_shortest_simple_paths(): G = cnlti(nx.grid_2d_graph(4, 4), first_label=1, ordering="sorted") paths = nx.shortest_simple_paths(G, 1, 12) assert_equal(next(paths), [1, 2, 3, 4, 8, 12]) assert_equal(next(paths), [1, 5, 6, 7, 8, 12]) assert_equal([len(path) for path in nx.shortest_simple_paths(G, 1, 12)], sorted([len(path) for path in nx.all_simple_paths(G, 1, 12)]))
Example #28
Source File: test_simple_paths.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_target_missing(): G = nx.Graph() G.add_path([1,2,3]) paths = list(nx.all_simple_paths(nx.MultiGraph(G),1,4)) # Tests for shortest_simple_paths
Example #29
Source File: test_simple_paths.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_source_missing(): G = nx.Graph() G.add_path([1,2,3]) paths = list(nx.all_simple_paths(nx.MultiGraph(G),0,3))
Example #30
Source File: test_simple_paths.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_cutoff_zero(): G = nx.complete_graph(4) paths = nx.all_simple_paths(G,0,3,cutoff=0) assert_equal(list(list(p) for p in paths),[]) paths = nx.all_simple_paths(nx.MultiGraph(G),0,3,cutoff=0) assert_equal(list(list(p) for p in paths),[])