04.09.2013 Views

Algorithm Design

Algorithm Design

Algorithm Design

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

6O<br />

Chapter 2 Basics of <strong>Algorithm</strong> Analysis<br />

~a<br />

EaCh node’s key is at least~<br />

s large as its parent’s.<br />

1 2 5 10 3 7 11 15 17 20 9 15 8 16 X<br />

Figure 2.3 Values in a heap shown as a binaD, tree on the left, and represented as an<br />

array on the right. The arrows show the children for the top three nodes in the tree.<br />

at position i, the children are the nodes at positions leftChild(i) = 2i and<br />

rightChild(f) = 2i + 1. So the two children of the root are at positions 2 and<br />

3, and the parent of a node at position i is at position parent(f) =/i/2J. If<br />

the heap has n < N elements at some time, we will use the first rt positions<br />

of the array to store the n heap elements, and use lenggh(H) to denote the<br />

number of elements in H. This representation keeps the heap balanced at all<br />

times. See the right-hand side of Figure 2.3 for the array representation of the<br />

heap on the left-hand side.<br />

Implementing the Heap Operations<br />

The heap element with smallest key is at the root, so it takes O(1) time to<br />

identify the minimal element. How do we add or delete heap elements? First<br />

conside~ adding a new heap element v, and assume that our heap H has n < N<br />

elements so far. Now it will have n + 1 elements. To start with, we can add the<br />

new element v to the final position i = n + 1, by setting H[i] = v. Unfortunately,<br />

this does not maintain the heap property, as the key of element v may be<br />

smaller than the key of its parent. So we now have something that is almost-a<br />

heap, except for a small "damaged" part where v was pasted on at the end.<br />

We will use the procedure Heap±f y-up to fix our heap. Letj = parent(i) =<br />

L//2] be the parent of the node i, and assume H[j] = w. If key[v] < key[w],<br />

then we will simply swap the positions of v and w. This wil! fix the heap<br />

property at position i, but the resulting structure will possibly fail to satisfy<br />

the heap property at position j--in other words, the site of the "damage" has<br />

moved upward from i to j. We thus call the process recursively from position<br />

2.5 A More Complex Data Structure: Priority Queues<br />

The H e a p i fy - u p process is moving I<br />

element v toward the root.<br />

Figure 2.4 The Heapify-up process. Key 3 (at position 16) is too small (on the left).<br />

After swapping keys 3 and 11, the heap xdolation moves one step closer to the root of<br />

the tree (on the right).<br />

] = parent(i) to continue fixing the heap by pushing the damaged part upward.<br />

Figure 2.4 shows the first two steps of the process after an insertion.<br />

Heapify-up (H, i) :<br />

If i> 1 then<br />

let ] = parent(i) = Lil2J<br />

If key[H[i]] key(v) such that raising the value of key(v) to c~ would make<br />

the resulting array satisfy the heap property. (In other words, element v in H[i]<br />

is too small, but raising it to cz would fix the problem.) One important point<br />

to note is that if H is almost a heap with the key of the root (i.e., H[1]) too<br />

small, then in fact it is a~heap. To see why this is true, consider that if raising<br />

the value of H[1] to c~ would make H a heap, then the value of H[!] must<br />

also be smaller than both its children, and hence it already has the heap-order<br />

property.<br />

61

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!