理想情况下,使用 Igraph 在 r 中的最短路径(总成本和步骤) - 简单的错误



我有一个表,第一列中有起始节点、结束节点和朝该方向移动的成本。 这是一个方向的,你不能向后移动。 这些都是组合。 似乎我犯了一个明显的错误..

mygraph = structure(list(V1 = c(1L, 1L, 2L, 2L, 2L, 3L, 3L, 4L, 4L, 4L, 
5L, 5L, 6L, 7L, 8L, 9L), V2 = c(3L, 4L, 3L, 4L, 5L, 7L, 6L, 6L, 
8L, 9L, 7L, 8L, 10L, 10L, 10L, 10L), V3 = c(3L, 2L, 4L, 2L, 4L, 
2L, 3L, 4L, 2L, 5L, 2L, 2L, 3L, 4L, 2L, 3L)), .Names = c("V1", 
"V2", "V3"), class = "data.frame", row.names = c(NA, -16L))
names(mygraph)=c('start','end','cost')
library(igraph)
mygraph = graph.data.frame(mygraph, directed=T) # I think this is right?
plot(mygraph) #looks completely wrong???
help=get.shortest.paths(mygraph,1,10)   #I'm doing something wrong want to see route and total cost of going from node 1-10
help
如果将

下一行中的最后一个术语"成本"更改为weight,它将生成正确的解决方案。

names(mygraph) <- c('start', 'end', 'weight')

发生这种情况是因为函数get.shortest.paths()使用属性weight(而不是cost)作为边的成本。

最新更新