First, I have a shortest path matrix generated with igraph (shortest path) When I want to retreive the node names with "get.shortest.path" it just brings me the number of each column and not its name:

       [,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 

then:

get.shortest.paths(g, 5, 1)    

the answer is:

[[1]]
[1] 5 4 3 2    

I want the node names not their numbers. Is there any solution? I checked vpath, too.

有帮助吗?

解决方案

This does the trick for me:

paths <- get.shortest.paths(g, 5, 1)$vpath
names <- V(g)$name
lapply(paths, function(x) { names[x] })

其他提示

There is a slightly simpler solution that does not use lapply:

paths <- get.shortest.paths(g, 5, 1)
V(g)$name[paths$vpath[[1]]]
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top