Mathematical Analysis of Recursive Algorithm CSG3F3 Lecture 7.

Slides:



Advertisements
Similar presentations
CS3024-FAZ1 Mathematical Analysis of Recursive Algorithms Design and Analysis of Algorithms (CS3024) 28/02/2006.
Advertisements

Introduction to Algorithms
1 Recursive Algorithm Analysis Dr. Ying Lu RAIK 283: Data Structures & Algorithms September 13, 2012.
Appendix B Solving Recurrence Equations : With Applications to Analysis of Recursive Algorithms.
1 Copyright M.R.K. Krishna Rao Solving Recurrence Relations Steps for solving a linear homogeneous recurrence relation of degree 2 : Step #1.
Recurrence Relations Reading Material –Chapter 2 as a whole, but in particular Section 2.8 –Chapter 4 from Cormen’s Book.
Recursion Sections 7.1 and 7.2 of Rosen Fall 2008 CSCE 235 Introduction to Discrete Structures Course web-page: cse.unl.edu/~cse235 Questions:
Discrete Structures Chapter 6 Recurrence Relations
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
Analysis of Recursive Algorithms
Recurrence Relations Reading Material –Chapter 2 as a whole, but in particular Section 2.8 –Chapter 4 from Cormen’s Book.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms.
Solving Linear Equations
6.Advanced Counting Techniques 1 Copyright M.R.K. Krishna Rao 2003 Ch 6. Recurrence Relations A recurrence relation for the sequence {a n } is an equation.
The Rational Zero Theorem
Lecture 6 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Applied Discrete Mathematics Week 9: Relations
Advanced Counting Techniques
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
The Rational Zero Theorem The Rational Zero Theorem gives a list of possible rational zeros of a polynomial function. Equivalently, the theorem gives all.
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Recurrence Relation. Outline  What is a recurrence relation ?  Solving linear recurrence relations  Divide-and-conquer algorithms and recurrence relations.
Module #17: Recurrence Relations Rosen 5 th ed., §
RECURRENCE Sequence Recursively defined sequence
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Recurrence.
Chapter 8 With Question/Answer Animations. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Solving Equations. The equations are equivalent If they have the same solution(s)
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Recursion Algorithm : Design & Analysis [3]. In the last class… Asymptotic growth rate The Sets ,  and  Complexity Class An Example: Maximum Subsequence.
7.2 Solving Linear Recurrence Relations Some of these recurrence relations can be solved using iteration or some other ad hoc technique. However, one important.
Asymptotics and Recurrence Equations Prepared by John Reif, Ph.D. Analysis of Algorithms.
CSE 2813 Discrete Structures Solving Recurrence Relations Section 6.2.
Agenda Lecture Content:  Recurrence Relations  Solving Recurrence Relations  Iteration  Linear homogenous recurrence relation of order k with constant.
Recurrence Relations. Outline Recurrence relationsSolving recurrence relationsRecurrence and Divide-and-conquer algorithmsGenerating functions
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
RECURRENCE Sequence Recursively defined sequence
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
1 RECURRENCE 1. Sequence 2. Recursively defined sequence 3. Finding an explicit formula for recurrence relation.
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Advanced Counting Techniques
1 Lecture Outline for Recurrences Already Covered: Recursive definition of sequences Recursive definition of sets Recursive definition of operations Recursive.
CSG523/ Desain dan Analisis Algoritma
CSG523/ Desain dan Analisis Algoritma
CMSC Discrete Structures
Theoretical analysis of time efficiency
Analysis of Algorithms
Modeling with Recurrence Relations
Introduction to the Design and Analysis of Algorithms
3.5 Recurrence Relations.
Introduction to Recurrence Relations
UNIT-6 Recurrence Relations
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
Module #17: Recurrence Relations
Module #17: Recurrence Relations
CS 2210 Discrete Structures Advanced Counting
CMSC Discrete Structures
Solving Recurrence Relations
CMSC Discrete Structures
At the end of this session, learner will be able to:
Recurrence Relations Discrete Structures.
Fundamentals of the Analysis of Algorithm Efficiency
Recurrence Relations Rosen 5th ed., §6.2 5/22/2019
ICS 253: Discrete Structures I
Presentation transcript:

Mathematical Analysis of Recursive Algorithm CSG3F3 Lecture 7

2 Set up a recurrence relations and initial condition

3 Example 1 ALGORITHM F(n) //computes n! recursively //input: a nonnegative integer n //output: the value of n! if n=0 return 1 else return F(n-1)*n

4 Recurrence relation and initial condition for the algorithm’s number of multiplication M(n): M(n)=M(n-1)+1for n>0, M(0)=0

5 M(n)=M(n-1)+1substitute M(n-1)=M(n-2)+1 =[M(n-2)+1]+1=M(n-2)+2 substitute M(n-2)=M(n-3)+1 =[M(n-3)+1]+2=M(n-3)+3 General formula: M(n)=M(n-i)+i

6 For n=0, M(n)=M(n-1)+1=…=M(n-i)+i=… =M(n-n)+n=n

7 Techniques for solving recurrence Substitution Characteristic equation Induction

8 Solving Recurrences by Substitution Recurrence: Work backward:

9 Substitution (cont.) Substitution:

Problem 1 10

11 Problem 2 ALGORITHM Power(x, n) // Computes x to the power of n. // Input: x is a real number, n an integer >= 0 // Output: Returns x to the power of n. if n=0 return 1 else return x * Power(x, n-­1)

12 Problem 2 (cont.) Analysis Basic operation: multiplication M(n) = 1 + M(n-­1): recurrence relation M(0) = 0 : initial condition

13 Problem 2 (cont.) Solving the recurrence relation M(n) = 1 + M(n-­1) = 1 + [M(n-­2) + 1 ] = 2 + M(n -2) = 2 + [M(n -3) + 1 ] = 3 + M(n -3) = : = k + M(n-­k)

14 Problem 2 (cont.) M(n) = k + M(n-­k) = n + M(n-n) = n + M(0) = n + 0 = n Order of growth M(n)  O(n)

15 General Plan 1.Decide on parameter n indicating input size 2.Identify algorithm’s basic operation 3.Determine worst, best, and average case for input of size n 4.Set up a recurrence relation and initial condition for C(n)-the number of times the basic operation will be executed for an input of size n (alternatively count recursive calls). 5.Solve the recurrence to obtain a closed form or estimate the order of magnitude of the solution

16 Problem 2: Binary Recursive Problem: investigate a recursive version of binary algorithm, which finds the number of binary digits in the binary representation of a positive decimal integer n. Recursive relation for the number of addition made by the algorithm. A(n)=A[n/2] +1for n>1 A(1)=0initial condition for n =1, no addition is made because no recursive call is executed.

17 Problem 3: Binary Recursive Algorithm BinRec(n) //input: a positive decimal integer n //output: the number of binary digits in n’s binary representation If n=1 return 1 Else return BinRec([n/2])+1

18 A(2 k ) = A(2 k-1 ) + 1 = [A(2 k-2 ) + 1] + 1 = A(2 k-2 ) + 2 = [A(2 k-3 ) + 1] + 2 = A(2 k-3 ) + 3 …… = A(2 k-i ) + i …… = A(2 k-k ) + k = k A(2 0 ) = A(1) = 0 = log 2 n n = 2 k A(n) = log 2 n  O(log n)

19 Exercises Set up and solve a recurrence relation for the number of times to the following algorithms

20 Tower of Hanoi ALGORITHM Move(n, first, temp, last) // Input: a positive integer n // first, temp, and last are tower numbers // Output: a list of moves for n disks from fir st to // last using temp as auxiliary. If n = 1 output "Move 1 disk from" first "to" last else Move(n-­1, first, last, temp) output "Move 1 disk from" first "to" last Move(n-­1, temp, first, last)

21 T(n)=2T(n/2)+n-1n >1, n a power of 2 T(1)=0 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

22 Characteristic Equation Homogeneous Linear Recurrences k and a i are constant It’s called “linear” because every term t i appears only to the first power It’s called “homogeneous” because the linear combination is equal to 0.

23 Characteristic (cont.) The following are homogeneous linear recurrence equations

24 Characteristic (cont.) The Fibonacci Sequence is defined as follows: Subtract t n-1 and t n-2 from both sides, we get homogeneous linear recurrence equation

25 Characteristic (cont.) Definition

26 Characteristic (cont.) Example

27 Characteristic (cont.) Theorem. Let the homogeneous linear recurrence equation with constant coefficients be given. If its characteristic equation has k distinct solutions r 1, r 2,…,r k, then the only solutions to the recurrence are

28 Characteristic (cont.) Consider the following recurrence 1.Obtain the characteristic equation

29 Characteristic (cont.) 2.Solve the characteristic equation 3.Apply the theorem to get the general solution: 4.Determine the values of the constants

30 Characteristic (cont.) The solution to this system is c 1 =1/5, c 2 =-1/5 5.Substitute the constant into the general solution to obtain the particular solution:

Exercise Fibonacci Sequence 31

32 Multiplicity root Theorem. Let r be a root of multiplicity m of the characteristic equation for a homogeneous linear recurrence with constant coefficients. Then are all solutions to the recurrence.

Exercises 33

34 Characteristic (cont.) Nonhomogeneous Linear Recurrences k and a i are constant, f(n) is a function other than the zero function. Develop a method for solving the common case where b is a constant and p(n) is a polynomial in n.

35 Characteristic (cont.) Theorem. A nonhomogeneous linear recurrence of the form can be transformed into homogeneous linear recurrence where d is the degree of p(n).

Characteristic (cont.)

Characteristic (cont.) 37 Cont’d a. Replace n with n-1 in the original recurrence so that the recurrence is expressed with 4 n-1 on the right: b. Divide the original recurrence by 4 so that the recurrence is expressed in another way with 4 n-1 on the right: c. To get rid of the term 4 n-1, subtracting the recurrence a) from b): Multiply by 4 to get rid of the fractions

38 Characteristic (cont.) Solve the following recurrence 1.Obtain the characteristic eq for the corresponding homogeneous eq:

39 Characteristic (cont.) 2.Obtain a term from the nonhomogeneous part of the recurrence the term is

40 Characteristic (cont.) 3.Apply theorem to obtain the characteristic eq. 4.Solve the characteristic equation the roots are r=3 and r=4, and the root r=4 has multiplicity 2

41 Characteristic (cont.) 5.Apply theorem to get the general solution We must find another initial condition. Because and t 1 =12,

42 Characteristic (cont.) 6.Determine the value of the constants 7.Substitute the constants into the general solution to obtain

Exercises 43

44 Change of variables The recurrence relation:

45 Change of variables 1.Set n=2 k, which means k= 2 log n 2.Substitute 2 k for n to obtain

46 Change of variables 3.Next set: t k = T(2 k ) in 2) to obtain the new recurrence t k = t k General solution t k = c 1 + c 2 k

47 Change of variables 5.Substitute T(2 k ) for t k in the general solution: T(2 k ) = c 1 + c 2 k 6.Substitute n for 2 k and lg n for k : T(n) = c 1 + c 2 lg n

48 Change of variables T(n) = 1 + lg n

Exercises 49

50 Neapolitan, Richard. Kumarss Naimipour. Foundations of algorithms. D.C Heath and Company. 1996

51 Next.. Brute Force Technique