Priority Queues & Heaps. CS16: Introduction to Data Structures & Algorithms Spring 2019

Similar documents
Heaps. 2/13/2006 Heaps 1

Heaps Goodrich, Tamassia. Heaps 1

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

CH 8. HEAPS AND PRIORITY QUEUES

CH. 8 PRIORITY QUEUES AND HEAPS

Stores a collection of elements each with an associated key value

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Priority Queues and Heaps. Heaps and Priority Queues 1

Heaps and Priority Queues

Data Structures and Algorithms

Describe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods

Chapter 2: Basic Data Structures

Data Structures and Algorithms

Priority Queues. Outline. COMP9024: Data Structures and Algorithms. Priority Queue ADT. Total Order Relations. Entry ADT

COMP250: Priority queue ADT, Heaps. Lecture 23 Jérôme Waldispühl School of Computer Science McGill University

Algorithms and Data Structures

Priority Queues & Heaps. Chapter 9

Data Structures and Algorithms " Priority Queues!

Sorting and Searching

Programming II (CS300)

Priority queue ADT Heaps. Lecture 21

Programming II (CS300)

Sorting and Searching

Elementary Data Structures 2

Heaps Outline and Required Reading: Heaps ( 7.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Data Structures Lecture 7

Priority Queues. Reading: 7.1, 7.2

Data Structures and Algorithms for Engineers

9. Heap : Priority Queue

Priority Queue ADT ( 7.1) Heaps and Priority Queues 2. Comparator ADT ( 7.1.4) Total Order Relation. Using Comparators in C++

DATA STRUCTURES AND ALGORITHMS

Priority Queues and Binary Heaps

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

CS 240 Fall Mike Lam, Professor. Priority Queues and Heaps

Priority queues. Priority queues. Priority queue operations

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 20, March 24, 2016

Chapter 6 Heapsort 1

Sorting. Outline. Sorting with a priority queue Selection-sort Insertion-sort Heap Sort Quick Sort

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

DATA STRUCTURES AND ALGORITHMS

Heaps. Heaps Priority Queue Revisit HeapSort

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

CS350: Data Structures Heaps and Priority Queues

10/1/2018 Data Structure & Algorithm. Circularly Linked List Doubly Linked List Priority queues Heaps

Implementations. Priority Queues. Heaps and Heap Order. The Insert Operation CS206 CS206

Binary Heaps in Dynamic Arrays

Heapsort. Heap data structure

CSED233: Data Structures (2017F) Lecture9: Priority Queues and Heaps

Height of a Heap. Heaps. 1. Insertion into a Heap. Heaps and Priority Queues. The insertion algorithm consists of three steps

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

Priority Queues and Heaps. More Data Structures. Priority Queue ADT ( 2.4.1) Total Order Relation. Sorting with a Priority Queue ( 2.4.

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

Priority Queues Heaps Heapsort

Algorithms and Data Structures

Binary heaps (chapters ) Leftist heaps

CMSC 341 Lecture 14: Priority Queues, Heaps

CSCI2100B Data Structures Heaps

Heaps. A heap is a certain kind of complete binary tree.

Dictionaries. Priority Queues

Module 2: Priority Queues

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Priority Queues Heaps Heapsort

Heap Model. specialized queue required heap (priority queue) provides at least

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

CS 240 Data Structures and Data Management. Module 2: Priority Queues

Priority Queues (Heaps)

CS 240 Data Structures and Data Management. Module 2: Priority Queues

Unit #2: Priority Queues

CS106X Programming Abstractions in C++ Dr. Cynthia Bailey Lee

CS165: Priority Queues, Heaps

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

UNIT III BALANCED SEARCH TREES AND INDEXING

Priority Queues and Heaps. Heaps of fun, for everyone!

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

CS/COE 1501

13.4 Deletion in red-black trees

CSE 214 Computer Science II Heaps and Priority Queues

CE 221 Data Structures and Algorithms

Topics on the Midterm

Priority Queues, Binary Heaps, and Heapsort

COMP 250 Fall priority queues, heaps 1 Nov. 9, 2018

Chapter 9. Priority Queue

Priority Queues and Heaps

CS 10: Problem solving via Object Oriented Programming. Priori9zing 2

Heaps and Priority Queues

Heaps in C. CHAN Hou Pong, Ken CSCI2100 Data Structures Tutorial 7

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi

Data Structures Lesson 9

Recall: Properties of B-Trees

Dictionaries. 2/17/2006 Dictionaries 1

Priority queues. Priority queues. Priority queue operations

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

Binary Heaps. CSE 373 Data Structures Lecture 11

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

csci 210: Data Structures Priority Queues and Heaps

Administration CSE 326: Data Structures

Programming Abstractions

Transcription:

Priority Queues & Heaps CS16: Introduction to Data Structures & Algorithms Spring 2019

Outline Priority Queues Motivation ADT Implementation Heaps insert( ) and upheap( ) removemin( ) and downheap( )

Motivation Priority queues store items with various priorities Examples Plane departures: some flights have higher priority than others Bandwidth management: real-time traffic like Skype transmitted first 3

Priority Queue ADT Stores key/element pairs insert(key, element): key determines position in queue inserts element with key removemin( ): removes pair w/ smallest key and returns element

Priority Queue Implementations 2 minactivity #1 5

Priority Queue Implementations 2 minactivity #1 6

Priority Queue Implementations 1 minactivity #1 7

Priority Queue Implementations 0 minactivity #1 8

Priority Queue Implementation Implementation insert removemin Unsorted Array O(1) O(n) Sorted Array O(n) O(1) Unsorted Linked List O(1) O(n) Sorted Linked List O(n) O(1) Hash Table O(1) O(n) Heap O(log n) O(log n) 9

What is a Heap? Data structure that implements priority queue Heaps can be implemented with Tree Array 2 Tree-based heap 5 6 9 7 10

Heap Properties Binary tree each node has at most 2 children Each node has a priority (key) Heap has an order min-heap: n.parent.key n.key max-heap: n.parent.key n.key Left-complete Height of O(log n) 11

Heap Properties To implement priority queue insert key/element pair at each node (1, Tsunade) (5, Kakashi) (3, Danzo) (9, Naruto) (7, Sakura) 12

Heap insert( ) Need to keep track of insertion node leaf where we will insert new node so we can keep heap left-complete 2 5 6 9 7 insertion node 13

Heap insert( ) Ex: insert(1) replace insertion node w/ new node 2 Heap order violated! 5 6 9 7 1 14

Heap upheap( ) Repair heap: swap new element up tree until keys are sorted First swap fixes everything below new location since every node below 6 s old location has to be at least 6 they must be at least 1 2 5 1 9 7 6 15

Heap upheap( ) One more swap since 1 2 Now left-completeness and order are satisfied 1 5 2 9 7 6 16

Heap insert( ) 2 minactivity #1 17

Heap insert( ) 2 minactivity #2 18

Heap insert( ) 1 minactivity #1 19

Heap insert( ) 0 minactivity #1 20

Heap upheap( ) Summary After inserting a key k, order may be violated upheap( ) restores order by swapping key upward from insertion node terminates when either root is reached or some node whose parent has key at most k Heap insertion has runtime O(log n), why? because heap has height O(log n) perfect binary tree with n nodes has height log(n+1)-1 21

Heap removemin( ) Remove root because it is always the smallest element How can we remove root w/o destroying heap? 1 Heap destroyed! 5 2 5 2 9 7 6 9 7 6 22

Heap removemin( ) Instead swap root with last element & remove it removing last element is easy 1 6 5 2 5 2 9 7 6 9 7 1 6 5 2 9 7 Order destroyed! 23

Heap removemin( ) Now swap root down as necessary 6 2 5 2 5 6 9 7 9 7 Heap is in order! 24

Heap downheap( ) Summary downheap( ) restores order by swapping key downward from root with smaller of 2 children terminates when either leaf is reached or some node whose children has key at least k downheap( ) has runtime O(log n), why? because heap has height O(log n) 25

Heap removemin( ) 2 minactivity #1 26

Heap removemin( ) 2 minactivity #1 27

Heap removemin( ) 1 minactivity #1 28

Heap removemin( ) 0 minactivity #1 29

Summary of Heap insert(key, value) insert value at insertion node insertion node must be kept track of upheap( ) from insertion node as necessary removemin( ) swap root with last item delete (swapped) last item downheap( ) from root as necessary 30

Array-based Heap Heap with n keys can be represented w/ array of size n+1 2 Storing nodes in array Node stored at index i 9 5 7 6 left child stored at index 2i right child stored at index 2i+1 Leaves & edges not stored Cell 0 not used 2 5 6 9 7 0 1 2 3 4 5 Operations insert: store new node at index n+1 removemin: swap w/ index n and remove

Finding Insertion Node Can be found in O(log n) Start at last added node Go up until a left child or root is reached If left child go to sibling (corresponding right child) then go down left until leaf is reached Can be done in O(1) time by using additional data structure need this for project! 32

Priority Queue Implementation Implementation insert removemin Unsorted Array O(1) O(n) Sorted Array O(n) O(1) Unsorted Linked List O(1) O(n) Sorted Linked List O(n) O(1) Hash Table O(1) O(n) Heap O(log n) O(log n) 33

References Slide #4 Queue in French means tail The picture depicts the tail of a whale Slide #7 The picture is of a Transformers character named Junkheap which transforms from a waste management garbage truck Slide #8 The names are characters from the Anime series Naruto (https:// en.wikipedia.org/wiki/naruto) The picture is the symbol of the Hidden Leaf Village (where the character Naruto is from) The heap priorities represent the importance of the character in the village 34