Recall What is the measurement of algorithm? How to compare two
algorithms? Definition of Asymptotic Notation
Slide 3
Today Topic
Slide 4
Interesting Topics of Upper Bound Rule of thumb! We neglect
Lower order terms from addition E.g. n 3 +n 2 = O(n 3 ) Constant
E.g. 3n 3 = O(n 3 ) Remember that we use = instead of (more
correctly)
Slide 5
Why Discard Constant? From the definition We can use any
constant E.g. 3n = O(n) Because When we let c >= 3, the
condition is satisfied
Slide 6
Why Discard Lower Order Term? Consider f(n) = n 3 +n 2 g(n) = n
3 If f(n) = O(g(n)) Then, for some c and n 0 c * g(n)-f(n) > 0
Definitely, just use any c >1
Slide 7
Why Discard Lower Order Term? Try c = 1.1 Does 0.1n 3 -n 2 >
0 ? It is when 0.1n > 1 E.g., n > 10 1.1 * g(n)-f(n) = 0.1n 3
-n 2 0.1n 3 -n 2 > 0 0.1n 3 > n 2 0.1n 3 /n 2 > 1 0.1n
> 1
Slide 8
Lower Order only? In fact, Its only the dominant term that
count Which one is dominating term? The one that grow faster Why?
Eventually, it is g*(n)/f*(n) If g(n) grows faster, g(n)/f*(n) >
some constant E.g, lim g(n)/f*(n) infinity The non- dominant term
The dominant term
Slide 9
What dominating what? nana n b (a > b) n log nn n 2 log nn
log 2 n cncn ncnc Log n1 nlog n Left side dominates
Slide 10
Putting into Practice What is the asymptotic class of 0.5n 3 +N
4 -5(n-3)(n-5)+n 3 log 8 n+25+n 1.5 (n-5)(n 2 +3)+log(n 20 ) 20n 5
+58n 4 +15n 3.2 *3n 2
Slide 11
Putting into Practice What is the asymptotic class of 0.5n 3 +N
4 -5(n-3)(n-5)+n 3 log 8 n+25+n 1.5 (n-5)(n 2 +3)+log(n 20 ) 20n 5
+58n 4 +15n 3.2 *3n 2 O(n 4 ) O(n 3 ) O(n 5.4 )
Slide 12
Asymptotic Notation from Program Flow Sequence Conditions Loops
Recursive Call
Slide 13
Sequence Block A Block B f (n) g (n) f(n) + g(n) = O(max
(f(n),g(n))
Slide 14
Example Block A Block B O(n) O(n 2 )
Slide 15
Example Block A Block B (n) O(n 2 )
Slide 16
Example Block A Block B (n) (n 2 )
Slide 17
Example Block A Block B O(n 2 ) (n 2 )
Slide 18
Condition Block ABlock B f (n)g (n) O(max (f(n),g(n))
Slide 19
Loops for (i = 1;i
Example : While loops While (n > 0) { n = n - 10; } While (n
> 0) { n = n - 10; } (n/10) = (n)
Slide 28
Example : While loops While (n > 0) { n = n / 2; } While (n
> 0) { n = n / 2; } (log n)
Slide 29
Example : Euclids GCD function gcd(a, b) { while (b > 0) {
tmp = b b = a mod b a = tmp } return a } function gcd(a, b) { while
(b > 0) { tmp = b b = a mod b a = tmp } return a }
Slide 30
Example : Euclids GCD function gcd(a, b) { while (b > 0) {
tmp = b b = a mod b a = tmp } return a } function gcd(a, b) { while
(b > 0) { tmp = b b = a mod b a = tmp } return a } How many
iteration? Compute mod and swap Until the modding one is zero
Slide 31
Example : Euclids GCD a b
Slide 32
a b a mod b If a > b a mod b < a / 2 If a > b a mod b
< a / 2
Slide 33
Case 1: b > a / 2 a b a mod b
Slide 34
Case 1: b a / 2 a b a mod b We can always put another b
Slide 35
Example : Euclids GCD function gcd(a, b) { while (b > 0) {
tmp = b b = a mod b a = tmp } return a } function gcd(a, b) { while
(b > 0) { tmp = b b = a mod b a = tmp } return a } B always
reduces at least half O( log n)