问题
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 thesizein descending order;c$membershipcontains 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