我的代碼段是如下:ř的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