Docsity

Lecture Notes on Dynamic Programming | CSCD 316, Study notes of Computer Science

Toggle sidebar
bg1
COSC242 Lecture 23 Slide 1
Dynamic programming
Dynamic programming
• is used for
optimisation
problems, where we want to
find the ‘best way’ of doing something;
• is a recursive approach that involves breaking a
global problem down into more local subproblems;
• assumes
optimal substructure
i.e. that there is a
simple way to combine optimal solutions of
subproblems to get an optimal global solution;
• avoids the inefficiency that straightforward recursion
may suffer from
subproblem overlap
(i.e. when
decomposition results in the sam e subproblems
occurring often and being solved many times) by:
•
memoizing
(i.e. storing the solutions of subproblems
in a table and then looking them up) and:
• computing the table bottom up rather than top down.
Dynamic programming often produces a polynomial-
time algorithm for finding the optimal solution when
brute force enumeration of possibilities would be
exponential.
COSC242 Lecture 23 Slide 2
Solving the 0-1 knapsack problem
Given
: a set S = {s1, s2, …, s
n
} of
n
items where each s
i
has
integer value
vi
and integer weight
wi
.
Required
: to choose a subset O of S such that the total
weight of the items chosen does not exceed W and the sum
of the values
vi
of items in O is maximal.
How can we break the global problem down into
subproblems in a way that gives subproblem optimality?
Suppose the optimal solution for S and W is a subset O in
which s
k
is the highest numbered item.
Then O-{s
k
} is an optimal solution for S
k
-1 = {s1, …, s
k
-1}
and total weight W-
wk
. And the
value
of the global solution
O is
vk
plus the value of the subproblem solution.
Given a target weight
w
and a set S
k
= {s1, …, s
k
} imagine
examining all the subsets of S
k
whose total weight is
£
w
.
Some of these subsets might have bigger total values than
others. Let V[
k
,
w
] be the biggest total value of such a
subset of S
k
. Now we give a recursive definition of V[
k
,
w
].
V
[k,
w
] = 0 if either
k
= 0 or
w
= 0, otherwise
V[
k
,
w
] = if
wk
>
w
then V[
k
-1,
w
]
else max{V[
k
-1,
w
],
vk
+ V[
k
-1,
w
-
wk
]}
COSC242 Lecture 23 Slide 3
The recursive definition of V[
k
,
w
]
The recursive definition of V[
k
,
w
] says that the value
of a solution for stage S
k
and target weight
w
either
• includes item s
k
, in which case it is
vk
plus a
subproblem solution for S
k
-1 and total weight
w
-
wk
, or
• doesn’t include s
k
, in which case it is just a
subproblem solution for S
k
-1 and the same weight
w
.
The thief can solve the problem by considering the
total set S
n
and weight W deriving its solution from
that of the subproblems S
n
-1 and weight W-
wn
or S
n
-1
and weight W. These subproblems in turn …
At each stage S
k
,there are two choices to be compared
and the better one made:
1. If the thief picks item s
k
then she gets the value
vk
and can choose from items s1, …, s
k
-1 up to the weight
limit
w
-
wk
, getting additional value V[
k
-1,
w
-
wk
],
for a total value
vk
+ V[
k
-1,
w
-
wk
].
2. If the thief decides not to take item s
k
then she can
choose from items s1, …, s
k
-1 up to the weight limit
w
,
and get the value V[
k
-1,
w
].
COSC242 Lecture 23 Slide 4
Using a table
Inputs = max weight W, item values (
v
1,
v
2, …,
vn
) and
weights (
w
1,
w
2, …,
wn
).
We can solve the problem by recursively computing
V[
k
,
w
]. But to avoid subproblem overlap dynamic
programming uses a
bottom-up table
—
The values V[
k
,
w
] are stored in a table V[0..
n
, 0..W]
whose entries are computed in row-major order (i.e.
the first row of V is filled in from left to right, then the
second row, and so on). This bottom-up approach
ensures that subproblems are solved once only.
At the end of the computation, V[
n
, W] contains the
maximum value the thief can take.
Tracing the choices — the subset of items to take can
be recovered from the table V by starting at V[
n
, W]
and tracing where the optimal values came from.
If V[
k
,
w
] = V[
k
-1,
w
] then item s
k
is not part of the
solution and we continue tracing with V[
k
-1,
w
].
Otherwise item s
k
is part of the solution and we
continue tracing with V[
k
-1,
w
-
wk
].
pf2

Partial preview of the text

Download Lecture Notes on Dynamic Programming | CSCD 316 and more Study notes Computer Science in PDF only on Docsity!

COSC242 Lecture 23 Slide 1

Dynamic programming

Dynamic programming

  • is used for optimisation problems, where we want to find the ‘best way’ of doing something;
  • is a recursive approach that involves breaking a global problem down into more local subproblems;
  • assumes optimal substructure i.e. that there is a simple way to combine optimal solutions of subproblems to get an optimal global solution;
  • avoids the inefficiency that straightforward recursion may suffer from subproblem overlap (i.e. when decomposition results in the same subproblems occurring often and being solved many times) by:
  • memoizing (i.e. storing the solutions of subproblems in a table and then looking them up) and:
  • computing the table bottom up rather than top down.

Dynamic programming often produces a polynomial- time algorithm for finding the optimal solution when brute force enumeration of possibilities would be exponential.

COSC242 Lecture 23 Slide 2

Solving the 0-1 knapsack problem

Given: a set S = {s 1 , s 2 , …, s n } of n items where each s i has integer value vi and integer weight wi. Required: to choose a subset O of S such that the total weight of the items chosen does not exceed W and the sum of the values vi of items in O is maximal. How can we break the global problem down into subproblems in a way that gives subproblem optimality? Suppose the optimal solution for S and W is a subset O in which s k is the highest numbered item. Then O-{s k } is an optimal solution for S k -1 = {s 1 , …, s k -1} and total weight W- wk. And the value of the global solution O is vk plus the value of the subproblem solution. Given a target weight w and a set S k = {s 1 , …, s k } imagine examining all the subsets of S k whose total weight is £ w. Some of these subsets might have bigger total values than others. Let V[ k , w ] be the biggest total value of such a subset of S k. Now we give a recursive definition of V[ k , w ]. V [k, w ] = 0 if either k = 0 or w = 0, otherwise V [ k , w ] = if wk > w then V[ k -1, w ] else max{V[ k -1, w ], vk + V[ k -1, w - wk ] }

COSC242 Lecture 23 Slide 3

The recursive definition of V[k,w]

The recursive definition of V [ k , w ] says that the value of a solution for stage S k and target weight w either

  • includes item s k , in which case it is vk plus a subproblem solution for S k -1 and total weight w - wk , or
  • doesn’t include s k , in which case it is just a subproblem solution for S k -1 and the same weight w.

The thief can solve the problem by considering the total set S n and weight W deriving its solution from that of the subproblems S n -1 and weight W- wn or S n - and weight W. These subproblems in turn …

A t each stage S k ,there are two choices to be compared and the better one made:

  1. If the thief picks item s k then she gets the value vk and can choose from items s 1 , …, s k -1 up to the weight limit w - wk , getting additional value V [ k -1, w - wk ], for a total value vk + V[ k -1, w - wk ].
  2. If the thief decides not to take item s k then she can choose from items s 1 , …, s k -1 up to the weight limit w , and get the value V[ k -1, w ].

COSC242 Lecture 23 Slide 4

Using a table

Inputs = max weight W, item values ( v 1 , v 2 , …, vn ) and weights ( w 1 , w 2 , …, wn ). We can solve the problem by recursively computing V [ k , w ]. But to avoid subproblem overlap dynamic programming uses a bottom-up table — The values V[ k , w ] are stored in a table V[0.. n , 0..W] whose entries are computed in row-major order (i.e. the first row of V is filled in from left to right, then the second row, and so on). This bottom-up approach ensures that subproblems are solved once only. At the end of the computation, V[ n , W] contains the maximum value the thief can take. Tracing the choices — the subset of items to take can be recovered from the table V by starting at V[ n , W] and tracing where the optimal values came from. If V[ k , w ] = V[ k -1, w ] then item s k is not part of the solution and we continue tracing with V[ k -1, w ]. Otherwise item s k is part of the solution and we continue tracing with V[ k -1, w - wk ].

COSC242 Lecture 23 Slide 5

Pseudocode

Dynamic_01_knapsack( n , W, v 1 , …, vn , w 1 , …, wn )

  1. for w from 0 to W, set V [0, w ] = 0
  2. for k from 1 to n
  3. set V[ k , 0] = 0
  4. for w from 1 to W
  5. if wk > w
  6. then set V[ k , w ] = V[ k -1, w ]
  7. else
  8. if V[ k -1, w ] > vk + V[ k -1, w - wk ]
  9. then set V[ k , w ] = V[ k -1, w ]
  10. else set V[ k , w ] = vk + V[ k -1, w - wk ]

The algorithm describes how the table is filled in.

It doesn’t describe how to trace the choices.

COSC242 Lecture 23 Slide 6

Example

Apply the algorithm to solve the 0-1 knapsack problem: s 1 : v 1 = 60 w 1 = 10 s 2 : v 2 = 100 w 2 = 20 s 3 : v 3 = 120 w 3 = 30

3

2

1

0

w 0 10 20 30 40 50 k