![](/img/trans.png)
[英]igraph in R: error with get.shortest.paths but not with shortest.paths
[英]How to get the a list of the edge attributes when performing shortest.paths in igraph (R)
我需要计算图形中两个顶点之间最短路径的edge属性的乘积。
例如:
data<-as.data.frame(cbind(c(1,2,3,4,5,1),c(4,3,4,5,6,5),c(0.2,0.1,0.5,0.7,0.8,0.2)))
G<-graph.data.frame(data, directed=FALSE)
set.edge.attribute(G, "V3", index=E(G), data$V3)
如果我根据属性计算出最短路径,则有两个可能性,第一个告诉我这些步骤:
get.shortest.paths (G, 2, 6, weights=E(G)$V3)
2 3 4 1 5 6
第二个告诉我沿路径的属性总和。
shortest.paths (G, 2, 6, weights=E(G)$V3)
1.8
由于我需要制作产品,因此我需要在路径的节点之间有一个边属性的向量。 在此示例中,我应该得到0.8 0.2 0.2 0.5 0.1,其乘积将为0.0016。 谁能建议我该怎么做?
使用get.shortest.paths
的output
参数:
library(igraph)
data <- data.frame(from =c(1, 2, 3, 4, 5, 1),
to =c(4, 3, 4, 5, 6, 5),
weight=c(0.2,0.1,0.5,0.7,0.8,0.2))
G <- graph.data.frame(data, directed=FALSE)
esp26 <- get.shortest.paths(G, 2, 6, output="epath")[[1]]
esp26
# [1] 2 3 1 6 5
prod(E(G)$weight[esp26])
# [1] 0.0016
plot(G, edge.label=paste("Id:", 1:ecount(G), "\n", "W:",
E(G)$weight, sep=""))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.