Professional Documents
Culture Documents
ANALYSIS
ASYMPTOTIC GROWTH FUNCTIONS
Agenda: How to compare two algorithms?
• Complexity measures
• Time
• Time is more important than space constraint unless space increase is exponential.
• Space
Machine
• Only taking CPU time of execution is not feasible because: dependence
• CPU execution time changes with type of machine, instruction set etc.
• Same program on same computer can have different times of execution due
to different size of inputs.
Input
dependence
Approximations to be made
• For every unit of computation give a fixed unit time.
• Eg: Maximum addition time for 10 bit numbers is x units. Based on this find
how many units of time does a function take.
• For different sizes of inputs, measure the computation time.
• Eg: Size of a input takes 20 units of time. Size of b input takes 30 units of time.
Measuring time complexity (in terms of
steps) as a function of input size
• Example of time complexity function f(n) = 12 n3 + 15 n logn – 5 n logn
n is a measure
of the input
size
• Why not take all 4 numbers as a string and sort? eg: 5#2#3#12
• In such a case the number of inputs is 1.
• Does the time complexity reduce?
Concept of Input Size
• In the previous case, the complexity of the sorting algorithm would be
measured proportional to the length of the input.
Carry
12345678 For n such
operations,
+ 23456789 f(n) = 2(n – 1) + 1
Concept of Range of Size
• Eg:
• If input numbers vary from 10 to 20 then the range of size of input is known.
• If it is known that numbers from 1 to 100 need to be sorted, then the range of
the maximum input size is known.
• Therefore, maximum complexity of the algorithm can be predetermined.
• i.e. for adding 100 nos. if complexity f(n) = 2n - 1 for 1 ≤ n ≤ 100, then f(n) is a
constant for maximum limit of 100. The algorithm will not take more time
than the maximum limit for 100 numbers.
• However, the analysis needs to be made for the range within 1 ≤ n ≤
100.
Worst case analysis for all ‘n’ inputs
• Consider f(n) = 305 n2 + 3n + 2
g(n) = n3 + 8n2 + 8n + 9
h(n) = 3000 n logn + 9
Take n = 1 to 3 and find which function yields the least value.
What if n tends to a very large value??
functions.
n0 n
f( n ) = n3 + 1999n + 1337 gives f( n ) = n3
Asymptotic Growth Functions
• Three types:
• Upper bounding function (O)
• Lower bounding function (Ω)
• Order function (Θ)
The O(f(n)) notation (Asymptotic upper
bounding)
A function g(n) = O(f(n)) if there are constants c and n0, both greater
than 0 such that,
0 ≤ g(n) ≤ cf(n) for all n ≥ n0
i.e. given g(n) we try to find another function that upper bounds it in a
certain range.
O(f(n))
Let g(n) = 502n2+ 3n – 7
f(n) = 715 n2 + 100n + 10
Find c and n0 such that for all n ≥ n0, 0 ≤ g(n) ≤ cf(n)
Solution:
Take c = 1; n0 = 2…, the condition 0 ≤ g(n) ≤ cf(n) would get satisfied.
Answer: Yes..
Therefore, g(n) = O(n2)
i.e. given g(n) we try to find another function that lower bounds it in a
certain range.
Alternatively, if g(n) upper bounds f(n), then the vice versa is true.
The Θ(f(n)) notation (Order function)
A function g(n) = Θ(f(n)) if there are constants c1, c2 and n0, all greater
than 0 such that,
0 ≤ c1f(n) ≤ g(n) ≤ c2f(n) for all n ≥ n0
{ O(n)
statements; Runs (n) times
}
THE FOR LOOP
Runs (n) times
for (i = 0; i < n; i=i+2)
O(n)
{
statements; Runs (n/2) times
{ 1
1*2 = 2
statements; 2*2 = 22
} 22*2 = 23
2k
Assume: i >= n
As i = 2k
2k >= n
k=O
THE FOR LOOP
{
statement;
}
}