Download presentation
Presentation is loading. Please wait.
Published byRosamund Hamilton Modified over 9 years ago
1
CS 361 – Chapter 5 Priority Queue ADT Heap data structure –Properties –Internal representation –Insertion –Deletion
2
Priority Queue An ADT where each item has a special value called its “key” or “priority” (in addition to its contents) Not really a queue It’s important to be able to find/extract smallest element –Could just as easily be defined with “largest” Application: –Scheduling a set of tasks. We could use “Earliest Deadline First” or “Shortest Job Next”. At all times, we need to know the winner. Desired operations: –insert (element, keyValue) –removeNext ( ) –findNext ( )
3
Implementation One approach is a sorted list (array). –Insert: O(n), to find the place in the list to insert, and possibly shift over other elements –Remove and find smallest: O(1), since it’s at the front Can we do better than O(n) insertion? Heap implementation –This d.s. is a special type of binary tree –Complete or almost complete: the lowest level may have gap along the right side, but nowhere else –Heap property: For all nodes i in the heap, value(parent(i)) value(i) with the exception of the root which has no parent.
4
Why a heap? Designed so that the PQ insert function can run in time proportional to the height of the tree: O(log(n)), rather than O(n). Because of the heap property, finding the min element is O(1). On the other hand, searching the heap for an arbitrary value is not a priority. That would still take O(n). –How do you solve that problem?
5
Insert & heapify up To insert a node, make it the last child. But now, we have probably violated the heap property, which we can restore by doing a “heapify up”. Heapify up: Starting with c, the child we just inserted: If value(c) value(parent(c)), we’re done. Else: swap c with its parent, and continue up the heap at the new location of c. Example p. 169 What is the complexity?
6
Delete & heapify down How to remove smallest element, which is at the root. Remove the root, and immediately replace it with the last child. But, we may have just violated the heap property, so… Heapify down: starting with the root node r: If value(r) values of both children, we’re done. Else: swap r with its smaller child, and continue down the heap at the new location of r. –(Why swap with smaller child; does it matter?)
7
Array, take 2 Earlier we saw that we didn’t want a sorted array representation. No need to keep all elements sorted to maintain heap property. Why is array attractive? –Insert/remove operations need to quickly find last child, which would be at end of array. O(1) –“Almost complete” binary tree: all elements in array contiguous from A[1..n]. Although internally represented as 1-D array, we still conceive of heap logically as a tree structure.
9
More on heaps Finish heap d.s. –Heap sort –How to build heap: from scratch more efficiently
10
Heap sort The desired ops for a PQ are enough to allow us to sort some list of items. –Insert(item) –removeMin() How to sort: –Insert items one at a time –Remove items one by one Analysis: (n inserts) + (n removeMin’s) –And we know that insert & remove both take O(log n) time. –(Recall that just finding an element is O(1) but need heapify.) –Total time is O(n log n). –More on sorting in Chapters 8 and 9.
11
Optimizing heap sort Doesn’t mean we’re perfecting it. Actually, it will still be O(n log n). The major improvement is in the insertion: we can bring this part down to O(n). Algorithm p. 176: bottomUpHeap(S) –Given a “sequence” of values, create a heap. 1.If size(S) < 2, return trivial heap. 2.Remove S[0] 3.H1 = bottomUpHeap (firstHalf(S)) 4.H2 = bottomUpHeap (secondHalf(S)) 5.H = tree with S[0] at root, and subtrees H1 and H2. 6.Heapify down H starting at S[0], and return H.
12
Build heap example Use the algorithm to build a heap out of: 5,4,7,3,2,6,1 –Not a base case –Remove S[0] = 5 –H1 = bottomUpHeap(4,7,3) Eventually creates a heap with 3 nodes. –H2 = bottomUpHeap(2,6,1) Eventually creates a heap with 3 nodes. –H = tree with 7 nodes with 5 at the root. –Need to heapify down. Try another example. Sometimes the 2 “halves” that we use in recursive call are not exactly the same size. No big deal.
13
Analysis of bUH Background: the classic way of creating a heap is to insert n elements one by one: O(n log n). We hope to show bottomUpHeap can do it in O(n). In the best case, no heapify down is needed. We create n heaps. Creating 1 takes constant time (just an assignment stmt and function call). n * const Ω(n) What is different about worst case? –Need to heapify down. Count the number of swaps. –Consider case of 15 nodes. (height = 3) 1 node needs 3 swaps 2 nodes each need 2 swaps 4 nodes each need 1 swap –Consider case of 31 nodes. (height = 4)
14
Analysis (2) If we have n nodes, h = floor(log n). Total maximum # swap operations depends on h. –The formula is: the sum i = 1 to h of: (2 h – i nodes * i swaps per node) –Let’s work out the sum from i = 1 to h of… Sum (i 2 h – i ) = 2 h Sum (i 2 – i ) = 2 h Sum (i / 2 i ) –What is this summation? Consider this: Let S = 1/2 1 + 2/2 2 + 3/2 3 + 4/2 4 + … Then, S/2 = 1/2 2 + 2/2 3 + 3/2 4 + … Subtracting, we obtain S/2 = 1/2 + 1/4 + 1/8 + 1/16 + … = 1. Therefore, S = 2. –Then, the # of swaps is 2 h * 2 = O(2 h ) = O(n).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.