Presentation is loading. Please wait.

Presentation is loading. Please wait.

Proofs, Recursion and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2.5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,

Similar presentations


Presentation on theme: "Proofs, Recursion and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2.5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,"— Presentation transcript:

1 Proofs, Recursion and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2.5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs, Recursion and Analysis of Algorithms

2 Solving Recurrence Relations Example 29 defined a recurrence relation S(1) = 2 Eq(1) S(n) =2S(n – 1) for n >= 2 Eq(2) We can recognize S as the sequence 2, 4, 8, 16,... which can also be written as S(n) = 2 n. Eq(3) If we want to compute S(10) we can do so directly using the second definition, instead of having to compute S(2), S(3), … Equation 3 is an example of what is called a closed- form solution to the recurrence relation. Finding a closed form-solution is called solving the recurrence relation. Section 2.5Recurrence Relations1

3 Section 2.5Recurrence Relations2 Expand, guess and verify One way to solve: so-called “expand, guess, and verify” approach, where we “expand” the sequence by generating several terms using the recurrence relation “guess” at a closed form solution by looking at the terms in the sequence and detecting a pattern “verify” the solution by actually proving that it is correct. Expand by working backwards: S(n), S(n - 1), S(n - 2), …, S(1). In general there will be k expansions, where k = n – 1.

4 Section 2.5Recurrence Relations3 Expand, guess and verify: Example Show that S(n) = 2 n for the following recurrence relation: S(1) = 1 S(n) = 2S(n-1) for n  2 Expansion: Using the recurrence relation over again everytime S(n) = 2S(n-1)  S(n) = 2(2S(n-2)) = 2 2 S( n -2)  S(n) = 2 2 (2S(n-3)) = 2 3 S( n -3) Looking at the developing pattern, we guess that after k such expansions, the equation has the form S(n) = 2 k S(n-k) This should stop when n-k =1, hence k = n-1, S(n) = 2 n-1 S(1)  S(n) = 2.2 n-1 = 2 n At this point we have a conjecture, it still must be proven.

5 Section 2.5Recurrence Relations4 Verification Step for Expand, Guess & Verify Confirm derived closed-form solution by induction on the value of n. In other words, prove that S(n) = 2.2 n-1 = 2 n Basis step, S(l) = 2 = 2 1. True, by definition. Now, assume that S(k) = 2 k. Then S(k+1) = 2S(k) (by using the recurrence relation definition) S(k+1) = 2(2 k ) (by using the above inductive hypothesis)  S(k+1) = 2 k+1 This proves that our closed-form solution is correct.

6 Section 2.5Recurrence Relations5 Properties of recurrence relations A recurrence relation is linear if it can be written as S(n) = f 1 (n)S(n-1) + f 2 (n)S(n-2) + …..+ f k (n)S(n-k) + g(n) The term linear means that each term of the sequence is defined as a linear function of the preceding terms; i.e., all the S(i) as shown above have power 1. Example: F(n) = F(n-1) + F(n-2) In general, the f i ’s can be expressions involving n. The relation is homogeneous if g(n) = 0 for all n.

7 Section 2.5Recurrence Relations6 Order of Recurrence Relation A recurrence relation is said to have constant coefficients if the f’s are all constants. Fibonaci relation is homogenous and linear: F(n) = F(n-1) + F(n-2) Non-constant coefficients: T(n) = 2nT(n-1) + 3n 2 T(n-2) Order of a relation is defined by the number of previous terms in a relation for the n th term. First order: S(n) = 2S(n-1) n th term depends only on term n-1 Second order: F(n) = F(n-1) + F(n-2) n th term depends only on term n-1 and n-2 Third Order: T(n) = 3nT(n-2) + 2T(n-1) + T(n-3) n th term depends only on term n-1 and n-2 and n-3

8 Section 2.5Recurrence Relations7 Solving Recurrence Relations Equation 8, page 150 is a formula that can be used to solve closed-form solutions for linear, first-order recurrence relations with constant coefficients. Table 2.6 on page 151 describes the EGV method of developing a conjectured solution and then explains how to use the solution formula to verify the conjecture. (Assuming, of course, that it’s correct.)

9 Section 2.5Recurrence Relations8 Properties of recurrence relations A nonlinear relation is the one that has earlier values in the definition as powers other than 1. Example: F(n+1) = 2nF(n-1)(1-F(n-1)) Solutions are quite complex. Homogenous relation is a relation that has g(n) = 0 for all n Example: a(n) – a(n-1) = 2n Inhomogenous relation: Example: S(n) = 2S(n-1)


Download ppt "Proofs, Recursion and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2.5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,"

Similar presentations


Ads by Google