Heap as array: descendItem: Difference between revisions
No edit summary |
|||
(3 intermediate revisions 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]] | ||
Line 13: | Line 15: | ||
'''Invariant:''' After <math>i</math> iterations: | '''Invariant:''' After <math>i</math> iterations: | ||
# <math>1 \le \ell \le n</math>. | # <math>1 \le \ell \le n</math>. | ||
# The parent's key and its parent's keys of the current heap item <math>h</math> identified by <math> | # The parent's key and its parent's keys of the current heap item <math>h</math> identified by <math>\ell</math> are smaller than the key <math>h.key</math>. | ||
'''Variant:''' | '''Variant:''' | ||
Line 56: | Line 58: | ||
==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: descendItem
Prerequisites:
Type of algorithm: loop
Auxiliary data: A natural number [math]\displaystyle{ \ell }[/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 parent's key and its parent's keys of the current heap item [math]\displaystyle{ h }[/math] identified by [math]\displaystyle{ \ell }[/math] are smaller than the key [math]\displaystyle{ h.key }[/math].
Variant:
- [math]\displaystyle{ \ell }[/math] increases each step and points to a valid position within the heap that is at a lower level than before.
Break condition:
- [math]\displaystyle{ (2* \ell \le n \Rightarrow h.key \le TheHeap[2* \ell ].key) \land (2* \ell +1 \le n \Rightarrow h.key \le TheHeap[2* \le +1].key) }[/math]
Induction basis
Abstract view:
- Set [math]\displaystyle{ \ell }[/math] to the positions of the heap item to be descended.
Implementation:
- [math]\displaystyle{ \ell := Positions[ID] }[/math]
Proof: Obvious.
Induction step
Abstract view:
- Let [math]\displaystyle{ h }[/math] denote the heap item at the position [math]\displaystyle{ \ell }[/math].
- If the existing children have higher or equal keys than [math]\displaystyle{ h }[/math], terminate algorithm.
- Let [math]\displaystyle{ leftChild }[/math] denote the left child heap item of [math]\displaystyle{ h }[/math] and [math]\displaystyle{ rightChild }[/math] the right one.
- If the left child has a lower key than the right one:
- Swap the left child with the parent heap item [math]\displaystyle{ h }[/math].
- Set [math]\displaystyle{ \ell }[/math] to the position of the child [math]\displaystyle{ leftChild }[/math].
- Otherwise:
- Swap the right child with the parent heap item [math]\displaystyle{ h }[/math].
- Set [math]\displaystyle{ \ell }[/math] to the position of the child [math]\displaystyle{ rightChild }[/math].
Implementation:
- [math]\displaystyle{ h := TheHeap[\ell] }[/math]
- If [math]\displaystyle{ (2* \ell \le n \Rightarrow h.key \le TheHeap[2* \ell ].key) \land (2* \ell +1 \le n \Rightarrow h.key \le TheHeap[2* \le +1].key) }[/math], terminate algorithm
- [math]\displaystyle{ leftChild := TheHeap[\ell *2] }[/math] and [math]\displaystyle{ rightChild := TheHeap[\ell *2+1] }[/math]
- If [math]\displaystyle{ leftChild.key \le rightChild.key }[/math]:
- Call [math]\displaystyle{ swapItems(\ell, 2* \ell) }[/math]
- [math]\displaystyle{ \ell := 2* \ell }[/math]
- Otherwise:
- Call [math]\displaystyle{ swapItems(\ell, 2* \ell +1) }[/math]
- [math]\displaystyle{ \ell := 2* \ell +1 }[/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 at least one child and its key is smaller than from [math]\displaystyle{ h.key }[/math]. We will further assume that only the subtree with [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] and its immediate children violate the property. Restoring the heap property in such a case requires to swap the minimum item with the root [math]\displaystyle{ h }[/math], this is done in step 4+5. We then have to ensure that the new subtree [math]\displaystyle{ T' }[/math] with again [math]\displaystyle{ h }[/math] as its new root also holds the heap property, this step is done the next iteration and by assigning [math]\displaystyle{ \ell }[/math] the position of the swapped child. The other pair of items, namely the earlier children satisfy the heap property because the minimum key was chosen as the new root for [math]\displaystyle{ T }[/math]. After at most [math]\displaystyle{ log n }[/math] iterations the heap item identified by [math]\displaystyle{ ID }[/math] is now a leaf or has two children that fulfill 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].