Module 4
Module 4
Prepared by
ARPITHA C N
Asst. professor,
Dept. of CS&E,
A.I.T., Chikamagaluru
Dynamic programming is an algorithm design method that can be used when the solution to
a problem can be viewed as the result of a sequence of decisions.
Examples:
1. Knapsack:
The solution to the knapsack problem can be viewed as the result of a
sequence of decisions. We have to decide the values of x i , 1<=i<=n. first we make
a decision on xi ,1<=i<=n. first make a decision on x 1, then on x2 , then on x3 and so
on. An optimal sequence of decisions maximizes the objective function ∑ pi x i. (it
also satisfies the constraints ∑ wi xi <=m and 1<=xi<=1.)
2. Shortest path:
One way to find the shortest path from vertex i to vertex j in a directed
graph G is to decide which vertex should be the second vertex, which is the third,
and so on, until vertex j is reached. An optimal sequence of decisions is one that
results in a path of least length
For some of the problems that may be viewed in the way, an optimal
sequence of decisions can be found by making the decisions one at a time and
never making an erroneous decision. This is true for all problems solvable by the
greedy method. For many other problems, it is not possible to make stepwise
decisions in such a manner that the sequence of decisions made is optimal.
and so on, and eventually terminates in stage k. fig 4.1 shows a five stage graph. A
minimum cost s to t path is indicated by the broken edges.
p[k]:=n;
for j:=2 to k-1 do p[j]:=d[p[j-1]];
}
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. We may also be interested in a matrix containing the
information about the existence of directed paths of arbitrary lengths between vertices
of a given graph. Such a matrix, called the transitive closure of the digraph, would allow
us to determine in constant time whether the jth vertex is reachable from the ith vertex
DEFINITION
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.
An example of a digraph, its adjacency matrix, and its transitive closure is given in
Figure 4.3. We can generate the transitive closure of a digraph with the help of depth
firstsearch or breadth-first search. Performing either traversal starting at the ith vertex
gives the information about the vertices reachable from it and hence the columns that
contain 1’s in the ith row of the transitive closure. Thus, doing such a traversal for every
vertex as a starting point yields the transitive closure in its entirety.
FIGURE 4.3 (a) Digraph. (b) Its adjacency matrix. (c) Its transitive closure.
Since this method traverses the same digraph several times, we should hope that a
better algorithm can be found. Indeed, such an algorithm exists. It is called Warshall’s
algorithm after Stephen Warshall, who discovered it. It is convenient to assume that the
digraph’s vertices and hence the rows and columns of the adjacency matrix are
numbered from 1 to n. Warshall’s algorithm constructs the transitive closure through a
series of n × n boolean matrices:
R(0). . . , R(k−1),R(k) ,. . . R(n) Eq 4.1.
Each of these matrices provides certain information about directed paths in the
digraph. Specifically, the element r(k) ij in the ith row and jth column of matrix R(k) (i, j =
1, 2, . . . , n, k = 0, 1, . . . , n) is equal to 1 if and only if there exists a directed path of a
positive length from the ith vertex to the jth vertex with each intermediate vertex, if any,
numbered not higher than k. Thus, the series starts with R(0), which does not allow any
intermediate vertices in its paths; hence, R(0) is nothing other than the adjacency matrix
of the digraph. (Recall that the adjacency matrix contains the information about one-
edge paths, i.e., paths with no intermediate vertices.) R(1) contains the information about
paths that can use the first vertex as intermediate; thus, with more freedom, so to speak,
it may contain more 1’s than R(0). In general, each subsequent matrix in series has one
more vertex to use as intermediate for its paths than its predecessor and hence may, but
does not have to, contain more 1’s. The last matrix in the series, R(n)reflects paths that
can use all n vertices of the digraph as intermediate and hence is nothing other than the
digraph’s transitive closure.
The central point of the algorithm is that we can compute all the elements of each
matrix R(k) from its immediate predecessor R(k−1) in series Eq 4.1. Let R(k) ij , the element
in the ith row and jth column of matrix R(k) , be equal to 1. This means that there exists a
path from the ith vertex vi to the jth vertex vj with each intermediate vertex numbered
not higher than k:
vi, a list of intermediate vertices each numbered not higher than k, vj . Eq 4.2
Formula in eq 4.3 is at the heart of Warshall’s algorithm. This formula implies the
following rule for generating elements of matrix R(k) from elements of matrix R(k−1) which
is particularly convenient for applying Warshall’s algorithm by hand: If an element rij is
1 in R(k−1), it remains 1 in R(k). If an element rij is 0 in R(k−1), it has to be changed to 1 in R(k)
if and only if the element in its row i and column k and the element in its column j and
row k are both 1’s in R(k−1). This rule is illustrated in Figure 4.4.
As an example, the application of Warshall’s algorithm to the digraph in Figure 4.3 is
shown in Figure 4.5.
FIGURE 4.5 Application of Warshall’s algorithm to the digraph shown. New 1’s are in
bold.
Here is pseudocode of Warshall’s algorithm.
ALGORITHM Warshall(A[1..n, 1..n])
//ImplementsWarshall’s algorithm for computing the transitive closure
Several observations need to be made about Warshall’s algorithm. First, its time
efficiency is only θ (n3). In fact, for sparse graphs represented by their adjacency lists, the
traversal-based algorithm
FIGURE 4.6(a) Digraph. (b) Its weight matrix. (c) Its distance matrix.
Floyd’s Algorithm
Given a weighted connected graph (undirected or directed), the all-pairs
shortestpaths problem asks to find the distances—i.e., the lengths of the shortest paths
—from each vertex to all other vertices. This is one of several variations of the problem
involving shortest paths in graphs. Because of its important applications to
communications, transportation networks, and operations research, it has been
thoroughly studied over the years. Among recent applications of the all-pairs shortest-
path problem is precomputing distances for motion planning in computer games. 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. For an
example, see Figure 4.4. We can generate the distance matrix with an algorithm that is
very similar to Warshall’s algorithm. It is called Floyd’s algorithm after its co-inventor
In other words, each of the paths is made up of a path from vi to vk with each
intermediate vertex numbered not higher than k − 1 and a path from vk to vj with each
intermediate vertex numbered not higher than k − 1. The situation is depicted
symbolically in Figure 4.7.
Since the length of the shortest path from vi to vk among the paths that use intermediate
vertices numbered not higher than k − 1 is equal to d(k−1) ik and the length of the shortest
path from vk to vj among the paths that use intermediate vertices numbered not higher
than k − 1is equal to d(k−1)kj , the length of the shortest path among the paths that use the
kth vertex is equal to d(k−1)ik + d(k−1)kj . Taking into account the lengths of the shortest
paths in both subsets leads to the following recurrence:
FIGURE 4.8 Application of Floyd’s algorithm to the digraph shown. Updated elements
are shown in bold.
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
natural to pose a question about an optimal binary search tree for which the average
number of comparisons in a search is the smallest possible. For simplicity, we limit our
discussion to minimizing the average number of comparisons in a successful search. The
method can be extended to include unsuccessful searches as well.
FIGURE 4.9 Two out of 14 possible binary search trees with keys A, B, C, and D.
As an example, consider four keys A, B, C, and D to be searched for with probabilities 0.1,
0.2, 0.4, and 0.3, respectively. Figure 4.9 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 second one it
is 0.1 . 2 + 0.2 . 1+ 0.4 . 2 + 0.3 . 3= 2.1.
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,
which grows to infinity as fast as 4n/n1.5 (see Problem 7 in this section’s exercises). So
let a1, . . . , an be distinct keys ordered from the smallest to the largest and let p1, . . . , pn
be the probabilities 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 Tji made up of keys
ai, . . . , aj , where i, j are some integer indices, 1≤ i ≤ j ≤ n. Following the classic dynamic
programming approach, we will find values of C(i, j) for all smaller instances of the
problem, although we are interested just in C(1, n). To derive a recurrence underlying a
dynamic programming algorithm, we will consider all possible ways to choose a root ak
among the keys ai, . . . , aj . For such a binary search tree (Figure 8.8), the root contains
FIGURE 4.10 Binary search tree (BST) with root ak and two optimal binary search
subtrees
k−1
T i and Tjk+1 contains keys ak+1, . . . , aj also optimally arranged. If we count tree
levels starting with 1 to make the comparison numbers equal the keys’ levels, the
following recurrence relation is obtained:
4.7
We assume in formula that C(i, i − 1) = 0 for 1≤ i ≤ n + 1, which can be interpreted as the
number of comparisons in the empty tree. Note that this formula implies that C(i, i) = pi
for 1≤ i ≤ n, as it should be for a one-node binary search tree containing ai .
FIGURE 4.11 Table of the dynamic programming algorithm for constructing an optimal
binary search tree.
The two-dimensional table in Figure 4.11 shows the values needed for
computing C(i, j) by formula (4.7): they are in row i and the columns to the left of
column j and in column j and the rows below row i. The arrows point to the pairs of
entries whose sums are computed in order to find the smallest one to be recorded as the
value of C(i, j). This suggests filling the table along its diagonals, starting with all zeros
on the main diagonal and given probabilities pi, 1≤ i ≤ n, right above it and moving
toward the upper right corner. The algorithm we just sketched computes C(1, n)—the
average number of comparisons for successful searches in the optimal binary tree. If we
also want to get the optimal tree itself, we need to maintain another two-dimensional
table to record the value of k for which the minimum in (4.7) is achieved. The table has
the same shape as the table in Figure 4.11 and is filled in the same manner, starting with
entries R(i, i) = i for 1≤ i ≤ n. When the table is filled, its entries indicate indices of the
roots of the optimal subtrees, which makes it possible to reconstruct an optimal tree for
the entire set given.
Thus, out of two possible binary trees containing the first two keys, A and B, the root of
the optimal tree has index 2 (i.e., it contains B), and the average number of comparisons
in a successful search in this tree is 0.4.
final tables:
Thus, the average number of key comparisons in the optimal tree is equal to 1.7. Since
R(1, 4) = 3, the root of the optimal tree contains the third key, i.e., C. Its left subtree is
made up of keys A and B, and its right subtree contains just key D (why?). To find the
specific structure of these subtrees, we find first their roots by consulting the root table
again as follows. Since R(1, 2) = 2, the root of the optimal tree containing A and B is B,
with A being its left child (and the root of the one node tree: R(1, 1) = 1). Since R(4, 4) =
4, the root of this one-node optimal tree is its only key D. Figure 4.12 presents the
optimal tree in its entirety.
We start this section with designing 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. (This problem was introduced in Section 3.4, where we discussed solving it
by exhaustive search.) We assume here that all the weights and the knapsack capacity
are positive integers; the item values do not have to be integers. To design a dynamic
programming algorithm , we need to derive a recurrence relation that expresses a
solution to an instance of the knapsack problem in terms 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.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 ith item and those that do. Note the following:
1. Among the subsets that do not include the ith item, the value of an optimal subset is,
by definition, F(i − 1, j).
2. 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. Of course, if the ith item does not fit into the knapsack,
the value of an optimal subset selected from the first i items is the same as the value of
an optimal subset selected from the first i − 1 items. These observations lead to the
following recurrence:
that fit into the knapsack of capacity W, and an optimal subset itself. Figure 4.13
illustrates the values involved in equations (8.6) and (8.7). For i, j > 0, to compute the
entry in the ith row and the jth column, F(i, j), we compute the maximum of the entry in
the previous row and the same column and the sum of vi and the entry in the previous
row and wi columns to the left. The table can be filled either row by row or column by
column.
FIGURE 4.13 Table for solving the knapsack problem by dynamic programming
Figure: 4.14 Example of solving an instance of the knapsack problem by the dynamic
programming algorithm.
Memory Functions
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(i, j )
//Implements the memory function method for the knapsack problem
//Input: 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
//Note: Uses as global variables input arrays Weights[1..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 F[i, 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]))
F[i, j ]←value
return F[i, j ]
EXAMPLE 2 Let us apply the memory function method to the instance considered
in Example 1. The table in Figure 4.15 gives the results. Only 11 out of 20 nontrivial
values (i.e., not those in row 0 or in column 0) have been computed.
FIGURE 4.15 Example of solving an instance of the knapsack problem by the memory
function algorithm.
Just one nontrivial entry, V (1, 2), is retrieved rather than being recomputed. For
larger instances, the proportion of such entries can be significantly larger.
Single source shortest path when some or all of the edges of the directed graph G may have negative
weights.
When negative edge lengths are permitted, It is requires that the graph have no cycles of
negative length. This is necessary to ensure that shortest paths consist of a finite
number of edges. For example consider Fig 4.17
The length of the shortest path from vertex 1 to 3 is -∞. the length of the path 1,2,1,2,1,2,
….1,2,3 can be made arbitrarily small.
let dist l[u] be the length of a shortest path from the source vertex to v to vertex u
under the constraint that the shortest path contains at most l edges. then,
dist1[u]=cost[u,v], 1<=u<= n. if there are no negative cycles the search for shortest path
can be limited to n-1 edges. Hence distn-1[u] is the length of an unrestricted shortest path
from v to u.
The goal is to compute dist n-1[u] for all u. this can be done using dynamic programming
method. With the following observations:
1. If the shortest path from v to u with at most k, k >1 , edges has no more than k-1
edges, then dist k[u]=distk-1[u].
2. If the shortest path from v to u with at most k, k >1, edges has exactly k 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 k-1[j]. all
vertices i such that the edge<i, u> is in the graph are candidates for j, since
shortest path is to be calculated, the I that minimizes dist k-1
[i] + cost[i,u] is the
correct value for j.
This recurrence can be used to compute dist k from dist k-1,for k=2,3,…. n-1.
Example 1:
//single source/ all destination shortest paths with negative edhe costs
for i:=1 to n do
dist[i]:=cost[v,i];
for each u such that u≠v and u has at least one incoming edge do
if dist[u]>dist[i]+cost[I,u] then
dist[u]:=dist[i]+cost[i,u];
Algorithm for bellman ford to compute shortest path with general weights.
Let G= (V,E) be a directed graph with edge costs c ij. The variable cij is defined
such that cij>0 for all I and j and c ij=∞ if <i,j>∉ 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 travelling salesperson problem is to find a tour of
minimum cost.
let g(I,S) be the length of a shortest path atarting at vertex I, going through all vertices in
the S, and terminating at vertex 1. the function g(1,V-{1}) I the length of an optimal
salesperson tour. from the principal of optimality it follows that an optimal salesperson
tour. from the principal of optimality it follows that
Example 1
Consider the directed graph of fig 4.19(a). The edge length are given in the matrix c of
fig 4.19 (b)
Thus,
using eq 2 we obtain
g(2,{3})=c23+g(3, ∅)=15
g(2,{4})=18
g(3,{2})=18
g(3,{4})=20
g(4,{2})=13
g(4,{3})=15
g(2,{3,4})=min(c23+g(3,{4}),c24+g(4,{3})}=25
g(3,{2,4})=min(c32+g(2,{4}),c34+g(4,{2})}=25
g(4,{2,3})=min(c42+g(2,{3}),c43+g(3,{2})}=23
g(1,{2,3,4})=min9c12+g(2,{3,4}),c13+g(3,{2,4}),c14+g(4,{2,3})}
=min{35,40,43}
=35
The optimal tour of the graph has length 35. A tour of this length can be
constructed if we retain with each g(I,S) be this value. Then J(1,{2,3,4})=2. Thus the tour
starts from 1 and goes to 2. the remaining tour can be obtained from g(2,{3,4{). so J(2,
{3,4})=4. thus the next edge is<2,4>. the remaining tour is for g(4,{3}). do J(4,{3})=3. the
optimal tour is 1 ,2 ,4 ,3 ,1.
This section discuss about how to use dynamic programming to solve a problem
with a multiplicative optimization function. the problem is to design a system that is
component of several devices connected in series fig 4.20. Let r i be the reliability of
device Di. Then, the reliability of the entire system is πr i. Even if the individual devices
are very reliable, the reliability of the system may not be very good.
For example if n=10 and ri=0.99, 1<i<10, then πri=0.904. Hence it is desirable to
duplicates the devices. Multiple copies of the same device type are connected in parallel
fig 4.21 through the use of switching circuits. The switching circuits determine which
devices in any given group are functioning properly. They then make use of one such
device at each stage.
If the stage i contains m i copies of devices Di, then the probability that all m i have a
malfunctions is (1-ri)mi. hence the reliability of stage i becomes 1-(1-r i)mi. thus if ri =0.99
and mi=2, the stage reliability becomes 0.9999. in many practical situations, the stage
reliability is a less than 1-(1-r i)mi because the switching circuits themselves are not fully
reliable. also, failures of copies of the same device may not be fully independent. let us
assume that the reliability of stage I is given by a function ∅ i(mi), 1<=n. the reliability of
the system of stages is ∏ ∅ i(mi).
1 ≤i ≤n
∏≤ n ∅ i(mi)
maximize 1<i
subject to 1∑
≤i ≤n
cimi ≤ c