【问题标题】:How to extract the sequence of the edges responsible for the diameter in a igraph object in R?如何提取负责R中igraph对象直径的边序列?
【发布时间】:2018-09-15 23:43:57
【问题描述】:

假设有一个igraph 对象:

library(igraph)
library(igraphdata)
data(karate)


get_diameter(karate)
+ 6/34 vertices, named, from 4b458a1:
[1] Actor 16 John A   Actor 20 Mr Hi    Actor 6  Actor 17
farthest_vertices(karate)
$vertices
+ 2/34 vertices, named, from 4b458a1:
[1] Actor 16 Actor 17

$distance
[1] 13

我很容易找到彼此相距最远的顶点。如何提取连接顶点 Actor 16Actor 17 的边序列?

【问题讨论】:

  • ?get.all.shortest.paths 也许?

标签: r igraph social-networking


【解决方案1】:

实际上get_diameter 确实为您提供了两个节点之间的边序列:

[1] Actor 16 John A   Actor 20 Mr Hi    Actor 6  Actor 17

在函数文档中:

get_diameter 返回具有实际直径的路径。如果直径长度的最短路径有很多条,则返回找到的第一个。

【讨论】:

  • 这是一个顶点列表,而不是边列表。两个顶点之间可以有多个边。此外,一些函数需要一个实际的边列表,而不仅仅是边连接的顶点
  • 好吧,我的错,所以你是对的,shortest_path 就是答案!
【解决方案2】:

所以diameter 是图中最长的shortest_path。幸运的是,shortest_paths 函数可以为您提供节点向量 (vpath) 和边向量 (epath)。输出是一个列表,如果要获取边向量,则需要设置参数output = 'bothoutput='epath'

shortest_paths(karate, 'Actor 16', 'Actor 17', output = 'both')$epath[[1]]

或者,如果你想动态地解决这个问题:

farthest_vertices(karate)$vertices %>% 
  {shortest_paths(karate, .[1], .[2], output = 'both')} %>%
  .$epath %>%
  .[[1]]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    相关资源
    最近更新 更多