Dynamic Programming - Simulation...

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

Transcript of Dynamic Programming - Simulation...

Dynamic ProgrammingMatakuliah Desain & Analisis Algoritma

(CS 3024)

ZK Abdurahman Baizal

STT Telkom Bandung

ZKA-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

ZKA-STT Telkom Bandung 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 saling berkaitan [diktat Rinaldi]

ZKA-STT Telkom Bandung 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)

ZKA-STT Telkom Bandung 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)

ZKA-STT Telkom Bandung 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

ZKA-STT Telkom Bandung 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

F(n-1)+F(n-2) untuk n>=2

ZKA-STT Telkom Bandung 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;

ZKA-STT Telkom Bandung 9

Ilustrasi

ZKA-STT Telkom Bandung 10

nth 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

nnFnF

n

n

nF

ZKA-STT Telkom Bandung 11

nth Fibonacci using Dynamic Programming (Bottom up Approach)

2. Solve the instance of the problem by computing the 1th and 2nd term of fibonacci sequenceExample. F[5]Compute 1th term: F[1]=1Compute 2nd term: F[2]=1Compute 3rd term: F[3]=F[2]+F[1]=2Compute 4th term: F[4]=F[3]+F[2]=3Compute 5th term: F[5]=F[4]+F[3]=5

ZKA-STT Telkom Bandung 12

nth Fibonacci using Dynamic Programming (Bottom up Approach)

problem: determine the nth term in the fibo sequenceinputs: a nonnegative integer noutputs: fib2, the nth term of the fibo sequence

function fib2(n:integer):integer;var

f: array[1..n] of integer;i: index;

beginf[1]:=1;if n > 1 then

beginf[2]:=1;for i := 3 to n

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

endend

fib2:=f[n];end;

ZKA-STT Telkom Bandung 13

Ilustrasi

ZKA-STT Telkom Bandung 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 xn-kyk adalah C(n, k). Bilangan C(n, k) disebut koefisien binomial.

ZKA-STT Telkom Bandung 15

Computing a binomial coefficient C(n,k)

1 if k=0 or n=k

C(n,k)=

C(n-1,k)+C(n-1,k-1) otherwise

ZKA-STT Telkom Bandung 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

RETURN comb(n-1,k)+comb(n-1,k-1)

ZKA-STT Telkom Bandung 17

Binomial Coefficient using Dinamic Programming (Bottom up approach)

Establish a recursive property

1 if k=0 or n=k

C[n,k]=

C[n-1,k]+C[n-1,k-1] otherwise

ZKA-STT Telkom Bandung 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)

ZKA-STT Telkom Bandung 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]

RETURN C[n,k]

ZKA-STT Telkom Bandung 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

ZKA-STT Telkom Bandung 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

ZKA-STT Telkom Bandung 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.

ZKA-STT Telkom Bandung 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

ijr

ZKA-STT Telkom Bandung 24

Warshall’s Algorithms

Recursive property:

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

(r(k-1)[i,k] AND r(k-1)[k,j])

ZKA-STT Telkom Bandung 25

Warshall’s Algorithms

Given graf :

a b

dc

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

ZKA-STT Telkom Bandung 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)

ZKA-STT Telkom Bandung 27

Warshall’s Algorithms

Given graf :

a b

dc

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)

ZKA-STT Telkom Bandung 28

Warshall’s Algorithms

Given graf :

a b

dc

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)

ZKA-STT Telkom Bandung 29

Warshall’s Algorithms

Given graf :

a b

dc

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

ZKA-STT Telkom Bandung 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

ZKA-STT Telkom Bandung 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.

ZKA-STT Telkom Bandung 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)

ZKA-STT Telkom Bandung 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.

ZKA-STT Telkom Bandung 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 fashion.

ZKA-STT Telkom Bandung 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 the instance

ZKA-STT Telkom Bandung 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]

The 0/1 Knapsack Problem

ZKA-STT Telkom Bandung 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

The 0/1 Knapsack Problem

ZKA-STT Telkom Bandung 38

Contoh Kasus Knapsack

item Weight value

1 2 $12

2 1 $10

3 3 $20

4 2 $15

Knapsack Kapacity

W=5

ZKA-STT Telkom Bandung 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

4 0 10 15 25 30 37

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

Thus the solution is {1,2,4} or s=(1,1,0,1)

Contoh Kasus Knapsack

ZKA-STT Telkom Bandung 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

ZKA-STT Telkom Bandung 41

Floyd’s Algorithm

Given Graph :

a b

dc

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

ZKA-STT Telkom Bandung 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 vertex, if any, numbered not higher than k

ZKA-STT Telkom Bandung 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

d(k)[i,j]=w[i,j] for k=0

ZKA-STT Telkom Bandung 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

DW

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

ZKA-STT Telkom Bandung 45

Floyd’s Algorithm

weighted graf :

a b

dc

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 getting D(k+1)

ZKA-STT Telkom Bandung 46

Floyd’s Algorithm

weighted graf :

a b

dc

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 getting D(k+1)

ZKA-STT Telkom Bandung 47

Floyd’s Algorithm

weighted graf :

a b

dc

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