NetworkX系列教程(7)-對graph進行分析


graph構建完成后,對graph的連通等屬性進行分析.

目錄:


注意:如果代碼出現找不庫,請返回第一個教程,把庫文件導入.

8.對圖進行分析

強連通:有向圖中任意兩點v1、v2間存在v1到v2的路徑(path)及v2到v1的路徑。
弱聯通:將有向圖的所有的有向邊替換為無向邊,所得到的圖稱為原圖的基圖。如果一個有向圖的基圖是連通圖,則有向圖是弱連通圖。

8.1連通子圖

  1. #定義圖的節點和邊 
  2. nodes=['0','1','2','3','4','5','a','b','c'] 
  3. 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)] 
  4.  
  5. #定義graph 
  6. G = nx.Graph() 
  7. G.add_nodes_from(nodes) 
  8. G.add_weighted_edges_from(edges) 
  9.  
  10. #找到所有連通子圖 
  11. print('connected_components of graph: ',list(nx.connected_components(G))) 
  12.  
  13. #顯示該graph 
  14. nx.draw(G, with_labels=True, font_weight='bold') 
  15. plt.axis('on') 
  16. plt.xticks([]) 
  17. plt.yticks([]) 
  18. plt.show() 

輸出:

  1. connected_components of graph: [{'a', 'b', 'c'}, {'4', '0', '5', '1', '2'}, {'3'}] 

png
連通子圖例子

8.2弱聯通

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

輸出:

  1. {0, 1, 2, 3, 7, 8} 
  2. {9, 5, 6} 
  3. [6, 3] 

png
弱聯通例子

8.3強連通

  1. G.clear() 
  2.  
  3. #定義圖 
  4. G = nx.path_graph(4, create_using=nx.DiGraph()) 
  5. G.add_path([3, 8, 1]) 
  6.  
  7. #找出所有的強連通子圖 
  8. con = nx.strongly_connected_components(G) 
  9. print(con,type(con),list(con)) 
  10.  
  11. #顯示該圖 
  12. nx.draw(G, with_labels=True, font_weight='bold') 
  13. plt.axis('on') 
  14. plt.xticks([]) 
  15. plt.yticks([]) 
  16. plt.show() 

輸出:

  1. <generator object strongly_connected_components at 0x7fe0eefe9c50> <class 'generator'> [{8, 1, 2, 3}, {0}] 

png
強連通例子

8.4子圖

  1. G.clear() 
  2.  
  3. #定義圖 
  4. G = nx.DiGraph() 
  5. G.add_path([5, 6, 7, 8]) 
  6. #抽取圖G的節點作為子圖 
  7. sub_graph = G.subgraph([5, 6, 8]) 
  8.  
  9. plt.subplots(1,2,figsize=(15,5)) 
  10. #畫原圖 
  11. plt.subplot(121) 
  12. nx.draw(G, with_labels=True, font_weight='bold') 
  13. plt.title('原圖',fontproperties=myfont) 
  14. plt.axis('on') 
  15. plt.xticks([]) 
  16. plt.yticks([]) 
  17.  
  18. #畫子圖 
  19. plt.subplot(122) 
  20. nx.draw(sub_graph, with_labels=True, font_weight='bold') 
  21. plt.title('子圖',fontproperties=myfont) 
  22. plt.axis('on') 
  23. plt.xticks([]) 
  24. plt.yticks([]) 
  25.  
  26. plt.show() 

png
子圖例子

8.5條件過濾

  1. #G.clear() 
  2.  
  3. #定義有向圖 
  4. G = nx.DiGraph() 
  5. road_nodes = {'a':{'id':1}, 'b':{'id':1}, 'c':{'id':3}, 'd':{'id':4}} 
  6. road_edges = [('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'd')] 
  7. G.add_nodes_from(road_nodes.items()) 
  8. G.add_edges_from(road_edges) 
  9.  
  10. #過濾函數 
  11. def flt_func_draw(): 
  12. flt_func = lambda d: d['id'] != 1 
  13. return flt_func 
  14.  
  15. plt.subplots(1,2,figsize=(15,5)) 
  16.  
  17. #畫出原圖 
  18. plt.subplot(121) 
  19. nx.draw(G, with_labels=True, font_weight='bold') 
  20. plt.title('過濾前',fontproperties=myfont) 
  21. plt.axis('on') 
  22. plt.xticks([]) 
  23. plt.yticks([]) 
  24.  
  25. #過濾原圖得到子圖 
  26. flt_func = flt_func_draw() 
  27. part_G = G.subgraph(n for n, d in G.nodes(data=True) if flt_func(d)) 
  28.  
  29. #畫出子圖 
  30. plt.subplot(122) 
  31. nx.draw(part_G, with_labels=True, font_weight='bold') 
  32. plt.title('過濾后',fontproperties=myfont) 
  33. plt.axis('on') 
  34. plt.xticks([]) 
  35. plt.yticks([]) 
  36.  
  37. plt.show() 

png
條件過濾后的子圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM