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

Similar documents
Recursive Algorithms. Recurrences. Recursive Algorithms Analysis

Recurrences COMP 215

Introduction. How can we say that one algorithm performs better than another? Quantify the resources required to execute:

CSL105: Discrete Mathematical Structures. Ragesh Jaiswal, CSE, IIT Delhi

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

Learning Objectives

CS Data Structures and Algorithm Analysis

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

Sets. Slides by Christopher M. Bourke Instructor: Berthe Y. Choueiry. Fall 2007

CS 310 Advanced Data Structures and Algorithms

Sets. Slides by Christopher M. Bourke Instructor: Berthe Y. Choueiry. Spring 2006

1 Recursive Algorithms

Solving Recurrence Relations 1. Guess and Math Induction Example: Find the solution for a n = 2a n 1 + 1, a 0 = 0 We can try finding each a n : a 0 =

Proof Techniques (Review of Math 271)

Notes. Relations. Introduction. Notes. Relations. Notes. Definition. Example. Slides by Christopher M. Bourke Instructor: Berthe Y.

Sequences of Real Numbers

Math 475, Problem Set #8: Answers

Induction and Recursion

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

Mathematical Fundamentals

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

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

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

Section 5.2 Solving Recurrence Relations

CDM. Recurrences and Fibonacci

Recursion: Introduction and Correctness

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

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

CS1800: Mathematical Induction. Professor Kevin Gold

Sec$on Summary. Sequences. Recurrence Relations. Summations. Ex: Geometric Progression, Arithmetic Progression. Ex: Fibonacci Sequence

Math 324 Summer 2012 Elementary Number Theory Notes on Mathematical Induction

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

Recurrence Relations and Recursion: MATH 180

Advanced Counting Techniques

CS Analysis of Recursive Algorithms and Brute Force

CS1800: Sequences & Sums. Professor Kevin Gold

Notes. Functions. Introduction. Notes. Notes. Definition Function. Definition. Slides by Christopher M. Bourke Instructor: Berthe Y.

MATH 243E Test #3 Solutions

Discrete Calculus and its Applications

Cpt S 223. School of EECS, WSU

Algorithm efficiency analysis

Section 7.2 Solving Linear Recurrence Relations

Chapter Summary. Mathematical Induction Strong Induction Well-Ordering Recursive Definitions Structural Induction Recursive Algorithms

Problem Solving in Math (Math 43900) Fall 2013

Fall 2017 Test II review problems

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

Algorithms Design & Analysis. Analysis of Algorithm

Why do we need math in a data structures course?

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

Recurrence Relations

Section Summary. Sequences. Recurrence Relations. Summations Special Integer Sequences (optional)

Topic 17. Analysis of Algorithms

Consider an infinite row of dominoes, labeled by 1, 2, 3,, where each domino is standing up. What should one do to knock over all dominoes?

Recurrence Relations

Slides by Christopher M. Bourke Instructor: Berthe Y. Choueiry. Spring 2006

Discrete Structures Lecture Sequences and Summations

282 Math Preview. Chris Brown. September 8, Why This? 2. 2 Logarithms Basic Identities Basic Consequences...

Induction and recursion. Chapter 5

MEETING 9 - INDUCTION AND RECURSION

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

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

8. Sequences, Series, and Probability 8.1. SEQUENCES AND SERIES

Lecture 10: Powers of Matrices, Difference Equations

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

Linear Algebra and Robot Modeling

CS383, Algorithms Spring 2009 HW1 Solutions

CSI2101-W08- Recurrence Relations

19. TAYLOR SERIES AND TECHNIQUES

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Catie Baker Spring 2015

Math 2602 Finite and Linear Math Fall 14. Homework 8: Core solutions

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Section 4.1: Sequences and Series

1 Recurrence relations, continued continued

Relations Graphical View

Chapter 11 - Sequences and Series

Discrete Mathematics: Logic. Discrete Mathematics: Lecture 14. Recursive algorithm

Algorithm Analysis, Asymptotic notations CISC4080 CIS, Fordham Univ. Instructor: X. Zhang

Rational Expressions and Radicals

Today s Outline. CS 362, Lecture 4. Annihilator Method. Lookup Table. Annihilators for recurrences with non-homogeneous terms Transformations

Generating Function Notes , Fall 2005, Prof. Peter Shor

Introduction to Series and Sequences Math 121 Calculus II Spring 2015

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

Problems for M 8/31: and put it into echelon form to see whether there are any solutions.

Advanced Counting Techniques. Chapter 8

LINEAR RECURSIVE SEQUENCES. The numbers in the sequence are called its terms. The general form of a sequence is

5. Sequences & Recursion

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

POWER SERIES REVIEW SOLUTIONS

6.042/18.062J Mathematics for Computer Science March 17, 2005 Srini Devadas and Eric Lehman. Recurrences

Notes for Recitation 14

Computational Complexity - Pseudocode and Recursions

University of California Berkeley CS170: Efficient Algorithms and Intractable Problems November 19, 2001 Professor Luca Trevisan. Midterm 2 Solutions

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

CLOSED FORM CONTINUED FRACTION EXPANSIONS OF SPECIAL QUADRATIC IRRATIONALS

MATH 324 Summer 2011 Elementary Number Theory. Notes on Mathematical Induction. Recall the following axiom for the set of integers.

CS 4349 Lecture August 30th, 2017

Homework 3 Solutions Math 309, Fall 2015

Notes. Combinatorics. Combinatorics II. Notes. Notes. Slides by Christopher M. Bourke Instructor: Berthe Y. Choueiry. Spring 2006

Notes. Number Theory: Applications. Notes. Number Theory: Applications. Notes. Hash Functions I

Discrete Calculus and its Applications

Transcription:

Slides by Christopher M. Bourke Instructor: Berthe Y. Choueiry Fall 2007 1 / 47 Computer Science & Engineering 235 to Discrete Mathematics Sections 7.1-7.2 of Rosen

Recursive Algorithms 2 / 47 A recursive algorithm is one in which objects are defined in terms of other objects of the same type. Advantages: Simplicity of code Easy to understand Disadvantages: Memory Speed Possibly redundant work Tail recursion offers a solution to the memory problem, but really, do we need recursion?

Recursive Algorithms Analysis We ve already seen how to analyze the running time of algorithms. However, to analyze recursive algorithms, we require more sophisticated techniques. Specifically, we study how to define & solve recurrence relations. 3 / 47

Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1 Consider the following (recursive) algorithm for computing n!: Algorithm (Factorial) 1 2 3 4 5 6 Input Output : n! if n = 1 then return 1 end : n N else return Factorial(n 1) n end 4 / 47

Motivating Example Factorial - Analysis? How many multiplications M(n) does Factorial perform? 5 / 47

Motivating Example Factorial - Analysis? How many multiplications M(n) does Factorial perform? When n = 1 we don t perform any. 5 / 47

Motivating Example Factorial - Analysis? How many multiplications M(n) does Factorial perform? When n = 1 we don t perform any. wise we perform 1. 5 / 47

Motivating Example Factorial - Analysis? How many multiplications M(n) does Factorial perform? When n = 1 we don t perform any. wise we perform 1. Plus how ever many multiplications we perform in the recursive call, Factorial(n 1). 5 / 47

Motivating Example Factorial - Analysis? How many multiplications M(n) does Factorial perform? When n = 1 we don t perform any. wise we perform 1. Plus how ever many multiplications we perform in the recursive call, Factorial(n 1). This can be expressed as a formula (similar to the definition of n!. M(0) = 0 M(n) = 1 + M(n 1) 5 / 47

Motivating Example Factorial - Analysis? How many multiplications M(n) does Factorial perform? When n = 1 we don t perform any. wise we perform 1. Plus how ever many multiplications we perform in the recursive call, Factorial(n 1). This can be expressed as a formula (similar to the definition of n!. M(0) = 0 M(n) = 1 + M(n 1) This is known as a recurrence relation. 5 / 47

I Definition Definition A recurrence relation for a sequence {a n } is an equation that expresses a n in terms of one or more of the previous terms in the sequence, a 0, a 1,..., a n 1 for all integers n n 0 where n 0 is a nonnegative integer. A sequence is called a solution of a recurrence relation if its terms satisfy the recurrence relation. 6 / 47

II Definition Consider the recurrence relation: a n = 2a n 1 a n 2. It has the following sequences a n as solutions: 1 a n = 3n, 2 a n = n + 1, and 3 a n = 5. Initial conditions + recurrence relation uniquely determine the sequence. 7 / 47

III Definition Example The Fibonacci numbers are defined by the recurrence, F (n) = F (n 1) + F (n 2) F (1) = 1 F (0) = 1 The solution to the Fibonacci recurrence is ( ) n ( f n = 1 1 5 5 1 + 5 2 1 5 2 ) n (your book derives this solution). 8 / 47

IV Definition More generally, recurrences can have the form or T (n) = αt (n β) + f(n), T (n) = αt T (δ) = c ( ) n + f(n), T (δ) = c β Note that it may be necessary to define several T (δ), initial conditions. 9 / 47

V Definition The initial conditions specify the value of the first few necessary terms in the sequence. In the Fibonacci numbers we needed two initial conditions, F (0) = F (1) = 1 since F (n) was defined by the two previous terms in the sequence. Initial conditions are also known as boundary conditions (as opposed to the general conditions). From now on, we will use the subscript notation, so the Fibonacci numbers are f n = f n 1 + f n 2 f 1 = 1 f 0 = 1 10 / 47

VI Definition relations have two parts: recursive terms and non-recursive terms. T (n) = 2T (n 2) }{{} recursive + n 2 10 }{{} non-recrusive Recursive terms come from when an algorithm calls itself. Non-recursive terms correspond to the non-recursive cost of the algorithm work the algorithm performs within a function. We ll see some examples later. First, we need to know how to solve recurrences. 11 / 47

Solving 2nd Order General There are several methods for solving recurrences. Characteristic Equations Forward Substitution Backward Substitution Trees Maple! 12 / 47

2nd Order General 13 / 47 Definition A linear homogeneous recurrence relation of degree k with constant coefficients is a recurrence relation of the form a n = c 1 a n 1 + c 2 a n 2 + + c k a n k with c 1,..., c k R, c k 0. : RHS is a sum of multiples of previous terms of the sequence (linear combination of previous terms). The coefficients are all constants (not functions depending on n). : no terms occur that are not multiples of the a j s. Degree k: a n is expressed in terms of k terms of the sequence.

Examples 2nd Order General Examples The Fibonacci sequence is a linear homogeneous recurrence relation. As are the following. a n = 4a n 1 + 5a n 2 + 7a n 3 a n = 2a n 2 + 4a n 4 + 8a n 8 How many initial conditions do we need to specify for these? So, how do we solve linear homogeneous recurrences? 14 / 47

Examples 2nd Order General Examples The Fibonacci sequence is a linear homogeneous recurrence relation. As are the following. a n = 4a n 1 + 5a n 2 + 7a n 3 a n = 2a n 2 + 4a n 4 + 8a n 8 How many initial conditions do we need to specify for these? As many as the degree, k = 3, 8 respectively. So, how do we solve linear homogeneous recurrences? 14 / 47

Solving I 2nd Order General We want a solution of the form a n = r n where r is some (real) constant. We observe that a n = r n is a solution to a linear homogeneous recurrence if and only if r n = c 1 r n 1 + c 2 r n 2 + + c k r n k We can now divide both sides by r n k, collect terms, and we get a k-degree polynomial. r k c 1 r k 1 c 2 r k 2 c k 1 r c k = 0 15 / 47

Solving II 2nd Order General r k c 1 r k 1 c 2 r k 2 c k 1 r c k = 0 This is called the characteristic equation of the recurrence relation. The roots of this polynomial are called the characteristic roots of the recurrence relation. They can be used to find solutions (if they exist) to the recurrence relation. We will consider several cases. 16 / 47

Second Order 2nd Order General A second order linear homogeneous recurrence is a recurrence of the form a n = c 1 a n 1 + c 2 a n 2 Theorem (Theorem 1, p462) Let c 1, c 2 R and suppose that r 2 c 1 r c 2 = 0 is the characteristic polynomial of a 2nd order linear homogeneous recurrence which has two distinct a roots, r 1, r 2. Then {a n } is a solution if and only if a n = α 1 r n 1 + α 2 r n 2 for n = 0, 1, 2,... where α 1, α 2 are constants dependent upon the initial conditions. a we discuss how to handle this situation later. 17 / 47

Second Order Example 2nd Order General Example Find a solution to a n = 5a n 1 6a n 2 with initial conditions a 0 = 1, a 1 = 4 18 / 47

Second Order Example 2nd Order General Example Find a solution to a n = 5a n 1 6a n 2 with initial conditions a 0 = 1, a 1 = 4 The characteristic polynomial is r 2 5r + 6 18 / 47

Second Order Example 2nd Order General Example Find a solution to a n = 5a n 1 6a n 2 with initial conditions a 0 = 1, a 1 = 4 The characteristic polynomial is r 2 5r + 6 Using the quadratic formula (or common sense), the root can be found; r 2 5r + 6 = (r 2)(r 3) 18 / 47 so r 1 = 2, r 2 = 3

Second Order Example Continued Using the 2nd-order theorem, we have a solution, a n = α 1 (2 n ) + α 2 (3 n ) 2nd Order General 19 / 47

Second Order Example Continued 2nd Order General Using the 2nd-order theorem, we have a solution, a n = α 1 (2 n ) + α 2 (3 n ) Now we can plug in the two initial conditions to get a system of linear equations. a 0 = α 1 (2) 0 + α 2 (3) 0 a 1 = α 1 (2) 1 + α 2 (3) 1 1 = α 1 + α 2 (1) 4 = 2α 1 + 3α 2 (2) 19 / 47

Second Order Example Continued 2nd Order General Solving for α 1 = (1 α 2 ) in (1), we can plug it into the second. 4 = 2α 1 + 3α 2 4 = 2(1 α 2 ) + 3α 2 4 = 2 2α 2 + 3α 2 2 = α 2 20 / 47

Second Order Example Continued 2nd Order General Solving for α 1 = (1 α 2 ) in (1), we can plug it into the second. 4 = 2α 1 + 3α 2 4 = 2(1 α 2 ) + 3α 2 4 = 2 2α 2 + 3α 2 2 = α 2 Substituting back into (1), we get α 1 = 1 20 / 47

Second Order Example Continued 2nd Order General Solving for α 1 = (1 α 2 ) in (1), we can plug it into the second. 4 = 2α 1 + 3α 2 4 = 2(1 α 2 ) + 3α 2 4 = 2 2α 2 + 3α 2 2 = α 2 Substituting back into (1), we get α 1 = 1 Putting it all back together, we have a n = α 1 (2 n ) + α 2 (3 n ) = 1 2 n + 2 3 n 20 / 47

Second Order Another Example 2nd Order General Example Solve the recurrence a n = 2a n 1 + 15a n 2 with initial conditions a 0 = 0, a 1 = 1. 21 / 47

Second Order Another Example 2nd Order General Example Solve the recurrence a n = 2a n 1 + 15a n 2 with initial conditions a 0 = 0, a 1 = 1. If we did it right, we have How can we check ourselves? a n = 1 8 (3)n 1 8 ( 5)n 21 / 47

Single Root Case 2nd Order General Recall that we can only apply the first theorem if the roots are distinct, i.e. r 1 r 2. If the roots are not distinct (r 1 = r 2 ), we say that one characteristic root has multiplicity two. In this case we have to apply a different theorem. Theorem (Theorem 2, p464) Let c 1, c 2 R with c 2 0. Suppose that r 2 c 1 r c 2 = 0 has only one distinct root, r 0. Then {a n } is a solution to a n = c 1 a n 1 + c 2 a n 2 if and only if a n = α 1 r n 0 + α 2 nr n 0 22 / 47 for n = 0, 1, 2,... where α 1, α 2 are constants depending upon the initial conditions.

Single Root Case Example 2nd Order General Example What is the solution to the recurrence relation a n = 8a n 1 16a n 2 with initial conditions a 0 = 1, a 1 = 7? 23 / 47

Single Root Case Example 2nd Order General Example What is the solution to the recurrence relation a n = 8a n 1 16a n 2 with initial conditions a 0 = 1, a 1 = 7? The characteristic polynomial is r 2 8r + 16 23 / 47

Single Root Case Example 2nd Order General Example What is the solution to the recurrence relation a n = 8a n 1 16a n 2 with initial conditions a 0 = 1, a 1 = 7? The characteristic polynomial is r 2 8r + 16 Factoring gives us r 2 8r + 16 = (r 4)(r 4) 23 / 47 so r 0 = 4

Single Root Case Example By Theorem 2, we have that the solution is of the form a n = α 1 4 n + α 2 n4 n 2nd Order General 24 / 47

Single Root Case Example 2nd Order General By Theorem 2, we have that the solution is of the form a n = α 1 4 n + α 2 n4 n Using the initial conditions, we get a system of equations; a 0 = 1 = α 1 a 1 = 7 = 4α 1 + 4α 2 24 / 47

Single Root Case Example 2nd Order General By Theorem 2, we have that the solution is of the form a n = α 1 4 n + α 2 n4 n Using the initial conditions, we get a system of equations; a 0 = 1 = α 1 a 1 = 7 = 4α 1 + 4α 2 Solving the second, we get that α 2 = 3 4 24 / 47

Single Root Case Example 2nd Order General By Theorem 2, we have that the solution is of the form a n = α 1 4 n + α 2 n4 n Using the initial conditions, we get a system of equations; a 0 = 1 = α 1 a 1 = 7 = 4α 1 + 4α 2 Solving the second, we get that α 2 = 3 4 And so the solution is a n = 4 n + 3 4 n4n 24 / 47

Single Root Case Example 2nd Order General By Theorem 2, we have that the solution is of the form a n = α 1 4 n + α 2 n4 n Using the initial conditions, we get a system of equations; a 0 = 1 = α 1 a 1 = 7 = 4α 1 + 4α 2 Solving the second, we get that α 2 = 3 4 And so the solution is We should check ourselves... a n = 4 n + 3 4 n4n 24 / 47

General 2nd Order General There is a straightforward generalization of these cases to higher order linear homogeneous recurrences. Essentially, we simply define higher degree polynomials. The roots of these polynomials lead to a general solution. The general solution contains coefficients that depend only on the initial conditions. In the general case, however, the coefficients form a system of linear equalities. 25 / 47

General I Distinct Roots 2nd Order General Theorem (Theorem 3, p465) Let c 1,..., c k R. Suppose that the characteristic equation r k c 1 r k 1 c k 1 r c k = 0 has k distinct roots, r 1,..., r k. Then a sequence {a n } is a solution of the recurrence relation if and only if a n = c 1 a n 1 + c 2 a n 2 + + c k a n k a n = α 1 r n 1 + α 2 r n 2 + + α k r n k for n = 0, 1, 2,..., where α 1, α 2,..., α k are constants. 26 / 47

General Any Multiplicity 2nd Order General Theorem (Theorem 4, p466) Let c 1,..., c k R. Suppose that the characteristic equation r k c 1 r k 1 c k 1 r c k = 0 has t distinct roots, r 1,..., r t with multiplicities m 1,..., m t. 27 / 47

General Any Multiplicity 2nd Order General Theorem (Continued) Then a sequence {a n } is a solution of the recurrence relation if and only if a n = c 1 a n 1 + c 2 a n 2 + + c k a n k a n = (α 1,0 + α 1,1 n + + α 1,m1 1n m1 1 )r1 n+ (α 2,0 + α 2,1 n + + α 2,m2 1n m2 1 )r2 n+. (α t,0 + α t,1 n + + α t,mt 1n mt 1 )r n t + for n = 0, 1, 2,..., where α i,j are constants for 1 i t and 0 j m i 1. 28 / 47

Nonhomogeneous For recursive algorithms, cost functions are often not homogenous because there is usually a non-recursive cost depending on the input size. Such a recurrence relation is called a linear nonhomogeneous recurrence relation. Such functions are of the form a n = c 1 a n 1 + c 2 a n 2 + + c k a n k + f(n) 29 / 47

Nonhomogeneous Here, f(n) represents a non-recursive cost. If we chop it off, we are left with a n = c 1 a n 1 + c 2 a n 2 + + c k a n k which is the associated homogenous recurrence relation. Every solution of a linear nonhomogeneous recurrence relation is the sum of a particular solution and a solution to the associated linear homogeneous recurrence relation. 30 / 47

Nonhomogeneous Theorem (Theorem 5, p468) If {a (p) n } is a particular solution of the nonhomogeneous linear recurrence relation with constant coefficients a n = c 1 a n 1 + c 2 a n 2 + + c k a n k + f(n) then every solution is of the form {a n (p) + a (h) n }, where {a (h) n } is a solution of the associated homogenous recurrence relation a n = c 1 a n 1 + c 2 a n 2 + + c k a n k 31 / 47

Nonhomogeneous There is no general method for solving such relations. However, we can solve them for special cases. In particular, if f(n) is a polynomial or exponential function (or more precisely, when f(n) is the product of a polynomial and exponential function), then there is a general solution. 32 / 47

Nonhomogeneous Theorem (Theorem 6, p469) Suppose that {a n } satisfies the linear nonhomogeneous recurrence relation a n = c 1 a n 1 + c 2 a n 2 + + c k a n k + f(n) where c 1,..., c k R and f(n) = (b t n t + b t 1 n t 1 + + b 1 n + b 0 ) s n where b 0,..., b n, s R. 33 / 47

Nonhomogeneous Theorem (Continued) When s is not a root of the characteristic equation of the associated linear homogeneous recurrence relation, there is a particular solution of the form (p t n t + p t 1 n t 1 + + p 1 n + p 0 ) s n When s is a root of this characteristic equation and its multiplicity is m, there is a particular solution of the form n m (p t n t + p t 1 n t 1 + + p 1 n + p 0 ) s n 34 / 47

Nonhomogeneous The examples in the text are quite good (see pp467 470) and illustrate how to solve simple nonhomogeneous relations. We may go over more examples if you wish. Also read up on generating functions in section 7.4 (though we may return to this subject). However, there are alternate, more intuitive methods. 35 / 47

Back Substitution Trees Maple When analyzing algorithms, linear homogenous recurrences of order greater than 2 hardly ever arise in practice. We briefly describe two unfolding methods that work for a lot of cases. Backward substitution this works exactly as its name implies: starting from the equation itself, work backwards, substituting values of the function for previous ones. Trees just as powerful but perhaps more intuitive, this method involves mapping out the recurrence tree for an equation. Starting from the equation, you unfold each recursive call to the function and calculate the non-recursive cost at each level of the tree. You then find a general formula for each level and take a summation over all such levels. 36 / 47

Backward Substitution Example Back Substitution Trees Maple Example Give a solution to where T (1) = 5. T (n) = T (n 1) + 2n 37 / 47

Backward Substitution Example Back Substitution Trees Maple Example Give a solution to where T (1) = 5. T (n) = T (n 1) + 2n We begin by unfolding the recursion by a simple substitution of the function values. 37 / 47

Backward Substitution Example Back Substitution Trees Maple Example Give a solution to where T (1) = 5. T (n) = T (n 1) + 2n We begin by unfolding the recursion by a simple substitution of the function values. Observe that T (n 1) = T ((n 1) 1) + 2(n 1) = T (n 2) + 2(n 1) 37 / 47

Backward Substitution Example Back Substitution Trees Maple Example Give a solution to where T (1) = 5. T (n) = T (n 1) + 2n We begin by unfolding the recursion by a simple substitution of the function values. Observe that T (n 1) = T ((n 1) 1) + 2(n 1) = T (n 2) + 2(n 1) Substituting this into the original equation gives us 37 / 47 T (n) = T (n 2) + 2(n 1) + 2n

Backward Substitution Example Continued If we continue to do this, we get the following. Back Substitution Trees Maple 38 / 47

Backward Substitution Example Continued If we continue to do this, we get the following. T (n) = T (n 2) + 2(n 1) + 2n Back Substitution Trees Maple 38 / 47

Backward Substitution Example Continued If we continue to do this, we get the following. T (n) = T (n 2) + 2(n 1) + 2n = T (n 3) + 2(n 2) + 2(n 1) + 2n Back Substitution Trees Maple 38 / 47

Backward Substitution Example Continued If we continue to do this, we get the following. T (n) = T (n 2) + 2(n 1) + 2n = T (n 3) + 2(n 2) + 2(n 1) + 2n = T (n 4) + 2(n 3) + 2(n 2) + 2(n 1) + 2n Back Substitution Trees Maple 38 / 47

Backward Substitution Example Continued If we continue to do this, we get the following. T (n) = T (n 2) + 2(n 1) + 2n = T (n 3) + 2(n 2) + 2(n 1) + 2n = T (n 4) + 2(n 3) + 2(n 2) + 2(n 1) + 2n. Back Substitution Trees Maple 38 / 47

Backward Substitution Example Continued If we continue to do this, we get the following. T (n) = T (n 2) + 2(n 1) + 2n = T (n 3) + 2(n 2) + 2(n 1) + 2n = T (n 4) + 2(n 3) + 2(n 2) + 2(n 1) + 2n. = T (n i) + i 1 j=0 2(n j) Back Substitution Trees Maple 38 / 47

Backward Substitution Example Continued Back Substitution Trees Maple If we continue to do this, we get the following. T (n) = T (n 2) + 2(n 1) + 2n = T (n 3) + 2(n 2) + 2(n 1) + 2n = T (n 4) + 2(n 3) + 2(n 2) + 2(n 1) + 2n. = T (n i) + i 1 j=0 2(n j) I.e. this is the function s value at the i-th iteration. Solving the sum, we get (i 1)(i 1 + 1) T (n) = T (n i) + 2n(i 1) 2 + 2n 2 38 / 47

Backward Substitution Example Continued We want to get rid of the recursive term. To do this, we need to know at what iteration we reach our base case; i.e. for what value of i can we use the initial condition, T (1) = 5? Back Substitution Trees Maple 39 / 47

Backward Substitution Example Continued We want to get rid of the recursive term. To do this, we need to know at what iteration we reach our base case; i.e. for what value of i can we use the initial condition, T (1) = 5? We can easily see that when i = n 1, we get the base case. Back Substitution Trees Maple 39 / 47

Backward Substitution Example Continued We want to get rid of the recursive term. To do this, we need to know at what iteration we reach our base case; i.e. for what value of i can we use the initial condition, T (1) = 5? We can easily see that when i = n 1, we get the base case. Substituting this into the equation above, we get Back Substitution Trees Maple 39 / 47

Backward Substitution Example Continued Back Substitution Trees Maple We want to get rid of the recursive term. To do this, we need to know at what iteration we reach our base case; i.e. for what value of i can we use the initial condition, T (1) = 5? We can easily see that when i = n 1, we get the base case. Substituting this into the equation above, we get T (n) = T (n i) + 2n(i 1) i 2 + i + 2n 39 / 47

Backward Substitution Example Continued Back Substitution Trees Maple We want to get rid of the recursive term. To do this, we need to know at what iteration we reach our base case; i.e. for what value of i can we use the initial condition, T (1) = 5? We can easily see that when i = n 1, we get the base case. Substituting this into the equation above, we get T (n) = T (n i) + 2n(i 1) i 2 + i + 2n = T (1) + 2n(n 1 1) (n 1) 2 + (n 1) + 2n 39 / 47

Backward Substitution Example Continued Back Substitution Trees Maple We want to get rid of the recursive term. To do this, we need to know at what iteration we reach our base case; i.e. for what value of i can we use the initial condition, T (1) = 5? We can easily see that when i = n 1, we get the base case. Substituting this into the equation above, we get T (n) = T (n i) + 2n(i 1) i 2 + i + 2n = T (1) + 2n(n 1 1) (n 1) 2 + (n 1) + 2n = 5 + 2n(n 2) (n 2 2n + 1) + (n 1) + 2n 39 / 47

Backward Substitution Example Continued Back Substitution Trees Maple We want to get rid of the recursive term. To do this, we need to know at what iteration we reach our base case; i.e. for what value of i can we use the initial condition, T (1) = 5? We can easily see that when i = n 1, we get the base case. Substituting this into the equation above, we get T (n) = T (n i) + 2n(i 1) i 2 + i + 2n = T (1) + 2n(n 1 1) (n 1) 2 + (n 1) + 2n = 5 + 2n(n 2) (n 2 2n + 1) + (n 1) + 2n = n 2 + n + 3 39 / 47

Trees Back Substitution Trees Maple When using recurrence trees, we graphically represent the recursion. Each node in the tree is an instance of the function. As we progress downward, the size of the input decreases. The contribution of each level to the function is equivalent to the number of nodes at that level times the non-recursive cost on the size of the input at that level. The tree ends at the depth at which we reach the base case. As an example, we consider a recursive function of the form ( ) n T (n) = αt + f(n), T (δ) = c β 40 / 47

Trees Iteration 0 T (n) Cost f(n) 1 T (n/β) α T (n/β) α f nβ Back Substitution Trees Maple 2. i T (n/β 2 ) α T (n/β 2 ) T (n/β 2 ) α T (n/β 2 ) «α 2 f n β 2. «α i f n β i.. log β n α log β n T (δ) 41 / 47

Trees Example Back Substitution Trees Maple The total value of the function is the summation over all levels of the tree: log β n ( ) n T (n) = α i f β i i=0 We consider the following concrete example. Example T (n) = 2T ( n 2 ) + n, T (1) = 4 42 / 47

Trees Example Continued Iteration 0 T (n) Cost n 1 T (n/2) T (n/2) n 2 + n 2 2 T (n/4) T (n/4) T (n/4) T (n/4) 4 n 4 «Back Substitution Trees Maple 3... i T (n/8) T (n/8) T (n/8) T (n/8) T (n/8) T (n/8) T (n/8) T (n/8) 8 n 8 «... 2 i 0 1 @ n 2 i A.... 43 / 47 log 2 n 2 log 2 n T (1)

Trees Example Continued Back Substitution Trees Maple The value of the function then, is the summation of the value of all levels. We treat the last level as a special case since its non-recursive cost is different. (log 2 n) 1 T (n) = 4n + 2 i n = n(log n) + 4n 2i i=0 44 / 47

Smoothness Rule I Back Substitution Trees Maple In the previous example we make the following assumption: that n was a power of two; n = 2 k. This was necessary to get a nice depth of log n and a full tree. We can restrict consideration to certain powers because of the smoothness rule, which is not studied in this course. For more information about the smoothness rule, please consult pages 481 483 in the textbook The Design & Analysis of Algorthims by Anany Levitin. 45 / 47

How To Cheat With Maple I Back Substitution Trees Maple Maple and other math tools are great resources. However, they are not substitutes for knowing how to solve recurrences yourself. As such, you should only use Maple to check your answers. relations can be solved using the rsolve command and giving Maple the proper parameters. The arguments are essentially a comma-delimited list of equations: general and boundary conditions, followed by the name and variable of the function. 46 / 47

How To Cheat With Maple II Back Substitution Trees Maple > rsolve({t(n) = T(n-1) + 2*n, T(1) = 5}, T(n)); ( ) 1 1 + 2(n + 1) 2 n + 1 2n You can clean up Maple s answer a bit by encapsulating it in the simplify command: > simplify(rsolve({t(n) = T(n-1) + 2*n, T(1) = 5}, T(n))); 3 + n 2 + n 47 / 47