Design and Analysis of Algorithm
Week 1: Introduction
Dr. Putu Harry Gunawan1
1Department of Computational Science School of Computing
Outline
1 Introduction
About this course Beginning of Algorithm The efficiency of algorithm
2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks
Outline
1 Introduction
About this course
Beginning of Algorithm The efficiency of algorithm
2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks
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.
References
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)
Outline
1 Introduction
About this course
Beginning of Algorithm
The efficiency of algorithm
2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks
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.
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
Example of computational domain
Statement of problem:
Input: A sequence of n numbers<a1,a2,· · · ,an>
Output: A reordering of the input sequence<a01,a20,· · ·,a0n>so that a0i ≤a0j wheneveri<j
Instance: The sequence<5,3,2,8,3>
Algorithms:
Selection sort Insertion sort Merge sort (many others)
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
Basic Issues Related to Algorithms
How to design algorithms How to express algorithms Proving correctness
Efficiency (or complexity) analysis
Theoretical analysis Empirical analysis
Optimality
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
Analysis algorithms
How good is the algorithm?
Correctness Time efficiency Space efficiency
Does there exist a better algorithm?
Lower bounds Optimality
Euclid’s algorithm
Problem: Find gcd(m,n), the greatest common divisor of two nonnegative, not both zero integers mand 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.
Euclid’s algorithm
Problem: Find gcd(m,n), the greatest common divisor of two nonnegative, not both zero integers mand 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
Euclid’s algorithm
Problem: Find gcd(m,n), the greatest common divisor of two nonnegative, not both zero integers mand 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.
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 n6= 0 do
r ←m mod n m←n n ←r
return m
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 n6= 0 do
r ←m mod n m←n n ←r
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 , vsO(logn)
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?
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 , vsO(logn)
Outline
1 Introduction
About this course Beginning of Algorithm
The efficiency of algorithm
2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks
Efficiency
Pertimbangan Memilih algoritma : Kebenaran
Tepat guna (efektif)
output sesuai dengan inputnya sesuai dengan permasalahan
Kemudahan/ kesederhanaan
Untuk dipahami
Untuk diprogram (proses coding)
Efficiency
Pertimbangan Memilih algoritma : Kecepatan Algoritma:
Berkaitan dengan kecepatan eksekusi program Hemat Biaya:
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
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.
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¿
Efficiency
Misal dipunyai :
Algoritma dengan waktu eksekusi dalam orde 2n Sebuah komputer dengan kecepatan 10−4×2n
n= 10→1/10 detik
n= 20→2 menit
n= 30→lebih dari satu hari
Efficiency
Misal dipunyai :
Algoritma dengan waktu eksekusi dalam orde 2n Sebuah komputer dengan kecepatan 10−6x2n
n= 45→1 tahun
Efficiency
Misal dipunyai :
Algoritma dengan waktu eksekusi dalam orde n3
Sebuah komputer dengan kecepatan 10−4xn3 n= 900→1 hari
Efficiency
Misal dipunyai :
Mengapa kita memerlukan algoritma yang efisien? Lihat grafik di bawah ini.
Outline
1 Introduction
About this course Beginning of Algorithm The efficiency of algorithm
2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks
Rules
Make a group
Each group consists of 5 or more peoples Choose a leader
Prepare the answer sheet
Outline
1 Introduction
About this course Beginning of Algorithm The efficiency of algorithm
2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks
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
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
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
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
Outline
1 Introduction
About this course Beginning of Algorithm The efficiency of algorithm
2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks
Problem 3
Given the following algorithm:
Outline
1 Introduction
About this course Beginning of Algorithm The efficiency of algorithm
2 Exercise Rules Skilled Group Careful Group Smart Group 3 Homeworks
Problem 4
Design a algorithm that reads as its inputs the (x,y) coordinates of the endpoints of two line segments P1,Q1 and P2,Q2 and determines
Problem 5
Design an algorithm for the following problem: Given a set ofn points in the Cartesian plane, determine whether all of them lie on the same circumference.
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) =n2 3 ifn∈ Z andn≥0, thenPn
i=0i·i! = (n+ 1)!−1
Compute the following sums.
1 Pn+1 i=3 1 2 Pn+1 i=3 i 3 Pn−1 i=0 i(i+ 1) 4 Pn i=03j+1 5 Pn i=1 Pn j=1ij
The end of week 1
Thank you for your attention!