Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/backend/base/langflow/graph/graph/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,25 +407,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:
Expand Down
Loading