Professional Documents
Culture Documents
Algorithm Analysis Big Oh Notation
Algorithm Analysis Big Oh Notation
Efficiency of Programs
Abstract Machine Model
Machine Model
Machine Model
Sorting time 1000 elements > time for sorting 3 elements. input size.
A given algorithm takes different amount of times for same
Data shifting is not required in insertion sort for a sorted sequence.
But required for a reverse sorted sequence.
Example
f o r ( i = 0 ; i < n ; i ++)
sum += a [ i ] ;
Example
Description Times executed
Initialization step 1
Comparison step n+1
Addition and assignment steps together 2n
Increment step n
Total 4n + 2.
Example
f o r ( i = 0 ; i < n ; i ++)
f o r ( j = 0 ; j < n ; j ++)
sum += b [ i ] [ j ] ;
Example
Description Times executed
Initialization 1 + n (1 for i, n for j)
Comparison step (n + 1) + n(n + 1)
Addition and assignment steps together 2n × n
Increment step for first loop n
Increment step for second loop n2
Total 4n2 + 4n + 2
Example
f o r ( i = 0 ; i < n ; i ++)
f o r ( j = i + 1 ; j < n ; j ++)
if (a [ i ] < a [ j ])
swap ( a [ i ] , a [ j ] ) ;
Example
The input has n element in array, so, we know that above code will
result in:
n − 1 comparisons for a[0]
n − 2 comparisons for a[1], and so on.
In general, n − i − 1 comparisons for a[i]
Pn−1
Thus
Pn−1total number of comparisons = i=0 (n − i − 1) or
i=1 i = n(n − 1)/2
Efficiency of Algorithms
Comparing Algorithms
Suppose we have two algorithms A and B
A takes time 5000n and B takes 1.1n time
For n = 10, A requires 50000 steps and B takes 5500 steps
But for n = 1000, A requires 5,000,000 steps and B takes 2.5×1041
steps.
B can not be used for large inputs whereas A is ok.
Efficiency of Algorithms
Comparing Algorithms
Efficiency of Algorithms
Speed of Machine
Complexity
Growth Rate and Big O
Important measure of efficiency of a program is how the number of
steps (time) grows as the input grows.
Growth function is defined by big-O notation.
Definition (Big-O)
Let g : R → R be a function.
A function f : R → R is big-O of g(n), if there exist positive
constants c > 0, and n0 ∈ N such that
f (n) ≤ cg(n), for all n ≥ n0
Complexity
Big O
f (n) is bounded above by g(n) from some point onwards.
g(n) is formulated as a simpler function.
g(n) exhibits same trend in growth as f (n).
Since we are interested for large n, it is alright if f (n) ≤ cg(n) for
n > n0 .
Example (4)
f (n) = n2 + 2n + 1 ≤ n2 + 2n2 , if n ≥ 2
= 3n2
Therefore, for c = 3 and n0 = 2, f (n) ≤ cn2 , whenever n ≥ n0 .
Complexity
Big O
If f (n) is O(n2 ), is it also O(n3 )?
Since O(n3 ) grows faster than O(n2 ), it is true.
So, our attempt will be to find smallest simple function for which
f (n) is O(g(n)).
Well Known Growth Functions
1, log n, n, n log n, n2 , n3 , 2n , etc.
Complexity
Example (5)
Prove f (n) = n3 + 20n is Ω(n2 )
We must find c > 0, and n0 > 0 such that n3 + 20n ≥ c.n2
√
I.e., c ≤ n + 20
n . RHS is minimum, when n = 20
Therefore, with n0 = 5 and c ≤ 9 f (n) ≥ c.n2 for n ≥ n0 .
Note this is same as saying n2 is O(n3 + 20n).
Determining Big-O
Dominance Functions
Find dominant term of the function and find its order.
Any exponential function dominates any polynomial function.
Any polynomial function dominates any logarithmic function.
Any logarithmic function dominates any constant.
Any polynomial of degree k dominates lower degree polynomial
Since dominant term grows more rapid compared to others, it will
quickly outgrow non-dominant terms.
Determining Big-O
Other Simple Rules
If T1 (n) = O(f1 (n)), and T2 (n) = O(f2 (n)), then
T1 (n) + T2 (n) = max{ O(f1 (n)), O(f2 (n))}
T1 (n) * T2 (n) = O(f1 (n) * f2 (n))
If T(n) is a polynomial of k then T(n) = Θ(nk )
logk n = O(n) for any constant k
For checking whether g(n) and f (n) are comparable find
lim fg(n)
(n)
→ 1?
n2 2n
Eg: lim n2 +6
= lim 2n = 1
log n (1/n)
lim log n2
= lim 2(1/n) = 1/2.
Determining Big-O
Example (6)
Examples of O(n2 ) functions: n2 , n2 + n, n2 + 1000n, 100n2 + 1000n, n,
n/100, n1.99999 , n2 /(log log log n)
Example (7)
log n! = log 1 + log 2 + . . . + log n
≤ log n + log n + . . . + log n = n log n
Determining Big-O
Example (8)
2n+1 = 2.2n for all n, so with c = 2, n0 = 1.
So, 2n+1 = O(2n ).
n
But 22 6= O(2n ).
This can be proved it by contradiction.
n
Suppose 0 ≤ 22 = 2n .2n ≤ c.2n , then 2n ≤ c.
But no constant is greater than 2n .
Determining Big-O
Example (9)
Prove that n3 + 20n + 1 is not O(n2 ).
By definition we should have n3 + 20n + 1 ≤ c.n2 .
20 1
So n + n + n ≤ c.
Since left side grows with n, c can not be a constant.
Determining Big-O
Example (10)
n2 +5 log n
f (n) = 2n+1 is O(n)
5 log n < 5n < 5n2 , for all n > 1
1 1
2n + 1 > 2n, so 2n+1 < 2n for all n > 0
n2 +5 log n n2 +5n2
Thus 2n+1 ≤ 2n = 3n for all n > 1.
So, with c = 3 and n0 = 1 we have f (n) < c.n
Determining Big-O
Example (11)
Let f (n) = nk , and m > k, then we can show f (n) = O(nm− ), where
>0
Set = (m − k)/2, so m − = (m + k)/2 > k.
Hence, n(m−) dominates nk .
Example (12)
Let f (n) = nk , and m < k, then we can show f (n) = Ω(nm+ ), where
>0
Set = (k − m)/2, so m + = (m + k)/2 < k.
Hence, n(m+) is dominated by nk .
Determining Big-O
Example (13)
Show f (n) = nk is of O(nlog log n ) for any k > 0
k
nk < nlog log n iff k < log log n, i.e., n > 22 .
k
Setting n0 = 22 , we have nk = O(nlog log n ).
Example (14)
1 for ( i = 0; i < n; i ++)
2 a [ i ] = 0;
3 for ( i = 0; i < n ; i ++)
4 for ( j = 0; j < n ; j ++) {
5 sum = i + j;
6 s i z e ++;
7 }
Example (15)
1 char key ;
2 int X[5];
3 int Y[ 5 ] [ 5 ] ;
4 int i , j ;
5 .........
6 switch ( key ) {
7 case ’ a ’ :
8 f o r ( i = 0 ; i < s i z e o f (X) / s i z e o f (X [ 0 ] ) ; i ++)
9 sum = sum + X [ i ] ;
10 break ;
11 case ’ b ’ :
12 f o r ( i = 0 ; i < s i z e o f (Y) / s i z e o f (Y [ 0 ] ) ; i ++)
13 f o r ( j = 0 ; j < s i z e o f (Y [ 0 ] ) / s i z e o f (Y [ 0 ] [ 0 ] ) ; j ++)
14 sum = sum + Y [ i ] [ j ] ;
15 break ;
16 } // End o f s w i t c h b l o c k
Example (16)
1 char key ;
2 int A[5][5] , B[5][5] , C[ 5 ] [ 5 ] ;
3 ........
4 i f ( k e y == ’+ ’ ) {
5 f o r ( i = 0 ; i < n ; i ++)
6 f o r ( j = 0 ; j < n ; j ++)
7 C[ i ] [ j ] = A[ i ] [ j ] + B[ i ] [ j ] ;
8 } // End o f i f b l o c k ===> O( n ˆ 2 )
9 e l s e i f ( k e y == ’ x ’ )
10 C = m a t r i x M u l t (A , B ) ; ===> O( n ˆ 3 )
11 else
12 p r i n t f ( ” E r r o r ! E n t e r ’+ ’ o r ’ x ’ ! : ” ) ; ===> O( 1 )
nk knk−1
lim = lim
bn ln b.bn