⚡️ Speed up method Graph.topologicalSort by 62%#885
Closed
codeflash-ai[bot] wants to merge 1 commit into
Closed
Conversation
The key optimization replaces the inefficient `stack.insert(0, v)` operation with `stack.append(v)` followed by a single `stack.reverse()` at the end. This changes the time complexity from O(n²) to O(n) for building the result stack. **What changed:** - In `topologicalSortUtil`: Changed `stack.insert(0, v)` to `stack.append(v)` - In `topologicalSort`: Added `stack.reverse()` after the DFS traversal completes - Minor style improvement: `if visited[i] == False:` → `if not visited[i]:` **Why this is faster:** `stack.insert(0, v)` has O(n) complexity because it must shift all existing elements one position right. When called repeatedly during DFS traversal (6,110 times in the profiler), this creates O(n²) overhead. `stack.append(v)` is O(1), and the final `stack.reverse()` is O(n) for the entire list, resulting in O(n) total complexity for stack operations. **Performance impact:** The line profiler shows the stack operation time dropped from 2.196ms to 1.649ms (25% improvement). The overall speedup is 62%, from 9.29ms to 5.72ms. Test results show the optimization particularly benefits larger graphs - achieving 59-75% speedups on 1000-node test cases while having minimal impact on small graphs (often within measurement noise). **Behavioral preservation:** The final topological order is identical since reversing the append-built stack produces the same result as the original insert-at-beginning approach, just much more efficiently.
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.
📄 62% (0.62x) speedup for
Graph.topologicalSortincode_to_optimize/topological_sort.py⏱️ Runtime :
9.29 milliseconds→5.72 milliseconds(best of16runs)📝 Explanation and details
The key optimization replaces the inefficient
stack.insert(0, v)operation withstack.append(v)followed by a singlestack.reverse()at the end. This changes the time complexity from O(n²) to O(n) for building the result stack.What changed:
topologicalSortUtil: Changedstack.insert(0, v)tostack.append(v)topologicalSort: Addedstack.reverse()after the DFS traversal completesif visited[i] == False:→if not visited[i]:Why this is faster:
stack.insert(0, v)has O(n) complexity because it must shift all existing elements one position right. When called repeatedly during DFS traversal (6,110 times in the profiler), this creates O(n²) overhead.stack.append(v)is O(1), and the finalstack.reverse()is O(n) for the entire list, resulting in O(n) total complexity for stack operations.Performance impact:
The line profiler shows the stack operation time dropped from 2.196ms to 1.649ms (25% improvement). The overall speedup is 62%, from 9.29ms to 5.72ms. Test results show the optimization particularly benefits larger graphs - achieving 59-75% speedups on 1000-node test cases while having minimal impact on small graphs (often within measurement noise).
Behavioral preservation:
The final topological order is identical since reversing the append-built stack produces the same result as the original insert-at-beginning approach, just much more efficiently.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
test_topological_sort.py::test_topological_sorttest_topological_sort.py::test_topological_sort_2test_topological_sort.py::test_topological_sort_3🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_9qb_ya1j/tmpxhmk42k9/test_concolic_coverage.py::test_Graph_topologicalSortTo edit these changes
git checkout codeflash/optimize-Graph.topologicalSort-mhp9l036and push.