我想進一步處理這個問題(Find total of second variable related to the distance of route from get.shortest.paths())。當使用newcost變量找到'最短'路徑時,如何得到節點之間的距離矩陣?使用get.shortest.paths()的第二個變量的距離矩陣()
(我對igraph的體驗非常有限)。
df2 = rbind(c(234,235,21.6,75),
c(234,326,11.0,35),
c(235,241,14.5,78),
c(326,241,8.2,98),
c(241,245,15.3,75),
c(234,245,38.46,65))
df2 = as.data.frame(df2)
names(df2) = c("start_id","end_id","newcost","distance")
require(igraph)
g2 <- graph.data.frame(df2, directed=FALSE)
tmp2 = shortest.paths(g2,weights=E(g2)$newcost)
tmp2 #this gives the matrix of newcost-weighted shortest distances
在那裏我可以使用幫助是如何找到的所有路徑,比如使用optimal.path <- get.shortest.paths
,並使用sum(E(g2, path = optimal.path)$distance)
創建距離
的矩陣是什麼,我真的很喜歡的是距離的所有節點對的EdgeList都,如:
startid endid shortestdist
234 235 75
234 245 208
什麼是棘手的這個問題是newcost是用來尋找最短路徑,但我要的是另一個變量的總和 - 節點對之間的各最短路徑上的距離變量。
所以,你想要'tmp2'中的所有數據,但你只是想以不同的格式?您需要每個開始/結束組合的行,而不是矩陣? – MrFlick
@MrFlick:謝謝你的提問。我想要的是不同的。 tmp2中的數據是'newcost'總和,它給出了加權路徑長度。我想要的是最短路徑的距離變量的總和。 – user2627717
那些你剛纔展示的兩個例子恰好相等? – MrFlick