0

I am confused and trying to figure out how to put this graph data into an adjacency matrix.

This is some sample input from a text file:

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

This is how the 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 would be some sort of multidimensional array but I'm lost on how to translate the input into one. Any help would be appreciated.

I am working in python.

Thanks!

Brejuro
  • 3,421
  • 8
  • 34
  • 61

1 Answers1

0

It is pretty straight forward,

for x x1,y1 x2,y2 input

it means that

there is edge between x and x1 with value y1 and edge x and x2 with value y2.

To parse it, do this

1: use space separator to split the dataset.
2: First element in the resulting array is your node(x).
3: for i (1 to n), do a split by comma(,), which will give an array of two elements, with first element being node(x1) and second being distance y(1).
4: To set the value in matrix for each pair, mat[x,x1]=y1

Edit It appears that your graph is a undirected garph. So for each pair, you have to do

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

Max
  • 9,100
  • 25
  • 72
  • 109