34
Analisa Kompleksitas Algoritma Sunu Wibirama

Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Embed Size (px)

Citation preview

Page 1: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Analisa Kompleksitas AlgoritmaSunu Wibirama

Page 2: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Referensi✦ Cormen, T.H., Leiserson, C.E., Rivest,

R.L., Stein, C., Introduction to Algorithms 2nd Edition, Massachusetts: MIT Press, 2002

✦ Sedgewick, R., Algorithms in C++ Part 1-4, Massachusetts: Addison-Wesley Publisher, 1998

✦ Video lecture MIT Opencourseware ke-1: Introduction

✦ Video lecture IIT Kharagpur, India ke-18: Complexity of Algorithm

Page 3: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Video lecture MIT Opencourseware ke-1:

Introduction

Video lecture IIT Kharagpur, India ke-18: Complexity of Algorithm

Page 4: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Agenda Hari Ini

• Pentingnya Analisa Algoritma

• Prinsip Perbandingan Algoritma

• Dasar-dasar Matematika dan Teori Big-O

• Contoh Implementasi

• Kesimpulan

Page 5: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

• Kehandalan dalam menyelesaikan masalah (robustness)

• Fungsionalitas (functionality)

• Tampilan grafis (user interface)

• Daya tahan (reliability)

• Keamanan (security)

• Kesederhanaan (simplicity)

• Kemudahan dalam penggunaan (user friendly)

• Kemudahan dalam pemeliharaan (maintainability)

Apa yang pertama kali Anda menjadi pertimbangan Anda saat membeli komputer,

selain PERFORMA?

Page 6: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Algoritma dan Performa

• Hal-hal yang menjadi pertimbangan utama Anda tidak muncul dengan gratis

• Performa sistem menjadi alat tukar seperti uang.

• Algoritma program memegang peran kunci

• Algoritma adalah teknologi, engineered, sebagaimana perangkat keras komputer

Page 7: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Pentingnya Analisa Algoritma• Algoritma membantu kita memahami skalabilitas

program kita

• Performa terkadang menjadi pembeda antara yang mungkin dilakukan dan yang tidak mungkin dilakukan

• Analisa algoritma memberi gambaran informasi tentang ‘perilaku program’ kita

• Mempelajari bagaimana menerapkan algoritma yang baik untuk kasus tertentu membedakan profesi system analyst dan programmer

Page 8: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Prinsip Perbandingan Algoritma

• Apa yang membuat sebuah algoritma dikatakan LEBIH BAIK dari algoritma yang lain?

• Kompleksitas waktu (Time Complexity)

• Kompleksitas ruang (Space Complexity)

• Kecenderungan saat ini:

• ruang (hard disk) semakin murah

• kapasitas data yang diproses semakin besar

• waktu pemrosesan harus semakin cepat

• Kompleksitas waktu menjadi variabel yang sangat penting

Page 9: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Penyebab variasi pada hasil analisa algoritma

• Program aras tinggi diterjemahkan ke bahasa mesin. Setiap tipe prosesor memiliki prosedur bahasa mesin yang berbeda

• Aplikasi dijalankan di shared environment, sehingga terpengaruh oleh penggunaan memori

• Program sangat sensitif terhadap masukan dan akan menunjukkan performa yang jauh berbeda untuk rentang masukan yang tidak terlalu berbeda

• Program tidak dipahami dengan baik, sehingga analisa matematika kurang merepresentasikan kondisi yang sesungguhnya

• Program tersebut memang tidak bisa dibandingkan dengan program yang lain karena hanya optimal untuk input-input tertentu

Page 10: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Hal-hal yang perlu diperhatikan pada analisa algoritma

• Memisahkan operasi pada tingkat abstraksi dan implementasi. Contoh: menghitung jumlah instruksi scanf pada program lebih diprioritaskan daripada memahami berapa nanoseconds instruksi scanf dieksekusi

• Mengidentifikasi data masukan:- Strategi average dan worst case- Strategi random dan biggest data

Page 11: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Dasar Matematika & Teori Big-O

• Sebagian besar algoritma memiliki parameter primer N yang sangat mempengaruhi waktu eksekusi

• Parameter N bisa berupa:- derajat polinomial- ukuran berkas (file) yang diproses- jumlah karakter pada text string- ukuran data yang diproses

• Pengukuran kompleksitas: Big-O

Page 12: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Teori Big-O

O(g(N )) = { f (N ) : jika terdapat konstanta positif c dan N0 , sehingga 0 ≤ f (N ) ≤ cg(N ) untuk semua N ≥ N0}

Teorema Matematika:

Engineering:

• Hilangkan orde yang lebih rendah dan konstanta. Gunakan hanya orde tertinggi pada polinomialContoh: 3n3 + 90n2 − 5n + 6046 = O(N 3)

3.1 Asymptotic notation 43

(b) (c)(a)

nnnn0n0n0 f (n) = !(g(n)) f (n) = O(g(n)) f (n) = "(g(n))

f (n)

f (n)f (n)

cg(n)

cg(n)

c1g(n)

c2g(n)

Figure 3.1 Graphic examples of the !, O, and " notations. In each part, the value of n0 shown isthe minimum possible value; any greater value would also work. (a) !-notation bounds a function towithin constant factors. We write f (n) = !(g(n)) if there exist positive constants n0, c1, and c2 suchthat to the right of n0, the value of f (n) always lies between c1g(n) and c2g(n) inclusive. (b) O-notation gives an upper bound for a function to within a constant factor. We write f (n) = O(g(n))

if there are positive constants n0 and c such that to the right of n0, the value of f (n) always lies onor below cg(n). (c) "-notation gives a lower bound for a function to within a constant factor. Wewrite f (n) = "(g(n)) if there are positive constants n0 and c such that to the right of n0, the valueof f (n) always lies on or above cg(n).

c1n2 ! 12

n2 " 3n ! c2n2

for all n # n0. Dividing by n2 yields

c1 ! 12

" 3n

! c2 .

The right-hand inequality can be made to hold for any value of n # 1 by choosingc2 # 1/2. Likewise, the left-hand inequality can be made to hold for any valueof n # 7 by choosing c1 ! 1/14. Thus, by choosing c1 = 1/14, c2 = 1/2, andn0 = 7, we can verify that 1

2 n2 " 3n = !(n2). Certainly, other choices for theconstants exist, but the important thing is that some choice exists. Note that theseconstants depend on the function 1

2n2 "3n; a different function belonging to !(n2)would usually require different constants.

We can also use the formal definition to verify that 6n3 $= !(n2). Suppose forthe purpose of contradiction that c2 and n0 exist such that 6n3 ! c2n2 for all n # n0.But then n ! c2/6, which cannot possibly hold for arbitrarily large n, since c2 isconstant.

Intuitively, the lower-order terms of an asymptotically positive function can beignored in determining asymptotically tight bounds because they are insignificantfor large n. A tiny fraction of the highest-order term is enough to dominate the

Page 13: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Macam-macam Parameter N

1Sebagian besar instruksi dieksekusi satu kali atau dalam

jumlah yang tidak terlalu banyak (waktu eksekusi konstan)

Pertumbuhan waktu eksekusi program tidak terlalu cepat. Waktu eksekusi ini terdapat pada program yang

memecahkan masalah dengan kapasitas yang cukup besar, dipecah-pecah menjadi beberapa bagian

N Waktu eksekusi program linier. Sebagian besar masukan diproses dalam jumlah yang tidak terlalu banyak

Waktu eksekusi ini terdapat pada program yang memecahkan masalah menjadi beberapa bagian,

menyelesaikannya secara terpisah, kemudian menggabungkannya kembali

logN

N logN

Page 14: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Macam-macam Parameter N (cont’d)

Biasanya digunakan untuk memecahkan masalah dalam jumlah kecil. Biasanya terdapat pada program yang

memproses pasangan data (quadratic) atau array dua dimensi secara bersamaan (double-nested loop)

Biasanya digunakan untuk memecahkan masalah dalam jumlah kecil. Biasanya terdapat pada program yang

memproses tiga buah data (cubic) atau array tiga dimensi secara bersamaan (triple-nested loop)

Waktu eksekusi program linier. Sebagian besar masukan diproses dalam jumlah yang tidak terlalu banyak

N 2

N 3

2N

Page 15: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Beberapa perbandingan kompleksitas algoritma

Figure 1-1 shows how the various measures of complexity compare with one another. The horizontalaxis represents the size of the problem — for example, the number of records to process in a search algo-rithm. The vertical axis represents the computational effort required by algorithms of each class. This isnot indicative of the running time or the CPU cycles consumed; it merely gives an indication of how thecomputational resources will increase as the size of the problem to be solved increases.

Figure 1-1: Comparison of different orders of complexity.

Referring back at the list, you may have noticed that none of the orders contain constants. That is, if analgorithm’s expected runtime performance is proportional to N, 2!N, 3!N, or even 100!N, in all casesthe complexity is defined as being O(N). This may seem a little strange at first — surely 2!N is better than100!N— but as mentioned earlier, the aim is not to determine the exact number of operations but ratherto provide a means of comparing different algorithms for relative efficiency. In other words, an algo-rithm that runs in O(N) time will generally outperform another algorithm that runs in O(N2). Moreover,when dealing with large values of N, constants make less of a difference: As a ration of the overall size,the difference between 1,000,000,000 and 20,000,000,000 is almost insignificant even though one is actu-ally 20 times bigger.

Of course, at some point you will want to compare the actual performance of different algorithms, espe-cially if one takes 20 minutes to run and the other 3 hours, even if both are O(N). The thing to remember,however, is that it’s usually much easier to halve the time of an algorithm that is O(N) than it is tochange an algorithm that’s inherently O(N2) to one that is O(N).

O(N!)

O(N2)

O(N log N)

O(log N)

O(N)

O(1)

5

Getting Started

04_596748 ch01_2.qxd 11/12/07 4:06 PM Page 5

Running Time(seconds)

Input size (N)

Page 16: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Formula Kondisi Periodik

• Sebagian besar algoritma terdiri dari proses penyelesaian masalah dengan memanfaatkan perulangan (rekursif)

• Bagian yang berulang tersebut secara tidak langsung berkontribusi pada kompleksitas sebuah algoritma

• Perlu mengetahui formula-formula dasar untuk kondisi periodik

Page 17: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Formula Kondisi Periodik • Formula 1: kondisi periodik muncul pada program

rekursif yang mengeliminasi satu item input

Sedgewick, R., Algorithms in C++ Part 1-4, Massachusetts: Addison-Wesley Publisher, 1998

Page 18: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Formula Kondisi Periodik • Formula 2: kondisi periodik muncul pada program rekursif yang

membagi input menjadi dua bagian hanya dengan satu langkah

Sedgewick, R., Algorithms in C++ Part 1-4, Massachusetts: Addison-Wesley Publisher, 1998

Page 19: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Formula Kondisi Periodik • Formula 3: kondisi periodik muncul pada program rekursif yang

membagi input menjadi dua bagian, namun perlu memeriksa tiap-tiap input

Sedgewick, R., Algorithms in C++ Part 1-4, Massachusetts: Addison-Wesley Publisher, 1998

Page 20: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Formula Kondisi Periodik • Formula 4: kondisi periodik muncul pada program rekursif yang

mengolah input secara linear sebelum, pada saat, dan sesudah membagi input menjadi dua bagian

Sedgewick, R., Algorithms in C++ Part 1-4, Massachusetts: Addison-Wesley Publisher, 1998

Page 21: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Formula Kondisi Periodik • Formula 5: kondisi periodik muncul pada program rekursif yang

membagi input menjadi dua bagian, kemudian mengerjakan input lain dengan kapasitas konstan

Sedgewick, R., Algorithms in C++ Part 1-4, Massachusetts: Addison-Wesley Publisher, 1998

Page 22: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Jenis analisa waktu eksekusi

• Strategi Worst case (umum digunakan)T(N) = waktu eksekusi maksimum untuk jumlah data N

• Strategi Average case (jarang digunakan)T(N) = waktu yang diharapkan dari sebuah algoritma untuk mengeksekusi data sejumlah N. Perlu ada asumsi statistik untuk distribusi data masukan

Page 23: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Analisa worst case

• Biasanya mengambil batas maksimal (upper bound), untuk memberi jaminan bahwa program tidak akan terus berjalan saat batas maksimal waktu eksekusi tercapai

• Waktu eksekusi juga tergantung pada kondisi awal input : data yang sudah terproses akan lebih mudah dieksekusi daripada yang belum

• Untuk worst-case, diambil kemungkinan yang paling buruk (data tidak terproses sama sekali, pada kondisi yang berlawanan dengan kondisi akhir yang diharapkan)

Page 24: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Machine-independent Running Time

• Berapakah waktu eksekusi terburuk (worst-case) dari sebuah algoritma?

• Tergantung dari kecepatan mesin kita: - kecepatan relatif: berjalan di komputer yang sama- kecepatan absolut: berjalan di komputer yang berbeda

• Kita ingin mengukur kecepatan algoritma tanpa mempertimbangkan kecepatan komputer

Analisa Asymptotic : Pertumbuhan waktu eksekusi T(N)saat N →∞

Page 25: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Performa Asymptotic

!"#$"%&"'()*(+,,- !"#$%&'(#)%"*#%*+,-%$)#./0 ./0+11%23$)-.#*4 566789*:$);*<=*<>/?)"> ?"&*1.?$,>0*:=*@>)0>$0%"

!"#$%&'&()*%+,-',$./)+

"

A2"3

",

4 5"(6789:;<=$(>?<8'"(@6A%#$8$>B@::A(6:8C"'(@:?8'>$7%6*(78C"D"'04 E"@:FC8':;(;"6>?<(6>$9@$>8<6(8G$"<(B@::(G8'(@(B@'"G9:(&@:@<B><?(8G("<?><""'><?(8&H"B$>D"604 I6A%#$8$>B(@<@:A6>6(>6(@(96"G9:($88:($8(7":#($8(6$'9B$9'"(89'($7><J><?0

57"<(" ?"$6(:@'?"("<89?7*(@(!2"+3 @:?8'>$7%(?,B?30 &"@$6(@(!2"13 @:?8'>$7%0

O(n2)

O(n3)

Pada jumlah data tertentu (> N0), algoritma dengan kompleksitas rendah O(n2) bisa saja lebih efisien dibandingkan algoritma dengan kompleksitas tinggi

Kita tidak boleh meremehkan sebuah algoritma yang secara asymptotic lebih lambat

Dalam disain riil, kita perlumenyeimbangkan proses engineering dengan melihat algoritma yang sesuai untuk masalah yang dihadapi

Analisa Asymptotic membantukita untuk berlaku lebih adil terhadap algoritma yang kita gunakan

Page 26: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Teori Big-O

• Untuk membandingkan dua buah algoritma, bandingkan tingkat kompleksitasnya

limN→∞

f (N )g(N )

→∞ Untuk N besarg(N) lebih efisien dari f(N)

limN→∞

f (N )g(N )

→ 0 Untuk N besarf(N) lebih efisien dari g(N)

Page 27: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Implementasi: Insertion Sort

!"#$"%&"'()*(+,,- !"#$%&'(#)%"*#%*+,-%$)#./0 ./0-1%23$)-.#*4 566789*:$);*<=*<>/?)"> ?"&*1.?$,>0*:=*@>)0>$0%"

!"#$%&'()#*$'+$,'&-./0

!"#$%& 1"23"45"(( ?/*(?+*(6*(?" 78(43%&"'10

'$%#$%& #"'%3$9$:74(( ?A/B*?A+B*6B*?A" 135;$;9$((?A/ ! ?A+ ! 6 ! ?A" 0

123*%)#4!"#$%& <((+((=((>((?((@

'$%#$%& +((?((=((@((<((>

Cormen, T.H., Leiserson, C.E., Rivest, R.L., Stein, C., Introduction to Algorithms 2nd Edition, Massachusetts: MIT Press, 2002

Page 28: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Implementasi: Insertion Sort

!"#$"%&"'()*(+,,- !"#$%&'(#)%"*#%*+,-%$)#./0 ./011%23$)-.#*4 566789*:$);*<=*<>/?)"> ?"&*1.?$,>0*:=*@>)0>$0%"

!"#$%&'()*(+,-'./+),(-)./1 + 2 3 4 5

!"#$"%&"'()*(+,,- !"#$%&'(#)%"*#%*+,-%$)#./0 ./011%23$)-.#*4 566789*:$);*<=*<>/?)"> ?"&*1.?$,>0*:=*@>)0>$0%"

!"#$%&'()*(+,-'./+),(-)./2 + 3 1 4 5

!"#$"%&"'()*(+,,- !"#$%&'(#)%"*#%*+,-%$)#./0 ./0/,1%23$)-.#*4 566789*:$);*<=*<>/?)"> ?"&*1.?$,>0*:=*@>)0>$0%"

!"#$%&'()*(+,-'./+),(-)./1 + 2 3 4 5

+ 1 2 3 4 5

!"#$"%&"'()*(+,,- !"#$%&'(#)%"*#%*+,-%$)#./0 ./0//1%23$)-.#*4 566789*:$);*<=*<>/?)"> ?"&*1.?$,>0*:=*@>)0>$0%"

!"#$%&'()*(+,-'./+),(-)./1 + 2 3 4 5

+ 1 2 3 4 5

!"#$"%&"'()*(+,,- !"#$%&'(#)%"*#%*+,-%$)#./0 ./0/+1%23$)-.#*4 566789*:$);*<=*<>/?)"> ?"&*1.?$,>0*:=*@>)0>$0%"

!"#$%&'()*(+,-'./+),(-)./1 + 2 3 4 5

+ 1 2 3 4 5

+ 2 1 3 4 5

!"#$"%&"'()*(+,,- !"#$%&'(#)%"*#%*+,-%$)#./0 ./0/11%23$)-.#*4 566789*:$);*<=*<>/?)"> ?"&*1.?$,>0*:=*@>)0>$0%"

!"#$%&'()*(+,-'./+),(-)./2 + 3 4 1 5

+ 2 3 4 1 5

+ 3 2 4 1 5

!"#$"%&"'()*(+,,- !"#$%&'(#)%"*#%*+,-%$)#./0 ./0/11%23$)-.#*4 566789*:$);*<=*<>/?)"> ?"&*1.?$,>0*:=*@>)0>$0%"

!"#$%&'()*(+,-'./+),(-)./2 + 1 3 4 5

+ 2 1 3 4 5

+ 1 2 3 4 5

+ 1 2 3 4 5

!"#$"%&"'()*(+,,- !"#$%&'(#)%"*#%*+,-%$)#./0 ./0/-1%23$)-.#*4 566789*:$);*<=*<>/?)"> ?"&*1.?$,>0*:=*@>)0>$0%"

!"#$%&'()*(+,-'./+),(-)./1 + 2 3 4 5

+ 1 2 3 4 5

+ 2 1 3 4 5

+ 2 1 3 4 5

!"#$"%&"'()*(+,,- !"#$%&'(#)%"*#%*+,-%$)#./0 ./0/11%23$)-.#*4 566789*:$);*<=*<>/?)"> ?"&*1.?$,>0*:=*@>)0>$0%"

!"#$%&'()*(+,-'./+),(-)./2 + 3 4 5 1

+ 2 3 4 5 1

+ 3 2 4 5 1

+ 3 2 4 5 1

+ 5 3 2 4 1

!"#$"%&"'()*(+,,- !"#$%&'(#)%"*#%*+,-%$)#./0 ./0/)1%23$)-.#*4 566789*:$);*<=*<>/?)"> ?"&*1.?$,>0*:=*@>)0>$0%"

!"#$%&'()*(+,-'./+),(-)./1 + 2 3 4 5

+ 1 2 3 4 5

+ 2 1 3 4 5

+ 2 1 3 4 5

+ 4 2 1 3 5

!"#$"%&"'()*(+,,- !"#$%&'(#)%"*#%*+,-%$)#./0 ./0/11%23$)-.#*4 566789*:$);*<=*<>/?)"> ?"&*1.?$,>0*:=*@>)0>$0%"

!"#$%&'()*(+,-'./+),(-)./1 + 2 3 4 5

+ 1 2 3 4 5

+ 2 1 3 4 5

+ 2 1 3 4 5

+ 4 2 1 3 5

+ 4 2 5 1 3 &%">

Cormen, T.H., Leiserson, C.E., Rivest, R.L., Stein, C., Introduction to Algorithms 2nd Edition, Massachusetts: MIT Press, 2002

Page 29: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

!"#$"%&"'()*(+,,- !"#$%&'(#)%"*#%*+,-%$)#./0 ./0)1%23$)-.#*4 566789*:$);*<=*<>/?)"> ?"&*1.?$,>0*:=*@>)0>$0%"

!"#$%&'(")#(%&12!3451627!645(8+*("9 +:/(0(0(";*(% A* + &( "

+( ;>3* +:(A;)* A*B /,-'.$ )*C*, <=>(+:);(?(;>3

+( +:)D/;( +:);)* )*B /

+:)D/;(@(;>3

A#B"C>DED>"F

BD'$">

) A

;>3+G

/ "

Cormen, T.H., Leiserson, C.E., Rivest, R.L., Stein, C., Introduction to Algorithms 2nd Edition, Massachusetts: MIT Press, 2002

Implementasi: Insertion Sort

Page 30: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

24 Chapter 2 Getting Started

In the following discussion, our expression for the running time of INSERTION-SORT will evolve from a messy formula that uses all the statement costs ci to amuch simpler notation that is more concise and more easily manipulated. Thissimpler notation will also make it easy to determine whether one algorithm is moreefficient than another.

We start by presenting the INSERTION-SORT procedure with the time “cost”of each statement and the number of times each statement is executed. For eachj = 2, 3, . . . , n, where n = length[A], we let t j be the number of times the whileloop test in line 5 is executed for that value of j . When a for or while loop exits inthe usual way (i.e., due to the test in the loop header), the test is executed one timemore than the loop body. We assume that comments are not executable statements,and so they take no time.

INSERTION-SORT(A) cost times1 for j ! 2 to length[A] c1 n2 do key ! A[ j ] c2 n " 13 ! Insert A[ j ] into the sorted

sequence A[1 . . j " 1]. 0 n " 14 i ! j " 1 c4 n " 15 while i > 0 and A[i] > key c5

!nj=2 t j

6 do A[i + 1] ! A[i] c6!n

j=2(t j " 1)

7 i ! i " 1 c7!n

j=2(t j " 1)

8 A[i + 1] ! key c8 n " 1

The running time of the algorithm is the sum of running times for each statementexecuted; a statement that takes ci steps to execute and is executed n times willcontribute ci n to the total running time.5 To compute T (n), the running time ofINSERTION-SORT, we sum the products of the cost and times columns, obtaining

T (n) = c1n + c2(n " 1) + c4(n " 1) + c5

n"

j=2

t j + c6

n"

j=2

(t j " 1)

+ c7

n"

j=2

(t j " 1) + c8(n " 1) .

Even for inputs of a given size, an algorithm’s running time may depend onwhich input of that size is given. For example, in INSERTION-SORT, the best

5This characteristic does not necessarily hold for a resource such as memory. A statement thatreferences m words of memory and is executed n times does not necessarily consume mn words ofmemory in total.

24 Chapter 2 Getting Started

In the following discussion, our expression for the running time of INSERTION-SORT will evolve from a messy formula that uses all the statement costs ci to amuch simpler notation that is more concise and more easily manipulated. Thissimpler notation will also make it easy to determine whether one algorithm is moreefficient than another.

We start by presenting the INSERTION-SORT procedure with the time “cost”of each statement and the number of times each statement is executed. For eachj = 2, 3, . . . , n, where n = length[A], we let t j be the number of times the whileloop test in line 5 is executed for that value of j . When a for or while loop exits inthe usual way (i.e., due to the test in the loop header), the test is executed one timemore than the loop body. We assume that comments are not executable statements,and so they take no time.

INSERTION-SORT(A) cost times1 for j ! 2 to length[A] c1 n2 do key ! A[ j ] c2 n " 13 ! Insert A[ j ] into the sorted

sequence A[1 . . j " 1]. 0 n " 14 i ! j " 1 c4 n " 15 while i > 0 and A[i] > key c5

!nj=2 t j

6 do A[i + 1] ! A[i] c6!n

j=2(t j " 1)

7 i ! i " 1 c7!n

j=2(t j " 1)

8 A[i + 1] ! key c8 n " 1

The running time of the algorithm is the sum of running times for each statementexecuted; a statement that takes ci steps to execute and is executed n times willcontribute ci n to the total running time.5 To compute T (n), the running time ofINSERTION-SORT, we sum the products of the cost and times columns, obtaining

T (n) = c1n + c2(n " 1) + c4(n " 1) + c5

n"

j=2

t j + c6

n"

j=2

(t j " 1)

+ c7

n"

j=2

(t j " 1) + c8(n " 1) .

Even for inputs of a given size, an algorithm’s running time may depend onwhich input of that size is given. For example, in INSERTION-SORT, the best

5This characteristic does not necessarily hold for a resource such as memory. A statement thatreferences m words of memory and is executed n times does not necessarily consume mn words ofmemory in total.

2.2 Analyzing algorithms 25

case occurs if the array is already sorted. For each j = 2, 3, . . . , n, we then findthat A[i] ! key in line 5 when i has its initial value of j " 1. Thus t j = 1 forj = 2, 3, . . . , n, and the best-case running time is

T (n) = c1n + c2(n " 1) + c4(n " 1) + c5(n " 1) + c8(n " 1)

= (c1 + c2 + c4 + c5 + c8)n " (c2 + c4 + c5 + c8) .

This running time can be expressed as an + b for constants a and b that depend onthe statement costs ci ; it is thus a linear function of n.

If the array is in reverse sorted order—that is, in decreasing order—the worstcase results. We must compare each element A[ j ] with each element in the entiresorted subarray A[1 . . j " 1], and so t j = j for j = 2, 3, . . . , n. Noting that

n!

j=2

j = n(n + 1)

2" 1

andn!

j=2

( j " 1) = n(n " 1)

2

(see Appendix A for a review of how to solve these summations), we find that inthe worst case, the running time of INSERTION-SORT is

T (n) = c1n + c2(n " 1) + c4(n " 1) + c5

"n(n + 1)

2" 1

#

+ c6

"n(n " 1)

2

#+ c7

"n(n " 1)

2

#+ c8(n " 1)

=$c5

2+ c6

2+ c7

2

%n2 +

$c1 + c2 + c4 + c5

2" c6

2" c7

2+ c8

%n

" (c2 + c4 + c5 + c8) .

This worst-case running time can be expressed as an2 + bn + c for constants a, b,and c that again depend on the statement costs ci ; it is thus a quadratic functionof n.

Typically, as in insertion sort, the running time of an algorithm is fixed for agiven input, although in later chapters we shall see some interesting “randomized”algorithms whose behavior can vary even for a fixed input.

Worst-case and average-case analysis

In our analysis of insertion sort, we looked at both the best case, in which the inputarray was already sorted, and the worst case, in which the input array was reversesorted. For the remainder of this book, though, we shall usually concentrate on

2.2 Analyzing algorithms 25

case occurs if the array is already sorted. For each j = 2, 3, . . . , n, we then findthat A[i] ! key in line 5 when i has its initial value of j " 1. Thus t j = 1 forj = 2, 3, . . . , n, and the best-case running time is

T (n) = c1n + c2(n " 1) + c4(n " 1) + c5(n " 1) + c8(n " 1)

= (c1 + c2 + c4 + c5 + c8)n " (c2 + c4 + c5 + c8) .

This running time can be expressed as an + b for constants a and b that depend onthe statement costs ci ; it is thus a linear function of n.

If the array is in reverse sorted order—that is, in decreasing order—the worstcase results. We must compare each element A[ j ] with each element in the entiresorted subarray A[1 . . j " 1], and so t j = j for j = 2, 3, . . . , n. Noting that

n!

j=2

j = n(n + 1)

2" 1

andn!

j=2

( j " 1) = n(n " 1)

2

(see Appendix A for a review of how to solve these summations), we find that inthe worst case, the running time of INSERTION-SORT is

T (n) = c1n + c2(n " 1) + c4(n " 1) + c5

"n(n + 1)

2" 1

#

+ c6

"n(n " 1)

2

#+ c7

"n(n " 1)

2

#+ c8(n " 1)

=$c5

2+ c6

2+ c7

2

%n2 +

$c1 + c2 + c4 + c5

2" c6

2" c7

2+ c8

%n

" (c2 + c4 + c5 + c8) .

This worst-case running time can be expressed as an2 + bn + c for constants a, b,and c that again depend on the statement costs ci ; it is thus a quadratic functionof n.

Typically, as in insertion sort, the running time of an algorithm is fixed for agiven input, although in later chapters we shall see some interesting “randomized”algorithms whose behavior can vary even for a fixed input.

Worst-case and average-case analysis

In our analysis of insertion sort, we looked at both the best case, in which the inputarray was already sorted, and the worst case, in which the input array was reversesorted. For the remainder of this book, though, we shall usually concentrate on

O(n2)

Page 31: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Cormen, T.H., Leiserson, C.E., Rivest, R.L., Stein, C., Introduction to Algorithms 2nd Edition, Massachusetts: MIT Press, 2002

Implementasi: Insertion Sort

Worst-case (seluruh input diurutkan terbalik):

Apakah Insertion Sort algoritma yang cepat? - Ya, jika N kecil - Tidak, jika N besarLihat perbandingannya dengan Merge Sort di slide berikutnya

T (N ) = O( j) = O(n2 )j=2

N

∑ Deret aritmatika

Page 32: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Contoh komparasi• Komputer A:

Insertion Sort, lebih cepat untuk N kecil, memproses N data dengan waktu eksekusi Kecepatan eksekusi prosesor 109 instruksi/detik

• Komputer B:Merge Sort, lebih lambat untuk N kecil, memproses N data dengan waktu eksekusi Kecepatan eksekusi prosesor 107 instruksi/detik

• Diketahui:

c1N2

c2N logN

c1 c2Jika c1=2, c2=50, N = 106 ,

mana algoritma yang lebih efisien?

Page 33: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Kesimpulan

• Analisa algoritma diperlukan untuk perbandingan algoritma tanpa tergantung spesifikasi mesin

• Analisa algoritma membantu kita memecahkan masalah sesuai kondisi dan data yang kita hadapi

• Analisa algoritma bukan alat mutlak untuk memilih algoritma terbaik, tapi membantu memahami perilaku algoritma saat diterapkan di dunia nyata

Page 34: Analisa Kompleksitas Algoritma - te.ugm.ac.idte.ugm.ac.id/~wibirama/tke670/week04/04-analisa-algoritma.pdf · Contoh: 3n3+90n2−5n+6046=O(N3) 3.1 Asymptotic notation 43 (a) (b) (c)

Terima Kasih