1 / 32

CSE 332: Data Structures Priority Queues – Binary Heaps

CSE 332: Data Structures Priority Queues – Binary Heaps. Richard Anderson Spring 2016. Recall Queues. FIFO: First-In, First-Out Print jobs File serving Phone calls and operators Lines at the Department of Licensing…. Priority Queues. Prioritize who goes first – a priority queue :

edington
Download Presentation

CSE 332: Data Structures Priority Queues – Binary Heaps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 332: Data StructuresPriority Queues – Binary Heaps Richard Anderson Spring 2016

  2. Recall Queues • FIFO: First-In, First-Out • Print jobs • File serving • Phone calls and operators • Lines at the Department of Licensing…

  3. Priority Queues Prioritize who goes first – a priority queue: • treat ER patients in order of severity • route network packets in order of urgency • operating system can favor jobs of shorter duration or those tagged as having higher importance • Greedy optimization: “best first” problem solving

  4. Priority Queue ADT • Need a new ADT • Operations: Insert an Item, Remove the “Best” Item 6 2 15 23 12 18 45 3 7 insert deleteMin

  5. Priority Queue ADT PQueue data : collection of data with priority PQueue operations insert deleteMin (also: create, destroy, is_empty) PQueue property: if x has lowerpriority than y, x will be deleted before y 4/1/2016 5

  6. Potential implementations Worst case O(1)/O(N) array full, O(N) – to find value O(N) – to find value O(1) O(1) to find, O(N) to move, O(1) if reverse order O(log N) to find, O(N) to shift O(N) to find loc, O(1) to do the insert O(1) O(N) O(N) Plus – good memory usage O(log N)close to O(1) 1.67 levels on average O(log N) Binary Heap 6

  7. Binary Heap data structure • binary heap (a kind of binary tree) for priority queues: • O(log n) worst case for both insert and deleteMin • O(1) average insert • It’s optimized for priority queues. Lousy for other types of operations (e.g., searching, sorting)

  8. Tree Review Tree T A root(T): A leaves(T): D-F, I-N children(B): D-F parent(H): G siblings(E): D,F ancestors(F): descendants(G): subtree(C): B C D E F G H I J K L M N

  9. # edges More Tree Terminology Tree T A depth(B): height(G): height(T): degree(B): branching factor(T): n-ary tree: # edges on path from root to self B C # edges on longest path from node to leaf D E F G H I # children of a node MAX # children of a node for a BST this is?? J K L M N

  10. Binary Heap Properties A binary heap is a binary tree with two important properties that make it a good choice for priority queues: • Completeness • Heap Order Note: we will sometimes refer to a binary heap as simply a “heap”.

  11. Completeness Property • A binary heap is a complete binary tree: • a binary tree with all levels full, except possibly the bottom level, which is filled left to right Examples: Height of a complete binary tree with n nodes?

  12. Heap Order Property Heap order property:For every non-root node X, the value in the parent of X is less than (or equal to) the value in X. This is the order for a MIN heap – could do the same for a max heap. 10 10 20 80 15 80 40 60 85 99 30 20 50 700 This is a PARTIAL order (diff than BST) For each node, its value is less than all of its descendants (no distinction between left and right)

  13. Heap Operations • Main ops: insert, deleteMin • Key is to maintain • Completeness • Heap Order • Basic idea is to propagate changes up/down the tree, fixing order as we go

  14. Heap – insert(val) Basic Idea: • Put val at last leaf position • Percolate up by repeatedly exchanging node with parent as long as needed How long does this take? How long does this take? – max # of exchanges = O(log N) On “average” only need to move up 1.67 levels so get O(1)

  15. Insert: percolate up 10 Now insert 90. (no swaps, even though 99 is larger!) Now insert 7. 20 80 40 60 85 99 50 700 65 15 Optimization, bubble up an empty space to reduce # of swaps 10 15 80 40 20 85 99 50 700 65 60

  16. Heap – deleteMin Basic Idea: • Remove min element • Put “last” leaf node value at root • Find smallest child of node • Swap node with its smallest child if needed. • Repeat steps 3 & 4 until no swaps needed. Why last?

  17. DeleteMin: percolate down Tworst = O(log N), Tavg? O(log N), since you usually need to percolate all the way down… - Could also percolate empty bubble down 10 20 15 40 60 85 99 50 700 65

  18. DeleteMin: percolate down 15 20 65 40 60 85 99 50 700

  19. 1 2 3 6 4 5 8 9 10 11 12 2 * i Representing Complete Binary Trees in an Array (2 * i)+1 └ i / 2┘ A From node i: left child: right child: parent: B C 7 F G D E H I J K L

  20. Why use an array? • Less space • *2 and /2,+ are usually faster operations than dereferencing a pointer • An array MAY get better locality in general than a linked structure. • Can get to the parent easily (and without an extra pointer)

  21. DeleteMin Code int percolateDown(int hole, Object val) { while (2*hole <= size) { left = 2*hole; right = left + 1; if (right ≤ size && Heap[right] < Heap[left]) target = right; else target = left; if (Heap[target] < val) { Heap[hole] = Heap[target]; hole = target; } else break; } return hole; } Object deleteMin() { assert(!isEmpty()); returnVal = Heap[1]; size--; newPos = percolateDown(1, Heap[size + 1]); Heap[newPos] = Heap[size + 1]; return returnVal; } Θ(log n) worst/ave case runtime: (Java code in book)

  22. Insert Code int percolateUp(int hole, Object val) { while (hole > 1 && val < Heap[hole/2]) Heap[hole] = Heap[hole/2]; hole /= 2; } return hole; } void insert(Object o) { assert(!isFull()); size++; newPos = percolateUp(size,o); Heap[newPos] = o; } Θ(log n) worst case ~ 1.67 = Θ(1) on average: only have to go up a few levels runtime: (Java code in book)

  23. Insert: 16, 32, 4, 69, 105, 43, 2

  24. More Priority Queue Operations decreaseKey(nodePtr, amount): given a pointer to a node in the queue, reduce its priority Binary heap: change priority of node and ________________ increaseKey(nodePtr, amount): given a pointer to a node in the queue, increase its priority Binary heap: change priority of node and ________________ percolateUp percolateDown Why do we need a pointer? Why not simply data value? Worst case running times? It’s hard to find in a pQ

  25. More Priority Queue Operations remove(objPtr): given a pointer to an object in the queue, remove it Binary heap: ______________________________________ findMax( ): Find the object with the highest value in the queue Binary heap: ______________________________________ decreasePriority(objPtr,∞), deleteMin O(logn) Search leaves, O(n) Worst case running times?

  26. More Binary Heap Operations expandHeap( ): If heap has used up array, copy to new, larger array. • Running time: buildHeap(objList): Given list of objects with priorities, fill the heap. • Running time: We do better with buildHeap... Call insert n times Θ(n log n) worst, Θ(n) average

  27. 12 5 11 3 10 6 9 4 8 1 7 2 Building a Heap: Take 1

  28. 12 5 11 3 10 6 9 4 8 1 7 2 BuildHeap: Floyd’s Method Add elements arbitrarily to form a complete tree. Pretend it’s a heap and fix the heap-order property! 12 Red nodes need to percolate down 5 11 Key idea: fix red nodes from bottom-up 3 10 6 9 4 8 1 7 2

  29. BuildHeap: Floyd’s Method 12 12 5 11 5 11 3 10 2 9 3 1 2 9 4 8 1 7 6 4 8 10 7 6 12 12 5 2 1 2 3 1 6 9 3 5 6 9 4 8 10 7 11 4 8 10 7 11

  30. - Runtime bounded by sum of heights of nodes, which is linear. O(n) - How many nodes at height 1, and height 2, up to root, - See text, Thm. 6.1 p. 194 for detailed proof. Finally… 1 3 2 4 5 6 9 12 8 10 7 11

  31. Buildheap pseudocode private void buildHeap() { for ( int i = currentSize/2; i > 0; i-- ) percolateDown( i ); } runtime:

  32. Buildheap Analysis n/4 nodes percolate at most 1 level n/8 percolate at most 2 levels n/16 percolate at most 3 levels ... runtime:

More Related