5-Stages of Algorithm Development & Time Complexity Analysis-05-01-2024
5-Stages of Algorithm Development & Time Complexity Analysis-05-01-2024
Analysis of Algorithms
Course Objective
Text Books:
1. Thomas H. Cormen, C.E. Leiserson, R L.
Rivest and C. Stein, Introduction to
Algorithms, 2009, 3rd Edition, MIT Press.
Reference Books:
1. Jon Kleinberg, ÉvaTardos ,Algorithm Design,
Pearson education, 2014.
2. Rajeev Motwani, Prabhakar Raghavan;
Randomized Algorithms, Cambridge
University Press,1995 (Online Print – 2013)
3. Ravindra K.Ahuja, Thomas L.Magnanti and
James B.Orlin, “ Network Flows: Theory,
Algorithms and Applications”, Pearson
Education, 2014.
Module 1: Design Paradigms: Greedy, Divide
and Conquer Techniques(6 Hours)
Overview and Importance of Algorithms
◦ Stages of algorithm development:
Describing the problem
Identifying a suitable technique
Design of an algorithm
Derive Time Complexity,
Proof of Correctness of the algorithm
Illustration of Design Stages
◦ Greedy techniques:
Fractional Knapsack Problem
Huffman coding
◦ Divide and Conquer:
Maximum Subarray
Karatsuba faster integer multiplication
algorithm.
Time Complexity
The amount of time taken by an
algorithm to run to completion
It is measured by number of primitive
operations required to produce output.
Space complexity
The amount of space required by an
algorithm to run to completion.
Mostly, time and space complexity is
computed as a function of input size
Frequency Count/Step Count
Method
Number of times a statement is executed.
RULES:
◦ For comments, declarations, STEP COUNT=0
◦ For return, assignment statements, STEP COUNT=1
◦ Ignore lower order exponents when higher order
exponents are present.
◦ Ignore constant multipliers
Example: Sum of N Numbers(Iterative )
Algorithm Step Count
--
Alg sum(a[],n)
--
{
1
sum=0;
N+1
for(i=1 to n) do
N
sum=sum+a[i];
1
return sum;
}
2N+3
TOTAL
O(N)
Example: Sum of N Numbers( Recursive)
{ -- --
if(n<=0) then 1 1
return 0; 1 --
} --
TOTAL 2 2+x
X=tRsum(n-1)
Recurrences equation
tRsum(n)= 2 if n=0
2+tRsum(n-1) if n>0
= 2+tRsum(n-1)
= 2+2+tRsum(n-2)
= 2+2+2+tRsum(n-3)
= n(2)+tRsum(0)
= 2n+2 = O(n)
Example: Sum of N Numbers(Space Complexity)
Algorithm
Alg sum(a[],n) A[]– n words
{ n=one word
sum=0; Sum=one word
for(i=1 to n) do i= one word
sum=sum+a[i];
return sum;
}
TOTAL N+3
O(n)
Recurrences and Running Time
} T(n) = C+T(n/2)
= K.C + T(1)
= C.logn +1
Note: Where
n/2k = 1 : Place not
divided further
= Θ(log n) so, n = 2k
Iteration Method - Example
T(n) = n + 2T(n/2)
T(n) = n + 2T (n/2)
= n + 2(n/2 + 2T(n/4))where T(n/2) = n/2 + 2T(n/4)
= n + n + 4T(n/4)
= n + n + 4(n/4 + 2T(n/8))
= n + n + n + 8T(n/8)
Pattern is repeated for i times
… = i(n) + 2i T(n/2i )
= i(n) + 2i T(1)
= nlogn + nT(1)
= Θ(nlogn)
Master Method
Master Theorem is the easiest way of
obtaining runtime of recursive algorithms.
General formula for the Master Theorem
is: Any other
instructions/
operation time
Number of taken
times done
Division halves
Where a ≥ 1, b ≥ 1 and f(n)= Θ(nklogp n) ,K ≥ 1
Once, we have a, b and f(n) we can
determine the runtime of the work done
by the recursion.
It is given by
3 CASES
CASE 1: Logb a > k T(n)= Θ(n logb a )
CASE 2: Logb a = k
◦ If p>-1, T(n)= Θ(nklogp+1 n)
◦ If p= -1, T(n)= Θ(nklog(log n))
◦ If p<-1, T(n)= Θ(nk)
CASE 3: Logb a < k
◦ If p>=0, T(n)= Θ(nklogp n)
◦ If p<0, T(n)= Θ(nk)
Solve the recurrence
T(n)= 16T(n/4)+n3
CASE 1: Logb a > k 1. a=16, b=4, f(n)= n3
T(n)= Θ(n logb a ) 2. Find the values of k and p
CASE 2: Logb a = k 3. f(n)= Θ(nklogp n)
4. In the problem f(n)= n3
If p>-1, T(n)= Θ(nklogp+1 n) Compute and find k and p
If p= -1, T(n)= Θ(nklog(log 5. k=3, p=0
n)) 6. Find Logb a = Log4 16 = 2
If p<-1, T(n)= Θ(nk) 7. Compare Logb a with k
CASE 3: Logb a < k Here it (2<3) falls on case
3
If p>=0, T(n)= Θ(nklogp n)
8. Check with p value it is >=0
If p< 0T(n)= Θ(nk) 9. Θ(n3log0 n)
10. Θ(n3)
T(n)= 4T(n/2)+n2 log n
Try yourself….
T(n) = T(n-1) + n ---- Θ(n2 )
Recursive algorithm that loops through the input to
eliminate one item- Bubble sort using Recursion
Algorithm Bubble sort(A,N)
{
if (n==1) 1
return;
20 -15 25 -8 80 14 51 27
1 3 Max Min 4 5 Max Min 6 7 Max Min 8 9 Max Min
3 7
4 6