Leiden Algorithm for Community Detection

0.11.0 · active · verified Tue Apr 14

leidenalg is a Python library that implements the Leiden algorithm for community detection in large networks. It is an extension of the Louvain algorithm, guaranteeing well-connected communities and often running faster. The library, currently at version 0.11.0, is actively maintained and provides Python bindings to a C++ core. It primarily relies on `python-igraph` for graph manipulation and can handle graphs with millions of nodes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple graph using `igraph` and then apply the Leiden algorithm using `leidenalg.find_partition` with the Modularity quality function to detect communities.

import igraph as ig
import leidenalg as la

# Create a sample graph (e.g., Erdos-Renyi graph)
G = ig.Graph.Erdos_Renyi(100, 0.1)

# Find a partition using the Leiden algorithm with Modularity
partition = la.find_partition(G, la.ModularityVertexPartition)

print(f"Number of nodes: {G.vcount()}")
print(f"Number of edges: {G.ecount()}")
print(f"Number of communities: {len(partition)}")
print(f"Modularity: {partition.modularity}")
# To get community membership for each node:
# print(partition.membership)

view raw JSON →