# Remove singleton clusters using induced subgraph

**URL:** https://igraph.discourse.group/t/remove-singleton-clusters-using-induced-subgraph/1409
**Category:** Usage
**Tags:** Python, R
**Created:** [10 November 2022 06:20 UTC](https://igraph.discourse.group/t/remove-singleton-clusters-using-induced-subgraph/1409 "2022-11-10T06:20:48Z")
**Posts on this page:** 1
**Showing post:** 5

<div class="post-metadata">

### Author: ![KeesP](https://avatars.discourse-cdn.com/v4/letter/k/2bfe46/32.png) [@KeesP](https://igraph.discourse.group/u/KeesP)
#### Post date: [11 November 2022 12:55 UTC](https://igraph.discourse.group/t/remove-singleton-clusters-using-induced-subgraph/1409/5 "2022-11-11T12:55:42Z")

</div>

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

```auto
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:

```auto
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 

```

---

_[View the full topic](https://igraph.discourse.group/t/remove-singleton-clusters-using-induced-subgraph/1409)._
