Heap as array: extract minimum: Difference between revisions
Jump to navigation
Jump to search
(Created page with "__NOTOC__ Category:Algorithm Category:Method of an implementation of a data structure '''Algorithmic problem:''' Bounded priority queue|Bounded priority queue: extra...") |
No edit summary |
||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
[[Category:Videos]] | |||
{{#ev:youtube|https://www.youtube.com/watch?v=j-r4YOPFp7E|500|right||frame}} | |||
[[Category:Checkup]] | |||
[[Category:Algorithm]] | [[Category:Algorithm]] | ||
[[Category:Method of an implementation of a data structure]] | [[Category:Method of an implementation of a data structure]] |
Latest revision as of 23:13, 19 June 2015
Algorithmic problem: Bounded priority queue: extract minimum
Prerequisites:
Type of algorithm: loop
Auxiliary data:
- A current index
. - An auxiliary index
Abstract view
Invariant: Before and after each iteration:
- The value of
is one less than immediately before the call to this method; one heap item has been removed, namely the one that was at position - The heap property is fulfilled for all heap items except for the one at index
(for that one, the heap property may or may not be fulfilled).
Variant:
Break condition: The heap property is fulfilled by
Induction basis
Abstract view:
- Overwrite
by . - Decrease
by one. - Update
and accordingly.
Implementation:
- Insert
in . - Set
. - Set
. - Set
. - Set
. - Set
.
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
follows the heap item.
Implementation
- If
, terminate the algorithm. - If
, set . - Otherwise, set
if ; , otherwise.
- If
, terminate the algorithm. - Swap
and . - Swap
and . - Swap
and . - Set
.
Correctness: Obviously, the heap item at the former position
Complexity
Statement: The asymptotic complexity is logarithmic in the worst case. Proof: Obvious.