{"id":5987,"library":"leidenalg","title":"Leiden Algorithm for Community Detection","description":"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.","status":"active","version":"0.11.0","language":"en","source_language":"en","source_url":"https://github.com/vtraag/leidenalg","tags":["graph","community-detection","network-analysis","clustering","igraph"],"install":[{"cmd":"pip install leidenalg","lang":"bash","label":"Install using pip"}],"dependencies":[{"reason":"leidenalg relies on python-igraph for graph data structures and functionality.","package":"python-igraph"}],"imports":[{"note":"Commonly aliased as 'la' in examples and documentation.","symbol":"leidenalg","correct":"import leidenalg as la"},{"note":"Required for creating and manipulating graph objects that leidenalg operates on. Commonly aliased as 'ig'.","symbol":"igraph","correct":"import igraph as ig"},{"note":"Partition types are classes within the leidenalg module and are directly imported for clarity when used as arguments to `find_partition`.","wrong":"import leidenalg.ModularityVertexPartition","symbol":"ModularityVertexPartition","correct":"from leidenalg import ModularityVertexPartition"}],"quickstart":{"code":"import igraph as ig\nimport leidenalg as la\n\n# Create a sample graph (e.g., Erdos-Renyi graph)\nG = ig.Graph.Erdos_Renyi(100, 0.1)\n\n# Find a partition using the Leiden algorithm with Modularity\npartition = la.find_partition(G, la.ModularityVertexPartition)\n\nprint(f\"Number of nodes: {G.vcount()}\")\nprint(f\"Number of edges: {G.ecount()}\")\nprint(f\"Number of communities: {len(partition)}\")\nprint(f\"Modularity: {partition.modularity}\")\n# To get community membership for each node:\n# print(partition.membership)","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade Python environment to 3.9 or higher. (e.g., `conda create -n myenv python=3.9`)","message":"Python 3.7 and 3.8 are no longer supported. Ensure you are using Python 3.9 or newer.","severity":"breaking","affected_versions":"<=0.10.x"},{"fix":"Experiment with different `resolution_parameter` values (e.g., between 0.4 and 2.0, or plot modularity/number of communities against resolution) to find a suitable partitioning for your specific data. Pass it as a keyword argument to the partition type, e.g., `la.find_partition(G, la.CPMVertexPartition, resolution_parameter=0.5)`.","message":"The default `resolution_parameter` (γ = 1) in `leidenalg` might not be optimal for all networks, potentially leading to an inappropriate number of communities.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For source installation, refer to the official documentation for detailed instructions on compiling the underlying C++ libraries. Using `conda-forge` for installation often handles these dependencies automatically.","message":"Direct installation with `pip install leidenalg` usually works due to binary wheels. However, if building from source, `leidenalg` depends on the C++ core libraries `igraph` (>= 1.0.0) and `libleidenalg` (>= 0.12), which may require manual compilation and specific build tools (e.g., `build-essential`, `autoconf`, `automake`, `flex`, `bison` on Ubuntu) if pre-built binaries are not available or suitable.","severity":"gotcha","affected_versions":"All versions (when installing from source)"},{"fix":"For `Scanpy`, run `sc.pp.neighbors()` to construct a k-nearest-neighbor graph before `sc.tl.leiden()`. Generally, ensure your data is converted to an `igraph.Graph` object.","message":"When integrating with other libraries like `Scanpy`, ensure the graph object is correctly prepared before calling the Leiden algorithm. Leiden operates on graph structures, not raw data matrices.","severity":"gotcha","affected_versions":"All versions"},{"fix":"When using `Scanpy`, specify `flavor=\"igraph\"` and `n_iterations=2` in `sc.tl.leiden()` to align with future defaults. For direct `leidenalg` usage, continue as normal.","message":"The `Scanpy` library, which often uses `leidenalg`, is shifting its default backend for Leiden clustering towards `igraph`'s internal implementation instead of `leidenalg`'s. While `leidenalg` is still the default for now, direct reliance on `leidenalg` via `Scanpy`'s API might change in future `Scanpy` versions.","severity":"deprecated","affected_versions":"Scanpy >= 1.12.0 (future defaults)"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}