Skip to content

fix(graphs): preserve Dijkstra priority queue state on extraction - #15816

Open
henokwon wants to merge 2 commits into
TheAlgorithms:masterfrom
henokwon:fix/dijkstra-priority-queue
Open

henokwon wants to merge 2 commits into
TheAlgorithms:masterfrom
henokwon:fix/dijkstra-priority-queue

Conversation

@henokwon

Copy link
Copy Markdown

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

PriorityQueue.extract_min leaves an obsolete entry in its backing array and does not update the replacement root's position before heapifying. A five-node graph containing a zero-weight edge can consequently crash with KeyError:

from graphs.dijkstra_algorithm import Graph

graph = Graph(5)
for source, target, weight in [
    (0, 1, 1), (0, 2, 2), (0, 3, 3), (0, 4, 2), (1, 4, 0)
]:
    graph.add_edge(source, target, weight)
graph.dijkstra(0)
assert graph.dist == [0, 1, 2, 3, 1]

Remove the last backing entry, update the replacement root's position, and skip heapifying when the queue becomes empty. The reproducer now returns distances [0, 1, 2, 3, 1]. The existing doctests remain unchanged.

Local validation on Python 3.15.2 free-threaded:

  • All 117 existing doctest examples and 16 pytest items pass.
  • All 15,444 shortest-path comparisons against a heapq reference pass for four-node undirected graphs with absent/zero/one/three-weight edges and no isolated vertices, from every source.
  • 10,000 extraction/reuse/root-decrease operations pass heap, size, and position-map invariants. Increasing insertions isolate extraction from unrelated bubble-up behavior.
  • Ruff 0.17.3 lint/format and git diff --check pass.
  • All applicable pre-commit hooks pass on the changed file.
  • Informational ty reports no source errors and four existing renamed-rule configuration warnings.

Regression harnesses were run separately from this code-only patch, following the repository's request to keep implementation and test changes in separate PRs. The legacy parent-index behavior in decrease_key is outside this extraction fix.

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Nov 27, 2026

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

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant