Design and Analysis of Algorithm Week 1: Introduction Dr. Putu Harry Gunawan 1 1 Department of Computational Science School of Computing Telkom University Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 1 / 41
Outline 1 Introduction About this course Beginning of Algorithm The efficiency of algorithm 2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 2 / 41
Outline 1 Introduction About this course Beginning of Algorithm The efficiency of algorithm 2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 3 / 41
Goals Sebelum UTS: 1 Mahasiswa mampu untuk mejelaskan kompleksitas waktu pada suatu algoritma 2 Mahasiswa mampu menganalisis kompleksitas suatu algoritma. Setelah UTS: 1 Mahasiswa mampu membedakan tipe dan karakteristik masing-masing algoritma. 2 Mahasiswa mampu merancang suatu algoritma berdasarkan contoh-contoh algoritma yang sudah diberikan. Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 4 / 41
References Figure : Main reference. Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 5 / 41
Grading TUGAS 15% KUIS 20% UTS 25% TUGAS BESAR 15% UAS 25% NB: Bonus jika absensi mencapai 100% (Membuat buku tugas yang akan dikumpul setiap pengumpulan tugas) Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 6 / 41
Outline 1 Introduction About this course Beginning of Algorithm The efficiency of algorithm 2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 7 / 41
What is algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time. Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 8 / 41
Algorithm An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time. Can be represented various forms Unambiguity/clearness Effectiveness Finiteness/termination Correctness Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 9 / 41
Example of computational domain Statement of problem: Input: A sequence of n numbers < a 1, a 2,, a n > Output: A reordering of the input sequence < a 1, a 2,, a n > so that a i a j whenever i < j Instance: The sequence < 5, 3, 2, 8, 3 > Algorithms: Selection sort Insertion sort Merge sort (many others) Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 10 / 41
Some well-known of computational domain Sorting Searching Shortest paths in a graph Minimum spanning tree Primality testing Traveling salesman problem Knapsack problem Chess Towers of Hanoi Program termination Some of these problems dont have efficient algorithms, or algorithms at all! Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 11 / 41
Basic Issues Related to Algorithms How to design algorithms How to express algorithms Proving correctness Efficiency (or complexity) analysis Theoretical analysis Empirical analysis Optimality Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 12 / 41
Algorithms and design strategies Brute force Divide and conquer Decrease and conquer Transform and conquer Greedy approach Dynamic programming Backtracking and branch-and-bound Space and time tradeoffs Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 13 / 41
Analysis algorithms How good is the algorithm? Correctness Time efficiency Space efficiency Does there exist a better algorithm? Lower bounds Optimality Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 14 / 41
Euclid s algorithm Problem: Find gcd(m, n), the greatest common divisor of two nonnegative, not both zero integers m and n Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) =? Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 15 / 41
Euclid s algorithm Problem: Find gcd(m, n), the greatest common divisor of two nonnegative, not both zero integers m and n Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) =? Euclids algorithm is based on repeated application of equality gcd(m, n) = gcd(n, m mod n) until the second number becomes 0, which makes the problem trivial. Example: gcd(60,24) Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 15 / 41
Euclid s algorithm Problem: Find gcd(m, n), the greatest common divisor of two nonnegative, not both zero integers m and n Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) =? Euclids algorithm is based on repeated application of equality gcd(m, n) = gcd(n, m mod n) until the second number becomes 0, which makes the problem trivial. Example: gcd(60,24) = gcd(24,12) = gcd(12,0) = 12 Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 15 / 41
Euclid s algorithm Exercise: Make an algorithm for computing the gcd(m,n)! r. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 16 / 41
Euclid s algorithm Exercise: Make an algorithm for computing the gcd(m,n)! Step 1 If n = 0, return m and stop; otherwise go to Step 2 Step 2 Divide m by n and assign the value of the remainder to r Step 3 Assign the value of n to m and the value of r to n. Go to Step 1. while n 0 do r m mod n m n n r return m Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 16 / 41
Another method to compute gcd Consecutive integer checking algorithm Step 1 Assign the value of min{m, n} to t Step 2 Divide m by t. If the remainder is 0, go to Step 3; otherwise, go to Step 4 Step 3 Divide n by t. If the remainder is 0, return t and stop; otherwise, go to Step 4 Step 4 Decrease t by 1 and go to Step 2 Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 17 / 41
Another method to compute gcd Consecutive integer checking algorithm Step 1 Assign the value of min{m, n} to t Step 2 Divide m by t. If the remainder is 0, go to Step 3; otherwise, go to Step 4 Step 3 Divide n by t. If the remainder is 0, return t and stop; otherwise, go to Step 4 Step 4 Decrease t by 1 and go to Step 2 Is this slower than Euclids algorithm? How much slower? Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 17 / 41
Another method to compute gcd Consecutive integer checking algorithm Step 1 Assign the value of min{m, n} to t Step 2 Divide m by t. If the remainder is 0, go to Step 3; otherwise, go to Step 4 Step 3 Divide n by t. If the remainder is 0, return t and stop; otherwise, go to Step 4 Step 4 Decrease t by 1 and go to Step 2 Is this slower than Euclids algorithm? How much slower? O(n), if n m, vs O(log n) Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 17 / 41
Outline 1 Introduction About this course Beginning of Algorithm The efficiency of algorithm 2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 18 / 41
Efficiency Pertimbangan Memilih algoritma : Kebenaran Tepat guna (efektif) output sesuai dengan inputnya sesuai dengan permasalahan Kemudahan/ kesederhanaan Untuk dipahami Untuk diprogram (proses coding) Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 19 / 41
Efficiency Pertimbangan Memilih algoritma : Kecepatan Algoritma: Berkaitan dengan kecepatan eksekusi program Hemat Biaya: mengacu pada kebutuhan memory Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 20 / 41
Efficiency Keempatnya sulit dicapai bersamaan, dan biasanya yang diutamakan adalah efisiensi (cepat dan hemat) Analisis Algoritma : menganalisis efisiensi algoritma, yang mencakup : efisiensi waktu (kecepatan) banyaknya operasi yang dilakukan efisiensi memori struktur data dan variabels yang digunakan Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 21 / 41
Efficiency Algoritma yang bagus adalah algoritma yang efisien Algoritma yang efisien ialah algoritma yang meminimumkan kebutuhan waktu dan ruang/memori. Kebutuhan waktu dan ruang suatu algoritma bergantung pada ukuran masukan (n), yang menyatakan jumlah data yang diproses. Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 22 / 41
Efficiency Question: Mana yang lebih baik : menggunakan algoritma yang waktu eksekusinya cepat dengan komputer standard atau menggunakan algoritma yang waktunya tidak cepat tetapi dengan komputer yang cepat? Watch Video Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 23 / 41
Efficiency Misal dipunyai : Algoritma dengan waktu eksekusi dalam orde 2 n Sebuah komputer dengan kecepatan 10 4 2 n n = 10 1/10 detik n = 20 2 menit n = 30 lebih dari satu hari n = 38 1 tahun Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 24 / 41
Efficiency Misal dipunyai : Algoritma dengan waktu eksekusi dalam orde 2 n Sebuah komputer dengan kecepatan 10 6 x2 n n = 45 1 tahun Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 25 / 41
Efficiency Misal dipunyai : Algoritma dengan waktu eksekusi dalam orde n 3 Sebuah komputer dengan kecepatan 10 4 xn 3 n = 900 1 hari n = 6800 1 tahun Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 26 / 41
Efficiency Misal dipunyai : Mengapa kita memerlukan algoritma yang efisien? Lihat grafik di bawah ini. Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 27 / 41
Outline 1 Introduction About this course Beginning of Algorithm The efficiency of algorithm 2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 28 / 41
Rules Make a group Each group consists of 5 or more peoples Choose a leader Prepare the answer sheet Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 29 / 41
Outline 1 Introduction About this course Beginning of Algorithm The efficiency of algorithm 2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 30 / 41
Problem 1 Buatlah algoritma untuk membuat Jus Nanas! Usahakan menggunakan sintax-syntax berikut sebanyak mungkin Comment Assignment Logical relational while loop for loop repeat loop conditional case input output procedure r. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 31 / 41
Problem 1 Buatlah algoritma untuk membuat kopi! Usahakan menggunakan sintax-syntax berikut sebanyak mungkin Comment Assignment Logical relational while loop for loop repeat loop conditional case input output procedure r. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 32 / 41
Problem 2 Buatlah algoritma untuk membuat Sambal Balado! Usahakan menggunakan sintax-syntax berikut sebanyak mungkin Comment Assignment Logical relational while loop for loop repeat loop conditional case input output procedure r. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 33 / 41
Problem 2 Buatlah algoritma untuk membuat Lumpia Basah! Usahakan menggunakan sintax-syntax berikut sebanyak mungkin Comment Assignment Logical relational while loop for loop repeat loop conditional case input output procedure r. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 34 / 41
Outline 1 Introduction About this course Beginning of Algorithm The efficiency of algorithm 2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 35 / 41
Problem 3 Given the following algorithm: if the input 60, 35, 81, 98, 14, 47, what is the output? Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 36 / 41
Outline 1 Introduction About this course Beginning of Algorithm The efficiency of algorithm 2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 37 / 41
Problem 4 Design a algorithm that reads as its inputs the (x, y) coordinates of the endpoints of two line segments P 1, Q 1 and P 2, Q 2 and determines whether the segments have a common point. Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 38 / 41
Problem 5 Design an algorithm for the following problem: Given a set of n points in the Cartesian plane, determine whether all of them lie on the same circumference. Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 39 / 41
Homeworks Compare two algorithms of shorting problem, give an analysis and a comment for the each algorithms. Compare two algorithms of searching problem, give an analysis and a comment for the each algorithms. Prove the following relations 1 1 + 2 + 3 + + n = n(n+1) 2 2 1 + 3 + 5 + + (2n 1) = n 2 3 if n Z and n 0, then n i=0 i i! = (n + 1)! 1 Compute the following sums. 1 2 3 4 5 n+1 i=3 1 n+1 i=3 i n 1 i=0 n i=0 3j+1 n i=1 i(i + 1) n j=1 ij Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 40 / 41
The end of week 1 Thank you for your attention! Dr. Putu Harry Gunawan (Telkom University) Design and Analysis of Algorithm 41 / 41