Lecture 13 Algo 1
Lecture 13 Algo 1
Lecture 13
1
Algorithms
2
Pseudo-Java
3
Pseudo-Java Version 1
4
Pseudo-Java version 2
5
Algorithm for Surjectivity
6
Improved Algorithm for Surjectivity
7
Algorithmic Complexity
8
Running Time
Basic steps—
Assignment Increment
Comparison Negation
Return Random array access
Function output access etc.
9
Running time of 1st algorithm
boolean isOnto( function f: (1, 2,…, n) (1, 2,…, m)
){
if( m > n ) return false 1+1 step OR:
soFarIsOnto = true 1 step (assigment)
for( j = 1 to m ){ m loops: 1 increment plus
soFarIsOnto = false 1 step (assignment)
for(i = 1 to n ){ n loops: 1 increment plus
if ( f(i ) == j ) 1 step possibly leads to:
soFarIsOnto = true 1 step (assignment)
if( !soFarIsOnto ) 1 step possibly leads to:
return false 1 step (return)
}
}
return true; possibly 1 step
}
WORST-CASE
Running time of 1st algorithm
running time:
Number of steps = 1
OR
1+
1 step (m>n) OR: m·
1 step (assigment) (1+ 1 +
m loops: 1 increment plus n·
1 step (assignment) (1
n loops: 1 increment plus +1
1 step possibly leads to: +1
1 step (assignment) +1
+1
1 step possibly leads to:
)
1 step (return)
)
possibly 1 step
1+m(2+5n)+1 +1
= 2+2m+5mn
=5mn+2m+2
= 1 (if m>n) OR
5mn+2m+2
Running time of 2nd algorithm
boolean isOntoB( function f: (1, 2,…, n) (1, 2,…, m) ){
if( m > n ) return false 1 step OR:
for( j = 1 to m ) m loops: 1 increment plus
beenHit[ j ] = false 1 step (assignment)
for(i = 1 to n )
n loops: 1 increment plus
beenHit[ f(i ) ] = true
1 step (assignment)
for(j = 1 to m )
if( !beenHit[ j ] ) m loops: 1 increment plus
return false 1 step possibly leads to:
return true 1 step
} possibly 1 step
.
Running time of 2nd algorithm
15
Running Times Issues Big-O Response
16
Running Times Issues Big-O Response
17
Notational Issues
18
Intuitive Notion of Big-O
19
Intuitive Notion of Big-O domain
y = 3x 3+5x 2 –9
y=x3
y=x2
y=x
Intuitive Notion of Big-O domain
y = 3x 3+5x 2 –9
y=x3
y=x2
y=x
Intuitive Notion of Big-O domain
y = 3x 3+5x 2 –9
y=x3
y=x2
y=x
Intuitive Notion of Big-O domain
y = 3x 3+5x 2 –9
y=x3
y=x2
y=x
23
Intuitive Notion of Big-O
In fact, 3x 3+5x 2 –9 is smaller than 5x 3 for large enough
values of x:
y = 5x 3
y = 3x 3+5x 2 –9
y=x2
y=x
24
Big-O. Formal Definition
26
EG: Show that 3x 3 + 5x 2 – 9 = O (x 3).
Find k so that
3x 3 + 5x 2 – 9 5x 3
for x > k
1. Collect terms: 5x 2 ≤ 2x 3 + 9
27
EG: Show that 3x 3 + 5x 2 – 9 = O (x 3).
Find k so that
3x 3 + 5x 2 – 9 5x 3
for x > k
1. Collect terms: 5x 2 ≤ 2x 3 + 9
2. What k will make 5x 2 ≤ x 3 for x > k ?
28
EG: Show that 3x 3 + 5x 2 – 9 = O (x 3).
Find k so that
3x 3 + 5x 2 – 9 5x 3
for x > k
1. Collect terms: 5x 2 ≤ 2x 3 + 9
2. What k will make 5x 2 ≤ x 3 for x > k ?
3. k = 5 !!
29
EG: Show that 3x 3 + 5x 2 – 9 = O (x 3).
Find k so that
3x 3 + 5x 2 – 9 5x 3
for x > k
1. Collect terms: 5x 2 ≤ 2x 3 + 9
2. What k will make 5x 2 ≤ x 3 for x > k ?
3. k = 5 !!
4. So for x > 5, 5x 2 ≤ x 3 ≤ 2x 3 + 9
30
EG: Show that 3x 3 + 5x 2 – 9 = O (x 3).
Find k so that
3x 3 + 5x 2 – 9 5x 3
for x > k
1. Collect terms: 5x 2 ≤ 2x 3 + 9
2. What k will make 5x 2 ≤ x 3 for x > k ?
3. k = 5 !
4. So for x > 5, 5x 2 ≤ x 3 ≤ 2x 3 + 9
5. Solution: C = 5, k = 5
31
Show that f ( x) x 2 x 1 is O( x )
2 2
If x>1 then
x<x2 and
1<x2
If x>2 then
2x<=x2 and
1<= x2
If x>7 then
7x2 < x3 and
34
Big-O. Negative Example
x 4 O (3x 3 + 5x 2 – 9) :
No pair C, k exist for which
x > k implies C (3x 3 + 5x 2 – 9) x 4
Argue using limits: 4
x x
lim lim
x C (3 x 5 x 9)
3 2 x C (3 5 / x 9 / x 3 )
x 1
lim lim x
x C (3 0 0) 3C x
x 4 always catches up regardless of C. •
35
Thank You
36