igraph get ids of connected components

别等时光非礼了梦想. 提交于 2019-12-11 07:04:03

问题


How can I access the ids of the top3 connected components of a graph in igraph?

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

will give the largest and

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

the same result as c$csize-1 will just subtract -1 from all values.


回答1:


You can use order to sort and find out the memberships of the top 3 largest clusters and use %in% to check if vertices are within one of them:

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

  • order(c$csize, decreasing = TRUE) gives the index(which corresponds to the cluster id) that will sort the size in descending order;
  • c$membership contains the cluster id for all vertices;
  • use %in% to check if the cluster id are within the top three;



回答2:


You can extract the top 3 (in terms of size) components with tail and then loop over those values to get the members of the component.

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


来源:https://stackoverflow.com/questions/41664769/igraph-get-ids-of-connected-components

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!