Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms.

Similar presentations


Presentation on theme: "Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms."— Presentation transcript:

1 Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms

2 Prof. Amr Goneid, AUC2 Recursive Algorithms

3 Prof. Amr Goneid, AUC3 Recursive Algorithms Modeling Recursive Algorithms Examples of Recurrences Exclude & Conquer General Solution of 1st Order Linear Recurrences 2 nd Order Linear Recurrence

4 Prof. Amr Goneid, AUC4 1. Modeling Recursive Algorithms Recursive Algorithm Model as a Recurrence Relation T(n) in terms of T(n-1) Solve to obtain T(n) as a function of n

5 Prof. Amr Goneid, AUC5 Recurrence Relations A recurrence relation is an equation which is defined in terms of itself. Many algorithms, particularly exclude & conquer and divide and conquer algorithms, have time complexities which are naturally modeled by recurrence relations. Many counting problems can be modeled as recurrences. Solving the recurrence relation solves the problem.

6 Prof. Amr Goneid, AUC6 Recurrence Relations Many natural functions are expressed as recurrences:

7 Prof. Amr Goneid, AUC7 Recurrence Relations To derive the recurrence relation: Identify the problem size (n). Find the base case size n 0 (usually n 0 = 0,1 or 2) and find T(n 0 ) The general case consists of “a” recursive calls to do smaller sub-problems (usually of size n-1) plus a cost “b” for all the work needed other than in the recursive calls.

8 Prof. Amr Goneid, AUC8 Recurrence Relations Hence, the recurrence relation will have the form: base case cost of base case no. of recursive calls cost of a recursive call cost of other operations

9 Prof. Amr Goneid, AUC9 T(0) = 0 A Recursive Factorial Function: factorial (n) { if (n == 0) return 1; else return ( factorial (n-1) * n); } Find T(n) = number of integer multiplications 2. Examples of Recurrences T(n) T(n-1) 1

10 Prof. Amr Goneid, AUC10 Examples The base case with n = 0 does zero multiplications. Hence T(0) = 0. The general case will cost T(n-1) plus one multiplication. Recurrence Relation:

11 Prof. Amr Goneid, AUC11 Examples Recurrence relations of this kind can be solved by successive substitution: Substitute several times (m) to show a pattern. Choose (m) so that each T( ) in which (m) appears reduces to T(n 0 ) Substitute for T(n 0 ) Solve the resulting equation

12 Prof. Amr Goneid, AUC12 Examples For example, for the recurrence relation of the recursive factorial:

13 Prof. Amr Goneid, AUC13 A Recursive Function to return x n : power (x, n) { if (n == 0 ) return 1.0; else return ( power(x,n-1)*x ); } Find T(n) = number of arithmetic operations T(n) = T(n-1) + 1 for n > 0 with T(0) = 0 Hence T(n) = n =  (n) Examples

14 Prof. Amr Goneid, AUC14 3. Exclude and Conquer n 1n-1 Base 1 1

15 Prof. Amr Goneid, AUC15 // Assume the data are in locations s….e of array a[0..n-1] // A problem of size (n) arises when the function is invoked as // selectsort(a,0,n-1), i.e. with s = 0 and e = n-1 Selectsort (a[ ], s, e) { if (s < e) { m = minimum (a, s, e) ; swap (a[s], a[m]); Selectsort (a,s+1,e); } } Example: Recursive Selection Sort T(s,e) T(s+1,e)

16 Prof. Amr Goneid, AUC16 Selection Sort (continued) Recurrence Relation: ( problem of size n when s = 0, e = n-1):

17 Prof. Amr Goneid, AUC17 4. General Solution of 1st Order Linear Recurrences Many recursive algorithms are modeled to the following general 1 st order linear recurrence relation: e.g. the factorial algorithm gives a n = 1 and b n = 1, T(0) given. Select sort gives a n = 1 and b n = (n-1), T(1) given.

18 Prof. Amr Goneid, AUC18 General Solution of 1st Order Linear Recurrences Such recurrence can be solved by successive substitution. e.g. for T(0) given:

19 Prof. Amr Goneid, AUC19 General Solution of 1st Order Linear Recurrences Successive substitution gives for the cases of T(0) given and T(1) given:

20 Prof. Amr Goneid, AUC20 Examples For the recursive factorial function, a n =1 so that a i =1 and similarly b i =1 with T(0)=0. Substitution in the general solution gives: hence T(n) = n as before. For the selection sort algorithm, a i =1, b i = (i-1) with T(1) = 0 so that

21 Prof. Amr Goneid, AUC21 Examples T(n) = 2 T(n-1) + 1 with T(0) = 0 T(n) = n T(n-1) with T(0) = 1

22 Prof. Amr Goneid, AUC22 Exercise(1): Exclude & Conquer with a Linear Process int FOO (int a[ ], int n) { if (n > 0) { int m = 5 * Process (a, n); return m * FOO(a,n-1); } Show that the number of integer multiplications if Process makes 2n such operations is:

23 Prof. Amr Goneid, AUC23 Solution

24 Prof. Amr Goneid, AUC24 Exercise(2): History Problem double Eval ( int n ) { int k ; double sum = 0.0; if (n == 0) return 1.0 ; else { for (k = 0; k < n; k++) sum += Eval (k); return 2.0 * sum / n + n ; } } Show that the number of double arithmetic operations performed as a function of (n) is:

25 Prof. Amr Goneid, AUC25 Solution

26 Prof. Amr Goneid, AUC26 Solution

27 Prof. Amr Goneid, AUC27 Exercise (3): Hanoi Towers Game Find the number of moves required for a Tower of Hanoi with n discs. An animation is available at: http://www.cosc.canterbury.ac.nz/people/mukundan/dsal /ToHdb.html

28 Prof. Amr Goneid, AUC28 5. 2 nd Order Linear Recurrence Example: Fibonacci Sequence F(n) = F(n-1) + F(n-2), F(0) = 0 F(1) = 1


Download ppt "Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms."

Similar presentations


Ads by Google