Repository navigation
Conversation
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. Skip only the rejected successor and keep iterating, as the recursive version did.
copybara-service Bot
pushed a commit
that referenced
this pull request
Oct 28, 2026
…rected graphs. This will be fixed by cl/1009013320. (progress toward fixing #8796) RELNOTES=n/a PiperOrigin-RevId: 1009103711
copybara-service Bot
pushed a commit
that referenced
this pull request
Oct 28, 2026
…rected graphs. This will be fixed by cl/1009013320. (progress toward fixing #8796) RELNOTES=n/a PiperOrigin-RevId: 1009160348
copybara-service Bot
pushed a commit
that referenced
this pull request
Oct 29, 2026
… in light of the recently detected bug. ### `Graphs` - We have been copying a `Set` to an `ArrayDeque` for no particular reason. We might as well use its `Iterator` to reduce how much we store. ### `GraphPropertiesTest` - `hasCycle_deepPathGraph` was populating `graphsToTest` but then asserting on `directedNetwork` and `undirectedNetwork`, which are empty! (This seems like a sign that the tests should really be restructed more significantly....) This is my fault from 63734b9. - Because of the recently detected bug (fix: cl/1009013320), our tests weren't actually traversing deeply into the graph, so they weren't actually proving that the switch from recursion to iteration was effective. Shame on me for not verifying that the tests failed before my change! - Until cl/1009013320, all tests that checked for `hasCycle(undirectedGraph) == true` triggered [a shortcut](https://github.com/google/guava/blob/ff4f4e8dee0cad772ffa0256a93cae5b4aae695f/guava/src/com/google/common/graph/Graphs.java#L65), so we were not testing the part of the logic that lives in `subgraphHasCycle`. This has probably been broken forever. Amazingly, all the new and fixed tests do pass without prod changes. (noticed during investigation of #8796) RELNOTES=n/a PiperOrigin-RevId: 1009644539
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Graphs.hasCyclereturnsfalsefor some undirected graphs that do have a cycle:This came in with 63734b9 (iteration instead of recursion, to avoid
StackOverflowError), so 33.3.0 through 33.6.0 are affected.In
subgraphHasCycle, when the next successor is the node we just came from (the undirected A->B->A case thatcanTraverseWithoutReusingEdgerejects), the loop falls through tostack.removeLast()and marks the nodeCOMPLETE, so the rest of its successors are never visited. The recursive version only skipped that one successor. Connected graphs with a cycle still hit thenumEdges >= nodes().size()shortcut, which is probably why the existing tests didn't catch it; it shows up once there are more nodes than edges.The fix moves the
continueout of theif, so only the rejected successor is skipped.The new
GraphPropertiesTest.hasCycle_threeCyclicEdgesAndIsolatedNodefails before the change and passes after, and the rest ofcom.google.common.graphpasses. A randomized comparison against a plain recursive DFS on 20,000 small graphs (directed/undirected, with and without self-loops) found 456 wrong answers before the change and none after. I leftandroid/alone per CONTRIBUTING.Unrelated, but nearby:
hasCycle_deepPathGraphadds the 100,000-edge path tographsToTestand then asserts ondirectedNetwork/undirectedNetwork, which are empty, so it doesn't actually check the deep-path graph case. Happy to fix that here or in a separate PR.I used Claude Code to write this change.