Lecture 09 Algo
Lecture 09 Algo
Lecture 09
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
Intermission
36
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
39
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. •
40
Review algorithm
i := 1
t := 0
while i ≤ n
t := t + i
i := i+1
41
Review Algorithm
Find Big O notation for the following code segment
for i = 1 to n
j=n;
while j >= 1
j=j-1;
42
Review Algorithm
Find Big O notation for the following code segment
for i = 1 to n{
j:=n;
while j >= 1{
j:=j-1;
}
}
43
Review Algorithm
Find Big O notation for the following code segment
do_it_now(n){
r ← 0;
for i ← 1 to n {
for j ← 1 to i {
for k ← j to i+j {
r ← r+1;
}
}
}
return r;
}
44
Review Algorithm
Give a steps count and give a big-O estimate of the
algorithm. (hint: n =x.length)
int do_it(int [] x)
{
int i,j;
int count =0;
for(i=0;i<x.length;++i){
for(j=0;j<i;++j){
if(x[i] + x[j]<0)
count+=1;
}
}
return count;
}
45
Review Algorithm
Calculate the number of steps for this function. (consider:
length(A) = m)
do_it(A:array){
46
Review Algorithm
Give as good a big-O estimate as possible for each of
these functions.
47
Review Algorithm
Give as good a big-O estimate as possible for each of
these functions.
1.(2n 1)(1 log n 3 )
2.4n 6n 2 log n
( n 2 1)(n 2 3)(n 2 5)
3.
n( n 2 2)(n 2 4)
4.3n 2 2n log( n 2 1)
48
Review Algorithm
Give as good a big-O estimate as possible for each of
these functions.
1. n 3 n 8 O(max(n 3 , n,1)) O (n 3 )
2. log n n O (max(log n, n)) O ( n)
3. n log n O((n)(log n)) O( n log n)
n!
4. O ((n!)(1)) O(n!)
7
49
Thank You
50