This preview shows page 1. Sign up to view the full content.
Unformatted text preview: COP 3503 – Computer Science II – CLASS NOTES  DAY #25 Priority Queues • A priority queue is a structure where the highest priority item is the next item that will be dequeued. • Implementation typically sets the highest priority item to have the lowest integer value as its priority. • Thus, the node with the lowest integer value should always be at the head of the queue. • If implemented as a binary heap, insertion and deletion can be done in logarithmic time in the worst case. Binary Heaps • • • • • Can be implemented as an array (just like the queue) Combines the best properties of binary search trees and queues. Insertion: average case – O(1), worst case – O(log2 N). DeleteMin: worst case – O(log2 N). FindMin: worst case – O(1). Like a binary search tree, the binary heap has two properties: a structure property and an ordering property. Any operation has the potential for destroying both the structure and ordering properties of the binary heap (hereafter called “the heap”). Therefore, operations on the heap should not be allowed to terminate until both properties are preserved. Structure Property • A tree is the only structure that gives logarithmic time bounds, therefore it makes sense to organize the heap as a tree. • To ensure a logarithmic worst case time bound, we will need to ensure that the tree is balanced. Note: a complete tree requires that every internal node has the maximum number of children. For a binary tree this means that each internal node must have two children. • A complete binary tree has several useful properties that make it well suited to the application of heaps. Day 25  1 1. The height (length of longest path) is at most log2 N (where the tree contains N nodes). A complete tree of height H has between 2H and 2H+1− 1 nodes. 2. In a complete binary tree, left and right references are not needed. Instead the levelorder traversal of the tree can be stored in singledimension array. The root node is stored in array position 1 (position 0 is utilized later by the priority queue). For any element in array position i, its left child will be found in position 2i and its right child will be found in position 2i+1. To determine it a node at index i has children test to see if 2i > number of elements. To determine the parent of a node at index i, check at index i/2 . Using an array to represent a tree is called an implicit tree representation. This method is very fast on most systems and the traversal operation become trivial and extremely fast. Example: A B C D E F G H I J 0 1 A 2 B 3 C 4 D 5 E 6 F 7 G 8 H 9 I 10 J Left child of node “A” ( index 1) is located at index (2*1) = 2 or node “B” Right child of node “A” (index 1) is located at index (2*1) +1 = 3 or node “C” Parent of node “F” (index 6) is located at 6/2 = 3 or node “C” Parent of node “I” (index 9) is located at 9/2 = 4 or node “D” Child of node “H” (index 8) is located at (2*8) = 16 out of range so no child exists.
Day 25  2 Ordering Property (Heap Order) • In a heap, for every node x with parent p, the key value in p is smaller than the key value in x. • The root always has the highest priority. • Parent nodes have higher priority than their children. • To indicate that the root has no parent and is of the highest priority, the implicit representation (the one using an array) will put − position 0 of the ∞ array. Basic Operations on the Heap Insert: 1. Insert a node into the next available spot (i.e., in the bottom ply). 2. Compare the key value of the new node with its parent’s key value, if the new node’s key value is less than its parent’s – interchange the nodes. 3. “Percolate” the node up into its correct position by recursively applying step #2. Example: 1 2 7 5 1 5 7 2 3 3 Example: 1 1 2 4 3 2 1 7 4 2 3 2 7 1 Day 25  3 7 2 4 3 2 1 1 Final tree maintaining structure and ordering properties. DeleteMin: 1. Get the key value from the root node. 2. Locate the bottom, rightmost child and interchange it with the root. 3. Compare the key values of the two children of the root and the compare the smaller of them with the key value of the root. Interchange if the root key value is greater than that of the smaller child key value. 4. Percolate down by treating each current node as a “root” and recursively applying step #3. Example: return node “1” 1 1 5 7 1 1 5 1 7 5 1 7 1 Day 25  4 Example: return 1 1 7 1 1 1 7 1 1 8 1 8 7 7 8 1 1 1 1 1 1 8 Sorting From A Binary Heap • Recall that the binary heap has both an ordering property and a structure property. The ordering property ensures that the items in the heap are basically sorted from minimum (root) value to maximum values (leafs). • Removing (or reading) everything results in a sort. • If this can be done in O(N log2 N) time, then we have found a “noncomparison” based sorting algorithm. The insertion technique that we used for inserting items into the binary heap, inserted N items and required O(log2 N) time to find the right spot at which to do the insertion. Thus the time required to insert N items is: O(N log2 N). Day 25  5 The deletion technique, called deleteMin( ) (our Java method), removes the root item from the heap and requires O(1) time to do this, then the heap structure is reset and the ordering preserved which requires an additional O(log2 N) time. Since there are N items in the heap, removing all of them (i.e., emptying the heap) requires O(N log2 N) time. A better solution A better solution to this problem involves a different technique for handling the insertion. Since the binary heap is implemented as an array, we can fill the array (insert the N items) without regard to their proper order in linear time, O(N). Then call fixHeap to structure the heap in linear time O(N). The method deleteMin( ) still requires O(N log2 N) time in the worst case. Therefore, this method will require O(N log2 N) time in the worst case. [Note: fixHeap can be implemented in linear time – see Figure 20.19 in Java book, Figure 21.16 called BuildHeap in C++ book. The following example illustrates how this technique works). Starting with the first nonleaf level from the bottom of the tree, using a reverse levelorder traversal, percolate the minimum value to the root of the current subtree. Note that leaf nodes do not need to be considered as they have no children with which to compare. Day 25  6 Example: Illustration of linear time fixHeap( ) using percolateDown( ) initial tree: has structure property, violates ordering property 9 4 2 2 1 4 6 6 1 5 3 2 6 8 7 1. call percolateDown(7) no changes 9 2 4 2 6 4 1 6 2. call percolateDown(6) swap 25, 45 4 1 7 8 6 2 3 5 9 2 2 1 2 6 6 1 5 3 4 6 8 7
Day 25  7 3. call percolateDown(5) no changes 4 9 2 2 1 2 6 6 1 5 3 9 4 6 8 7 4. call percolateDown(4) swap 17, 20 4 2 1 1 2 6 6 2 5 3 4 6 8 7 5. call percolateDown(3) no changes 9 2 4 1 1 2 6 6 2 5 3 4 6 8 7 Day 25  8 6. call percolateDown(2) swap 12, 47 then swap 37, 47 9 2 1 1 3 2 6 6 2 5 4 4 6 8 7 7. call percolateDown(1) swap 12, 92, followed by17, 92, followed by 20, 92 1 1 2 2 3 2 6 6 9 5 4 4 6 8 7 final heap shown above Day 25  9 This technique is not without its problems, which include: • Requires 2N space as 2 arrays are needed, one for the unordered heap and one created in heap order by fixHeap( ). • Fix: This problem can be solved by using a “sliding heap” and altering the method fixHeap( ) to return the largest item. As maximal items are returned we remove them from the heap and decrease the size of the heap by one. The removed item is then placed into the cell of the array which has been “freed” from the heap. Example: 9 heap 1. call fixHeap( ) 7 heap 2. call fixHeap( ) 7 4 2 5 1 6 0 8 9 end of heap 4 2 5 1 6 8 0 9 end of heap 7 4 2 5 1 6 8 0 end of heap heap 3. call fixHeap( ) heap 4 2 5 1 6 0 7 8 9 end of heap 4. call fixHeap( ) 4 2 5 1 0 6 7 8 9 end of heap heap Day 25  10 5. call fixHeap( ) 4 2 1 0 5 6 7 8 9 end of heap heap 6. call fixHeap( ) 2 1 0 4 5 6 7 8 9 end of heap heap 7. call fixHeap( ) 1 0 2 4 5 6 7 8 9 end of heap heap 8. call fixHeap( ) 0 1 2 4 5 6 7 8 9 end of heap heap 9. call fixHeap( ) 0 1 2 4 5 6 7 8 9 all items in the heap heap When the index counter (the sliding part of the heap) gets to 0, the resulting array is in heap order. Day 25  11 ...
View
Full
Document
 Spring '08
 Staff
 Computer Science

Click to edit the document details