1 / 22

Strategi Algoritma Kuliah 3 : Algoritma Efisien

Strategi Algoritma Kuliah 3 : Algoritma Efisien. Teknik Informatika Universitas Ahmad Dahlan. Contoh 1. Void main(void) { int x= 4; float y=13.99; float z=12/7*x^3+y/9; int p=z/6*y clrscr; cout <<“Hasil : ” << x<< endl; …. …. …. }.

barton
Télécharger la présentation

Strategi Algoritma Kuliah 3 : Algoritma Efisien

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Strategi Algoritma Kuliah 3 : Algoritma Efisien TeknikInformatika Universitas Ahmad Dahlan

  2. Contoh 1 Void main(void) { int x= 4; float y=13.99; float z=12/7*x^3+y/9; int p=z/6*y clrscr; cout <<“Hasil : ” << x<< endl; …. …. …. } Waktu prosesnya konstan, misalnya t detik

  3. Contoh 2 .. { … y =…; x =1; …. while( x<n) { … … cout …. …. x++; } … … } Waktu prosesnya berarti a + b*n + c a bn+a+c  O (n) b c

  4. .. { ... y=1; x =1; ... for(;x<n;x++) {... ... ...} for( ;y<n;y++) {... ... ... } } a Waktu prosesnya berarti a + b*n + c*n b c (b+c)n+a  O (n)

  5. .. { ... y=1; x =1; ... for(;x<n;x++) {... ... ... for( ;y<n;y++) {... ... ... } } } a Waktu prosesnya berarti a + b*n + c*n2 b c cn2+bn+a  O(n2)

  6. Aparunning time dari algorithm berikut? PUZZLE(x) while x != 1     if x is genap     then  x = x / 2      else x = 3x + 1 Contoh pd data:  7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1

  7. Contoh (1/2) • Problem: diberikan sekelompok n angka, tentukan angka terbesar ke-k • Algoritma 1 • Simpan angka dalam array • Urutkan array secara descending • Hasil angka pada posisi k

  8. Contoh (2/2) • Algoritma 2 • Simpan angka2, pertama kpada array • Urutkan array secara descending • Untuk setiap angka sisa, jika angka lebih besar dari angka ke-k,sisipkan angka tsb pd posisi yg benar pdarray • Hasil angka pada posisi k Mana algorithm yg lebih baik?

  9. Contoh: Algoritma Apa? I.I What is an Algorithm? Problem: Input adlh himp bil integer yg disimpan dlm array. Outputnya adalah bil minimum. Algoritma INPUT m:= a[1]; for I:=2 to size of input if m > a[I] then m:=a[I]; return s OUTPUT Random 11 25, 90, 53, 23, 11, 34 m Data-Structure

  10. Contoh Algoritma A Problem: Input adlh urutan dari integer yg disimpan dlm array. Outputnya adalah max Algorithm A Max:=Y[0]; For i:=1 to n-1 do If Y[i]>max then max:=Y[i]

  11. Contoh Algoritma B Algoritma ini menggunakan 2 array temporary Max:=0; For i:= 0 to (n div 2)-1 do Begin If X[2*i]>X[(2*i)+1] then bantu:=X[2*i] else bantu:=X[(2*i)+1]; If bantu>Max then Max:=bantu end; If (n mod 2) = 1 then If Max<X[n-1] then Max:=X[n-1];

  12. Visualisasi Algoritma B 7 34 6 5 9 20 8 11 Loop 0 9 20 11 34 Loop 1 20 34 Loop 2 34

  13. Contoh Algoritma C For i:= 0 to (n div 2)-1 do If X[2*i]<X[(2*i)+1] then X[2*i]:=X[(2*i)+1]; For i:= 1 to (n div 2) do If X[i-1]>X[i] then X[i]:=X[i-1]; If (n mod 2) = 1 then If X[1]<X[n-1] then X[i]:=X[n-1];

  14. Contoh Algoritma D Untuk setiap elemen, test kondisinya minimum.

  15. Contoh Pemanfaatan 7 34 6 5 9 20 8 11 5 6 7 8 9 11 20 34 Mengurutkan input secara menaik. Hasil elemen pertama dari data terurut. black box Sorting

  16. Mana algoritma yg lebih baik? Semua algoritma benar, tetapi mana yang terbaik? • Mengukur running time (Jumlah operasi yg dibutuhkan). • Mengukur jumlah memori yg digunakan. • Catatan bahwa peningkatan dari running time algoritma sebagai peningkatan ukuran dari input .

  17. Apa yg kita perlukan? Kebenaran: kondisi penghitungan algoritma Solusi yg benar untuk semua hal Effisiensi : Resources yg dibutuhkan oleh algoritma 1. Time: Jumlah langkah. 2. Space: Jumlah memori yg dibutuhkan “Model” ukuran : Worst case, Average case and Best case.

  18. 4 Td(n) Tc (n) Running time (second) 2 0 Time vs. Ukuran Input Diukur dengan parameter jumlah input Algorihtma A,B,C di implementasikan dan dijalankan pada PC Algoritma D is implementasi-kan dan dijalankan pada supercomputer. Tb (n) Ta (n) 500 1000 Input Size

  19. Running timeuntuk input sedikit

  20. Running timeuntuk input besar

  21. Function of Growth rate

  22. Referensi • Rinaldi Munir, 2010, Diktat Kuliah Strategi Algoritma ITB • Gilles Brassard, 1996, Fundamental Of Algoritmh, Prentice Hall, New Jersey • Cormen et al, 2009, Introduction to Algorithms : thrid edition, MIT

More Related