Download presentation
Presentation is loading. Please wait.
Published byRussell Evans Modified over 9 years ago
1
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES 20-21 Priority Queues and Binary Heaps Algorithms and Data Structures CMPSC 465
2
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 2 Trees Rooted Tree: collection of nodes and edges Edges go down from root (from parents to children) No two paths to the same node Sometimes, children have “ names ” (e.g. left, right, middle, etc)
3
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. Where are trees useful? Some important applications Parse trees (compilers) Heaps Search trees String search Game trees (e.g. chess program) E.g. parse tree for 3*(4+2)*(2^6)*( 8/3) 3 3 4 + 2 ^ 2683 / *
4
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 4 Priority Queue ADT Dynamic set of pairs (key, data), called elements Supports operations: MakeNewPQ () Insert ( S,x ) where S is a PQ and x is a (key,data) pair Extract-Max ( S ) removes and returns the element with the highest key value (or one of them if several have the same value) Example: managing jobs on a processor, want to execute job in queue with the highest priority Can also get a “ min ” version, that supports Extract-Min instead of Extract-Max Sometimes support additional operations like Delete, Increase-Key, Decrease-Key, etc.
5
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 5 Heaps: Tree Structure Data Structure that implements Priority Queue Conceptually: binary tree In memory: (often) stored in an array Recall: complete binary tree all leaves at the same level, and every internal node has exactly 2 children Heaps are nearly complete binary trees Every level except possibly the bottom one is full Bottom layer is filled left to right
6
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 6 Height Heap Height = length of longest simple path from root to some leaf e.g. a one-node heap has height 0, a two- or three-node heap has height 1,... Exercises: What is are max. and min. number of elements in a heap of height h? ans: min = 2 h, max = 2 h +1 -1 What is height as a function of number of nodes n? ans: floor( log(n) )
7
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 7 Array representation Instead of dynamically allocated tree, heaps often represented in a fixed-size array smaller constants, works well in hardware. Idea: store elements of tree layer by layer, top to bottom, left to right Navigate tree by calculating positions of neighboring nodes: Left(i) := 2i Right(i) := 2i+1 Parent(i) := floor(i/2) Example: [20 15 8 10 7 5 6]
8
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 8 Review Questions Is the following a valid max-heap? 20 10 4 9 6 3 2 8 7 5 12 1 (If not, repair it to make it valid) Is an array sorted in decreasing order a valid max- heap?
9
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 9 Local Repair: Heapify “Down” Two important “ local repair operations ” : Heapify “Down”, Heapify “ Up ” Suppose we start from a valid heap and decrease the key of node i, so it is still smaller than parent the two subtrees rooted at i remain valid How do we rearrange nodes to make the heap valid again? Idea: let new, smaller key “ sink ” to correct position Find index with largest key among { i, Left( i ), Right( i )} If i != largest, EXCHANGE i with largest and recurse on largest
10
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 10 Local Repair: Heapify “Down” Pseudocode in book Running time: O(height) = O(log n) Tight in worst case Correctness: by induction on height
11
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 11 MAX-HEAPIFY(-DOWN) Exercise: what does Max-Heapify do on the following input? 2.5 10 4 9 6 3 2 8 7 5 0 1 This gives us our first PQ operation: Extract-Max(A) 1. tmp := A[1] 2. A[1] := A[heap-size] 3. heap-size := heap-size-1 4. MAX-HEAPIFY-DOWN(A,1) return tmp
12
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 12 Local Repair: Heapify “Up” Two important “ local repair operations ” Heapify “ Down ” Heapify “Up” Suppose we start from a valid heap and increase the key of node i, so it is still larger than both children, but might be larger than parent How do we rearrange nodes to make the heap valid again? Idea: let new, larger key “ float ” to right position If A[i] > A[Parent(i)], EXCHANGE i with parent and recurse on parent Pseudocode in CLRS Chap 6.2: “ MAX-HEAPIFY ”
13
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 13 Local Repair: Heapify “Up” Pseudocode in book (see “ Increase-key ” ) Running time: O(log n) tight in worst case Correctness: by induction on height
14
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 14 MAX-HEAPIFY(-UP) Exercise: what does Max-Heapify do on the following input? 20 10 4 9 6 3 2 8 7 21 0 1 This gives us our second PQ operation: Insert(A,x) 1. A[heap-size+1] := x 2. heap-size := heap-size+1 3. MAX-HEAPIFY-UP(A,heap-size)
15
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 15 Other PQ operations Max(S): return max element without extracting it Increase-Key(S,i,new-key) Decrease-Key(S,i,new-key) Both of these require knowing position of desired element Can be taken care by “ augmenting ” using a dictionary data structure, such as hash table Delete(S,i) Delete the element in position i
16
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 16 Heap Sort Heaps give an easy O(n log n) sorting algorithm: For i=2 to n Insert(A,A[i]) For i= n downto 3 A[i] := Extract-Max(A) There is a faster way (O(n) time) to build a heap from an unsorted array.
17
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 17 Search Can we search for an element in a heap in sublinear (that is, o(n)) time, in the worst case? How would you prove that such a search algorithm does not exist?
18
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. Heaps: Review Heap-leap-jeep-creep(A): 1. A.heap-size = 0 2. For i=1 to len(A) tmp = A[i] Heap-Insert(A,tmp) 3. For i=len(A) down to 1 tmp = Heap-Extract-Max(A) A[i]=tmp What does this do? What is the running time of this operation? 18
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.