trekhleb / trekhleb/javascript-algorithms

Calling Graph.reverse() on a graph with a 2-cycle throws an Error

Open
#302 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
197k
Forks
31k
PR merge metrics
No merged PRs in 30d

Description

Graph.reverse() is currently broken for directed graphs with a 2-cycle. Consider the complete, directed graph of size 2, if you call the reverse() method in its current form, it will throw an error.

I think a fix would be to make the reverse() methods of both the Graph and GraphEdge objects pure functions which return new instances of the respective objects.

This would add the additional benefit of allowing calling these functions to not change the original graph, which can be overwritten/garbage collected if desired.

I am currently working on a pull request for issue #252 and would like to use the already-implemented stronglyConnectedComponents function to implement Johnson's algorithm.

I would be willing to open a preliminary PR making the change described above (and refactoring tests) if the owners are ok with this change.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the failure with a complete directed graph of size 2 and inspect Graph.reverse() and GraphEdge.reverse(). Trace the existing behavior for the 2-cycle, then verify that the chosen fix preserves the original graph and handles reversal without throwing. Confirm the result with the repository's existing graph tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.