L3_CS1201-AsypComplexityAnalysis
L3_CS1201-AsypComplexityAnalysis
Correctness Robustness
Adaptability
Efficiency
Reusability
Data Structures and Algorithms
Algorithm
Outline, the essence of a computational procedure,
step-by-step instructions
Data structure– Organization of data needed
to solve the problem
Program
an implementation of an algorithm in some
programming language
Algorithmic problem
Specification
Specification ? of output as
of input a function of
input
6n
5n
4n
3n
2n
1n
Best/Worst/Average Case (3)
worst-case
6n average-case
Running time
5n
best-case
4n
3n
2n
1n
1 2 3 4 5 6 7 8 9 10 11 12 …..
Input instance size
Input Size
polynomial degree
# of elements in a matrix
Efficiency:
Running time
Space used
Efficiency as a function of input size:
Number of data elements (numbers, points)
A number of bits in an input number
What is Algorithmic Complexity
return s;
}
The RAM model
RAM (Random Access Machine)
Cost Cost
arr[0] = 0; c1 for(i=0; i<N; i++) c2
arr[1] = 0; c1 arr[i] = 0; c1
arr[2] = 0; c1
... ...
arr[N-1] = 0; c1
----------- -------------
c1+c1+...+c1 = c1 x N (N+1) x c2 + N x c1 =
(c2 + c1) x N + c2
Another Example
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
Performance Analysis
1000n2+1000n = O(n2):
1000n2+1000n ≤ 1000n2+ n2 =1001n2 c=1001 and n0 = 1000
Value of function
It isn’t even
less than 31n
everywhere. 30n+8
But it is less than
O(n)
n
31n everywhere to
the right of n=8.
n>n0=8
Increasing n
Big-O Visualization
O(g(n)) is the set of
functions with smaller
or same order of
growth as g(n)
No Uniqueness
There is no unique set of values for n0 and c in proving the
asymptotic bounds
for all n ≥ 5
10n2 - 3n = Q(n2)
What constants for n0, c1, and c2 will
work? (c1=9, c2=11 and n0=10 )
Make c1 a little smaller than the leading
coefficient, and c2 a little bigger.
To compare orders of growth, look
at the leading term.
Relations Between Thera,O, omega
Asymptotic Notation
O notation: asymptotic “less than”:
Logarithmic
complexity:
time T(n) log n
O(log n) running
time does not grow
faster than a log
function of the
problem size n
Common orders of magnitude
Common orders of magnitude
o-notation
For a given function g(n), the set little-o:
o(g(n)) = {f(n): c > 0, n0 > 0 such that
n n0, we have 0 f(n) < cg(n)}.
f(n) becomes insignificant relative to g(n) as n
approaches infinity:
lim [f(n) / g(n)] = 0
n
f (n) = O(g(n)) a b
f (n) = W(g(n)) a b
f (n) = Q(g(n)) a = b
f (n) = o(g(n)) a < b
f (n) = w (g(n)) a > b
Limits
lim [f(n) / g(n)] = 0 f(n) o(g(n))
n
0 < lim
n
[f(n) / g(n)] < f(n) Q(g(n))
0 < lim
n
[f(n) / g(n)] f(n) W(g(n))
lim [f(n) / g(n)] = f(n) w(g(n))
n
lim
n
[f(n) / g(n)] undefined can’t say
Exercise
Exercise-Ans
Useful Mathematic Summations
n(n 1)
1 2 3 .... (n 1) n
2
n 1
( n 1) a 1
a a a a .... a
0 1 2 3
a n
a 1
n 1
( n 1) 2 1
2 2 2 2 .... 2
0 1 2 3
2
n
2 1
Examples: Determining Big-O
Repetition
Sequence
Selection
Logarithm
Repetition: Simple Loops
for (i = 1; i <= n; i++)
executed {
n times k = k + 5;
} constant time
Time Complexity
T(n) = (a constant c) * n = cn = O(n)
constant time
Time Complexity
T(n) = (a constant c) * n * n = cn2 = O(n2)
constant time
Time Complexity
T(n) = c + 2c + 3c + 4c + … + nc = cn(n+1)/2 =
(c/2)n2 + (c/2)n = O(n2)
Ignore non-dominating terms
Time Complexity
T(n) = 20 * c * n = O(n)
Time Complexity
T(n) = c *10 + 20 * c * n = O(n)
For Loops Analysis, Example
sum1 = 0;
for (k = 1; k < n; k *= 2) {
for (j = 0; j < n; j++) {
sum1++;
}
}
sum1 = 0;
for (i = n; i > 0; i /= 2) {
for (j = 1; j < n; j *= 2) {
for (k = 0; k < n; k +=
2) {
sum1++;
}
}
}
Find the Big-O for the code !!
sum1 = 0;
for (i = n; i > 0; i /= 2) {
for (j = 1; j < n; j *= 2) {
for (k = 0; k < n; k+ = 2) {
//Const. no. of
operations
}
}
}
sum1 = 0;
for (i = n; i > 0; i --) {
for (j = 1; j < n; j *= 2) {
for (k = 0; k < j; k++) {
sum1++;
}
}
}
sum1 = 0;
for (i = n; i > 0; i --) {
for (j = 1; j < n; j *= 2) {
for (k = 0; k < j; k++) {
sum1++;
}
}
}
Successive inner loops
sum1 = 0;
for (int bound = 1; bound < n; bound *= 2) {
for (i = 0; i < n; i++) {
for (j = 0; j < n; j += 2) {
// O(1) operations
}
for (k= 1; k < n; k *= 2) {
//constant no. of operations
}
}
}
Common Functions
Monotonicity
f(n) is
monotonically increasing if m n f(m) f(n).
monotonically decreasing if m n f(m) f(n).
strictly increasing if m < n f(m) < f(n).
strictly decreasing if m > n f(m) > f(n).
Exponentials
Useful Identities:
1 1
a
a
(a m ) n a mn
a m a n a m n
n b o( a n )
log c a
log b a
Natural log: ln a = logea log c b
Binary log: lg a = log2a log (1 / a ) log a
b b
1
lg2a = (lg a)2 log b a
lg lg a = lg (lg a) log a b
a log b c
c log b a
Sample Problems
Algorithm Big O ( Big Big Theta
Upper) Omega (Tight
(Lower) Bound)
Linear O(n) Ω(1) Θ(n)
Search
Binary O(log n) Ω(1) Θ(log n)
Search
Bubble O(n^2) Ω(n) Θ(n^2)
Sort
Useful Mathematic
Summations
n(n 1)
1 2 3 .... (n 1) n
2
n 1
( n 1) a 1
a a a a .... a
0 1 2 3
a n
a 1
n 1
( n 1) 2 1
2 2 2 2 .... 2
0 1 2 3
2
n
2 1