I am trying to implement tarjan's algorithm for practice. I decided to generate a random graph to give as input to the algorithm, adding one edge at a time.
I generated the random graph and saved it in a file, as shown below
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()
The output that I got in the test.edgelist file is something like this.
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,{}
How ever, in the tarjan's algorithm that I've implemented, the input is in the format
add_edge(1,2)
add_edge(2,3)
....
I wish to use the randomly generated graph in a loop to give as input.
How do i not get the {}? Also, if there is some better way to implement this, please help, since, for a massive dataset, it'll be difficult to save it an a single list(add_edge() adds the edge to a list)