W2 2 255 Asymptotic 1
W2 2 255 Asymptotic 1
1 2
• 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
3 4
More Examples: Show f(n) = O(g(n)) More Examples: Show f(n) = O(g(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
5 6
1
9/14/21
N = 1, c = 2 N = 1, c = 1
7 8
N = 2, c = 1
9 10
11 12
2
9/14/21
• 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
16
15 16
17 18
3
9/14/21
• 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
• 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