小书匠Graph图论

有时候graph建好后,我们并不清除该graph内节点的,边的信息,这就需要调用函数去查看了.

目录:


注意:如果代码出现找不库,请返回第一个教程,把库文件导入.

6.查看Graph的信息

6.1查看graph内节点,边的数量

  1. #生成graph 

  2. G=nx.path_graph(8) 

  3. nx.draw(G,with_labels=True) 

  4. plt.axis('on') 

  5. plt.xticks([]) 

  6. plt.yticks([]) 

  7. plt.show() 


  8. #查看节点和边的情况 

  9. print('number of nodes',G.number_of_nodes()) 

  10. print('number of edges',G.number_of_edges()) 


例子图

输出:

  1. number of nodes 8 

  2. number of edges 7 

6.2查看graph中的点,边

  1. #输出graph所有的点和边 

  2. print('all nodes of Graph',G.nodes()) 

  3. print('all edges of Graph',G.edges()) 

输出:

  1. all nodes of Graph [0, 1, 2, 3, 4, 5, 6, 7] 

  2. all edges of Graph [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7)] 

6.3查看某些节点的度

  1. #查看节点2和3的度 

  2. print('degree of some nodes',G.degree([2, 3])) 

输出:

  1. degree of some nodes [(2, 2), (3, 2)] 

6.4查看节点&边信息

  1. #设置一些节点信息 

  2. G.nodes[1]['room'] = 714 

  3. G.nodes[1]['color'] = 'b' 

  4. #设置一些边信息 

  5. G[1][2]['weight'] = 4.7 

  6. G[1][2]['color'] = "blue" 


  7. print('imformation of one nodes',G.nodes[1]) 

  8. print('imformation of all nodes',G.nodes.data()) 


  9. print('imformation of all nodes',G.edges.data()) #边不支持[x]这样的下标访问 

输出:

  1. imformation of one nodes {'room': 714, 'color': 'b'} 

  2. imformation of all nodes [(0, {}), (1, {'room': 714, 'color': 'b'}), (2, {}), (3, {}), (4, {}), (5, {}), (6, {}), (7, {})] 

  3. imformation of all nodes [(0, 1, {}), (1, 2, {'weight': 4.7, 'color': 'blue'}), (2, 3, {}), (3, 4, {}), (4, 5, {}), (5, 6, {}), (6, 

7, {})]

6.5遍历一个有权图

  1. #定义一个有权无向图 

  2. FG = nx.Graph() 

  3. FG.add_weighted_edges_from([(1, 2, 0.125), (1, 3, 0.75), (2, 4, 1.2), (3, 4, 0.375)]) 


  4. #遍历邻接矩阵 

  5. for n, nbrs in FG.adj.items(): 

  6. for nbr, eattr in nbrs.items(): 

  7. wt = eattr['weight'] 

  8. #权重小于0.5的输出 

  9. if wt < 0.5:  

  10. print('way1-(%d, %d, %.3f)' % (n, nbr, wt)) 


  11. #遍历所有边 

  12. for (u, v, wt) in FG.edges.data('weight'): 

  13. #权重小于0.5的输出 

  14. if wt < 0.5:  

  15. print('way2-(%d, %d, %.3f)' % (u, v, wt)) 

输出:

  1. way1-(1, 2, 0.125) 

  2. way1-(2, 1, 0.125) 

  3. way1-(3, 4, 0.375) 

  4. way1-(4, 3, 0.375) 

  5. way2-(1, 2, 0.125) 

最新文章

  1. Jvm --- 常用工具
  2. android基础(五)网络编程
  3. alphaBlend
  4. Windows如何修改MySQL用户root密码
  5. css常用效果总结
  6. js中eval() 方法的使用以及一些特殊的使用方式
  7. fbset 移植
  8. 0506--Scrum项目1.0
  9. discuz怎么根据连接知道调用的是什么模板页面
  10. SRM475 - SRM479(1-250pt,500pt)
  11. data mining,machine learning,AI,data science,data science,business analytics
  12. 梦游前端,JavaScript兼容性
  13. C++ 安全单例模式总结
  14. 【工具篇】.NET开发常用工具
  15. sql 查询 某字段 重复次数 最多的记录
  16. 【M2】软件工程终期总结报告——阅读作业
  17. c语言的重构、清理与代码分析图形化浏览工具: CScout
  18. zabbix3.0.4安装部署与SendEmail报警配置
  19. C#重点内容之:接口(interface)(一)网络初级示例
  20. 轻松搞定 easyui datagrid 二次加载的问题(转)

热门文章

  1. Istio旨在成为容器化微服务的网格管道
  2. SOS dp
  3. sqlserver 转化函数
  4. C++ 去掉字符串的首尾空格和全部空格
  5. android 错误解决
  6. 写在NOIP2018后
  7. vue组件间的数据传递
  8. vue 做的tabBar组件
  9. Subversion和TortoiseSVN,VisualSVN图文教程
  10. ios设备app作为蓝牙外设端