我正在尝试将一个有向(无环)图分割成方向连接的路径,依靠连通性:
当我测试弱连接和强连接子图时,我得到了以下结果:Weak connectivity :
['16', '17'], ['3', '41', '39', '42']
Strong connectivity :
['17'], ['16'], ['39'], ['41'], ['3'], ['42']
我理解了弱连通性结果,但是对于强连通性结果并不理解,因为我期望得到3个子图:[16, 17]、[42, 39]和[3, 41, 39]。
我错过了什么?为什么只有单节点列表?如何获得预期结果?
下面是代码:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.DiGraph()
G.add_edges_from([('16', '17'), ('3', '41'), ('41', '39'), ('42', '39')])
print("Weak connectivity : ")
for subgraph in (G.subgraph(c).copy() for c in nx.weakly_connected_components(G)) :
print(subgraph.nodes)
print("Strong connectivity : ")
for subgraph in (G.subgraph(c).copy() for c in nx.strongly_connected_components(G)) :
print(subgraph.nodes)
nx.draw_networkx(G, pos=nx.circular_layout(G))
plt.show()