本文主要是介绍【NetWorkX】Graph基础操作,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
转载
更全面的NetworkX中文使用手册,请收藏:NetworkX中文使用手册
在NetworkX库中,我们总是先生成一个Graph对象,然后对其进行操作,下面通过两个简单的实例来看看我们能用NetworkX干什么。
目录
1. 读写Graph数据
2. lollipop网络特性探索
3. NetWorkX图、边、节点等相关方法
1. 读写Graph数据
import sys
import matplotlib.pyplot as plt
import networkx as nxG = nx.grid_2d_graph(5, 5) #生成2d网格图像#打印邻接列表
for line in nx.generate_adjlist(G):print(line)
#将邻接列表写到文件中
nx.write_edgelist(G, path="grid.edgelist", delimiter=":")
#读取邻接列表文件
H = nx.read_edgelist(path="grid.edgelist", delimiter=":")nx.draw(H)
plt.show()
(0, 0) (1, 0) (0, 1)
(0, 1) (1, 1) (0, 2)
(0, 2) (1, 2) (0, 3)
(0, 3) (1, 3) (0, 4)
(0, 4) (1, 4)
(1, 0) (2, 0) (1, 1)
(1, 1) (2, 1) (1, 2)
(1, 2) (2, 2) (1, 3)
(1, 3) (2, 3) (1, 4)
(1, 4) (2, 4)
(2, 0) (3, 0) (2, 1)
(2, 1) (3, 1) (2, 2)
(2, 2) (3, 2) (2, 3)
(2, 3) (3, 3) (2, 4)
(2, 4) (3, 4)
(3, 0) (4, 0) (3, 1)
(3, 1) (4, 1) (3, 2)
(3, 2) (4, 2) (3, 3)
(3, 3) (4, 3) (3, 4)
(3, 4) (4, 4)
(4, 0) (4, 1)
(4, 1) (4, 2)
(4, 2) (4, 3)
(4, 3) (4, 4)
(4, 4)
2. lollipop网络特性探索
import matplotlib.pyplot as plt
from networkx import nxG = nx.lollipop_graph(4, 6)
pathlengths = []print("source vertex {target:length, }")
for v in G.nodes():spl = dict(nx.single_source_shortest_path_length(G, v)) #计算每个顶点的单源最短路径print('{} {} '.format(v, spl))for p in spl:pathlengths.append(spl[p])print('')
print("average shortest path length %s" % (sum(pathlengths) / len(pathlengths)))# 统计每种长度出现次数
dist = {}
for p in pathlengths:if p in dist:dist[p] += 1else:dist[p] = 1print('')
print("length #paths")
#打印每种长度出现次数
verts = dist.keys()
for d in sorted(verts):print('%s %d' % (d, dist[d]))
#打印棒棒糖网络的其他特性,如半径、直径等
print("radius: %d" % nx.radius(G))
print("diameter: %d" % nx.diameter(G))
print("eccentricity: %s" % nx.eccentricity(G))
print("center: %s" % nx.center(G))
print("periphery: %s" % nx.periphery(G))
print("density: %s" % nx.density(G))nx.draw(G, with_labels=True)
plt.show()
source vertex {target:length, }
0 {0: 0, 1: 1, 2: 1, 3: 1, 4: 2, 5: 3, 6: 4, 7: 5, 8: 6, 9: 7}
1 {1: 0, 0: 1, 2: 1, 3: 1, 4: 2, 5: 3, 6: 4, 7: 5, 8: 6, 9: 7}
2 {2: 0, 0: 1, 1: 1, 3: 1, 4: 2, 5: 3, 6: 4, 7: 5, 8: 6, 9: 7}
3 {3: 0, 0: 1, 1: 1, 2: 1, 4: 1, 5: 2, 6: 3, 7: 4, 8: 5, 9: 6}
4 {4: 0, 5: 1, 3: 1, 6: 2, 0: 2, 1: 2, 2: 2, 7: 3, 8: 4, 9: 5}
5 {5: 0, 4: 1, 6: 1, 3: 2, 7: 2, 0: 3, 1: 3, 2: 3, 8: 3, 9: 4}
6 {6: 0, 5: 1, 7: 1, 4: 2, 8: 2, 3: 3, 9: 3, 0: 4, 1: 4, 2: 4}
7 {7: 0, 6: 1, 8: 1, 5: 2, 9: 2, 4: 3, 3: 4, 0: 5, 1: 5, 2: 5}
8 {8: 0, 7: 1, 9: 1, 6: 2, 5: 3, 4: 4, 3: 5, 0: 6, 1: 6, 2: 6}
9 {9: 0, 8: 1, 7: 2, 6: 3, 5: 4, 4: 5, 3: 6, 0: 7, 1: 7, 2: 7} average shortest path length 2.86length #paths
0 10
1 24
2 16
3 14
4 12
5 10
6 8
7 6
radius: 4
diameter: 7
eccentricity: {0: 7, 1: 7, 2: 7, 3: 6, 4: 5, 5: 4, 6: 4, 7: 5, 8: 6, 9: 7}
center: [5, 6]
periphery: [0, 1, 2, 9]
density: 0.26666666666666666
3. NetWorkX图、边、节点等相关方法
在【NetWorkX实例(1)】基础操作一文中,介绍了networkx中图的生成,下面就介绍一下图、边、节点等相关方法。
Graph相关方法 | 功能 |
---|---|
degree(G[, nbunch,weight) | 返回单个节点或nbunch节点的度视图。 |
degree_histogram(G) | 返回每个度的频率列表 |
density(G) | 返回图的密度 |
info(G[, n]) | 为图G或节点n打印简短的信息摘要 |
Nodes相关方法 | 功能 |
---|---|
nodes(G) | 返回图节点的迭代器 |
number_of_nodes(G) | 返回图中节点的数量 |
neighbors(G, n) | 返回图G中和节点n相连的节点列表 |
all_neighbors(graph, node) | 返回节点node在graph中的所有节点 |
non_neighbors(graph, node) | 返回图中不是节点node的邻居节点的所有节点 |
common_neighbors(G, u, v) | 返回节点u,v在图G中的共同邻居节点 |
Edges相关方法 | 功能 |
---|---|
edges(G[, nbunch]) | 返回与nbunch节点关联的edge视图 |
number_of_edges(G) | 返回图G中的边数量 |
density(G) | 返回图的密度 |
non_edges(graph) | 返回图中不存在的边 |
Self loops相关方法 | 功能 |
---|---|
Attributes相关方法 | 功能 |
---|---|
这篇关于【NetWorkX】Graph基础操作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!