Chapter - 12 (Dynamic Programming)
Chapter - 12 (Dynamic Programming)
Chapter 12
Dynamic Programming
We can divide all the subsets of the first i items that fit the knapsack of
capacity j into two categories: those that do not include the i th item and
those that do.
Among the subsets that do not include the ith item, the value of an
optimal subset is, by definition, F(i − 1, j).
Among the subsets that do include the ith item (hence, j − wi ≥ 0), an
optimal subset is made up of this item and an optimal subset of the first
i − 1 items that fits into the knapsack of capacity j − wi . The value of
such an optimal subset is vi + F(i − 1, j − wi).
Thus, the value of an optimal solution among all feasible subsets of the
first i items is the maximum of these two values.
Example:
capacity j
i 0 1 2 3 4 5
0 0 0 0 0 0 0
w1 = 2, v1 = 12 1 0 0 12 12 12 12
w2 = 1, v2 = 10 2 0 10 12 22 22 22
w3 = 3, v3 = 20 3 0 10 12 22 30 32
w4 = 2, v4 = 15 4 0 10 25 35 37 47
Thus, the maximal value is F(4, 5) = $47. We can find the composition of
an optimal subset by back tracing the computations of this entry in the
table.
Since F(4, 5) > F(3, 5), item 4 has to be included in an optimal solution
along with an optimal subset for filling 5 − 2 = 3 remaining units of the
knapsack capacity.
The value of the latter is F(3, 3). Since F(3, 3) = F(2, 3), item 3 need not be
in an optimal subset.
Since F(2, 3) > F(1, 3), item 2 is a part of an optimal selection, which
leaves element F(1, 3 − 1) to specify its remaining composition.
Similarly, since F(1, 2) > F(0, 2), item 1 is the final part of the optimal
solution {item 1, item 2, item 4}.
Example:
Use Warshall’s algorithm to find the transitive closure matrix
of the following digraph.
Thank You!