Contents
1, Introduction to Dynamic Programming
1.1, General method with Examples
1.2. Multistage Graphs
2. Transitive Closure:
2.1. Warshall’s Algorithm,
3. Alll Pairs Shortest Paths:
3.1. Floyd's Algorithm,
Optimal Binary Search Trees
Knapsack problem
Bellman-Ford Algorithm
Travelling Sales Person problem
Reliability design
ee1, Introduction to Dynamic Programming
Dynamic programming is a technique for solving problems with averlapping subproblems,
Typically, these subproblems arise from a recurrence relating a given problem's solution to
solutions of its smaller subproblems. Rather than solving overlapping subproblems again and
again, dynamic programming suggests solving each of the smaller subproblems only once
and recording the results in a table from which a solution to the original problem can then be
obtained. [From T1]
‘The Dynamic programming can also be used when the solution to a problem can be viewed
as the result of sequence of decisions, / From T2/, Here are some examples.
Example 1 [Knopsack] The solution to the knapsack problem
van be viewed as the result of a sequence of decisions, We have to
decide the values of 4,1 < i
Yocien pip. Hence. the sequence y1, ++y2n i8 a sequence for (5.1)
with greater value. Again the principle of optimality applies. aExample 5.7 [Shortest path] Let A; be the set of vertices adjacent to vertex
i. For each vertex & € Aj, let [, be a shortest path from & to j. Then, a
shortest i to j path is the shortest of the paths {i,1',|k ¢ Ai}. a
Example 5.8 [0/1 knapsack] Let g;(y) be the value of an optimal solution
to KNAP(j + 1.7,y). Clearly, go(m) is the value of an optimal solution to
KNAP(1,2,m). The possible decisions for 2; are 0 and 1 (D, = {0,1}).
From the principle of optimality it follows that
go(m) = max {gi(m), gi(m — wi) + pi} (5.2)
o
While the principle of optimality has been stated only with respect to
the initial state and decision, it can be applied equally well to intermediate
states and decisions. The next two examples show how this can be done.
Example 5.9 Cire path] Let k be an intermediate vertex on a shortest
ito j path i,i),42,....k,pi,p2,---.j. The paths #, i, skand ky pis... j
must, respectively. be shortest i to k and k to j paths. ou
Example 5.10 [0/1 knapsack] Let y1,y2.--..Jm be an optimal solution to
KNAP(1,n,m). Then, for each j, 1 0 and
gn(y) = —00 for y < 0. From g,(y), one can obtain gn—1(y) using (5.3) with
i=n-—1. Then, using gn_1(y), one can obtain gn—a(y). Repeating in this
way, one can determine gi(y) and finally go(m) using (5.3) with i = 0
1.2 Multistage Graphs
A multistage graph G = (V, E) is a directed graph in which the vertices are
partitioned into k > 2 disjoint sets Vj, 1 | 20 5 Je] Withintermediate vertices numbered
Be le 7 ot not higher than 3, ie., a, b, and c
S| PE STaT| _ tnote four new shortest paths from a to b
from a to d, from b to d, and from d to b).
abed
alo 03 4 Lengths of the shortest peths
ob} 2056 with intermediate vertices numbered
DH =O) > 7 9 7 | nothigher than 4, ie., a, b,c, and d
(note a new shortest path from c to a).
d| 6 169 0
4. Optimal Binary Search Trees
A binary search tree is one of the most important data structures in computer science. One of
its principal applications is to implement a dictionary, a set of elements with the operations of
searching, insertion, and deletion.
If probabilities of searching for elements of a set are known e.g., from accumulated data about
past searches it is
average number of compari
tural to pose a question about an optimal binary search tree for which the
ns in a search is the smallest possible.
‘Asan example, consider four keys A,B, C,andDto
be searched for with probabilities 0.1, 0.2, 0.4, and
0.3, respectively. The figure depicts two out of 14
possible binary search trees containing these keys.
The average number of comparisons in a successful
search in the first of these trees is 0.1 * 1+0.2* 2+0.4* 3+0.3* 4=2.9, and for the secondone it is 0.1 *2+02* 140.4 * 2+03* 3521. Neither of these two trees is, in fact,
optimal.
For our tiny example, we could find the optimal tree by generating all 14 binary search trees
with these keys. As a general algorithm, this exhaustive-search approach is unrealistic: the total
number of binary search trees with n keys is equal to the nth Catalan number,
cm =— (?") forn >0, (0)
n+i\n
which grows to infinity as fast as 4"/ n'
Soletai,.
the probabil
«ay be distinct keys ordered from the smallest to the largest and let pi, . .. . Pa be
of searching for them. Let C(i, j) be the smallest average number of
comparisons made in a successful search in a binary search tree T! made up of keys ai, ... jy
where i, j are some integer indices, I< i py-level of a, in T,, + > pst
tS a 1 =i
i
= min {CG.kK-D+CK+1, D+ DY py
isks)
‘Thus, we have the recurrence
i
CG. f= min {CUkK-D+CK+1L D+ Yop, forlsisj 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+ FG ~ 1, j wy.
Thus, the value of an optimal solution among all feasible subsets of the first I items is the
maximum of these two values.ra jy — | maXxtFG-1,/).y+FG-1. j—w)) if j—w,>0,
FOD=1 pG- 19 itj—w; <0.
It is convenient to define the initial conditions as follows:
F(0, j) = 0 for j > 0 and F(i, 0) = 0 for i> 0.
Our goal is to find F(n, W), the maximal value of a subset of the n given items that fit into the
knapsack of capacity W, and an optimal subset itself.
° nm i w
ofo ° ° °
i-a}o Fu-1j-w) Fun.
my i fo Fu
a goal
Table for solving the knapsack problem by dynamic programming.
Example-1: Let us consider the instance given by the following data:
value
1 si2
2 $10 capacity W
3 $20
4 2 Sis
‘The dynamic programming table, filled by applying formulas is given below
capacity j
i oO 1 2 3 4 5
0 o 0 0 0 0 O
1 o 0 BR 2 2
2 o wo 12 n 2
3 o wo 12 a2
wy = 2, y= 4 o Ww 1s 300037
‘Thus, the maximal value is F(4, 5) = $37.
We can find the composition of an optimal subset by backtracing 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(., 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(O, 2), item 1 is the final part of the
optimal solution {item 1, item 2, item 4).Anal
s
The time efficiency and space efficiency of this algorithm are both in @(nW). The time needed
to find the composition of an optimal solution is in O(n).
Memory Functions
‘The direct top-down approach to finding a solution to such a recurrence leads to an algorithm
that solves common subproblems more than once and hence is very inefficient,
‘The classic dynamic programming approach, on the other hand, works bottom up: it fills a able
with solutions to all smaller subproblems, but each of them is solved only once. An unsatisfying
aspect of this approach is that solutions to some of these smaller subproblems are often not
necessary for getting a solution to the problem given. Since this drawback is not present in the
top-down approach, it is natural to try to combine the strengths of the top-down and bottom-up
approaches
does so only once. Such a method exists; itis based on using memory funetions.
‘The goal is to get a method that solves only subproblems that are necessary and
‘This method solves a given problem in the top-down manner but, in addition, maintains a table
of the kind that would have been used by a bottom-up dynamic programming algorithm.
Initially, all the table's entries are initialized with a special “null” symbol to indicate that they
have not yet been calculated. Thereafter, whenever a new value needs to be calculated, the
method checks the corresponding entry in the table first: if this entry is not “null,” it is simply
retrieved from the table; otherwise, it is computed by the recursive call whose result is then
recorded in the table.
The following algorithm implements this idea for the knapsack problem. After initializing the
table, the recursive function needs to be called with i = n (the number of items) and j = W (the
knapsack capacity).
Algorithm MFKnapsack(, j )
//mplements the memory function method for the knapsack problem
/Mnput: A nonnegative integer i indicating the number of the first items being
considered and a nonnegative integer j indicating the knapsack capacity
//Output: The value of an optimal feasible subset of the first i items
Not
Uses as global variables input arrays Weightsf1..n}, V alues{1..n}, and
table F[0..n, 0..W ] whose entries are initialized with —1’s except for
row 0 and column 0 initialized with 0°s
if Fli, j)<0
if j < Weights|i]
value — MFKnapsack(i = 1, j)
else
value —max(MFKnapsack(i — 1, j),
Values[i] + MFKnapsack(i — 1, j — Weights{i]))
Fli, j) 1, edges has no
dist*~ fal
2. If the shortest path from v to u with at most k, & > 1, edges has
exactly & edges, then it is made up of a shortest path from v to some
vertex j followed by the edge (j,u). The path from v to j has k —1
edges, and its length is dist*'[j]. All vertices i such that the edge
{i,u) is in the graph are candidates for j. Since we are interested in a
shortest path, the i that minimizes dist*-'{i] + cost[i, u] is the correct
value for j.
These observations result in the following recurrence for dist:
dist*{u) = min {dist*'[u], min {dist*"[i] + costfi, u)}}
This recurrence can be used to compute dist* from dist*—!, for k = 2,3,...,
n-1.
Bellman-Ford algorithm to compute shortest path
Algorithm BellmanFord(», cost, dist, n)
// Single-source/all-destinations shortest.
7/ paths with negative edge costs
for i:— 1 to n do // Initialize dist.
dist{t] = cost[v, 1];
for k :=2 to n—1do
for each u such that u 4 v and u has
at least one incoming edge do
for cach (i, u) in the graph do
if dist|u] > dist(i] + cost[i,u) then
dist|u] := dist{i) + cost[i,u);Example 5.16
igure 5.10 gives a seven-vertex graph, together with the
arrays dist", k ‘These arrays mputed using the equation
just given, For instance, dist*[1] . for all & since 1 is the source node.
Also, dist'[2] = 6, dist![3] = 5, and dist‘[4] = 5, since there are edges from
1 to these nodes. The distance dist'{] is oo for the nodes 5,6, and 7 since
there are no edges to these from 1
dist®Q) = min {dist!(2], min; dist*{i] + cost[i,2}}
= min {6,0 + 6,5 — 2,5 +00, 00 + 00,00 + 00, 00 + co}
Here the terms 0 + 6,5 — 2,5 + 00, 00 + 00,00 + 09, and oo + 00 correspond
to a choice of i = 1,3,4,5,6, and 7, respectively. The rest of the entries are
computed in an analogous manner. o
| dist*(1..7|
kT 234567
T0655 e600
20335540
S)0 13 5 2.4 7)
40135045
50135043
60135043
(a) A directed graph (b) dise*
Figure 5.10 Shortest paths with negative edge lengths
7. Travelling Sales Person problem (T2:5.9),
We have seen how to apply dynamic programming to a subset selection prob-
Jem (0/1 knapsack). Now we turn our attention to a permutation problem.
Note that permutation problems usually are much harder to solve than sub-
set problems as there are n! different permutations of n objects whereas
there are only 2" different subsets of n objects (n! > 2"). Let G = (V,E)
be a directed graph with edge costs cj. The variable ci; is defined such that
¢ij > 0 for all é and j and ¢; = 00 if (i. 7) ¢ E. Let [V| = n and assume
n> 1. A tour of G is a directed simple cycle that includes every vertex in
V. The cost of a tour is the sum of the cost of the edges on the tour. The
traveling salesperson problem is to find a tour of minimum cost.
The traveling salesperson problem finds application in a variety of situ-
ations. Suppose we have to route a postal van to pick up mail from mail
boxes located at n different sites. An n +1 vertex graph can be used to
represent the situation. One vertex represents the post office from which the
postal van starts and to which it must return. Edge (i,j) is assigned a cost
equal to the distance from The route taken by the postal van
is a tour, and we are interested in finding a tour of minimum length.As a second example, suppose we wish to use a robot arm to tighten
the nuts on some piec: machinery on an assembly line. The arm will
start from its initial p« ion (which is over the first nut to be tightened),
successively move to each of the remaining nuts, and return to the initial
position. The path of the arm is clearly a tour on a graph in which vertices
represent the nuts. A minimum-cost tour will minimize the time needed for
the arm to complete its task (note that only the total arm movement time
is variable; the nut tightening time is independent of the tour).
In the following discussion we shall, without loss of generality, regard
a tour to be a simple path that starts and ends at vertex 1. Every tour
consists of an edge (1,k) for some k € V — {1} and a path from vertex k to
vertex 1. The path from vertex k to vertex 1 goes through each vertex in
V —{1,k} exactly once. It is easy to see that if the tour is optimal, then the
S
path from & to 1 must be a shortest k to 1 path going through all verti
in V ~ {1,k}. Hence, the principle of optimality holds. Let 9(i,$) be the
length of a shortest path starting at vertex i, going through all vertices in
S, and terminating at vertex 1. The function g(1,V — {1}) is the length of
an optimal salesperson tour. From the principal of optimality it follows that
9(1,V ~ {1}) = ymin {ere + 9(k,V ~ {1,k})} (5.20)
Generalizing (5.20), we obtain (for i ¢ S)
gi, S) = min{o +9(5.5 —{5})} (5.21)
Equation 5.20 can be solved for g(1,V — {1}) if we know g(k,V — {1,k})
for all choices of k. The g values can be obtained by using (5.21). Clearly,
91,6) = ca, 1 | Ds ee
1 >: | a
uJ} LI L__]
Figure 5.20 Multiple devices connected in parallel in each stage
If stage i contains mj copies of device D;, then the probability that all
‘m, have a malfunction is (1—rj)"'. Hence the reliability of stage « becomes
1-(1—rj)™. Thus, if rj = 99 and m; = 2, the stage reliability becomes
.9999. In any practical situation, the stage reliability is a little less than
1-(1—r,)™ because the switching circuits themselves are not fully reliable.
Also, failures of copies of the same device may not be fully independent (e.
if failure is due to design defect). Let us assume that the reliability of stage
i is given by a function 4;(m;), 1 1 and integer, 1 0, each
m, must be in the range 1 < mj < u;, where
w=[lera-Sevral
‘The upper bound u; follows from the observation that m, > 1. An optimal
solution 12;,71,...,1™Mp is the result of a sequence of decisions, one decision
for each m,. Let fi(«) represent the maximum value of Hh ejmj Sv and 1< mj; 1, this equation generalizes to
ia) = , max, {bi(mi) fi-1(@ ~ errns)} (5.19)
Clearly, fo(x) = 1 for all 2, 0 < x fo and a, < 2 holds
for this problem too. Hence, dominated tuples can be discarded from S'.Example 5.25 We are to design a three stage system with device types
D,, D2, and D3. The costs are $30, $15, and $20 respectively. The cost of
the system is to be no more than $105. The reliability of each device type is
9, .8 and .5 respectively. We assume that if stage i has m; devices of type i
in parallel, then $j(m;) = 1—(1—r;)”'. In terms of the notation used earlier,
ec, = 30, c2 = 15, ¢3 = 20, ¢ = 105, ry = 9, re = .8, 73 = .5,u) = 2,u2 = 3,
and us
We use S' to represent the set of all undominated tuples (f,«) that
may result. from the various decision sequences for m,my,...,™mj. Hence,
f(x) = fi(x). Beginning with S° = {(1,0)}, we can obtain each S' from S*~!
by trying out all possible values for m, and combining the resulting tuples
together. Using S' to represent, all tuples obtainable from S'- by choosing
j, we obtain $}] = {(.9, 30)} and $} = {(.9, 30), (.99,60)}. The set
(.72, 45), (.792, 75)}; S3= {(.864, 60)}. Note that the tuple (.9504, 90)
mes from (.99, 60) has been eliminated from $3 as this leaves only
$10. This is not enough to allow mg = 1. The set $2 4: 8928, 75)}. Com-
bining, we get S? = {(.72, 45), (.864, 60), (.8928, 75)} as the tuple (.792, 75) is
dominated by (.864, 60). ae set 53 fess 65), (432, 80), (.4464, 95)}, 93
= {(.54, 85), (.648, 100)}, and $3 = {(.63,105)}. Combining, we get S° =
{(.36, 65), (432, 80), (.54, 85), (.648, or
‘The best design has a reliability of .648 and a cost of 100. Tracing os
through the S'’s, we determine that m; = 1,m2 = 2, and mg = 2.
As in the case of the knapsack problem, a complete dynamic programming
algorithm for the reliability problem will use heuristics to reduce the size of
the S's. There is no need to retain any tuple (f,2) in S' with x value
greater that c — Dy