Unit 2.1 AsymptoticAnalysis
Unit 2.1 AsymptoticAnalysis
Unit 2.1 AsymptoticAnalysis
Algorithm
Compiled By
Prof. Dharmesh R. Tank
CE\IT Department, LDRP-ITR
Analysis of Algorithms
• An algorithm is a finite set of precise instructions
for performing a computation or for solving a
problem.
• What is the goal of analysis of algorithms?
– To compare algorithms mainly in terms of running
time but also in terms of other factors (e.g., memory
requirements, programmer's effort etc.)
• What do we mean by running time analysis?
– Determine how running time increases as the size
of the problem increases.
Input Size
• Input size (number of elements in the input)
– size of an array
– polynomial degree
– # of elements in a matrix
– # of bits in the binary representation of the input
• Algorithm 3 Cost
sum = 0; c1
for(i=0; i<N; i++) c2
for(j=0; j<N; j++) c2
sum += arr[i][j]; c3
------------
=c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N2
Asymptotic Analysis
• To compare two algorithms with running
times f(n) and g(n), we need a rough
measure that characterizes how fast
each function grows.
• Hint: use rate of growth
• Compare functions in the limit, that is,
asymptotically!
(i.e., for large values of n)
Rate of Growth
• Consider the example of buying elephants and
goldfish:
Cost: cost_of_elephants + cost_of_goldfish
Cost ~ cost_of_elephants (approximation)
• The low order terms in a function are relatively
insignificant for large n
n4 + 100n2 + 10n + 50 ~ n4
Value of function
function fA(n)=30n+8
eventually
becomes
larger... fB(n)=n2+1
Increasing n
Example
Problem: Prove that 2n + 10 is O(n)
Solution:
• 2n + 10 <= cn
• 10<= cn -2n
• (c - 2) n >=10
• n >= 10/(c - 2)
• If we pick c = 3 and n0 = 10, at this value is fall
under Big-O.
Example
•
More Examples
7n-2 is O(n)
Solution: need c > 0 and n0 1 such that 7n-2 c•n for n n0
this is true for c = 7 and n0 = 1
Algorithm 3 Cost
sum = 0; c1
for(i=0; i<N; i++) c2
for(j=0; j<N; j++) c2
sum += arr[i][j]; c3
------------
c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N2 = O(N2)
More Examples …
Value of function
less than 31n
everywhere.
• But it is less than
31n everywhere to
30n+8
n
the right of n=8. O(n)
n>n0=8
Increasing n
No Uniqueness
• There is no unique set of values for n0 and c in proving the
asymptotic bounds
• Prove that 100n + 5 = O(n2)
– 100n + 5 ≤ 100n + n = 101n ≤ 101n2
for all n ≥ 5
Must find SOME constants c and n0 that satisfy the asymptotic notation relation
Asymptotic notations (cont.)
• - notation
– 100n + 5 ≠ (n2)
c, n0 such that: 0 cn2 100n + 5
100n + 5 100n + 5n ( n 1) = 105n
cn2 105n n(cn – 105) 0
Since n is positive cn – 105 0 n 105/c
contradiction: n cannot be smaller than a constant
– n = (2n), n3 = (n2), n = (logn)
Asymptotic notations (cont.)
• -notation
• ½ n2 - ½ n ≤ ½ n2 n ≥ 0 c2= ½
• ½ n2 - ½ n ≥ ½ n2 - ½ n * ½ n ( n ≥ 2 ) = ¼ n2
c1= ¼
– n ≠ (n2): c1 n2 ≤ n ≤ c2 n2
c2 ≥ n/logn, n≥ n0 – impossible
Relations Between Different Sets
• Subset relations between order-of-growth sets.
RR
O( f ) ( f )
•f
( f )
Common orders of magnitude
Common orders of magnitude
Logarithms and properties
• In algorithm analysis we often use the notation “log n”
without specifying the base
log b x log a x
log a b
More Examples
• For each of the following pairs of functions, either f(n) is
O(g(n)), f(n) is Ω(g(n)), or f(n) = Θ(g(n)). Determine
which relationship is correct.
– f(n) = log n2; g(n) = log n + 5 f(n) = (g(n))
– f(n) = n; g(n) = log n2 f(n) = (g(n))
– f(n) = log log n; g(n) = log n f(n) = O(g(n))
– f(n) = n; g(n) = log2 n f(n) = (g(n))
– f(n) = n log n + n; g(n) = log n f(n) = (g(n))
– f(n) = 10; g(n) = log 10 f(n) = (g(n))
– f(n) = 2n; g(n) = 10n2 f(n) = (g(n))
– f(n) = 2n; g(n) = 3n f(n) = O(g(n))
Properties
• Theorem:
f(n) = (g(n)) f = O(g(n)) and f = (g(n))
• Transitivity:
– f(n) = (g(n)) and g(n) = (h(n)) f(n) = (h(n))
– Same for O and
• Reflexivity:
– f(n) = (f(n))
– Same for O and
• Symmetry:
– f(n) = (g(n)) if and only if g(n) = (f(n))
• Transpose symmetry:
– f(n) = O(g(n)) if and only if g(n) = (f(n))
Asymptotic Notations in Equations
• On the right-hand side
– (n2) stands for some anonymous function in (n2)
2n2 + 3n + 1 = 2n2 + (n) means:
There exists a function f(n) (n) such that
2n2 + 3n + 1 = 2n2 + f(n)
• On the left-hand side
2n2 + (n) = (n2)
No matter how the anonymous function is chosen on
the left-hand side, there is a way to choose the
anonymous function on the right-hand side to make
the equation valid.
Common Summations
n
n(n 1)
• Arithmetic series: k 1 2 ... n
k 1 2
n
x n 1 1
x 1 x x ... x
k 2 n
x 1
• Geometric series: k 0 x 1
1
– Special case: |x| < 1: x
k 0
k
1 x
n
1 1 1
• Harmonic series:
k 1 k
1
2
...
n
ln n
n
1
k p 1p 2 p ... n p
k 1 p 1
n p 1
Mathematical Induction
• A powerful, rigorous technique for proving that a
statement S(n) is true for every natural number n,
no matter how large.
• Proof: