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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With