DAA_02

Introduction to The Design &
Analysis of Algorithms
By
INDRIATI
Teknik Informatika - UB
See page 2; quoted:
“..…. a person does not really understand
something until after teaching it to someone
else ……”
actually:
“ a person does not REALLY understand
something until after teaching it to a
computer.”
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.
What is algorithm?
• An algorithm is a sequence of
computational steps that transform the
input into the output
• An algorithm is a tool for solving a wellspecified computational problem
Algorithm is....
Algoritma adalah cara yang dapat
ditempuh oleh komputer dalam mencapai
suatu tujuan, terdiri atas langkah-langkah
yang terdefinisi dengan baik, menerima
input, melakukan proses, dan
menghasilkan output. Meskipun tidak
selalu, biasanya sebuah algoritma
memiliki sifat bisa dihitung (computable)
atau bisa diukur (measurable).
An algorithm is written in pseudocode,
i.e. a mixture of a natural language and
programming language-like construct
if – then
if – then – else
for – do
while – do
repeat – until
C
false
true
C
false
true
S
next statement
if – then
– else
S1
S2
next statement
if – then
for k  1 to n do
.
.
.
done n times
while – do
false
C
true
S
next statement
repeat – until
S
false
C
true
next statement
STEPS
1.
understand the problem
2.
ascertain the capabilities of the computational device
3.
choose between exact and approximate problem
solving
4.
decide on appropriate data structures
5.
determine algorithm design techniques
6.
select the method of specifying an algorithm
7.
prove the algorithm’s correctness
8.
analyze the algorithm
9.
code the algorithm
the most important problem types are:
1. sorting
rearrange items of a given list in a specific order, based on some key
2. searching
find a given search key in a given set
3. string processing
search a word in a text
next…
4. graph problems
includes graph traversal problems, shortest-path algorithms,
topological sorting, graph coloring problems
5. combinatorial problems
e.g. traveling salesman
next…
6. geometric problems
the closest-pair problem, the convex hull problem
7. numerical problems
solve equations & systems of equations, evaluate functions, etc.
PSEUDOCODE (1)
• Algorithm to display seven symbols of #
1 for i <- 1 to 7 do
2
display ”#”
3 end for
PSEUDOCODE (2)
• Algorithm to calculate Faktorial from N
1
2
3
4
5
iTemp <- 1
for i <- 1 to N do
iTemp <- iTemp*i
end for
display ”Factorial from ”,N,” is ”,iTemp
[buku utama, pseudocode 2.8b]
PSEUDOCODE (3)
• Algorithm to display 8 Fibonacci number
1
2
3
4
5
6
7
8
f1 <- 0
f2 <- 1
for i <-1 to 8 do
iFibo <- f1+f2
display ”Number ”,i,” is ”,iFibo
f1 <-f2
f2<- iFibo
end for
LATIHAN
• Buatlah sebuah pseudocode untuk
menampilkan N bilangan pertama secara
terbalik.
– contoh : 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
– Buat dengan menggunakan teknik for-do!
– Buat dengan menggunakan teknik repeatuntil!
– Buat dengan menggunakan teknik while-do!
LATIHAN
• Buat sebuah pseudocode 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.
Assignment
Algorithm Efficiency
• Description
• Parameters
• Types and description
Deadline: Sept 22, 2010
Email to : [email protected]
Subject : Algorithm Effieciency [NIM]