Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent given adjacency matrix as undirected weighted graph in matlab?

For below given matrix,how it can be represented as undirected weighted graph G(V,E,W) where V is set of vertices,E is set of edges and W is set of weights.

  4     2     3     1     4
  2     2     3     1     4
  2     3     3     1     4
  1     2     3     1     4
  4     2     3     1     5

Source code i tried:

%table2 is given matrix
bg = biograph(table2,[],'ShowArrows','off','ShowWeights','on');
h = view(bg);

Is this the right way to represent undirected weighted graph for my matrix?Iam getting 2 edges between same 2 verteces.Just like this

enter image description here

I got this output for your suggested code enter image description here

Iam having 2 adjacency matrices.I have to perform below 2 operations:

1.I have to make 2 graphs from these 2 adjacency matrices.

2.Then i have to match vertices of 2 graphs depending on weights of edges in graph.

  4     2     3     1     4
  2     2     3     1     4
  2     3     3     1     4
  1     2     3     1     4
  4     2     3     1     5

 4     1     3     2     4
 1     1     2     3     4
 3     1     3     2     4
 2     1     3     2     4
 4     1     3     2     5

The way iam going is right or wrong?Please suggest me

like image 464
prashanth Avatar asked May 11 '14 06:05

prashanth


1 Answers

To get only one edge between each vertex, you only want a triangular matrix.

If you try something like this:

table1 = (table2 + table2') - triu((table2 + table2')) 
table1 =
     0     0     0     0     0
     4     0     0     0     0
     5     6     0     0     0
     2     3     4     0     0
     8     6     7     5     0

bg = biograph(table1,[],'ShowArrows','off','ShowWeights','on');
h = view(bg);

Now I have assumed you want to sum up the weights from both edges. I.e. the weight of the edge between 1-2 equal the weight of the edge from 1->2 + 2->1, from your original matrix. enter image description here

like image 84
Stewie Griffin Avatar answered Sep 21 '22 19:09

Stewie Griffin