Skip to main content

Module community

Module community 

Source
Expand description

Community detection, modularity, partition comparison and hierarchical random graphs (igraph_community.h, igraph_hrg.h).

Community detection clusters the vertices of a network into groups that are densely connected internally and sparsely connected with each other. A clustering is represented, as everywhere in igraph, by a membership vector: membership[v] is the community id of vertex v, ids being numbered from zero. Hierarchical methods also return a dendrogram as a list of merges (a, b): the i-th merge joins the clusters a and b into a new cluster with id n + i (n being the number of vertices).

Good introductions to the topic are S. Fortunato, Community Detection in Graphs, Physics Reports 486 (2010) and S. Fortunato and D. Hric, Community Detection in Networks: A User Guide, Physics Reports 659 (2016).

§Examples

Two 5-cliques joined by a single edge are split into their cliques by every reasonable method:

use igraph::prelude::*;
use igraph::community::compare_communities;

let mut edges = vec![];
for base in [0, 5] {
    for i in 0..5 {
        for j in i + 1..5 {
            edges.push((base + i, base + j));
        }
    }
}
edges.push((0, 5));
let g = Graph::from_edges(&edges, 10, false).unwrap();

let louvain = g.community_multilevel(None, 1.0).unwrap();
assert_eq!(louvain.membership, vec![0, 0, 0, 0, 0, 1, 1, 1, 1, 1]);
assert!((louvain.modularity() - 0.4524).abs() < 1e-4);

let greedy = g.community_fastgreedy(None).unwrap();
let nmi = compare_communities(&louvain.membership, &greedy.membership,
                              CommunityComparison::Nmi).unwrap();
assert!((nmi - 1.0).abs() < 1e-12);

A typical workflow on a real network: Zachary’s karate club (from Graph::famous) is clustered with a seeded Leiden run, and the communities are then collapsed into a weighted “community graph” with Graph::contract_vertices, whose modularity for the singleton partition is, by definition, the modularity of the clustering:

use igraph::prelude::*;
use igraph::community::{LeidenObjective, LeidenOptions};

let karate = Graph::famous("Zachary").unwrap();
rng::seed(42).unwrap();
let leiden = karate
    .community_leiden_simple(None, LeidenObjective::Modularity,
                             &LeidenOptions::default().with_iterations(None))
    .unwrap();
assert_eq!(leiden.nb_clusters, 4);
assert!((leiden.quality - 0.4198).abs() < 1e-4); // the known optimum

let mut quotient = karate.clone();
quotient.contract_vertices(&leiden.membership).unwrap();
let singletons: Vec<i64> = (0..leiden.nb_clusters as i64).collect();
let q = quotient.modularity(&singletons, None, 1.0, false).unwrap();
assert!((q - leiden.quality).abs() < 1e-12);

§Provided functionality

Rust APIC functionWhat
Graph::community_multileveligraph_community_multilevelLouvain modularity optimization
Graph::community_leidenigraph_community_leidenLeiden, raw vertex weights
Graph::community_leiden_simpleigraph_community_leiden_simpleLeiden with modularity / CPM / ER objective
Graph::community_fastgreedyigraph_community_fastgreedyClauset–Newman–Moore greedy agglomeration
Graph::community_walktrapigraph_community_walktraprandom-walk distances (Pons–Latapy)
Graph::community_edge_betweennessigraph_community_edge_betweennessGirvan–Newman divisive method
Graph::community_eb_get_mergesigraph_community_eb_get_mergesdendrogram from an edge removal order
Graph::community_leading_eigenvector, Graph::community_leading_eigenvector_withigraph_community_leading_eigenvectorNewman’s spectral method
Graph::community_spinglassigraph_community_spinglassReichardt–Bornholdt Potts model
Graph::community_spinglass_singleigraph_community_spinglass_singlecommunity of a single vertex
Graph::community_label_propagationigraph_community_label_propagationlabel propagation
Graph::community_infomapigraph_community_infomapmap equation (Infomap)
Graph::community_fluid_communitiesigraph_community_fluid_communitiesfluid communities
Graph::community_voronoiigraph_community_voronoiVoronoi partitioning
Graph::community_optimal_modularityigraph_community_optimal_modularityexact maximum modularity (GLPK)
Graph::modularityigraph_modularitymodularity of a partition
Graph::modularity_matrixigraph_modularity_matrixthe modularity matrix B
Graph::corenessigraph_corenessk-core decomposition
Graph::trussnessigraph_trussnessk-truss decomposition
community_to_membership, Dendrogram::cutigraph_community_to_membershipcut a dendrogram
le_community_to_membershipigraph_le_community_to_membershipcut a leading eigenvector dendrogram
reindex_membershipigraph_reindex_membershipmake community ids contiguous
compare_communitiesigraph_compare_communitiesVI, NMI, split-join, (adjusted) Rand
split_join_distanceigraph_split_join_distanceboth projection distances
Graph::hrg_fit, Graph::hrg_refitigraph_hrg_fitfit a hierarchical random graph by MCMC
Graph::hrg_consensus, Graph::hrg_predictigraph_hrg_consensus, igraph_hrg_predictconsensus dendrogram, missing link prediction
Hrg::create, Hrg::sizeigraph_hrg_create, igraph_hrg_sizebuild / inspect an Hrg
Hrg::sample, Hrg::sample_many, Graph::hrg_gameigraph_hrg_sample, igraph_hrg_sample_many, igraph_hrg_gamesample graphs from an HRG
Graph::from_hrg_dendrogram, Hrg::dendrogramigraph_from_hrg_dendrograman HRG dendrogram as a tree

Not bound: igraph_hrg_resize (plain storage sizing: Hrg::create and Graph::hrg_fit size the model themselves, and resizing leaves the new tree entries uninitialized, which Hrg::sample would then read as vertex ids), and igraph_hrg_init / igraph_hrg_destroy, which the constructors and Drop of Hrg call.

Randomized methods (Louvain, Leiden, label propagation, spinglass, Infomap, fluid communities, Voronoi generator ties, HRG, …) draw from the random number generator of the calling thread (each thread has its own): call rng::seed first for reproducible results, or run them with a dedicated generator through Rng::scoped.

§See also

Structs§

Clustering
A flat partition of the vertices together with its modularity.
Dendrogram
A hierarchical clustering (dendrogram) together with the modularity of each of its levels and the best cut.
EdgeBetweennessCommunities
Result of the Girvan–Newman algorithm, see Graph::community_edge_betweenness.
EdgeRemovalMerges
Result of Graph::community_eb_get_merges.
Hrg
A hierarchical random graph (HRG) model (igraph_hrg_t), after Clauset, Moore and Newman.
HrgConsensus
Result of Graph::hrg_consensus.
HrgPrediction
Result of Graph::hrg_predict.
Infomap
Result of Infomap, see Graph::community_infomap.
InfomapOptions
Options of Graph::community_infomap.
LabelPropagationOptions
Options of Graph::community_label_propagation.
LeadingEigenvector
Result of Newman’s leading eigenvector method, see Graph::community_leading_eigenvector.
LeadingEigenvectorStep
The state passed to the callback of Graph::community_leading_eigenvector_with after each eigenvector computation.
Leiden
Result of the Leiden algorithm, see Graph::community_leiden.
LeidenOptions
Options of Graph::community_leiden and Graph::community_leiden_simple.
Multilevel
Result of the multi-level (Louvain) algorithm, see Graph::community_multilevel.
Spinglass
Result of the spinglass method, see Graph::community_spinglass.
SpinglassOptions
Options of Graph::community_spinglass and Graph::community_spinglass_single, with the defaults suggested by igraph.
SpinglassSingle
Result of Graph::community_spinglass_single.
Voronoi
Result of Voronoi partitioning, see Graph::community_voronoi.

Enums§

LeadingEigenvectorEvent
One step of the history of Graph::community_leading_eigenvector (igraph_leading_eigenvector_community_history_t).
LeidenObjective
Objective function optimized by Graph::community_leiden_simple (igraph_leiden_objective_t).

Functions§

community_to_membership
Cuts a dendrogram after steps merges, returning the membership vector and the size of each community.
compare_communities
Compares two partitions of the same set with the given measure.
le_community_to_membership
Applies steps merges of a leading eigenvector dendrogram to an initial partition, returning the new membership vector and community sizes.
reindex_membership
Relabels a membership vector in place so that community ids are 0..k, and returns the mapping from new to old ids (its length k is the number of communities).
split_join_distance
The two projection distances between two partitions, whose sum is the split-join distance of van Dongen.