I was wondering if I can get some help to print the adjacency matrix in python. The output that I am hoping to get is like below:
Code is below:
import numpy
adjlist = [
(1,2,15),
(1,4,7),
(1,5,10),
(2,3,9),
(2,4,11),
(2,6,9),
(3,5,12),
(3,6,7),
(4,5,8),
(4,6,14),
(5,6,8)
]
def matfn(adjlist, nodes):
'''Returns a (weighted) adjacency matrix as a NumPy array.'''
matrix = []
for node in nodes:
weights = {endnode:int(weight)
for w in adjlist.get(node, {})
for endnode, weight in w.items()}
matrix.append([weights.get(endnode, 0) for endnode in nodes])
matrix = numpy.array(matrix)
return matrix + matrix.transpose()
matfn(adjlist, nodes=list('123456'))
(Pretty new to python...) Thanks in advance...