Skip to content
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
17 changes: 12 additions & 5 deletions src/dsa/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,18 @@ 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}
# Find all strongly connected components instead of enumerating all cycles
sccs = nx.strongly_connected_components(graph)

# Collect vertices that are part of cycles
cycle_vertices = set()
for component in sccs:
if len(component) > 1:
cycle_vertices.update(component)
else:
vertex = next(iter(component))
if graph.has_edge(vertex, vertex):
cycle_vertices.add(vertex)

return sorted(cycle_vertices)

Expand Down
Loading