1
votes

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.

2

2 Answers

2
votes

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;
1
votes

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))