存储networkx生成的图的方法参考networkx手册:
https://networkx.github.io/documentation/stable/reference/readwrite/index.html
Adjacency List经过适当处理可以转换成metis可识别的格式。
先看graph的Adjacency List
import networkx as nx
import metis
import numpy as np
import matplotlib.pyplot as plt
#用random_graphs.barabasi_albert_graph(n, m)方法生成一个含有n个节点、每次加入m条边的BA无标度网络
BA = nx.random_graphs.barabasi_albert_graph(20, 1)
pos = nx.spring_layout(BA) # 图形的布局样式,这里是中心放射状
nx.draw(BA, pos, with_labels=True, node_size=30, node_color='red')
plt.show()
for line in nx.generate_adjlist(BA):
print(line)
0 1 2 4 18
1 3 5
2
3
4
5 6 7 8 9 10 13 14 15 17
6
7 11 12 19
8
9
10
11
12 16
13
14