graph構建完成后,對graph的連通等屬性進行分析.
目錄:
注意:如果代碼出現找不庫,請返回第一個教程,把庫文件導入.
8.對圖進行分析
強連通:有向圖中任意兩點v1、v2間存在v1到v2的路徑(path)及v2到v1的路徑。
弱聯通:將有向圖的所有的有向邊替換為無向邊,所得到的圖稱為原圖的基圖。如果一個有向圖的基圖是連通圖,則有向圖是弱連通圖。
8.1連通子圖
- #定義圖的節點和邊
- nodes=['0','1','2','3','4','5','a','b','c']
- edges=[('0','0',1),('0','1',1),('0','5',1),('0','5',2),('1','2',3),('1','4',5),('2','1',7),('2','4',6),('a','b',0.5),('b','c',0.5),('c','a',0.5)]
- #定義graph
- G = nx.Graph()
- G.add_nodes_from(nodes)
- G.add_weighted_edges_from(edges)
- #找到所有連通子圖
- print('connected_components of graph: ',list(nx.connected_components(G)))
- #顯示該graph
- nx.draw(G, with_labels=True, font_weight='bold')
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.show()
輸出:
- connected_components of graph: [{'a', 'b', 'c'}, {'4', '0', '5', '1', '2'}, {'3'}]

連通子圖例子
8.2弱聯通
- #定義graph
- G = nx.path_graph(4, create_using=nx.DiGraph())
- G.add_path([7, 8, 3])
- G.add_path([5, 6,9])
- #找出所有的弱連通圖
- for c in nx.weakly_connected_components(G):
- print(c)
-
- #由大到小的規模判斷弱連通子圖
- print([len(c) for c in sorted(nx.weakly_connected_components(G), key=len, reverse=True)])
- nx.draw(G, with_labels=True, font_weight='bold')
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.show()
輸出:
- {0, 1, 2, 3, 7, 8}
- {9, 5, 6}
- [6, 3]

弱聯通例子
8.3強連通
- G.clear()
- #定義圖
- G = nx.path_graph(4, create_using=nx.DiGraph())
- G.add_path([3, 8, 1])
- #找出所有的強連通子圖
- con = nx.strongly_connected_components(G)
- print(con,type(con),list(con))
- #顯示該圖
- nx.draw(G, with_labels=True, font_weight='bold')
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.show()
輸出:
- <generator object strongly_connected_components at 0x7fe0eefe9c50> <class 'generator'> [{8, 1, 2, 3}, {0}]

強連通例子
8.4子圖
- G.clear()
- #定義圖
- G = nx.DiGraph()
- G.add_path([5, 6, 7, 8])
- #抽取圖G的節點作為子圖
- sub_graph = G.subgraph([5, 6, 8])
- plt.subplots(1,2,figsize=(15,5))
- #畫原圖
- plt.subplot(121)
- nx.draw(G, with_labels=True, font_weight='bold')
- plt.title('原圖',fontproperties=myfont)
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- #畫子圖
- plt.subplot(122)
- nx.draw(sub_graph, with_labels=True, font_weight='bold')
- plt.title('子圖',fontproperties=myfont)
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.show()

子圖例子
8.5條件過濾
- #G.clear()
- #定義有向圖
- G = nx.DiGraph()
- road_nodes = {'a':{'id':1}, 'b':{'id':1}, 'c':{'id':3}, 'd':{'id':4}}
- road_edges = [('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'd')]
- G.add_nodes_from(road_nodes.items())
- G.add_edges_from(road_edges)
- #過濾函數
- def flt_func_draw():
- flt_func = lambda d: d['id'] != 1
- return flt_func
- plt.subplots(1,2,figsize=(15,5))
- #畫出原圖
- plt.subplot(121)
- nx.draw(G, with_labels=True, font_weight='bold')
- plt.title('過濾前',fontproperties=myfont)
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- #過濾原圖得到子圖
- flt_func = flt_func_draw()
- part_G = G.subgraph(n for n, d in G.nodes(data=True) if flt_func(d))
- #畫出子圖
- plt.subplot(122)
- nx.draw(part_G, with_labels=True, font_weight='bold')
- plt.title('過濾后',fontproperties=myfont)
- plt.axis('on')
- plt.xticks([])
- plt.yticks([])
- plt.show()

條件過濾后的子圖