Skip to content

to_html crashes with ZeroDivisionError on graphs with zero edges #217

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)

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