Recurrences COMP 215

Similar documents
Recursion. Slides by Christopher M. Bourke Instructor: Berthe Y. Choueiry. Fall 2007

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

Algorithms Test 1. Question 1. (10 points) for (i = 1; i <= n; i++) { j = 1; while (j < n) {

Recursion: Introduction and Correctness

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

CSC2100B Data Structures Analysis

Recurrence Relations

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

Quiz 3 Reminder and Midterm Results

Topic 17. Analysis of Algorithms

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

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

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

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

Asymptotic Notation. such that t(n) cf(n) for all n n 0. for some positive real constant c and integer threshold n 0

CS 2110: INDUCTION DISCUSSION TOPICS

Notes for Recitation 14

Lecture 10: Powers of Matrices, Difference Equations

O Notation (Big Oh) We want to give an upper bound on the amount of time it takes to solve a problem.

CS 2210 Discrete Structures Advanced Counting. Fall 2017 Sukumar Ghosh

1 Recursive Algorithms

with the size of the input in the limit, as the size of the misused.

CSE 21 Mathematics for

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Analysis of Algorithms

Solving Recurrences. Lecture 23 CS2110 Fall 2011

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

Logic and Discrete Mathematics. Section 6.7 Recurrence Relations and Their Solution

CS Analysis of Recursive Algorithms and Brute Force

A SUMMARY OF RECURSION SOLVING TECHNIQUES

Advanced Counting Techniques. Chapter 8

CDM. Recurrences and Fibonacci

CDM. Recurrences and Fibonacci. 20-fibonacci 2017/12/15 23:16. Terminology 4. Recurrence Equations 3. Solution and Asymptotics 6.

Algorithms CMSC The Method of Reverse Inequalities: Evaluation of Recurrent Inequalities

Binomial Coefficient Identities/Complements

CS Algorithms and Complexity

Selection and Adversary Arguments. COMP 215 Lecture 19

CS Data Structures and Algorithm Analysis

MCS 256 Discrete Calculus and Probability Exam 5: Final Examination 22 May 2007

Data Structures and Algorithms Chapter 3

Part III, Sequences and Series CS131 Mathematics for Computer Scientists II Note 16 RECURRENCES

Lecture 6: Recurrent Algorithms:

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

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

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

Lecture 2: Divide and conquer and Dynamic programming

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

Sequences of Real Numbers

CSCE 222 Discrete Structures for Computing. Review for Exam 2. Dr. Hyunyoung Lee !!!

Data Structures and Algorithms Chapter 3

Review Of Topics. Review: Induction

Computational Complexity - Pseudocode and Recursions

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

Analysis of Algorithms - Using Asymptotic Bounds -

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

Advanced Counting Techniques

Induction. Induction. Induction. Induction. Induction. Induction 2/22/2018

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK UNIT-I SUB NAME: DESIGN AND ANALYSIS OF ALGORITHMS. PART A (2 Marks)

1 Examples of Weak Induction

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

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

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Chapter 4 Divide-and-Conquer

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

Mathematical Background. Unsigned binary numbers. Powers of 2. Logs and exponents. Mathematical Background. Today, we will review:

Dynamic Programming: Interval Scheduling and Knapsack

Cpt S 223. School of EECS, WSU

University of New Mexico Department of Computer Science. Midterm Examination. CS 361 Data Structures and Algorithms Spring, 2003

Introduction. An Introduction to Algorithms and Data Structures

CS173 Running Time and Big-O. Tandy Warnow

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

3.1 Asymptotic notation

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

CHAPTER 8 Advanced Counting Techniques

Algorithms Chapter 4 Recurrences

Section Summary. Sequences. Recurrence Relations. Summations. Examples: Geometric Progression, Arithmetic Progression. Example: Fibonacci Sequence

Every subset of {1, 2,...,n 1} can be extended to a subset of {1, 2, 3,...,n} by either adding or not adding the element n.

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

Algorithm Design and Analysis

Math 324 Summer 2012 Elementary Number Theory Notes on Mathematical Induction

Why do we need math in a data structures course?

Section 4.1: Sequences and Series

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

Discrete Mathematics: Logic. Discrete Mathematics: Lecture 17. Recurrence

CS2223 Algorithms D Term 2009 Exam 3 Solutions

Introduction to Divide and Conquer

Problem Solving in Math (Math 43900) Fall 2013

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

Lecture 4. Quicksort

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

CS383, Algorithms Spring 2009 HW1 Solutions

LECTURE NOTES ON DESIGN AND ANALYSIS OF ALGORITHMS

Math.3336: Discrete Mathematics. Advanced Counting Techniques

Outline. Part 5. Computa0onal Complexity (3) Compound Interest 2/15/12. Recurrence Rela0ons: An Overview. Recurrence Rela0ons: Formal Defini0on

Math Circle: Recursion and Induction

Mergesort and Recurrences (CLRS 2.3, 4.4)

COE428 Notes Week 4 (Week of Jan 30, 2017)

Introduction to Algorithms

Asymptotic Analysis 1

Transcription:

Recurrences COMP 215

Analysis of Iterative Algorithms //return the location of the item matching x, or 0 if //no such item is found. index SequentialSearch(keytype[] S, in, keytype x) { index location = 1; while (location <= n && S[location]!= x) location++ if (location > n) location = 0; return location; } It is straightforward to figure out the complexity here. Count the number of times the basic operation occurs, accounting for loops.

Analysis of Recursive Algorithms //factorial function int fact(in) { if (n == 0) return 1; else return n * fact(n-1); } The situation here is trickier. How many times does the basic operation (multiplication) occur?

Analysis of Recursive Algorithms The situation here is trickier. How many times does the basic operation (multiplication) occur? Easily described in terms of a recurrence: = 1 + 1 Closed form? //factorial function int fact(in) { if (n == 0) return 1; else return n * fact(n-1); }

Inductive Proof... t 0 = 0 = 1 + 1 Candidate solution: = n

Exercise inewfun(in) { a = 0; if (n == 0) return 1; else a += newfun(n-1); a += newfun(n-1); for (i = 1; i <=n; i++) a = a * n; return a; } Assuming multiplication is the basic operation, develop a recurrence.

Binary Search //return the location of the item matching x, or 0 if //no such item is found. S must be sorted. index BinarySearch(keytype[] S, int low, int high, keytype x) { index mid; } if (low > high) return 0; else { mid = floor( (low + high) / 2 ) if (x == S[mid]) return mid; else if (x < S[mid]) return BinarySearch(low, mid 1); else return BinarySearch(mid + 1, high); }

Another Inductive Proof The recurrence is: For the same of simplicity, assume tha is a power of 2. Candidate solution? =t n/ 2 1 t 1 =1 =/ 2 1 t 1 =1

Handling Non Powers of Two (or b) For binary search we were able to exactly determine the complexity, as long as n was a power of 2: lg n +1. It would be nice to be able to say something about binary search even if n is not a power of 2. T n lg n E.g. for all n. First some definitions...

Definitions A complexity function f(n) is strictly increasing if f(n) always gets larger as n gets larger. That is, if n 1 > n 2, then f(n 1 ) > f(n 2 ). A complexity function f(n) is non decreasing if f(n) never gets smaller as n gets larger. That is, if n 1 > n 2, then f(n 1 ) >= f(n 2 ). Reminder: A complexity function can be any function that maps positive integers to non negative reals.

More Definitions A complexity function f(n) is eventually non decreasing if for all n past some point the function never gets smaller as n gets larger. That is, there exists an N such that if n 1 > n 2 > N then f(n 1 ) >= f(n 2 ). A complexity function f(n) is smooth if f(n) is eventually non decreasing and if f 2n f n. (Note that this is not the same as the calculus definition of smoothness)

Finally, The Theorem let b >= 2 be an integer, let f(n) be a smooth complexity function, and let T(n) be an eventually non decreasing complexity function. If T n f n for n a power of b, then T n f n The same implication holds if is replaced by big O,, or small o.

Binary Search is in lg n We already know that W(n)=lg n +1, if n is a power of 2. We need to show that lg n is smooth and that, pretty easy. =t n/ 2 1 is eventually non decreasing. requires induction.

What if You Don't Have a Guess? Backward substitution... Recursion trees particularly for divide and conquer algorithms. Recursion tree for = 2/2 + n... Cookbook solutions.

General Solution for Divide and Conquer Suppose a complexity function T(n) is eventually nondecreasing and satisfies T n =at n f n for n 1, n a power of b b T 1 =d If f n n k Where b >= 2 and k >= 0 are constant integers and a, c, and d are constants such that a > 0, c > 0 and d >=0. Then n T n { k if a b k } n k lg n if a=b k n log a b if a b k (This is slightly more general than the version in the book. Proof in Cormen et al. Intro to Algorithms 2 nd edition.)

Applying The Theorem Let's take another look at = 2/2 + n. If we replace With T n =at n b f n T n at n b f n or T n a T n b f n Then the same result holds, replacing with big O or respectively.

Homogeneous Linear Recurrences Another cookbook approach. Homogeneous Linear Recurrences have the form: Where each a i is a constant. For example: a 0 a 1 1 a k k =0 7 3 1 =0 6 5 1 8 2 =0 = 1 2 Recall the Fibonacci sequence:, or 1 2 =0

Example Solution Consider this recurrence: Substitute = r n : A little algebra: 5 1 6 2 =0 t 0 =0 t 1 =1 r n 5r n 1 6r n 2 =0 r n 2 r 2 5r 6 =0 Factor: r n 2 r 2 r 3 =0

Example Solution Continued So, we have roots at r = 0, r = 2 and r = 3. This means that = 0, = 2 n, = 3 n are all solutions to the recurrence. It turns out that the general solution can be specified as: =c 1 2 n c 2 3 n Where c 1 and c 2 are arbitrary constants. We fix the constants by plugging in the initial conditions

General Solution Let the homogeneous linear recurrence equation a 0 a 1 1 a k k =0 be given. If its characteristic equation a 0 r k a 1 r k 1 a k r 0 =0 has k distinct solutions r 1, r 2,...,r k, then the only solutions to the recurrence are: =c 1 r 1 n c 2 r 2 n c k r k n The values of the constants are determined by the initial conditions

Example (From Book) 3 1 4 2 =0 t 0 =0 t 1 =1

What if there are Repeated Roots? Theorem B.2 in the book gives the technique for handling repeated roots: Let r be a root of multiplicity m of the characteristic equation for a homogeneous linear recurrence with constant coefficients. Then =r n, =nr n, =n 2 r n,, =n m 1 r n are all solutions to the recurrence. Therefore a term for each is included in the general solution. E.g. if the characteristic equation had the solution: r 1 r 3 3 The solution to the recurrence would be: =c 1 1 n c 2 3 n c 3 n 3 n c 4 n 2 3 n

Non Homogeneous Linear Recurrences a 0 a 1 1 a k k = f n There is no general method that works for any f(n). We will show a cookbook method for f(n) = bn p(n). b is a constant and p(n) is any polynomial in n. Examples 5 1 6 2 =4 n, here b = 4, p(n) =1 6t, b = 2, p(n) = 3n 2 n 5 1 8 2 =2 n 3n 2 n 1 +n+1

General Solution A non homogeneous linear recurrence of the form a 0 a 1 1 a k k =b n p n can be transformed into a homogeneous linear recurrence that has the characteristic equation a 0 r k a 1 r k 1 a k r b d 1 =0 where d is the degree of p(n)

Change of Variables Other Approaches...