CoDeBetHe

Community detection with the Bethe-Hessian in sparse static and time-evolving graphs

JuliaPython Lorenzo Dall'Amico, Romain Couillet, Nicolas Tremblay
Code examples in

Community Detection with the Bethe Hessian (CoDeBetHe) is an efficient implementation of our spectral algorithms for community detection in sparse, heterogeneous and time-evolving graphs. It is available both as a Julia package (CoDeBetHe.jl) and as a pure-Python package (codebethe, NumPy and SciPy only, no Julia required). The two share the same functions and give the same results.

The package implements the algorithms of three papers:

  • Revisiting the Bethe-Hessian: improved community detection in sparse heterogeneous graphs (NeurIPS 2019);
  • A unified framework for spectral clustering in sparse graphs (JMLR 2021): the static algorithm, which also estimates the number of communities;
  • Community detection in sparse time-evolving graphs with a dynamical Bethe-Hessian (NeurIPS 2020): the dynamic algorithm.

It also contains generators of synthetic graphs with communities (the static and dynamic degree-corrected stochastic block models) and a collection of real-world datasets.

Note

Use the Python | Julia switch above any code example to choose your language. Your choice is remembered on every page.

Installation

pip install codebethe

Requires Python 3.9 or later; the only dependencies are NumPy, SciPy and scikit-learn.

pkg> add https://github.com/lorenzodallamico/CoDeBetHe.jl

Type ] in the Julia REPL to enter the package manager.

Quick start

from codebethe import community_detection, dynamic_community_detection

# static graph: A is a scipy.sparse adjacency matrix
res = community_detection(A)          # k is estimated if not given
res.ℓ, res.k, res.modularity          # labels (0-based), number of communities, modularity

# time-evolving graph: AT is a list of T sparse adjacency matrices
res = dynamic_community_detection(AT, η, k)
res.ℓ                                 # T x n array of labels
using CoDeBetHe

# static graph: A is a SparseMatrixCSC adjacency matrix
cluster = community_detection_optimal_BH(A)   # k is estimated if not given
cluster.ℓ, cluster.k, cluster.modularity      # labels (1-based), number of communities, modularity

# time-evolving graph: AT is a vector of T sparse adjacency matrices
cluster = dynamic_community_detection_BH(AT, η, k)
cluster.ℓ                                     # T x n matrix: cluster.ℓ[t, :] are the labels at time t

Python and Julia

The Python package is a faithful port of the Julia one, with a few differences:

  • labels are 0-based in Python (0, …, k-1) and 1-based in Julia (1, …, k);
  • Python adds the shorter aliases community_detection and dynamic_community_detection for community_detection_optimal_BH and dynamic_community_detection_BH;
  • Python results are dataclasses (StaticResult, DynamicResult), and the random functions accept a seed.

Contents

Citation

If you use CoDeBetHe, please cite the related articles:

@inproceedings{dallamico2019revisiting,
  title     = {Revisiting the Bethe-Hessian: improved community detection in sparse heterogeneous graphs},
  author    = {Dall'Amico, Lorenzo and Couillet, Romain and Tremblay, Nicolas},
  booktitle = {Advances in Neural Information Processing Systems},
  pages     = {4039--4049},
  year      = {2019}
}

@article{dallamico2021unified,
  title   = {A unified framework for spectral clustering in sparse graphs},
  author  = {Dall'Amico, Lorenzo and Couillet, Romain and Tremblay, Nicolas},
  journal = {Journal of Machine Learning Research},
  volume  = {22},
  number  = {217},
  pages   = {1--56},
  year    = {2021},
  url     = {http://jmlr.org/papers/v22/20-261.html}
}

@article{dallamico2020community,
  title   = {Community detection in sparse time-evolving graphs with a dynamical Bethe-Hessian},
  author  = {Dall'Amico, Lorenzo and Couillet, Romain and Tremblay, Nicolas},
  journal = {Advances in Neural Information Processing Systems},
  volume  = {33},
  year    = {2020}
}

CoDeBetHe is released under the GNU Affero General Public License v3.