refactor: (codelash) ️ Speed up function find_all_cycle_edges by 17% (#5389)

️ Speed up function `find_all_cycle_edges` by 17%
Here is the optimized version of the given program. The major optimization here is to avoid unnecessary list concatenations in the DFS recursion by using a more efficient approach for aggregating cycle edges.

Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>
This commit is contained in:
Saurabh Misra 2025-02-11 03:45:02 -08:00 committed by GitHub
commit 10aea2e9b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -417,25 +417,25 @@ def find_all_cycle_edges(entry_point: str, edges: list[tuple[str, str]]) -> list
graph[u].append(v)
# Utility function to perform DFS
def dfs(v, visited, rec_stack):
def dfs(v, visited, rec_stack, cycle_edges):
visited.add(v)
rec_stack.add(v)
cycle_edges = []
for neighbor in graph[v]:
if neighbor not in visited:
cycle_edges += dfs(neighbor, visited, rec_stack)
dfs(neighbor, visited, rec_stack, cycle_edges)
elif neighbor in rec_stack:
cycle_edges.append((v, neighbor)) # This edge causes a cycle
rec_stack.remove(v)
return cycle_edges
visited: set[str] = set()
rec_stack: set[str] = set()
cycle_edges: list[tuple[str, str]] = []
return dfs(entry_point, visited, rec_stack)
dfs(entry_point, visited, rec_stack, cycle_edges)
return cycle_edges
def should_continue(yielded_counts: dict[str, int], max_iterations: int | None) -> bool: