W-2 - L-1 - Asymptotic Notation Complexity Analysis
W-2 - L-1 - Asymptotic Notation Complexity Analysis
W-2 - L-1 - Asymptotic Notation Complexity Analysis
Complexity Analysis
Week-02, Lecture-01
Is it correct ?
FACTORS
Hardware
Operating System
Compiler
Size of input
Nature of Input
Algorithm
Depends upon
Input Size
Nature of Input
N = 10 => 53 steps
N = 100 => 503 steps
N = 1,000 => 5003 steps
N = 1,000,000 => 5,000,003 steps
f(N) = O(g(N))
16
EXAMPLE (2): COMPARING
FUNCTIONS
4000
Which function is better?
10 n2 Vs n3 3500
3000
2500
10 n^2
2000
n^3
1500
1000
500
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
17
COMPARING FUNCTIONS
0.05 N2 = O(N 2)
Time (steps)
3N = O(N)
Input (size)
N = 60
18
BIG-OH NOTATION
Simple Rule:
Drop lower order terms and constant factors
7n-3 is O(n)
8n2log n + 5n2 + n is O(n2 log n)
BIG OMEGA NOTATION
If we wanted to say “running time is at least…” we use Ω
If f(n) and g(n) are two complexity functions then we can say:
f(n) is Ω(g(n)) if there exist positive numbers c and n 0such that 0<=f(n)>=cΩ (n) for all n>=n 0
BIG THETA NOTATION
If we wish to express tight bounds we use the theta notation, Θ
21
WHAT DOES THIS ALL MEAN?
If f(n) = Θ(g(n)) we say that f(n) and g(n) grow at the same
rate, asymptotically
Why?
log n Logarithmic: when n increases, so does run time, but much slower. Common in programs which
solve large problems by transforming them into smaller problems.
n Linear: run time varies directly with n. Typically, a small amount of processing is done on each
element.
n log n When n doubles, run time slightly more than doubles. Common in programs which break a problem
down into smaller sub-problems, solves them independently, then combines solutions
n2 Quadratic: when n doubles, runtime increases fourfold. Practical only for small problems; typically
the program processes all pairs of input (e.g. in a double nested loop).
2n Exponential: when n doubles, run time squares. This is often the result of a natural, “brute force”
solution.
24
SIZE DOES MATTER[1]
N log2N 5N N log2N N2 2N
8 3 40 24 64 256
16 4 80 64 256 65536
32 5 160 160 1024 ~10 9
64 6 320 384 4096 ~10 19
128 7 640 896 16384 ~10 38
256 8 1280 2048 65536 ~10 76
25
COMPLEXITY CLASSES
) s p e st ( e m i T
26
SIZE DOES MATTER[2]
Suppose a program has run time O(n!) and the run time for
n = 10 is 1 second