来自NetworkX的有向图(DiGraph)中的所有非冗余路径。蟒蛇

时间:2015-11-20 23:53:20

标签: python matrix network-programming networkx redundancy

我正在寻找一种方法来导航有向图以找到图的构建块。 NetworkXall_simple_paths找到sourcetarget个节点之间的所有路径。我想做的是类似的,但发现不完全一样。 将网络无偏分解为最小单位

下面的定向网络H建立在以下路径上:

#Non-redundant paths
['A', 'B', 'C', 'E', 'F']
['A', 'B', 'D', 'E', 'F']
['G','C','E','F']

Python中找到这些路径的最有效方法是什么?导航nx.adjacency_matrix(H).toarray()是否可以做到这一点?

enter image description here

#!/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']

0 个答案:

没有答案