Answer first
NetworkX’s label_propagation_communities is non-deterministic by design. Repeated calls on the same graph can return different partitions. Reproducibility is achievable, but the supported mechanism depends on the NetworkX version you are running.
Confirmed facts
- The implementation processes nodes in random order and breaks label ties randomly. That is the source of run-to-run variation.
- In the 2.6-2.x series the public API did not expose a seed argument. The algorithm used the global Python
random module for node ordering and tie-breaking.
- Because of global state, unrelated random calls elsewhere in the program can change the result.
Likely explanation, not confirmed
Label propagation is an asynchronous heuristic. Different node iteration orders and random tie-breaking lead to different local optima. The variation is therefore expected behavior of the heuristic, not a bug.
Recommended pattern for this case
Version determines the correct call. The exact NetworkX version is the one missing diagnostic detail that changes the recommendation.
- Check your version:
import networkx as nx; print(nx.__version__)
- If your release accepts a seed argument for label propagation:
- Call with an explicit seed and keep graph construction identical.
- Run twice with the same seed and verify partitions are identical.
- If your release does not expose a seed argument, as in the original 2.6 introduction:
- Set the global RNG immediately before the call:
import random; random.seed(0)
- Avoid any other random calls between seeding and the community call.
- Keep node ordering stable by constructing the graph in the same way each run.
Verification steps:
- Run
label_propagation_communities twice with the same seed/graph and compare the frozenset partitions for equality.
- Run with different seeds and observe different partitions to confirm randomness is the driver.
Version sensitivity and caveats
Seeding reduces variation but does not guarantee identical output across NetworkX or Python versions due to internal implementation changes. Deterministic output means reproducible, not correct or unique; label propagation is a heuristic.
Do not assume global random.seed is sufficient in all releases. Some releases use an internal RNG instance. That is why the exact NetworkX version is needed to give a precise, supported call signature.