CS 350 Algorithms and Complexity Winter 2015 Lecture 4: Analyzing Recursive Algorithms Andrew P. Black Department of Computer Science Portland State University Tuesday, 20 January 2015 General Plan for Analysis of Recursive algorithms ✦ ✦ ✦ ✦ ✦ Decide on parameter n indicating input size Identify algorithm’s basic operation Determine worst, average, and best cases for input of size n Set up a recurrence relation, with initial condition, for the number of times the basic operation is executed Solve the recurrence, or at least ascertain the order of growth of the solution (see Levitin Appendix B) 2 Tuesday, 20 January 2015 Ex 2.4, Problem 1(a) ! Use a piece of paper and do this now, individually. " Solve this recurrence relation: x(n) = x(n 1) + 5 for n > 1 x(1) = 0 3 Tuesday, 20 January 2015 Individual Problem (Q1): Solve the recurrence x(n) = x(n﹣1) + 5 for n > 1 x(1) = 0 What’s the answer? 4 Tuesday, 20 January 2015 Individual Problem (Q1): Solve the recurrence x(n) = x(n﹣1) + 5 for n > 1 x(1) = 0 What’s the answer? A. B. C. D. x(n) = n﹣1 x(n) = 5n x(n) = 5n﹣5 None of the above 5 Tuesday, 20 January 2015 My Solution x(n) = x(n 1) + 5 for all n > 1 x(1) = 0 6 Tuesday, 20 January 2015 My Solution x(n) = x(n 1) + 5 for all n > 1 x(1) = 0 replace n by n−1: 6 Tuesday, 20 January 2015 My Solution x(n) = x(n 1) + 5 for all n > 1 x(1) = 0 replace n by n−1: x(n 1) = x(n 2) + 5 6 Tuesday, 20 January 2015 My Solution x(n) = x(n 1) + 5 for all n > 1 x(1) = 0 replace n by n−1: x(n 1) = x(n 2) + 5 substitute for x(n−1): 6 Tuesday, 20 January 2015 My Solution x(n) = x(n 1) + 5 for all n > 1 x(1) = 0 replace n by n−1: x(n substitute for x(n−1): x(n) = x(n 1) = x(n 2) + 5 2) + 5 + 5 6 Tuesday, 20 January 2015 My Solution x(n) = x(n 1) + 5 for all n > 1 x(1) = 0 replace n by n−1: x(n substitute for x(n−1): x(n) = x(n 1) = x(n 2) + 5 2) + 5 + 5 substitute for x(n−2): 6 Tuesday, 20 January 2015 My Solution x(n) = x(n 1) + 5 for all n > 1 x(1) = 0 replace n by n−1: x(n substitute for x(n−1): x(n) = x(n substitute for x(n−2): = x(n 1) = x(n 2) + 5 2) + 5 + 5 3) + 5 + 5 + 5 6 Tuesday, 20 January 2015 My Solution x(n) = x(n 1) + 5 for all n > 1 x(1) = 0 replace n by n−1: x(n substitute for x(n−1): x(n) = x(n substitute for x(n−2): = x(n 1) = x(n 2) + 5 2) + 5 + 5 3) + 5 + 5 + 5 generalize: 6 Tuesday, 20 January 2015 My Solution x(n) = x(n 1) + 5 for all n > 1 x(1) = 0 replace n by n−1: x(n substitute for x(n−1): x(n) = x(n substitute for x(n−2): = x(n 3) + 5 + 5 + 5 generalize: = x(n i) + 5i 1) = x(n 2) + 5 2) + 5 + 5 8i < n 6 Tuesday, 20 January 2015 My Solution x(n) = x(n 1) + 5 for all n > 1 x(1) = 0 replace n by n−1: x(n substitute for x(n−1): x(n) = x(n substitute for x(n−2): = x(n 3) + 5 + 5 + 5 generalize: = x(n i) + 5i 1) = x(n 2) + 5 2) + 5 + 5 8i < n put i = (n−1) : 6 Tuesday, 20 January 2015 My Solution x(n) = x(n 1) + 5 for all n > 1 x(1) = 0 replace n by n−1: x(n substitute for x(n−1): x(n) = x(n substitute for x(n−2): = x(n 3) + 5 + 5 + 5 generalize: = x(n i) + 5i put i = (n−1) : = x(n (n 1) = x(n 2) + 5 2) + 5 + 5 8i < n 1)) + 5(n 1) 6 Tuesday, 20 January 2015 My Solution x(n) = x(n 1) + 5 for all n > 1 x(1) = 0 replace n by n−1: x(n substitute for x(n−1): x(n) = x(n substitute for x(n−2): = x(n 3) + 5 + 5 + 5 generalize: = x(n i) + 5i put i = (n−1) : = x(n (n 1) = x(n 2) + 5 2) + 5 + 5 8i < n 1)) + 5(n 1) substitute for x(1): 6 Tuesday, 20 January 2015 My Solution x(n) = x(n 1) + 5 for all n > 1 x(1) = 0 replace n by n−1: x(n substitute for x(n−1): x(n) = x(n substitute for x(n−2): = x(n 3) + 5 + 5 + 5 generalize: = x(n i) + 5i put i = (n−1) : = x(n (n substitute for x(1): = 5(n 1) 1) = x(n 2) + 5 2) + 5 + 5 8i < n 1)) + 5(n 1) 6 Tuesday, 20 January 2015 Ex 2.4, Problem 1(c) ! Use a piece of paper and do this now, individually. " Solve this recurrence relation: x(n) = x(n 1) + n for n > 0 x(0) = 0 7 Tuesday, 20 January 2015 Ex 2.4, Problem 1(d) ! Use a piece of paper and do this now, individually. k " Solve this recurrence relation for n = 2 : x(n) = x(n/2) + n for n > 1 x(1) = 1 8 Tuesday, 20 January 2015 What does that mean? 9 Tuesday, 20 January 2015 What does that mean? ! “Solving a Recurrence relation” means: 9 Tuesday, 20 January 2015 What does that mean? ! “Solving a Recurrence relation” means: " find an explicit (non-recursive) formula that satisfies the relation and the initial condition. 9 Tuesday, 20 January 2015 What does that mean? ! “Solving a Recurrence relation” means: " " find an explicit (non-recursive) formula that satisfies the relation and the initial condition. For example, for the relation x(n) = 3x(n the solution is x(n) = 4 ⇥ 3n 1) for n > 1, x(1) = 4 1 9 Tuesday, 20 January 2015 What does that mean? ! “Solving a Recurrence relation” means: " " find an explicit (non-recursive) formula that satisfies the relation and the initial condition. For example, for the relation x(n) = 3x(n the solution is " x(n) = 4 ⇥ 3n 1) for n > 1, x(1) = 4 1 Check: 9 Tuesday, 20 January 2015 What does that mean? ! “Solving a Recurrence relation” means: " " find an explicit (non-recursive) formula that satisfies the relation and the initial condition. For example, for the relation x(n) = 3x(n the solution is " x(n) = 4 ⇥ 3n 1) for n > 1, x(1) = 4 1 Check: x(1) = 4 ⇥ 30 = 4 ⇥ 1 = 4 9 Tuesday, 20 January 2015 What does that mean? ! “Solving a Recurrence relation” means: " " find an explicit (non-recursive) formula that satisfies the relation and the initial condition. For example, for the relation x(n) = 3x(n the solution is " x(n) = 4 ⇥ 3n 1) for n > 1, x(1) = 4 1 Check: x(1) = 4 ⇥ 30 = 4 ⇥ 1 = 4 x(n) = 3x(n 1) definition of recurrence = = Tuesday, 20 January 2015 3 ⇥ [4 ⇥ 3(n 4 ⇥ 3n 1 1) 1 ] substitute solution = x(n) 9 What does that mean? ! “Solving a Recurrence relation” means: " " find an explicit (non-recursive) formula that satisfies the relation and the initial condition. For example, for the relation x(n) = 3x(n the solution is " x(n) = 4 ⇥ 3n 1) for n > 1, x(1) = 4 1 Check: x(1) = 4 ⇥ 30 = 4 ⇥ 1 = 4 x(n) = 3x(n 1) definition of recurrence = = Tuesday, 20 January 2015 3 ⇥ [4 ⇥ 3(n 4 ⇥ 3n 1 1) 1 ] substitute solution = x(n) 9 What does that mean? ! “Solving a Recurrence relation” means: " " find an explicit (non-recursive) formula that satisfies the relation and the initial condition. For example, for the relation x(n) = 3x(n the solution is " x(n) = 4 ⇥ 3n 1) for n > 1, x(1) = 4 1 Check: x(1) = 4 ⇥ 30 = 4 ⇥ 1 = 4 x(n) = 3x(n 1) definition of recurrence = = Tuesday, 20 January 2015 3 ⇥ [4 ⇥ 3(n 4 ⇥ 3n 1 1) 1 ] substitute solution = x(n) 9 c. x(n) = x(n − 1) + n for n > 0, x(0) = 0 Ex 2.4, Problem 2 e. x(n) = x(n/3) + 1 for n > 1, x(1) = 1 d. x(n) = x(n/2) + n for n > 1, x(1) = 1 (solve for n = 2k ) (solve for n = 3k ) 2. Set up and solve a recurrence relation for the number of calls made by F (n), the recursive algorithm for computing n!. 3. Consider the following recursive algorithm for computing the sum of the F(n)n ≝ if = 0 = 13 + 23 + ... + n3 . first cubes:n S(n) then return 1 Algorithm S(n) else return F(n – 1) ⨉ n //Input: A positive integer n //Output: The sum of the first n cubes if n = 1 return 1 else return S(n − 1) + n ∗ n ∗ n a. Set up and solve a recurrence relation for the number of times the algorithm’s basic operation is executed. b. How does this algorithm compare with the straightforward nonrecursive algorithm for computing this function? 10 4. Consider the following recursive algorithm. Tuesday, 20 January 2015 my solution F(n) ≝ if n = 0 then return 1 else return F(n – 1) ⨉ n 11 Tuesday, 20 January 2015 my solution F(n) ≝ if n = 0 then return 1 else return F(n – 1) ⨉ n Let C(n) be the number of calls made in computing F (n) 11 Tuesday, 20 January 2015 my solution F(n) ≝ if n = 0 then return 1 else return F(n – 1) ⨉ n Let C(n) be the number of calls made in computing F (n) C(n) = C(n C(0) = 1 1) + 1 (when n = 0, there is 1 call) 11 Tuesday, 20 January 2015 my solution F(n) ≝ if n = 0 then return 1 else return F(n – 1) ⨉ n Let C(n) be the number of calls made in computing F (n) C(n) = C(n 1) + 1 C(0) = 1 (when n = 0, there is 1 call) C(n) = C(n = [C(n 1) +1 2) + 1] + 1 11 Tuesday, 20 January 2015 my solution F(n) ≝ if n = 0 then return 1 else return F(n – 1) ⨉ n Let C(n) be the number of calls made in computing F (n) C(n) = C(n 1) + 1 C(0) = 1 (when n = 0, there is 1 call) C(n) = C(n 1) +1 = [C(n 2) + 1] + 1 = C(n 2) + 2 11 Tuesday, 20 January 2015 my solution F(n) ≝ if n = 0 then return 1 else return F(n – 1) ⨉ n Let C(n) be the number of calls made in computing F (n) C(n) = C(n 1) + 1 C(0) = 1 (when n = 0, there is 1 call) C(n) = C(n 1) +1 = [C(n 2) + 1] + 1 = C(n = C(n 2) + 2 i) + i 8i < n (generalize) 11 Tuesday, 20 January 2015 my solution F(n) ≝ if n = 0 then return 1 else return F(n – 1) ⨉ n Let C(n) be the number of calls made in computing F (n) C(n) = C(n 1) + 1 C(0) = 1 (when n = 0, there is 1 call) C(n) = C(n = [C(n Put i = n: 1) +1 2) + 1] + 1 = C(n 2) + 2 = C(n i) + i 8i < n = C(0) + n (generalize) 11 Tuesday, 20 January 2015 my solution F(n) ≝ if n = 0 then return 1 else return F(n – 1) ⨉ n Let C(n) be the number of calls made in computing F (n) C(n) = C(n 1) + 1 C(0) = 1 (when n = 0, there is 1 call) C(n) = C(n = [C(n Put i = n: 1) +1 2) + 1] + 1 = C(n 2) + 2 = C(n i) + i 8i < n = C(0) + n (generalize) = 1+n 11 Tuesday, 20 January 2015 my solution F(n) ≝ if n = 0 then return 1 else return F(n – 1) ⨉ n Let C(n) be the number of calls made in computing F (n) C(n) = C(n 1) + 1 C(0) = 1 (when n = 0, there is 1 call) C(n) = C(n = [C(n Put i = n: 1) +1 2) + 1] + 1 = C(n 2) + 2 = C(n i) + i 8i < n = C(0) + n (generalize) = 1+n Now check! Tuesday, 20 January 2015 11 e. x(n) = x(n/3) + 1 for n > 1, x(1) = 1 (solve for n = 3k ) Ex 2.4, Problem 3 2. Set up and solve a recurrence relation for the number of calls made by F (n), the recursive algorithm for computing n!. 3. Consider the following recursive algorithm for computing the sum of the first n cubes: S(n) = 13 + 23 + ... + n3 . Algorithm S(n) //Input: A positive integer n //Output: The sum of the first n cubes if n = 1 return 1 else return S(n − 1) + n ∗ n ∗ n a. Set up and solve a recurrence relation for the number of times the algorithm’s basic operation is executed. b. How does this algorithm compare with the straightforward nonrecursive algorithm for computing this function? 4. Consider the following recursive algorithm. Algorithm Q(n) //Input: A positive integer n if n = 1 return 1 else return Q(n − 1) + 2 ∗ n − 1 Tuesday, 20 January 2015 12 e. x(n) = x(n/3) + 1 for n > 1, x(1) = 1 (solve for n = 3kk ) e. x(n) = x(n/3) + 1 for n > 1, x(1) = 1 (solve for n = 3 ) Set up and solve a recurrence relation for the number of calls made by Set up and solve a recurrence relation for the number of calls made by F (n), the recursive algorithm for computing n!. F (n), the recursive algorithm for computing n!. Consider the following recursive algorithm for computing the sum of the Consider the following recursive algorithm for computing the sum of the first n cubes: S(n) = 133 + 233 + ... + n33 . first n cubes: S(n) = 1 + 2 + ... + n . Ex 2.4, Problem 3 2. 2. 3. 3. Algorithm S(n) Algorithm S(n) //Input: A positive integer n //Input: A positive integer n //Output: The sum of the first n cubes //Output: The sum of the first n cubes if n = 1 return 1 if n = 1 return 1 else return S(n − 1) + n ∗ n ∗ n else return S(n − 1) + n ∗ n ∗ n a. and solve solve aa recurrence recurrence relation relation for for the the number number of of times times the the a. Set Set up up and algorithm’s basic operation operation is is executed. executed. algorithm’s basic b. this algorithm algorithm compare compare with with the the straightforward straightforwardnonrecursive nonrecursive b. How How does does this algorithm for computing computing this this function? function? algorithm for S 1 the 4. the following following recursive recursive algorithm. algorithm. 4. Consider Consider for i 2 to n do Algorithm Q(n) S S + Q(n) i⇥i⇥i Algorithm //Input: A positive positive integer integer nn return SA //Input: if return 11 if n n= =1 1 return else Q(n − − 1) 1) + + 22 ∗∗ nn − − 11 else return return Q(n Tuesday, 20 January 2015 12 algorithm’s basic operation is executed. Ex 2.4, Problem 4(a) b. How does this algorithm compare with the straightforward nonrecursive algorithm for computing this function? 4. Consider the following recursive algorithm. Algorithm Q(n) //Input: A positive integer n if n = 1 return 1 else return Q(n − 1) + 2 ∗ n − 1 a. Set up a recurrence relation for this function’s values and solve it to determine what this algorithm computes. b. Set up a recurrence relation for the number of multiplications made by this algorithm and solve it. c. Set up a recurrence relation for the number of additions/subtractions made by this algorithm and solve it. 27 Tuesday, 20 January 2015 13 algorithm’s basic operation is executed. Ex 2.4, Problem 4(a) b. How does this algorithm compare with the straightforward nonrecursive algorithm for computing this function? 4. Consider the following recursive algorithm. Algorithm Q(n) //Input: A positive integer n if n = 1 return 1 else return Q(n − 1) + 2 ∗ n − 1 a. Set up a recurrence relation for this function’s values and solve it to determine what this algorithm computes. b. Set up a recurrence relation for the number of multiplications made by this algorithm and solve it. c. Set up a recurrence relation for the number of additions/subtractions made by this algorithm and solve it. 27 Tuesday, 20 January 2015 13 algorithm’s basic operation is executed. Ex 2.4, Problem 4(a) b. How does this algorithm compare with the straightforward nonrecursive algorithm for computing this function? 4. Consider the following recursive algorithm. Algorithm Q(n) //Input: A positive integer n if n = 1 return 1 else return Q(n − 1) + 2 ∗ n − 1 a. Set up a recurrence relation for this function’s values and solve it to determine what this algorithm computes. b. Set up a recurrence relation for the number of multiplications made by this algorithm and solve it. c. Set up a recurrence relation for the number of additions/subtractions made by this algorithm and solve it. 27 Tuesday, 20 January 2015 13 Ex 2.4, Problem 8 b. Set up a recurrence relation for the number of additions made by the nonrecursive version of this algorithm (see Section 2.3, Example 4) and solve it. 7. a. Design a recursive algorithm for computing 2n for any nonnegative integer n that is based on the formula: 2n = 2n−1 + 2n−1 . b. Set up a recurrence relation for the number of additions made by the algorithm and solve it. c. Draw a tree of recursive calls for this algorithm and count the number of calls made by the algorithm. d. Is it a good algorithm for solving this problem? 8. Consider the following recursive algorithm. Algorithm Min1 (A[0..n − 1]) //Input: An array A[0..n − 1] of real numbers if n = 1 return A[0] else temp ← Min1 (A[0..n − 2]) if temp ≤ A[n − 1] return temp else return A[n − 1] Tuesday, 20 January 2015 14 the one for the recursive version: Solution to Problem 8 A(n) = A(⌊n/2⌋) + 1 for n > 1, A(1) = 0, with the solution A(n) = ⌊log2 n⌋ + 1. 7. a. Algorithm Power (n) //Computes 2n recursively by the formula 2n = 2n−1 + 2n−1 //Input: A nonnegative integer n //Output: Returns 2n if n = 0 return 1 else return P ower(n − 1) + P ower(n − 1) We didn’t simplify! 36 15 Tuesday, 20 January 2015 the one for the recursive version: Solution to Problem 8 A(n) = A(⌊n/2⌋) + 1 for n > 1, A(1) = 0, with the solution A(n) = ⌊log2 n⌋ + 1. 7. a. Algorithm Power (n) //Computes 2n recursively by the formula 2n = 2n−1 + 2n−1 //Input: A nonnegative integer n //Output: Returns 2n if n = 0 return 1 else return P ower(n − 1) + P ower(n − 1) We didn’t simplify! b. A(n) = 2A(n − 1) + 1, A(0) = 0. A(n) = = = = = = = 36 2A(n − 1) + 1 2[2A(n − 2) + 1] + 1 = 22 A(n − 2) + 2 + 1 22 [2A(n − 3) + 1] + 2 + 1 = 23 A(n − 3) + 22 + 2 + 1 ... 2i A(n − i) + 2i−1 + 2i−2 + ... + 1 ... 2n A(0) + 2n−1 + 2n−2 + ... + 1 = 2n−1 + 2n−2 + ... + 1 = 2n − 1. c. The tree of recursive calls for this algorithm looks as follows: n Tuesday, 20 January 2015 n-2 15 n-1 n-1 n-2 n-2 n-2 (n) = 2A(n − 1) + 1, A(0) = 0. Solution to Problem 8 = = = = = = = 2A(n − 1) + 1 2[2A(n − 2) + 1] + 1 = 22 A(n − 2) + 2 + 1 22 [2A(n − 3) + 1] + 2 + 1 = 23 A(n − 3) + 22 + 2 + 1 ... 2i A(n − i) + 2i−1 + 2i−2 + ... + 1 ... 2n A(0) + 2n−1 + 2n−2 + ... + 1 = 2n−1 + 2n−2 + ... + 1 = 2n − 1. he tree of recursive calls for this algorithm looks as follows: n n-1 n-1 1 0 ... n-2 0 n-2 1 0 0 ... n-2 n-2 ... 1 0 0 0 1 0 16 that it has one extra level compared to the similar tree for the Tower Tuesday, 20 January 2015 b. Which of the algorithms Min1 or Min2 is faster? Can you suggest an algorithm for the problem they solve that would be more efficient than either of them? Ex 2.4, Problem 11 10. The determinant of an n-by-n matrix ⎡ a11 ⎢ a21 A=⎢ ⎣ an1 ⎤ a1n a2n ⎥ ⎥, ⎦ ann denoted det A, can be defined as a11 for n = 1 and, for n > 1, by the recursive formula n ' det A = sj a1j det Aj , j=1 where sj is +1 if j is odd and -1 if j is even, a1j is the element in row 1 and column j, and Aj is the (n − 1)-by-(n − 1) matrix obtained from matrix A by deleting its row 1 and column j. a.◃ Set up a recurrence relation for the number of multiplications made by the algorithm implementing this recursive definition. (Ignore multiplications by sj .) b.◃ Without solving the recurrence, what can you say about the solution’s order of growth as compared to n! ? 17 9. von Neumann neighborhood revisited Find the number of cells in the von Neumann neighborhood of range n (see Problem 11 in Exercises 2.3) by Tuesday, 20 January 2015 b. Which of the algorithms Min1 or Min2 is faster? Can you suggest an algorithm for the problem they solve that would be more efficient than either of them? Ex 2.4, Problem 11 10. The determinant of an n-by-n matrix ⎡ a11 ⎢ a21 A=⎢ ⎣ an1 ⎤ a1n a2n ⎥ ⎥, ⎦ ann denoted det A, can be defined as a11 for n = 1 and, for n > 1, by the recursive formula n ' det A = sj a1j det Aj , j=1 where sj is +1 if j is odd and -1 if j is even, a1j is the element in row 1 and column j, and Aj is the (n − 1)-by-(n − 1) matrix obtained from matrix A by deleting its row 1 and column j. a.◃ Set up a recurrence relation for the number of multiplications made by the algorithm implementing this recursive definition. (Ignore multiplications by sj .) b.◃ Without solving the recurrence, what can you say about the solution’s order of growth as compared to n! ? 17 9. von Neumann neighborhood revisited Find the number of cells in the von Neumann neighborhood of range n (see Problem 11 in Exercises 2.3) by Tuesday, 20 January 2015 my solution 18 Tuesday, 20 January 2015 my solution Let M (n) be the number of multiplications made in computing the determinant of an n ⇥ n matrix. M (1) = 0 n X M (n) = (1 + M (n j=1 1)) 8n > 1 18 Tuesday, 20 January 2015 my solution Let M (n) be the number of multiplications made in computing the determinant of an n ⇥ n matrix. M (1) = 0 n X M (n) = (1 + M (n 1)) 8n > 1 M (n) = n ⇥ (1 + M (n 1)) 8n > 1 j=1 M (n) = n + nM (n 1) 18 Tuesday, 20 January 2015 my solution Let M (n) be the number of multiplications made in computing the determinant of an n ⇥ n matrix. M (1) = 0 n X M (n) = (1 + M (n 1)) 8n > 1 M (n) = n ⇥ (1 + M (n 1)) 8n > 1 j=1 M (n) = n + nM (n 1) Without solving this relation, what can you say about M’s order of growth, compared to n! ? 18 Tuesday, 20 January 2015
© Copyright 2025