0% found this document useful (0 votes)
8 views16 pages

Lecture 17 Algo 2

The document provides a detailed explanation of Big-O notation, including its formal definition and examples demonstrating how to determine if one function is asymptotically dominated by another. It includes several code segments and algorithms, analyzing their time complexity and providing Big-O estimates. Additionally, it discusses negative examples and limits related to Big-O relationships.

Uploaded by

tasnia.rifah009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views16 pages

Lecture 17 Algo 2

The document provides a detailed explanation of Big-O notation, including its formal definition and examples demonstrating how to determine if one function is asymptotically dominated by another. It includes several code segments and algorithms, analyzing their time complexity and providing Big-O estimates. Additionally, it discusses negative examples and limits related to Big-O relationships.

Uploaded by

tasnia.rifah009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Algorithm

Lecture 17

1
Big-O. Formal Definition

f (x ) is asymptotically dominated by g (x ) if there’s a


constant multiple of g (x ) bigger than f (x ) as x
goes to infinity:
DEF: Let f , g be functions with domain R0 or N and
codomain R. If there are constants C and k such
 x > k, |f (x )|  C  |g (x )|
then we write:
f (x ) = O ( g (x ) )

f does not grow faster than g


2
Show that f ( x)  x  2 x  1 is O( x )
2 2

If x>1 then
x<x2 and
1<x2

C=4 and k=1

If x>2 then
2x<=x2 and
1<= x2

C=3 and k=2


2 3
Show that 7 x is O( x )

If x>7 then
7x2 < x3 and

C=1 and k=7


If x>1 then
7x2 < 7x3 and

C=7 and k=1


Big-O and limits

LEMMA: If the limit as x   of the quotient |f (x) / g (x)|


exists then f (x ) = O ( g (x ) ).
EG: 3x 3 + 5x 2 – 9 = O (x 3 ). Compute:
3x 3  5 x 2  9 3  5 / x  9 / x3
lim 3
 lim 3
x  x x  1
…so big-O relationship proved.

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

Count the number of steps in the following code segment

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){

for j=2 to length(A){


key=A[j]
i=j-1
while i>0 and A[i]>key {
A[i+1]=A[i]
i--
}
A[i+1]:=key
}
}

12
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)

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

O (n)  O (n log n)  O (n 3 )  O (n!)

15
Thank You

16

You might also like