Unit 3 - Analysis Design of Algorithm - WWW - Rgpvnotes.in
Unit 3 - Analysis Design of Algorithm - WWW - Rgpvnotes.in
Tech
Subject Name: Analysis and Design of Algorithm
Subject Code: CS-402
Semester: 4th
Downloaded from be.rgpvnotes.in
Concept of dynamic programming, problems based on this approach such as 0/1 knapsack,
multistage graph, reliability design, Floyd Warshall algorithm
Unit-3
Concept of Dynamic Programming
Dynamic programming is a name, coined by Richard Bellman in 1955. Dynamic programming, as
greedy method, is a powerful algorithm design technique that can be used when the solution to the
problem may be viewed as the result of a sequence of decisions. In the greedy method we make
irrevocable decisions one at a time, using a greedy criterion. However, in dynamic programming we
examine the decision sequence to see whether an optimal decision sequence contains optimal
decision subsequence.
When optimal decision sequences contain optimal decision subsequences, we can establish
recurrence equations, called dynamic-programming recurrence equations, that enable us to solve the
problem in an efficient way.
Dynamic programming is based on the principle of optimality (also coined by Bellman). The
principle of optimality states that no matter whatever the initial state and initial decision are, the
remaining decision sequence must constitute an optimal decision sequence with regard to the state
resulting from the first decision. The principle implies that an optimal decision sequence is comprised
of optimal decision subsequences. Since the principle of optimality may not hold for some
formulations of some problems, it is necessary to verify that it does hold for the problem being
solved. Dynamic programming cannot be applied when this principle does not hold.
• Solve the dynamic-programming recurrence equations for the value of the optimal solution.
• Perform a trace back step in which the solution itself is constructed.
0/1 – KNAPSACK
Given weights and values of n items, put these items in a knapsack of capacity W to get the
maximum total value in the knapsack. In other words, given two integer arrays val[0..n-1] and
wt[0..n-1] which represent values and weights associated with n items respectively. Also given an
integer W which represents knapsack capacity, find out the maximum value subset of val[] such that
sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick
the complete item, or do ’t pick it (0-1 property).
Dynamic-0-1-knapsack (v, w, n, W)
for w = 0 to W do
c[0, w] = 0
for i = 1 to n do
c[i, 0] = 0
for w = 1 to W do
if i ≤ the
if vi + c[i-1, w-wi] then
c[i, w] = vi + c[i-1, w-wi]
else c[i, w] = c[i-1, w]
else
c[i, w] = c[i-1, w]
int[] MStageForward(Graph G)
{
// returns vector of vertices to follow through the graph
// let c[i][j] be the cost matrix of G
Time complexity:
Complexity is O (|V| + |E|). Where the |V| is the number of vertices and |E| is the number of edges.
Example
Consider the following example to understand the concept of multistage graph.
Cost(2, 3) = {c(3, 4) + Cost(4, 8) + Cost(8, 9), c(3, 5) + Cost(5, 8)+ Cost(8, 9), c(3, 6) + Cost(6, 8) + Cost(8, 9)} = 10
RELIABILITY DESIGN
In reliability design, the problem is to design a system that is composed of several devices connected
in series.
If we imagine that r1 is the reliability of the device.Then the reliability of the function can be given
by πr .If r1 = 0.99 and n = 10 that n devices are set in a series, 1 <= i <= 10, then reliability of the whole
system πri can be given as: Πri = .
So, if we duplicate the devices at each stage then the reliability of the system can be increased.
It can be said that multiple copies of the same device type are connected in parallel through the use of
switching circuits. Here, switching circuit determines which devices in any given group are functioning
properly. Then they make use of such devices at each stage, that result is increase in reliability at each
stage. If at each stage, there are mi similar types of devices Di, then the probability that all mi have a
malfunction is (1 - ri)^mi, which is very less.
And the reliability of the stage I becomes (1 – (1 - ri) ^mi). Thus, if ri = 0.99 and mi = 2, then the stage
reliability becomes 0.9999 which is almost equal to 1. Which is much better than that of the previous
case or we can say the reliability is little less than 1 - (1 - ri) ^mi because of less reliability of switching
circuits.
In reliability design, we try to use device duplication to maximize reliability. But this maximization
should be considered along with the cost.
In the all pairs shortest path problem, we are to find a shortest path between every pair of vertices in
a directed graph G. That is, for every pair of vertices (i, j), we are to find a shortest path from i to j as
well as one from j to i. These two paths are the same when G is undirected.
When no edge has a negative length, the all-pairs shortest path problem may be solved by using
Dijkstra’s greedy si gle source algorith ti es, o ce ith each of the ertices as the source
vertex.
The all pairs shortest path problem is to determine a matrix A such that A (i, j) is the length of a
shortest path from i to j. The matrix A can be obtained by solving n single-source problems using the
algorithm shortest Paths. Since each application of this procedure requires O (n 2) time, the matrix A
can be obtained in O (n3) time.
Complexity Analysis:
A Dynamic programming algorithm based on this recurrence involves in calculating n+1 matrices,
each of size n x n. Therefore, the algorithm has a complexity of O (n3).
Problem-
Consider the following directed weighted graph-
Step-03:
The four matrices are-
The last matrix D4 represents the shortest path distance between every pair of vertices .