Remove singleton clusters using induced subgraph

In addition to singletons, one could remove small weakly connected components as follows:

require(igraph)
set.seed(1L)
g <- sample_gnm(5000L, 10000L)
V(g)$names <- as.character(V(g))
min_wcc <- 2

## Calculate weakly connected components (see Wikipedia) and
## delete all weakly connected components with less then min_wcc members.
## Or feel free to apply a different fiter.
## Filter min_wcc equal to 2, will remove singletons only.
wcc <- components(g, mode='weak')
wcg <- groups(wcc)
sz <- table("Community sizes" = wcc$membership)
table("Community frequency, dimnames by size" = sz)
filter_clusters <- ( which(sz < min_wcc) )
V_to_delete <- unlist(wcg[filter_clusters])
g5 <- delete_vertices(g, V_to_delete)
table("Community sizes, indexed by community (after cleansing)" =
      components(g5, mode = "weak")$membership)

Output:

Community frequency, dimnames by size
   1    2 4893 
 101    3    1 

Community sizes, indexed by community (after cleansing)
   1    2    3    4 
4893    2    2    2 
1 Like