igraph 获取连接组件的 id

igraph get ids of connected components

如何访问 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。

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

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

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

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

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