⚡️ Speed up function find_last_node by 13,482%#233
Closed
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Closed
⚡️ Speed up function find_last_node by 13,482%#233codeflash-ai[bot] wants to merge 1 commit intomainfrom
find_last_node by 13,482%#233codeflash-ai[bot] wants to merge 1 commit intomainfrom
Conversation
The optimized code achieves a **135x speedup** by eliminating a nested loop that was causing O(N×E) time complexity.
**What changed:**
The original code checked `all(e["source"] != n["id"] for e in edges)` for every node, meaning for each of N nodes, it scanned all E edges. The optimized version preprocesses edges once into a set `edge_sources = {e["source"] for e in edges}`, then performs constant-time lookups `n["id"] not in edge_sources`.
**Why this is faster:**
- **Set lookups are O(1)** vs. list scans which are O(E)
- The algorithm complexity drops from **O(N×E) to O(N+E)**
- In Python, the `all()` generator with nested iteration is particularly expensive because it repeats the same edge traversal for every node
**Performance characteristics from tests:**
- **Small graphs (2-5 nodes)**: 26-104% faster due to overhead of set creation being small
- **Large graphs**: Dramatic speedups where the quadratic cost dominates:
- 1000-node linear chain: **319x faster** (18.5ms → 57.6μs)
- 1000-node disconnected graph: **293x faster** (15.0ms → 50.8μs)
- 100-node fully connected graph: **85x faster** (16.8ms → 194μs)
- **Empty/tiny graphs**: Slightly slower (10-20%) due to set creation overhead with minimal computation
**When this matters:**
This optimization is critical for graphs with many edges or when called frequently in tight loops. The quadratic behavior of the original makes it prohibitively slow for non-trivial graph sizes (100+ nodes/edges), while the optimized version scales linearly.
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.
📄 13,482% (134.82x) speedup for
find_last_nodeinsrc/algorithms/graph.py⏱️ Runtime :
50.4 milliseconds→371 microseconds(best of250runs)📝 Explanation and details
The optimized code achieves a 135x speedup by eliminating a nested loop that was causing O(N×E) time complexity.
What changed:
The original code checked
all(e["source"] != n["id"] for e in edges)for every node, meaning for each of N nodes, it scanned all E edges. The optimized version preprocesses edges once into a setedge_sources = {e["source"] for e in edges}, then performs constant-time lookupsn["id"] not in edge_sources.Why this is faster:
all()generator with nested iteration is particularly expensive because it repeats the same edge traversal for every nodePerformance characteristics from tests:
When this matters:
This optimization is critical for graphs with many edges or when called frequently in tight loops. The quadratic behavior of the original makes it prohibitively slow for non-trivial graph sizes (100+ nodes/edges), while the optimized version scales linearly.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-find_last_node-mjnhkx3oand push.