• Tidak ada hasil yang ditemukan

Dynamic Programming Matakuliah Desain & Analisis Algoritma (CS 3024) ZK Abdurahman Baizal STT Telkom Bandung

N/A
N/A
Protected

Academic year: 2021

Membagikan "Dynamic Programming Matakuliah Desain & Analisis Algoritma (CS 3024) ZK Abdurahman Baizal STT Telkom Bandung"

Copied!
47
0
0

Teks penuh

(1)

Dynamic Programming

Matakuliah Desain & Analisis Algoritma (CS 3024)

ZK Abdurahman Baizal STT Telkom Bandung

(2)

 Ditemukan oleh Seorang matematikawan

AS, Richard Bellman tahun 1950

Kata Programming lebih mengacu ke

planning, dan bukan mengacu ke

pemrograman komputer

The word dynamic is related with the

manner in which the tables used in obtaining the solution are constructed

(3)

Pemrograman Dinamis adalah sebuah teknik

untuk menyelesaikan masalah dengan cara

membagi masalah dalam beberapa sub masalah yang tidak saling independent (istilah

lain:overlapping subproblem) [Levitin]

Pemrograman Dinamis (dynamic

programming): metode pemecahan masalah dengan cara menguraikan solusi menjadi

sekumpulan langkah (step) atau tahapan (stage) sedemikian sehingga solusi dari persoalan dapat dipandang dari serangkaian keputusan yang

(4)

 Dynamic programming strategy is related with

divide and conquer strategy because both of

them are based on dividing a problem in subproblems. However there are some

differences between these two approaches:  in divide and conquer approaches the

subproblems are usually independent so all of them have to be solved

 in dynamic programming approaches the

subproblems are dependent (overlapping) so we need the result obtained for a

subproblem many times (so it is important to store it)

(5)

 Divide & Conquer menggunakan

pendekatan top down

 Pemrograman Dinamis :

 Pada umumnya menggunakan pendekatan bottom up

 Pendekatan Top down + bottom up  memory functions (lihat levitin halaman 295)

(6)

The steps in the development of a

dynamic programming

1. Establish a recursive property that

gives the solution to an instance of the problem

2. Solve an instance of the problem in a

bottom-up

fashion by solving smaller instance first

(7)

Bilangan Fibonacci

 Bilangan Fibonacci

0,1, 1, 2, 3, 5, 8, 13, 21, 34,…

 Dengan relasi recurrence :

0 untuk n=0 F(n) = 1 untuk n=1

(8)

Fibonacci using

Divide-and-Conquer (Top down approach)

problem: determine the nth term in the fibo

sequence

inputs: a nonnegative integer n

outputs: fib, the nth term of the fibo sequence function fib(n:integer):integer; begin if (n=1) or (n=2) then fib:=1 else fib:=fib(n-1)+fib(n-2) end end;

(9)
(10)

n

th Fibonacci using Dynamic Programming (Bottom up Approach)

1. Establish a recursive property in terms of F, it

is

 

            3 ] 2 [ ] 1 [ 2 1 1 1 n n F n F n n n F

(11)

n

th Fibonacci using Dynamic Programming (Bottom up Approach)

2. Solve the instance of the problem by

computing the 1th and 2nd term of fibonacci sequence Example. F[5] Compute 1th term: F[1]=1 Compute 2nd term: F[2]=1 Compute 3rd term: F[3]=F[2]+F[1]=2 Compute 4th term: F[4]=F[3]+F[2]=3 Compute 5th term: F[5]=F[4]+F[3]=5

(12)

n

th Fibonacci using Dynamic

Programming (Bottom up Approach)

problem: determine the nth term in the fibo sequence

inputs: a nonnegative integer n

outputs: fib2, the nth term of the fibo sequence

function fib2(n:integer):integer;

var f: array[1..n] of integer; i: index; begin f[1]:=1; if n > 1 then begin f[2]:=1; for i := 3 to n begin

f[i]:=f[i-1]+f[i-2] end

end fib2:=f[n]; end;

(13)
(14)

Binomial Coefficient

Teorema Binomial :

(x + y)n =

C(n, 0) xn + C(n, 1) xn-1 y1 + … + C(n, k) xn-k yk

+ … + C(n, n) yn

Koefisien untuk

x

n-k

y

k adalah C(n, k).

Bilangan C(n, k) disebut koefisien

(15)

Computing a binomial coefficient C(n,k) 1 if k=0 or n=k

C(n,k)=

(16)

Binomial Coefficient using Divide-and-Conquer (Top down approach)

comb(n,k)

//Problem : Compute the binomial coefficient

//Input : nonnegatif integers n and k, where k<=n //Output :comb, the binomial coefficient C(n,k)

IF (k=0) OR (n=k) THEN RETURN 1

ELSE

(17)

Binomial Coefficient using Dinamic Programming (Bottom up approach) Establish a recursive property

1 if k=0 or n=k C[n,k]=

(18)

Bottom-up approach: constructing the Pascal’s triangle 0 1 2 3 4 … k-1 k 0 1 1 1 1 2 1 2 1 3 1 3 3 1 4 1 4 6 4 1 … 1 … 1 … n-1 1 C(n-1,k-1) C(n-1,k) n 1 C(n,k)

Binomial Coefficient using Dinamic Programming (Bottom up Approach)

(19)

Binomial Coefficient using Dinamic Programming (Bottom up approach)

Algorithm :

Comb(n,k)

//Problem : Compute the binomial coefficient

//Input : nonnegative integers n and k, where k<=n //Ouput : Comb, the binomial coefficient C(n,k)

FOR i:=0 to n DO FOR j:=0,min{i,k} DO IF (j=0) OR (j=k) THEN C[i,j]:=1 ELSE C[i,j]:=C[i-1,j]+C[i-1,j-1]

(20)

Binomial Coefficient using Dinamic Programming (Bottom up approach)

Contoh : Compute C[4,2] ! Compute row 0 : C[0,0] = 1 Compute row 1 : C[1,0] = 1 C[1,1] = 1 Compute row 2 : C[2,0] = 1 C[2,1] = C[1,0]+C[1,1]=1+1=2

(21)

Binomial Coefficient using Dinamic Programming (Bottom up approach) Compute row 3 : C[3,0] = 1 C[3,1] = C[2,0]+C[2,1]=1+2=3 C[3,2] = C[2,1]+C[2,2]=2+1=3 Compute row 4 : C[4,0] = 1 C[4,1] = C[3,0]+C[3,1]=1+3=4 C[4,2] = C[3,1]+C[3,2]=3+3=6 Jadi, C[4,2] = 6

(22)

Warshall’s Algorithms

 Warshall’s Algorithms digunakan untuk

menentukan transitif closure dari suatu graf berarah

 Transitif closure  suatu matriks yang

berisi informasi tentang keberadaan

lintasan antar vertex dalam sebuah graf berarah.

(23)

Warshall’s Algorithms

 Warshall’s algorithm membentuk transitif

closure dari graf dengan n vertex melalui sederetan matriks boolean

R(0),...., R(k-1), R(k),...., R(n)

 Element r(k)[i,j] of matrix R(k) is equal to 1 if

only if there exists a directed path from vertex i to the vertex j with intermediate vertex, if any, numbered not higher than k.

k ij

(24)

Warshall’s Algorithms

 Recursive property:

r(k)[i,j] = r(k-1)[i,j] OR

(25)

Warshall’s Algorithms

Given graf : a b d c a b c d a 0 1 0 0 b 0 0 0 1  adjacency c 0 0 0 0 matrix d 1 0 1 0 a b c d a 1 1 1 1 b 1 1 1 1  transitive c 0 0 0 0 closure d 1 1 1 1

(26)

Warshall’s Algorithms

 Algorithm

Warshall(A[1..n, 1..n])

//Input : The adjacency matrix A of digraph //Output : The transitive closure of digraph R(0) A

for k  1 to n do for i  1 to n do

for j  1 to n do

r(k)[i,j ]= r(k-1)[i,j] OR (r(k-1)[i,k] AND r(k-1)[k,j]) return R(k)

(27)

Warshall’s Algorithms

Given graf : a b d c a b c d a 0 1 0 0 b 0 0 0 1  R(0) c 0 0 0 0 d 1 0 1 0 a b c d a 0 1 0 0 b 0 0 0 1  R(1) c 0 0 0 0 d 1 1 1 0

Boxes are used for getting R(k+1)

(28)

Warshall’s Algorithms

Given graf : a b d c a b c d a 0 1 0 1 b 0 0 0 1  R(2) c 0 0 0 0 d 1 0 1 1 a b c d a 0 1 0 1 b 0 0 0 1  R(3) c 0 0 0 0 d 1 1 1 1

Boxes are used for getting R(k+1)

(29)

Warshall’s Algorithms

Given graf : a b d c a b c d a 1 1 1 1 b 1 1 1 1  R(4) c 0 0 0 0 Transitive d 1 1 1 1 Closure

(30)

Dynamic Programming and

Optimization Problems

 Dynamic programming is also related to

greedy strategy since both of them can be

applied to optimization problems which have the property of optimal substructure

(31)

Prinsip Optimalitas

 Pada program dinamis, rangkaian

keputusan yang optimal dibuat dengan menggunakan Prinsip Optimalitas.

 Prinsip Optimalitas:

jika solusi total

optimal, maka bagian solusi sampai tahap

ke-k juga optimal.

(32)

 Prinsip optimalitas berarti bahwa jika kita

bekerja dari tahap k ke tahap k + 1, kita dapat menggunakan hasil optimal dari tahap k tanpa harus kembali ke tahap awal.

 ongkos pada tahap k +1 =

(ongkos yang dihasilkan pada tahap k ) + (ongkos dari tahap k ke tahap k + 1)

(33)

 Dengan prinsip optimalitas ini dijamin

bahwa pengambilan keputusan pada suatu tahap adalah keputusan yang benar untuk tahap-tahap selanjutnya.

 Pada metode greedy hanya satu rangkaian

keputusan yang pernah dihasilkan,

sedangkan pada metode program dinamis lebih dari satu rangkaian keputusan.

Hanya rangkaian keputusan yang

memenuhi prinsip optimalitas yang akan dihasilkan.

(34)

The steps in the development of a dynamic programming (on optimization Problems)

1. Establish a recursive property that gives the

optimal solution to an instance of the problem.

2. Compute the value of an optimal solution in a

bottom-up fashion.

3. Construct an optimal solution in a bottom-up

(35)

The 0/1 Knapsack Problem

 Let us consider an instance defined by the

first i items 1 ≤ i ≤ n

 Weights w1,w2,..,wn  Values v1,v2,….,vn

 Knapsack capasity j, 1 ≤ j ≤ W

 V[i,j]  value of an optimal solution to

(36)

All subsets of first i items that fit into knapsack of capacity j divide into 2 categories :

 Among subsets that do not include the i

item  V[i-1,j]

 Among the subsets that do include ith

item (hence, j-wi ≤0)  vi+V[i-1,j-wi]

(37)

 Recurrence property :

V[i-1,j] if j-wi <0 V[i,j]=

max{V[i-1,j], vi+V[i-1,j-wi]} if j-wi ≥0 Initial conditions :

V[0,j]=0 dan V[i,0]=0 i,j ≥ 0

(38)

Contoh Kasus Knapsack

item Weight value

1 2 $12 2 1 $10 3 3 $20 4 2 $15 Knapsack Kapacity

W=5

(39)

Constructing the solution Example: 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 12 12 12 12 2 0 10 12 22 22 22 3 0 10 12 22 30 32 Steps:

 Compare V[4,5] with V[3,5]. Since

they are different it means that the object item 4 is selected

 Go to V[3,5-w4]=V[3,3]=22 and

compare it with V[2,3]=22. Since they are equal it means that item 3 is not selected

 Go to V[2,3]<>V[1,3]. it means that

also item 2 is selected

 Go to V[1,3-w2]=V[1,2]=12 and

compare it with V[0,2]=0. Since they are different it means that the object item 1 is selected

(40)

Floyd’s Algorithm

 Floyd’s Algorithm digunakan untuk mencari jarak

dari masing-masing vertex ke semua vertex

yang lain dalam sebuah graf terhubung (berarah maupun tidak berarah) yang sering juga disebut

all-pairs shortest-path problem

 Elemen d[i,j] dari sebuah distance matrix

menyatakan panjang lintasan terpendek dari vertex i ke vertex j

(41)

Floyd’s Algorithm

Given Graph : a b d c 2 6 7 1 3 a b c d a 0 ∞ 3 ∞ b 2 0 ∞ ∞  weight matrix c ∞ 7 0 1 d 6 ∞ ∞ 0 a b c d a 0 ∞ 3 ∞ b 2 0 ∞ ∞  distance matrix c ∞ 7 0 1 d 6 ∞ ∞ 0

(42)

Floyd’s Algorithm

 Floyd’s algorithm computes distance

matrix of a wighted graph with n vertces through a series of n-by-n matrices:

D(0),….,D(k-1),D(k),….,D(n)

 Element d(k)[i,j] of D(k) is the length of

shortest path among all path from vertex i to the vertex j with each intermediate

(43)

Floyd’s Algorithm

 Recurrence property :

min{d(k-1)[i,j], d(k-1)[i,k]+d(k-1)[k,j]}

d(k)[i,j]= for k≥1

(44)

Floyd’s Algorithm

 Algorithm

Floyd(W[1..n,1..n]

//input : The weight matrix W of a graph

//output : The distance matrix of the shortest path length DW

for k 1 to n do for i 1 to n do

for j 1 to n do

D[i,j]  min{D[i,j], D[i,k]+D[k,j]} return D

(45)

Floyd’s Algorithm

weighted graf : a b d c 2 6 7 1 3 a b c d a 0 ∞ 3 ∞ b 2 0 ∞ ∞  D(0) c ∞ 7 0 1 d 6 ∞ ∞ 0 a b c d a 0 ∞ 3 ∞ b 2 0 5 ∞  D(1) c ∞ 7 0 1 d 6 ∞ 9 0 Boxes are used for

(46)

Floyd’s Algorithm

weighted graf : a b d c 2 6 7 1 3 a b c d a 0 ∞ 3 ∞ b 2 0 5 ∞  D(2) c 9 7 0 1 d 6 ∞ 9 0 a b c d a 0 10 3 4 b 2 0 5 6  D(3) c 9 7 0 1 d 6 16 9 0 Boxes are used for

(47)

Floyd’s Algorithm

weighted graf : a b d c 2 6 7 1 3 a b c d a 0 10 3 4 b 2 0 5 6  D(4) c 7 7 0 1  distance matrix d 6 16 9 0

Referensi

Dokumen terkait

Puji dan syukur penulis panjatkan kehadirat Allah SWT yang telah melimpahkan segala rahmat dan karunia-Nya, sehingga penulis dapat menyelesaikan laporan tugas akhir

Adapun rumusan masalah dalam penelitian ini adalah : 1) Bagaimanakah gambaran tingkat kompetensi interpersonal siswa sebelum dilaksanakan layanan bimbingan kelompok?

Keberadaan lembaga bantuan hukum sebagai bagian dari sebuah sistem hukum memiliki andil yang positif bagi pemenuhan akan rasa keadilan masyarakat, melalui

Multiplikasi tanaman stevia telah dilakukan dalam berbagai penelitian, diantaranya perbanya- kan stevia menggunakan sumber eksplan pucuk dengan media Lismainer dan Skoog

Indonesia Comnets Plus (Icon+) terutama pada Wireless Local Area Network yaitu dengan menggunakan password yang telah di enkripsikan pada masing-masing Access Point,

Hasil penelitian yang telah dikembangkan dalam bentuk media pembelajaran interaktif berpotensi digunakan dalam pembelajaran pada KD 3.9 yaitu menganalisis informasi/ data

triliun. Angka tersebut harusnya dapat berdampak luar biasa dalam upaya pengentasan kemiskinan di Indonesia. Namun demikian laporan BAZNAS mengungkapkan bahwa dari

Di pihak lain langkah terapi yang dilakukan oleh pihak PG juga seder- hana, yakni menaikkan harga provenu, sehingga pengusaha gula merah tidak mampu membeli bahan baku, atau