Dynamic Programming
Dynamic Programming
Module 4
DYNAMIC PROGRAMMING
Dynamic programming is a technique for solving problems with overlapping subproblems.
These subproblems arise from a recurrence relating a given problem’s solution to solutions of its
smaller subproblems.
Rather than solving overlapping subproblems again and again, solve each of the smaller
subproblems only once and record the results in a table from which a solution to the original
problem can then be obtained.
Multistage Graph: A multistage graph G=(V, E) is a directed graph in which the vertices are partitioned
into k disjoint sets, Where k ≥ 2. (V1, V2, ….Vi,…Vk) . If (u,v) is an edge in E, then u ϵ Vi and v ϵ Vi+1.
The Multistage-graph problem is to find a minimum-cost path from ‘s’ (source-V1) to ‘t’ (sink-Vk).
Example: The following figure shows a five stage graph. A minimum-cost s to t path is indicated by the
broken edges.
Formula:
2. Backward approach
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. Here all the weights and the knapsack capacity
are positive integers; the item values do not have to be integers.
Solution to knapsack problem using dynamic approach
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.
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).
It can be expressed by using the following recurrence relation
The initial conditions are defined as follows: F(0, j) = 0 for j ≥ 0 and F(i, 0) = 0 for i ≥ 0.
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. The following figure illustrates the values involved in equations the
above equations. 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 time efficiency and space efficiency of this algorithm are both in Θ(nW).
Problem 1: Find the optimal solution for the following instance of knapsack problem using dynamic
programming. Maximum capacity of knapsack W=5. (06 Marks, Dec 2015)
Solution:
Warshall’s algorithm
Used for computing the transitive closure of a directed graph. The transitive closure of a directed graph with
‘n’ vertices can be defined as the n × n boolean matrix T = {t ij}, 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, t ij is 0.
Applications of transitive closure
When a value in a spreadsheet cell is changed, the spreadsheet software must know all the other cells
affected by the change. If the spreadsheet is modeled by a digraph whose vertices represent the
spreadsheet cells and edges indicate cell dependencies, the transitive closure will provide such
information.
In software engineering, transitive closure can be used for investigating data flow and control flow
dependencies as well as for inheritance testing of object-oriented software.
In electronic engineering, it is used for redundancy identification and test generation for digital
circuits.
Problem:
According to dijkstra’s algorithm, the dist[2] = 7 and dist[3] = 5. But actually dist[3]= 1-2-3 = 2
Bellman ford does this. But it does not allow cycles of negative length
Here the shortest path from 1 to 3 is -∞. The path is 1, 2, 1, 2, ………1, 2, 3. So when there are no cycles of
negative length, there is a shortest path between any two vertices of an n vertex graph that has at-most n-1
edges on it.
Formula:
Reliability design
The reliability design problem is to design a system that is composed of several devices connected in series.
The reliability of the designed system should be as high as possible.
Figure:
If one system is connected per stage, the total reliability of the system may not be very good. For
example: ri be the reliability of device Di. = 0.99.
Number of system n = 10.
The reliability of the entire system is Π ri. = 0.904.
To increase the reliability of the entire system, Multiple copies of the same device are connected in
parallel through the use of switching circuits. The switching circuits determine which devices in the
group are functioning properly. They make use of one such device at each stage.
Formula: