Sorting and Searching

Similar documents
Sorting and Searching

Chapter 6 Heapsort 1

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

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

Priority Queues and Binary Heaps

Algorithms and Theory of Computation. Lecture 7: Priority Queue

Heaps, Heap Sort, and Priority Queues.

CSCI2100B Data Structures Heaps

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

Data Structures and Algorithms

Recall: Properties of B-Trees

Algorithms and Data Structures

9. Heap : Priority Queue

CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues. Ruth Anderson Winter 2019

Programming II (CS300)

Binary heaps (chapters ) Leftist heaps

CS711008Z Algorithm Design and Analysis

CS711008Z Algorithm Design and Analysis

Binary Heaps. CSE 373 Data Structures Lecture 11

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

CE 221 Data Structures and Algorithms

CH 8. HEAPS AND PRIORITY QUEUES

CH. 8 PRIORITY QUEUES AND HEAPS

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

Topic: Heaps and priority queues

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

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

EECS 477: Introduction to algorithms. Lecture 10

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

Heaps and Priority Queues

Priority Queues Heaps Heapsort

CSE332: Data Abstractions Lecture 4: Priority Queues; Heaps. James Fogarty Winter 2012

Data Structures and Algorithms for Engineers

Properties of a heap (represented by an array A)

Heaps Goodrich, Tamassia. Heaps 1

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

Priority Queues. Priority Queues Trees and Heaps Representations of Heaps Algorithms on Heaps Building a Heap Heapsort.

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

Module 2: Priority Queues

Design and Analysis of Algorithms

Module 2: Priority Queues

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

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

Priority Queues (Heaps)

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

Priority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Dictionaries. Priority Queues

Data Structures Lesson 9

CS350: Data Structures Heaps and Priority Queues

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

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn

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

Module 2: Priority Queues

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

Programming II (CS300)

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

Stores a collection of elements each with an associated key value

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

Fibonacci Heaps. Structure. Implementation. Implementation. Delete Min. Delete Min. Set of min-heap ordered trees min

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

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

Appendix A and B are also worth reading.

Binary Heaps in Dynamic Arrays

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

Heapsort. Heap data structure

Priority Queues Heaps Heapsort

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

CSC263 Week 2. Larry Zhang

Priority queue ADT Heaps. Lecture 21

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

Programming Abstractions

COMP 103 RECAP-TODAY. Priority Queues and Heaps. Queues and Priority Queues 3 Queues: Oldest out first

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Heaps. 2/13/2006 Heaps 1

quiz heapsort intuition overview Is an algorithm with a worst-case time complexity in O(n) data structures and algorithms lecture 3

Chapter 6 Heap and Its Application

Lecture Summary CSC 263H. August 5, 2016

Priority Queues. Binary Heaps

CSE 373: Data Structures and Algorithms

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

l Heaps very popular abstract data structure, where each object has a key value (the priority), and the operations are:

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

Introduction to Algorithms 3 rd edition

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

Priority Queues. Meld(Q 1,Q 2 ) merge two sets

Administration CSE 326: Data Structures

CSE 548: Analysis of Algorithms. Lectures 14 & 15 ( Dijkstra s SSSP & Fibonacci Heaps )

CS165: Priority Queues, Heaps

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

BM267 - Introduction to Data Structures

Algorithms and Data Structures

CSE 5311 Notes 4a: Priority Queues

Lecture 5 Using Data Structures to Improve Dijkstra s Algorithm. (AM&O Sections and Appendix A)

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Adding a Node to (Min) Heap. Lecture16: Heap Sort. Priority Queue Sort. Delete a Node from (Min) Heap. Step 1: Add node at the end

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

The priority is indicated by a number, the lower the number - the higher the priority.

CMSC 341 Lecture 14: Priority Queues, Heaps

Binary Heaps. CpSc 212: Algorithms and Data Structures. School of Computing Clemson University Summer, Raghuveer Mohan. Thanks to Brian C.

Transcription:

Sorting and Searching Lecture 2: Priority Queues, Heaps, and Heapsort Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 1 / 24

Priority Queue: Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. Consider the printing pool at this moment. Sizes: Job A 100 pages Job B 10 pages Job C 1 page Average finish time with FIFO service: (100+110+111) / 3 = 107 time units Average finish time for shortest-job-first service: (1+11+111) / 3 = 41 time units Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 2 / 24

Priority Queue: Motivating Example The elements in the queue are printing jobs, each with the associated number of pages that serves as its priority Processing the shortest job first corresponds to extracting the smallest element from the queue Insert new printing jobs as they arrive A queue capable of supporting two operations: Insert and Extract-Min? Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 3 / 24

Priority Queue Priority queue is an abstract data structure that supports two operations Insert: inserts the new element into the queue Extract-Min: removes and returns the smallest element from the queue Extract-Min Priority Queue Insert Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 4 / 24

Possible Implementations unsorted list + a pointer to the smallest element Insert in O(1) time Extract-Min in O(n) time, since it requires a linear scan to find the new minimum sorted array Insert in O(n) time Extract-Min in O(1) time sorted doubly linked list Insert in O(n) time Extract-Min in O(1) time Question Is there any data structure that supports both these priority queue operations in O(log n) time? Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 5 / 24

(Binary) Heap Pack to the left Heaps are almost complete binary trees All levels are full except possibly the lowest level If the lowest level is not full, then nodes must be packed to the left Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 6 / 24

Heap-order Property 1 4 2 5 2 5 4 3 6 1 3 6 A min-heap Not a heap Heap-order property: The value of a node is at least the value of its parent Min-heap Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 7 / 24

Heap Properties If the heap-order property is maintained, heaps support the following operations efficiently (assume there are n elements in the heap) Insert in O(log n) time Extract-Min in O(log n) time Structure properties A heap of height h has between 2 h to 2 h+1 1 nodes. Thus, an n-element heap has height Θ(log n). The structure is so regular, it can be represented in an array and no links are necessary!!! Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 8 / 24

Array Implementation of Heap A B D E F G C 1 2 3 4 5 6 7 8 9 10 A B C D E F G H I J H I J The root is in array position 1 For any element in array position i The left child is in position 2i The right child is in position 2i + 1 The parent is in position i/2 We will draw the heaps as trees, with the understanding that an actual implementation will use simple arrays Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 9 / 24

Insertion Add the new element to the next available position at the lowest level Restore the min-heap property if violated General strategy is percolate up (or bubble up): if the parent of the element is larger than the element, then interchange the parent with child. 2 3 4 5 4 6 5 6 7 8 Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 10 / 24

Insertion Add the new element to the next available position at the lowest level Restore the min-heap property if violated General strategy is percolate up (or bubble up): if the parent of the element is larger than the element, then interchange the parent with child. 2 3 4 5 4 6 5 6 7 8 1 Insert 1 Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 11 / 24

Insertion Add the new element to the next available position at the lowest level Restore the min-heap property if violated General strategy is percolate up (or bubble up): if the parent of the element is larger than the element, then interchange the parent with child. 2 3 4 5 1 6 5 6 7 8 4 swap Percolate up to maintain the min-heap property Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 12 / 24

Insertion Add the new element to the next available position at the lowest level Restore the min-heap property if violated General strategy is percolate up (or bubble up): if the parent of the element is larger than the element, then interchange the parent with child. 2 1 swap 4 5 3 6 5 6 7 8 4 Percolate up to maintain the min-heap property Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 13 / 24

Insertion Add the new element to the next available position at the lowest level Restore the min-heap property if violated General strategy is percolate up (or bubble up): if the parent of the element is larger than the element, then interchange the parent with child. swap 1 2 4 5 3 6 5 6 7 8 4 Percolate up to maintain the min-heap property Correctness: after each swap, the min-heap property is satisfied for the subtree rooted at the new element Time complexity = O(height) = O(log n) Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 14 / 24

Extract-Min: First Attempt 2 3 4 3 4 4 5 6 5 4 5 6 5 6 7 8 6 7 8 3 3 3 4 4 4 4 4 4 5 6 5 5 6 5 6 5 6 5 6 7 8 6 7 8 7 8 Min-heap property preserved, but completeness not preserved! Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 15 / 24

Extract-Min Copy the last element to the root (i.e., overwrite the minimum element stored there) Restore the min-heap property by percolate down (or bubble down): if the element is larger than either of its children, then interchange it with the smaller of its children. 2 3 4 4 5 6 5 6 7 8 Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 16 / 24

Extract-Min Copy the last element to the root (i.e., overwrite the minimum element stored there) Restore the min-heap property by percolate down (or bubble down): if the element is larger than either of its children, then interchange it with the smaller of its children. 8 3 4 4 5 6 5 6 7 Copy the last element to the root Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 17 / 24

Extract-Min Copy the last element to the root (i.e., overwrite the minimum element stored there) Restore the min-heap property by percolate down (or bubble down): if the element is larger than either of its children, then interchange it with the smaller of its children. swap 3 8 4 4 5 6 5 6 7 Percolate down to maintain the min-heap property Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 18 / 24

Extract-Min Copy the last element to the root (i.e., overwrite the minimum element stored there) Restore the min-heap property by percolate down (or bubble down): if the element is larger than either of its children, then interchange it with the smaller of its children. 3 swap 4 4 8 5 6 5 6 7 Percolate down to maintain the min-heap property Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 19 / 24

Extract-Min Copy the last element to the root (i.e., overwrite the minimum element stored there) Restore the min-heap property by percolate down (or bubble down): if the element is larger than either of its children, then interchange it with the smaller of its children. 3 4 4 swap 8 6 5 6 5 7 Percolate down to maintain the min-heap property Correctness: after each swap, the min-heap property is satisfied for all nodes except the node containing the element (with respect to its children) Time complexity = O(height) = O(log n) Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 20 / 24

Heapsort Build a binary heap of n elements the minimum element is at the top of the heap insert n elements one by one = O(n log n) (A more clever approach can do this in O(n) time.) Perform n Extract-Min operations the elements are extracted in sorted order each Extract-Min operation takes O(log n) time = O(n log n) Total time complexity: O(n log n) Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 21 / 24

Summary A Priority queue is an abstract data structure that supports two operations: Insert and Extract-Min. If priority queues are implemented using heaps, then these two operations are supported in O(log n) time. Heapsort takes O(n log n) time, which is as efficient as merge sort and quicksort. Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 22 / 24

New Operation Sometimes priority queues need to support another operation called Decrease-Key Decrease-Key: decreases the value of one specified element Decrease-Key is used in later algorithms, e.g., in Dijkstra s algorithm for finding Shortest Path Trees Question How can heaps be modified to support Decrease-Key in O(log n) time? Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 23 / 24

Going Further For some algorithms, there are other desirable Priority Queue operations, e.g., Delete an arbitrary item and Melding or taking the union of two priority queues There is a tradeoff between the costs of the various operations. Depending upon where the data structure is used, different priority queues might be better. Most famous variants are Binomial Heaps and Fibonacci Heaps Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 24 / 24