Inferensys

Glossary

Graph-Based Clustering

An unsupervised method that partitions cells into groups by constructing a nearest-neighbor graph and applying community detection algorithms like Louvain or Leiden.
Knowledge engineer constructing knowledge base on laptop, document hierarchy visible, casual office setup.
DEFINITION

What is Graph-Based Clustering?

Graph-based clustering is an unsupervised machine learning technique that partitions data points into groups by representing them as nodes in a nearest-neighbor graph and applying community detection algorithms to identify densely connected subgraphs.

Graph-based clustering is an unsupervised learning method that partitions cells into distinct populations by first constructing a k-nearest neighbor (KNN) graph from a reduced-dimensional representation, such as PCA space. Each cell becomes a node, and edges connect cells with highly similar transcriptomic profiles. The core principle is that cells belonging to the same biological type will form dense, highly intra-connected neighborhoods within this graph structure, while distinct cell types remain sparsely connected.

After graph construction, community detection algorithms like the Louvain or Leiden algorithm are applied to optimize a modularity function, iteratively merging nodes to maximize the density of connections within communities versus between them. The Leiden algorithm is generally preferred for its guarantee of well-connected communities and faster convergence. This approach scales efficiently to millions of cells, robustly captures complex, non-spherical cluster topologies that density-based methods miss, and has become the standard clustering backbone in single-cell analysis frameworks like Seurat and Scanpy.

MECHANISMS

Key Features of Graph-Based Clustering

Graph-based clustering partitions cells by constructing a nearest-neighbor network and applying community detection. This approach excels at capturing complex, non-linear relationships in high-dimensional single-cell data.

01

Nearest-Neighbor Graph Construction

The foundational step where each cell is connected to its k most similar neighbors based on a distance metric (typically Euclidean distance in PCA space). This transforms the data into a graph object where nodes represent cells and edges represent transcriptomic similarity. The choice of k directly influences cluster resolution—smaller values detect fine substructure, while larger values produce broader groupings.

02

Shared Nearest Neighbor (SNN) Refinement

Raw k-NN graphs are refined into a Shared Nearest Neighbor graph to emphasize robust connections. The edge weight between two cells is calculated based on the overlap in their neighborhoods (Jaccard index). This step effectively denoises the graph by down-weighting spurious connections and strengthening edges between cells that share many common neighbors, improving cluster separation.

03

Louvain Community Detection

A greedy optimization algorithm that partitions the graph by iteratively moving nodes between communities to maximize modularity—a measure comparing edge density within communities versus a random null model. Louvain is computationally fast and widely implemented in tools like Seurat and Scanpy, though it can occasionally produce arbitrarily badly connected communities.

04

Leiden Algorithm

An improvement over Louvain that guarantees well-connected communities by adding an intermediate refinement phase after the initial partition. The Leiden algorithm runs through three phases: local moving, refinement, and network aggregation. This ensures that all clusters are internally connected subgraphs, avoiding the fragmentation artifacts sometimes observed with Louvain. It is now the default in Scanpy.

05

Resolution Parameter Tuning

A critical hyperparameter controlling the granularity of clustering. Higher resolution values produce more, smaller clusters; lower values yield fewer, larger clusters. Optimal resolution is often determined empirically by inspecting cluster stability, marker gene specificity, and biological plausibility. Tools like clustree help visualize cluster assignments across a range of resolutions.

06

Modularity Optimization

The mathematical objective function underlying Louvain and Leiden. Modularity quantifies the strength of division of a network into communities. It compares the fraction of edges within communities to the expected fraction if edges were distributed randomly. Maximizing this score identifies partitions where intra-community connections are denser than expected by chance, defining biologically meaningful cell groups.

GRAPH-BASED CLUSTERING IN SINGLE-CELL ANALYSIS

Frequently Asked Questions

Graph-based clustering is the dominant paradigm for partitioning cells in single-cell RNA sequencing studies. These FAQs address the core mechanisms, algorithmic choices, and practical considerations that immunology researchers and core facility directors encounter when moving from a count matrix to discrete cell populations.

Graph-based clustering is an unsupervised learning method that partitions cells into groups by first constructing a k-nearest neighbor (KNN) graph from a reduced dimensionality representation, then applying a community detection algorithm to identify densely connected modules. The process begins with a cell-by-cell distance matrix computed in PCA space. Each cell is connected to its k most similar neighbors, forming a shared nearest neighbor (SNN) graph where edge weights represent the overlap in neighborhoods. The Louvain or Leiden algorithm then optimizes a modularity function, iteratively merging cells into clusters that maximize intra-cluster connections while minimizing inter-cluster links. Unlike traditional k-means, graph-based clustering does not assume spherical cluster shapes and can capture complex, branching topologies characteristic of developmental trajectories and immune cell continua.

COMMUNITY DETECTION COMPARISON

Louvain vs. Leiden Algorithm

A technical comparison of the two primary modularity-based community detection algorithms used for partitioning single-cell nearest-neighbor graphs into biologically meaningful clusters.

FeatureLouvain AlgorithmLeiden Algorithm

Optimization Objective

Modularity maximization

Modularity maximization with refinement

Guarantees connected communities

Resolution parameter support

Iterative refinement step

Typical cluster quality

High

Higher

Speed on large graphs (>100K cells)

Fast

Comparable

Susceptibility to badly connected communities

Yes

No

Default in Scanpy (legacy)

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.