实际上,这就是
write_adjlist
定义为,以便按照您的需要编写文件。可以使用以下函数进行简单的解决:
def adj_list_to_file(G,file_name):
f = open('tst.txt', "w")
for n in G.nodes():
f.write(str(n) + ' ')
for neighbor in G.neighbors(n):
f.write(str(neighbor) + ' ')
f.write('\n')
N=5 #Number of nodes
v=2 #Number of neighbours
p=.1 #rewiring proba
G = nx.connected_watts_strogatz_graph(N,v,p)
nx.draw(G, with_labels= True)
plt.show()
adj_list_to_file(G.to_undirected(),"tst.txt")
文件输出为:
0 1 4
1 0 2
2 1 3
3 2 4
4 0 3