Heaps & Priority Queues

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

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.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
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!
PRIORITY QUEUES AND HEAPS Lecture 16 CS2110 Spring 2015.
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:
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
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.
Topic 24 Heaps "You think you know when you can learn, are more sure when you can write even more when you can teach, but certain when you can program."
PRIORITY QUEUES AND HEAPS Lecture 16 CS2110 Fall 2014.
PRIORITY QUEUES AND HEAPS Lecture 16 CS2110 Spring 2015.
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 AND HEAPS Slides of Ken Birman, Cornell University.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
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”
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
CompSci 100e 8.1 Scoreboard l What else might we want to do with a data structure? AlgorithmInsertionDeletionSearch Unsorted Vector/array Sorted vector/array.
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.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
"Teachers open the door, but you must enter by yourself. "
CSE373: Data Structures & Algorithms Priority Queues
Data Structure By Amee Trivedi.
Priority Queues and Heaps
Chapter 11 Heap.
Priority Queues and Heaps
Heaps And Priority Queues
Priority Queues and Heaps
Week 11 - Friday CS221.
Hashing Exercises.
Compsci 201 Priority Queues & Autocomplete
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
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.
- Alan Perlis Heaps "You think you know when you can learn,
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Building Java Programs
Wednesday, April 18, 2018 Announcements… For Today…
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued
Priority Queues.
i206: Lecture 14: Heaps, Graphs intro.
CSE 373: Data Structures and Algorithms
B-Tree Insertions, Intro to Heaps
- Alan Perlis Topic 24 Heaps "You think you know when you can learn,
Priority Queues.
CSE332: Data Abstractions Lecture 4: Priority Queues
Priority Queues.
Hassan Khosravi / Geoffrey Tien
"Teachers open the door, but you must enter by yourself. "
Data Structures and Analysis (COMP 410)
Priority Queues & Heaps
Heaps and Priority Queues
Binary and Binomial Heaps
CSE 12 – Basic Data Structures
Priority Queues.
Amortized Analysis and Heaps Intro
Priority Queues.
Priority Queues CSE 373 Data Structures.
Priority Queues & Heaps
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
Chapter 9 The Priority Queue ADT
Heaps & Multi-way Search Trees
Heaps.
Presentation transcript:

Heaps & Priority Queues Lecture 16 CS2110 Spring 2018

Announcements A4 due TOMORROW. Late deadline is Sunday. A5 released. Due next Thursday. Deadline for Prelim 1 regrade requests is tomorrow. Remember to complete your TA evaluations by tonight.

Abstract vs concrete data structures Abstract data structures are interfaces they specify only interface (method names and specs) not implementation (method bodies, fields, …) Concrete data structures are classes. Abstract data structures can have multiple possible implementations by different concrete data structures.

Concrete data structures Array LinkedList (singley-linked, doubly-linked) Trees (binary, general, red-black)

Abstract data structures interface List defines an “abstract data type”. It has methods: add, get, remove, … Various classes ("concrete data types") implement List: Class: ArrayList LinkedList Backing storage: array chained nodes add(i, val) O(n) add(0, val) O(1) add(n, val) get(i) get(0) get(n)

Abstract data structures List (ArrayList, LinkedList) Set (HashSet, TreeSet) Map (HashMap, TreeMap) Stack Queue PriorityQueue 55 12 19 16 head tail Both stack and queue efficiently implementable using a singly linked list with head and tail

Priority Queue Data structure in which data items are Comparable Elements have a priority order (smaller elements---determined by compareTo()---have higher priority) remove() return the element with the highest priority break ties arbitrarily

Many uses of priority queues Surface simplification [Garland and Heckbert 1997] Event-driven simulation: customers in a line Collision detection: "next time of contact" for colliding bodies 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 College: prioritizing assignments for multiple classes.

java.util.PriorityQueue<E> interface PriorityQueue<E> { boolean add(E e) {...} //insert e. E poll() {...} //remove/return min elem. E peek() {...} //return min elem. void clear() {...} //remove all elems. boolean contains(E e) boolean remove(E e) int size() {...} Iterator<E> iterator() }

Priority queues as lists Maintain as a 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 an ordered list add() must search the list – O(n) poll() min element at front – O(1) peek() O(1) Maintain as red-black tree add() must search the tree & rebalance – O(log n) poll() must search the tree & rebalance – O(log n) peek() O(log n) Can we do better?

Heaps A heap is a binary tree that satisfies two properties Completeness. Every level of the tree (except last) is completely filled. Heap Order Invariant. Every element in the tree is <= its parent Do not confuse with heap memory, where a process dynamically allocates space–different usage of the word heap.

Completeness Property Every level (except last) completely filled. Nodes on bottom level are as far left as possible. 55 38 22 35 12 19 21 20 6 4 10 8

Completeness Property Not a heap because: missing a node on level 2 bottom level nodes are not as far left as possible 55 38 22 35 12 19 20 4 10 8 missing nodes

Order Property Every element is <= its parent 55 38 22 35 12 19 2 Note: Bigger elements can be deeper in the tree! 20 6 4 10 18

Heap Quiz (a) (b) (c) (d) Which of the following are valid heaps? 11 13 12 15 20 11 10 12 20 11 12 15 14 20 11 10 12 15 20 (a) (b) (c) (d) No No No Yes

Heaps A heap is a binary tree that satisfies two properties Completeness. Every level of the tree (except last) is completely filled. All holes in last level are all the way to the right. Heap Order Invariant. Every element in the tree is <= its parent A heap implements three key methods: add(e): adds a new element to the heap poll(): deletes the max element and returns it peek(): returns the max element

add(e) Time is O(log n) 55 38 22 50 35 12 19 50 22 2 heap maintains balanced tree, so bubble up takes (worst-case) logarithmic time 20 6 4 10 18 50 19 Put in the new element in a new node (leftmost empty leaf) Bubble new element up while greater than parent

poll() Time is O(log n) 55 19 50 55 38 50 19 22 35 12 19 22 2 heap maintains balanced tree, so bubble up takes (worst-case) logarithmic time 20 6 4 10 18 19 Save root element in a local variable Assign last value to root, delete last node. While less than a child, switch with bigger child (bubble down)

peek() 50 Time is O(1) 50 38 22 35 12 19 2 20 6 4 10 18 Return root value

Implementing Heaps public class HeapNode<E> { private E value; private HeapNode left; private HeapNode right; ... }

Implementing Heaps public class Heap<E> { private E[] heap; ... }

Numbering the nodes Number node starting at root row by row, left to right Level-order traversal 55 38 22 1 2 35 12 19 21 3 4 5 6 20 6 4 7 8 9 Children of node k are nodes 2k+1 and 2k+2 Parent of node k is node (k-1)/2

Storing a heap in an array Store node number i in index i of array b Children of b[k] are b[2k +1] and b[2k +2] Parent of b[k] is b[(k-1)/2] 55 38 22 1 2 35 12 19 21 3 4 5 6 20 6 4 7 8 9 parent 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 55 38 22 35 12 19 21 20 6 4 children

add() --assuming there is space /** An instance of a heap */ class Heap<E> { 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= n + 1; bubbleUp(n - 1); // given on next slide }

add(). Remember, heap is in b[0..n-1] class Heap<E> { /** Bubble element #k up to its position. * Pre: heap inv holds except maybe for k */ private void bubbleUp(int k) { // inv: p is parent of k and every elmnt // except perhaps k is <= its parent while ( ) { } int p= (k-1)/2; k > 0 && b[k].compareTo(b[p]) > 0 swap(b[k], b[p]); k= p; p= (k-1)/2;

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

poll() /** Tree has n node. * Return index of bigger child of node k (2k+2 if k >= n) */ public int biggerChild(int k, int n) { int c= 2*k + 2; // k’s right child if (c >= n || b[c-1] > b[c]) c= c-1; return c; }

poll() int k= 0; int c= biggerChild(k, n); c < n && b[k] < b[c] /** Bubble root down to its heap position. Pre: b[0..n-1] is a heap except maybe b[0] */ private void bubbleDown() { // inv: b[0..n-1] is a heap except maybe b[k] AND // b[c] is b[k]’s biggest child while ( ) { } int k= 0; int c= biggerChild(k, n); c < n && b[k] < b[c] swap(b[k], b[c]); k= c; c= biggerChild(k, n);

peek(). Remember, heap is in b[0..n-1] /** Return the largest element * (return null if list is empty) */ public E peek() { if (n == 0) return null; return b[0]; // largest value at root. }

Quiz 2: Let's try it! Here's a heap, stored in an array: [9 5 2 1 2 2] What is the state of the array after execution of add(6)? Assume the existing array is large enough to store the additional element. A. [9 5 2 1 2 2 6] B. [9 5 6 1 2 2 2] C. [9 6 5 1 2 2 2] D.   [9 6 5 2 1 2 2] 

Quiz 2: Let's try it! Here's a heap, stored in an array: [9 5 2 1 2 2] Write the array after execution of add(6) ⇒ [9 5 6 1 2 2 2] 9 1 5 2 2 6 1 4 2 3 5 2 6 6 2

HeapSort Make b[0..n-1] into a max-heap (in place) for (k= n-1; k > 0; k= k-1) { b[k]= poll –i.e. take max element out of heap. } 4 6 14 55 12 55 6 4 14 6 12 0 1 2 3 4 4 12 4 55 55 14 6 6 14 4 4 6 4 6 12 4 12 12 6 14 4 14 55 6 6 4 6 4 14 4 12 1 2 4 6 6 14 3 4

Priority queues as heaps A heap is can be used to implement priority queues Note: need a min-heap instead of a max-heap Gives better complexity than either ordered or unordered list implementation: add(): O(log n) (n is the size of the heap) poll(): O(log n) peek(): O(1)

java.util.PriorityQueue<E> interface PriorityQueue<E> { TIME boolean add(E e) {...} //insert e. log void clear() {...} //remove all elems. E peek() {...} //return min elem. constant E poll() {...} //remove/return min elem. log boolean contains(E e) linear boolean remove(E e) linear int size() {...} constant Iterator<E> iterator() } IF implemented with a heap!

What if the priority is independent from the value? Separate priority from value and do this: add(e, p); //add element e with priority p (a double) THIS IS EASY! Be able to change priority change(e, p); //change priority of e to p THIS IS HARD! Big question: How do we find e in the heap? Searching heap takes time proportional to its size! No good! Once found, change priority and bubble up or down. OKAY Assignment A5: implement this heap! Use a second data structure to make change-priority expected log n time