在igraph(R)中执行shortest.paths时如何获取边缘属性的列表 [英] How to get the a list of the edge attributes when performing shortest.paths in igraph (R)
问题描述
我需要计算图中两个顶点之间最短路径的edge属性的乘积.
I need to calculate the product of the edges attributes of the shortest path between two vertices in my graph.
例如:
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)
如果我根据属性计算出最短路径,则有两个可能性,第一个告诉我这些步骤:
If I calculate the shortest path according to the attribute I have two posibilities, the first tell me the steps:
get.shortest.paths (G, 2, 6, weights=E(G)$V3)
2 3 4 1 5 6
2 3 4 1 5 6
第二个告诉我路径上属性的总和.
The second tell me the sum of the attribute along the path.
shortest.paths (G, 2, 6, weights=E(G)$V3)
1.8
由于我需要制作产品,因此我需要在路径的节点之间具有边属性的向量.在此示例中,我应该得到0.8 0.2 0.2 0.5 0.1,其乘积将为0.0016. 谁能建议我该怎么做?
Since I need to make a product, I would need to have a vector of the edge attributes between the nodes of my path. In this example I should get 0.8 0.2 0.2 0.5 0.1, whose product would be 0.0016. Can anyone suggest me how to do it?
推荐答案
使用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=""))
这篇关于在igraph(R)中执行shortest.paths时如何获取边缘属性的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!