Repository navigation
fix(graph): distinguish self-loops from other incident edges - #2240
Open
Shubham-Padkonde wants to merge 1 commit into
Open
Shubham-Padkonde wants to merge 1 commit into
Shubham-Padkonde wants to merge 1 commit into
Conversation
moshejs
added a commit
to moshejs/javascript-algorithms
that referenced
this pull request
Oct 25, 2026
The same fix and equivalent regression tests are proposed in trekhleb#2240. Leave it there to avoid duplicate review and merge conflicts. The Rabin-Karp rolling hash change from the previous commit stays. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Looking up an edge from a vertex to itself currently returns its first incident edge, even if that edge leads to another vertex. Match both endpoints, and share that lookup with
hasNeighbor.For directed edges A→B (-3) and A→A (9), Bellman-Ford previously reported distance -3 to A even though there is no negative cycle. It now reports 0. An undirected adjacency matrix also now uses the actual self-loop's weight.
Addresses the self-loop lookup part of #2230. The separate parallel-edge policy question in that issue is unchanged. This fix is independent of #2187's negative-cycle detection proposal.
Validation: three new regressions fail before the fix. All 178 suites / 590 tests pass with coverage, including 100% coverage of GraphVertex.js.
npm run lintpasses.AI-assisted contribution.