Week 4
Week 4
ALGORITHMS
Course Code : AR212
Credit Hours :3
Prerequisite : AR211
Instructor Information
[Pre-req. AR211
Textbook: Introduction to Algorithms, Thomas Cormen, Charls Leiserson & Ronald Rivest. 3rd Edition,
MIT Press, 2009.
By:
Dr. Aryaf Al-Adwan
Autonomous Systems Dept.
Design and Analysis of algorithms Course
8/28
Introduction toRecursion
❑ Recursive procedures are functions that invoke themselves
directly (call themselves from within themselves)
❑ Recursion:
. An alternative to iteration
. Recursion can be very elegant at times,
. Not inexpensive to implement...
Classic examples of recursion
. Recursive calculation of a string length, factorials, divide and
conquer, towers of Hanoi, binary searches, and more
9/28
Example: Add the numbers from 1 to 5
Base case
int sum (int n)
{
if (n <= 1)
return n;
else
return (n + sum(n-1)); Recursive Case
} // end sum()
10/2
8
int public sum (int n)
{
if (n <= 1)
return n;
else
return (n + sum(n-1));
}// end sum()
START WITH N = 5:
SUM(1): 1
11/2
8
WHEN WE GET SUM(1)=1,
THEN WE REFER TO 2 + SUM(1) equals 2 + 1 = 3. (complete)
12/2
8
Consider: FACTORIAL FUNCTION
Thus 5! = 5 * 4 * 3 * 2 * 1 = 120
13/2
8
Factorial
14/2
8
int public factorial (int n)
Factorial {
if (n==1)
return (1) /* base case */
else
main calls argument n = 7. return (n * factorial (n-1) );
factorial(7) /* recursive case
value of n at this node: returned value }// end factorial()
n=7
return (7*factorial(6)) return(7*720) = 5040 = answer!!
recursive call
n=6
return(6*factorial(5)) return(6*120) = 720
n=5
return(5*factorial(4)) return(5*24) = 120
n=4
return (4*factorial(3)) return(4*6) = 24
n=3
return (3*factorial(2)) return (3 *2) = 6
n=2
return (2*factorial(1)) return( 2 * factorial(1)) = 2 * 1 = 2 Thus factorial (2) = 2. return 2.
15/2
8
Recursive Definitions: FibonacciSeries
• fib1 = 1
• fib2 = 1
Chapter 7: Recursion 16
Fibonacci SeriesCode
Chapter 7: Recursion 17
11/28
Data Structure in Recursion
❑ Stacks are Used
12/28
Efficiency ofRecursion
Chapter 7: Recursion 13
Recurrences
21/28
Definition
22/28
Solving arecurrence
23/28
IterationMethod
Example1
• Assume we have the following recursive function
24/28
FindingtheTotaltimebythisalgorithm
26/28
Solving the recurrence (usingiteration)
27/28
Cont.
• T (n-k) + k
• This means that I will stay in the recursive state and stop after k steps
• So, when is the stop case?
when n=0
n-k = 0 , so n=k
• T(n)= T(n-k) + k , but n=k
• T(n)= T(n-n) +n
• T(n) = T(0) + n , but T(0)=1
• T(n)= 1+n
= n+1 ➔ O(n)
28/28
IterationMethod
Example2
• Assume we have the following recursive function
29/28
Finding the Total time by this algorithm
31/28
Solving the recurrence (byiteration)
32/28
Cont.
33/28
Solving the recurrence byiteration
Example3
34/28
Solving the recurrence byiteration
Example3
T(n) = 2T(n/2) + 4n ……….. Eq1
Find T(n/2) ?
T(n/2)=2T(n/22)+4(n/2)
So, Substitute T(n/2) in Eq1
T(n)=2 [2T(n/22)+4(n/2)] +4n ➔ T(n)=22 T(n/ 22 )+4n+ 4n ……… Eq2
Find T(n/22) ?
T(n/ 22)=2T(n/23)+4(n/ 22)
So, Substitute T(n/ 22) in Eq2
T(n)= 22 [2T(n/23)+4(n/ 22)]+4n+ 4n ➔ T(n)=23 T(n/ 23 )+4n+ 4n+4n
Continues for k times ➔ 2k T(n/ 2k )+k (4n)
35/28
Cont.
36/28
Solving the recurrence using MasterTheorem
37/28
GenericForm
a>=1
b>1
f(n)=O(nk log pn)
where:
• n is the size of an input problem.
• a is the number of subproblems in the recursion
• n/b is the size of each subproblem
• f (n) is the cost of the work done outside the recursive calls, which
includes the cost of dividing the problem and the cost of merging the
solutions to the subproblems.
We have to find :
1) log ba
2) k 38/28
Cont.
39/28
Case 1Example
40/28
Another example onCase1
41/28
Case 2Example
42/28
Another example onCase 2
43/28
Case 3Example
44/28
End
45/28