Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

1

Chapter 12
Dynamic Programming

Prepared By: Eyob S.


Introduction
2

 It was invented by a prominent U.S. mathematician, Richard


Bellman, in the 1950s as a general method for optimizing
multistage decision processes.

 Dynamic programming is a technique for solving problems with


overlapping sub problems. Typically, these sub problems arise
from a recurrence relating a given problem’s solution to solutions
of its smaller sub problems.

 Rather than solving overlapping sub problems again and again,


dynamic programming suggests solving each of the smaller sub
problems only once and recording the results in a table from
which a solution to the original problem can then be obtained.
Prepared By: Eyob S.
The Knapsack Problem
3

 Here we design a dynamic programming algorithm for the knapsack


problem: given n items of known weights w1, . . . , wn and values
v1, . . . , vn and a knapsack of capacity W, find the most valuable subset
of the items that fit into the knapsack.
 We assume all the weights and the knapsack capacity are positive
integers; the item values do not have to be integers.
 Let us consider an instance defined by the first i items, 1≤ i ≤ n, with
weights w1, . . . , wi, values v1, . . . , vi , and knapsack capacity j, 1 ≤ j ≤
W.
 Let F(i, j) be the value of an optimal solution to this instance, i.e., the
value of the most valuable subset of the first i items that fit into the
knapsack of capacity j.
Prepared By: Eyob S.
Continued…
4

 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.

Prepared By: Eyob S.


Continued…
5

Example:

Prepared By: Eyob S.


Continued…
6

 The dynamic programming table is filled as shown below :

  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

Prepared By: Eyob S.


Continued…
7

 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}.

Prepared By: Eyob S.


Warshall’s Algorithm
8

 Warshall’s algorithm is used for computing the transitive


closure of a directed graph.

 Recall that the adjacency matrix A = {aij} of a directed graph


is the Boolean matrix that has 1 in its ith row and jth column if
and only if there is a directed edge from the ith vertex to the jth
vertex.

 The transitive closure of a directed graph with n vertices can


be defined as the n × n Boolean matrix T = {tij}, in which the
element in the ith row and the jth column is 1 if there exists a
nontrivial path (i.e., directed path of a positive length) from
the ith vertex to the jth vertex; otherwise, tij is 0.

Prepared By: Eyob S.


Continued…
9

Prepared By: Eyob S.


Continued…
10

Example:
Use Warshall’s algorithm to find the transitive closure matrix
of the following digraph.

Prepared By: Eyob S.


Solution:
11

Prepared By: Eyob S.


Floyd’s Algorithm
12

 Floyd’s algorithm is used for computing the all-pairs shortest-paths


problem.
 Given a weighted connected graph (undirected or directed), the all-
pairs shortest paths problem asks to find the distances — i.e., the
lengths of the shortest paths — from each vertex to all other vertices.
 Because of its important applications to communications,
transportation networks, and operations research, it has been
thoroughly studied over the years.
 It is convenient to record the lengths of shortest paths in an n × n
matrix D called the distance matrix: the element dij in the ith row and
the jth column of this matrix indicates the length of the shortest path
from the ith vertex to the jth vertex.

Prepared By: Eyob S.


Continued…
13

 Use Floyd’s algorithm to determine the distance matrix for the


following digraph.

Prepared By: Eyob S.


Solution
14

Prepared By: Eyob S.


15

Thank You!

Prepared By: Eyob S.

You might also like