I have the following txt file:
31 1262 -1
531 13930 1
531 16139 -1
531 17567 1
531 20653 1
The first column show the starting nodes and the second column shows the ending nodes. I want to create a graph like
graph = {'0': set(['1', '2']),
'1': set(['0', '3', '4']),
'2': set(['0']),
'3': set(['1']),
'4': set(['2', '3'])}
using the input from text file. I saved the first column in start_vertex = [] and the second column in end_vertex = []. I wrote the code below but i cant save the graph.
for i in range(len(lines)):
v = start_vertex[i]
if v == start_vertex[i]:
graph = graph + {'v' : set(['end_vertex[i]'])}
And the full code is here:
file = open("network.txt","r")
lines = file.readlines()
start_vertex = []
end_vertex = []
sign = []
graph = []
for x in lines:
start_vertex.append(x.split('\t')[0])
end_vertex.append(x.split('\t')[1])
sign.append(x.split('\t')[2])
file.close()
def dfs(graph, start, visited = None):
if visited is None:
visited = set()
visited.add(start)
print(start)
for next in graph[start] - visited:
dfs(graph, next, visited)
return visited
for i in range(len(lines)):
v = start_vertex[i]
if v == start_vertex[i]:
graph = graph + {'v' : set(['end_vertex[i]'])}