I need to calculate the product of the edges attributes of the shortest path between two vertices in my graph.
For example:
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
The second tell me the sum of the attribute along the path.
shortest.paths (G, 2, 6, weights=E(G)$V3)
1.8
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?
Use the output
argument of get.shortest.paths
:
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=""))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.