Lecture 17 Algo 2
Lecture 17 Algo 2
Lecture 17
1
Big-O. Formal Definition
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
5
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. •
6
Review algorithm
i := 1
t := 0
while i ≤ n
t := t + i
i := i+1
7
Review Algorithm
Find Big O notation for the following code segment
for i = 1 to n
j=n;
while j >= 1
j=j-1;
8
Review Algorithm
Find Big O notation for the following code segment
for i = 1 to n{
j:=n;
while j >= 1{
j:=j-1;
}
}
9
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;
}
10
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;
}
11
Review Algorithm
Calculate the number of steps for this function. (consider:
length(A) = m)
do_it(A:array){
12
Review Algorithm
Give as good a big-O estimate as possible for each of
these functions.
13
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)
14
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
15
Thank You
16