5.3 Second Order Recurrence Relations In the previous section we saw how to solve first order linear recurrence relations. This is when an is given by a linear formula of an-1, i.e. an = pnan-1 + sn where pn and sn are given sequences. In this section and the next we look at second order linear recurrence relations when an is given by a linear formula of an-1 and an-2, i.e an = pnan-1 + qnan-2 + sn where pn, qn and sn are given sequences. For simplicity we concentrate on the constant coefficient case when pn and qn don't vary with n, i.e. (1) an = pan-1 + qan-2 + sn where p and q are just numbers. In this section we look at the situation where the recurrence relation is homogeneous which is when rn = 0 for all n, i.e. (2) an = pan-1 + qan-2 In the next section we look at the inhomogeneous case (1). We illustrate the method of solution of equations of the form (2) with the following example. Example 1. Consider the equation (3) an = an-1 + 2an-2 along with the initial conditions (4) a0 = 2 and a1 = 3 (3) is the special case of (2) when p = 1 and q = 2. To solve equations of the form (2) we start by looking for solutions which have the special form (5) an = rn where r is a number to be determined. To determine r we substitute into (2). We illustrate this with (3). If an is given by (3) then (6) an-1 = rn-1 5.3 - 1 and (7) an-2 = rn-2 Substituting (5), (6) and (7) into (3) gives rn = rn-1 + 2rn-2 Divide this by rn-2 giving r2 = r + 2 or r2 - r - 2 = 0 This is called the characteristic equation. It is a quadratic equation. The roots are the values of r in the solutions an = rn. To solve we either factor or use the quadratic formula. In this case we can factor. (r – 2)(r + 1) = 0 This equation has two solutions r1 = 2 and r2 = - 1 Recall the r is a number such that (5) is a solution to (3). This gives the following two solutions to (3) an = 2n and an = (- 1)n Neither of these solutions satisfy the initial conditions (4). In order to get a solution which satisfies (4) we need to take a superposition of these two solutions, i.e. multiply them by constants and add. We can do this because of the following Superposition Principle. If an and bn are two solutions of the equation (2) then so are an + bn Aan Aan + Bbn for any constants A and B. Proof. By hypothesis we have 5.3 - 2 (8) an = pan-1 + qan-2 and (9) bn = pbn-1 + qbn-2 If we add the equations (8) and (9) we get (an + bn) = p(an-1 + bn-1) + q(an-2 + bn-2) which shows that an + bn is a solution. If we multiply equation (8) by A we get (Aan) = p(Aan-1) + q(Aan-2) which shows that Aan is a solution. If we multiply equation (8) by A and equation (9) by B and add we get (Aan + Bbn) = p(Aan-1 + Bbn-1) + q(Aan-2 + Bbn-2) which shows that Aan + Bbn is a solution. // It follows from the superposition principle that an = A 2n + B (- 1)n is a solution to (3) for any constants A and B. Now we choose the constants A and B to satisfy the initial conditions (4). Plugging in n = 0 we get 2 = a0 = A 20 + B (- 1)0 = A + B Plugging in n = 1 we get 3 = a1 = A 21 + B (- 1)1 = 2A - B This is a system of two equations and two unknowns. We multiply them by numbers to get the coefficient of one of the unknowns the same and then add or subtract. In this case we can just add the equations A + B = 2 2A - B = 3 3A = 5 5 A = 3 5 1 B = 2–A = 2-3 = 3 5 1 So an = 3 2n + 3 (- 1)n. If we were interested in the behavior for large n then an = O(2n). If the roots of the characteristic equation are equal then an = nrn is also a solution where r is a root of the characteristic equation. Then the general solution is an = Arn + Bnrn. 5.3 - 3 Example 2. Consider the equation (10) an = 4an-1 - 4an-2 along with the initial conditions (11) a0 = 2 and a1 = 5 Try an = rn. Substituting into (10) gives rn = 4rn-1 - 4rn-2 r2 = 4r – 4 r2 - 4r + 4 = 0 (r – 2)2 = 0 r1 = r2 = 2 So an = 2n is a solution. As indicated, when the roots of the characteristic equation are equal, then an = nrn is a solution. So in this case an = n2n is a solution. We can check this by plugging into (10). When we do this we get ? n2n = 4(n – 1)2n-1 - 4(n – 2)2n-2 Dividing by 2n-2 gives ? ? 4n = 8(n – 1) - 4(n – 2) 4n = 8n – 8 - 4n + 8 which is true. We get the general solution by taking a superposition of an = 2n and an = n2n. So an = A 2n + Bn2n is a solution to (10) for any constants A and B. Now we choose the constants A and B to satisfy the initial conditions (4). Plugging in n = 0 we get 2 = a0 = A 20 + B (0) 20 = A Plugging in n = 1 we get 5 = a1 = A 21 + B (1) 21 = 2A + 2B = (2)(2) + 2B. 1 1 So B = 2 and an = 2 2n + 2 n2n = (n + 4)2n-1. If we were interested in the behavior for large n then an = O(n2n). Example 3 (Fibonacci sequence). Recall the Fibonacci sequence fn is defined by the recurrence relation (12) fn = fn-1 + fn-2 along with the initial conditions 5.3 - 4 (13) f0 = 1 and f1 = 1 To solve we look for solutions of the form an = rn where r determined by substituting int (12). Doing this gives rn = rn-1 + rn-2 Divide this by rn-2 giving r2 = r + 1 or r2 - r - 1 = 0 This doesn't look easy to factor so we use the quadratic formula. r = 1 1+4 1 5 = 2 2 This equation has two solutions r1 = 1+ 5 1 + 2.24 = 1.62 2 2 r2 = 1- 5 1 - 2.24 = - 0.62 2 2 This gives the following two solutions to (3) 1 + 5n fn = 2 1.62n and 1 - 5n fn = 2 (- 0.62)n By the superposition principle the general solution is fn 1 + 5n 1 - 5n = A +B 2 2 is a solution to (3) for any constants A and B. Now we choose the constants A and B to satisfy the initial conditions (13). Plugging in n = 0 we get 1 = f0 = A + B So B = 1 – A. Plugging in n = 1 we get 5.3 - 5 1 + 5 1 - 5 1 = f1 = A 2 + B 2 Using B = 1 – A gives 1 = 5A + 1- 5 2 or A = 1+ 5 5+ 5 5 + 2.24 = = 0.724 10 10 2 5 B = 1 - A = 1 - 5+ 5 5- 5 5 - 2.24 = = 0.276 10 10 10 So fn = n n 5 + 5 1 + 5 5 - 5 1 - 5 + 0.724 (1.62)n + 0.276 (- 0.62)n 10 2 10 2 Note that (- 0.62)n 0 as n , so fn 0.724 (1.62)n for large n. In particular, 1 + 5 n fn = O 2 . Example 4. Let Sn be the number of n bit strings that don't contain two consecutive 1's. Find a formula for Sn. We discussed this in section 5.1 where we saw that Sn satisfied the recurrence relation and initial conditions if n 3 Sn-1 + Sn-2 if n = 1 Sn = 2 3 if n = 2 This is the same recurrence relation as the Fibonacci sequence fn except the initial conditions are different. In fact S1 = f2 and S2 = f3. It follows that Sn = fn+1 for all n. So 5 + 5 1 + 5 5 - 5 1 - 5 + 10 2 10 2 n n 5 + 3 5 1 + 5 5 - 3 5 1 - 5 + = 10 2 10 2 n 1.17 (1.62) - 0.17 (- 0.62)n n+1 n+1 Sn = 5.3 - 6