我的代码段是如下:ř的igraph最短距离
df = as.data.frame(rbind(
c("a","b",2),
c("b","d",2),
c("d","g",2),
c("g","j",8),
c("j","i",2),
c("i","f",6),
c("f","c",2),
c("c","a",4),
c("c","e",4),
c("e","h",2),
c("h","j",4),
c("e","g",1),
c("e","i",3),
c("e","b",7)
))
names(df) = c("start_node","end_node","dist")
# Convert this to "igraph" class
gdf <- graph.data.frame(df, directed=FALSE)
# Compute the min distances from 'a' to all other vertices
dst_a <- shortest.paths(gdf,v='a',weights=E(gdf)$dist)
# Compute the min distances from 'a' to 'j'
dst_a[1, which(V(gdf)$name == 'j')]
虽然它返回结果12,我需要得到在这种情况下应该是一个的最短路径 - B - d - 克 - 电子 - 我 - j。我试图使用get.shortest.paths(),但徒劳无功。
但是,也许OP只需要一个路径?无论如何,'get.shortest.paths()',AFAIK没有错。 – 2014-10-13 13:09:49
@GaborCsardi根本没有问题!但我认为OP对某个特定的路径感兴趣,而AFAIK可能不是'get.shortest.path()'(?)返回的解决方案。因此,我建议'get.all.shortest.paths()'。 – ddiez 2014-10-13 14:02:21