Methods for solving recurrences

Similar documents
Asymptotic Analysis and Recurrences

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

1 Substitution method

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

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

Data Structures and Algorithms Chapter 3

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

Analysis of Algorithms - Using Asymptotic Bounds -

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

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

CSCI 3110 Assignment 6 Solutions

Divide and Conquer. Arash Rafiey. 27 October, 2016

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

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

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

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

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

Data Structures and Algorithms Chapter 3

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

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

Recurrence Relations

CS483 Design and Analysis of Algorithms

Algorithm efficiency analysis

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

Data Structures and Algorithms

What we have learned What is algorithm Why study algorithm The time and space efficiency of algorithm The analysis framework of time efficiency Asympt

Mergesort and Recurrences (CLRS 2.3, 4.4)

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

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Design and Analysis of Algorithms Recurrence. Prof. Chuhua Xian School of Computer Science and Engineering

Algorithms Chapter 4 Recurrences

Objective. - mathematical induction, recursive definitions - arithmetic manipulations, series, products

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation:

Computational Complexity

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

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

Divide and Conquer Algorithms

Problem 5. Use mathematical induction to show that when n is an exact power of two, the solution of the recurrence

Introduction to Divide and Conquer

CS 2110: INDUCTION DISCUSSION TOPICS

Data Structures and Algorithms CMPSC 465

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

CS483 Design and Analysis of Algorithms

Divide and Conquer. Andreas Klappenecker

Divide and Conquer algorithms

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

CS 161 Summer 2009 Homework #2 Sample Solutions

Chapter 4. Recurrences

CS/COE 1501 cs.pitt.edu/~bill/1501/ Integer Multiplication

COMP Analysis of Algorithms & Data Structures

More Asymptotic Analysis Spring 2018 Discussion 8: March 6, 2018

CS361 Homework #3 Solutions

Computational Complexity - Pseudocode and Recursions

Divide and Conquer Algorithms

4.4 Recurrences and Big-O

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

Review Of Topics. Review: Induction

Design Patterns for Data Structures. Chapter 3. Recursive Algorithms

Chapter 5. Divide and Conquer CLRS 4.3. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

5 + 9(10) + 3(100) + 0(1000) + 2(10000) =

CSC236 Week 4. Larry Zhang

Chapter 4 Divide-and-Conquer

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

CMSC250 Homework 9 Due: Wednesday, December 3, Question: Total Points: Score:

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

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

CPS 616 DIVIDE-AND-CONQUER 6-1

Analysis of Algorithms - Midterm (Solutions)

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

Lecture 3. Big-O notation, more recurrences!!

Asymptotic Algorithm Analysis & Sorting

Divide & Conquer. CS 320, Fall Dr. Geri Georg, Instructor CS320 Div&Conq 1

CS Data Structures and Algorithm Analysis

Chapter 5. Divide and Conquer CLRS 4.3. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

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

Computational Complexity. This lecture. Notes. Lecture 02 - Basic Complexity Analysis. Tom Kelsey & Susmit Sarkar. Notes

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

CS1800: Mathematical Induction. Professor Kevin Gold

data structures and algorithms lecture 2

Practical Session #3 - Recursions

Lecture 3: Big-O and Big-Θ

k 6, then which of the following statements can you assume to be

DM507 - Algoritmer og Datastrukturer Project 1

Lecture 6 September 21, 2016

Divide and Conquer. Andreas Klappenecker. [based on slides by Prof. Welch]

1 Caveats of Parallel Algorithms

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

CS Non-recursive and Recursive Algorithm Analysis

Discrete Mathematics. Kishore Kothapalli

COMP 9024, Class notes, 11s2, Class 1

LECTURE NOTES ON DESIGN AND ANALYSIS OF ALGORITHMS

CMPSCI611: Three Divide-and-Conquer Examples Lecture 2

Recursion. Algorithms and Data Structures. (c) Marcin Sydow. Introduction. Linear 2nd-order Equations. Important 3 Cases. Quicksort Average Complexity

Algorithms Design & Analysis. Analysis of Algorithm

Math 324 Summer 2012 Elementary Number Theory Notes on Mathematical Induction

Divide-and-Conquer. Reading: CLRS Sections 2.3, 4.1, 4.2, 4.3, 28.2, CSE 6331 Algorithms Steve Lai

Math 475, Problem Set #8: Answers

1 Terminology and setup

Transcription:

Methods for solving recurrences Analyzing the complexity of mergesort The merge function Consider the following implementation: 1 int merge ( int v1, int n1, int v, int n ) { 3 int r = malloc ( ( n1+n ) sizeof ( int ) ) ; 4 int i =0, j =0, k=0; 5 6 while ( i <n1 && j<n ) 7 { 8 i f ( v1 [ i ] > v [ j ] ) 9 r [ k++] = v [ j ++]; 10 else 11 r [ k++] = v1 [ i ++]; 1 } 13 14 while ( i <n1 ) 15 r [ k++] = v1 [ i ++]; 16 17 while ( j<n ) 18 r [ k++] = v [ j ++] 19 0 return r ; 1 } We can easily identify the following asymptotic bounds for each line: lines 3 and 5 O(n1 + n) line 13 O(n1) line 16 O(n) it can be said that every other line runs in constant time O(1) 1

Summing these up, we get a bound for the entire function: merge = O(n1 + n) + O(n1) + O(n) = O(n1 + n) Another bound can be found by using our intuition; each element from the two vectors must be read at least once, and thus we have: merge = Ω(n1 + n) When the two bounds are combined, the final result is: merge = Θ(n1 + n) The mergesort function The implementation is give below: 1 int mergesort ( int v, int n ) { 3 i f ( n == 1) 4 return v ; 5 6 v1 = malloc ( n/ sizeof ( int ) ) ; 7 v = malloc ( ( n n/) sizeof ( int ) ) ; 8 9 int i = 0 ; 10 11 while ( i < n /) 1 v1 [ i ] = v [ i ] ; 13 14 while ( i < n ) 15 v [ n/ i ] = v [ i ] ; 16 17 v1 = mergesort ( v, n / ) ; 18 v = mergesort ( v, n n / ) ; 19 0 return merge ( v1, n /, v, n n / ) ; 1 } We can try once more to assign a bound to each line: lines 6 and 7 O(n/) = O(n) lines 11 and 14 Θ(n/) = Θ(n) line 17 T ( n/ ); by this we mean the complexity of the same function, but with a different input size (in this case, n divided by and rounded down) line 18 T ( n/ ) line 0 Θ(n/ + n n/) = Θ(n) other lines O(1)

Before adding them up, we must take into consideration line 3 If n = 1, the whole function runs in O(1) because the return statement is executed Otherwise, we get: T (n) = O(n) + Θ(n) + T ( n/ ) + T ( n/ ) This can further change into: T (n) = T ( n/ ) + T ( n/ ) + Θ(n) For all algorithms that we are going to encounter, T (n) is considered to be = O(1), for sufficiently small values of n Because of this, the distinction made before is no longer necessary, and we rely exclusively on the recurrence relation above to describe the running time of the function Another simplifying assumption is that n/ = n/ = n/ For large values of n (which are usually the ones of interest in complexity analysis), we consider that the addition or subtraction of a single unit does not influence the result Thus, the previous expression becomes: T (n) = T (n/) + T (n/) + Θ(n) = T (n/) + Θ(n) Finally, we replace the Θ(n) above (which denotes a set of functions) with a single function from Θ(n), written as, where c R is a constant There are a number of reasons for this First of all, the running time of that particular part of the recurrence relation must be described by a function, and not by a whole set of functions Secondly, by not doing this, we risk missing the fact that the running time of that part gradually decreases as the recurrence unfolds For example T (n/) = T (n/4) + Θ(n/) = T (n/4) + Θ(n) By replacing Θ(n) with, we get T (n/) = T (n/4) + / The iteration method This method is helpful in finding an informed estimation about the bounds that can be applied to the running time of an algorithm It does not also establish a definite proof about our findings, but it is of great help as a starting point The iteration method consists of (partially) drawing the recursion tree Each node represents the cost of a single sub-problem, and the number of its children is equal to the number of sub-problems that are directly created from that point For the mergesort function, we would get something like the following tree: 3

k k k k How tall is this tree? The height is equal to k + 1, because we have the root level and then k additional levels How large is the value of k? A leaf must represent a problem that cannot be subdivided any longer; thus we can say that n = 1 This leads to n = k, which means that k = log n (we omit writing the k base of the logarithm) Thus, the above tree for mergesort is log n + 1 levels tall How many leaves can be found on the last level? The number of nodes doubles from one level to the next, starting with the root node There are i nodes on level i (assuming the root node is on level 0), so we have log n = n leaves In order to get an estimate for the run time of mergesort we have to sum the contents of each node A useful observation is that on every level i, there are i nodes, each one containing the expression The sum for every level i is thus i i = Since there are 1 + log n levels, the global sum is (1 + log n) = i log n + = Θ(n log n) The substitution method The previous method gave us the intuition that mergesort = Θ(n log n), but we would also like a formal proof, just to be sure This can be obtained with the substitution method, which requires a guess for T (n) (in our case, we got from the previous method that T (n) = Θ(n log n)) With this starting point, we rely on induction to prove the truth of the statement P (n) c 1 n log n T (n) c n log n, for all n > n 0, n N, where n 0, c 1 and c are chosen beforehand, as per the definition of Θ There are two steps in the induction process: we prove that, for any given n N, if P holds for all natural values smaller than n (induction hypothesis), then P also holds for n (the induction step) 4

we prove that P holds for the base case (sometime there are multiple base cases) Let s start with the induction step We presume that P holds for all values smaller than n, and try to prove that P (n) also holds based on this assumption An interesting value smaller than n is n/ If P (n/) holds, it means that n 0 N and c 1, c R + such that n n 0 : c 1 n/ log (n/) T (n/) c n/ log (n/) Now, we want to substitute (hence substitution method ) these inequalities into the recurrence T (n) = T (n/) +, and this basically means that we multiply both ends by and add afterwards By doing this, we get: c 1 n/ log (n/) + T (n/) + c n/ log (n/) + c 1 n log (n/) + T (n) c n log (n/) + c 1 n(log n log ) + T (n) c n(log n log ) + c 1 n log n c 1 n + T (n) c n log n c n + Remember that the goal of this step is to prove that P (n) holds; in other words we have to prove that c 1 n log n T (n) c n log n For this to be true, starting from where we just left, we must have c 1 n 0 and c n 0 This means that c 1 c and c c Since c 1 can be chosen arbitrarily small and c can be chosen arbitrarily large, the induction step has been successfully proven Why do we need to show that P holds for at least one base case? Well, the induction step shows us that we can prove P (n) by relying on that fact that P holds for smaller values However, if we follow this reasoning, at some point we are going to find some m that is the smallest value (or the smallest interesting value) In this case (base case), we cannot rely on the induction step to prove P (m), as all values smaller than m doesn t make any sense What s the base case for our recurrence relation? If we consider 0 to be the base case, the expression log(0) will show up, which doesn t make sense If 1 is the base case, then we re going to have 0 T (n) 0, but this goes against the convention that T (1) = O(1) Thus, our base case will be (n 0 = ) For, the inequalities become: c 1 log T () c log c 1 T (1) + c c c 1 (c + 1) c c 1 c + 1 c As before, these can be easily satisfied with a proper choice of c, c 1 and c It is important to note that we were able to choose the base case at will, because it represents the n 0 from the definition of Θ, which is existentially qualified 5

Another very important thing is that the constants must not change when applying the substitution method Here s an example of what this means and why it matters so much: we are going to prove that T (n) = O(n), where T (n) = T (n/) + n (we omit the base case for brevity) For this problem, P (n) T (n) The induction hypothesis give us T (n/) / During substitution we get: T (n) / + n + n (c + 1)n While c + 1 is a constant, it is not the one we were looking form, and also T (n) (c + 1)n does not imply that T (n) The induction step is complete only if we can prove the exact form of the inequality (in this case T (n) ) The master method The master method is basically a recipe for solving recurrence relations We can use it to obtains answers in very little time, but the drawback is that it cannot be applied in every situation It can be used for recurrences of the form T (n) = at (n/b) + f(n), where a 1 and b > 1 are constants, and f(n) is an asymptotically positive function There are three possible cases when the master theorem provides an answer: 1 If f(n) = O(n log b a ɛ ) for some constant ɛ > 0, then T (n) = Θ(n log b a ) If f(n) = Θ(n log b a ), then T (n) = Θ(n log b a log n) 3 If f(n) = Ω(n log b a+ɛ ) for some constant ɛ > 0, and if af(n/b) cf(n) for some constant c < 1 and all sufficiently large n, then T (n) = Θ(f(n)) 6