Week 02 - Algorithm Complexity - Design Analysis of Algorithm
Week 02 - Algorithm Complexity - Design Analysis of Algorithm
Week – 2
Algorithms Complexity
A sequence of operations:
Total Cost = c1 + c2
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
The Execution Time of Algorithms (cont.)
Cost Times
i = 1; c1 1
sum = 0; c2 1
while (i <= n) { c3 n+1
i = i + 1; c4 n
sum = sum + i; c5 n
}
Total Cost = c1 + c2 + (n+1)*c3 + n*c4 + n*c5
The time required for this algorithm is proportional to n
Cost Times
i=1; c1 1
sum = 0; c2 1
while (i <= n) { c3 n+1
j=1; c4 n
while (j <= n) { c5 n*(n+1)
sum = sum + i; c6 n*n
j = j + 1; c7 n*n
}
i = i +1; c8 n
}
Total Cost = c1 + c2 + (n+1)*c3 + n*c4 + n*(n+1)*c5+n*n*c6+n*n*c7+n*c8
The time required for this algorithm is proportional to n2
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
General Rules for Estimation
Loops: The running time of a loop is at most the running
time of the statements inside of that loop times the
number of iterations.
Nested Loops: Running time of a nested loop containing
a statement in the inner most loop is the running time of
statement multiplied by the product of the sized of all
loops.
Consecutive Statements: Just add the running times of
those consecutive statements.
If/Else: Never more than the running time of the test plus
the larger of running times of S1 and S2.
7
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Algorithm Growth Rates
12
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Design & Analysis of Algorithm @copyright – The Islamia University of Bahawalpur
Design & Analysis of Algorithm
End
Week – 2