Dynamic Programming

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Design and Analysis of Algorithms Module 4 15CS43

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.

Multistage graph problem can be solved by using two methods


1. Forward approach

Dept. of CSE, DSATM 2017-2018 Page 1


Design and Analysis of Algorithms Module 4 15CS43

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.

Dept. of CSE, DSATM 2017-2018 Page 2


Design and Analysis of Algorithms Module 4 15CS43

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:

Dept. of CSE, DSATM 2017-2018 Page 3


Design and Analysis of Algorithms Module 4 15CS43

Dept. of CSE, DSATM 2017-2018 Page 4


Design and Analysis of Algorithms Module 4 15CS43

Problem 3: Using dynamic programming, solve the following knapsack instance:


n=3, [ω1, ω2, ω3] = [1, 2, 2] and [P1, P2, P3] = [18, 16, 6] and M=4. (04 M, Jan 10)

Dept. of CSE, DSATM 2017-2018 Page 5


Design and Analysis of Algorithms Module 4 15CS43

Dept. of CSE, DSATM 2017-2018 Page 6


Design and Analysis of Algorithms Module 4 15CS43

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.

Dept. of CSE, DSATM 2017-2018 Page 7


Design and Analysis of Algorithms Module 4 15CS43

Rules for changing zeros to one.


 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).

Dept. of CSE, DSATM 2017-2018 Page 8


Design and Analysis of Algorithms Module 4 15CS43

Time complexity for warshall O(n3)

Floyd’s Algorithm for the All-Pairs Shortest-Paths 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.
Floyd’s is applicable to both undirected and directed weighted graphs provided that they do not contain a
cycle of a negative length.

Time complexity of warshall O(n3)

Problem:

Dept. of CSE, DSATM 2017-2018 Page 9


Design and Analysis of Algorithms Module 4 15CS43

Single-Source Shortest Paths: General Weights


Dijkstra ‘s Algorithm does not allow negative weights to its edges. If the graph contains negative weights in
its edges.

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.

Dept. of CSE, DSATM 2017-2018 Page 10


Design and Analysis of Algorithms Module 4 15CS43

Time complexity of Bellman ford


If adjacency matrix is used- O(n3)
If adjacency list is used- O(ne)

The Travelling Salesperson Problem


Let G=(V, E) be a directed graph with edge costs cij. The variable cij is defined such that cij>0 for all i and j
and cij= ∞ 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 the 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.
Time complexity of this problem using dynamic programming is exponential : O(2n)

Optimal Binary Search Tree problem.


Given {a1, a2,…an} be a set of identifiers such that a1 < a2 < a3…. and p(i)- the probability of successful
search. Optimal binary search tree problem aims at constructing the optimal binary search tree in which any
node can be accessed efficiently.
Note: the total number of possible binary search trees with n keys is equal to the nth Catalan number

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:

Dept. of CSE, DSATM 2017-2018 Page 11


Design and Analysis of Algorithms Module 4 15CS43

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.

Fig: Multiple devices connected in parallel in each stage.


If stage ‘i’ contains mi copies of devices Di,
Reliability of stage i= 1-(1-ri)mi
If ri = 0.99 and mi = 2, then stage reliability= 0.9999.

Formula:

Dept. of CSE, DSATM 2017-2018 Page 12

You might also like