python - How to read this graph input and put into an adjacency matrix? -


i confused , trying figure out how put graph data adjacency matrix.

this sample input text file:

0 1,28 3,33 1 2,10 4,44 2 3,50 3 4,30 4  

this how matrix should look

  0   1   2   3   4 0 inf 28  inf 33  inf 1 28  inf 10  inf 44  2 inf 10  inf 50  inf  3 33  inf 50  30  inf   4 inf 44  inf inf inf 

this sort of multidimensional array i'm lost on how translate input one. appreciated.

i working in python.

thanks!

it pretty straight forward,

for x x1,y1 x2,y2 input

it means

there edge between x , x1 value y1 , edge x , x2 value y2.

to parse it, this

1: use space separator split dataset.
2: first element in resulting array node(x).
3: (1 n), split comma(,), give array of 2 elements, first element being node(x1) , second being distance y(1).
4: set value in matrix each pair, mat[x,x1]=y1

edit appears graph undirected garph. each pair, have

mat[x,x1]=y1
mat[x1,x]=y1


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -