⚡️ Speed up function find_leaf_nodes
by 27,577%
#30
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.
📄 27,577% (275.77x) speedup for
find_leaf_nodes
insrc/dsa/nodes.py
⏱️ Runtime :
164 milliseconds
→592 microseconds
(best of548
runs)📝 Explanation and details
Analysis
The bottleneck is clear from line profiling:
The nested loop (
for node in nodes
, thenfor edge in edges
) is quadratic in time (O(N*M)
), which is confirmed by the profiling lines.Here, almost 100% of time is spent scanning all edges for each node, which is inefficient.
Recommendation
Build a set of all node ids that appear as
"source"
in any edge.Then any node whose
id
is not in this set is a leaf node.This reduces total cost to
O(N+M)
instead ofO(N*M)
.You have no relevant installed libraries, so no library-based optimizations apply here.
Optimized Code
Explanation
outgoing_sources
set in one pass overedges
: O(M)O(1)
, much faster than the previousO(M)
scan for each node.Total complexity:
O(N + M)
Memory: Only a set of unique
source
ids (usually much smaller thanlen(edges)
).Summary of Changes
This will make your code orders of magnitude faster without changing results or altering function signatures.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-find_leaf_nodes-mc8qw2dn
and push.