首先,当我想用"get.shortest.path“返回节点名时,我有一个用its (最短路径)生成的最短路径矩阵,它只给出每个列的数目,而不是它的名称:
[,a] [,b] [,c] [,d] [,e] [,f] [,g] [,h] [,i] [,j]
[a,] 0 1 2 3 4 5 4 3 2 1
[b,] 1 0 1 2 3 4 5 4 3 2
[c,] 2 1 0 1 2 3 4 5 4 3
[d,] 3 2 1 0 1 2 3 4 5 4
[e,] 4 3 2 1 0 1 2 3 4 5
[f,] 5 4 3 2 1 0 1 2 3 4
[g,] 4 5 4 3 2 1 0 1 2 3
[h,] 3 4 5 4 3 2 1 0 1 2
[i,] 2 3 4 5 4 3 2 1 0 1
[j,] 1 2 3 4 5 4 3 2 1 0 然后:
get.shortest.paths(g, 5, 1) 答案是:
[[1]]
[1] 5 4 3 2 我要的是节点名,而不是它们的编号。有什么解决办法吗?我也查过vpath了。
发布于 2014-04-08 14:10:04
这对我来说很管用:
paths <- get.shortest.paths(g, 5, 1)$vpath
names <- V(g)$name
lapply(paths, function(x) { names[x] })发布于 2014-06-05 18:43:20
有一个稍微简单一些的解决方案,不使用lapply:
paths <- get.shortest.paths(g, 5, 1)
V(g)$name[paths$vpath[[1]]]https://stackoverflow.com/questions/22935225
复制相似问题