Asymptotic Analysis of Algorithms (Growth of Function)
Asymptotic Analysis of Algorithms (Growth of Function)
function)
Resources for an algorithm are usually expressed as a
function regarding input. Often this function is messy
and complicated to work. To study Function growth
efficiently, we reduce the function down to the
important part.
Let f (n) = an2+bn+c
Asymptotic notation:
For example: In bubble sort, when the input array is already sorted, the
time taken by the algorithm is linear i.e. the best case.
But, when the input array is in reverse condition, the algorithm takes the
maximum time (quadratic) to sort the elements i.e. the worst case.
When the input array is neither sorted nor in reverse order, then it takes
average time. These durations are denoted using asymptotic notations.
Theta notation encloses the function from above and below. Since it
represents the upper and the lower bound of the running time of an
algorithm, it is used for analyzing the average case complexity of an
algorithm.
or a function g(n) , Θ(g(n)) is given by the relation:
4) f(n) = 5n2 + 5n +6
<=5n2 + 5n + n (n>=6)
<=5n2 + 6n
For n>=6, n2>=6n
<= 5n2 + n2
<= 6n2 , c2=6,c1=5 , n0=6
F(n) =(n2)
5) f(n) = 5n3 + n2 + 3n + 2
For n>= 2
5n3 + n2 + 3n + 2 <= 5n3 + n2 + 3n + n <=5n3 + n2 + 4n
For n2>= 4n
5n3 + n2 + 4n <= 5n3 + n2 + n2<= 5n3 + 2n2
For n3>=2n2
5n3 + 2n2<= 5n3 + n3<= 6n3 where c2=6 and n0=2
5n3< 5n3 + n2 + 3n + 2 for all n>n0 and c1= 5
5n3< 5n3 + n2 + 3n + 2 <= 6n3
F(n) = (n3)
Big-O Notation (O-notation)
Since it gives the worst case running time of an algorithm, it is widely used
to analyze an algorithm as we are always interested in the worst case
scenario.
Example
1) f(n) = 20
f(n) = O(1)
2)
F(n) = 30n+7
<=30n+n n>=7
<=31n c=31, n0 =7
Example
1) f(n) = 20n + 7
20n<20n+7 ; c=20 n0 =7
f(n) = Ω (n)
F(n) = Ω(n2)
Asymptotic analysis
1. Sequencing
2. If-then-else
3. for loop
4. While loop
1. Sequencing:
Example
2. If-then-else:
Example:
3. For loop:
2. Statement(s);
Complexity of for loop:
The outer loop executes N times. Every time the outer
loop executes, the inner loop executes M times. As a
result, the statements in the inner loop execute a total
of N * M times. Thus, the total complexity for the two
loops is O (N2)
1. for i ← 1 to n
2. {
3. P (i)
4. }
If the computation time ti for ( PI) various as a function
of "i", then the total computation time for the loop is
given not by a multiplication but by a sum i.e.
1. For i ← 1 to n
2. {
3. P (i)
4. }
Takes
For j ← 1 to n
{
P (ij)
}
}
Example:
1. For i ← 2 to n-1
2. {
3. For j ← 3 to i
4. {
5. Sum ← Sum+A [i] [j]
6. }
7. }
Solution:
The total Computation time is:
4. While loop:
Algorithm:
1. 1. [Initialize] Set k: =1, LOC: =1 and MAX: = DATA [1]
Example:
Solution:
1. array Max (A, n)
2. 1. Current max ← A [0]
3. 2. For i ← 1 to n-1
4. 3. do if current max < A [i]
5. 4. then current max ← A [i]
6. 5. return current max.
1. 2 + 1 + n +4 (n-1) + 1=5n
2. 2 + 1 + n + 6 (n-1) + 1=7n-2