-1

I have found a minimum spanning tree (MST) of a graph using igraph (minimum.spanning.tree). From this MST I extracted an adjacency matrix with weights. Now I want to get a matrix of the shortest paths in this MST. This is quite easily done using shortest.paths. But I need the matrix A, where the element A(i,j) is the weight of the edge with maximum weight in the shortest path from vertex i to j. I simply do not need the total length of the shortest path in the matrix but only the maximum edge weight. Thanks in advance for any advice. For example

A = matrix(c(0, 0.25, 0, 0, 0.25, 0, 0.5, 0, 0, 0.5, 0, 0.75, 0,0,0.75, 0),nrow=4,ncol=4,   byrow = TRUE)
mst<-graph.adjacency(A, mode=c("undirected"), weighted=TRUE)
shortest.paths(mst)

I do not need shortest.paths(mst) but only weight of the maximum edge in the corresponding shortest path.

  • Can you make a reproducible example? – Spacedman Jul 02 '14 at 11:34
  • mst_graph<-minimum.spanning.tree(graph_adj), where graph_adj is my distance matrix. If I type shortest.paths(mst_graph) I get a matrix of the lengths of all the shortest paths in my MST. But what I need is simillar matrix but with the values of the maximum weight of the edge in that shortest path. Thus every element of that matrix will be lower or equal to the corresponding element in the matrix of lengths of the shortest paths. – user3797363 Jul 02 '14 at 12:32
  • graph_adj is not a distance matrix but a graph from a distance matrix of course – user3797363 Jul 02 '14 at 12:45
  • I've added also a reproducible example above @Spacedman – user3797363 Jul 02 '14 at 13:53
  • I think you need to loop over nodes i,j, use `get.shortest.path(g,i,j)` with `output="epath"` and then loop over the `$epath` component to get the edges on the path and take the largest `$weight` over the edges. – Spacedman Jul 02 '14 at 14:07

1 Answers1

0

For nodes i, j, the max weight in the shortest path is, I think, given by:

max(E(g,path=get.shortest.paths(g,i,j)$vpath[[1]])$weight)

(basically get the shortest path, get the edges as a path, find the max weight.

To magically loop over and make a matrix...

bigweight = function(g){Vectorize(function(i,j){ifelse(i==j,0,max(E(g,path=get.shortest.paths(g,i,j)$vpath[[1]])$weight))})}

Then bigweight is a function-generating function.... So you can then do:

> outer(1:4,1:4,bigweight(mst))
     [,1] [,2] [,3] [,4]
[1,] 0.00 0.25 0.50 0.75
[2,] 0.25 0.00 0.50 0.75
[3,] 0.50 0.50 0.00 0.75
[4,] 0.75 0.75 0.75 0.00

which should be the matrix of largest weights over edges of shortest paths between i,j.

Note inefficiency due to symmetry and you can probably speed it up by calling get.shortest.paths with more than one destination node, but I think this works and is a benchmark for testing. Do test it.

My testing is as far as:

> E(mst)[[1]]$weight=99
> outer(1:4,1:4,bigweight(mst))
     [,1]  [,2]  [,3]  [,4]
[1,]    0 99.00 99.00 99.00
[2,]   99  0.00  0.50  0.75
[3,]   99  0.50  0.00  0.75
[4,]   99  0.75  0.75  0.00

which shows if I up the weight from 2 to 1 then it only affects the paths going through 2 to 1 (your sample graph is 1--2--3--4).

Spacedman
  • 92,590
  • 12
  • 140
  • 224