KEMBAR78
UNIT 4 Chapter 1 DYNAMIC PROGRAMMING.pptx
UNIT 4 CHAPTER 1
DYNAMIC PROGRAMMING
WARSHALLS
ALGORITHM
 Warshall’s algorithm for computing the transitive closure of a directed graph and
Floyd’s algorithm for the all-pairs shortest-paths problem. These algorithms are based
on essentially the same idea: exploit a relationship between a problem and its simpler
rather than smaller version
 Recall that the adjacency matrixA = {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.
 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).
 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.
 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 .
 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:
 EXAMPLE 2 :
R(4)= 0 1 1 1
0 0 1 1
0 0 0 1
0 0 0 0
FLOYDS ALGORITHM
ALL PAIR SHORTEST PATH PROBLEM
 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.
 Floyd’s algorithm computes the distance matrix of a weighted graph
with n vertices through a series of n × n matrices:
 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.
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 Knapsack Problem and
Memory Functions
 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.
 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 of solutions to its smaller sub instances.
 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.
 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.
 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:
 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.
 Figure 8.4 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.
 Thus, the maximal value is F(4, 5) = $37.
 We can find the composition of an optimal subset by backtracking 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}.
UNIT 4 Chapter 1 DYNAMIC PROGRAMMING.pptx
UNIT 4 Chapter 1 DYNAMIC PROGRAMMING.pptx
UNIT 4 Chapter 1 DYNAMIC PROGRAMMING.pptx
UNIT 4 Chapter 1 DYNAMIC PROGRAMMING.pptx
UNIT 4 Chapter 1 DYNAMIC PROGRAMMING.pptx

UNIT 4 Chapter 1 DYNAMIC PROGRAMMING.pptx

  • 1.
    UNIT 4 CHAPTER1 DYNAMIC PROGRAMMING
  • 2.
  • 3.
     Warshall’s algorithmfor computing the transitive closure of a directed graph and Floyd’s algorithm for the all-pairs shortest-paths problem. These algorithms are based on essentially the same idea: exploit a relationship between a problem and its simpler rather than smaller version  Recall that the adjacency matrixA = {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.
  • 4.
     DEFINITION  Thetransitive 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.
  • 5.
     It isconvenient 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).  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.
  • 6.
     The centralpoint 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 .  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:
  • 12.
     EXAMPLE 2: R(4)= 0 1 1 1 0 0 1 1 0 0 0 1 0 0 0 0
  • 13.
    FLOYDS ALGORITHM ALL PAIRSHORTEST PATH PROBLEM
  • 14.
     Given aweighted 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.
  • 15.
     It isconvenient 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.  Floyd’s algorithm computes the distance matrix of a weighted graph with n vertices through a series of n × n matrices:
  • 16.
     Each ofthese 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.
  • 17.
    The element inrow 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.
  • 22.
    The Knapsack Problemand Memory Functions
  • 23.
     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.  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 of solutions to its smaller sub instances.  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.  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.
  • 25.
     Thus, thevalue 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:
  • 26.
     Our goalis 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.  Figure 8.4 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.
  • 29.
     Thus, themaximal value is F(4, 5) = $37.  We can find the composition of an optimal subset by backtracking 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}.