【问题标题】:retrieve integer name in shortest.path function of igraph在 igraph 的 shortest.path 函数中检索整数名称
【发布时间】:2019-08-13 06:22:26
【问题描述】:

首先,我有一个用 igraph 生成的最短路径矩阵(最短路径) 当我想用“get.shortest.path”检索节点名称时,它只会给我带来每列的数量而不是它的名称:

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

【问题讨论】:

    标签: igraph


    【解决方案1】:

    这对我有用:

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

    【讨论】:

      【解决方案2】:

      有一个稍微简单一点的解决方案,不使用 lapply:

      paths <- get.shortest.paths(g, 5, 1)
      V(g)$name[paths$vpath[[1]]]
      

      【讨论】:

      • @OsmanKhalid 抱歉,应该是“路径”,而不是“p”。我已经修好了。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2012-05-12
      • 2011-04-23
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 2023-03-14
      相关资源
      最近更新 更多