我正在尝试实现 tarjan 的算法进行练习。我决定生成一个随机图作为算法的输入,一次添加一条边。
我生成了随机图并保存在一个文件中,如下图
from networkx import *
import sys
import matplotlib.pyplot as plt
n = 10 # 10 nodes
m = 20 # 20 edges
G = gnm_random_graph(n, m)
# print the adjacency list to a file
try:
nx.write_edgelist(G, "test.edgelist", delimiter=',')
except TypeError:
print "Error in writing output to random_graph.txt"
fh = open("test.edgelist", 'rb')
G = nx.read_adjlist(fh)
fh.close()
我在 test.edgelist 文件中得到的输出是这样的。
0,4,{}
0,5,{}
0,6,{}
1,8,{}
1,3,{}
1,4,{}
1,7,{}
2,8,{}
2,3,{}
2,5,{}
3,8,{}
3,7,{}
4,8,{}
4,9,{}
5,8,{}
5,9,{}
5,7,{}
6,8,{}
6,7,{}
7,9,{}
但是,在我实现的 tarjan 算法中,输入格式为
add_edge(1,2)
add_edge(2,3)
....
我希望在循环中使用随机生成的图作为输入。
我怎么没有得到 {}?另外,如果有更好的方法来实现这一点,请帮忙,因为对于海量数据集,很难将其保存为单个列表(add_edge() 将边缘添加到列表中)