Reputation: 17676
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.
Upvotes: 1
Views: 857
Reputation: 214957
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;%in%
to check if the cluster id are within the top three;Upvotes: 2
Reputation: 3729
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))
Upvotes: 1