Algorithms and Data Structures

Similar documents
Algorithms, Spring 2014, CSE, OSU Lecture 2: Sorting

Introduction to Algorithms 3 rd edition

Next. 1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

Heaps and Priority Queues

403: Algorithms and Data Structures. Heaps. Fall 2016 UAlbany Computer Science. Some slides borrowed by David Luebke

Heapsort. Algorithms.

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

The Heap Data Structure

Lecture 3. Recurrences / Heapsort

Chapter 6 Heap and Its Application

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

HEAP. Michael Tsai 2017/4/25

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

Heapsort. Heap data structure

Partha Sarathi Manal

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Lecture 5: Sorting Part A

Topic: Heaps and priority queues

Appendix A and B are also worth reading.

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

Basic Data Structures and Heaps

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

Heaps, Heapsort, Priority Queues

Properties of a heap (represented by an array A)

Heap: A binary heap is a complete binary tree in which each, node other than root is smaller than its parent. Heap example: Fig 1. NPTEL IIT Guwahati

Module 2: Priority Queues

Lecture: Analysis of Algorithms (CS )

Data Structures and Algorithms Week 4

A data structure and associated algorithms, NOT GARBAGE COLLECTION

CSC263 Week 2. Larry Zhang

Questions from the material presented in this lecture

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Binary Heaps in Dynamic Arrays

overview use of max-heaps maxheapify: recall pseudo code heaps heapsort data structures and algorithms lecture 4 heap sort: more

Module 2: Priority Queues

data structures and algorithms lecture 4

Data Structures and Algorithms Chapter 4

Data Structures and Algorithms. Roberto Sebastiani

Module 2: Priority Queues

Design and Analysis of Algorithms

CS 241 Analysis of Algorithms

9. Heap : Priority Queue

Outline. Computer Science 331. Heap Shape. Binary Heaps. Heap Sort. Insertion Deletion. Mike Jacobson. HeapSort Priority Queues.

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

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

Data Structures and Algorithms

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

Sorting and Searching

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

Sorting and Searching

BM267 - Introduction to Data Structures

EST Solutions. Ans 1(a): KMP Algorithm for Preprocessing: KMP Algorithm for Searching:

Recall: Properties of B-Trees

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

Chapter 6 Heapsort 1

1 Tree Sort LECTURE 4. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

Heaps and Priority Queues

csci 210: Data Structures Priority Queues and Heaps

CS2 Algorithms and Data Structures Note 6

Priority queues. Priority queues. Priority queue operations

Sorting Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

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

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci2000, Bowdoin College

Collection of priority-job pairs; priorities are comparable.

Partha Sarathi Mandal

Priority Queues. T. M. Murali. January 29, 2009

Lower Bound on Comparison-based Sorting

CS2 Algorithms and Data Structures Note 6

CMSC 341 Lecture 14: Priority Queues, Heaps

1 Heaps. 1.1 What is a heap? 1.2 Representing a Heap. CS 124 Section #2 Heaps and Graphs 2/5/18

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

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

Heaps. Heaps. A heap is a complete binary tree.

Priority Queues, Heaps, and Heapsort

Heaps, Heap Sort, and Priority Queues.

Heaps Goodrich, Tamassia. Heaps 1

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

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

CSCI2100B Data Structures Heaps

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Priority Queues. T. M. Murali. January 23, T. M. Murali January 23, 2008 Priority Queues

CSE5311 Design and Analysis of Algorithms. Administrivia Introduction Review of Basics IMPORTANT

Outline. CS 561, Lecture 6. Priority Queues. Applications of Priority Queue. For NASA, space is still a high priority, Dan Quayle

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

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

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

Priority Queues Heaps Heapsort

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

Unit #2: Priority Queues

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

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

COSC Advanced Data Structures and Algorithm Analysis Lab 3: Heapsort

Priority queue ADT Heaps. Lecture 21

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

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

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Transcription:

Algorithms and Data Structures CMPSC 465 LECTURES 11-12 Priority Queues and Heaps Adam Smith 1

Priority Queue ADT Dynamic set of pairs (key, data), called elements Supports operations: MakeNewPQ() Insert(S,x) where S is a PQ and x is a (key,data) pair Extract-Max(S) removes and returns the element with the highest key value (or one of them if several have the same value) Example: managing jobs on a processor, want to execute job in queue with the highest priority Can also get a min version, that supports Extract-Min instead of Extract-Max Sometimes support additional operations like Delete, Increase-Key, Decrease-Key, etc. 2

Heaps: Tree Structure Data Structure that implements Priority Queue Conceptually: binary tree In memory: (often) stored in an array Recall: complete binary tree all leaves at the same level, and every internal node has exactly 2 children Heaps are nearly complete binary trees Every level except possibly the bottom one is full Bottom layer is filled left to right 3

Height Heap Height = length of longest simple path from root to some leaf e.g. a one-node heap has height 0, a two- or three-node heap has height 1,... Exercises: What is are max. and min. number of elements in a heap of height h? ans: min = 2 h, max = 2 h+1-1 What is height as a function of number of nodes n? ans: floor( log(n) ) 4

Array representation Instead of dynamically allocated tree, heaps often represented in a fixed-size array smaller constants, works well in hardware. Idea: store elements of tree layer by layer, top to bottom, left to right Navigate tree by calculating positions of neighboring nodes: Left(i) := 2i Right(i) := 2i+1 Parent(i) := floor(i/2) 5

Review Questions Is the following a valid max-heap? 20 10 4 9 6 3 2 8 7 5 12 1 (If not, repair it to make it valid) Is an array sorted in decreasing order a valid maxheap? 6

Local Repair: Heapify Down Two important local repair operations : Heapify Down, Heapify Up Suppose we start from a valid heap and decrease the key of node i, so it is still smaller than parent the two subtrees rooted at i remain valid How do we rearrange nodes to make the heap valid again? Idea: let new, smaller key sink to correct position Find index with largest key among {i, Left(i), Right(i)} If i!= largest, EXCHANGE i with largest and recurse on largest 7

Local Repair: Heapify Down Pseudocode in book Running time: O(height) = O(log n) Tight in worst case Correctness: by induction on height 8

MAX-HEAPIFY(-DOWN) Exercise: what does Max-Heapify do on the following input? 2.5 10 4 9 6 3 2 8 7 5 0 1 This gives us our first PQ operation: Extract-Max(A) 1.tmp := A[1] 2.A[1] := A[heap-size] 3.heap-size := heap-size-1 4.MAX-HEAPIFY-DOWN(A,1) 5.return tmp 9

Local Repair: Heapify Up Two important local repair operations Heapify Down Heapify Up Suppose we start from a valid heap and increase the key of node i, so it is still larger than both children, but might be larger than parent How do we rearrange nodes to make the heap valid again? Idea: let new, larger key float to right position If A[i] > A[Parent(i)], EXCHANGE i with parent and recurse on parent 10

Local Repair: Heapify Up Pseudocode in book (see Increase-key ) Running time: O(log n) tight in worst case Correctness: by induction on height 11

MAX-HEAPIFY(-UP) Exercise: what does Max-Heapify do on the following input? 20 10 4 9 6 3 2 8 7 21 0 1 This gives us our second PQ operation: Insert(A,x) 1.A[heap-size+1] := x 2.heap-size := heap-size+1 3.MAX-HEAPIFY-UP(A,heap-size) 12

Other PQ operations Max(S): return max element without extracting it Increase-Key(S,i,new-key) Decrease-Key(S,i,new-key) Both of these require knowing position of desired element Can be taken care by augmenting using a dictionary data structure, such as hash table 13

Heap Sort Heaps give an easy O(n log n) sorting algorithm: For i=2 to n Insert(A,A[i]) For i= n downto 3 A[i] := Extract-Max(A) There is a faster way (O(n) time) to build a heap from an unsorted array. 14

Search Can we search for an element in a heap in sublinear (that is, o(n)) time, in the worst case? How would you prove that such a search algorithm does not exist? 15