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 [math]\displaystyle{ \ell }[/math] denoting a position within the heap.
- A pointer [math]\displaystyle{ h }[/math] of type heap item.
- A natural number [math]\displaystyle{ p }[/math] denoting a position within the heap.
Abstract view
Invariant: After [math]\displaystyle{ i }[/math] iterations:
- [math]\displaystyle{ 1\le \ell \le n }[/math].
- The left and right child's key and their children's keys of the current heap item [math]\displaystyle{ h }[/math] identified by [math]\displaystyle{ \ell }[/math] are bigger than the key [math]\displaystyle{ h.key }[/math].
Variant:
- [math]\displaystyle{ \ell }[/math] decreases each step and points to a valid position within the heap that is at a higher level than before.
Break condition:
- [math]\displaystyle{ \ell = 1 }[/math] or [math]\displaystyle{ TheHeap[\lfloor \ell /2 \rfloor ].key \le TheHeap[\ell].key }[/math]
Induction basis
Abstract view:
- Set [math]\displaystyle{ \ell }[/math] to the position of the heap item to be ascended.
Implementation:
- Retrieve [math]\displaystyle{ \ell }[/math] from the index handler.
Proof: Obvious:
Induction step
Abstract view:
- Let [math]\displaystyle{ h }[/math] denote the heap item at the position [math]\displaystyle{ \ell }[/math].
- If [math]\displaystyle{ h }[/math] is the root or its parent has a lower key than h, terminate algorithm.
- Swap [math]\displaystyle{ h }[/math] with its parent.
- Set [math]\displaystyle{ \ell }[/math] to the position of its parent (new position of [math]\displaystyle{ h }[/math]).
Implementation:
- [math]\displaystyle{ h := TheHeap[\ell] }[/math]
- If [math]\displaystyle{ \ell = 1 }[/math] or [math]\displaystyle{ TheHeap[\lfloor \ell /2 \rfloor ].key \le h.key }[/math], terminate algorithm
- [math]\displaystyle{ p := \lfloor \ell /2 \rfloor }[/math]
- Call [math]\displaystyle{ swapItems(\ell ,p) }[/math]
- [math]\displaystyle{ \ell := p }[/math]
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 [math]\displaystyle{ h.key }[/math]. We will further assume that only the subtree with the parent of [math]\displaystyle{ h }[/math] as its root violates the heap property, to be more exact only the subtree [math]\displaystyle{ T }[/math] with [math]\displaystyle{ h }[/math], the parent and the other child violate the property. Restoring the heap property in such a case requires to swap [math]\displaystyle{ h }[/math] with its parent (namely the root of [math]\displaystyle{ T }[/math]), this is done in step 4. We then have to ensure that the subtree [math]\displaystyle{ T' }[/math] with [math]\displaystyle{ h }[/math] as a child, its new parent and its sibling also holds the heap property, this step is done by the next iteration and by assigning [math]\displaystyle{ \ell }[/math] the position of the swapped root. The new child and parent pair, [math]\displaystyle{ h }[/math] and its previous sibling satisfy the heap property because the key of [math]\displaystyle{ h }[/math] is smaller than its previous parent. After at most [math]\displaystyle{ log n }[/math] iterations the heap item identified by [math]\displaystyle{ ID }[/math] is now the new root of the whole tree or has a parent that fulfills the heap property. Therefore the heap property for the whole tree is restored again.
Complexity
Statement: The asymptotic complexity is in [math]\displaystyle{ \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]\displaystyle{ \Theta (log n) }[/math].