Download presentation
Presentation is loading. Please wait.
Published byLilian Norman Modified over 6 years ago
1
CSE 332: Data Structures Priority 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… Play lists (e.g., Pandora) Processor Scheduling: Not all “fair shares” are equal
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 There are, of course, gobs of applications for priority queues. Here are just a few.
4
Priority Queue ADT Need a new ADT
Operations: Insert an Item, Remove the “Best” Item 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 lower priority than y, x will be deleted before y We need a new ADT for this. One that returns the best (which we’ll generally define as lowest priority value) node next. Unfortunate terminology: low priority is better. Think of the priority value as a “rank” instead of a score. I.e., 1 is the best. 11/29/2018 5 5
6
Potential implementations
Worst case insert deleteMin Unsorted list (Array) Unsorted list (Linked-List) Sorted list (Array) Sorted list (Linked-List) Binary Search Tree (BST) O(1)/O(N) array full, O(N) – to find value O(1) O(N) – to find value 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) We are going to do an insert = O(log N) worst case, O(1) on average (on average 1.67 levels) Deletemin O(log N) – worst and average case. O(N) O(N) Plus – good memory usage O(log N) close to O(1) levels on average O(log N) Binary Heap 6 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 root(T): A leaves(T): D-F, I-N children(B): D-F
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 Trees have many uses, and we’ll take about them later. To start, let’s just review the basics. Let’s review the words: root: A leaf: DEFJKLMNI child:A - C or H - K leaves have no children parent: C - A or L - H the root has no parent sibling: D - E or F or J - K,L,M, or N grandparent: G to A grandchild: C to H or I ancestor: the node itself or any ancestor’s parent descendent: the node itself or any child’s descendent subtree: a node and all its descendents
9
More Tree Terminology # edges 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 Depth: the number of edges along the path from the root to the node height: the number of edges along the longest path from the node to a leaf degree: the number of children of the node branching factor: the maximum degree of any node (or sometimes the average) preorder traversal: running through all the nodes in the tree, starting with the parent, then all the children postorder traversal: run through all the nodes starting with the children and then the parents
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? Show an example, counter-example Why is this useful? 1. Height at most floor(log_2 n). To see this, make a table (h=0: n=1; h=1: n=2-3; h=2: n=4-7, … ) 2. Efficient storage (next slide)
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) Tree is PARTIALLY ORDERED. For each node: its value is less than value of all of its descendants. This is the definition for a MIN heap – could do the same for a max heap.
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) How long does step 1 take? QUESTION: Max number of exchanges is ? O(log N) – must percolate up to root. On average, analysis shows that you tend to only need to move up 1.67 levels, so get O(1) on average. Optimization in the book – bubble up an EMPTY space, and then do a swap (reduces the # of swaps).
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 Now insert 90. (no swaps, even though 99 is larger!) Now insert 7.
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? If I < = both children, then you are done. How long does step 1 take? QUESTION: Max number of exchanges is ? O(log N) – must percolate all the way to the bottom level. In fact this is often the case, you just took the value from the bottom, there is a good chance it has to go down to the lowest level. O(log N) Optimization in the book – bubble up an EMPTY space down, and then do a swap (reduces the # of swaps).
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 Now delete 15
19
Representing Complete Binary Trees in an Array
1 2 3 4 5 6 9 8 10 11 12 A From node i: left child: right child: parent: B C 7 D E F G H I J K L A B C D E F G H I J K L 1 2 3 4 5 6 7 8 9 10 11 12 13 Left child of node I = 2 * I Right child I = (2*i) +1 Parent of node I is at i/2 (floor)
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) no pointers (space). *2, /2, + are faster operations than dereferencing a pointer. (faster operations) but also, better locality. can get to parent easily Can we use the implicit representation for binary search trees?
21
DeleteMin Code runtime: (Java code in book) Object deleteMin() {
assert(!isEmpty()); returnVal = Heap[1]; size--; newPos = percolateDown(1, Heap[size + 1]); Heap[newPos] = Heap[size + 1]; return returnVal; } 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; } break; return hole; Θ(log n) worst/ave case runtime: Note that there are three things going on here: 1. find the smaller child 2. if the smaller child is still smaller than the moving value, move the smaller child up 3. otherwise, we’ve found the right spot, and stop. Runtime log N (worst case and average) (Java code in book)
22
Insert Code runtime: (Java code in book) void insert(Object o) {
assert(!isFull()); size++; newPos = percolateUp(size,o); Heap[newPos] = o; } int percolateUp(int hole, Object val) { while (hole > 1 && val < Heap[hole/2]) Heap[hole] = Heap[hole/2]; hole /= 2; } return hole; Θ(log n) worst case ~ 1.67 = Θ(1) on average: only have to go up a few levels runtime: Optimization: no swaps Notice that this code is a _lot_ easier. Runtime: log N worst case, but constant on average (only have to go up a few levels) (Java code in book)
23
Insert: 16, 32, 4, 69, 105, 43, 2 1 2 3 4 5 6 7 8 Deletemin - > returns 2 Deletemin -> returns 4
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? Why do I insist on a pointer to each item in decreaseKey, increaseKey, and remove? Because it’s hard to find an item in a heap; it’s easy to delete the minimum, but how would you find some arbitrary element? This is where the Dictionary ADT and its various implementations which we will study soon come up. decreaseKey: percolateUp increaseKey: percolateDown 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. We do better with buildHeap... Call insert n times Θ(n log n) worst, Θ(n) average
27
Building a Heap: Take 1 5 11 3 10 6 9 4 8 1 7 2 12 Let’s try pretending it’s a heap already and just fixing the heap-order property. The red nodes are the ones that are out of order. Question: which nodes MIGHT be out of order in any heap?
28
BuildHeap: Floyd’s Method
5 11 3 10 6 9 4 8 1 7 2 12 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 Let’s try pretending it’s a heap already and just fixing the heap-order property. The red nodes are the ones that are out of order. Question: which nodes MIGHT be out of order in any heap? 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
Finally… 1 3 2 4 5 6 9 12 8 10 7 11 - 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: How long does this take? Well, everything above the fringe might move 1 step. Everything height 2 or greater might move 2 steps. Most nodes move only a small number of steps the runtime is O(n). (see text for proof) Full sum = (I=0 to height) SUM (h-I) * 2^i
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: n/4 + 2*n/8 + 3*n/ = n (1/4 + 2/8 + 3/ ) = n*1 = n
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.