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

Similar documents
BM267 - Introduction to Data Structures

Heapsort. Why study Heapsort?

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

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

Algorithms Lab 3. (a) What is the minimum number of elements in the heap, as a function of h?

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

Transform & Conquer. Presorting

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

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?

CS165: Priority Queues, Heaps

Heapsort. Heap data structure

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

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

Lecture 5: Sorting Part A

Properties of a heap (represented by an array A)

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

CS350: Data Structures Heaps and Priority Queues

CSci 231 Final Review

Collection of priority-job pairs; priorities are comparable.

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

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

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

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

The Heap Data Structure

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

Heap sort. Carlos Moreno uwaterloo.ca EIT

Priority Queues Heaps Heapsort

Binary Heaps in Dynamic Arrays

Introduction to Algorithms 3 rd edition

Algorithms and Data Structures

Partha Sarathi Manal

Sorting and Searching

Lecture 3. Recurrences / Heapsort

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

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;

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

Sorting and Searching

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

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

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

Heaps and Priority Queues

Heaps, Heapsort, Priority Queues

Priority Queues Heaps Heapsort

Heaps and Priority Queues

Chapter 6 Heapsort 1

Priority Queues. T. M. Murali. January 26, T. M. Murali January 26, 2016 Priority Queues

1 Interlude: Is keeping the data sorted worth it? 2 Tree Heap and Priority queue

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

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

Data Structures and Algorithms

CSE 373 Data Structures and Algorithms. Lecture 15: Priority Queues (Heaps) III

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

Figure 1: A complete binary tree.

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

Basic Data Structures and Heaps

Module 2: Priority Queues

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

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

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

9. Heap : Priority Queue

Lecture: Analysis of Algorithms (CS )

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

Module 2: Priority Queues

HEAPS & PRIORITY QUEUES

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

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

Figure 1: A complete binary tree.

Algorithms and Data Structures

Heaps Goodrich, Tamassia. Heaps 1

Design and Analysis of Algorithms

Heapsort. Algorithms.

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

Topic: Heaps and priority queues

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

Data Structures and Algorithms Chapter 4

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

Chapter 6 Heap and Its Application

Lecture 17 Notes Priority Queues

Midterm solutions. n f 3 (n) = 3

Heap sort. Carlos Moreno uwaterloo.ca EIT

Heaps. 2/13/2006 Heaps 1

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Data Structures and Algorithms Week 4

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

CSC263 Week 2. Larry Zhang

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

Tirgul 4. Order statistics. Minimum & Maximum. Order Statistics. Heaps. minimum/maximum Selection. Overview Heapify Build-Heap

Programming II (CS300)

Priority Queues, Heaps, and Heapsort

Binary heaps (chapters ) Leftist heaps

Priority Queues. Chapter 9

Trees & Tree-Based Data Structures. Part 4: Heaps. Definition. Example. Properties. Example Min-Heap. Definition

Priority Queues and Binary Heaps

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Week 9 Student Responsibilities. Mat Example: Minimal Spanning Tree. 3.3 Spanning Trees. Prim s Minimal Spanning Tree.

Data Structures Lesson 9

CS2 Algorithms and Data Structures Note 6

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

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

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

Transcription:

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci000, Bowdoin College So far we have discussed tools necessary for analysis of algorithms (growth, summations and recurrences) and we have seen a couple of sorting algorithms as case-studies. Today we discuss a data structure called priority queue, and its implementation with a heap. The heap will lead us to a different algorithm for sorting, called heapsort. 1 Priority Queue A priority queue maintains a set of elements which have priorities. For example: the elements can be numbers, and in this case the priority is the value; the elements can be people records containing first name, last name and age, where the priority of a person is the age. Generally speaking imagine an element as containing possibly several fields, and one of them is the designated priority. The goal is to store the set of elements in order to efficiently insert new elements, and find the elements of best priority. A priority queue supports the following operations on a set S of n elements: Insert a new element e in S Peek at the best element in S Delete the best element in S The best element is the one having the best priority. Depending on the specific application and what priorities represent, sometimes smaller priorities are better than larger priorities (for e.g. when priority represents the error), other times larger priorities are better than smaller priorities (for e.g. when priority represents seniority). We can define max-priority queues and min-priority queues. A max-priority queue supports the following basic operations: Insert: Insert a new element e in S Find-Max: Return the element with max priority in S Delete-Max: Delete the elements with max priority in S Symmetrically, a min-priority queue supports the following basic operations: Insert: Insert a new element e in S Find-Min: Return the element with min priority in S 1

Delete-Min: Delete the elements with min priority in S Priority queues have many applications, e.g. in discrete event simulation, graph algorithms, process scheduling We can obviously sort using a priority queue: Insert all elements using Insert Delete all elements in order using Delete-Min Sometimes we may require priority queues to support the following additional operations: Change the priority of an element in S Delete an element from S How to implement a priority queue? We ll focus on min-priority queues. Max-priority queues are symmetrical. Implementing a priority queue with an array or list The first implementation that comes to mind is an ordered array: 1 5 6 7 8 9 11 1 15 17 Find-Min can be performed in O(1) time Delete-Min and Insert both take Θ(n) time in the worst case since they would need to expand/compress the array after inserting or deleting element. If the array is unordered, then inserts are faster, because the new element can be added at the end (however in the worst case it s still Θ(n) because it needs to re-allocate the array and copy all elements over). Find-Min and Delete-Min would take Θ(n) time worst-case because they need to traverse the unordered array and search for the minimum. To avoid the limitations of arrays we could use an ordered double linked sorted list (to avoid the O(n) expansion/compression cost) FIND-MIN and DELETE-MIN are now constant time; but Insert can still take O(n) time because it first needs to locate where in the list to insert the new element. A Priority Queue with a Heap The standard implementation of a priority queue is a simple and elegant structure called heap. Heap definition: An array that is viewed as representing a perfectly balanced binary tree; the lowest level can be incomplete, but filled from left-to-right. Heap property: For all nodes v we have priority(v) priority(parent(v))

Note: this is a min-heap; a symmetrical definition gives the max-heap. Min-heap example: 5 9 19 11 15 1 The beauty of heaps is that although they are trees, they can be implemented as arrays. The elements in the heap are stored level-by-level, left-to-right in the array. Example: 5 9 19 11 15 1 the left and right children of node in entry i are in entry i and i + 1, respectively the parent of node in entry i is in entry i Properties of heaps: Height Θ(log n) For a min-heap: Minimum of S is stored in root (for a max-heap, the maximum element is stored in the root). Insert in a heap: Insert element in new leaf in leftmost possible position on lowest level Repeatedly swap element with element in parent node until heap order is reestablished (this is refered to as up-heapify). Example: Insertion of 5 9 19 11 9 5 11 15 1 15 1 19 Find-Min in a heap:

Return root element Delete-Min in a heap: Delete element in root Move element from rightmost leaf on lowest level to the root (and delete leaf) Repeatedly swap element with the smaller of the children elements until heap order is reestablished (this is refered to as down-heapify or sometimes just Heapify). Example: 19 9 5 11 9 5 11 19 15 1 15 1 Heapify Used by Delete-min Generally speaking Heapify can be called on a node i: Heapify(i) means it is called on node i in the heap. When calling Heapify(i) the left and right children of node i must be heaps. After Heapify (i) is complete, the tree rooted at node i is a heap. Running time: All operations traverse at most one root-leaf path O(log n) time. Other operations: Change and delete Changing the priority of a given node or deleting a given node can be handled similarly in O(log n) time. However, it s important to realize that we can delete or update nodes in a heap if we are given their index in the array. For e.g. we cannot say delete the node with priority 7 because we cannot search (efficiently) in a heap! But we can say delete the node at index 5..1 Building a heap in O(n) time Say you got n elements in an array A, and you want to build a heap out of them. The straightforward way is to insert them one at a time. BUILDHEAP-straightforward (A) For i = 1 to n: insert (A[i]) This works but takes Θ(n lg n) worst case. It is actually possible to build a heap faster, in linear time. This is a neat and simple algorithm. The idea is to call Heapify() on all nodes, going bottom-up in the tree.

BUILDHEAP-smart (A) For i = n/ down to 1: Heapify(i) Recall how Heapify works: Heapify(i) means it s called on node i in the heap. When calling Heapify(i) the left and right children of node i must be heaps, and after Heapify (i) is complete, the tree rooted at node i is a heap. Correctness: Why does this work? Note that we call Heapify bottom-up, which means that when doing level i, all trees rooted at level i 1 have been already made into heaps, so the invariants of Heapify are fullfilled (Note: Formally this would require induction on height of tree). Analysis: The leaves are at height 0, the level above is at height 1, and so on, the root is at height log n Cost of Heapify(i) on a node i at height h is O(h) In a heap of n elements there are n h elements at height h Total cost is log n i=1 h n h = Θ(n) log n i=1 This is not a sum that we can immediately compute, but it can be shown that log n i=1 O(1) = the total buildheap cost is Θ(n) Heapsort Sorting using a heap takes Θ(n log n) time. O(n) time to build a heap. n O(log n) time to call Delete-Min to get the next element in order h h h h = This is not in place. An in-place sorting algorithm with a heap is possible, and it is reffered to as heapsort. Build a max-heap Repeatedly, delete the largest element, and put it at the end of the array. 5