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?

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

The Heap Data Structure

Heapsort. Algorithms.

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

Heaps and Priority Queues

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

Lecture 5: Sorting Part A

Lecture 3. Recurrences / Heapsort

Introduction to Algorithms 3 rd edition

Properties of a heap (represented by an array A)

Chapter 6 Heap and Its Application

Heapsort. Heap data structure

A data structure and associated algorithms, NOT GARBAGE COLLECTION

Data Structures and Algorithms Week 4

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

Partha Sarathi Manal

Lecture: Analysis of Algorithms (CS )

Data Structures and Algorithms Chapter 4

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

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

Data Structures and Algorithms. Roberto Sebastiani

Heaps, Heapsort, Priority Queues

Basic Data Structures and Heaps

CS 241 Analysis of Algorithms

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

Topic: Heaps and priority queues

CS 303 Design and Analysis of Algorithms

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

BM267 - Introduction to Data Structures

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

Algorithms and Data Structures

Data structures. Organize your data to support various queries using little time and/or space

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

HEAP. Michael Tsai 2017/4/25

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

Sorting Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Questions from the material presented in this lecture

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

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

COSC Advanced Data Structures and Algorithm Analysis Lab 3: Heapsort

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

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

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

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;

CS420/520 Algorithm Analysis Spring 2010 Lecture 09

CSCE 750, Fall 2002 Notes 2 Page Bubble Sort // sort the array `a[*]' of length `count' // perform only the first `howmany' sort steps // keep t

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

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

CSC263 Week 2. Larry Zhang

Collection of priority-job pairs; priorities are comparable.

Appendix A and B are also worth reading.

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

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

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.

T(n) = expected time of algorithm over all inputs of size n. half the elements in A[1.. j 1] are less than A[ j ], and half the elements are greater.

Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort

Design and Analysis of Algorithms

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.

Chapter 6 Heapsort 1

Deliverables. Quick Sort. Randomized Quick Sort. Median Order statistics. Heap Sort. External Merge Sort

Module 2: Priority Queues

Data Structure and Algorithm, Spring 2017 Final Examination

Binary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...

Priority Queues, Heaps, and Heapsort

9. Heap : Priority Queue

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

Lower Bound on Comparison-based Sorting

COMP Data Structures

Design and Analysis of Algorithms

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

CS 310 Advanced Data Structures and Algorithms

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

data structures and algorithms lecture 4

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Sorting and Searching

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

Module 2: Priority Queues

Partha Sarathi Mandal

Sorting and Searching

Unit 1 Basics of Algorithms and Mathematics. Computer Engineering

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

Module 2: Priority Queues

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

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

CSCI2100B Data Structures Heaps

arxiv: v3 [cs.ds] 18 Apr 2011

Advanced algorithms. binary heap, d-ary heap, binomial heap, amortized analysis, Fibonacci heap. Jiří Vyskočil, Radek Mařík 2013

HOMEWORK 4 CS5381 Analysis of Algorithm

CS165: Priority Queues, Heaps

QuickSort and Others. Data Structures and Algorithms Andrei Bulatov

Binary Heaps. CSE 373 Data Structures Lecture 11

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

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

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

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

EECS 2011M: Fundamentals of Data Structures

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

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

Transcription:

So far we have studied: Comparisons Insertion Sort Merge Sort Worst case Θ(n 2 ) Θ(nlgn) Best case Θ(n) Θ(nlgn) Sorting Revisited So far we talked about two algorithms to sort an array of numbers What is the advantage of merge sort? Answer: O(n lg n) worst-case running time What is the advantage of insertion sort? Sorts in place (only a constant number of elements of the input array are ever stored outside the array) Fast with small input size (so when array nearly sorted, runs fast in practice) Next: Heapsort Combines advantages of both previous algorithms 1

Heaps A heap can be seen as a complete binary tree: 8 7 9 3 2 4 1 What makes a binary tree complete? Is the example above complete? Heaps Tree is completely filled on all levels except possibly the lowest, which is filled from the left up to a point 8 7 9 3 2 4 1 1 1 1 1 1 The book calls them nearly complete binary trees; can think of unfilled slots as null pointers 2

Heaps In practice, heaps are usually implemented as arrays: 1 2 3 4 5 6 7 8 9 10 A = 8 7 9 3 2 4 1 = 2 1 4 5 6 7 8 7 9 3 2 4 1 8 9 10 Array A representing a heap is an object with two attributes: length[a] = the number of elements in the array, and heap-size[a] = the number of elements in the heap stored within array A. Clearly, heap-size[a] length[a] 3 Heaps To represent a complete binary tree as an array: The root node is A[1] Node i is A[i] The parent of node i is A[i/2] (note: integer divide) The left child of node i is A[2i] 1 The right child of node i is A[2i + 1] 1 2 3 4 5 6 7 8 9 10 A = 8 7 9 3 2 4 1 = 2 3 4 5 6 7 8 7 9 3 2 4 1 8 9 10 2 3

Referencing Heap Elements The root of the tree is A[1], and given the index i of a node, the indices of its parent PARENT(i), left child LEFT(i), and right child RIGHT(i) can be computed simply: PARENT(i) { return i/2 } LEFT(i) { return 2*i } RIGHT(i) { return 2*i + 1 } The Heap Property Heaps also satisfy the heap property: A[Parent(i)] A[i] for all nodes i > 1 In other words, the value of a node is at most the value of its parent Largest element in a heap stored at the root Definitions: The height of a node in the tree = the number of edges on the longest downward path to a leaf The height of a tree = the height of its root 4

Heap Height Since a heap of n elements is based on a complete binary tree, its height is Θ(lg n) (in other words a binary tree of height h has at most 2 h leaves) This is nice: turns out that basic heap operations take at most time proportional to the height of the heap Basic Heap Procedures The HEAPIFY procedure, which runs in O(lg n) time, is the key to maintaining the heap property. The BUILD-HEAP procedure, which runs in linear time, produces a heap from an unordered input array. The HEAPSORT procedure, which runs in O(n lg n) time, sorts an array in place 5

Heap Operations: Heapify() Heapify(): maintain the heap property Given: a node i in the heap with children l and r Given: two subtrees rooted at l and r, assumed to be heaps Problem: The subtree rooted at i may violate the heap property Action: let the value of the parent node float down so subtree at i satisfies the heap property Heap Operations: Heapify() HEAPIFY(A, i) 1 l LEFT(i) 2 r RIGHT(i) 3 if l heap-size[a] and A[l] > A[i] 4 then largest l 5 else largest i 6 if r heap-size[a] and A[r] > A[largest] 7 then largest r 8 if largest i 9 then exchange A[i] A[largest] 10 HEAPIFY(A, largest) 6

Heap Operations: Heapify() At each step, the largest of the elements A[i], A[LEFT(i)], and A[RIGHT(i)] is determined, and its index is stored in largest. If A[i] is largest, then the subtree rooted at node i is a heap and the procedure terminates. Otherwise, one of the two children has the largest element, and A[i] is swapped with A[largest], which causes node i and its children to satisfy the heap property. The node indexed by largest, however, now has the original value A[i], and thus the subtree rooted at largest may violate the max-heap property. Consequently, HEAPIFY must be called recursively on that subtree. Heapify() Example 4 10 14 7 9 3 2 8 1 A = 4 10 14 7 9 3 2 8 1 7

Heapify() Example 4 10 14 7 9 3 2 8 1 A = 4 10 14 7 9 3 2 8 1 Heapify() Example 4 10 14 7 9 3 2 8 1 A = 4 10 14 7 9 3 2 8 1 8

Heapify() Example 4 7 9 3 2 8 1 A = 4 7 9 3 2 8 1 Heapify() Example 4 7 9 3 2 8 1 A = 4 7 9 3 2 8 1 9

Heapify() Example 4 7 9 3 2 8 1 A = 4 7 9 3 2 8 1 Heapify() Example 8 7 9 3 2 4 1 A = 8 7 9 3 2 4 1 10

Heapify() Example 8 7 9 3 2 4 1 A = 8 7 9 3 2 4 1 Heapify() Example 8 7 9 3 2 4 1 A = 8 7 9 3 2 4 1 11

Analyzing Heapify() Fixing up relationships between i, l, and r takes Θ(1) time (constant time) If the heap at i has n elements, how many elements can the subtrees at l or r have? Answer: 2n/3 (worst case: last row 1/2 full) So time taken by Heapify() is given by T(n) T(2n/3) + Θ(1) So we have Analyzing Heapify() T(n) T(2n/3) + Θ(1) By case 2 of the Master Theorem, T(n) = O(lg n) Alternatively, we can characterize the running time of HEAPIFY on a node of height h as O(h). 12

Heap Operations: BuildHeap() We can build a heap in a bottom-up manner by running Heapify() on successive subarrays Fact: for array of length n, all elements in range A[ n/2 + 1.. n] are all leaves of the tree and so each is a 1-element heap So: Go backwards through the array from n/2 to 1, calling Heapify() on each node. Order of processing guarantees that the children of node i are heaps when i is processed BUILD-HEAP(A) Produces a heap from an unordered input array 1 heap-size[a] length[a] 2 for i length[a]/2 downto 1 3 do HEAPIFY(A, i) 13

BuildHeap() Example Work through example A = {4, 1, 3, 2,, 9, 10, 14, 8, 7} 4 1 3 2 9 10 14 8 7 14

Build-Heap:Worked Example The operation of BUILD-HEAP, showing the data structure before the call to HEAPIFY in line 3 of BUILD-HEAP. (a)a 10-element input array A and the binary tree it represents. The figure shows that the loop index i refers to node 5 before the call HEAPIFY(A, i). (b) The data structure that results. The loop index i for the next iteration refers to node 4. (c)-(e) Subsequent iterations of the for loop in BUILD-HEAP. Observe that whenever HEAPIFY is called on a node, the two subtrees of that node are both heaps. (f) The heap after BUILD-HEAP finishes. Analyzing BuildHeap() Each call to Heapify() takes O(lg n) time There are O(n) such calls (specifically, n/2 ) Thus the running time is O(n lg n) Is this a correct asymptotic upper bound? Is this an asymptotically tight bound? A tighter bound is O(n) How can we prove this? 15

Analyzing BuildHeap(): Tight To Heapify() a subtree takes O(h) time where h is the height of the subtree h = O(lg m), m = # nodes in subtree The height of most subtrees is small Fact: an n-element heap has at most n/2 h+1 nodes of height h CLR 7.3 uses this fact to prove that BuildHeap() takes O(n) time Heapsort Given BuildHeap(), an in-place sorting algorithm is easily constructed: Maximum element is at A[1] Discard by swapping with element at A[n] Decrement heap_size[a] A[n] now contains correct value Restore heap property at A[1] by calling Heapify() Repeat, always swapping A[1] for A[heap_size(A)]

Heapsort HEAPSORT(A) 1 BUILD-HEAP(A) 2 for i length[a] downto 2 3 do exchange A[1] A[i] 4 heap-size[a] heap-size[a] - 1 5 HEAPIFY(A, 1) Example: Heapsort 17

Example: Heapsort The operation of HEAPSORT: (a) The max-heap data structure just after it has been built by BUILD-HEAP. (b)-(j) The max-heap just after each call of HEAPIFY in line 5. The value of i at that time is shown. Only lightly shaded nodes remain in the heap. (k) The resulting sorted array A. Analyzing Heapsort The call to BuildHeap() takes O(n) time Each of the n - 1 calls to Heapify() takes O(lg n) time Thus the total time taken by HeapSort() = O(n) + (n - 1) O(lg n) = O(n) + O(n lg n) = O(n lg n) 18

Application: Priority Queues The heap data structure itself has enormous utility. One of the most popular applications of a heap: its use as an efficient priority queue. A priority queue is a data structure for maintaining a set S of elements, each with an associated value called a key. One application of max-priority queues is to schedule jobs on a shared computer. The priority queue keeps track of the jobs to be performed and their relative priorities. 19