280 likes | 297 Views
This course covers recursive algorithms, modeling recursive algorithms, examples of recurrences, exclude & conquer, general solution of 1st order linear recurrences, and more.
E N D
Analysis & Design of Algorithms(CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms Prof. Amr Goneid, AUC
Recursive Algorithms Prof. Amr Goneid, AUC
Recursive Algorithms • Modeling Recursive Algorithms • Examples of Recurrences • Exclude & Conquer • General Solution of 1st Order Linear Recurrences • 2nd Order Linear Recurrence Prof. Amr Goneid, AUC
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 Prof. Amr Goneid, AUC
Recurrence Relations • A recurrence relation is an equation describing a function in terms of its value for smaller instances. • 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. Prof. Amr Goneid, AUC
Recurrence Relations • Many natural functions are expressed as recurrences: Prof. Amr Goneid, AUC
Recurrence Relations To derive the recurrence relation: • Identify the problem size (n). • Find the base case size n0 (usually n0 = 0 ,1 or 2) and find T(n0) • 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. Prof. Amr Goneid, AUC
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 Prof. Amr Goneid, AUC
2. Examples of Recurrences • A Recursive Factorial Function: factorial (n) { if (n == 0) return 1; else return ( factorial (n-1) * n); } Find T(n) = number of integer multiplications T(n) T(0) = 0 T(n-1) 1 Prof. Amr Goneid, AUC
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: Prof. Amr Goneid, AUC
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(n0) • Substitute for T(n0) • Solve the resulting equation Prof. Amr Goneid, AUC
Examples For example, for the recurrence relation of the recursive factorial: Prof. Amr Goneid, AUC
Examples • A Recursive Function to return xn: 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) Prof. Amr Goneid, AUC
3. Exclude and Conquer n 1 n-1 1 1 Base Prof. Amr Goneid, AUC
Example: Recursive Selection Sort // 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); } } T(s,e) T(s+1,e) Prof. Amr Goneid, AUC
Selection Sort (continued) Recurrence Relation: ( problem of size n when s = 0 , e = n-1): Prof. Amr Goneid, AUC
4. General Solution of 1st Order Linear Recurrences Many recursive algorithms are modeled to the following general 1st order linear recurrence relation: e.g. the factorial algorithm gives an= 1 and bn = 1, T(0) given. Select sort gives an= 1 and bn = (n-1), T(1) given. Prof. Amr Goneid, AUC
General Solution of 1st Order Linear Recurrences Such recurrence can be solved by successive substitution. e.g. for T(0) given: Prof. Amr Goneid, AUC
General Solution of 1st Order Linear Recurrences Successive substitution gives for the cases of T(0) given and T(1) given: Prof. Amr Goneid, AUC
Examples • For the recursive factorial function, an=1 so that ai=1 and similarly bi=1 with T(0)=0. Substitution in the general solution gives: hence T(n) = n as before. • For the selection sort algorithm, ai=1, bi= (i-1) with T(1) = 0 so that Prof. Amr Goneid, AUC
Examples • T(n) = 2 T(n-1) + 1 with T(0) = 0 • T(n) = n T(n-1) with T(0) = 1 Prof. Amr Goneid, AUC
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: Prof. Amr Goneid, AUC
Solution Prof. Amr Goneid, AUC
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: Prof. Amr Goneid, AUC
Solution Prof. Amr Goneid, AUC
Solution Prof. Amr Goneid, AUC
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 Prof. Amr Goneid, AUC
5. 2nd Order Linear Recurrence Example: Fibonacci Sequence F(n) = F(n-1) + F(n-2) , F(0) = 0 F(1) = 1 Prof. Amr Goneid, AUC