Heap as array: ascendItem: Difference between revisions
No edit summary |
|||
(3 intermediate revisions by 2 users 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]] | ||
Line 24: | Line 26: | ||
==Induction basis== | ==Induction basis== | ||
'''Abstract view:''' | '''Abstract view:''' | ||
# Set <math>\ell</math> to the | # Set <math>\ell</math> to the position of the heap item to be ascended. | ||
'''Implementation:''' | '''Implementation:''' | ||
# <math>\ell | # Retrieve <math>\ell</math> from the index handler. | ||
'''Proof:''' Obvious: | '''Proof:''' Obvious: | ||
Line 44: | Line 46: | ||
==Complexity== | ==Complexity== | ||
'''Statement:''' The asymptotic complexity is in <math>\Theta (log n)</math> in the best and worst case. | |||
'''Proof:''' Follows immediately from the fact that the height of the heap tree is in <math>\Theta (log n)</math>. | |||
==Further information== | ==Further information== |
Latest revision as of 23:12, 19 June 2015
Algorithmic problem: Heap as array: ascendItem
Prerequisites:
Type of algorithm: loop
Auxiliary data:
- A natural number
denoting a position within the heap. - A pointer
of type heap item. - A natural number
denoting a position within the heap.
Abstract view
Invariant: After
.- The left and right child's key and their children's keys of the current heap item
identified by are bigger than the key .
Variant:
decreases each step and points to a valid position within the heap that is at a higher level than before.
Break condition:
or
Induction basis
Abstract view:
- Set
to the position of the heap item to be ascended.
Implementation:
- Retrieve
from the index handler.
Proof: Obvious:
Induction step
Abstract view:
- Let
denote the heap item at the position . - If
is the root or its parent has a lower key than h, terminate algorithm. - Swap
with its parent. - Set
to the position of its parent (new position of ).
Implementation:
- If
or , terminate algorithm - Call
Correctness: If the break condition holds the algorithm is obviously correct, as we already restored the heap property. So consider the case where we have a parent that has a key that is bigger than from
Complexity
Statement: The asymptotic complexity is in
Proof: Follows immediately from the fact that the height of the heap tree is in