Reputation: 101
I have this file that has this adjacency matrix:
a:g:w:Q:front:Z
0 2 0 9 12 0
2 0 15 2 0 0
0 15 0 7 2 0
9 2 7 0 3 5
12 0 2 3 0 2
0 0 0 5 2 0
From the matrix,we can see that a is not connected to a but is connected to g,Q,front In python I read the file and put the alphabets into a separate list and the numbers into a list of list.
verticeName=['a','g','w','Q', 'front','z']
adjacency_list=[[0 2 0 9 12 0],[ 2 0 15 2 0 0 ],[0 15 0 7 2 0],[9 2 7 0 3 5 ],[12 0 2 3 0 2],[0 0 0 5 2 0]]
But I want to read this into a dict format so that I can easily modify it later.How do change it to this:
graph = { "a" : ["g","Q","front"],
"g" : ["a", "W","Q"],
"w" : ["g", "Q", "front", ],
"Q" : ["a","g","w","front,"z"],
"front" : ["a", "w","Q","z"],
"z" :["Q"]
}
Basically the left side is the vertice and the right side is the vertices it connects to.
Upvotes: 0
Views: 651
Reputation: 939
This may help:
vertices_names = []
from collections import defaultdict
graph = defaultdict(list)
with open('your_file_name') as f:
for line_num, line in enumerate(f):
line = line.strip()
if line_num == 0:
# set cols names
vertices_names = line.split(':')
else:
for idx, each_number in enumerate(line.split(' ')):
if int(each_number) > 0:
append_to = vertices_names[line_num-1]
append_what = vertices_names[idx]
graph[append_to].append(append_what)
print("graph is:")
for key, value in graph.items():
print(key, ":", value)
For your given input, the output of the above code is:
graph is:
w : ['g', 'Q', 'front']
Q : ['a', 'g', 'w', 'front', 'Z']
Z : ['Q', 'front']
front : ['a', 'w', 'Q', 'Z']
g : ['a', 'w', 'Q']
a : ['g', 'Q', 'front']
As you asked in comments about the weight of every edge also being stored, so here it is:
vertices_names = []
from collections import defaultdict
graph = defaultdict(list)
with open('data') as f:
for line_num, line in enumerate(f):
line = line.strip()
if line_num == 0:
# set cols names
vertices_names = line.split(':')
else:
for idx, each_number in enumerate(line.split(' ')):
if int(each_number) > 0:
append_to = vertices_names[line_num-1]
append_what = (vertices_names[idx], int(each_number))
graph[append_to].append(append_what)
print("graph is:")
for key, value in graph.items():
print(key, ":", value)
Output of this updated code on your given input is:
graph is:
front : [('a', 12), ('w', 2), ('Q', 3), ('Z', 2)]
w : [('g', 15), ('Q', 7), ('front', 2)]
Q : [('a', 9), ('g', 2), ('w', 7), ('front', 3), ('Z', 5)]
a : [('g', 2), ('Q', 9), ('front', 12)]
Z : [('Q', 5), ('front', 2)]
g : [('a', 2), ('w', 15), ('Q', 2)]
Upvotes: 1
Reputation: 83
You can read names as you do now:
names = ['a', 'g', 'w', 'Q', 'front', 'z']
And then create dictionary of empty lists like this:
graph = {}
for name in names:
graph[name] = []
Then just read the file and add vertices to proper list in dictionary if there exists an edge:
for source in names:
line = read_line_from_file() # Not a real function
costs = [int(x) for x in line.split()]
for i in range(6):
if costs[i]:
graph[source].append(names[i])
I hope I got your question right.
Upvotes: 0