Skip to content

Fix Graphs.hasCycle missing cycles in undirected graphs. - #8801

Merged
copybara-service[bot] merged 1 commit into
masterfrom
test_1009013320
Oct 29, 2026
Merged

copybara-service[bot] merged 1 commit into
masterfrom
test_1009013320

Conversation

@copybara-service

Copy link
Copy Markdown
Contributor

Fix Graphs.hasCycle missing cycles in undirected graphs.

Since the switch from recursion to iteration in 63734b9, subgraphHasCycle pops a node and marks it COMPLETE as soon as it dequeues the node it was reached from (the A->B->A case that canTraverseWithoutReusingEdge rejects), so the node's remaining successors are never visited. In an undirected graph whose successor order puts the parent first, cycles reachable only through those successors are missed.

Connected undirected graphs with a cycle are caught by the numEdges >= nodes().size() shortcut, so this shows up when the graph has more nodes than edges, e.g. a triangle plus an isolated node.

Fixes #8796

RELNOTES=n/a

Since the switch from recursion to iteration in 63734b9, `subgraphHasCycle` pops a node and marks it `COMPLETE` as soon as it dequeues the node it was reached from (the A->B->A case that `canTraverseWithoutReusingEdge` rejects), so the node's remaining successors are never visited. In an undirected graph whose successor order puts the parent first, cycles reachable only through those successors are missed.

Connected undirected graphs with a cycle are caught by the `numEdges >= nodes().size()` shortcut, so this shows up when the graph has more nodes than edges, e.g. a triangle plus an isolated node.

Fixes #8796

RELNOTES=n/a
PiperOrigin-RevId: 1009602021
@copybara-service
copybara-service Bot merged commit ca8aedb into master Oct 29, 2026
4 checks passed
@copybara-service
copybara-service Bot deleted the test_1009013320 branch October 29, 2026 12:00
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.

1 participant