design and analysis algorithm - universitas...
TRANSCRIPT
![Page 1: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/1.jpg)
Design and Analysis Algorithm
Ahmad Afif Supianto, S.Si., M.Kom
![Page 2: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/2.jpg)
2
![Page 3: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/3.jpg)
Contoh Algoritma
3
m a[1];
for i 2 to size do
if m < a[i] then
m a[i];
end if
end for
return m
1. copy the input a to array t1;
assign n size of input;
2. While n > 1
For i 1 to n/2
t2[i ] min (t1 [ 2*i ], t1[ 2*i + 1] );
copy array t2 to t1;
n n/2;
end for
end while
3. Output t2[1];
1
2
3
4
![Page 4: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/4.jpg)
Algoritma
Recipe for getting things done successfully
Recipe
1. .... 2. .... 3. .... 4. .... 5. .... 6. dst
Algoritma
4
![Page 5: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/5.jpg)
Algoritma
Recipe for getting things done successfully
"Recipe" - well defined sequence of computational
steps
"things" - computational problems specifying an
input/output relation
"done" - in finite steps and time
"successfully" – correctly
Any special method of solving a certain kind of
problem (Webster Dictionary)
5
![Page 6: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/6.jpg)
Algoritma
An algorithm is a sequence of unambiguous
instructions for solving a problem
An algorithm is a sequence of computational
steps that transform the input into the output
An algorithm is a tool for solving a well-specified
computational problem
An algorithm is written in pseudocode
6
![Page 7: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/7.jpg)
Contoh : Suatu Algoritma
Problem: Input is a sequence of integers
stored in an array.
7
25, 90, 53, 23, 11, 34
INPUT OUTPUT
instance
11
ALGORITHM
m a[1];
for i 2 to size do
if m < a[i] then
m a[i];
end if
end for
return m
Output the minimum
![Page 8: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/8.jpg)
Contoh : Algoritma A
Problem: Input is a sequence of integers
stored in an array.
8
Output the minimum
Algorithm A
![Page 9: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/9.jpg)
Contoh : Algoritma B
This algorithm uses two temporary arrays.
Visualisasikan ?
9
1. copy the input a to array t1;
assign n size of input;
2. While n > 1
For i 1 to n/2
t2[i ] min (t1 [ 2*i ], t1[ 2*i + 1] );
copy array t2 to t1;
n n/2;
end for
end while
3. Output t2[1];
8 9 5 6 11 34 7 20
![Page 10: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/10.jpg)
Visualisasi Algoritma B
10
8 9 5 6 11 34 7 20
6 5 8 7
5 7
5
Loop 1
Loop 2
Loop 3
![Page 11: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/11.jpg)
Contoh : Algoritma C
Sort the input in increasing order. Return the
first element of the sorted data.
11
8 9 5 6 11 34 7 20
5 6 7 8 9 11 20 34
Sorting black
box
![Page 12: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/12.jpg)
Contoh : Algoritma D
For each element, test whether it is the
minimum.
12
![Page 13: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/13.jpg)
The Study of Algorithm
How to devise/design algorithms
How to express algorithms
How to validate algorithms
How to analyze algorithms
How to test a program
13
![Page 14: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/14.jpg)
Define Problem
Problem:
Description of Input-Output relationship
Algorithm:
A sequence of computational step that transform the
input into the output.
Data Structure:
An organized method of storing and retrieving data.
Our task:
Given a problem, design a correct and good
algorithm that solves it.
14
![Page 15: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/15.jpg)
Importance of Analyze Algorithm
Need to recognize limitations of various
algorithms for solving a problem
Need to understand relationship between
problem size and running time When is a running program not good enough?
Need to learn how to analyze an algorithm's
running time without coding it
Need to learn techniques for writing more
efficient code
Need to recognize bottlenecks in code as well
as which parts of code are easiest to optimize
15
![Page 16: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/16.jpg)
Why do we analyze about them?
understand their behavior
(Job -- Selection, performance, modify)
improve them
(Research)
16
![Page 17: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/17.jpg)
What do we analyze about them?
Correctness
Does the input/output relation match algorithm
requirement?
Amount of work done (aka complexity)
Basic operations to do task
Amount of space used
Memory used
Simplicity, clarity
Verification and implementation.
Optimality
Is it impossible to do better?
17
![Page 18: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/18.jpg)
What is the running time of this algorithm?
PUZZLE(x)
while x != 1
if x is even then
x = x / 2
else
x = 3x + 1
end if
Sample run: 7, 22; 11, 34; 17, 52; 26,13; 40, 20; 10, 5; 16, 8; 1
18
![Page 19: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/19.jpg)
The Selection Problem (1/2)
Problem: given a group of n numbers,
determine the kth largest
Algorithm 1
Simpan bilangan dalam sebuah array
Urutkan array secara descending
Beri nilai balikan bilangan pada posisi-k
19
![Page 20: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/20.jpg)
The Selection Problem(2/2)
Algorithm 2
Simpan bilangan k pertama dalam sebuah array
Urutkan array secara descending
Untuk setiap bilangan yang tersisa, jika bilangan tersebut lebih
besar daripada bilangan ke-k, masukkan bilangan tersebut
pada posisi yang benar di dalam array
Beri nilai balikan bilangan pada posisi-k
Which algorithm is better?
20
![Page 21: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/21.jpg)
Which algorithm is better?
The algorithms are correct,
but which is the best?
Measure the running time (number of
operations needed).
Measure the amount of memory used.
Note that the running time of the algorithms
increase as the size of the input increases.
21
![Page 22: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/22.jpg)
Time vs. Size of Input
Measurement
parameterized by the
size of the input.
The algorithms
A,B,C are
implemented and run
in a PC.
Algorithms D is
implemented and run
in a supercomputer.
Let Tk( n ) be the
amount of time taken
by the Algorithm
22
Input Size
4
0
2
Tc (n)
Runnin
g tim
e
(
second)
Td(n)
![Page 23: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/23.jpg)
What do we need?
Correctness: Whether the algorithm
computes the correct solution for all instances
Efficiency: Resources needed by the algorithm
1. Time: Number of steps.
2. Space: amount of memory used.
23
![Page 24: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/24.jpg)
24
![Page 25: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/25.jpg)
Design dan Analysis Algorithm
Design an algorithm (Correctness) Prove the algorithm is correct.
• Loop invariant.
• Recursive function.
• Formal (mathematical) proof.
Analyze the algorithm (Efficiency) Time
• Worse case, best case, average case.
• For some algorithms, worst case occurs often, average case is often roughly as bad as the worst case. So generally, worse case running time.
Space
25
![Page 26: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/26.jpg)
Latihan
Buatlah sebuah algoritma untuk menampilkan N
bilangan pertama secara terbalik.
Contoh : 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
26
![Page 27: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/27.jpg)
Latihan
Buat sebuah algoritma yang akan menerima
sebuah bilangan X dari user. Tampilkan pesan
“benar” jika X habis dibagi 2, 3 atau 7 dan
tampilkan “salah” jika tidak habis dibagi.
27
![Page 28: Design and Analysis Algorithm - Universitas Brawijayaafif.lecture.ub.ac.id/files/2014/07/DAA-Pertemuan01.pdf · Contoh : Algoritma B ... • For some algorithms, worst case occurs](https://reader030.vdocuments.mx/reader030/viewer/2022040702/5d63857788c993274d8b5638/html5/thumbnails/28.jpg)
Click to edit subtitle style