Index-tracked open-set heap: O(log n) decrease-key + sift-down fix#7
Merged
GenerelSchwerz merged 1 commit intoJun 18, 2026
Conversation
…x sift-down The A* open set is a binary min-heap. It had two issues: 1. Correctness: pop()'s sift-down used `smallerChild < size - 1`, which skips the right child when it is the final slot. pop() could therefore leave the minimum off the root, so A* popped nodes out of f-order -- exploring extra nodes and able to return a worse path. 2. Performance: update() (decrease-key, called whenever A* finds a cheaper route to an already-open node) did `heap.indexOf(val)` -- an O(n) scan over the whole open set, i.e. ~O(n^2) across a search. Fix: each node stores its slot in `heapIdx`, maintained on every sift, so update() is O(log n) with no scan. Sift-up/down are rewritten with the correct child bound; pop() uses Array.pop() instead of splice. Tests: tests/heap.test.ts (ascending order, the boundary regression, decrease-key, heapIdx lifecycle, 200-node randomized stress). All existing pathfinder tests still pass, so real A* output is unchanged -- just correct and faster.
9fabef7
into
Minecraft-Pathfinding:2026-rewrite
1 check passed
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.
Open-set heap: fix sift-down + O(log n) decrease-key
A* uses a binary min-heap (
src/abstract/heap.ts) as its open set. Two issues:1. Correctness bug in
pop()— the sift-down's right-child guard wassmallerChild < size - 1, off by one: when the smaller child is the last slot it gets skipped, sopop()can leave the minimum off the root. A* then pops nodes out offorder — exploring extra nodes and able to return a worse path.2.
update()was O(n) — decrease-key (called whenever A* finds a cheaper route to an already-open node) didheap.indexOf(val), a linear scan over the whole open set → ~O(n²) across a search.Fix
Every node now stores its slot in
heapIdx, maintained on each sift, soupdate()is O(log n) with no scan. Sift-up/down are rewritten with the correct child bound;pop()usesArray.pop()instead ofsplice.Tests
tests/heap.test.ts:forder,[1, 8, …]instead of[1, 2, …]),update()decrease-key,heapIdxlifecycle (set on push,-1after pop),All existing pathfinder tests still pass, so real A* output is unchanged — just correct and faster.
tscbuild clean,ts-standardreports 0 problems.