Algorithm

void labelPropagation(List[] graph, int[] label) {
  boolean changed = true;
  while (changed) {
    changed = false;
    for (int u : shuffle(vertices)) {
      Map count = new HashMap<>();
      for (int v : graph[u]) count.merge(label[v], 1, Integer::sum);
      int best = argmax(count);
      if (best != label[u]) { label[u] = best; changed = true; }
    }
  }
}
Advertisement

Complexity

O(V+E) per iteration. Typically converges in 5-10 iterations.

Advertisement

Non-determinism

Random vertex order + tie-breaking → different results per run. Run multiple times, take mode.

Applications

Huge social networks (Facebook, Twitter). Real-time community updates. Baseline for large-scale clustering.