【发布时间】:2014-05-16 20:37:18
【问题描述】:
我想进一步回答这个问题 (Find total of second variable related to the distance of route from get.shortest.paths())。当使用 newcost 变量找到“最短”路径时,如何获得节点之间的距离矩阵?
(我对 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) 创建距离矩阵
我真正想要的是所有节点对的距离边缘列表,例如:
startid endid shortestdist
234 235 75
234 245 208
这个问题的棘手之处在于 newcost 用于找到最短路径,但我想要的是另一个变量的总和 - 节点对之间每条最短路径上的距离变量。
【问题讨论】:
-
那么,您想要
tmp2中的所有数据,但您只是想要不同的格式?您想要每个开始/结束组合的行而不是矩阵? -
@MrFlick:感谢您的提问。我想要的不一样。 tmp2 中的数据是“newcost”总和,它给出了加权路径长度。我想要的是最短路径的距离变量的总和。
-
你展示的两个例子恰好是相等的?
-
@MrFlick:该示例现已更正。谢谢!