0% found this document useful (0 votes)
27 views

Module 4

Uploaded by

cbgopinath
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Module 4

Uploaded by

cbgopinath
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Design and Analysis of Algorithms

Design and Analysis of Algorithms (15CS43)

MODULE 4- Dynamic Programming

Prepared by

ARPITHA C N
Asst. professor,
Dept. of CS&E,
A.I.T., Chikamagaluru

4.1 General method with Examples

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.

4.2 Multistage graphs

A multistage graph G=<V,E> is a directed graph in which the vertices are


partitioned into k≥ 2 disjoint sets Vi, 1 ≤i ≤ k . in addition, if <u,v> is an edge in E, then u ∈
Vi and v∈Vi+1 for some i, 1 ≤i<k . The sets V1 and Vk. the vertex s is the source, and t the
sink. Let c(I,j) be the cost of edge <I,j>. the cost of the path from s to t is the sum of the
costs of the edges on the path. The multistage graph problem is to find a minimum- cost
path from s to t. each set V i defines a stage in the graph. Because of the constraints on E,
every path from s to t starts in stage 1, goes to stage 2, then to stage 3, then to stage 4

Department of CS&E,A.I.T., Chikamagaluru 1


Design and Analysis of Algorithms

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.

Fig 4.1 directed weighted graph

Fig 4.2 five stage graph


Algorithm: multistage graph pseudo code corresponding to the forward
approach
Algorithm FGRAPH(G,k,n,p)
// Input: a k stage graph G=(V,E) with n vetices indexed inorder of stages. E is a
set of edges and c(I,j) is the cost of(I,j) . p[1..k] is a minimum cost path.
{
cost[n]:=0.0;
for j:=n-1 to 1 step-1 do
{
//compute cost[j]
Let r be a vertex such that <j,r> is an edge of G and c[j,r]+cost[r] is minimum;
cost[j]:=c[j,r]+cost[r];
d[j]:=r;
}
// find minimum cost path
p[1]:=1;

Department of CS&E,A.I.T., Chikamagaluru 2


Design and Analysis of Algorithms

p[k]:=n;
for j:=2 to k-1 do p[j]:=d[p[j-1]];
}

Algorithm: multistage graph pseudo code corresponding to the backward


approach
Algorithm BGraph(G,k,n,p)
{
//same function as FgRAPH
bcost[1:=0.0;
for j:=2 to n do
{
// compute bcost[j]
let r such that <r,j> is an edge of bcost[j]:=bcost[r]+c[r,j];
D[j];=r;
}
//find a minimum cost path
p[1]:=1;p[k]:=n;
for j:=k-1 to 2 do p[j]:=d[p[j+1]];
}
Algorithm: multistage graph pseudo code corresponding to the backward
approach

4.3 Transitive Closure

4.3.1 Warshall’s Algorithm

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.

Department of CS&E,A.I.T., Chikamagaluru 3


Design and Analysis of Algorithms

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

Department of CS&E,A.I.T., Chikamagaluru 4


Design and Analysis of Algorithms

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

FIGURE 4.4 Rule for changing zeros in Warshall’s algorithm.


Two situations regarding this path are possible. In the first, the list of its intermediate
vertices does not contain the kth vertex. Then this path from vi to vj has intermediate
vertices numbered not higher than k − 1, and therefore r(k−1) ij is equal to 1 as well. The
second possibility is that path Eq 4.2 does contain the kth vertex vk among the
intermediate vertices. Without loss of generality, we may assume that vk occurs only
once in that list. (If it is not the case, we can create a new path from vi to vj with this
property by simply eliminating all the vertices between the first and last occurrences of
vk in it.) With this caveat, path (Eq 4.2) can be rewritten as follows:
vi, vertices numbered ≤ k − 1, vk, vertices numbered ≤ k − 1, vj .
The first part of this representation means that there exists a path from vi to vk with
each intermediate vertex numbered not higher than k − 1 (hence, r(k−1) ik = 1), and the
second part means that there exists a path from vk to vj with each intermediate vertex
numbered not higher than k − 1 (hence, r(k−1) kj= 1).
What we have just proved is that if r(k)ij = 1, then either r(k−1)ij= 1 or both r(k−1) ik= 1 and
r(k−1)kj= 1. It is easy to see that the converse of this assertion is also true. Thus, we have
the following formula for generating the elements of matrix R(k) from the elements of
matrix R(k−1): r(k) ij= r(k−1)ij OR (r(k−1)ik AND r(k−1)kj) Eq 4.3

Department of CS&E,A.I.T., Chikamagaluru 5


Design and Analysis of Algorithms

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

Department of CS&E,A.I.T., Chikamagaluru 6


Design and Analysis of Algorithms

//Input: The adjacency matrix A of a digraph with n vertices


//Output: The transitive closure of the digraph
R(0) ←A
for k←1 to n do
for i ←1 to n do
for j ←1 to n do
R(k)[i, j ]←R(k−1)[i, j ] or (R(k−1)[i, k] and R(k−1)[k, j])
return R(n)

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.

4.4 All Pairs Shortest Paths

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

Department of CS&E,A.I.T., Chikamagaluru 7


Design and Analysis of Algorithms

Robert W. Floyd. It is applicable to both undirected and directed weighted graphs


provided that they do not contain a cycle of a negative length. (The distance between
any two vertices in such a cycle can be made arbitrarily small by repeating the cycle
enough times.) The algorithm can be enhanced to find not only the lengths of the
shortest paths for all vertex pairs but also the shortest paths themselves.
Floyd’s algorithm computes the distance matrix of a weighted graph with n vertices
through a series of n × n matrices:
D(0), . . . , D(k−1), D(k), . . . , D(n). (4.4)
Each of these matrices contains the lengths of shortest paths with certain
constraints on the paths considered for the matrix in question. Specifically, the element
d(k) ij in the ith row and the jth column of matrix D(k) (i, j = 1, 2, . . . , n, k = 0, 1, . . . , n) is
equal to the length of the shortest path among all paths from the ith vertex to the jth
vertex with each intermediate vertex, if any, numbered not higher than k. In particular,
the series starts with D(0), which does not allow any intermediate vertices in its paths;
hence,D(0) is simply the weight matrix of the graph. The last matrix in the series, D(n),
contains the lengths of the shortest paths among all paths that can use all n vertices as
intermediate and hence is nothing other than the distance matrix being sought.
As in Warshall’s algorithm, we can compute all the elements of each matrix D(k)
from its immediate predecessor D(k−1) in series . Let d(k) ij be the element in the ith
row and the jth column of matrix D(k). This means that d(k) ij is equal to the length of
the shortest path among all paths from the ith vertex vi to the jth vertex vj with their
intermediate vertices numbered not higher than k: vi, a list of intermediate vertices each
numbered not higher than k, vj . (4.5)
we can partition all such paths into two disjoint subsets: those that do not use the kth
vertex vk as intermediate and those that do. Since the paths of the first subset have their
intermediate vertices numbered not higher than k − 1, the shortest of them is, by
definition of our matrices, of length d(k−1) ij .
What is the length of the shortest path in the second subset? If the graph does not
contain a cycle of a negative length, we can limit our attention only to the paths in the
second subset that use vertex vk as their intermediate vertex exactly once (because
visiting vk more than once can only increase the path’s length). All such paths have the
following form: vi, vertices numbered ≤ k − 1, vk, vertices numbered ≤ k − 1, vj .

Department of CS&E,A.I.T., Chikamagaluru 8


Design and Analysis of Algorithms

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.7 Underlying idea of Floyd’s algorithm.


d(k)ij= min{d(k−1)ij , d(k−1) ik+ d(k−1)kj} for k ≥ 1, d(0)ij = wij . (4.6)
To put it another way, the element in row i and column j of the current distance matrix
D(k−1) is replaced by the sum of the elements in the same row i and the column k and in
the same column j and the row k if and only if the latter sum is smaller than its current
value. The application of Floyd’s algorithm to the graph in Figure 4.4 is illustrated in
Figure 4.8. Here is pseudo code of Floyd’s algorithm. It takes advantage of the fact that
the next matrix in sequence (4.6) can be written over its predecessor.
ALGORITHM Floyd(W[1..n, 1..n])
//Implements Floyd’s algorithm for the all-pairs shortest-paths problem
//Input: The weight matrix W of a graph with no negative-length cycle
//Output: The distance matrix of the shortest paths’ lengths
D ←W //is not necessary if W can be overwritten
for k←1 to n do
for i ←1 to n do
for j ←1 to n do

Department of CS&E,A.I.T., Chikamagaluru 9


Design and Analysis of Algorithms

D[i, j ]←min{D[i, j ], D[i, k]+ D[k, j]}


return D
Obviously, the time efficiency of Floyd’s algorithm is cubic—as is the time efficiency of
Warshall’s algorithm. In the next chapter, we examine Dijkstra’s algorithm—another
method for finding shortest paths.

FIGURE 4.8 Application of Floyd’s algorithm to the digraph shown. Updated elements
are shown in bold.

4.5 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

Department of CS&E,A.I.T., Chikamagaluru 10


Design and Analysis of Algorithms

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

Department of CS&E,A.I.T., Chikamagaluru 11


Design and Analysis of Algorithms

key ak, the left subtree T k−1


i contains keys ai, . . . , ak−1 optimally arranged, and the
right subtree Tjk+1

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 .

Department of CS&E,A.I.T., Chikamagaluru 12


Design and Analysis of Algorithms

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.

Department of CS&E,A.I.T., Chikamagaluru 13


Design and Analysis of Algorithms

EXAMPLE Let us illustrate the algorithm by applying it to the four-key set


Key A B C D
Probabilit 0.1 0.2 0.4 0.3
y

The initial tables look like this:

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.

Department of CS&E,A.I.T., Chikamagaluru 14


Design and Analysis of Algorithms

FIGURE 4.12 Optimal binary search tree for the example.


Here is pseudocode of the dynamic programming algorithm.
ALGORITHM OptimalBST(P [1..n])
//Finds an optimal binary search tree by dynamic programming
//Input: An array P[1..n] of search probabilities for a sorted list of n keys
//Output: Average number of comparisons in successful searches in the
// optimal BST and table R of subtrees’ roots in the optimal BST
for i ←1 to n do
C[i, i − 1]←0
C[i, i]←P[i]
R[i, i]←i
C[n + 1, n]←0
for d ←1 to n − 1 do //diagonal count
for i ←1 to n − d do
j ←i + d
minval←∞
for k←i to j do
if C[i, k − 1]+ C[k + 1, j]< minval
minval←C[i, k − 1]+ C[k + 1, j]; kmin←k
R[i, j ]←kmin
sum←P[i]; for s ←i + 1 to j do sum←sum + P[s]
C[i, j ]←minval + sum
return C[1, n], R
The algorithm’s space efficiency is clearly quadratic; the time efficiency of this version
of the algorithm is cubic (why?).Amore careful analysis shows that entries in the root
table are always non decreasing along each row and column. This limits values for R(i, j)
to the range R(i, j − 1), . . . , R(i + 1, j) and makes it possible to reduce the running time of
the algorithm to θ (n2).

Department of CS&E,A.I.T., Chikamagaluru 15


Design and Analysis of Algorithms

4.6 Knapsack problem

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:

It is convenient to define the initial conditions as follows:

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

Department of CS&E,A.I.T., Chikamagaluru 16


Design and Analysis of Algorithms

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

EXAMPLE 1 Let us consider the instance given by the following data:


item weight value
1 2 $12
2 1 $10 capacity W = 5.
3 3 $20
4 2 $15
The dynamic programming table, filled by applying formulas is shown in Figure 4.14.
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(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}.

Figure: 4.14 Example of solving an instance of the knapsack problem by the dynamic

Department of CS&E,A.I.T., Chikamagaluru 17


Design and Analysis of Algorithms

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.

Department of CS&E,A.I.T., Chikamagaluru 18


Design and Analysis of Algorithms

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.

4.7 Bellman-Ford Algorithm

Single source shortest path when some or all of the edges of the directed graph G may have negative
weights.

Fig 4.16: Directed graph with negative edge length.

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

Fig 4.17: Graph with negative edge cycle

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.

Department of CS&E,A.I.T., Chikamagaluru 19


Design and Analysis of Algorithms

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.

These observations result in the following recurrence for dist:

dist k[u]=min{ dist k-1[u], min i{dist k-1[i]+cost[i,u]}

This recurrence can be used to compute dist k from dist k-1,for k=2,3,…. n-1.

Example 1:

Fig 4.18: Shortest paths with negative edge lengths

Algorithm bellman ford(v,cost,dist,n)

//single source/ all destination shortest paths with negative edhe costs

for i:=1 to n do

Department of CS&E,A.I.T., Chikamagaluru 20


Design and Analysis of Algorithms

dist[i]:=cost[v,i];

for k:=2 to n-1 do

for each u such that u≠v and u has at least one incoming edge do

for each <i,u> in the graph 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.

4.8 Travelling Sales Person problem

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.

The travelling salesperson problem finds application in a variety of situations.


Suppose we have to route a postal van to pick up mail from mail boxes located in 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 site I to site j . The route taken by
the postal van is tour, and to calculate the minimum tour.

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

g(1,V-{1})=2min {c1 k + g ( k , V − {1 , K } ) } eq1


≤ K ≤n

By generalizing we obtain (for i∉S)

Department of CS&E,A.I.T., Chikamagaluru 21


Design and Analysis of Algorithms

g(i, S)=min {cij + g ( j , S−{ j}) } eq2


j ∈S

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)

fig 4.19: Directed graph with edge length matrix c

Thus,

g(2,∅)=c21=5 , g(3, ∅)=c31=6 and g(4, ∅)=c41=8

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

Next we compute g(I,S) with |S|=2,i≠S and i∉S

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

Finally from eq 1 we obtain

g(1,{2,3,4})=min9c12+g(2,{3,4}),c13+g(3,{2,4}),c14+g(4,{2,3})}

=min{35,40,43}

Department of CS&E,A.I.T., Chikamagaluru 22


Design and Analysis of Algorithms

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

4.9 Reliability design

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.

Fig 4.20: n devices Di 1<=i<n, connected in series

Fig 4.21: Multiple devices connected in parallel in 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

Department of CS&E,A.I.T., Chikamagaluru 23


Design and Analysis of Algorithms

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

Our problem is to use device duplication to maximize reliability. This


maximization is to be carried out under a cost constraint. Let c i be the cost of each unit
of device I and let c be the maximum allowable cost of the system being designed. we
wish to solve the following maximization problem:

∏≤ n ∅ i(mi)
maximize 1<i

subject to 1∑
≤i ≤n
cimi ≤ c

mi>=1 and integer , 1<=i<=n

Department of CS&E,A.I.T., Chikamagaluru 24

You might also like