SlideShare a Scribd company logo
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:
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
 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.
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
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.
UNIT 4 Chapter 1 DYNAMIC PROGRAMMING.pptx
 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.
UNIT 4 Chapter 1 DYNAMIC PROGRAMMING.pptx
UNIT 4 Chapter 1 DYNAMIC PROGRAMMING.pptx
 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

More Related Content

PPT
Dynamic Programming for 4th sem cse students
PPTX
Dynamic Programming in design and analysis .pptx
PPTX
LU_30_Dynamic_Programming_Warshal_Floyd_1712140744434.pptx
PPTX
Dynamic Programming.pptx
PPT
4900514.ppt
PDF
Algorithm chapter 8
Dynamic Programming for 4th sem cse students
Dynamic Programming in design and analysis .pptx
LU_30_Dynamic_Programming_Warshal_Floyd_1712140744434.pptx
Dynamic Programming.pptx
4900514.ppt
Algorithm chapter 8

Similar to UNIT 4 Chapter 1 DYNAMIC PROGRAMMING.pptx (20)

PDF
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
PPTX
DAA-Floyd Warshall Algorithm.pptx
PDF
module4_dynamic programming_2022.pdf
PPTX
Design and Analysis of Algorithm-Lecture.pptx
PPTX
Algorithm Design Techiques, divide and conquer
PDF
PDF
Floyd Warshal's Algorithm
PPTX
unit3_Dynamic_Progrghaiajawzjabagamming1.pptx
PPTX
unit-4-dynamic programming
PDF
My presentation all shortestpath
PPT
Warshalls Floyds Algorithm in Data Structure.ppt
PPTX
Warshalls and floyds algorithms
PPT
dynamic-programming unit 3 power point presentation
PPT
Elak3 need of greedy for design and analysis of algorithms.ppt
PPTX
Chapter 5.pptx
PPT
d0a2de03-27d3-4ca2-9ac6-d83440657a6c.ppt
PPTX
Dynamic Program Problems
PDF
21 All Pairs Shortest Path
PPT
Learn about dynamic programming and how to design algorith
PPTX
Data Structures and algorithms using dynamicProgramming
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
DAA-Floyd Warshall Algorithm.pptx
module4_dynamic programming_2022.pdf
Design and Analysis of Algorithm-Lecture.pptx
Algorithm Design Techiques, divide and conquer
Floyd Warshal's Algorithm
unit3_Dynamic_Progrghaiajawzjabagamming1.pptx
unit-4-dynamic programming
My presentation all shortestpath
Warshalls Floyds Algorithm in Data Structure.ppt
Warshalls and floyds algorithms
dynamic-programming unit 3 power point presentation
Elak3 need of greedy for design and analysis of algorithms.ppt
Chapter 5.pptx
d0a2de03-27d3-4ca2-9ac6-d83440657a6c.ppt
Dynamic Program Problems
21 All Pairs Shortest Path
Learn about dynamic programming and how to design algorith
Data Structures and algorithms using dynamicProgramming
Ad

Recently uploaded (20)

PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Geodesy 1.pptx...............................................
PPT
Project quality management in manufacturing
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
OOP with Java - Java Introduction (Basics)
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
web development for engineering and engineering
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Geodesy 1.pptx...............................................
Project quality management in manufacturing
bas. eng. economics group 4 presentation 1.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
UNIT 4 Total Quality Management .pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
OOP with Java - Java Introduction (Basics)
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Operating System & Kernel Study Guide-1 - converted.pdf
Model Code of Practice - Construction Work - 21102022 .pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Fundamentals of safety and accident prevention -final (1).pptx
Internet of Things (IOT) - A guide to understanding
web development for engineering and engineering
Embodied AI: Ushering in the Next Era of Intelligent Systems
Ad

UNIT 4 Chapter 1 DYNAMIC PROGRAMMING.pptx

  • 1. UNIT 4 CHAPTER 1 DYNAMIC PROGRAMMING
  • 3.  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.
  • 4.  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.
  • 5.  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.
  • 6.  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:
  • 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 PAIR SHORTEST PATH PROBLEM
  • 14.  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.
  • 15.  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:
  • 16.  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.
  • 17. 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.
  • 22. The Knapsack Problem and 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, 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:
  • 26.  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.
  • 29.  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}.