0% found this document useful (0 votes)
32 views

Dynamic Programming (Optimization Problem)

Dynamic programming is an optimization technique for solving problems with optimal substructure and overlapping subproblems. It works by breaking down a problem into smaller subproblems and storing the results in a table to avoid recomputing the same subproblems. This document provides examples of using dynamic programming to solve the discrete knapsack problem and coin changing problem. It explains the key steps of establishing a recursive relationship, computing values in a bottom-up manner, and constructing the optimal solution.

Uploaded by

Andi Sulasikin
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Dynamic Programming (Optimization Problem)

Dynamic programming is an optimization technique for solving problems with optimal substructure and overlapping subproblems. It works by breaking down a problem into smaller subproblems and storing the results in a table to avoid recomputing the same subproblems. This document provides examples of using dynamic programming to solve the discrete knapsack problem and coin changing problem. It explains the key steps of establishing a recursive relationship, computing values in a bottom-up manner, and constructing the optimal solution.

Uploaded by

Andi Sulasikin
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

Dynamic

Dynamic Programming
Programming
(optimization
(optimization problem)
problem)
CS3024
Lecture 10
Outline
• Optimization problem (DP vs Greedy),
e.g making change, knapsack.
• Exercises.

21/4/06 RMB/lecture 9 2
What is dynamic programming ?

• The word programming stands for planning


not for computer programming.

• The word dynamic is related with the


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

21/4/06 RMB/lecture 9 3
What is dynamic programming ?

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

21/4/06 RMB/lecture 9 4
The steps in the development
of a dynamic programming
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.

21/4/06 RMB/lecture 9 5
The principle of optimality

It is said to apply in a problem if an


optimal solution to an instance of a
problem always contains optimal
solution to all subinstances.

21/4/06 RMB/lecture 9 6
The principle of optimality

Dynamic programming may be used


only when the principle of optimality
holds.

21/4/06 RMB/lecture 9 7
Application: discrete knapsack
Hypothesis:
the capacity C and the dimensions d1,…,dn are natural numbers

The problem can be reformulated as:


find (s1,s2,…,sn) with si in {0,1} such that:
s1d1 +…+ sndn <= C (constraint)
s1p1 +…+ snpn is maximal (optimization criterion)

Remark
the greedy technique can be applied but it does not guarantee the
optimality

21/4/06 RMB/lecture 9 8
Application: discrete knapsack
Example: n=3, Greedy idea:
C=5, • Sort decreasingly the set of
d1=1, d2=2, d3=3 objects on the relative profit (pi/di)
p1=6, p2=10, p3=12 • Select the elements until the
knapsack is overloaded
Relative profit:
Greedy solution: (1,1,0)
pr1=6, pr2=5, pr3=4
Total value: V=16

Remark: this is not the optimal solution; the solution (0,1,1) is better
since V=22

21/4/06 RMB/lecture 9 9
Application: discrete knapsack
1. Analyzing the structure of an optimal solution
Let P(i,j) be the generic problem of selecting from the set of objects {o1,
…,oi} in order to fill in a knapsack of capacity j.

Remarks:
• P(n,C) is the initial problem
• If i<n, j<C then P(i,j) is a subproblem of P(n,C)
• Let s(i,j) be an optimal solution of P(i,j). There are two situations:
– si=1 (the object oi is selected) => this lead us to the subproblem P(i-1,j-
di) and if s(i,j) is optimal then s(i-1,j-di) should be optimal
– si=0 (the object oi is not selected) => this lead us to the subproblem P(i-
1,j) and if s(i,j) is optimal then s(i-1,j) should be optimal
Thus the solution s has the optimal substructure property

21/4/06 RMB/lecture 9 10
Application: discrete knapsack

2. Find a recurrence relation

Let V(i,j) be the total value corresponding to an optimal solution of P(i,j)

0 if i=0 or j=0 (the set is empty or the


knapsack has not capacity at all)

V(i,j) = V(i-1,j) if di>j or V(i-1,j)>V(i-1,j-di)+ pi


(either the object i doesn’t fit the knapsack or by
selecting it we obtain a worse solution than by not
selecting it)

V(i-1,j-di)+pi otherwise

21/4/06 RMB/lecture 9 11
Application: discrete knapsack
The recurrence relation can be written also as:

0 if i=0 or j=0

V(i,j) = V(i-1,j) if di>j

max{V(i-1,j), V(i-1,j-di)+ pi } if di<=j

Remarks:
• for the problem P(n,C) the table V has (n+1) rows and (C+1)
columns
• V(n,C) gives us the value corresponding to the optimal solution

21/4/06 RMB/lecture 9 12
Application: discrete knapsack
Example: V

0 if i=0 or j=0 0 1 2 3 4 5
0 0 0 0 0 0 0
V(i,j) = V(i-1,j) if d i>j
1 0 6 6 6 6 6
max{V(i-1,j),
V(i-1,j-di)+ pi } if di<=j 2 0 6 10 16 16 16

3 0 6 10 16 18 22
d: 1 2 3
p: 6 10 12

21/4/06 RMB/lecture 9 13
Application: discrete knapsack
3. Developing the recurrence Algorithm:
relation
computeV (p[1..n],d[1..n],C)
FOR i:=0,n DO V[i,0]:=0
0 if i=0 or j=0
FOR j:=1,n DO V[0,j]:=0
FOR i:=1,n DO
V(i,j) = V(i-1,j) if d i>j
FOR j:=1,C DO
IF j<d[i] THEN V[i,j]:=V[i-1,j]
max{V(i-1,j),
ELSE
V(i-1,j-di)+ pi } if di<=j
V[i,j]:=max(V[i-1,j],V[i-1,j-d[i]]+p[i])
RETURN V[0..n,0..C]

21/4/06 RMB/lecture 9 14
Application: discrete knapsack
4. Constructing the solution Steps:

Example: • Compare V[3,5] with V[2,5]. Since


they are different it means that the
object o3 is selected
0 1 2 3 4 5
• Go to V[2,5-d3]=V[2,2]=10 and
0 0 0 0 0 0 0 compare it with V[1,2]=6. Since
they are different it means that also
1 0 6 6 6 6 6 o2 is selected
• Go to V[1,2-d2]=V[1,0]=0. Since the
2 0 6 10 16 16 16 current capacity is 0 we cannot
select another object

3 0 6 10 16 18 22
Thus the solution is {o2,o3} or s=(0,1,1)

21/4/06 RMB/lecture 9 15
Application: discrete knapsack
4. Constructing the solution Algorithm:

Example: Construct(V[0..n,0..C],d[1..n])
FOR i:=1,n DO s[i]:=0
0 1 2 3 4 5 i:=n; j:=C
0 0 0 0 0 0 0 WHILE j>0 DO
WHILE (i>1) AND (V[i,j]=V[i-1,j])
1 0 6 6 6 6 6 DO i:=i-1
s[i]:=1
2 0 6 10 16 16 16 j:=j-d[i]
i:=i-1
3 0 6 10 16 18 22 RETURN s[1..n]

21/4/06 RMB/lecture 9 16
Problem 1
Coin changing problem

By using coins of values {v1,v2,…,vn} we want to find a subset of


coins that total the amount C and the number of used coins
is minimal

Let us suppose that vn > vn-1 > … > v1 =1

Example:
v1=1 v2=6 v3=10
C=12

Greedy solution: 10+1+1 (3 coins) Not optimal !


Better solution: 6+6 (2 coins)

21/4/06 RMB/lecture 9 17
Problem 1
• The generic problem P(i,j) asks for finding a minimal subset
of {v1,v2,…,vi} which covers the value j
An optimal solution of P(i,j) belongs to one of the classes:
– It doesn’t use the coin vi (it is identical to the optimal solution
of P(i-1,j))
– It uses the coin vi (it contains an optimal solution of P(i,j-vi))
• Let R(i,j) be the number of coins corresponding to the
optimal solution of P(i,j)
j if i=1
R(i,j)= 0 if j=0
R(i-1,j) if vi>j
min{R(i-1,j),1+R(i,j- vi )} otherwise

• Let Q(i,j) in {0,1} be an indicator of the using of coin i in


covering the value j

21/4/06 RMB/lecture 9 18
Problem 1
j if i=1
R(i,j)= 0 if j=0
R(i-1,j) if vi>j
min{R(i-1,j),1+R(i,j- vi )} if vi<=j

Example: C=12, v: 10, 6, 1


0 1 2 3 4 5 6 7 8 9 10 11 12

1 0 1 2 3 4 5 6 7 8 9 10 11 12

2 0 1 2 3 4 5 1 2 3 4 5 6 2

3 0 1 2 3 4 5 1 2 3 4 1 2 2

21/4/06 RMB/lecture 9 19
Problem 1
0 if coin i is not used in the optimal solution of P(i,j)
Q(i,j)=
1 if coin i is used in the optimal solution of P(i,j)

Example:
0 1 2 3 4 5 6 7 8 9 10 11 12

1 0 1 1 1 1 1 1 1 1 1 1 1 1

2 0 0 0 0 0 0 1 1 1 1 1 1 1

3 0 0 0 0 0 0 0 0 0 0 1 1 0

21/4/06 RMB/lecture 9 20
Problem 1 Solution
0 1 2 3 4 5 6 7 8 9 10 11 12 construction

1 0 1 2 3 4 5 6 7 8 9 10 1112
Find the minimal
2 0 1 2 3 4 5 1 2 3 4 5 6 2 value on the
last column
of R matrix
3 0 1 2 3 4 5 1 2 3 4 1 2 2 which
corresponds
to a one in Q
0 1 2 3 4 5 6 7 8 9 10 11 12 matrix
Go on the same line
1 0 1 1 1 1 1 1 1 1 1 1 1 1 on the
column
placed vi
2 0 0 0 0 0 0 1 1 1 1 1 1 1 positions to
the left and
3 0 0 0 0 0 0 0 0 0 0 1 1 0 check if the
correspondin
g value in Q
The first value equal to one starting from bottom is one …

21/4/06 RMB/lecture 9 21
Problem 1
Construct(R[1..n,0..C],Q[1..n,0..C])
FOR i:=1,n DO S[i]:=0
imin=n
WHILE Q[imin,C]=0 DO imin:=imin-1
i:=imin
j:=C
WHILE j>0 AND i>0 DO
IF Q[i,j]=1 THEN S[i]:=S[i]+1
j:=j-v[i]
ELSE i:=i-1
IF j=0 THEN RETURN S[1..n]
ELSE “ it doesn’t exist a solution”

This can happen only if v[1]<>1

21/4/06 RMB/lecture 9 22
Exercises
Compute the optimum solution for the
following knapsack problem
C=5, n=3. Determine which items are
inserted into the knapsack.
item weight Profit
1 2 $12
2 1 $10
3 3 $20
4 2 $15

21/4/06 RMB/lecture 9 23
Next lecture will be on..

… backtracking algorithm

21/4/06 RMB/lecture 9 24

You might also like