Академический Документы
Профессиональный Документы
Культура Документы
Drs. Achmad Ridok M.Kom Fitra A. Bachtiar, S.T., M. Eng Imam Cholissodin, S.Si., M.Kom Aryo Pinandito, MT
Pertemuan 04
Contents
Asymptotic Notation
2
Asymptotic Notation
Think of n as the number of records we wish to
sort with an algorithm that takes f(n) to run. How long will it take to sort n records? What if n is big? We are interested in the range of a function as n gets large. Will f stay bounded? Will f grow linearly? Will f grow exponentially? Our goal is to find out just how fast f grows with respect to n.
Asymptotic Notation
Memperkirakan formula untuk run-time
Misalkan:
T(n) = 5n2 + 6n + 25 T(n) proporsional untuk ordo n2 untuk data yang sangat besar.
Asymptotic Notation
Indikator efisiensi algoritma bedasar pada OoG
pada basic operation suatu algoritma. Penggunaan notasi sebagai pembanding urutan OoG:
O (big oh) (big omega) (big theta)
Example
Example: f(n) = n2 - 5n + 13. The constant 13 doesn't change as n grows,
so it is not crucial. The low order term, -5n, doesn't have much effect on f compared to the quadratic term, n2. We will show that f(n) = Q(n2) .
Q: What does it mean to say f(n) = Q(g(n)) ? A: Intuitively, it means that function f is the
same order of magnitude as g.
Example (cont.)
Q: What does it mean to say f1(n) = Q(1)? A: f1(n) = Q(1) means after a few n, f1 is
bounded above & below by a constant. Q: What does it mean to say f2(n) = Q(n log n)? A: f2(n) = Q(n log n) means that after a few n, f2 is bounded above and below by a constant times n log n. In other words, f2 is the same order of magnitude as n log n. More generally, f(n) = Q(g(n)) means that f(n) is a member of Q(g(n)) where Q(g(n)) is a set of functions of the same order of magnitude.
Big-Oh
The O symbol was introduced in 1927 to
indicate relative growth of two functions based on asymptotic behavior of the functions now used to classify functions and families of functions
In general a function
f(n) is O(g(n)) if positive constants c and n0 such that f(n) c g(n) n n0
f(n)
g(n)
n0
Example 1
g(n) for all n>6, g(n) > 1 f(n). Thus the function f is in the big-O of g. that is, f(n) in O(g(n)).
f(n)
Example 2
g(n) There exists a n0=5 s.t. for all n>n0, f(n) < 1 g(n). Thus, f(n) is in O(g(n)).
f(n)
Example 3
3.5 h(n)
There exists a n0=5, c=3.5, s.t. for all n>n0, f(n) < c h(n). Thus, f(n) is in O(h(n)).
f(n) h(n)
f(n)=3n2+5
g(n)=n2
Exercise on O-notation
Show that 3n2+2n+5 = O(n2)
10 n2 = 3n2 + 2n2 + 5n2 3n2 + 2n + 5 for n 1
c = 10, n0 = 1
Tugas 1 : O-notation
f1(n) = 10 n + 25 f2(n) = 20 n log n + 5 n f3(n) = 12 n log n + 0.05 n2 f4(n) = n1/2 + 3 n log n n2 O(n2) O(n log n) O(n2) O(n log n)
Review Tugas n!
Kompleksitas waktu :
untuk kasus basis, tidak ada operasi perkalian (0) untuk kasus rekurens, kompleksitas waktu diukur dari jumlah perkalian (1) ditambah kompleksitas waktu untuk faktorial (n-1)
Review Tugas n! (Lanjutan) Kompleksitas waktu n! : T(n)=1+T(n-1) =T(n)=1+1+T(n-2)=2+T(n-2) =T(n)=2+1+T(n-3)=3+T(n-3) = = = n+T(0) =n+0 Jadi T(n) = n T(n) O(n)
Proof:
Suppose run time is an + b
Assume a and b are positive (what if b is negative?)
an an + b
f(n)
c g(n)
n0
g(n)/4 = n2/4 = n2/2 n2/4 n2/2 9 for all n 6 < n2/2 7 Thus, f(n)= W(g(n)).
f(n)=n2/2-7
c g(n)=n2/4
f(n) c1 g(n)
n0
Examples
1. 2n3 + 3n2 + n = 2n3 + 3n2 + O(n) = 2n3 + O( n2 + n) = 2n3 + O( n2 ) = O(n3 ) = O(n4)
2. 2n3 + 3n2 + n = 2n3 + 3n2 + O(n) = 2n3 + Q(n2 + n) = 2n3 + Q(n2) = Q(n3)
Example (cont.)
n3 = 503 * 729 n = 3 503 * 729 n = 3 503 3 729 n = 50 * 9 n = 50 * 9 = 450 3n = 350 * 729 n = log3 (729 * 350) n = log3(729) + log3 350 n = 6 + log3 350 n = 6 + 50 = 56
More Examples
(a) 0.5n2 - 5n + 2 = W( n2). Let c = 0.25 and n0 = 25. 0.5 n2 - 5n + 2 = 0.25( n2) for all n = 25 (b) 0.5 n2 - 5n + 2 = O( n2). Let c = 0.5 and n0 = 1. 0.5( n2) = 0.5 n2 - 5n + 2 for all n = 1 (c) 0.5 n2 - 5n + 2 = Q( n2) from (a) and (b) above. Use n0 = 25, c1 = 0.25, c2 = 0.5 in the definition.
More Examples
(d) 6 * 2n + n2 = O(2n). Let c = 7 and n0 = 4. Note that 2n = n2 for n = 4. Not a tight upper bound, but it's true. (e) 10 n2 + 2 = O(n4). There's nothing wrong with this, but usually we try to get the closest g(n). Better is to use O(n2 ).
f(n) = n f(n) = log(n) f(n) = n log(n) f(n) = n^2 f(n) = n^3 f(n) = 2^n
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
f(n) = n f(n) = log(n) f(n) = n log(n) f(n) = n^2 f(n) = n^3 f(n) = 2^n
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
f(n) = n f(n) = log(n) f(n) = n log(n) f(n) = n^2 f(n) = n^3 f(n) = 2^n
0 1 3 5 7 9 11 13 15 17 19
4000 f(n) = n 3000 f(n) = log(n) f(n) = n log(n) f(n) = n^2 2000 f(n) = n^3 f(n) = 2^n 1000
0 1 3 5 7 9 11 13 15 17 19