Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork30.8k
Description
In the functiondetectUndirectedCycle
, the cycle path is being constructed as an object with vertex keys as properties and their parent vertices as values. This makes the returnedcycle
not a standard path array, but rather a mapping, which may not be intuitive or useful for consumers expecting an ordered path of vertices forming the cycle. A more conventional approach would be to return anordered array of vertices representing the cycle, starting and ending at the same vertex.
Additionally, if a cycle is detected, the function continues to traverse the graph until DFS completes, although traversal is blocked byallowTraversal
once a cycle is set. However, early exit after detecting a cycle could improve efficiency.
Suggested Fix:
- Change
cycle
to be an array that accumulates the ordered path of the cycle. - Optionally, add a mechanism to exit the traversal immediately after the cycle is found.