⚡️ Speed up function find_leaf_nodes by 14,900%#303
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
⚡️ Speed up function find_leaf_nodes by 14,900%#303codeflash-ai[bot] wants to merge 1 commit intomainfrom
find_leaf_nodes by 14,900%#303codeflash-ai[bot] wants to merge 1 commit intomainfrom
Conversation
The optimization replaces the nested O(N×M) loop structure with a two-pass O(N+M) algorithm: first building a set of all source node IDs in a single pass over edges, then filtering nodes via a list comprehension that performs O(1) membership checks against that set instead of scanning all edges for each node. Line profiler confirms the nested loop previously consumed 99.5% of runtime (708ms total), while the optimized version completes in 0.52ms—a 1367× speedup. Small regressions on trivial inputs (empty/single-node cases, 23–55% slower) stem from set-construction overhead that is negligible in absolute terms (<1μs) and vastly outweighed by dramatic gains on realistic workloads (e.g., 1000-node tests improve 286–312×).
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.
📄 14,900% (149.00x) speedup for
find_leaf_nodesinsrc/algorithms/graph.py⏱️ Runtime :
46.5 milliseconds→310 microseconds(best of250runs)📝 Explanation and details
The optimization replaces the nested O(N×M) loop structure with a two-pass O(N+M) algorithm: first building a set of all source node IDs in a single pass over edges, then filtering nodes via a list comprehension that performs O(1) membership checks against that set instead of scanning all edges for each node. Line profiler confirms the nested loop previously consumed 99.5% of runtime (708ms total), while the optimized version completes in 0.52ms—a 1367× speedup. Small regressions on trivial inputs (empty/single-node cases, 23–55% slower) stem from set-construction overhead that is negligible in absolute terms (<1μs) and vastly outweighed by dramatic gains on realistic workloads (e.g., 1000-node tests improve 286–312×).
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
🔎 Click to see Concolic Coverage Tests
To edit these changes
git checkout codeflash/optimize-find_leaf_nodes-mmuc1iroand push.