Skip to content

⚡️ Speed up function find_cycle_vertices by 215% #26

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

Closed
Closed
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
17 changes: 10 additions & 7 deletions src/dsa/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ def _get_all_json_refs(item: Any) -> set[JsonRef]:
def find_cycle_vertices(edges):
# Create a directed graph from the edges
graph = nx.DiGraph(edges)

# Find all simple cycles in the graph
cycles = list(nx.simple_cycles(graph))

# Flatten the list of cycles and remove duplicates
cycle_vertices = {vertex for cycle in cycles for vertex in cycle}

cycle_vertices = set()
for scc in nx.strongly_connected_components(graph):
if len(scc) > 1:
# All nodes in a non-trivial SCC participate in cycles
cycle_vertices.update(scc)
else:
# For singleton SCCs, check self-loop
v = next(iter(scc))
if graph.has_edge(v, v):
cycle_vertices.add(v)
return sorted(cycle_vertices)


Expand Down