Mergesort and Recurrences (CLRS 2.3, 4.4)

Similar documents
COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

Introduction to Divide and Conquer

Inf 2B: Sorting, MergeSort and Divide-and-Conquer

Analysis of Algorithms - Using Asymptotic Bounds -

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

data structures and algorithms lecture 2

1 Divide and Conquer (September 3)

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

Divide and Conquer. Arash Rafiey. 27 October, 2016

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

A design paradigm. Divide and conquer: (When) does decomposing a problem into smaller parts help? 09/09/ EECS 3101

CS483 Design and Analysis of Algorithms

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

Lecture 22: Multithreaded Algorithms CSCI Algorithms I. Andrew Rosenberg

CS 4104 Data and Algorithm Analysis. Recurrence Relations. Modeling Recursive Function Cost. Solving Recurrences. Clifford A. Shaffer.

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

Lecture 4. Quicksort

Outline. 1 Introduction. Merging and MergeSort. 3 Analysis. 4 Reference

Week 5: Quicksort, Lower bound, Greedy

Algorithms. Quicksort. Slide credit: David Luebke (Virginia)

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Review Of Topics. Review: Induction

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

Asymptotic Analysis and Recurrences

Methods for solving recurrences

DM507 - Algoritmer og Datastrukturer Project 1

Notes for Recitation 14

Divide and Conquer Strategy

Divide-and-conquer: Order Statistics. Curs: Fall 2017

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

Recurrence Relations

COMP 9024, Class notes, 11s2, Class 1

Lecture 5: Loop Invariants and Insertion-sort

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

Divide and Conquer. Andreas Klappenecker

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

COMP 382: Reasoning about algorithms

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

CS 2110: INDUCTION DISCUSSION TOPICS

CS 5321: Advanced Algorithms - Recurrence. Acknowledgement. Outline. Ali Ebnenasir Department of Computer Science Michigan Technological University

CS2223 Algorithms D Term 2009 Exam 3 Solutions

CS 5321: Advanced Algorithms Analysis Using Recurrence. Acknowledgement. Outline

Divide and Conquer Algorithms

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Dynamic Programming II Date: 10/12/17

Chapter 4. Recurrences

Space Complexity of Algorithms

Fundamental Algorithms

Fundamental Algorithms

Quicksort (CLRS 7) We previously saw how the divide-and-conquer technique can be used to design sorting algorithm Merge-sort

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17

CS483 Design and Analysis of Algorithms

Solving Recurrences. 1. Express the running time (or use of some other resource) as a recurrence.

Solving recurrences. Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive algorithms.

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

Data Structures and Algorithms CSE 465

Algorithms And Programming I. Lecture 5 Quicksort

Divide and Conquer Algorithms

Partition and Select

Analysis of Algorithms

Algorithm Analysis Recurrence Relation. Chung-Ang University, Jaesung Lee

Algorithms. Jordi Planes. Escola Politècnica Superior Universitat de Lleida

Outline. 1 Merging. 2 Merge Sort. 3 Complexity of Sorting. 4 Merge Sort and Other Sorts 2 / 10

Divide-and-conquer. Curs 2015

Extended Algorithms Courses COMP3821/9801

Divide-Conquer-Glue Algorithms

CS 161 Summer 2009 Homework #2 Sample Solutions

CS 4407 Algorithms Lecture 3: Iterative and Divide and Conquer Algorithms

Midterm Exam. CS 3110: Design and Analysis of Algorithms. June 20, Group 1 Group 2 Group 3

Lecture 6: Recurrent Algorithms:

Linear Time Selection

COMP Analysis of Algorithms & Data Structures

5. DIVIDE AND CONQUER I

Divide and Conquer CPE 349. Theresa Migler-VonDollen

Solving Recurrences. 1. Express the running time (or use of some other resource) as a recurrence.

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

ECE250: Algorithms and Data Structures Analyzing and Designing Algorithms

Lecture 2: MergeSort. CS 341: Algorithms. Thu, Jan 10 th 2019

CSCI 3110 Assignment 6 Solutions

CMPS 2200 Fall Divide-and-Conquer. Carola Wenk. Slides courtesy of Charles Leiserson with changes and additions by Carola Wenk

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 3. Θ Notation. Comparing Algorithms

COMP 250 Fall Midterm examination

6.046 Recitation 2: Recurrences Bill Thies, Fall 2004 Outline

CSE 613: Parallel Programming. Lecture 8 ( Analyzing Divide-and-Conquer Algorithms )

Algorithm Design and Analysis

Data Structures and Algorithms Chapter 3

Algorithms Design & Analysis. Analysis of Algorithm

COMP 355 Advanced Algorithms

Chapter 4 Divide-and-Conquer

Dynamic Programming (CLRS )

Divide-Conquer-Glue. Divide-Conquer-Glue Algorithm Strategy. Skyline Problem as an Example of Divide-Conquer-Glue

Appendix: Solving Recurrences

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

5. DIVIDE AND CONQUER I

Recommended readings: Description of Quicksort in my notes, Ch7 of your CLRS text.

COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background

Divide-and-Conquer Algorithms Part Two

Transcription:

Mergesort and Recurrences (CLRS 2.3, 4.4) We saw a couple of O(n 2 ) algorithms for sorting. Today we ll see a different approach that runs in O(n lg n) and uses one of the most powerful techniques for algorithm design, divide-and-conquer. Outline: 1. Discuss a new sorting algorithm, mergesort. 2. Generalize it to the so-called divide-and-conquer technique. 3. Introduce reccurences as a means to express the running time of recursive algorithms. 4. Show how to solve recurrences by repeated iteration 1 MergeSort The idea of mergesort is the following: Mergesort: Divide: Divide an array of n elements into two arrays of n/2 elements each. Conquer: Sort the two arrays recursively. Combine: Merge the two sorted arrays. To implement this, we ll write a recursive procedure MergeSort(A, p, r) which sorts the subarray A[p..r]. Initially we ll call MergeSort(A, 0, n 1), that is, p = 0 and r = n 1. First we ll abstract away from how merge works. Let s assume we have (somebody gives us) a procedure Merge(A, p, q, r) which takes as arguments an array A and three indices p, q, r such that A[p..q] and A[q + 1...r] are both sorted, and it merges A[p..q] and A[q + 1...r] such that A[p..r] is sorted. We can use Merge(A, p, q, r) as a black box to sort A[p...r] as follows: Merge Sort(A,p,r) If p < r then q = (p + r)/2 MergeSort(A,p,q) MergeSort(A,q+1,r) Merge(A,p,q,r) //else return 1

Now back to merging: How does Merge(A, p, q, r) work? Imagine merging two sorted piles of cards. The basic idea is to choose the smallest of the two top cards and put it into the output pile. Running time: Θ(r p) Implementation is a bit messier. 2 Mergesort Example 5 2 4 6 1 3 2 6 1 2 2 3 4 5 6 6 2 4 5 6 1 2 3 6 2 5 4 6 1 3 2 6 2

3 Mergesort Correctness 1. First, we ll argue that merge is correct, whatever p q r are. Why is merge correct? As you look at the next item to put into the merged output, what has to be true in order for the output to be sorted? Why is this true? 2. Second, we ll prove that Mergesort() is correct when sorting only 1 or 2 elements (the basecase). 3. Third, we ll prove that Mergesort(A, p, r) is correct (for any p, r such that p r). Proof idea: Denote the size of the array to be sorted by n. Note that the initial call to Mergesort() the array of size n will call Mergesort() on two arrays of half size, that is, n/2. Assuming that these smaller mergesorts are correct, and that merge is correct, and that the base-case is correct, it follows that Mergesort() is correct. Formally, this is called proof by induction on n. Proof: Basecase: Mergesort() is correct when sorting 1 or 2 elements (argue why that s true). Induction hypothesis: Assume that mergesorting any array of size n/2 is correct. We ll prove that this implies that mergesorting any array of size n is correct. Proof: mergesorting an array of size n results in two calls to mergesorting arrays of size n/2, which, by the induction hypothesis, are correct. Then the results of teh two recursive sorts are merged, and merge, by step 1, is correct. Therefore mergesorting the array of size n is correct. 4 Mergesort Analysis To simplify things, let us assume that n is a power of 2, i.e n = 2 k for some k. Running time of a recursive algorithm can be analyzed using a recurrence relation. Each divide step yields two sub-problems of size n/2. Let T (n) denote the worst-case running time of mergesort on an array of n elements. We have: Simplified, T (n) = 2T (n/2) + Θ(n) T (n) = c 1 + T (n/2) + T (n/2) + c 2 n = 2T (n/2) + (c 1 + c 2 n) We can see that the recurrence has solution Θ(n log 2 n) by looking at the recursion tree: the total number of levels in the recursion tree is log 2 n + 1 and each level costs linear time (more below). 3

Note: If n 2 k the recurrence gets more complicated. { Θ(1) If n = 1 T (n) = T ( n 2 ) + T ( n 2 ) + Θ(n) If n > 1 But we are interested in the order of growth, not in the exact answer. So we first solve the simple version (equivalent to assuming that n = 2 k for some constant k, and leaving out base case and constant in Θ). Once we know the solution for the simple version, one needs to solve the original recursion by induction. This step is necessary for a complete proof, but it is rather mechanical, so it is usually skipped. So even if we are sloppy with ceilings and floors, the solution is the same. assume n = 2 k or whatever to avoid complicated cases. 5 Divide-and-conquer We usually The general idea of mergesort is an instance of a general technique called divide-and-conquer which can be applied to any problem P. For e.g. P could be the problem of sorting an array, or finding the smallest element in an array. Divide-and-conquer goes like this: Divide-and-Conquer To Solve P: 1. Divide P into two smaller problems P 1, P 2. 2. Conquer by solving the (smaller) subproblems recursively. 3. Combine solutions to P 1, P 2 into solution for P. The simplest way is to divide into two subproblems, as above, but this can be extended to divide into k subproblems. Analysis of divide-and-conquer algorithms and in general of recursive algorithms leads to recurrences. 6 Solving recurrences The steps for solving a recurrence relation are the following: 1. Draw the recursion tree to get a feel for how the recursion goes. Sometimes, for easy recurrences, the recursion tree is sufficient to see the bound. This step can be skipped. 2. Iterate and solve the summations to get the final bound. 3. Use induction to prove this bound formally (substitution method). 4

In this incarnation of the class we will skip the induction step - generally speaking, once you know induction, this step is pretty mechanical. In this class, solving a recurrence will mean finding a theta-bound for T (n) by iteration. 7 Solving Recurrences by iteration Example: Solve T (n) = 8T (n/2) + n 2 (with T (1) = 1) T (n) = n 2 + 8T (n/2) = n 2 + 8(8T ( n 2 2 ) + (n 2 )2 ) = n 2 + 8 2 T ( n 2 2 ) + 8(n2 4 )) = n 2 + 2n 2 + 8 2 T ( n 2 2 ) = n 2 + 2n 2 + 8 2 (8T ( n 2 3 ) + ( n 2 2 )2 ) = n 2 + 2n 2 + 8 3 T ( n 2 3 ) + 82 ( n2 4 2 )) = n 2 + 2n 2 + 2 2 n 2 + 8 3 T ( n 2 3 ) =... = n 2 + 2n 2 + 2 2 n 2 + 2 3 n 2 + 2 4 n 2 +... Recursion depth: How long (how many iterations) it takes until the subproblem has constant size? i times where n = 1 i = log n 2 i What is the last term? 8 i T (1) = 8 log n T (n) = n 2 + 2n 2 + 2 2 n 2 + 2 3 n 2 + 2 4 n 2 +... + 2 log n 1 n 2 + 8 log n = log n 1 log n 1 = n 2 2 k n 2 + 8 log n 2 k + (2 3 ) log n Now log n 1 2 k is a geometric sum so we have log n 1 2 k = Θ(2 log n 1 ) = Θ(n) (2 3 ) log n = (2 log n ) 3 = n 3 T (n) = n 2 Θ(n) + n 3 = Θ(n 3 ) 5

8 Other recurrences Some important/typical bounds on recurrences not covered by master method: Logarithmic: Θ(log n) Recurrence: T (n) = 1 + T (n/2) Typical example: Recurse on half the input (and throw half away) Variations: T (n) = 1 + T (99n/100) Linear: Θ(N) Recurrence: T (n) = 1 + T (n 1) Typical example: Single loop Variations: T (n) = 1 + 2T (n/2), T (n) = n + T (n/2), T (n) = T (n/5) + T (7n/10 + 6) + n Quadratic: Θ(n 2 ) Recurrence: T (n) = n + T (n 1) Typical example: Nested loops Exponential: Θ(2 n ) Recurrence: T (n) = 2T (n 1) 6