⚡️ Speed up function find_last_node by 26,750%
#228
+2
−1
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.
📄 26,750% (267.50x) speedup for
find_last_nodeinsrc/algorithms/graph.py⏱️ Runtime :
102 milliseconds→381 microseconds(best of250runs)📝 Explanation and details
The optimized code achieves a 267x speedup by eliminating a nested loop antipattern that caused O(n*m) complexity, reducing it to O(n+m).
Key Optimization:
The original code checked
all(e["source"] != n["id"] for e in edges)for every node, resulting in nested iteration: for each of the n nodes, it scanned all m edges. This created O(n*m) time complexity.The optimized version pre-computes a set of all source IDs with
source_ids = {e["source"] for e in edges}, then uses fast O(1) set membership testing withn["id"] not in source_ids. This reduces complexity to O(m) for set creation + O(n) for the lookup, giving O(n+m) overall.Why This Is Faster:
Performance Characteristics:
The optimization is especially valuable when
find_last_nodeis called repeatedly on moderately-sized graphs, as the speedup scales quadratically with graph size. Since finding terminal nodes is a common graph traversal pattern (e.g., identifying workflow endpoints, DAG sinks), this optimization would benefit any hot path performing graph analysis.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-find_last_node-mjncnrj1and push.