label_propagation

label_propagation#

graph_tool.inference.label_propagation(g, weight=None, b=None, fixed=None, mode='total', variant='dominance', max_niter=None)[git][source]#

Find communities with the label propagation heuristic.

Parameters:
gGraph

Graph to be used.

weightEdgePropertyMap (optional, default: None)

Edge weights. If not given, every edge has unit weight.

bVertexPropertyMap (optional, default: None)

Initial labels. Negative values mean that the vertex is unlabeled. If not given, every vertex starts with a distinct label.

fixedVertexPropertyMap (optional, default: None)

Boolean vertex property map marking the vertices whose labels do not change. Unlabeled vertices cannot be fixed.

modestr (optional, default: "total")

For directed graphs, the neighbors whose labels are taken into account: "in" (labels propagate along the edges), "out", or "total" (both).

variantstr (optional, default: "dominance")

Variant of the algorithm: "dominance", "retention", or "fast" (see below).

max_niterint (optional, default: None)

If given, the maximum number of sweeps over the vertices.

Returns:
bVertexPropertyMap

The labels, numbered consecutively from zero in order of first appearance (or -1 for the vertices that remain unlabeled).

See also

harmonic_function

Label prediction with the harmonic function

local_and_global_consistency

Local and global consistency method

ModularityState

Modularity maximization

minimize_blockmodel_dl

Inference of the stochastic block model

Notes

In the label propagation algorithm of Raghavan, Albert, and Kumara [raghavan-near-2007], every vertex starts with its own label (or with the label given in b) and repeatedly adopts a label with the largest total edge weight among its neighbors, i.e. one of the “dominant” labels

\[D_v = \underset{r}{\operatorname{arg\,max}}\sum_{u\in\partial v} w_{uv}\delta_{b_u,r},\]

where \(\partial v\) are the neighbors of \(v\) selected by mode and \(w_{uv}\) the edge weights (summed over parallel edges), with ties broken uniformly at random, until every vertex has a dominant label, \(b_v\in D_v\). The labels then define the communities. Unlabeled vertices do not take part in the vote (and remain unlabeled if none of their neighbors is labeled), and the labels of the vertices marked in fixed never change, which allows semi-supervised classification. Self-loops are ignored.

The variants differ in how the end is reached. With "dominance", sweeps that only check whether all labels are dominant, in which case the algorithm stops, alternate with update sweeps over the vertices in random order, in which every vertex takes a label from \(D_v\) uniformly at random (even if its current one is dominant), as in the original algorithm. With "retention", the vertices are updated in sweeps in random order, a vertex keeps its label if it is in \(D_v\), and otherwise takes one from \(D_v\) uniformly at random, and the algorithm stops after a sweep without changes. With "fast" [traag-large-2023], a queue of vertices, initially all vertices in random order, is processed instead of whole sweeps: every vertex taken from the queue gets a label from \(D_v\) uniformly at random, and, when its label changes to \(r\), those of its neighbors that are influenced by it, not fixed, not already in the queue, and with a label other than \(r\) are appended to the queue, which is several times faster. With max_niter, the fast variant stops after max_niter times as many vertex updates as there are vertices that are not fixed.

Every sweep takes time \(O(V + E)\), and the number of sweeps is typically small and grows slowly with the size of the network. The algorithm optimizes no objective function: different runs give different partitions, and it often finds a single community in networks without a strong structure, so that its results are mostly useful as a fast baseline. Principled alternatives, which also determine the number of groups, are given by minimize_blockmodel_dl() and the other inference methods in this module.

References

[raghavan-near-2007]

Usha Nandini Raghavan, Réka Albert and Soundar Kumara, “Near linear time algorithm to detect community structures in large-scale networks”, Physical Review E 76, 036106 (2007), DOI: 10.1103/PhysRevE.76.036106 [sci-hub, @tor], arXiv: 0709.2938

[traag-large-2023]

Vincent A. Traag and Lovro Šubelj, “Large network community detection by fast label propagation”, Scientific Reports 13, 2701 (2023), DOI: 10.1038/s41598-023-29610-z [sci-hub, @tor], arXiv: 2209.13338

Examples

>>> g = gt.collection.data["football"]
>>> b = gt.label_propagation(g)
>>> print(b.a.max() + 1, gt.modularity(g, b))
9 0.5988373...

For semi-supervised classification, we can fix the labels of two vertices of the karate club network and leave the others unlabeled, which gives the classes shown in the figure below:

>>> g = gt.collection.data["karate"]
>>> labels = g.new_vp("int", val=-1)
>>> labels[0] = 0
>>> labels[33] = 1
>>> fixed = g.new_vp("bool", vals=labels.a >= 0)
>>> b = gt.label_propagation(g, b=labels, fixed=fixed)
>>> fill = g.new_vp("string", vals=np.array(["#9bbedc", "#f28c8e"])[b.a])
>>> shape = g.new_vp("string", vals=np.where(fixed.a, "square", "circle"))
>>> gt.graph_draw(g, pos=g.vp.pos, vertex_fill_color=fill,
...               vertex_shape=shape, vertex_halo=fixed,
...               vertex_halo_color="#555555", vertex_halo_size=1.4,
...               output="label-propagation-karate.pdf")
<...>
../_images/label-propagation-karate.png

Labels of the vertices of the karate club network found by label propagation from the fixed labels of vertices 0 and 33 (squares with a dark halo). The vertex colors indicate the labels.#