You are on page 1of 11

f f(n) = 10n3 – 43n2 + n – 100

f(n) = (n3) 成長率大


g
g(n) = 2n2 + 1000000n
g(n) = (n2) 成長率小
the rate of growth of g(n) is n2
n when n   (sufficiently large)
n0
*成長率 (rate of growth)指的是"最大項"
* f 的成長率比g大表示
"f(n)  g(n) for n  n0 "

when n is sufficiently large

3-1x
* When comparing the complexities of algorithms, 3-1a
we compare their rates of growth.
"最大項"
* "algo. A is better than algo B" means
"A is faster when n is sufficiently large"

insertion sort: O(n2) merge sort: O(nlg n)


faster for small n better
* Simple
* Constants depend on
-- hardware
-- programming skills
f, g: two functions (假設可以調整係數, when n   ) 3-1b

*如果 f, g
的最大項不一樣, 分出勝負
調整係數無法改變大小關係
f(n) = n3 + n2 – 100n 大 10-6  f(n) 大
g(n) = 30n2 + 100n 小 106  g(n) 小

* 如果 f, g 的最大項一樣, 由最大項係數決定
調整係數可以改變大小關係
f(n) = 4n3 + n2 – 100n 大 f(n) 小
g(n) = 3n3 + 5n2 + 100n 小 106  g(n) 大
f, g: two functions

調整 g 係數可讓 g  f 也可讓 g  f
f的最大項"="g的最大項
f的成長率"="g的成長率

調整 g 係數可讓 f  g
f的最大項""g的最大項
f的成長率""g的成長率

調整 g 係數可讓 g  f
f的最大項""g的最大項
f的成長率""g的成長率

3-1y
method 1. try n0 = 1, 2, 3, ...

n0 n0 n0
n 1 2 3 4 5 6 7 8 
3-6/n -3 0 1 1.5 1.8 2 2.14 2.25   

c1 ~c1c2~c1c2~ c2
try
- if n0 = 1, positive c1 and c2 exist ?
- if n0 = 2, positive c1 and c2 exist ?
- if n0 = 3, positive c1 and c2 exist ?


3-1y
method 2. choose c1, c2 and then find n0

n 1 2 3 4 5 6 7 8 
3-6/n -3 0 1 1.5 1.8 2 2.14 2.25   

c1n2  3n2 - 6n  c2n2

- choose c1 = 2 and c2 = 3 -----> n0 = 6


- choose c1 = 0.5 and c2 = 4 -----> n0 = ???



3-1y
f, g: two functions

調整 g 係數可讓 g  f 也可讓 g  f
f的最大項"="g的最大項
f的成長率"="g的成長率 f= (g)

調整 g 係數可讓 f  g
f的最大項""g的最大項
f的成長率""g的成長率 f= O(g)

調整 g 係數可讓 g  f
f的最大項""g的最大項
f的成長率""g的成長率 f= (g)

3-3x
o(g(n)) = Ο(g(n)) \ Θ(g(n)) ???

Mathematically, no!
g(n) = n2 f = O(g), but not o or 

f(n)  0 if "n is even" and is n2 otherwise

Algorithmically, yes!
Because, it is reasonable to assume that
T(n) is "regular" for large n.

3-3y
functions:    O 
real numbers:     

Transitivity
a b, b c f(n) = O(g(n)), g(n) = O(h(n))
a c f(n) = O(h(n))
Reflexivity a a f(n) = (f(n))

Symmetry a b b a f(n) = (g(n) g(n) = (f(n))

Transpose a b ba f(n) = (g(n) g(n) = (f(n))


Symmetry
a b ba f(n) = (g(n) g(n) = (f(n))

3-4x
time complexities

constant n0.000000000001 linear

1 lg* n lglg n lg n n n n2 n3    nk 2n   
2

polynomial exponential

g(n)  a(n) g(n)  b(n)


大 小 All logarithms are
base-2 in CS
大 小
n2n0.01 n2lg n
大 小
大 小

3-4y


1
x 
k
( x  1) 等比會收斂 !
k 0
1  x

n + (1/2)n + (1/2)2 n + (1/2)3 n + ... + (1/2)k n


= n  (1 + (1/2) + (1/2)2 + (1/2)3 + ... + (1/2)k )
n2
= O(n)

n2 + (2/3)n2 + (2/3)2 n2 + (2/3)3 n2 + ... + (2/3)k n2


= n2  (1 + (2/3) + (2/3)2 + (2/3)3 + ... + (2/3)k )
 n2  3
= O(n2)

3-5x

You might also like