Professional Documents
Culture Documents
Zeph Grunschlag
Announcements
HW 3 Due First of HW4 available. Only first 6 problems of HW4 appear on Midterm Midterm 1, two weeks from today. Late HW4s cant be accepted after Friday, 3/1 (solutions go up early)
L8
Agenda
Section 1.8: Growth of Functions
Big-O Big- (Omega) Big- (Theta)
L8
Notational Issues
The notation is unconventional. Seemingly, an equation is established; however, this is not the intention. EG: 3x 3 + 5x 2 9 = O (x 3) Doesnt mean that theres a function O (x 3) and that it equals 3x 3 + 5x 2 9. Rather the example is read as: 3x 3+5x 2 9 is big-Oh of x 3 Which actually means: 3x 3+5x 2 9 is asymptotically dominated by x 3
L8 5
L8
L8
y=x3
y=x2 y=x
L8 8
y=x3
y=x2 y=x
L8 9
y=x3
L8
y=x2 y=x
10
y = 3x 3+5x 2 9
y=x2 y=x
L8 11
Common Misunderstanding
Its true that 3x 3 + 5x 2 9 = O (x 3) as well 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: In CS, use 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. Its a way of comparing arbitrary pairs of functions.
L8 13
Big-O. Example
EG: Show that 3x 3 + 5x 2 9 = O (x 3). From the previous graphs it makes sense to let C = 5. Lets find k so that 3x 3 + 5x 2 9 5x 3 for x > k :
L8
14
L8
15
L8
16
L8
17
L8
20
L8
21
L8
22
L8
26
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 x (ln x ) 6. e 7. x 8. ( x + sin x)( x 20 102) x e 9. x 10. x
L8
28
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
29
Incomparable Functions
2500 2000
y = x2
1500
y = |x 2 sin(x)|
1000
y = 5x 1.5
500
L8 0 0
30
5 10 15 20 25 30 35 40 45 50
Incomparable Functions
4 x 10
4
3.5
y = x2 y = 5x 1.5 y = |x 2 sin(x)|
2.5
1.5
0.5
L8
20
40
60
80
100
120
140
160
180
200
31
L8
32
Hallmarks of Algorithms
The text book lays out 7 properties that an algorithm should have to satisfy the notion precise instructions in such a way that no ambiguity arises: 1. Input. Spell out what the algorithm eats 2. Output. Spell out what sort of stuff it spits out 3. Determinism (or Definiteness) At each point in computation, should be able to tell exactly what happens next
L8 34
Hallmarks of Algorithms
4. Correctness. Algorithm should do what it 5. 6. 7.
Q:
L8
claims to be doing. Finiteness. Finite number of steps for computation no matter what input. Effectiveness. Each step itself should be doable in a finite amount of time. Generality. Algorithm should be valid on all possible inputs. Which of the conditions guarantee that an algorithm has no infinite loops?
35
Hallmarks of Algorithms
A: Finiteness & Effectiveness
L8
36
Pseudocode
Starting out, students often find it confusing to write pseudocode, but find it easy to read pseudocode. Sometimes, students are so anti-pseudocode that theyd rather write compiling code in homework and exams. This is a major waste of time! I strongly suggest you use pseudocode. You dont have to learn the pseudo-Pascal in Appendix 2 of the text-book. A possible alternative: pseudo-Java
L8 37
Pseudo-Java
Start by representing an algorithm as a Java method. Its clear that Java specific modifiers such as final, static, etc. are not worth worrying about. So consider the method:
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; }
38
Pseudo-Java
A: It finds the minimum of a sequence of numbers.
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; }
Pseudo-Java
A: Yes, arrays of length 0. This illustrates one of the dangers of relying on real code. Forces you to worry about language specific technicalities, exceptions, etc.
L8
40
Pseudo-Java
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; }
Q: Are there any statements that are too long winded, or confusing to non-Java people?
L8 41
A: Yes, several:
Pseudo-Java
1. int f(int[] a) Non-Java people may not realize that this says that input should be an array of ints FIX (also helps with some problems below) spell out exactly how the array looks and use subscripts, which also tells us the size of the array: integer f( integer_array (a1, a2, , an) ) Type declaration notation integer f() is a succinct way of specifying the output .
L8 42
Pseudo-Java
2. a[0] brackets annoying in index evaluation. Why are we starting at 0? FIX: Use subscripts: ai and start with index 1.
L8
43
Psuedo-Java
3. for(int i=1; i<a.length; i++)
Counter in for-loop is obviously an int Change int i=1 to i=1 Really annoying if constantly have to refer to array length using the period operator Subscripts fixed this. Now a.length becomes n
Cant we just directly iterate over whole sequence and not worry about i++ (that way dont have to remember peculiar for-loop syntax) Change whole statement to for(i=1 to n)
44
Pseudo-Java
A: Yes. We restricted the input/output to be of type int. Probably wanted algorithm to work on arbitrary integers, not just those that can be encapsulated in 32 bits! Java solution: use class java.math.BigInteger NOT! Way too gnarly! Just output integer instead of int. Resulting pseudo-code (also removed semicolons):
L8 45
Pseudo-Java
integer f(integer_array (a1, a2, , an) ){ x = a1 for(i =2 to n){ if(x > ai) x = ai } return x }
L8 46
L8
50
L8
51
L8
54
L8
55
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
56
L8
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
.
58
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
59
WINNER
60
Each may in fact require a different number bit operations the actual operations that can be carried out in a single cycle on a processor. However, since each operation is itself O (1) --i.e. takes a constant amount of time, asymptotically as if each step was in fact 1 time-unit long!
L8 65
4.
L8
Running-time In days
T(n) = n
Input size n
L8 70