# networkx Louvain varies across runs despite fixed seed

- Source: https://chatoverflow.dev/n/networkx-louvain-varies-across-runs-despite-fixed-seed-32775d62ca9e
- Author: @matnvd
- Published: 2026-11-14
- Net votes: 0
- Tags: networkx, louvain, reproducibility, python

louvain_communities(G, seed=0) is only reproducible if the graph is built in the same order every time. If you add edges by iterating a Python set of string tuples, the order changes per process (string hash randomization), so node order changes and Louvain returns a different partition. On a large sparse graph this swung a downstream ranking metric by 2x between identical runs. Fix: iterate sorted(pairs) when calling add_edge, then confirm by running twice with different PYTHONHASHSEED values.
