Fix recursive mapperify() calls in add_edge() - #114
Open
ishikaghosh2201 wants to merge 1 commit into
Open
ishikaghosh2201 wants to merge 1 commit into
ishikaghosh2201 wants to merge 1 commit into
Conversation
ishikaghosh2201
requested review from
lizliz
and
a lite review from Copilot
September 19, 2026 21:12
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
MapperGraph.add_edge calls self.mapperify() unconditionally on every edge added. mapperify() scans every integer level between the graph's min and max function value and calls self.subdivide_edge(...) for every edge crossing a level. subdivide_edge (on the parent ReebGraph class) internally reconnects the new vertex via self.add_edge(u, w) / self.add_edge(w, v) — and since self is a MapperGraph instance, these calls dispatch back through the overridden MapperGraph.add_edge, re-triggering a full mapperify() rescan of the entire graph for every single subdivision vertex inserted. Each nested call can itself trigger more subdivisions, each of which recurses again.
This PR:
No public API changes anywhere.
Motivation and Context
Fixes #113 . A single edge spanning 5 integer levels triggered 11 total mapperify() calls instead of 1, and timing on a 40-node graph with distinct integer heights went 6 edges → ~9s, 8 edges → ~23s, 10 edges → ~40s — far worse than linear or quadratic growth. On real point-cloud data with denser clustering this makes computeMapper (which builds its output graph edge-by-edge the same way, via computemapper.py's __addedges) impractically slow or effectively hung.
How has this been tested?
--test_mapperify_called_once_per_add_edge: asserts a single add_edge() call -triggers exactly one top-level mapperify() scan.
--test_mapperify_no_recursion_correctness: asserts the mapper invariant still holds — a single edge spanning 6 levels produces the correct subdivision vertices at every crossed integer level.
--test_mapperify_no_exponential_blowup: asserts adding an edge spanning 18 levels completes in well under 1s, guarding against the recursion regressing.
Types of changes
Checklist
pyproject.tomlfile if a new version needs to be pushed to pypi. Note that if the number isn't incremented, the package will not be pushed to pypi, which is useful if this PR is only for updating documentation.make formatto clean up the code withblack.make tests).