Overview
Sedgewick’s Indexed d-ary Min-Priority Queue maintaining bidirectional heap/key position maps for O(1) lookup and O(log_d N) decreaseKey and arbitrary deletion.
Guided Implementation Walkthrough (4 Progressive Steps)
Step 1: d-ary Heap Geometry & Bidirectional Swap
- Core Mechanical Concept: 0-Indexed d-ary Parent/Child Arithmetic & Synchronized pq[]/qp[] Maps
- Target Module:
index.js(IndexedDaryMinPQ._parent / _firstChild / _swap) - Engineering Specification: Implement
_parent(pos),_firstChild(pos), and_swap(i, j). In a 0-indexed $d$-ary heap: -_parent(pos) = Math.floor((pos - 1) / this.d)-_firstChild(pos) = pos * this.d + 1-this.qp[this.pq[pos]] === posmust hold for every0 <= pos < this._size. - Verification Invariant: maintains bidirectional pq and qp bijection across swaps
Step 2: d-ary _siftUp and _siftDown
- Core Mechanical Concept: Fast O(log_d N) Sift-Up vs d-Child Cache-Line Scan on Sift-Down
- Target Module:
index.js(IndexedDaryMinPQ._siftUp / _siftDown) - Engineering Specification: Implement
_siftUp(pos)and_siftDown(pos). In_siftDown(pos), inspect childrenfirst = pos * d + 1throughMath.min(size - 1, first + d - 1), findminChild, and swap if_less(minChild, pos). - Verification Invariant: restores d-ary heap order via siftUp and siftDown
Step 3: Indexed Insert and popMin
- Core Mechanical Concept: Root Extraction & Inverse Index Cleanup
- Target Module:
index.js(IndexedDaryMinPQ.insert / popMin) - Engineering Specification: Implement
insert(id, priority)andpopMin(). InpopMin(), swap root0withsize - 1, decrement_size, markqp[minId] = -1, clearkeys[minId] = undefined, and_siftDown(0)if non-empty. - Verification Invariant: inserts and extracts minimum elements in priority order
Step 4: O(log_d N) updateKey and Arbitrary Deletion
- Core Mechanical Concept: Jumping Directly to Heap Position via qp[id]
- Target Module:
index.js(IndexedDaryMinPQ.updateKey / delete) - Engineering Specification: Implement
updateKey(id, newPriority)anddelete(id). Becausepos = this.qp[id]gives the exact heap slot in $O(1)$, updating or deleting an arbitrary key requires only_siftUp(pos)and_siftDown(pos). - Verification Invariant: updates priorities and deletes arbitrary internal keys in O(log_d N)
Asymptotic & Memory Layout Guarantees
- Category: Trees & Priority Queues
- Difficulty: Intermediate (~30 minutes)
- Key Architectural Highlights:
• Configurable arity d (default d=4) reducing siftUp height by log2(d) and grouping children into one cache line
• Bidirectional index maps pq[] and qp[] supporting O(1) contains(id) and O(log_d N) updateKey/delete
• Zero duplicate heap entries during Dijkstra and Prim edge relaxations - Tags & Primitives:
Indexed Priority Queue,d-ary Heap,Decrease-Key,Cache Line Alignment,Dijkstra
Production Systems Context & Verification
This from-scratch implementation is engineered with zero external dependencies and deterministic verification harnesses. Every internal invariant—from pointer restoration and bitmask addressing to amortized potential bounds and tree height balance—is asserted directly in test.js. Use the interactive browser workbench above to complete the guided steps in Guided Lab mode or inspect and benchmark the complete reference architecture in Full Solution mode.