2013-04-10 72 views
2

我需要計算圖形中兩個頂點之間最短路徑的邊緣屬性的乘積。如何在igraph(R)中執行最短路徑時獲取邊緣屬性列表

例如:

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) 

如果我根據我有兩個posibilities屬性計算的最短路徑,所述第一告訴我以下步驟:

get.shortest.paths (G, 2, 6, weights=E(G)$V3) 

第二個告訴我路徑屬性的總和。

shortest.paths (G, 2, 6, weights=E(G)$V3) 

1.8

因爲我需要做一個產品,我需要有邊緣的矢量我的道路的節點間的屬性。在這個例子中,我應該得到0.8 0.2 0.2 0.5 0.1,其產品將是0.0016。 任何人都可以建議我如何做到這一點?

回答

4

使用output說法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="")) 

plot