Heaps and Priority Queue

Slides:



Advertisements
Similar presentations
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
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
Advanced Data Structures Chapter 16. Priority Queues Collection of elements each of which has a priority. Does not maintain a first-in, first-out discipline.
Heapsort By: Steven Huang. What is a Heapsort? Heapsort is a comparison-based sorting algorithm to create a sorted array (or list) Part of the selection.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
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 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Chapter 21 Binary Heap.
data ordered along paths from root to leaf
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.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
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.
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.
Priority Queues, Heaps, and Heapsort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
Priority Queues CS /02/05 L7: PQs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Priority Queues, Heaps, and Heapsort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1 Last modified: 2/22/2016.
Priority Queues, Heaps, and Heapsort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
"Teachers open the door, but you must enter by yourself. "
CSE373: Data Structures & Algorithms
Heap Chapter 9 Objectives Define and implement heap structures
Chapter 11 Heap.
Heaps (8.3) CSE 2011 Winter May 2018.
Data Structures and Algorithms I Day 9, 9/22/11 Heap Sort
Heaps, Heap Sort and Priority Queues
Priority Queues and Heaps
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Binary Heaps What is a Binary Heap?
Bohyung Han CSE, POSTECH
Heapsort.
Priority Queues Linked-list Insert Æ Æ head head
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!
Chapter 8 – Binary Search Tree
Part-D1 Priority Queues
Data Structures & Algorithms Priority Queues & HeapSort
Heapsort Heap & Priority Queue.
CSE 373: Data Structures and Algorithms
Priority Queues.
Tree Representation Heap.
Ch. 8 Priority Queues And Heaps
"Teachers open the door, but you must enter by yourself. "
Priority Queues & Heaps
CHAPTER 11: Priority Queues and Heaps
Heaps and Priority Queues
HEAPS.
Priority Queues.
Heapsort.
1 Lecture 10 CS2013.
Priority Queues.
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Computer Algorithms CISC4080 CIS, Fordham Univ.
Priority Queue and Heap
Priority Queues Binary Heaps
EE 312 Software Design and Implementation I
Presentation transcript:

Heaps and Priority Queue Reference: Chapter 2, Algorithms,4rd Edition, Robert Sedgewick, Kevin Wayne

Outline Priority Queue Binary Heaps Implementation and demo HeapSort 11/7/2018 Prioriry Queue

Example 1: Scheduling EDF (Earliest Deadline First) Scheduling Q T1 … Tasks wait in the queue A task with a shorter deadline has a higher priority Executes a job with the earliest deadline Q T1 … T3 T2 Tn … 11/7/2018 Prioriry Queue

Example 1: Cont. T1 Q T3 T2 … Tn … Tn+1 T3 T2 … Q Tn … Task T1 is dispatched and removed from the Task waiting queue. Before T1 is completed, Task Tn+1 arrives. It has the earliest deadline. Tn+1 will be dispatched next. T1 Q T3 T2 … Tn … Tn+1 T3 T2 … Q Tn … 11/7/2018 Prioriry Queue

Priority Queue EDF scheduler processes Tasks in order. But not necessarily in full sorted order and not necessarily all at once. An appropriate data type for Task Waiting Queue supports two operations: remove the maximum priority task and insert new tasks. Such a data type is called a priority queue. Priority queues are characterized by the remove the maximum and insert operations. 11/7/2018 Prioriry Queue

Priority Queue Interface public interface PriorityQueue <T extends Comparable<T> > { void insert(T t); void remove() throws EmptyQueueException; T top() throws EmptyQueueException; boolean empty(); } 11/7/2018 Prioriry Queue

Example 2: Statistics Find the largest M items in a stream of N items (N huge, M large) N is huge, cannot sort in memory M is large, insert, remove must be fast. Order of growth of finding the largest M in a stream of N items Implementation Time Space Sort N log N M Array N M 11/7/2018 Prioriry Queue

Elementary Implementations Unordered Array: Ordered Array: Linked List: Binary Tree Order-of-growth of running time for priority queue with N items 5 1 4 8 2 7 6 3 1 2 3 4 5 6 7 8 Implementation Insert Remove Max Max Unordered Array 1 N Ordered Array Linked List (unsorted) Goal Log N 11/7/2018 Prioriry Queue

Binary Heap Complete Binary Tree Each node is larger than (or equal to) its two children (if any). 11/7/2018 Prioriry Queue

Complete Binary Tree in Nature 11/7/2018 Prioriry Queue

Binary Heap Properties The largest is found at the root. Height of complete tree with N nodes is ⌊ lg N⌋ Height only increases when N is a power of 2 11/7/2018 Prioriry Queue

Binary Heap Representations Array representation of a complete binary tree Take nodes in level order No explicit links needed 11/7/2018 Prioriry Queue

Binary Heap Representations Largest key is a[1], which is root of binary tree. Can use array indices to move through tree. Parent of node at k is at k/2. two children of the node at k are in positions 2k and 2k + 1. 11/7/2018 Prioriry Queue

Algorithms on Heaps Promotion: Child's key becomes larger key than its parent's key. To eliminate the violation: Exchange key in child with key in parent. Repeat until heap order restored. private void swim(int k) { while (k > 1 && less(k/2, k)){ swap(k, k/2); k = k/2; } 11/7/2018 Prioriry Queue

Algorithms on Heaps Insertion in a heap: Insert. Add node at end, then swim it up. Cost. At most lg N compares. public void insert(T t){ pqArray.add(t); Size++; swim(Size); } 11/7/2018 Prioriry Queue

Algorithms on Heaps Demotion: Parent's key becomes smaller than one (or both) of its children's keys. To eliminate the violation: Exchange key in parent with key in larger child. Repeat until heap order restored. private void sink(int k){ while(2 * k <= Size){ int j = 2*k; if(j< Size && less(j,j+1)) j++; if(!less(k,j)) break; swap(k,j); k = j; } 11/7/2018 Prioriry Queue

Algorithms on Heaps Remove the maximum in a heap: Delete max: Replace root with node at end, then sink it down. Cost: At most 2 lg N compares. public void remove(){ if(Size == 0){ throw new EmptyQueueException("Queue is empty."); } pqArray.set(1,pqArray.get(Size)); pqArray.remove(Size); Size--; sink(1); 11/7/2018 Prioriry Queue

Binary Heap Demo Insertion 35 33 26 4 5 15 24 Insert 34 12 1 2 21 34 23 21 Violation. swim -- 35 26 33 15 24 5 4 12 1 23 21 2 34 11/7/2018 Prioriry Queue

Binary Heap Demo Insertion 35 33 26 4 5 15 24 12 1 2 21 34 23 Violation. swim -- 35 26 33 15 24 5 4 12 1 23 21 2 34 11/7/2018 Prioriry Queue

Binary Heap Demo Insertion 35 33 26 4 5 15 34 24 12 1 2 23 21 Violation. swim -- 35 26 33 15 24 5 34 4 12 1 23 21 2 11/7/2018 Prioriry Queue

Binary Heap Demo Insertion 35 Done! 33 26 4 15 34 24 Violation. swim 12 1 2 Violation. swim 23 21 5 -- 35 26 33 15 24 34 4 12 1 23 21 2 5 11/7/2018 Prioriry Queue

Binary Heap Demo Remove max: 35 34 26 4 33 15 24 12 1 2 Delete the last leaf 23 21 5 5 -- 35 26 34 15 24 33 4 12 1 23 21 2 5 5 Move the last leaf to root 11/7/2018 Prioriry Queue

Binary Heap Demo Remove Violation. sink 5 Violation. sink 34 26 4 33 15 24 12 1 2 23 21 -- 5 26 34 15 24 33 4 12 1 23 21 2 11/7/2018 Prioriry Queue

Binary Heap Demo Remove 34 Violation. sink 5 26 4 33 15 24 12 1 2 23 21 -- 34 26 5 15 24 33 4 12 1 23 21 2 11/7/2018 Prioriry Queue

Binary Heap Demo Remove 34 33 26 4 5 15 24 12 1 2 23 21 Done! -- 34 26 Prioriry Queue

Binary Heap Java Code Demo File name Description PriorityQueue.java Interface MaxPQ.java PQ implementation GraphVizWrite.java Visualize the heap EmptyQueueException.java Exception MaxPQTest.java main method InputHelper.java input utility 11/7/2018 Prioriry Queue

Cost summary Implementation Insert Remove Max Max Unordered Array 1 N Linked List (unsorted) Binary Heap Log N 11/7/2018 Prioriry Queue

Immutability of keys Assumption: client does not change keys while they're on the PQ. Best practice: use immutable keys. Immutability: implementing in Java Immutable data type. Can't change the data type value once created. Immutable. String, Integer, Double, Color, Vector, Transaction, Point2D. Mutable. StringBuilder, Stack, Counter, Java array. 11/7/2018 Prioriry Queue