【问题标题】:igraph get ids of connected componentsigraph 获取连接组件的ID
【发布时间】:2017-01-15 18:33:34
【问题描述】:

如何访问igraph 中图的前3 个连通分量的ID?

c <- igraph::components(g, mode = 'weak')
which(c$membership == which.max(c$csize))

将给出最大的 和

which(c$membership == which.max(c$csize-1))

c$csize-1 相同的结果只会从所有值中减去 -1。

【问题讨论】:

    标签: r igraph


    【解决方案1】:

    您可以使用order 排序并找出前 3 个最大集群的成员资格,并使用%in% 检查顶点是否在其中之一内:

    which(c$membership %in% order(c$csize, decreasing = TRUE)[1:3])
    

    • order(c$csize, decreasing = TRUE) 给出了将size 按降序排序的索引(对应于集群ID);
    • c$membership 包含所有顶点的集群 ID;
    • 使用%in%检查集群id是否在前三名之内;

    【讨论】:

      【解决方案2】:

      您可以使用tail 提取前 3 个(就大小而言)组件,然后遍历这些值以获取组件的成员。

      top3 <- which(c$csize %in% tail(sort(c$csize),3) )
      sapply(top3, function(x) which(c$membership == x))
      

      【讨论】:

        猜你喜欢
        • 2015-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多