Date post: | 26-Aug-2019 |
Category: |
Documents |
Upload: | truongkhanh |
View: | 218 times |
Download: | 0 times |
Design and Analysis Algorithm
Ahmad Afif Supianto, S.Si., M.Kom
2
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
Algoritma
Recipe for getting things done successfully
Recipe
1. .... 2. .... 3. .... 4. .... 5. .... 6. dst
Algoritma
4
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
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
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
Contoh : Algoritma A
Problem: Input is a sequence of integers
stored in an array.
8
Output the minimum
Algorithm A
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
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
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
Contoh : Algoritma D
For each element, test whether it is the
minimum.
12
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
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
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
Why do we analyze about them?
understand their behavior
(Job -- Selection, performance, modify)
improve them
(Research)
16
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
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
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
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
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
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)
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
24
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
Latihan
Buatlah sebuah algoritma untuk menampilkan N
bilangan pertama secara terbalik.
Contoh : 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
26
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
Click to edit subtitle style