-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
Open
Description
In the function detectUndirectedCycle, the cycle path is being constructed as an object with vertex keys as properties and their parent vertices as values. This makes the returned cycle 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 an ordered 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 by allowTraversal once a cycle is set. However, early exit after detecting a cycle could improve efficiency.
Suggested Fix:
- Change
cycleto 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.
Metadata
Metadata
Assignees
Labels
No labels