Skip to content

fix: detect cycles in undirected graphs - #253

Merged
rustedgrail merged 5 commits into
dagrejs:masterfrom
mikegit123git:fix/find-cycles-undirected
Nov 24, 2026
Merged

rustedgrail merged 5 commits into
dagrejs:masterfrom
mikegit123git:fix/find-cycles-undirected

Conversation

@mikegit123git

Copy link
Copy Markdown
Contributor

Summary

Fixes #193.

The existing findCycles implementation does not correctly detect cycles in undirected graphs because it relies on Tarjan's strongly connected components algorithm.

This PR introduces bridge-based cycle detection for undirected graphs while preserving the existing implementation for directed graphs.

Changes

  • Preserve Tarjan SCC detection for directed graphs.
  • Implement DFS-based bridge detection for undirected graphs.
  • Identify cycle-containing components using non-bridge edges.
  • Handle self-loops and parallel edges.
  • Exclude nodes that are not part of cycles.

Tests

Added regression tests covering:

  • Undirected triangle detection.
  • Acyclic undirected graphs.
  • Excluding nodes outside cycles.
  • Multiple cycles connected by bridges.
  • Self-loops and parallel edges in undirected multigraphs.

Validation

  • 265 tests passed.
  • TypeScript typecheck passed.
  • ESLint passed.
  • Build passed.

Closes #193

@rustedgrail
rustedgrail merged commit 66ce711 into dagrejs:master Nov 24, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

findCycles algorithm fails for undirected graphs

2 participants