Skip to content

to_html crashes with ZeroDivisionError on graphs with zero edges #217

Closed

Description

@pedrolbacelar

Bug

to_html in graphify/export.py crashes with ZeroDivisionError when the graph has nodes but zero edges.

How to reproduce

Build a graph where all semantic edges were filtered out (e.g. malformed extraction) but AST nodes exist. The graph has nodes with degree 0.

from graphify.export import to_html

# Graph with nodes but no edges
G = nx.Graph()
G.add_node("a", label="A")
G.add_node("b", label="B")
communities = {0: ["a", "b"]}
to_html(G, communities, "out.html")  # ZeroDivisionError

Root cause

Line 349:

max_deg = max(degree.values()) if degree else 1

When all nodes have degree 0, max(degree.values()) returns 0 (not falsy), so the if degree else 1 guard doesn't trigger. Then line 358:

size = 10 + 30 * (deg / max_deg)  # division by zero

Suggested fix

max_deg = max(degree.values()) if degree else 1
if max_deg == 0:
    max_deg = 1

I have a PR ready for this: #217 (pending)

Activity

  1. added a commit that references this issue on Apr 11, 2026
  2. safishamsi commented on Apr 11, 2026

    @safishamsi
    Member

    Fixed in v0.4.2. The degree normalization now uses max(degree.values(), default=1) or 1 to avoid ZeroDivisionError on empty graphs.

  3. added a commit that references this issue on Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions