Algorithm Aalysis Kita perlu memproses jumlah data yag sagat besar. Harus diyakika bahwa program berheti dalam batas waktu yag wajar (reasoable) Tidak terikat pada programmig laguage atau bahka metolodologi (mis. Procedural vs OO) 19-Feb-04 IKI10100 - PM 4-1 Program Attributes Reliable code Easy modificatio Faster code Ease of use Total developmet time Total developmet cost Mea space ad time take 19-Feb-04 IKI10100 - PM 4-2 Algoritma Suatu set istruksi yag harus diikuti oleh computer utuk memecahka suatu masalah. Dega algorithm yag bear, perlu diketahui berapa resource yag terpakai spt: time & space yag diperluka oleh algorithm. Time Cosumptio Wall-Clock time CPU time Process 1: Process 2: Process 3: Wait time: 19-Feb-04 IKI10100 - PM 4-3 19-Feb-04 IKI10100 - PM 4-4
Algorithm s Fuctios Jumlah waktu yg dibutuhka hampir selalu tergatug pada jumlah iput yag diproses. Exact value depeds o: speed of host machie machie architecture quality of compilers Algorithm Aalysis Lagkah perhituga resource yag diperluka oleh sebuah algorithm. Tujua sesi ii: bagaimaa memperkiraka waktu yag dibutuhka oleh sebuah algoritma tekik utuk meuruka ruig time pedekata matematis utuk meggambarka ruig time 19-Feb-04 IKI10100 - PM 4-5 19-Feb-04 IKI10100 - PM 4-6 Cotoh Algoritma Mecari ilai terbesar dalam sebuah array yag berisi iteger positif it biggest ( it a[] ) { it temp = 0, = size_of_array; for (i=0; i<; i++) if (a[i] > temp) temp = a[i]; retur temp; ruig time liear thd jumlah iteger dalam array 19-Feb-04 IKI10100 - PM 4-7 Algorithm s Fuctios For a give program o a give computer, we ca plot the graph that represets the ruig time fuctio. Four commo fuctios: liear logarithmic quadratic cubic 19-Feb-04 IKI10100 - PM 4-8
Fuctio Curve Liear Sample Dowload time N kilobytes 2-sec delay for coectio setup speed of trasfer: 1.6 Kb/sec T(N) = N/1.6 + 2 N = 80 K T(N) = 52 sec N = 160K T(N) = 102 sec 19-Feb-04 IKI10100 - PM 4-9 19-Feb-04 IKI10100 - PM 4-10 Big-O Performace Measure O( expr ) Big-Oh T(N) is O(F(N)) if there are positive costat c ad N 0 such that T(N) cf(n) whe N N 0. Rule of Big-O reduce all costat factors ad terms to 1 10 2 + 9 + 120 + 400 2 + 1 + + 1 throw away all costat terms except oe 2 + 1 + + 1 2 + + 1 throw away all terms but the domiat oe 2 + + 1 2 19-Feb-04 IKI10100 - PM 4-11 19-Feb-04 IKI10100 - PM 4-12
Big Oh Big-Oh otatio suatu algoritma diyataka oleh suku yag domia (domiat term). 10 3 + 2 + 40 + 80 O( 3 ) 0.5 2 + log + 100000 O( 2 ) 15 + 4log + 10 3 O(log ) Domiat Term Megapa haya suku domiat saja yag diperhitugka? Cotoh: 3 + 350 2 + O( 3 ) Utuk = 10000 Nilai aktual 1,003,500,010,000 Nilai estimasi 1,000,000,000,000 Kesalaha dalam estimasi 0.35%, dapat diabaika 19-Feb-04 IKI10100 - PM 4-13 19-Feb-04 IKI10100 - PM 4-14 Domiat Term Utuk jumlah iput () yag besar, suku domia lebih megidikasika perilaku dari algoritma. Utuk yag kecil, umumya berjala sagat cepat sehigga kita tidak perlu perhatika. 19-Feb-04 IKI10100 - PM 4-15 Domiat Term Cotoh: Algoritma A = 0.5 3 + 20 + 10 Algoritma B = 1000 + 250 Executio time utuk = 10 Algoritma A = 710 Algortima B = 10250 Executio time utuk = 100 Algoritma A = 502,010 Algortima B = 100250 19-Feb-04 IKI10100 - PM 4-16
Domiat Term Megapa ilai kosta/koefisie pada setiap suku tidak diperhatika? Nilai kosta/koefisie tidak memiliki arti pada mesi yag berbeda Big-Oh diguaka utuk merepresetasika laju pertumbuha (growth rate) Rule of Big-O Jika ada lebih dari satu parameter, maka atura tersebut berlaku utuk setiap parameter. 4log(m) + 50 2 + 500m + 1853 O ( log(m) + 2 + m) 4 m log(m) + 50 2 + 500m + 853 O (m log(m) + 2 ) 19-Feb-04 IKI10100 - PM 4-17 19-Feb-04 IKI10100 - PM 4-18 Fuctio Curve Best, Average, Worst Case Big-Oh ruig time dari sequetial search: best, average ad worst? Mecari idex dari suatu eleme dalam sebuah array size. it SeqSearch ( it k, Array a ) { it i; for (i = 0; i<; i++) if (k == a[ i ]) retur i; retur ( -1 ); 19-Feb-04 IKI10100 - PM 4-19 19-Feb-04 IKI10100 - PM 4-20
Best ad Worst Case it SeqSearch ( it k, Array a ) { it i; for (i = 0; i<; i++) if (k == a[ i ]) retur i; retur ( -1 ); Best case: O( 1 ) Worst case: O( ) Average Case it SeqSearch ( it k, it a[ ] ) { it i; b for (i = 0; i<; i++) a if (k == a[ i ]) retur i; retur ( -1 ); Total computatio time utuk semua kemugkia posisi k ditemuka dibagi oleh jumlah kemugkia. Σ (ai + b) = a Σ i + Σ b 19-Feb-04 IKI10100 - PM 4-21 19-Feb-04 IKI10100 - PM 4-22 Average Case Total computatio time utuk semua kemugkia posisi k ditemuka dibagi oleh jumlah kemugkia. Total = Σ (ai + b) = a Σ i + Σ b = a (+1)/2 + b Examples of Algorithm Ruig Times Jarak Terdekat dari Dua Titik Algortima utuk meemuka sepasag dari N titik dalam suatu bidag (2 dimesi) yag memiliki jarak terpedek dibadig pasaga titik yag lai. ( + 1) Average = Total / = a + b 2 a a = + + b 2 2 O( ) N (N -1) / 2 O( 2 ) 19-Feb-04 IKI10100 - PM 4-23 19-Feb-04 IKI10100 - PM 4-24
Examples of Algorithm Ruig Times Tiga Titik Segaris Algortima utuk meemuka tiga yag segaris dari N titik dalam suatu bidag (2 dimesi). Tugas Baca 4 versi (cubic, quadratic, liear, logarithmic) dari Maximum Cotiguous Subsequece Sum Problem 4-3 7 10-6 8 9 5-3 -1 N (N - 1) (N - 2) / 6 O( 3 ) start ed 19-Feb-04 IKI10100 - PM 4-25 Maximum sum 19-Feb-04 IKI10100 - PM 4-26