【问题标题】:Find total of second variable related to the distance of route from get.shortest.paths()从 get.shortest.paths() 中找到与路线距离相关的第二个变量的总数
【发布时间】:2012-05-31 04:35:18
【问题描述】:

我发现以下问题 (Find distance of route from get.shortest.paths()) 很有帮助,但想更进一步。我在数据框中添加了一列,我想获得与最小 newcost 路径相关的“总距离”。

在我使用的 igraph / R 代码下方。

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")

df2

require(igraph)
g2 <- graph.data.frame(df2, directed=FALSE)

tkplot(g2)

(tmp2 = get.shortest.paths(g2, from='234', to='245',weights=E(g2)$newcost))

# This gives the shortest path based on $newcost
V(g2)[tmp2[[1]]]

我想回答的问题是与这条最短路径相关的距离是多少。最短路径的答案是 34.5,(手动计算)与这条路径相关的距离是 208。

了解有关如何自动获取此距离的一些提示。

谢谢!约赫姆

# What is the distance related to the min newcost?

【问题讨论】:

    标签: r routing dijkstra igraph


    【解决方案1】:

    这为您提供最佳路径的边缘:

    optimal.path <- V(g2)[tmp2[[1]]] 
    E(g2, path = optimal.path)
    # Edge sequence:
    #               
    # [1] 326 -- 234
    # [3] 241 -- 326
    # [4] 245 -- 241
    

    (请注意,它们不是按照您的最佳路径的顺序出现,而是出现在图表的定义中g2。)

    这给了你总距离:

    sum(E(g2, path = optimal.path)$distance)
    # [1] 208
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多