2013 4-Algoritma Design and Analysis Recurrence.ppt
1. DR. Gatot F. Hertono, MSc.
Design and Analysis of ALGORITHM
(Session 3)
2. Rekursif
Bentuk rekursif banyak digunakan di dalam rancangan suatu
algoritma
Suatu fungsi rekursif f(x): adalah suatu fungsi dimana evaluasi
untuk suatu input xi (xi bukan initial input x0) memerlukan evaluasi
fungsi dirinya sendiri untuk input xj yang lain.
Contoh:
Fact(n) = n * Fact(n-1), dengan kondisi awal: Fact(1) = 1
Fibo(n) = Fibo(n-1)+Fibo(n-2), dengan kondisi awal: Fibo(0)=0, Fibo(1)=1
Gcd(a,b)=Gcd(b,a mod b), dengan kondisi awal: Gcd(a,0) = a
3. Top-Down dan Bottom-Up
Strategi Top-Down dan Bottom-Up pada struktur rekursif
Function Fib1(n) recursive
Input: n (n 0)
Output: Bilangan Fibonacci ke-n
if n 1 then
return(n)
else
return(Fib1(n-1) + Fib1(n-2))
endif
end Fib1
Function Fib2(n)
Input: n (n 0)
Output: Bilangan Fibonacci ke-n
if n 1 then
return(n)
else
Prev:=0; Curr:=1
for i:=2 to n do
Next := Prev + Curr; Prev:=Curr; Curr := Next;
endfor
endif
return(Curr)
end Fib2
5. Recurrence Relations
Beberapa algoritma dapat memiliki versi rekursif:
Mis. Binary search, Insertion sort & Merge sort
Analisa kompleksitas algoritma rekursif lebih rumit.
Running time berupa recurrence relations
Contoh: kompleksitas Merge sort
1
n
jika
,
)
(
)
2
/
(
2
1
jika
,
)
1
(
)
(
n
n
T
n
n
T
Yang memiliki solusi: T(n) = (n log n)
Recurrences are a major tool for analysis of algorithms
7. Recurrence Relations (cont.)
Recurrence Relation
Linear Recurrence
Non-Linear Recurrence
(e.q. from divide and conquer)
t(n)= c1.t(n-1) + c2.t(n-2)+…+ ck.t(n-k) + f(n),
c1, c2, …, ck 0 konstanta
Homogen
f(n)=0
non-homogen
f(n)0
,...
2
,
1
,
0
,
1
,
,
)
1
(
),
(
)
(
.
)
( 1
k
b
b
n
c
t
n
f
b
n
t
a
n
t k
f(n) = c
f(n) = c.n
8. Linear Recurrence
First-order : t(n) = c.t(n-1) + f(n), n > 0, c0 positif konstanta,
dan suatu fungsi f(n) tertentu.
Higher-order : t(n)= c1.t(n-1) + c2.t(n-2)+…+ ck.t(n-k) + f(n),
c1, c2, …, ck 0 konstanta
Initial cond. t(0) = d0
Initial cond. t(0) = d0 , … , t(k-1) = dk-1
Suatu linier recurrence dikatakan homogeneous bila f(n) = 0, n
k-th order
9. Metode penyelesaian
• First-order linear homogeneous: t(n) = c.t(n-1), n > 0 dan t(0) = d0
solution : t(n) = d0.cn , n 0
Contoh: t(n) = 3.t(n-1), t(0) = 5 Solusi: t(n)= 5.3n
• First-order linear nonhomogeneous: t(n) = c.t(n-1) + f(n),
n > 0 dan t(0) = d0
solution :
n
i
i
n
n
i
f
c
c
d
n
t
2
1
0 )
(
.
.
)
(
Contoh: W(n) = W(n-1) + (n-1), init. cond. W(1)= 0
Solusi:
n
i
i
n
n
n
n
i
n
t
2
2
1
2
/
2
/
)
1
.(
1
0
.
1
)
(
10. Metode Penyelesaian (lanj.)
,...
2
,
1
,
0
,
1
,
,
)
1
(
),
(
)
(
.
)
( 1
k
b
b
n
c
t
n
f
b
n
t
a
n
t k
.
1
jika
,
log
.
,
1
jika
,
1
).
1
(
)
(
1
log
1
a
n
c
c
a
a
c
n
a
a
c
n
t
b
a
b
Recurrence relation yang muncul dari suatu teknik algoritma
divide-and-conquer :
Solusi:
Jika f(n) = c
Jika f(n) = c.n
jika
,
1
)
/
(
.
.
jika
,
log
.
.
.
)
( log
log
1
1
b
a
b
a
n
n
c
n
c
b
a
n
n
c
n
c
n
t a
a
b
b
b
11. Metode Penyelesaian (lanj.)
Proposisi:
1. Bila t(n) adalah fungsi eventually non-decreasing dan berbentuk:
t(n) = a.t(n/b)+c, dengan init.cond. t(1)=c1 , a,b,c, c1 konstanta positif
dan b > 1, maka:
Sebuah fungsi h(n) dikatakan eventually non-decreasing bila
0
0 untuk
,
)
(
)
(
integer n
n
m
m
h
n
h
n
1
),
(log
1
),
(
)
(
log
a
n
a
n
n
t
a
b
12. Metode Penyelesaian (lanj.)
2. Bila t(n) adalah fungsi eventually non-decreasing dan berbentuk:
t(n) = a.t(n/b)+c.n, dengan init.cond. t(1)=c1 , a,b,c, c1 konstanta positif
dan b > 1, maka:
b
a
n
b
a
n
n
b
a
n
n
t
a
b
),
(
),
log
.
(
),
(
)
(
log
Contoh: worst case untuk Merge-sort
W(n) = W(n/2)+1, W(1) = 1
Solusi W(n) ?
W(n) (?)
21. Metode Penyelesaian Lainnya
Metode penyelesaian yang lain:
1. Metode substitusi
2. Metode iterasi
3. Metode master
Master Theorem (see Cormen):
Misal a 1, b 1 adalah konstanta, dan f(n) adalah suatu fungsi.
Misalkan t(n) adalah suatu recurrence relation berbentuk
t(n) = a.t(n/b) + f(n)
Maka t(n) memiliki batas-batas asymptot sbb:
1. Jika f(n)=O(n log
b
a-), untuk beberapa >0, maka t(n)=(n log
b
a).
2. Jika f(n)=(n log
b
a), maka t(n)=(n log
b
a log n).
3. Jika f(n)=(n log
b
a+) , untuk beberapa >0, dan jika
a.f(n/b) c.f(n), untuk c < 1, maka t(n)= (f(n)).