0% found this document useful (0 votes)
5 views

Lab 4

The document outlines the analysis of recursive code in algorithms, covering key concepts such as recursion, base cases, and methods for analyzing running time including the substitution method and master method. It provides examples of calculating recurrence relations for factorial, Fibonacci, and binary search algorithms. The master method is discussed in detail, including its applicable forms and cases for solving recurrences.

Uploaded by

laptop2112006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab 4

The document outlines the analysis of recursive code in algorithms, covering key concepts such as recursion, base cases, and methods for analyzing running time including the substitution method and master method. It provides examples of calculating recurrence relations for factorial, Fibonacci, and binary search algorithms. The master method is discussed in detail, including its applicable forms and cases for solving recurrences.

Uploaded by

laptop2112006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Theory of Algorithms

Lab 4
AGENDA
• Analysis: Recursive Code
• Recursion Recap
• How to Analyze Recursive Code?
• Substitution method
• Master Method

Theory of algorithms Lab4


Recursion
Recap
Function calls itself for one or more times
Consists of two parts:
- Recursive part
- Base case (stopping condition)
Fun(…)
General form: ….
Base Case
….
Fun(…), Fun(…), …etc Recursive Code
Normal Code
….
….
Theory of algorithms Lab4
How to Analyze Recursive
Code?
1. Calculate Running Time T(N) [Recurrence]
T(N) = Time of Normal Code + T(Recursive Code) + … etc
T(Base) = Time of Base Case

2. Solve it to Get the Order ( or O)


1. Substitution method
2. Recursion-tree method
3. Master method

Theory of algorithms Lab4


1. Calculate Recurrence
Relation T(N)
Example1: Factorial
Fact(N)
If (N == 0)
return 1
else
return N × Fact(N-1)

Theory of algorithms Lab4


1. Calculate Recurrence
Relation T(N)
Example1: Factorial
Fact(N)
Base Case If (N == 0)
return 1
Normal Code
else
return N × Fact(N-1) Recursive Code

T(N) = (1) + T(N – 1)


T(0) = (1)
Theory of algorithms Lab4
1. Calculate Recurrence
Relation T(N)
Example2: Fibonacci
Fib(N)
If (N == 1 or N == 2)
return 1
else
return Fib(N-1) + Fib(N-2)
Theory of algorithms Lab4
1. Calculate Recurrence
Relation T(N)
Example2: Fibonacci
Fib(N)
Base Case If (N == 1 or N == 2)
return 1
Normal Code
else
return Fib(N-1) + Fib(N-2) Recursive Code

T(N) = (1) + T(N – 1) + T(N – 2)


T(1) = (1)
Theory of algorithms Lab4
1. Calculate Recurrence
Relation T(N)
Example3: Binary Search
BinSrch(A, I, S, E)
If (S > E)
return False
M = (S + E) / 2
If (I == A[M]) return true
else If (I > A[M])
return BinSrch(A, I, M+1, E)
else If (I < A[M])
return BinSrch(A, I, S, M-1)
Theory of algorithms Lab4
1. Calculate Recurrence
Relation T(N)
Example3: Binary Search
BinSrch(A, I, S, E) T(N) = (1) + T(N / 2)
If (S > E)
Base Case return False T(1) = (1)
M = (S + E) / 2
If (I == A[M]) return true
Normal Code else If (I > A[M])
return BinSrch(A, I, M+1, E) Recursive Code
else If (I < A[M]) OR
return BinSrch(A, I, S, M-1) Recursive Code

Theory of algorithms Lab4


How to Analyze Recursive
Code?
1. Calculate Running Time T(N) [Recurrence]
T(N) = Time of Normal Code + T(Recursive Code) + … etc
T(Base) = Time of Base Case

2. Solve it to Get the Order ( or O)


1. Substitution Method
2. Recursion-tree Method
3. Master Method

Theory of algorithms Lab4


Substitution
Method
The Substitution Method is a powerful technique for solving recurrence
relations by guessing a solution and proving it correct using mathematical
induction. This method helps us find the time complexity of recursive
algorithms.

Theory of algorithms Lab4


Substitution
Method
Example:
Solution:
step 1
step 2
step 3
step k
T(1) will be reached when n = 2k ➔ k =

Theory of algorithms Lab4


Master
Method
Suitable ONLY for recurrences of the following form:

T(N) = 2 T(N/2) + cN2 T


T(N) = T(N/3) + O(1) T
T(N) = 2 T(3N/2) + √N F (b = 2/3 < 1)
T(N) = T(N-1) + T(N-2) + c F (not on the form)
Theory of algorithms Lab4
Master
Method
log 𝒃 𝒂
𝒇 ( 𝑵 ) 𝒗𝒔 𝑵
Theory of algorithms Lab4
Master
Method
Case 1: If :
-
Case 2: If :
-
Case 3: If : ,.
AND
-
Theory of algorithms Lab4
Master
Method
 

  n 
log b a
f (n) O n 
log b a   


 
   0
T (n)  n log b a
log n  f (n)  n 
log b a

  c 1
 
 f (n)  f (n)  n 
log b a 
AND  
 
 af (n / b)  cf (n) for large n
Theory of algorithms Lab4
Master
Method
Example 1:
Solution:

Since where =0.2, case 3 applies:


Check the condition:

Thus the solution is


Theory of algorithms Lab4
Master
Method
Example 2:
Solution:

CASE 2: f (n) = Q(n2lg0n), that is, k = 0.


 T(n) = Q(n2lg n).

Theory of algorithms Lab4

You might also like