Algorithm complexity Lesson 4 CSG3F3
Fundamentals of Algorithmic Problem Solving Understand the Problem Decide on : Computational means, exact vs approximate solving, data structures, algorithms design techniques Design algorithm Prove corectness Analyze the algorithm Code the algorithm RMB/CSG3F3
Important Problem Types Sorting To rearrange the items of given list in ascending/descending order Stable sorting algorithm and Not Stable sorting algorithm RMB/CSG3F3
Important Problem Types Searching Deals with finding a given value, called a search key, in a given set No single algorithm that fits all situations best Sequencial search, binary search RMB/CSG3F3
Important Problem Types String Processing String : a sequence of characters (Levitin) Particular problem : searching for a given word in a text → string matching RMB/CSG3F3
Important Problem Types Graph Problems Graph : a collection of point (vertex), some of which are connected by line segments (edge) For modelling communication network, event schedulling, games, topsort, TSP, graph-coloring. RMB/CSG3F3
Important Problem Types Combinatorial Problems Problem ask to find a combinatorial object Combinatorial objects typically grows extremely fast with problem’s size There no known algorithms for solving most such problems in acceptable amount of time exception : shortest-path problem (diantaranya) Contoh : graph coloring problem, traveling salesman problem, knapsack problems, dll RMB/CSG3F3
Sorting To rearrange the items of given list in ascending/descending order Stable sorting algorithm and Not Stable sorting algorithm RMB/CSG3F3
Important Problem Types Searching Deals with finding a given value, called a search key, in a given set No single algorithm that fits all situations best Sequencial search, binary search RMB/CSG3F3
Important Problem Types String Processing String : a sequence of characters (Levitin) Particular problem : searching for a given word in a text → string matching RMB/CSG3F3
Important Problem Types Graph Problems Graph : a collection of point (vertex), some of which are connected by line segments (edge) For modelling communication network, event schedulling, games, topsort, TSP, graph-coloring. RMB/CSG3F3
Important Problem Types Combinatorial Problems Problem ask to find a combinatorial object Combinatorial objects typically grows extremely fast with problem’s size There no known algorithms for solving most such problems in acceptable amount of time exception : shortest-path problem (diantaranya) Contoh : graph coloring problem, traveling salesman problem, knapsack problems, dll RMB/CSG3F3
Important Problem Types Geometric Problems Geometric algorithms deal with geometric objects such as points, lines and polygons. Applications : computer graphics, robotics, dsb Classic problems of computational geometry: The closest pair problem and convex hull problem RMB/CSG3F3
Important Problem Types Numerical Problems Problems that involve mathematical objects of continuous nature Contoh : solving equations and systems of equations, computing definite integrals, evaluating functions, dsb Majority can be solved only approximately Application : information storage, information retreval, transportation through networks, dsb RMB/CSG3F3
Analisis Algoritma Algoritma tidak cukup hanya benar saja (correctness), tetapi juga harus mangkus (efisien) RMB/CSG3F3
Tujuan Analisis Algoritma Mengukur efisiensi algoritma dari segi waktu/ruang memori Membanding-bandingkan dua/lebih algoritma untuk masalah yang sama. RMB/CSG3F3
Ukuran input (input’s size) Hampir seluruh algoritma memiliki waktu eksekusi lebih lama jika mendapat input yang berukuran besar, contoh Pengurutan array yang berukuran besar Perkalian matriks yang berukuran besar RMB/CSG3F3
Penentuan ukuran input Penentuan parameter n yang menunjukkan ukuran input sebuah algoritma Sorting, searching, the smallest element size of the list Polynomial p(x)=anxn+…+a0 polynomial’s degree/number of its coefficients Product of two n -by-n matrices matrix order n / the number of elements being mulltiplied RMB/CSG3F3
Penentuan ukuran input Apa ukuran input untuk a spell-checking algorithm ? checking whether a given integer n is a prime ? RMB/CSG3F3
Satuan ukuran kompleksitas waktu Operasi dasar (Basic operation ) The most important operation of the algorithm The most time-consuming operation in the algorithm’s innermost loop Contoh: sorting algorithm, matrix multiplication, polynomial evaluation. RMB/CSG3F3
Pengukuran efisiensi waktu Counting the number of times the algorithm’s basic operation is executed on input of size n RMB/CSG3F3
Ilustrasi Efisiensi Algoritma Algorithm: Insertion sort c1n2, Merge sort c2 n log n ; c1 < c2 Hardware: Kecepatan komputer A one billion instructions per second Kecepatan komputer B ten million instructions per second RMB/CSG3F3
Jika c1= 2 dan c2= 50 dan komputer A untuk eksekusi insertion sort dan komputer B untuk merge sort. Berapakah waktu yang dibutuhkan komputer A dan B (second) untuk mengurutkan bilangan sebanyak 1 juta (n one million numbers) ? RMB/CSG3F3
Mengapa kita memerlukan algoritma yang mangkus Mengapa kita memerlukan algoritma yang mangkus? Lihat grafik di bawah ini. RMB/CSG3F3
Kompleksitas waktu, T(n), diukur dari jumlah tahapan komputasi yang dibutuhkan untuk menjalankan algoritma sebagai fungsi dari ukuran masukan n. RMB/CSG3F3
Kompleksitas Waktu Dalam praktek, kompleksitas waktu dihitung berdasarkan jumlah operasi dasar yang mendasari suatu algoritma Contoh 1. Tinjau algoritma untuk mencari elemen terbesar dalam larik yang berukuran n elemen. RMB/CSG3F3
Contoh 1. Algoritma untuk mencari elemen terbesar di dalam sebuah larik (array) yang berukuran n elemen. procedure CariElemenTerbesar(input a1, a2, ..., an : integer, output maks : integer) { Mencari elemen terbesar dari sekumpulan elemen larik integer a1, a2, ..., an. Elemen terbesar akan disimpan di dalam maks. Masukan: a1, a2, ..., an Keluaran: maks (nilai terbesar) } Deklarasi k : integer Algoritma maksa1 k2 while k n do if ak > maks then maksak endif kk+1 endwhile { k > n } RMB/CSG3F3
Kompleksitas waktu CariElemenTerbesar : T(n) = n – 1. Kompleksitas waktu algoritma dihitung berdasarkan jumlah operasi perbandingan elemen larik (A[i] > maks). Kompleksitas waktu CariElemenTerbesar : T(n) = n – 1. RMB/CSG3F3
Contoh 2. Algoritma pengurutan seleksi (selection sort). procedure Urut(input/output a1, a2, ..., an : integer) Deklarasi i, j, imaks, temp : integer Algoritma for in downto 2 do { pass sebanyak n – 1 kali } imaks1 for j2 to i do if aj > aimaks then imaksj endif endfor { pertukarkan aimaks dengan ai } tempai aiaimaks aimakstemp RMB/CSG3F3
RMB/CSG3F3
RMB/CSG3F3
RMB/CSG3F3
Contoh 3. Algoritma sequential search. procedure PencarianBeruntun(input a1, a2, ..., an : integer, x : integer,output idx : integer) Deklarasi k : integer ketemu : boolean { bernilai true jika x ditemukan atau false jika x tidak ditemukan } Algoritma: k1 ketemu false while (k n) and (not ketemu) do if ak = x then ketemutrue else k k + 1 endif endwhile { k > n or ketemu } if ketemu then { x ditemukan } idxk idx 0 { x tidak ditemukan } RMB/CSG3F3
RMB/CSG3F3
Contoh 4. Hitung kompleksitas waktu algoritma berikut berdasarkan jumlah operasi kali.
Jawaban Untuk j = n, jumlah operasi perkalian = n … j = 1, jumlah operasi perkalian = 1 Jumlah operasi perkalian seluruhnya adalah = n + n/2 + n/4 + … + 2 + 1 deret geometri
Exercise Determine the time complexity of best, average, and worst case of binary searching RMB/CSG3F3
Referensi Levitin, Anany. Introduction to the design and analysis of algorithm. Addison Wesley. 2003 Munir,Rinaldi. Diktat Strategi Algoritmik IF2251. Departemen Teknik Informatika. Institut Teknologi Bandung. 2006 RMB/CSG3F3
kompleksitas waktu asimptotik Next will be… kompleksitas waktu asimptotik RMB/CSG3F3