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
条件过滤后的子图

原文地址:https://www.cnblogs.com/wushaogui/p/9204797.html