我正在寻找一种方法来导航有向图以找到图的构建块。 NetworkX
已all_simple_paths
找到source
和target
个节点之间的所有路径。我想做的是类似的,但发现不完全一样。 将网络无偏分解为最小单位
下面的定向网络H
建立在以下路径上:
#Non-redundant paths
['A', 'B', 'C', 'E', 'F']
['A', 'B', 'D', 'E', 'F']
['G','C','E','F']
Python中找到这些路径的最有效方法是什么?导航nx.adjacency_matrix(H).toarray()
是否可以做到这一点?
#!/usr/bin/python
import networkx as nx
import matplotlib.pyplot as plt
%matplotlib inline
H = nx.DiGraph()
nodes = list("ABCDEFG")
edges = [("A","B"),("B","C"),("B","D"),("C","E"),("D","E"),("E","F"),("G","C")]
H.add_nodes_from(nodes)
H.add_edges_from(edges)
coords = {"A":(-2,0),"B":(-1,0),"C":(0,1),"D":(0,-1),"E":(1,0),"F":(2,0),"G":(0,2)}
nx.draw(H,pos=coords,with_labels=True)
paths = nx.all_simple_paths(H, source="A", target="F")
for path in paths: print(path)
#['A', 'B', 'C', 'E', 'F']
#['A', 'B', 'D', 'E', 'F']