Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以R为单位的最大边缘重量(IGRAPHE)_R_Igraph_Shortest Path_Minimum Spanning Tree - Fatal编程技术网

以R为单位的最大边缘重量(IGRAPHE)

以R为单位的最大边缘重量(IGRAPHE),r,igraph,shortest-path,minimum-spanning-tree,R,Igraph,Shortest Path,Minimum Spanning Tree,我使用igraph(minimum.span.tree)找到了图的最小生成树(MST)。从这个MST中,我提取了一个带有权重的邻接矩阵。现在我想得到这个MST中最短路径的矩阵。使用最短路径很容易做到这一点。但我需要矩阵A,其中元素A(I,j)是从顶点I到j的最短路径中具有最大权重的边的权重。我不需要矩阵中最短路径的总长度,只需要最大边权重。 提前谢谢你的建议。比如说 A = matrix(c(0, 0.25, 0, 0, 0.25, 0, 0.5, 0, 0, 0.5, 0, 0.75, 0,0

我使用igraph(minimum.span.tree)找到了图的最小生成树(MST)。从这个MST中,我提取了一个带有权重的邻接矩阵。现在我想得到这个MST中最短路径的矩阵。使用最短路径很容易做到这一点。但我需要矩阵A,其中元素A(I,j)是从顶点I到j的最短路径中具有最大权重的边的权重。我不需要矩阵中最短路径的总长度,只需要最大边权重。 提前谢谢你的建议。比如说

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)
A=矩阵(c(0,0.25,0,0,0,0.25,0,0.5,0,0,0,0.5,0,0,0,0,0.75,0),nrow=4,ncol=4,byrow=TRUE)

mst对于节点i,j,我认为最短路径中的最大权重由下式给出:

max(E(g,path=get.shortest.paths(g,i,j)$vpath[[1]])$weight)
(基本上得到最短路径,得到边作为路径,找到最大权重

神奇地循环并形成一个矩阵

bigweight = function(g){Vectorize(function(i,j){ifelse(i==j,0,max(E(g,path=get.shortest.paths(g,i,j)$vpath[[1]])$weight))})}
然后,
bigweight
是一个函数生成函数……因此您可以执行以下操作:

> 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
它应该是i,j之间最短路径边上的最大权重矩阵

请注意,由于对称性而导致效率低下,您可能可以通过使用多个目标节点调用
get.shortest.paths
来提高效率,但我认为这是可行的,并且是测试的基准。一定要测试它

我的测试包括:

> 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

这表明,如果我将权重从2增加到1,那么它只会影响通过2到1的路径(示例图是1--2--3--4).

你能做一个可复制的例子吗?mst_graphgraph_adj不是一个距离矩阵,而是一个来自距离矩阵的图形。我在@spaced上面也添加了一个可复制的例子。我想你需要在节点i,j上循环,使用
get.shortest.path(g,i,j)
output=“epath”
然后在
$epath
组件上循环以获取路径上的边,并在边上取最大的
$weight