Skip to content

Commit 338e8d9

Browse files
⚡️ Speed up function graph_traversal by 1,193%
The optimized code achieves a ~12x speedup by replacing a list-based visited tracking mechanism with a set-based approach, addressing the core performance bottleneck in graph traversal. **Key Optimization Applied:** - **Separated concerns**: Uses a `set()` for O(1) membership checking (`visited`) and a separate `list` for maintaining traversal order (`result`) - **Fixed graph.get() default**: Changed from `graph.get(n, [])` to `graph.get(n, {})` to match the expected dict type **Why This Creates Massive Speedup:** The original code's `if n in visited` operation on a list has O(n) time complexity - it must scan through the entire list linearly. As the graph grows, each membership check becomes progressively slower. The optimized version uses `if n in visited` on a set, which is O(1) average case due to hash table lookups. **Performance Impact by Graph Size:** - **Small graphs (1-10 nodes)**: Minimal improvement or slight regression (~5-20% slower) due to set overhead - **Medium graphs (30-200 nodes)**: Significant gains (155-331% faster) as O(n) vs O(1) difference becomes apparent - **Large graphs (500-1000 nodes)**: Dramatic speedups (844-2362% faster) where the quadratic behavior of list membership checking becomes the dominant cost **Best Use Cases:** The optimization excels for: - Large star graphs where many nodes are visited quickly - Complete or dense graphs with high connectivity - Long traversal paths where membership checks accumulate - Any scenario where the visited set grows beyond ~20-30 nodes The annotation test results clearly show this pattern - small test cases are slightly slower due to set initialization overhead, while large-scale tests show exponential performance gains as the visited collection grows.
1 parent 9b951ff commit 338e8d9

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/dsa/various.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,19 @@ def matrix_sum(matrix: list[list[int]]) -> list[int]:
4848

4949

5050
def graph_traversal(graph: dict[int, dict[int]], node: int) -> dict[int]:
51-
visited = []
51+
visited = set()
52+
result = []
5253

5354
def dfs(n: int) -> None:
5455
if n in visited:
5556
return
56-
visited.append(n)
57-
for neighbor in graph.get(n, []):
57+
visited.add(n)
58+
result.append(n)
59+
for neighbor in graph.get(n, {}):
5860
dfs(neighbor)
5961

6062
dfs(node)
61-
return visited
63+
return result
6264

6365

6466
def regex_match(strings: list[str], pattern: str) -> list[str]:

0 commit comments

Comments
 (0)