Skip to content

Treat neighbor-only vertices as sinks in heap Dijkstra - #2783

Open
pralav-25 wants to merge 1 commit into
keon:mainfrom
pralav-25:fix_dijkstra_implicit_sinks
Open

pralav-25 wants to merge 1 commit into
keon:mainfrom
pralav-25:fix_dijkstra_implicit_sinks

Conversation

@pralav-25

@pralav-25 pralav-25 commented Sep 27, 2026 •

Copy link
Copy Markdown

dijkstra({'s': {'a': 1, 't': 2}}, 's', 't') raises KeyError('a') before reaching t. Treat a vertex omitted from the adjacency mapping as having no outgoing edges, consistent with the existing distance initialization for discovered neighbors.

Documents this representation and tests competing dead ends, zero-cost edges, reachable/unreachable targets, input preservation, and equivalence with explicitly supplied empty adjacency dictionaries.

Validation on Python 3.12.14:

  • The new test file reproduced 4 failing cases on unmodified main.
  • python -m pytest -q: 606 passed.
  • python -m pytest --doctest-modules algorithms/ -q: 480 passed, 1 skipped.
  • ruff check algorithms/ tests/ and git diff --check: passed.

Prepared with AI assistance; the failures and fixes above were reproduced locally.

Summary by CodeRabbit

  • Bug Fixes

    • Dijkstra’s algorithm now handles vertices listed only as neighbors as having no outgoing edges, rather than raising an error.
  • Tests

    • Added coverage for implicit sink vertices, unreachable targets, zero-weight edges, and source-equals-target cases.

@coderabbitai

coderabbitai Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: a65ff685-a576-4573-97fc-3525621499d7

📥 Commits

Reviewing files that changed from the base of the PR and between bd1d152 and 6820f3b.

📒 Files selected for processing (2)
  • algorithms/graph/dijkstra_heapq.py
  • tests/test_dijkstra_implicit_sinks.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.


📝 Walkthrough

Walkthrough

Dijkstra now treats vertices that appear only as neighbors as having no outgoing edges. Tests cover expected distances and paths, input graph preservation, and equivalence with explicit empty adjacency lists.

Changes

Implicit sink handling

Layer / File(s) Summary
Implicit sink traversal and validation
algorithms/graph/dijkstra_heapq.py, tests/test_dijkstra_implicit_sinks.py
The traversal uses an empty adjacency mapping when a popped vertex is absent from the graph. The documentation describes this behavior, and tests cover distances, paths, graph preservation, and equivalence with explicit empty adjacency lists.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 6820f

Neighbor-only vertices can be treated as sinks without an identified merge-blocking issue.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: treating neighbor-only vertices as sinks in heap Dijkstra.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
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.

1 participant