0% found this document useful (0 votes)
10 views4 pages

W2 2 255 Asymptotic 1

The document explains Big-O notation, which is used to describe the upper bound on the growth rate of functions. It provides examples demonstrating how to show that one function is O of another by finding appropriate constants c and N. Additionally, it outlines properties of Big-O notation and its application in algorithm analysis.

Uploaded by

ahmed.waasel
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)
10 views4 pages

W2 2 255 Asymptotic 1

The document explains Big-O notation, which is used to describe the upper bound on the growth rate of functions. It provides examples demonstrating how to show that one function is O of another by finding appropriate constants c and N. Additionally, it outlines properties of Big-O notation and its application in algorithm analysis.

Uploaded by

ahmed.waasel
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/ 4

9/14/21

Big-O Notation Example: Show f(n) = O(g(n))

• Definition: • Show that 30n+8 is O(n).


f(n) = O(g(n)) if there exist positive constants c – Show there exist positive constant c, N, such that
and N such that f(n) £ cg(n) for all n ³ N 30n+8 £ cn, for all n>N.
• Let c = 31, N = 8. For all n > N = 8, we have
cn = 31n = 30n + n >= 30n+8, so 30n+8 <= cn.
• Big-O expresses an upper bound on the growth
rate of a function, for sufficiently large values of
n.

• Graph to understand Big-O


To show that f(n) = O(g(n)) need to find two 2
constants c and N, and show that f(n) < cg(n) for all n>= N

1 2

Big-O example, Graphically More Examples: Show f(n) = O(g(n))

• Note that f (n) =30n+8 is not – f(n) =2n2 = O(n3):


less than g(n) = n
anywhere (n>0). cn =
31n 30n+8 Here g(n) = n3 .
Value of function ®

• 30n+8 is not
less than 31n To show that f(n) = O(n3 ), the condition to check is: 2n2 ≤ cn3 for all n > N
for the positive constants c and N.
everywhere.
30n+8 Next, we will see if we can find c and N to make the condition work.
• But 30n+8 it is less n
than 31n =O(n) 2n2 ≤ cn3 Þ 2 ≤ cn Þ we choose c = 1 and N= 2

With c =1 and N =2, we have


everywhere to n>=N=8 ® 2n2 ≤ cn3 for all n >=N, thus 2n2 = O(n3 )
the right of N=8.
The two constants
Increasing n ®
N = 8, c = 31
3 4

3 4

More Examples: Show f(n) = O(g(n)) More Examples: Show f(n) = O(g(n))

– f(n) =n2 = O(n2):


f(n)= 1000n2 +1000n = O(n2 ):
To show that f(n) = O(n2 ), the condition to check is: 2n2 ≤ cn2 for all n > N
for the positive constants c and N.

Next, we will see if we can find c and N to make the condition work. • ……
n2 ≤ cn2 Þ c ≥ 1 Þ c = 1 and N= 1 • 1000n2 +1000n ≤ 1000n2 + n2 =1001n2 Þ c=1001 and N = 1000

With c =1 and N =1, we have


n2 ≤ cn2 for all n >=N, thus n2 = O(n2 )

5 6

1
9/14/21

More Examples More Examples

f(n) = 2n2 f(n) = n


g(n) = n2 g(n) = n2
Is f(n) = O(g(n))? Is f(n) = O(g(n))?

N = 1, c = 2 N = 1, c = 1

7 8

More Examples More Examples

• Show that 2n2 + 1 = O(n2)


f(n) = 2n
g(n) = n2
Is f(n) = O(g(n))?

N = 2, c = 1

9 10

Show f(n) = O(g(n)), Different c and N


Proof with Induction
Choices
• later • f(n) = 2n2 + 3n + 1
• g(n) = n2
• N =1 c >= 6
• N = 2 c >=3.75
• …

11 12

2
9/14/21

Big-O Notation Visualizing Orders of Growth

• On a graph, as
• We say f1(n)=30n+8 is of order n, or O (n) you go to the
It is, at most, roughly proportional to n. right, a faster

Value of function ®
• f2(n)=n2+1 is of order n2, or O(n2). It is, at growing f1(n)=30n+8
most, roughly proportional to n2. function
• In general, any O(n2) function is at least as eventually
faster- growing as any O(n) function. becomes f2(n)=n2+1
larger...

Increasing n ®
13 14

13 14

Typical functions Big-O Visualization


O(g(n)) is the set of
functions with lower
or same order of
growth as g(n)

16

15 16

Properties of Big-O Notation Properties of Big-O Notation (cont’d)

• Fact 1 (Transitivity) • It follows from those facts that every polynomial


– If f(n)=O(g(n)) and g(n)=O(h(n)), then f(n)=O(h(n)) is big-O of n raised to the largest power
– Prove it (done in class)
• Fact 2 f(n) = aknk + ak-1nk-1 + . . . + a1n1 + a0 = O(nk)
– If f(n)=O(h(n)) and g(n)=O(h(n)), then
f(n)+g(n)=O(h(n))
• Fact 3
– The function ank=O(nk)
• Fact 4
– The function nk=O(nk+j) for any positive j

17 18

3
9/14/21

Now Quickly Determine Big-O…


Properties of Big-O Notation (cont’d)

• Fact 5
– If f(n)=cg(n), then f(n)=O(g(n))
• n4 + 100n2 + 10n + 50 is O(n4)
• Fact 6
• 10n3 + 2n2 is O(n3) is also O(n4) O(n5) …
– The function logan = O(logbn) for any positive
numbers a and b ¹ 1 • n3 - n2 is O(n3), O(n4)
– WHY?
• Fact 7 • constants
– logan = O(lgn) for any positive a ¹ 1, where lg n = – 10 is O(1)
log2n – 1273 is O(1)

20

19 20

Back to Program Analysis


Example (cont’d)
Algorithm 1 Algorithm 2
Cost Cost Rep
arr[0] = 0; c1 for(i=0; i<N; i++) c2 N+1
arr[1] = 0; c1 arr[i] = 0; c1 N
arr[2] = 0; c1 Algorithm 3 Cost Rep
...
arr[N-1] = 0; c1 sum = 0; c1 1
----------- -------------
c1+c1+...+c1 = c1 x N (N+1) x c2 + N x c1 = for(i=0; i<N; i++) c2 N+1
(c2 + c1) x N + c2 for(j=0; j<N; j++) c2 N*(N+1)

• Both algorithms are of the same order: O(N) sum += arr[i][j]; c3 N*N
------------
c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N2 = O(N2)

21 22

21 22

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy