Module 4 - Dynamic Programming
Module 4 - Dynamic Programming
Multistage Graphs:
Q. Explain the multistage graph with an example. Write backward multistage graph algorithm
Q Define a multi stage graph. Give an example. Explain the technique for finding the minimum cost path
in a multi stage graph
A Multistage graph is a directed, weighted graph in which the nodes can be divided into a set of
stages such that all edges are from a stage to next stage only (In other words there is no edge between
vertices of same stage and from a vertex of current stage to previous stage). The vertices of a
multistage graph are divided into n number of disjoint subsets S = { S 1 , S2, S3 ……….. Sn }, where S1 is the
source and Sn is the sink ( destination ). The cardinality of S 1 and Sn are equal to 1. i.e., |S 1| = |Sn| = 1.
We are given a multistage graph, a source and a destination, we need to find shortest path from source
to destination. Following is an example graph.
Warshall's algorithm is used to determine the transitive closure of a directed graph or all paths in a directed graph
by using the adjacency matrix. For this, it generates a sequence of n matrices. Where, n is used to describe the
number of vertices.
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.
Example: An example of a digraph, its adjacency matrix, and its transitive closure is given below.
We can generate the transitive closure of a digraph with the help of depth first search 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.
Warshall’s algorithm constructs the transitive closure through a series of n × n boolean matrices:
As an example, the application of Warshall’s algorithm to the digraph is shown below. New 1’s are in bold.
Analysis
Its time efficiency is Θ(n3 ). We can make the algorithm to run faster by treating matrix rows as bit strings and
We can generate the distance matrix with an algorithm that is very similar to Warshall’s algorithm. It is called
Floyd’s algorithm. Floyd’s algorithm computes the distance matrix of a weighted graph with n vertices through
a series of n × n matrices:
Knapsack problem:
Q. Design an 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. 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.
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.
The algorithm for the knapsack problem can be stated as follows
Input: n – total items, W – capacity of the knapsack
wi– weight of the ith item, vi– value of the ith item,
Output: F(i, j) be the value of an optimal solution to this instance considering first i items with capacity j. F(n,W)
is the optimal solution
Method:
Problems on Knapsack