Recursion Review of recursive functions Recursive definitions Contrast

  • Slides: 30
Download presentation
Recursion • Review of recursive functions • Recursive definitions – Contrast with explicit formula.

Recursion • Review of recursive functions • Recursive definitions – Contrast with explicit formula. – In the case of defining a set of strings, compare with regular expressions • Later: Solving recurrence relations – The purpose is to convert a recursive definition of a sequence of numbers into an explicit definition. – Simple 1 st and 2 nd order – Verifying explicit formula matches recursive rule – Inhomogeneous recurrence relations

Geometric series • Before we dive in, let’s review geometric series. • Let r

Geometric series • Before we dive in, let’s review geometric series. • Let r = common ratio; n = # terms; f = first term • Finite number of terms Sum = f * (rn – 1) / (r – 1) Example: 5 + 10 + 20 + 40 + 80 + 160. sum = 5 * (26 – 1) / (2 – 1) = 5 * 63 = 315. • Infinite number of terms Sum = f / (1 – r) Only works if – 1 < r < 1. Otherwise sum diverges.

Recursive functions • A function that calls itself • To make it work, there

Recursive functions • A function that calls itself • To make it work, there should also be a base case to exit the recursion. • Factorial – fact(0) = 1 – fact(n) = n * fact(n – 1) for n 1 • Fibonacci – F(1) = 1, and F(2) = 1 – F(n) = F(n – 1) + F(n – 2) for n 3 – Question: Can you write a formula for F(n+4) in terms of just F(n) and F(n+1)?

More examples • Chebyshev (Чебышёв) polynomials – T 0(x) = 1 – T 1(x)

More examples • Chebyshev (Чебышёв) polynomials – T 0(x) = 1 – T 1(x) = x – Tn(x) = 2 x Tn– 1 (x) – Tn– 2 (x) • Pascal’s triangle / binomial coefficients – How should we define this one? r=0 r=1 r=2 r=3 n=0 1 n=1 1 1 n=2 1 n=3 1 3 3 1 n=4 1 4 6 4 r=4 1

Recursive definition • How do we write a recursive definition, e. g. for a

Recursive definition • How do we write a recursive definition, e. g. for a list of numbers? • Often, it’s sufficient to do… – Base case(s): Write the rule for the first value. – Recursive rule: How to go from one value to the next? • Observe difference between explicit and recursive formulas. Simple example: 4, 5, 6, 7, 8, … – Explicit formula is: an = n + 3 – Recursive definition: a 1 = 4, and an = an– 1 + 1 for n ≥ 2.

More examples • 4, 7, 10, 13, 16, … a 1 = 4, and

More examples • 4, 7, 10, 13, 16, … a 1 = 4, and for n ≥ 2, an = an– 1 + 3 • 5, 10, 20, 40, 80, … a 1 = 5, and for n ≥ 2, an = 2 an– 1 • 7, – 9, 11, – 13, 15, – 17, 19, – 21, … a 1 = 7, and for n ≥ 2, an = (– 1)n+1 (|an– 1|+ 2) • In each case above, what is the explicit formula?

Try this • Write both a recursive and an explicit formula for these sequences:

Try this • Write both a recursive and an explicit formula for these sequences: 16, 8, 4, 2, 1, ½, … 10, 9, 8, 7, 6, … 2, 222, 2222, …

For strings • Often a set of strings is introduced this way. • Skill:

For strings • Often a set of strings is introduced this way. • Skill: given a verbal description of a set of strings, write a precise definition • Base case(s): think about the shortest string that satisfies the definition • Recursive case(s): how do we build larger strings? • *** See handout • Given a recursive definition, we can prove using induction that an essential property (e. g. even length) is satisfied.

Regular expressions (review) • Often, a set of strings does not have to be

Regular expressions (review) • Often, a set of strings does not have to be defined recursively. • A regular expression gives the essential format of a string in the language, using these operators: – Concatenation – +, which means “or” – *, which means 0 or more instances of • Examples… (can you convert these to recursive definitions? ) – 001(0+1)* – (0+1)0(0+1)* – 0(0+1)*1 – 0*10* + 1*01* – ((0+1)1)*

Recurrence relations • A sequence of numbers defined recursively • Often we need to

Recurrence relations • A sequence of numbers defined recursively • Often we need to “solve” a recurrence: Simplify a recursive formula into an explicit one. – Why? Somebody might ask for the 100 th element! • Examples: – a 1 = 3 and an = an– 1 + 5 – a 0 = 1 and an = 2 an– 1 • If we expand the recursive rule a couple of times, we see a simple pattern emerge, and we can write an explicit formula.

Simple types • Let’s begin by considering 3 kinds of recurrences: 1. Adding a

Simple types • Let’s begin by considering 3 kinds of recurrences: 1. Adding a constant a 1 = 6 an = an– 1 + 4 2. Multiplying by a constant a 1 = 5 an = 3 an– 1 3. Both a 1 = 7 an = 2 an– 1 + 1 • The first two are simple sequences. The third one takes some work.

Example of 3 rd type • a 0 = 1 and an = 3

Example of 3 rd type • a 0 = 1 and an = 3 an– 1 + 5 • Expand the recursive definition to detect a pattern. a 1 = 3 a 0 + 5 a 2 = 3 a 1 + 5 = 3(3 a 0 + 5) + 5 = 9 a 0 + 3 * 5 + 5 a 3 = 3 a 2 + 5 = 3(9 a 0 + 3*5 + 5) + 5 = 27 a 0 + 9*5 + 3*5 + 5 a 4 = 3 a 3 + 5 = 3(27 a 0 + 9*5 + 3*5 + 5) + 5 = 81 a 0 + 27*5 + 9*5 + 3*5 + 5 = 34 a 0 + 5*(33 + 32 +31 +30) So, an = 3 na 0 + 5*(3 n – 1)/2 • Since a 0 = 1, we conclude: an = (7/2) 3 n – (5/2) • If we didn’t know value of a 0, we would leave it in our answer. Doing so is not considered recursion.

Additional types • Here are some more interesting recurrence relation types 4. Based on

Additional types • Here are some more interesting recurrence relation types 4. Based on the previous two terms a 1 = 2, a 2 = 1, an = 6 a n – 1 + 7 a n – 2 – Note the two base cases! 5. Previous two terms, plus an independent term a 1 = 3, a 2 = 0, an = 7 a n – 1 + 8 a n – 2 + 4 n – This is called an “inhomogeneous” recurrence relation, and we’ll do these last.

Remarks • In general, recurrence relations are difficult to solve, because we have to

Remarks • In general, recurrence relations are difficult to solve, because we have to expand look for a pattern. • In some cases, there is a much more straightforward approach: Type First order Second order Form of recursive rule Form of solution an = K a n – 1 ___ K n an = K a n – 1 + L a n – 2 ___ p n + ___ q n • In second order, we create a quadratic equation using K and L. Let p and q be the roots.

Solving 2 nd order • First, set equal to zero, and solve the corresponding

Solving 2 nd order • First, set equal to zero, and solve the corresponding quadratic equation with roots p and q. • Form of the solution is an = c 1 p n + c 2 q n, where c 1 and c 2 are determined by the base cases. – If there is a double root, insert factor n in one term. • Try these examples a 0 = 7 a 1 = 1 an = 5 a n – 1 – 6 a n – 2 a 0 = 1 a 1 = 1 an = 3 a n – 1 + 4 a n – 2 a 0 = 2 a 1 = 5 an = 7 an – 12 an – 2 a 0 = 1 a 1 = 1 an = 6 a n – 1 – 9 a n – 2 a 0 = 4 a 1 = 10 an = 6 an – 1 – 8 an – 2

Verifying • Now that we can solve a recurrence, it would be nice to

Verifying • Now that we can solve a recurrence, it would be nice to check our answer for all n. Use induction. • We have 2 ways to write a function: recursive and explicit. Pn is the statement that n 0, the two formulas are equivalent. • First step: Usually there are 2 base cases, e. g. n = 0 and n = 1. Verify them individually: – recursive(0) = explicit(0) – recursive(1) = explicit(1)

Verifying (2) • Next, assume Pk is true, which says the recursive and explicit

Verifying (2) • Next, assume Pk is true, which says the recursive and explicit are equal all the way up to n = k. In other words, we can say: recursive(k) = explicit(k) • Next, we consider the next term of the sequence, and write out the formula for recursive(k+1). – It should look like this: recursive(k+1) = … recursive(k) + … recursive(k– 1) – On the right hand side, replace recursive calls to k and k – 1 with explicit calls to k and k – 1. recursive(k+1) = … explicit(k) + … explicit(k– 1) – Finally, simplify the right side until it looks like explicit(k+1). Then your final equation is Pk+1.

Verifying (3) • Let’s try one we just solved. We’d like to verify that

Verifying (3) • Let’s try one we just solved. We’d like to verify that an = 20(2 n) – 13(3 n) solves the recurrence a 0 = 7, a 1 = 1, an = 5 an– 1 – 6 an– 2 • Begin with base cases. Plug 0 and 1 into explicit formula to see if they match base case values 7 and 1. • Next, assume that for some k, the explicit formula is correct. In other words: ak = 5 ak – 1 – 6 ak – 2 = 20(2 k) – 13(3 k) • Let’s compute the recursive formula for k+1, and substitute the explicit formula where we can. ak+1 = 5 ak – 6 ak – 1 = 5[20(2 k) – 13(3 k)] – 6[20(2 k – 1) – 13(3 k – 1)] = 100(2 k) – 65(3 k) – 120(2 k – 1) + 78(3 k – 1) = 100(2 k) – 65(3 k) – 60(2 k) + 26(3 k) = 40(2 k) – 39(3 k) = 20(2 k+1) – 13(3 k+1) Note this last equation is Pk+1.

Non-homogeneous case • More about recurrence relations – i. e. recursively defined sequence of

Non-homogeneous case • More about recurrence relations – i. e. recursively defined sequence of values – “solving” one means re-write as explicit formula – Purposes: explicit may be more efficient to evaluate; useful in analysis of an algorithm • Two kinds – Homogeneous: In the recursive rule, all terms refer to the sequence (e. g. an or an-1 or an-2) √ – Non-homogeneous: Some terms do not refer to the sequence (e. g. 15, 3 n 2, 2 n) • Today we will solve non-homogeneous problems, since this is the more general case.

What they look like • Here is a non-homogeneous recurrence relation: an = 2

What they look like • Here is a non-homogeneous recurrence relation: an = 2 an-1 – an-2 + 7 • We would also specify base cases, e. g. a 2 and a 3. • We customarily write all of the sequence terms on the left, to obtain: an – 2 an-1 + an-2 = 7 • Notice the right side is not 0. If it were 0, this would just be a homogeneous equation, which we know how to solve already. • Note: no limit on the number of terms on left. Usually 2, 3, or 4.

Procedure 1. Find a particular solution to the recurrence. a. Examine right side of

Procedure 1. Find a particular solution to the recurrence. a. Examine right side of equation to deduce the general form of the solution, e. g. 2 nd-degree polynomial. This will be some function of n. b. Express an , an-1 , an-2 using this general form by plugging in n, (n – 1), (n – 2), respectively. c. Substitute into LHS of equation d. Determine values of constant coefficients. 2. Find a homogeneous solution to the recurrence. a. Solve the parallel (characteristic) equation. Roots become exponentials. E. g. x = 2 means we need 2 n. b. Write general form of homogeneous solution, and add the particular solution you found in #1. c. Use base cases to determine coefficient values.

Example #1 • • • an – 5 an-1 + 6 an-2 = 1

Example #1 • • • an – 5 an-1 + 6 an-2 = 1 a 0 = 3/2 a 1 = 9/2 The right side of equation is a constant, so this is the form of our particular solution for an. Let it be c. In this case, if an = c, then an-1 = c and an-2 = c also. Re-write the equation, substituting: c – 5 c + 6 c = 1 We need to determine c. We get 2 c = 1 c = 1/2. So, our particular solution is just an = 1/2. What does this mean? It tells us by how much our solution differs from the homogeneous situation.

continued • • • an – 5 an-1 + 6 an-2 = 1 a

continued • • • an – 5 an-1 + 6 an-2 = 1 a 0 = 3/2 a 1 = 9/2 We now need a homogeneous solution. The characteristic equation is x 2 – 5 x + 6 = 0. Roots are x = 2 and 3. Homogeneous solution has form c 1 2 n + c 2 3 n. Let’s add (particular) + (homogeneous): an = c 1 2 n + c 2 3 n + 1/2 Use base cases to determine c 1 and c 2, and we’re done. Solution is an = – 2 n + (2) 3 n + 1/2

Particular forms • The first step is to determine the format of the particular

Particular forms • The first step is to determine the format of the particular solution. Based on the right side of the recurrence equation. RHS Root of Char. Polynomial Form of particular soln Polynomial 1 is NOT a root Polynomial (of same degree) Polynomial 1 is a root, r times Polynomial * nr k cn c is NOT a root a cn k cn c is a root, r times a nr c n Polynomial * k cn c is NOT a root Polynomial * cn Polynomial * k cn c is a root, r times Polynomial * nr cn

Example forms RHS Form of particular soln 7 5 n 2 (18) 3 n,

Example forms RHS Form of particular soln 7 5 n 2 (18) 3 n, char roots are 5, 2 c an 2 + bn + c a 3 n (n+1) 3 n, char roots are 5, 2 (12) 3 n, char roots are 5, 3 (20) 3 n, char roots are 3, 3 (n+1) 2 n, char roots are 2, 2 (an + b) 3 n an 2 3 n (an + b) n 2 2 n • Special rule: if 1 is a characteristic root, you need to multiply the particular form by n. If 1 is a root of multiplicity r, multiply by nr.

Example #2 an + 5 an-1 + 6 an-2 = 42*4 n a 2

Example #2 an + 5 an-1 + 6 an-2 = 42*4 n a 2 = 278 • Form of particular solution is c*4 n. • In this case, we have an = c * 4 n an-1 = c * 4 n – 1 a 3 = 962 an-2 = c * 4 n – 2 • Substitute into equation, to obtain: (c * 4 n) + 5(c * 4 n – 1) + 6(c * 4 n – 2) = 42*4 n c * 4 n + (5/4)c * 4 n + 6/16 c * 4 n = 42 *4 n c (1 + 5/4 + 3/8) = 42 c = 16 • So, our particular solution is an = 16 * 4 n.

continued • • • an + 5 an-1 + 6 an-2 = 42*4 n

continued • • • an + 5 an-1 + 6 an-2 = 42*4 n a 2 = 278 a 3 = 962 Next, we need homogeneous solution. Characteristic equation is x 2 + 5 x + 6 = 0. Roots are – 2 and – 3. Homogeneous solution has form c 1 (– 2)n + c 2 (– 3)n. Let’s add (particular) + (homogeneous): an = c 1 (– 2)n + c 2 (– 3)n + 16*4 n Use base cases to determine values of c 1 and c 2. It turns out c 1 = 1 and c 2 = 2.

Example #3 • • an – an-1 = 2 n – 2 a 2

Example #3 • • an – an-1 = 2 n – 2 a 2 = 4 The RHS is 2 n – 2, so ordinarily this means the form of the particular solution is (an + b). However, 1 is a characteristic root, so need to multiply by n. This means an = an 2 + bn, and an-1 = a(n– 1)2 + b(n– 1) Rewrite equation: (an 2+bn) – (a(n – 1)2+b(n – 1)) = 2 n – 2 2 an – a + b = 2 n – 2 2 a = 2, and (–a + b) = – 2 a = 1 and b = – 1 This means our particular solution is an = n 2 – n.

continued • • • an – an-1 = 2 n – 2 a 2

continued • • • an – an-1 = 2 n – 2 a 2 = 4 Now we need a homogeneous solution. x– 1=0 x=1 Its general form is c*1 n or simply c. To solve for c, we need (particular) + (homogeneous) an = n 2 – n + c Substitute a 2 = 4, and we obtain 4 = 22 – 2 + c c = 2, so our total solution is an = n 2 – n + 2.

Homework • The “nine rings puzzle” is an ancient Chinese game. The goal is

Homework • The “nine rings puzzle” is an ancient Chinese game. The goal is to remove 9 rings from a handle according to certain rules. The problem can generalize to n rings. • The puzzle asks: how many moves are necessary, in terms of n, the initial number of rings? The various solutions are 1, 2, 5, 10, 21, 42, … The current number equals the previous number plus twice the number before that, plus 1. 1. Write this sequence as a recurrence relation. Use the first two terms as base cases, a 1 = 1 and a 2 = 2. 2. Solve the recurrence. 3. Briefly check your solution by verifying a 3 is computed correctly by your explicit solution.