label_propagation_communities random_state default change for reproducible partitions
0 reputation · 02 Jan 2021, 03:27 UTC
0 reputation · 02 Jan 2021, 03:27 UTC
NetworkX's label_propagation_communities partitions graphs by iteratively updating node labels, but its node visitation order is randomized. When random_state is left as None, repeated calls on the same graph can yield different community assignments, which complicates reproducible research workflows.
The documentation acknowledges this non-determinism and exposes a random_state parameter for seeding, yet it does not recommend a fixed default seed. This leaves an unresolved design boundary: whether the function should become deterministic by default—perhaps by seeding internally or fixing visitation order—or remain stochastic with reproducibility delegated entirely to the caller.
What is the intended compatibility boundary here? Should a future version change the default random_state to a fixed seed, or is stochastic behavior considered part of the API contract? How can users who need deterministic partitions across runs distinguish a supported configuration from an incidental side effect of the current implementation?
35275 reputation · 02 Jan 2021, 08:35 UTC
The current public API of networkx.algorithms.community.label_propagation.label_propagation_communities intentionally leaves random_state=None as the default. That means the visitation order of nodes is chosen by the underlying numpy.random generator each time the function is called, and the resulting community partition can differ between runs on the same graph.
random_state is None, the algorithm is nondeterministic.numpy.random.RandomState).42 or 2026-10-16).import networkx as nx
from networkx.algorithms.community import label_propagation_communities
G = nx.erdos_renyi_graph(100, 0.05, seed=123)
communities = list(label_propagation_communities(G, random_state=42))
numpy.random.RandomState instance, serialize its state (e.g., via np.save or pickle) and restore it before calling the community function.In short, the default nondeterministic behaviour is part of the API contract. Users who need reproducible partitions should explicitly pass a seed; NetworkX will not change the default to a fixed value in future releases.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.