Heap as array: extract minimum
Algorithmic problem: Bounded priority queue: extract minimum
Prerequisites:
Type of algorithm: loop
Auxiliary data:
- A current index [math]\displaystyle{ k \in \mathbb{N} }[/math].
- An auxiliary index [math]\displaystyle{ k' \in \mathbb{N} }[/math]
Abstract view
Invariant: Before and after each iteration:
- The value of [math]\displaystyle{ N }[/math] is one less than immediately before the call to this method; one heap item has been removed, namely the one that was at position [math]\displaystyle{ 1 }[/math]
- The heap property is fulfilled for all heap items except for the one at index [math]\displaystyle{ k }[/math] (for that one, the heap property may or may not be fulfilled).
Variant: [math]\displaystyle{ k }[/math] is at least doubled.
Break condition: The heap property is fulfilled by [math]\displaystyle{ k }[/math] (note that the heap property is trivially fulfilled by [math]\displaystyle{ k }[/math] once [math]\displaystyle{ 2 * k \gt N }[/math]).
Induction basis
Abstract view:
- Overwrite [math]\displaystyle{ TheHeap[1] }[/math] by [math]\displaystyle{ TheHeap[N] }[/math].
- Decrease [math]\displaystyle{ N }[/math] by one.
- Update [math]\displaystyle{ Positions }[/math] and [math]\displaystyle{ Unused }[/math] accordingly.
Implementation:
- Insert [math]\displaystyle{ TheHeap[1].ID }[/math] in [math]\displaystyle{ Unused }[/math].
- Set [math]\displaystyle{ TheHeap[1].key := TheHeap[N].key }[/math].
- Set [math]\displaystyle{ TheHeap[1].ID := TheHeap[N].ID }[/math].
- Set [math]\displaystyle{ N := N-1 }[/math].
- Set [math]\displaystyle{ Positions[TheHeap[1].ID] := 1 }[/math].
- Set [math]\displaystyle{ k := 1 }[/math].
Proof: Obvious.
Induction step
Abstract view:
- If the current heap item fulfills the heap property, terminate the algorithm.
- Otherwise, exchange the current heap item with one of its children, viz. the smaller one (ties arbitrarily broken). The index [math]\displaystyle{ k }[/math] follows the heap item.
Implementation
- If [math]\displaystyle{ 2*k\gt N }[/math], terminate the algorithm.
- If [math]\displaystyle{ 2*k+1\gt N }[/math], set [math]\displaystyle{ k' := 2*k }[/math].
- Otherwise, set
- [math]\displaystyle{ k' := 2*k }[/math] if [math]\displaystyle{ TheHeap[2k] \le TheHeap[2k+1] }[/math];
- [math]\displaystyle{ k' := 2*k+1 }[/math], otherwise.
- If [math]\displaystyle{ TheHeap[k] \le TheHeap[k'] }[/math], terminate the algorithm.
- Swap [math]\displaystyle{ Positions[TheHeap[k].ID] }[/math] and [math]\displaystyle{ Positions[TheHeap[k'].ID] }[/math].
- Swap [math]\displaystyle{ TheHeap[k].key }[/math] and [math]\displaystyle{ TheHeap[k'].key }[/math].
- Swap [math]\displaystyle{ TheHeap[k].ID }[/math] and [math]\displaystyle{ TheHeap[k'].ID }[/math].
- Set [math]\displaystyle{ k := k' }[/math].
Correctness: Obviously, the heap item at the former position [math]\displaystyle{ k }[/math] now fulfills the heap property, and the new [math]\displaystyle{ k }[/math] is now the only index that does not necessarily fulfill it.
Complexity
Statement: The asymptotic complexity is logarithmic in the worst case. Proof: Obvious.