PRIORITY QUEUES AND HEAPS Lecture 16 CS2110 Spring 2015.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

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.
PRIORITY QUEUES AND HEAPS Lecture 19 CS2110 Spring
Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out The “smallest” element.
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.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
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.
CS 261 – Data Structures Priority Queues & Heaps.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
1 CS211, Lecture 20 Priority queues and Heaps Readings: Weiss, sec. 6.9, secs When they've got two queues going, there's never any queue!
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
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:
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
COMP 103 Priority Queues, Partially Ordered Trees and Heaps.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Priority Queues Dr. David Matuszek
PRIORITY QUEUES AND HEAPS Lecture 16 CS2110 Fall 2014.
PRIORITY QUEUES AND HEAPS Lecture 16 CS2110 Spring 2015.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
Chapter 13 Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-in-first-out The “smallest”
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.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
CSE 373: Data Structures and Algorithms Lecture 11: Priority Queues (Heaps) 1.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
CompSci 100e 8.1 Scoreboard l What else might we want to do with a data structure? AlgorithmInsertionDeletionSearch Unsorted Vector/array Sorted vector/array.
CS 367 Introduction to Data Structures Lecture 8.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Properties: -The value in each node is greater than all values in the node’s subtrees -Complete tree! (fills up from left to right) Max Heap.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Catie Baker Spring 2015.
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 Heaps
CS 201 Data Structures and Algorithms
Priority Queues and Heaps
Heaps And Priority Queues
Hashing Exercises.
Compsci 201 Priority Queues & Autocomplete
Bohyung Han CSE, POSTECH
ADT Heap data structure
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.
Heaps & Priority Queues
Chapter 8 – Binary Search Tree
CSE 373: Data Structures and Algorithms
Hassan Khosravi / Geoffrey Tien
CE 221 Data Structures and Algorithms
Data Structures and Analysis (COMP 410)
Priority Queues & Heaps
Heaps and Priority Queues
Priority Queues.
CSE 373 Priority queue implementation; Intro to heaps
Priority Queues.
Priority Queues CSE 373 Data Structures.
Priority Queues & Heaps
Presentation transcript:

PRIORITY QUEUES AND HEAPS Lecture 16 CS2110 Spring 2015

Readings and Homework Read Chapter 26 “A Heap Implementation” to learn about heaps Exercise: Salespeople often make matrices that show all the great features of their product that the competitor’s product lacks. Try this for a heap versus a BST. First, try and sell someone on a BST: List some desirable properties of a BST that a heap lacks. Now be the heap salesperson: List some good things about heaps that a BST lacks. Can you think of situations where you would favor one over the other? 2 With ZipUltra heaps, you’ve got it made in the shade my friend!

Cool data structures you now know about  Linked lists –singly linked, doubly linked, circular  Binary search trees  BST-like tree for A4 (BlockTree)  Example of how one changes a data structure to make for efficiency purposes: In A4 a Shape (consisting of 1,000 Blocks?) gets moved around, rather than change the position field in each Block, have a field of Shape that gives the displacement for all Blocks. 3

Interface Bag (not In Java Collections) interface Bag implements Iterable { void add(E obj); boolean contains(E obj); boolean remove(E obj); int size(); boolean isEmpty(); Iterator iterator() } Refinements of Bag: Stack, Queue, PriorityQueue 4 Also called multiset Like a set except that a value can be in it more than once. Example: a bag of coins

Stacks and queues are restricted lists Stack (LIFO) implemented as list –add(), remove() from front of list Queue (FIFO) implemented as list –add() on back of list, remove() from front of list These operations are O(1) head tail 5 Both efficiently implementable using a singly linked list with head and tail

Priority queue Bag in which data items are Comparable Smaller elements (determined by compareTo() ) have higher priority remove() return the element with the highest priority = least in the compareTo() ordering break ties arbitrarily 6

Scheduling jobs to run on a computer default priority = arrival time priority can be changed by operator Scheduling events to be processed by an event handler priority = time of occurrence Airline check-in first class, business class, coach FIFO within each class Tasks that you have to carry out. You determine priority Examples of Priority Queues 7

java.util.PriorityQueue 8 boolean add(E e) {...} //insert an element void clear() {...} //remove all elements E peek() {...} //return min element without removing E poll() {...} //remove and return min element boolean contains(E e) boolean remove(E e) int size() {...} Iterator iterator() //an iterator over the priority queue

Priority queues as lists 9 Maintain as unordered list –add() put new element at front – O(1) – poll() must search the list – O(n) – peek() must search the list – O(n) Maintain as ordered list –add() must search the list – O(n) – poll() must search the list – O(n) – peek() O(1) Can we do better?

Important Special Case 10 Fixed number of priority levels 0,...,p – 1 FIFO within each level Example: airline check-in add() – insert in appropriate queue – O(1) poll() – must find a nonempty queue – O(p) first class many miles paying frequent flier

Heap 11 A heap is a concrete data structure that can be used to implement priority queues Gives better complexity than either ordered or unordered list implementation: –add(): O(log n) –poll(): O(log n) O(n log n) to process n elements Do not confuse with heap memory, where the Java virtual machine allocates space for objects – different usage of the word heap

Heap 12 Binary tree with data at each node Satisfies the Heap Order Invariant: Binary tree is complete (no holes) 1. The least (highest priority) element of any subtree is at the root of that subtree. 2. Every level (except last) completely filled. Nodes on bottom level are as far left as possible.

Smallest element in any subtree is always found at the root of that subtree Note: 19, 20 < 35: Smaller elements can be deeper in the tree! Heap 13

Should be complete: * Every level (except last) completely filled. * Nodes on bottom level are as far left as possible. missing nodes Not a heap —has two holes 14

Heap: number nodes as shown children of node k: at 2k + 1 and 2k + 2 parent of node k: at (k-1) / 2 Remember, tree has no holes

Heap nodes in b in order, going across each level from left to right, top to bottom Children b[k] are b[2k + 1] and b[2k + 2] Parent of b[k] b[(k – 1)/2] We illustrate using an array b (could also be ArrayList or Vector) Tree structure is implicit. No need for explicit links! to parent to children

Add e at the end of the array If this violates heap order because it is smaller than its parent, swap it with its parent Continue swapping it up until it finds its rightful place The heap invariant is maintained! add(e) 17

add()

add()

add()

add()

add()

add()

add()

add()

add()

add()

Time is O(log n), since the tree is balanced –size of tree is exponential as a function of depth –depth of tree is logarithmic as a function of size 28 add() to a tree of size n

/** An instance of a heap */ Class Heap { E[] b= new E[50]; //heap is b[0..n-1] int n= 0; // heap invariant is true /** Add e to the heap */ public void add(E e) { b[n]= e; n= b + 1; bubbleUp(n - 1); // given on next slide } 29 add() --assuming there is space

class Heap { /** Bubble element #k up to its position. * Precondition: heap inv true except maybe for element k */ private void bubbleUp(int k) { int p= (k-1)/2; // p is the parent of k // inv: p is k’s parent and // Every element satisfies the heap property // except perhaps k (might be smaller than its parent) while (k>0 && b[k].compareTo(b[p]) < 0) { Swap b[k] and b[p]; k= p; p= (k-1)/2; } 30 add(). Remember, heap is in b[0..n-1]

Remove the least element and return it – (at the root) This leaves a hole at the root – fill it in with the last element of the array If this violates heap order because the root element is too big, swap it down with the smaller of its children Continue swapping it down until it finds its rightful place The heap invariant is maintained! 31 poll()

poll()

poll()

poll()

poll()

poll()

poll()

poll()

poll()

poll()

poll()

poll()

poll()

poll()

poll()

Time is O(log n), since the tree is balanced 46 poll()

/** Remove and return the smallest element (return null if list is empty) */ public E poll() { if (n == 0) return null; E val= b[0]; // smallest value is at root b[0]= b[n-1]; // move last element to root n= n - 1; bubbleDown(0); return val; } 47 poll(). Remember, heap is in b[0..n-1]

/** Bubble root down to its heap position. Pre: b[0..n-1] is a heap except: b[0] may be > than a child */ private void bubbleDown() { int k= 0; // Set c to smaller of k’s children int c= 2*k + 2; // k’s right child if (c >= n || b[c-1].compareTo(b[c]) < 0) c= c-1; // inv: b[0..n-1] is a heap except: b[k] may be > than a child. // Also, b[c] is b[k]’s smallest child while (c 0) { Swap b[k] and b[c]; k= c; c= 2*k + 2; // k’s right child if (c >= n || b[c-1].compareTo(b[c]) < 0) c= c-1; } 48

HeapSort(b, n) —Sort b[0..n-1] 49 1.Make b[0..n-1] into a max-heap (in place) 1.for (k= n-1; k > 0; k= k-1) { b[k]= poll –i.e. take max element out of heap. } A max-heap has max value at root Whet your appetite –use heap to get exactly n log n in-place sorting algorithm. 2 steps, each is O(n log n) We’ll post this algorithm on course website

Many uses of priority queues & heaps  Mesh compression: quadric error mesh simplification  Event-driven simulation: customers in a line  Collision detection: "next time of contact" for colliding bodies  Data compression: Huffman coding  Graph searching: Dijkstra's algorithm, Prim's algorithm  AI Path Planning: A* search  Statistics: maintain largest M values in a sequence  Operating systems: load balancing, interrupt handling  Discrete optimization: bin packing, scheduling  Spam filtering: Bayesian spam filter 50 Surface simplification [Garland and Heckbert 1997]