Skip to content

fix(#2057): return ordered cycle path array instead of parent mapping in detectUndirectedCycle - #2198

Open
gonzoblasco wants to merge 1 commit into
trekhleb:masterfrom
gonzoblasco:fix-2057-cycle-path
Open

fix(#2057): return ordered cycle path array instead of parent mapping in detectUndirectedCycle#2198
gonzoblasco wants to merge 1 commit into
trekhleb:masterfrom
gonzoblasco:fix-2057-cycle-path

Conversation

@gonzoblasco

Copy link
Copy Markdown

Description

The detectUndirectedCycle function was returning an object mapping vertex keys to their parent vertices (e.g. { B: vertexC, C: vertexD, D: vertexE, E: vertexB }), which is not a standard cycle representation and may not be intuitive for consumers expecting an ordered path.

This PR changes the return value to an ordered array of vertices forming the cycle, starting and ending at the same vertex (e.g. [vertexB, vertexC, vertexD, vertexE, vertexB]).

Changes

  • detectUndirectedCycle.js: Replaced the parent-mapping object with an ordered array built by walking the parent chain from the repeated vertex back to itself.
  • detectUndirectedCycle.test.js: Updated the test expectation to match the new array format.

Testing

Test Suites: 178 passed, 178 total
Tests:       587 passed, 587 total

Fixes #2057

… mapping in detectUndirectedCycle

The function was returning an object mapping vertex keys to their parent
vertices, which is not a standard cycle representation. Changed to return
an ordered array of vertices forming the cycle, starting and ending at
the same vertex.

Fixes trekhleb#2057
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.

Incorrect Cycle Path Construction in detectUndirectedCycle

1 participant