Algorithm Analysis Module 1 Important Topics
Algorithm Analysis Module 1 Important Topics
Topics
For more notes visit
https://rtpnotes.vercel.app
Algorithm-Analysis-Module-1-Important-Topics
1. Methods to solve Recurrence Equation
2. Iteration Method
Example 1
Example 2
Example 3
3. Recursion Tree method
Example Problem
Tree for Equation 1
Tree for Equation 2
Tree for Equation 3
Merging the 3 trees together
4. Substitution Method
How to do the substitution method?
Example
Step 1: Guess the form of the answer
Step 2
Step 3
Step 4
Step 5
5. Master theorem
What is Masters Theorem?
Statement
Master theorem Example 1
Master theorem Example 2
Master theorem Example 3
6. Asymptotic Notations
Theta Notation
Definition
Graph
Problems based on Theta Notations
Problem 1
Problem 2
Problem 3
Problem 4
Big Oh Notation
Definition
Graph
Problems based on Big Oh Notations
Problem 1
Problem 2
Big Omega
Definition
Problems based on Big Omega
Problem 1
Little oh notation
Definition
Problems based on Little oh notation
Problem 1
Problem 2
Insertion sort
Algorithm
Total time taken for execution of insertion sort
Best case analysis of insertion sort
Worst case
Average case
2. Iteration Method
Example 1
T (n) = 2 + [1 + T (n − 3)] = 3 + T (n − 3)
Lets assume n − k = 1, k = n - 1
T (n) = n − 1 + T (1)
Example 2
Repeating k times
Subbing these values, we get
Example 3
No of divisions = 2
Size of division = n/4
Cost = Cn/2
Input size = n/2
No of divisions = 2
Size of division = n/8
Cost = cn/4
4. Substitution Method
How to do the substitution method?
Example
= (logn + 1) x cn
= cnlogn + cn
= O(n logn)
Step 2
Step 3
From the above guess, write the form of equation for T(n/2)
Step 4
Substitute the previous equation into equation 1 (Step 1)
Equation 1
Equation 2
Subbing 2 in 1
Ignoring 2
Step 5
Statement
Where n / b is either a floor or ceil operation
1. Case 1
1.
2. Case 2
1.
3. Case 3
1.
2.
n
log
b
a
=n log
3
9
=n 2
Definition
Graph
Problems based on Theta Notations
Problem 1
Problem 2
Problem 3
Problem 4
1. Get the C1 value
1.
2. Get the C2 Value
1. Dont Consider the 20n^2
2.
3.
Big Oh Notation
Definition
Graph
Problem 1
3n^2 ∈ O(n^2)
Problem 2
Big Omega
Ω(g) = {f | f is non negative function, there exists positive constant c1 and n0, where
C1g(gn)<=f(n)
}
Definition
Problem 1
Little oh notation
Here the upperbound is not tight as in Big Oh
In Big Oh Notiation the upperbound is
Definition
Problem 1
Let f(n) = 5n
Let g(n) = n^2
Problem 2
Definition
Problem 1
Apply the formula, And we will get infinity, which is what we need.
Example
Algorithm mAdd(m,n,a,b,c)
{
for i = 1 to m do
for j = 1 to n do
c[i,j] := a[i,j] + b[i,j]
}
Space Complexity = Space for parameters and space for local variables
m -> 1
n -> 1
a[] -> mn
b[] -> mn
c[] -> mn
i -> 1
j -> 1
Here the space complexity = 3mn + 4
Time Complexity
The time complexity of an algorithm is the amount of computer time it needs to run to
completion
Compilation time is excluded
8. Analysis of algorithms
Linear Search
Algorithm
Note
1. n<-- length(A) -- 1
2. for i <-- 1 to n do -- n+1
1. if A[i] = key then -- n
1. found, break -- 1
3. end for -- 1
Best input
Insertion sort
Algorithm
2 for j = 2 to n do C2
n
3 key = A[j] C3
n − 1
4 i = j-1 C4
n − 1
∑ tj
j=2
6 A[i+1] = A[i] C6 n
∑ tj − 1
j=2
7 i = i-1 C7
n
∑ tj − 1
j=2
8 A[i+1] = key C8
n − 1
Number Code Cost Count
9 endof
j=2
tj + C6 ∑ n
j=2
tj − 1 + C7 ∑ n
j=2
tj − 1
+ C8(n-1)
Taking n as common
= n(C2+C3+C4+C8) + C5 ∑ + (C6+C7) ∑ + (C1-C3-C4-C8)
n n
j=2
tj j=2
tj − 1
Best case occurs when the input list given is sorted in ascended order
For each iteration of the for loop in step 2 observe that while statement in step 5 does not
hold true and statements 6 and 7 wont be executing
T(n) = an+b
This is a linear polynomial
Worst case
Average case
∑
n
j=2
t j /2 = 1/2 x (n(n+1)/2 - 1)
= 1/2 x (n(n+1)/2)
n
∑ j=2 t j /2 − 1