Big O, Big Theta, Big Omega
Big O, Big Theta, Big Omega
Big O, Big Theta, Big Omega
Zeph Grunschlag
Agenda
Section 2.1: Algorithms
L8
L8
Pseudo-Java
Possible alternative to text s pseudo-Java Start with real Java and simplify:
int f(int[] a){ int x = a[0]; for(int i=1; i<a.length; i++){ if(x > a[i]) x = a[i]; } return x; }
L8
Pseudo-Java Version 1
integer f(integer_array (a1, a2, x = a1 for(i =2 to n){ if(x > ai) x = ai } return x }
L8
, an) ){
Pseudo-Java version 2
INPUT: integer_array V = (a1, a2, begin x = a1 for(y V) if(x > y) x=y end OUTPUT: x
L8
, a n)
Recursive Algorithms
long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }
Compute 5!
L8
10
Recursive Algorithms
long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }
f(5)= 5f(4)
L8
11
Recursive Algorithms
long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }
f(4)= 4f(3)
f(5)= 5f(4)
L8
12
Recursive Algorithms
long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }
f(3)= 3f(2)
f(4)= 4f(3)
f(5)= 5f(4)
L8
13
Recursive Algorithms
long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }
f(2)= 2f(1)
f(3)= 3f(2)
f(4)= 4f(3)
f(5)= 5f(4)
L8
14
Recursive Algorithms
long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }
f(1)= 1f(0)
f(2)= 2f(1)
f(3)= 3f(2)
f(4)= 4f(3)
f(5)= 5f(4)
L8
15
Recursive Algorithms
long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }
f(0)= 1
f(1)= 1f(0)
f(2)= 2f(1)
f(3)= 3f(2)
f(4)= 4f(3)
f(5)= 5f(4)
L8
16
Recursive Algorithms
long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }
11= 1
f(2)= 2f(1)
f(3)= 3f(2)
f(4)= 4f(3)
f(5)= 5f(4)
L8
17
Recursive Algorithms
long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }
21= 2
f(3)= 3f(2)
f(4)= 4f(3)
f(5)= 5f(4)
L8
18
Recursive Algorithms
long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }
32= 6
f(4)= 4f(3)
f(5)= 5f(4)
L8
19
Recursive Algorithms
long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }
46= 24
f(5)= 5f(4)
L8
20
Recursive Algorithms
long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); }
524= 120
L8
21
Recursive Algorithms
long factorial(int n){ if (n<=0) return 1; return n*factorial(n-1); } Return 5!
= 120
L8
22
L8
23
Running Time
Basic steps
Assignment Increment Comparison Negation Return Random array access Function output access etc.
In a particular problem, may tell you to consider other operations (e.g. multiplication) and ignore all others
L8 24
Running time of
boolean isOnto( function f: (1, 2, , n) (1, 2, , m) ){ if( m > n ) return false soFarIsOnto = true for( j = 1 to m ){ soFarIsOnto = false for(i = 1 to n ){ if ( f(i ) == j ) soFarIsOnto = true if( !soFarIsOnto ) return false } } return true; }
L8
st 1
algorithm
1 step OR: 1 step (assigment) m loops: 1 increment plus 1 step (assignment) n loops: 1 increment plus 1 step possibly leads to: 1 step (assignment) 1 step possibly leads to: 1 step (return)
possibly 1 step
25
Running time of
1 step (m>n) OR: 1 step (assigment) m loops: 1 increment plus 1 step (assignment) n loops: 1 increment plus 1 step possibly leads to: 1 step (assignment) 1 step possibly leads to: 1 step (return) possibly 1 step
st 1
algorithm
WORST-CASE running time: Number of steps = 1 OR 1+ 1+ m (1+ 1 + n (1+1 + 1 +1 + 1 ) +1 ) = 1 (if m>n) OR 5mn+3m+2
26
L8
Running time of
boolean isOntoB( function f: (1, 2, , n) (1, 2, , m) ){ if( m > n ) return false for( j = 1 to m ) beenHit[ j ] = false for(i = 1 to n ) beenHit[ f(i ) ] = true for(j = 1 to m ) if( !beenHit[ j ] ) return false return true }
nd 2
algorithm
1 step OR: m loops: 1 increment plus 1 step (assignment) n loops: 1 increment plus 1 step (assignment) m loops: 1 increment plus 1 step possibly leads to: 1 step possibly 1 step
.
27
L8
Running time of
1 step (m>n) OR: m loops: 1 increment plus 1 step (assignment) n loops: 1 increment plus 1 step (assignment) m loops: 1 increment plus 1 step possibly leads to: 1 step possibly 1 step
nd 2
algorithm
WORST-CASE running time: Number of steps = 1 OR 1+ m (1+ 1) + n (1+ 1) + m (1+ 1 + 1) + 1 = 1 (if m>n) OR 5m + 2n + 2
L8
28
n 2+3n+2
vs.
8 +2
L8
29
L8
31
L8
32
L8
33
L8
34
L8
36
Notational Issues
Big-O notation is a way of comparing functions. Notation unconventional: EG: 3x 3 + 5x 2 9 = O (x 3) Doesn t mean 3x 3 + 5x 2 9 equals the function O (x 3) Which actually means 3x 3+5x 2 9 is dominated by x 3 Read as: 3x 3+5x 2 9 is big-Oh of x 3
L8 37
L8
38
y=x y=x
L8
39
y=x
y=x
y=x
L8
40
y=x
y=x y=x
L8
41
y=x
y=x y=x
L8
42
y = 3x 3+5x 2 9
y=x y=x
L8 43
L8
44
Common Misunderstanding
It s true that 3x 3 + 5x 2 9 = O (x 3) as we ll prove shortly. However, also true are:
3x 3 + 5x 2 9 = O (x 4) x 3 = O (3x 3 + 5x 2 9) sin(x) = O (x 4)
NOTE: C.S. usage of big-O typically involves mentioning only the most dominant term. The running time is O (x 2.5) Mathematically big-O is more subtle.
L8 45
Big-O. Example
EG: Show that 3x 3 + 5x 2 9 = O (x 3). Previous graphs show C = 5 good guess. Find k so that 3x 3 + 5x 2 9 e 5x 3 for x > k
L8
46
L8
47
L8
48
L8
49
L8
54
L8
55
f (x ) = ;(g (x ))
f is of order g
L8
56
Useful facts
Any polynomial is big-5 of its largest term
EG: x 4/100000 + 3x 3 + 5x
9 = 5(x 4)
L8
A: 1.1 x 2.13 1 x 3. ln x, lg 2 x (change of base formula) 4. x sin x, x x ,13 x 5. x ln x 2 6. x(ln x) e 7. x 8. ( x sin x)( x 20 102) x 9. e 10. x x
L8
59
Incomparable Functions
Given two functions f (x ) and g (x ) it is not always the case that one dominates the other so that f and g are asymptotically incomparable. E.G: f (x) = |x 2 sin(x)| vs. g (x) = 5x 1.5
L8
60
Incomparable Functions
2500 2000
y = x2
1500
1000
y = |x 2 sin(x)|
y = 5x 1.5
500
L8
0 0 5 10 15 20 25 30 35 40 45 50
61
Incomparable Functions
x 10 4
4
3.5
2.5
y = x2
1.5
y = 5x 1.5 y = |x 2 sin(x)|
0.5
L8
20
40
60
80
100
120
140
160
180
200
62
Running-time In days
T(n) = n
Input size n
L8 64
L8
66