Skip to content

fix: preserve KeyPriorityQueue order with infinite priorities - #1917

Open
tianrking wants to merge 1 commit into
TheAlgorithms:masterfrom
tianrking:codex/key-priority-queue-infinity
Open

tianrking wants to merge 1 commit into
TheAlgorithms:masterfrom
tianrking:codex/key-priority-queue-infinity

Conversation

@tianrking

Copy link
Copy Markdown

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

KeyPriorityQueue._shiftDown treats Infinity as an absent node and returns before restoring heap order. For example, pushing priorities [0, 1, Infinity] and popping the minimum leaves Infinity above 1; increasing an existing root or internal node's priority to Infinity has the same problem. Infinity is also used by the existing PrimMST implementation when initializing this queue.

Check whether the position is outside the heap instead of checking its priority. Existing comparisons can then sink a present node with infinite priority through finite descendants, while retaining the empty-heap guard.

The regression tests cover replacement nodes with one or two children, root and internal-node updates, equal infinite priorities and queue reuse, and mixed updates/extractions checked against a reference map. They also verify membership, cardinality and the parent/child heap invariant.

Validation

  • Original production source: existing 9 tests pass; 6 of the 7 new cases fail on the heap invariant, and the equal-infinity control passes. Original Node 22/Vitest run.
  • Exact submitted source d00827c097c4faa460100501edadb0415b7d6d28: npm ci, 16 focused tests, all 11,977 tests, whole-project npm run check-style, and npm test -- --coverage pass. Native Node 22 validation.
  • The isolated validation workflow checks out the submitted commit explicitly and verifies the commit, both source blobs and SHA-256 hashes before and after execution, with a clean final diff. The workflow is outside this PR.

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 JavaScript files are placed inside an existing directory.
  • All filenames use the UpperCamelCase (PascalCase) style.
  • New-algorithm reference URL: not applicable; this fixes the existing queue.
  • Issue-closing commit reference: not applicable; no existing open issue for this defect was found.

AI assistance was used to inspect the implementation and draft the minimal fix and regression tests. The change was checked against the original source and validated with the project's native tests and formatting tools.

@tianrking
tianrking requested a review from appgurueu as a code owner October 4, 2026 20:45
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.91%. Comparing base (5c39e87) to head (d00827c).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1917   +/-   ##
=======================================
  Coverage   85.91%   85.91%           
=======================================
  Files         379      379           
  Lines       19778    19778           
  Branches     3016     3017    +1     
=======================================
  Hits        16993    16993           
  Misses       2785     2785           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

2 participants