Priority Queues and Heaps 15-211 Fundamental Data Structures and Algorithms Klaus Sutner January 29, 2004.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
Heaps, Heap Sort, and Priority Queues. Sorting III / Slide 2 Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two.
Priority Queues and Heaps Fundamental Data Structures and Algorithms Margaret Reid-Miller 29 January 2004.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Priority Queues And the amazing binary heap Chapter 20 in DS&PS Chapter 6 in DS&AA.
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
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.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting an.
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting an.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Lecture 11 Binary Heap King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
CSE 326: Data Structures Lecture #17 Priority Queues Alon Halevy Spring Quarter 2001.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
1 Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
data ordered along paths from root to leaf
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Priority Queues (Heaps)
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
8 January Heap Sort CSE 2011 Winter Heap Sort Consider a priority queue with n items implemented by means of a heap  the space used is.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
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.
CS 367 Introduction to Data Structures Lecture 8.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Priority Queues and Binary Heaps Fundamental Data Structures and Algorithms Peter Lee February 4, 2003.
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
CS 201 Data Structures and Algorithms
October 30th – Priority QUeues
Binary Heaps What is a Binary Heap?
Heaps, Heap Sort, and Priority Queues
Priority Queues (Heaps)
Binary Heaps What is a Binary Heap?
Binary Heaps What is a Binary Heap?
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
7/23/2009 Many thanks to David Sun for some of the included slides!
Heaps, Heap Sort, and Priority Queues
CMSC 341: Data Structures Priority Queues – Binary Heaps
Instructor: Lilian de Greef Quarter: Summer 2017
Heaps and the Heapsort Heaps and priority queues
CE 221 Data Structures and Algorithms
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
CSE373: Data Structures & Algorithms Lecture 7: Binary Heaps, Continued Dan Grossman Fall 2013.
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heaps & Multi-way Search Trees
Presentation transcript:

Priority Queues and Heaps Fundamental Data Structures and Algorithms Klaus Sutner January 29, 2004

Plan Today: Demo: intelligent math handbook Priority Queues Binary heaps Implementation questions Reading: Chapter 20 in MAW. See also 6.7 and 6.8.

We want to simulate a chain of events E i. Each event E i is associated with a time t i. No problem: sort the times, and run through them in order. t 1 < t 2 < t 3 <... < t n But what if an event can cause the creation of some new events in the future? A Simulation

We can still sort the original events by time, but not the additional ones: we don't yet know what/when they are. We need to be able to dynamically - insert an event into a data structure, and - extract the next event. And, of course, the operations should be cheap. A Simulation

Suppose a robot wants to cross a street. No jaywalking, it can only move straight forward or backward. Maximum speed of the robot is c. Cars are coming from either side, but the robot has complete information about when and where they cross your line. Problem: Find a way for the robot to get across without getting hit. An Example: Street Crossing

How do we model the given information? In space-time a car is just a rectangular box. Movement of the robot is constrained to a light- cone. More Street Crossing time space

Hugging an obstacle (not that unrealistic, just inflate the obstacles to account for safety buffer). More Street Crossing

Note how obstacles may be “fused” together. A Death Trap

The times when obstacles appear/disappear are clearly events. But there are more: The times when the robot touches an obstacle. Or when the light cones from both ends of a disappearing obstacle meet. Note that these events have to be computed as we go along, there is no way to compute them all at the outset. Street Crossing

Note how the first event happens before the first obstacle arrives! We need to go backwards in time, starting at the end of the last obstacle. Time Travel

How does this translate into a data structure? What are the crucial operations? What would be a simple (not necessarily efficient) implementation? Where is room for improvement? Towards An Algorithm

Priority Queue Interface Crucial Operations insert find-min delete-min Think about just inserting times (real numbers), but in reality there is associated information.

Some Applications Event simulations Shortest path computation Huffman coding Sorting (sort of) Not a universal data structure (like hash tables) but irreplaceable in some cases.

PQ Interface Requires total order on the elements of the universe. Could be done with any traversable container: just run through and find the min. We would like much better performance. Binary search trees come to mind: min element is in the leftmost position.

Linked list  deleteMinO(1)O(N)  insert O(N)O(1) Search trees  All operationsO(log N) Heaps avg (assume random)worst  deleteMinO(log N) O(log N)  insert2.6O(log N) special case :  buildheapO(N)O(N) i.e., insert*N or Possible priority queue implementations

Linked list  deleteMinO(1)O(N)  insert O(N)O(1) Search trees  All operationsO(log N) Heaps avg (assume random)worst  deleteMinO(log N) O(log N)  insert2.6O(log N) special case :  buildheapO(N)O(N) i.e., insert*N or Possible priority queue implementations

Linked list  deleteMinO(1)O(N)  insert O(N)O(1) Search trees  All operationsO(log N) Heaps avg (assume random)worst  deleteMinO(log N) O(log N)  insert2.6O(log N)  buildheapO(N)O(N) N inserts or Possible priority queue implementations

A Digression: Perfect and Complete Binary Trees

Perfect binary trees

Perfect binary trees

Perfect binary trees

Perfect binary trees How many nodes? h=3 N=15

Perfect binary trees How many nodes?  In general: N = = 2 h  Most of the nodes are leaves h=3h=3 N=15

A Serious Proof Define PBT by structural induction: - nil is a PBT and H(nil) = 0 - (a,L,R) is a PBT whenever L and R are PBTs and H(L) = H(R). Moreover, H( (a,L,R) ) = H(L) + 1. size(nil) = 0 size( (a,L,R) ) = size(L) + size(R) + 1. Then size(T) = 2 H(T) -1.

Quiz Break

Red-green quiz In a perfect binary tree, what is the sum of the heights of the nodes?  Give a mathematical characterization, the prettier the better.  Give a tight upper bound (in big-O terms)

Perfect binary trees What is the sum of the heights? S = < N = O(N) h=3 N=15

Complete binary trees

Complete binary trees

Representing complete binary trees Linked structures?

Representing complete binary trees Linked structures? No! Instead, use arrays!

Representing complete binary trees Arrays  Parent at position i in the array  Children at positions 2i and 2i

Representing complete binary trees Arrays ( 1-based )  Parent at position i  Children at 2i and 2i

Representing complete binary trees Arrays ( 1-based )  Parent at position i  Children at 2i and 2i

Representing complete binary trees Arrays ( 1-based )  Parent at position i  Children at 2i (and 2i+1)

Representing complete binary trees Arrays ( 1-based )  Parent at position i  Children at 2i and 2i+1. public class BinaryHeap { private Comparable[] heap; private int size; public BinaryHeap(int capacity) { size=0; heap = new Comparable[capacity+1]; }...

Representing complete binary trees Arrays  Parent at position i  Children at 2i and 2i+1. Example: find the leftmost child int left=1; for(; left<size; left*=2); return heap[left/2]; Example: find the rightmost child int right=1; for(; right<size; right=right*2+1); return heap[(right-1)/2];

Implementing Priority Queues with Binary Heaps

Binary heaps: the invariant Representation invariants 1. Structure property Complete binary tree (i.e. the elements of the heap are stored in positions 1…size of the array) 2. Heap order property Parent keys less than children keys

Heaps Representation invariant 1. Structure property Complete binary tree Hence: efficient compact representation 2. Heap order property Parent keys less than children keys Hence: rapid insert, findMin, and deleteMin O(log(N)) for insert and deleteMin O(1) for findMin

The heap order property Each parent is less than each of its children. Hence: Root is less than every other node.  (obvious proof by induction)

Operating with heaps Representation invariant: All methods must: 1. Produce complete binary trees 2. Guarantee the heap order property All methods may assume 1. The tree is initially complete binary 2. The heap order property holds

Constructor method All methods must: 1. Produce complete binary trees Trivially true 2. Guarantee the heap order property Also trivially true This is the base case

findMin () The code public boolean isEmpty() { return size == 0; } public Comparable findMin() { if(isEmpty()) return null; return heap[1]; } Does not change the tree  Trivially preserves the invariant

insert (Comparable x) Process 1. Create a “hole” at the next tree cell for x. heap[size+1] This preserves the completeness of the tree. 2. Percolate the hole up the tree until the heap order property is satisfied. This assures the heap order property is satisfied.

insert (Comparable x) Process 1. Create a “hole” at the next tree cell for x. heap[size+1] This preserves the completeness of the tree assuming it was complete to begin with. 2. Percolate the hole up the tree until the heap order property is satisfied. This assures the heap order property is satisfied assuming it held at the outset.

Percolation up public void insert(Comparable x) throws Overflow { if(isFull()) throw new Overflow(); int hole = ++size; for(; hole>1 && x.compareTo(heap[hole/2])<0; hole/=2) heap[hole] = heap[hole/2]; heap[hole] = x; }

Percolation up Bubble the hole up the tree until the heap order property is satisfied. hole = 11 HOP false Not really there... 21

Percolation up Bubble the hole up the tree until the heap order property is satisfied. hole = 11hole = 5 HOP falseHOP false

Percolation up Bubble the hole up the tree until the heap order property is satisfied. hole = 5hole = 2 HOP falseHOP true done

Percolation up public void insert(Comparable x) throws Overflow { if(isFull()) throw new Overflow(); int hole = ++size; for(; hole>1 && x.compareTo(heap[hole/2])<0; hole/=2) heap[hole] = heap[hole/2]; heap[hole] = x; } Integer division

deleteMin() /** * Remove the smallest item from the priority queue. the smallest item, or null, if empty. */ public Comparable deleteMin( ) { if(isEmpty()) return null; Comparable min = heap[1]; heap[1] = heap[size--]; percolateDown(1); return min; } Temporarily place last element at top Grab min element !!!

Percolation down Bubble the transplanted leaf value down the tree until the heap order property is satisfied

Percolation down Bubble the transplanted leaf value down the tree until the heap order property is satisfied

Percolation down Bubble the transplanted leaf value down the tree until the heap order property is satisfied done

percolateDown(int hole) private void percolateDown( int hole ) { int child = hole; Comparable tmp = heap[hole]; for( ; hole*2 <= size; hole=child ) { child = hole * 2; if(child!=size && heap[child+1].compareTo(heap[child]) < 0) child++; if(array[child].compareTo(tmp) < 0) heap[hole] = heap[child]; else break; } heap[hole] = tmp; } Is there a right child? Bubble up if smaller than tmp Initially 1 Select smaller child Finally, place the orphan Start at left child Exit loop if not bubbling

deleteMin () Observe that both components of the representation invariant are preserved by deleteMin. 1. Completeness The last cell ( heap[size] ) is vacated, providing the value to percolate down. This assures that the tree remains complete. 2. Heap order property

deleteMin () Observe that both components of the representation invariant are preserved by deleteMin. 1. Completeness The last cell ( heap[size] ) is vacated, providing the value to percolate down. This assures that the tree remains complete. 2. Heap order property

deleteMin () Observe that both components of the representation invariant are preserved by deleteMin. 1. Completeness The last cell ( heap[size] ) is vacated, providing the value to percolate down. This assures that the tree remains complete. 2. Heap order property The percolation algorithm assures that the orphaned value is relocated to a suitable position.

buildHeap() Start with complete (unordered) tree Starting from bottom, repeatedly call percolateDown() Void buildHeap () { for (int i = size/2; i>0; i--) percolateDown(i); }

buildHeap() performance At each iteration, have to do work proportional to the height of the current node Therefore, total running time is bounded by the sum of the heights of all of the nodes

Another Digression: Invariants

Representation Invariants Necessary for a clear understand an algorithm or piece of code. The most useful kind of comment you can make in a piece of code!

(Invariants) Plus ça change, plus c’est la même chose. The role of an induction hypothesis

(Invariants and induction) Induction hypothesis  What you are allowed to assume At the start About the result values of a recursive call About the object state when a method is called  What you must deliver At the end Of the result values when the recursive call returns About the object state when the method returns

(Invariants and induction) Induction hypothesis  What you are allowed to assume At the start of a loop iteration About the result values of a recursive call About the object state when a method is called  What you must deliver At the end of the loop iteration Of the result values when the recursive call returns About the object state when the method returns

(Invariants and induction) Induction hypothesis  What you are allowed to assume About the result values of a recursive call About the object state when a method is called  What you must deliver Of the result values when the recursive call returns About the object state when the method returns

(Invariants and induction) Induction hypothesis  What you are allowed to assume At the start of a loop iteration About the result values of a recursive call About the object state when a method is called  What you must deliver At the end of the loop iteration Of the result values when the recursive call returns About the object state when the method returns

(Invariants and induction) Induction hypothesis  What you are allowed to assume At the start of a loop iteration About the result values of a recursive call About the object state when a method is called  What you must deliver At the end of the loop iteration Of the result values when the recursive call returns About the object state when the method returns

(Invariants) Plus ça change, plus c’est la même chose The role of an induction hypothesis Invariants in programs  Loop invariants  Recursion invariants  Representation invariants

(Representation invariant) What must always be true of the data structure when an operation completes. Theorem :  Suppose each constructor assures the representation invariant is initially correct.  Suppose each method preserves the representation invariant, assuming it is true initially.  Then the representation invariant will always be true at the completion of each method.

(Representation invariant) What must always be true of the data structure when an operation completes. Theorem :  Suppose each constructor assures the representation invariant is initially correct.  Suppose each method preserves the representation invariant, assuming it is true initially.  Then the representation invariant will always be true at the completion of each method.

(Representation invariants) What must always be true of the data structure when an operation completes. Theorem:  Suppose each constructor assures the representation invariant is initially correct.  Suppose each method preserves the representation invariant, assuming it is true initially.  Then the representation invariant will always be true at the completion of each method.  Assuming the code is not concurrent!

Heapsort Obviously we can use a priority queue to sort... just insert all the keys then do repeated deleteMins However we can take advantage of the fact that the heap always uses the first part of the array to do this with no extra space. This is called heapsort.

Heapsort Reverse the sense of the heap order (largest at the root) Start from position 0 in the array (children are 2i+1 and 2i+2) Call it percDown(a, i, len) Public static void heapsort(Comparable[] a) { for(int i = a.length/2; i>=0; i--) percDown(a, i, a.length); for (int j = a.length-1; j>0; j--) { swapReferences(a, 0, j); percDown(a, 0, j); }

Heapsort invariant At the start of the loop over j:  a[j]…a[a.length-1] are sorted  a[0]…a[j-1] are a heap

Homework 3 On Blackboard. You’re all working on it already. Right?