Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the biggest connected component in R igraph

Tags:

r

igraph

How do I get a subgraph of the the biggest component of a graph?

Say for example I have a graph g.

size_components_g <-clusters(g, mode="weak")$csize
size_components_g
#1 2 3 10 25 2 2 1

max_size <- max(size_components_g)
max_size
#25

So 25 is the biggest size.

I want to extract the component that has these 25 vertices. How do I do that?

like image 888
Kay Avatar asked Sep 03 '25 07:09

Kay


1 Answers

Well, detailed explanation of output value of any function in the R package could be found in its documentation. In this case igraph::clusters returns a named list where in csize sizes of clusters are stored while membership contains the cluster id to which each vertex belongs to.

g <- igraph::sample_gnp(20, 1/20)

components <- igraph::clusters(g, mode="weak")
biggest_cluster_id <- which.max(components$csize)

# ids
vert_ids <- V(g)[components$membership == biggest_cluster_id]

# subgraph
igraph::induced_subgraph(g, vert_ids)
like image 129
krashkov Avatar answered Sep 04 '25 19:09

krashkov