CSE 332: Data Structures Priority Queues – Binary Heaps Part II

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued Tyler Robison Summer
1 CSE 326: Data Structures Priority Queues – Binary Heaps.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
1 Binary heaps binary tree that satisfy two properties –structural property (is a complete tree) –heap-ordering property (minimum item on top) Can have.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
CSE 326: Data Structures Lecture #17 Priority Queues Alon Halevy Spring Quarter 2001.
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
CSE 326: Data Structures Lecture #13 Priority Queues and Binary Heaps Nick Deibel Winter Quarter 2002.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2010W2 1.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued Dan Grossman Spring 2012.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps Linda Shapiro Spring 2016.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
Heaps and Heap Sort. Sorting III / Slide 2 Background: Complete Binary Trees * A complete binary tree is the tree n Where a node can have 0 (for the leaves)
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structure & Algorithms
CSE373: Data Structures & Algorithms
CS 201 Data Structures and Algorithms
Priority Queues and Heaps
Binary Search Trees.
October 30th – Priority QUeues
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
Heaps, Heap Sort, and Priority Queues
CMSC 341: Data Structures Priority Queues – Binary Heaps
CSE 326 Heaps and the Priority Queue ADT
CSE 326: Data Structures Lecture #4 Heaps more Priority Qs
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued
Data Structures & Algorithms Priority Queues & HeapSort
Instructor: Lilian de Greef Quarter: Summer 2017
CSE 373: Data Structures and Algorithms
CMSC 341 Lecture 14 Priority Queues & Heaps
CSE 326: Data Structures Lecture #4 Mind Your Priority Queues
B-Tree Insertions, Intro to Heaps
CSE 332: Data Structures Priority Queues – Binary Heaps
Instructor: Lilian de Greef Quarter: Summer 2017
Heaps and the Heapsort Heaps and priority queues
CSE 332: Data Abstractions Binary Search Trees
CE 221 Data Structures and Algorithms
Priority Queues (Chapter 6.6):
CSE 326: Data Structures Priority Queues & Binary Heaps
CSE373: Data Structures & Algorithms Lecture 7: Binary Heaps, Continued Dan Grossman Fall 2013.
Amortized Analysis and Heaps Intro
CSE 373 Data Structures and Algorithms
CSE 326: Data Structures Lecture #3 Mind your Priority Qs
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
Priority Queues (Chapter 6):
CSE 326: Data Structures Lecture #5 Heaps More
CSE 373: Data Structures and Algorithms
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
Heaps.
CSE 373: Data Structures and Algorithms
Priority Queues (Heaps)
Presentation transcript:

CSE 332: Data Structures Priority Queues – Binary Heaps Part II Richard Anderson Spring 2016

Administrative Turn in HW1 HW2 available P1 Due next Wednesday

Priority Queue ADT Insert(v) DeleteMin() 6 2 15 23 12 18 45 3 7 insert 6 2 15 23 12 18 45 3 7 insert deleteMin

Binary Heap data structure binary heap for priority queues: O(log n) worst case for both insert and deleteMin Heap properties Complete binary tree Value of a node less than or equal to the values of its children

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

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

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)

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?

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)

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)

Insert: 16, 32, 4, 69, 105, 43, 2 1 2 3 4 5 6 7 8 Deletemin - > returns 2 Deletemin -> returns 4

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

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?

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

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?

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

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

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

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

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/16 + ... = n (1/4 + 2/8 + 3/16 + ...) = n*1 = n