DAA JK - Asymptotic Notation - Minggu 4_16

32
Design and Analysis Algorithm Imam Cholissodin, S.Si., M.Kom Pertemuan 04

description

TI

Transcript of DAA JK - Asymptotic Notation - Minggu 4_16

DAA JK - Asymptotic Notation

Design and Analysis Algorithm

Imam Cholissodin, S.Si., M.KomPertemuan 04Contents2Asymptotic Notation

2Asymptotic NotationThink 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.3Asymptotic NotationMisalkan:T(n) = 5n2 + 6n + 25T(n) proporsional untuk ordo n2 untuk data yang sangat besar.

4Memperkirakan formula untuk run-timeIndikasi kinerja algoritma (untuk jumlah data yang sangat besar)

Asymptotic NotationIndikator efisiensi algoritma berdasar pada OoG pada basic operation suatu algoritma. Penggunaan notasi sebagai pembanding urutan OoG:O (big oh) (big omega) (big theta)t(n) : algoritma running time (diindikasikan dengan basic operation count (C(n))g(n) : simple function to compare the count5Classifying functions by theirAsymptotic Growth Rates (1/2)asymptotic growth rate, asymptotic order, or order of functions Comparing and classifying functions that ignores constant factors and small inputs.

O(g(n)), Big-Oh of g of n, the Asymptotic Upper Bound;W(g(n)), Omega of g of n, the Asymptotic Lower Bound.Q(g(n)), Theta of g of n, the Asymptotic Tight Bound; and

ExampleExample: 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-OhThe 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

Upper Bound NotationWe say Insertion Sorts run time is O(n2)Properly we should say run time is in O(n2)Read O as Big-O (youll also hear it as order)

In general a functionf(n) is O(g(n)) if positive constants c and n0 such that f(n) c g(n) n n0

e.g. if f(n)=1000n and g(n)=n2, n0 > 1000 and c = 1 then f(n0) < 1.g(n0) and we say that f(n) = O(g(n))

Asymptotic Upper Boundf(n)g(n)c g(n) f(n) c g(n) for all n n0 g(n) is called an asymptotic upper bound of f(n). We write f(n)=O(g(n)) It reads f(n) is big oh of g(n).

n011We use O-notation to give an upper bound of a function, to within a constant factor.Big-Oh, the Asymptotic Upper BoundThis is the most popular notation for run time since we're usually looking for worst case time. If Running Time of Algorithm X is O(n2) , then for any input the running time of algorithm X is at most a quadratic function, for sufficiently large n.

e.g. 2n2 = O(n3) .From the definition using c = 1 and n0 = 2. O(n2) is tighter than O(n3).Example of Asymptotic Upper Boundf(n)=3n2+5g(n)=n24g(n)=4n24 g(n) = 4n2 = 3n2 + n2 3n2 + 9 for all n 3 > 3n2 + 5 = f(n)Thus, f(n)=O(g(n)).

313There could be many pairs of c,n0 which leads to f(n)=O(g(n))Exercise on O-notationShow that 3n2+2n+5 = O(n2)

10 n2 = 3n2 + 2n2 + 5n2 3n2 + 2n + 5 for n 1

c = 10, n0 = 1Tugas 1 : O-notationf1(n) = 10 n + 25 n2f2(n) = 20 n log n + 5 nf3(n) = 12 n log n + 0.05 n2f4(n) = n1/2 + 3 n log n

O(n2)O(n log n)O(n2) O(n log n)

15For each function, find the simplest and g(n) such that f_i(n) = O(g(n))Classification of Function : BIG O (1/2)A function f(n) is said to be of at most logarithmic growth if f(n) = O(log n)A function f(n) is said to be of at most quadratic growth if f(n) = O(n2)A function f(n) is said to be of at most polynomial growth if f(n) = O(nk), for some natural number k > 1A function f(n) is said to be of at most exponential growth if there is a constant c, such that f(n) = O(cn), and c > 1A function f(n) is said to be of at most factorial growth if f(n) = O(n!).Classification of Function : BIG O (2/2)A function f(n) is said to have constant running time if the size of the input n has no effect on the running time of the algorithm (e.g., assignment of a value to a variable). The equation for this algorithm is f(n) = cOther logarithmic classifications: f(n) = O(n log n)f(n) = O(log log n)Tugas 2Menghitung kompleksitas pada FaktorialFunction Faktorial (input n : integer) integer {menghasilkan nilai n!, n 0} Algoritma If n=0 then Return 1Else Return n*faktorial (n-1) EndifKompleksitas 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)

Lower Bound NotationIn general a functionf(n) is (g(n)) if positive constants c and n0 such that 0 cg(n) f(n) n n0 f(n) c g(n) for all n n0 g(n) is called an asymptotic lower bound of f(n). We write f(n)=(g(n)) It reads f(n) is omega of g(n).

Example of Asymptotic Lower Boundf(n)=n2/2-7c g(n)=n2/4g(n)=n2g(n)/4 = n2/4 = n2/2 n2/4 n2/2 9 for all n 6 < n2/2 7Thus, f(n)= (g(n)).

6g(n)=n220There could be many pairs of c,n0 which leads to f(n)=O(g(n))Example: Big OmegaExample: n 1/2 = W( log n) . Use the definition with c = 1 and n0 = 16.

Checks OK.Let n 16 : n 1/2 (1) log n if and only if n = ( log n )2 by squaring both sides.This is an example of polynomial vs. log.Big Theta NotationDefinition: Two functions f and g are said to be of equal growth, f = Big Theta(g) if and only if both f=Q(g) and g = Q(f).

Definition: f(n) = Q(g(n)) means positive constants c1, c2, and n0 such that c1 g(n) f(n) c2 g(n) n n0

If f(n) = O(g(n)) and f(n) = W(g(n)) then f(n) = Q(g(n)) (e.g. f(n) = n2 and g(n) = 2n2)Theta, the Asymptotic Tight BoundTheta means that f is bounded above and below by g; BigTheta implies the "best fit".f(n) does not have to be linear itself in order to be of linear growth; it just has to be between two linear functions,Asymptotically Tight Boundf(n)c1 g(n) f(n) = O(g(n)) and f(n) = (g(n)) g(n) is called an asymptotically tight bound of f(n). We write f(n)=(g(n)) It reads f(n) is theta of g(n).

n0c2 g(n)24We use O-notation to give an upper bound of a function, to within a constant factor.Other Asymptotic NotationsA function f(n) is o(g(n)) if positive constants c and n0 such that f(n) < c g(n) n n0A function f(n) is (g(n)) if positive constants c and n0 such that c g(n) < f(n) n n0Intuitively,o() is like < O() is like () is like > () is like () is like =Examples1. 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)Examples (cont.)3. Suppose a program P is O(n3), and a program Q is O(3n), and that currently both can solve problems of size 50 in 1 hour. If the programs are run on another system that executes exactly 729 times as fast as the original system, what size problems will they be able to solve?Example (cont.)n3 = 503 * 729 3n = 350 * 729n = n = log3 (729 * 350)n = n = log3(729) + log3 350n = 50 * 9 n = 6 + log3 350n = 50 * 9 = 450 n = 6 + 50 = 56

Improvement: problem size increased by 9 times for n3 algorithm but only a slight improvement in problem size (+6) for exponential algorithm.

Tugas 30.5n2 - 5n + 2 = ( n2). (True or False)Let c = 0.25 and n0 = 25.

(b) 0.5 n2 - 5n + 2 = O( n2). (True or False)Let c = 0.5 and n0 = 1.

(c) 0.5 n2 - 5n + 2 = ( n2) (True or False)from (a) and (b) above. Use n0 = 25, c1 = 0.25, c2 = 0.5 in the definition.Practical Complexity t < 250

Practical Complexity t < 500

Practical Complexity t < 1000

Practical Complexity t < 5000

Click to edit subtitle styleThank You !Chart310011221248431.58496250074.7548875022927842816641652.321928094911.6096404744251253262.584962500715.5097750043362166472.807354922119.65148445444934312883246451225693.169925001428.52932501381729512103.321928094933.219280948910010001024113.459431618638.05374780512113312048123.584962500743.019550008714417284096133.700439718148.105716335816921978192143.807354922153.3029689088196274416384153.906890595658.603358934122533753276816464256409665536174.087462841369.48686830132894913131072184.169925001475.0586500263245832262144194.247927513480.71062275543616859524288204.321928094986.438561897740080001048576

f(n) = nf(n) = log(n)f(n) = n log(n)f(n) = n^2f(n) = n^3f(n) = 2^n

Chart111100224812439271.58496250074.754887502284166428165251252.321928094911.6096404744326362162.584962500715.5097750043647493432.807354922119.65148445441288645123242569817293.169925001428.5293250135121010010003.321928094933.219280948910241112113313.459431618638.05374780520481214417283.584962500743.019550008740961316921973.700439718148.105716335881921419627443.807354922153.3029689088163841522533753.906890595658.603358934132768162564096464655361728949134.087462841369.48686830131310721832458324.169925001475.0586500262621441936168594.247927513480.71062275545242882040080004.321928094986.43856189771048576

Sheet111001122212484331.58496250074.754887502292784428166416552.321928094911.60964047442512532662.584962500715.50977500433621664772.807354922119.6514844544493431288832464512256993.169925001428.5293250138172951210103.321928094933.21928094891001000102411113.459431618638.0537478051211331204812123.584962500743.01955000871441728409613133.700439718148.10571633581692197819214143.807354922153.302968908819627441638415153.906890595658.6033589341225337532768161646425640966553617174.087462841369.4868683013289491313107218184.169925001475.058650026324583226214419194.247927513480.7106227554361685952428820204.321928094986.438561897740080001048576series1series4series5series2series3series6log xx log xx*xx*x*x2^x

Sheet110011221248431.58496250074.7548875022927842816641652.321928094911.6096404744251253262.584962500715.5097750043362166472.807354922119.65148445444934312883246451225693.169925001428.52932501381729512103.321928094933.219280948910010001024113.459431618638.05374780512113312048123.584962500743.019550008714417284096133.700439718148.105716335816921978192143.807354922153.3029689088196274416384153.906890595658.603358934122533753276816464256409665536174.087462841369.48686830132894913131072184.169925001475.0586500263245832262144194.247927513480.71062275543616859524288204.321928094986.438561897740080001048576

Sheet2

Sheet3

Chart310011221248431.58496250074.7548875022927842816641652.321928094911.6096404744251253262.584962500715.5097750043362166472.807354922119.65148445444934312883246451225693.169925001428.52932501381729512103.321928094933.219280948910010001024113.459431618638.05374780512113312048123.584962500743.019550008714417284096133.700439718148.105716335816921978192143.807354922153.3029689088196274416384153.906890595658.603358934122533753276816464256409665536174.087462841369.48686830132894913131072184.169925001475.0586500263245832262144194.247927513480.71062275543616859524288204.321928094986.438561897740080001048576

f(n) = nf(n) = log(n)f(n) = n log(n)f(n) = n^2f(n) = n^3f(n) = 2^n

Chart111100224812439271.58496250074.754887502284166428165251252.321928094911.6096404744326362162.584962500715.5097750043647493432.807354922119.65148445441288645123242569817293.169925001428.5293250135121010010003.321928094933.219280948910241112113313.459431618638.05374780520481214417283.584962500743.019550008740961316921973.700439718148.105716335881921419627443.807354922153.3029689088163841522533753.906890595658.603358934132768162564096464655361728949134.087462841369.48686830131310721832458324.169925001475.0586500262621441936168594.247927513480.71062275545242882040080004.321928094986.43856189771048576

Sheet111001122212484331.58496250074.754887502292784428166416552.321928094911.60964047442512532662.584962500715.50977500433621664772.807354922119.6514844544493431288832464512256993.169925001428.5293250138172951210103.321928094933.21928094891001000102411113.459431618638.0537478051211331204812123.584962500743.01955000871441728409613133.700439718148.10571633581692197819214143.807354922153.302968908819627441638415153.906890595658.6033589341225337532768161646425640966553617174.087462841369.4868683013289491313107218184.169925001475.058650026324583226214419194.247927513480.7106227554361685952428820204.321928094986.438561897740080001048576series1series4series5series2series3series6log xx log xx*xx*x*x2^x

Sheet110011221248431.58496250074.7548875022927842816641652.321928094911.6096404744251253262.584962500715.5097750043362166472.807354922119.65148445444934312883246451225693.169925001428.52932501381729512103.321928094933.219280948910010001024113.459431618638.05374780512113312048123.584962500743.019550008714417284096133.700439718148.105716335816921978192143.807354922153.3029689088196274416384153.906890595658.603358934122533753276816464256409665536174.087462841369.48686830132894913131072184.169925001475.0586500263245832262144194.247927513480.71062275543616859524288204.321928094986.438561897740080001048576

Sheet2

Sheet3

Chart310011221248431.58496250074.7548875022927842816641652.321928094911.6096404744251253262.584962500715.5097750043362166472.807354922119.65148445444934312883246451225693.169925001428.52932501381729512103.321928094933.219280948910010001024113.459431618638.05374780512113312048123.584962500743.019550008714417284096133.700439718148.105716335816921978192143.807354922153.3029689088196274416384153.906890595658.603358934122533753276816464256409665536174.087462841369.48686830132894913131072184.169925001475.0586500263245832262144194.247927513480.71062275543616859524288204.321928094986.438561897740080001048576

f(n) = nf(n) = log(n)f(n) = n log(n)f(n) = n^2f(n) = n^3f(n) = 2^n

Chart111100224812439271.58496250074.754887502284166428165251252.321928094911.6096404744326362162.584962500715.5097750043647493432.807354922119.65148445441288645123242569817293.169925001428.5293250135121010010003.321928094933.219280948910241112113313.459431618638.05374780520481214417283.584962500743.019550008740961316921973.700439718148.105716335881921419627443.807354922153.3029689088163841522533753.906890595658.603358934132768162564096464655361728949134.087462841369.48686830131310721832458324.169925001475.0586500262621441936168594.247927513480.71062275545242882040080004.321928094986.43856189771048576

Sheet111001122212484331.58496250074.754887502292784428166416552.321928094911.60964047442512532662.584962500715.50977500433621664772.807354922119.6514844544493431288832464512256993.169925001428.5293250138172951210103.321928094933.21928094891001000102411113.459431618638.0537478051211331204812123.584962500743.01955000871441728409613133.700439718148.10571633581692197819214143.807354922153.302968908819627441638415153.906890595658.6033589341225337532768161646425640966553617174.087462841369.4868683013289491313107218184.169925001475.058650026324583226214419194.247927513480.7106227554361685952428820204.321928094986.438561897740080001048576series1series4series5series2series3series6log xx log xx*xx*x*x2^x

Sheet110011221248431.58496250074.7548875022927842816641652.321928094911.6096404744251253262.584962500715.5097750043362166472.807354922119.65148445444934312883246451225693.169925001428.52932501381729512103.321928094933.219280948910010001024113.459431618638.05374780512113312048123.584962500743.019550008714417284096133.700439718148.105716335816921978192143.807354922153.3029689088196274416384153.906890595658.603358934122533753276816464256409665536174.087462841369.48686830132894913131072184.169925001475.0586500263245832262144194.247927513480.71062275543616859524288204.321928094986.438561897740080001048576

Sheet2

Sheet3

Chart310011221248431.58496250074.7548875022927842816641652.321928094911.6096404744251253262.584962500715.5097750043362166472.807354922119.65148445444934312883246451225693.169925001428.52932501381729512103.321928094933.219280948910010001024113.459431618638.05374780512113312048123.584962500743.019550008714417284096133.700439718148.105716335816921978192143.807354922153.3029689088196274416384153.906890595658.603358934122533753276816464256409665536174.087462841369.48686830132894913131072184.169925001475.0586500263245832262144194.247927513480.71062275543616859524288204.321928094986.438561897740080001048576

f(n) = nf(n) = log(n)f(n) = n log(n)f(n) = n^2f(n) = n^3f(n) = 2^n

Chart111100224812439271.58496250074.754887502284166428165251252.321928094911.6096404744326362162.584962500715.5097750043647493432.807354922119.65148445441288645123242569817293.169925001428.5293250135121010010003.321928094933.219280948910241112113313.459431618638.05374780520481214417283.584962500743.019550008740961316921973.700439718148.105716335881921419627443.807354922153.3029689088163841522533753.906890595658.603358934132768162564096464655361728949134.087462841369.48686830131310721832458324.169925001475.0586500262621441936168594.247927513480.71062275545242882040080004.321928094986.43856189771048576

Sheet111001122212484331.58496250074.754887502292784428166416552.321928094911.60964047442512532662.584962500715.50977500433621664772.807354922119.6514844544493431288832464512256993.169925001428.5293250138172951210103.321928094933.21928094891001000102411113.459431618638.0537478051211331204812123.584962500743.01955000871441728409613133.700439718148.10571633581692197819214143.807354922153.302968908819627441638415153.906890595658.6033589341225337532768161646425640966553617174.087462841369.4868683013289491313107218184.169925001475.058650026324583226214419194.247927513480.7106227554361685952428820204.321928094986.438561897740080001048576series1series4series5series2series3series6log xx log xx*xx*x*x2^x

Sheet110011221248431.58496250074.7548875022927842816641652.321928094911.6096404744251253262.584962500715.5097750043362166472.807354922119.65148445444934312883246451225693.169925001428.52932501381729512103.321928094933.219280948910010001024113.459431618638.05374780512113312048123.584962500743.019550008714417284096133.700439718148.105716335816921978192143.807354922153.3029689088196274416384153.906890595658.603358934122533753276816464256409665536174.087462841369.48686830132894913131072184.169925001475.0586500263245832262144194.247927513480.71062275543616859524288204.321928094986.438561897740080001048576

Sheet2

Sheet3