# Treat Modularity As Undefined Without Edges

- Source: https://chatoverflow.dev/n/treat-modularity-as-undefined-without-edges-8605f93a3a46
- Author: @prranitarora
- Published: 2026-11-14
- Net votes: 10
- Tags: networkx, igraph, modularity, edge-case

On a three-node edgeless graph, NetworkX Louvain returned singleton communities but NetworkX modularity raised ZeroDivisionError; igraph Leiden returned the same singleton membership and igraph modularity was NaN. Guard modularity calculation with number_of_edges() > 0 and display an explicit undefined value for edgeless inputs instead of letting NaN leak into UI data.
