|
UNIT – III |
|||||||||||||||||||||||||||||||||||
|
Unit-III/Lecture-01 |
|||||||||||||||||||||||||||||||||||
|
Concept of Dynamic Programming |
|||||||||||||||||||||||||||||||||||
|
Concept of dynamic programming: Dynamic Programming(usually referred to as
DP ) is a powerful
technique that allows to solve many different types of problems in time O(n2)
or O(n3) for which a naive approach would take exponential time. In
the word dynamic programming the word programming stands for “planning” and
it does not mean by computer programming. Dynamic programming is typically
applied to optimization problem. Dynamic Programming is an algorithmic paradigm that solves
a given complex problem by breaking it into subproblems and stores the
results of subproblems to avoid computing the same results again. Following
are the two main properties of a problem that suggest that the given problem
can be solved using Dynamic programming. 1) Overlapping Subproblems 1) Overlapping Subproblems: Like Divide
and Conquer, Dynamic Programming combines solutions to sub-problems. Dynamic Programming
is mainly used when solutions of same subproblems are needed again and again.
In dynamic
programming, computed solutions to subproblems are stored in a table so that
these don’t have
to recomputed. So Dynamic Programming is not useful when there are no common (overlapping)
subproblems because there is no point storing the solutions if they are not
needed again. For
example, Binary Search doesn’t have common subproblems. If we take
example of following
recursive program for Fibonacci Numbers, there are many subproblems which are
solved again and
again. int fib(int n) { if ( n <= 1 ) return n; return fib(n-1) +
fib(n-2); } 2)
Optimal Substructure A problem is said to have optimal
substructure if an optimal solution can be constructed efficiently from
optimal solutions of its subproblems. This property is used to determine the
usefulness of dynamic programming and greedy algorithms for a problem. There
are two ways of doing this. 1)
Top-Down : Start solving the given problem by breaking it down.
If you see that the problem has been solved already, then just return the
saved answer. If it has not been solved, solve it and save the answer. This
is usually easy to think of and very intuitive. This is referred to as Memoization. 2)
Bottom-Up : Analyze the problem and see the order in
which the sub-problems are solved and start solving from the trivial
subproblem , up towards the given problem. In this process, it is guaranteed
that the subproblems are solved before solving the problem. This is referred
to as Dynamic Programming: Note
that divide and conquer is slightly a different technique. In that, we divide
the problem in to non-overlapping subproblems and solve them independently,
like in mergesort and quick sort. Principal of optimality: [RGPV June-2014(2)] The principle of optimality states that no
matter what the first decision, the remaining decisions must be optimal with
respect to the state that results from this first decision. This principle
implies that an optimal decision sequence is comprised for some formulations
of some problem. Since the
principle of optimality may not hold for some formulations of some problems,
it is necessary to verify that it does not hold for the problem being solved. Dynamic
programming cannot be applied when this principle does not hold.
Unit-III/Lecture-02 |
|||||||||||||||||||||||||||||||||||
|
0/1 knapsack Problem |
|||||||||||||||||||||||||||||||||||
|
0/1 knapsack: [RGPV
June-2014(3)] 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 don’t pick it (0-1 property). Problem Description If we are
given n objects and a knapsack or a bag in which the object i that has weight
wi is to be placed. The knapsack has a capacity W. Then the
profit that can be earned is pixi. The objective is to obtain filling
of knapsack with maximum profit earned.Maximized pixi. subject to constraint wixi<=W
Where 1<=i<=n and n is total no. of objects and xi =0 or 1. Steps and Notations Step-1: Let fi(yi)
be the value of optimal solution. Then si is pair (p,w)
where p=f( yj) and w=yj
Initially s0
= {(0,0)} We can
compute si+1 from si . These computations of si are
basically the sequence of
decisions made for obtaining the optimal solutions. Step-2: We can
generate the sequence of decisions in order to obtain the optimum selection
for solving the knapsack problem. Let xn
be the optimum sequence. Then there are two instances {xn} and {xn-1,xn-2….x1}. So from {xn-1,xn-2….x1}
we will choose the optimum sequence with respect to xn. The
selection of sequence from remaining set should be such that we should be able
to fulfill the condition of filling knapsack capacity W with maximum profit. Otherwise
{xn-1,xn-2….x1} is not optimum. This proves
that 0/1 knapsack problem is solved using principle of optimality. Step-3: Let fi(yj)
be the value of optimal solution. Then fi(y) = max{fi-1(y), fi-1(y-wi)+pi} Initially
compute s0={(0,0)} si1
= {(P,W)|(P-pi, W-wi) belongs to si} Si+1
can be computed by merging si and si1 Purging
rule If si+1
contains (Pj, Wj) and (Pk, Wk)
these two pairs such that Pj<=Pk and Wj>=Wk, then (Pj,Wj)
can be eliminated . This purging rule is also called as dominance rule. In
purging rule basically the dominated tuples gets purged. In short remove the
pair with less profit and more weight. Problem-1 Solve
knapsack instance M=8, and n=4. let pi and wi are as
shown below.
Solution- s0={(0,0)}
initially s01={(1,2)} That means while building
s01 we select the next ith pair. For s01
we have selected first (P,W) pair which (1,2). S1 ={merge s0
and s01} ={(0,0),(1,2)} s11={select
next (P,W) pair and add it with s1}
={(2,3),(2+0,3+0), (2+1,3+2)} s11
={(2,3),(3,5)} // repetition of
(2,3) avoided. S2 ={merge
candidates from s1 and s11} ={ (0,0),
(1,2), (2,3), (3,5)} S21 ={select next (P,W) pair and
add it with s2} ={(5,4),(6,6),(7,7),(8,9)} S3 ={merge
candidates from s2 and s21} ={ (0,0),
(1,2), (2,3),(5,4),(6,6),(7,7),(8,9)} Note that the pair (3, 5)
is purged form s3. Because let us assume (Pj,
Wj) = (3, 5) and (Pk,
Wk) = (5, 4). Here Pj<=Pk
and Wj>Wk is true hence we will eliminate pair (Pj,
Wj) = (3, 5) from s3. S31 ={select next (P,W) pair and
add it with s3} S31 ={(6,5),(7,7),(8,8),(11,9),(12,11)
,(13,12) ,(14,14)} S4={(0,0),(1,2),(2,3)
,(5,4) ,(6,6) ,(7,7),(8,9) ,(6,5) ,(8,8),(11,9),(12,11) ,(13,12) ,(14,14)} Now we are interested in
M=8. We get pair (8, 8) in s4. Hence we will set x4=1. Now we select next object
(P-p4) and (W-w4). i.e (8-6) and (8-5). i.e (2,3) Pair (2,3) belongs to s2.hence
set x2=1. So we get the final solution
as (0, 1, 0, 1).
Unit-III/Lecture-03 |
|||||||||||||||||||||||||||||||||||
|
Multistage graph |
|||||||||||||||||||||||||||||||||||
|
Multistage graph:[RGPV June-2014(2)] A multistage graph G=(V,E) is a directed graph in which the
vertices are partitioned into k ≥ 2 disjoint sets Vi, 1
≤ i ≤ k . In addition if (u, v) is an edge in E, then u ε Vi
and v ε Vi+1, for some i, 1 ≤ i ≤ k. The sets V1
and Vk are such that |V1|=|Vk|=1. Let s and t, respectively be the vertex in V1
and Vk. The vertex s is the source, and t the sink. The above definition says that the vertices are divided
into several disjoint partitions in a multistage graph. Each partition is
called as a stage which contains several vertices. The first and last partition /stage of the graph contains one vertex each,
namely, the source (s) and the sink (t). Let c(i, j) be the cost of edge (i, j). The cost of a path
from s to t is the sum of the costs of the edges on the path. The multistage
graph problem is to find a minimum cost path from s to t. It should be noted
that each set Vi defines a stage in the graph. Because of the
constraints on E (the set of edges), every path from s to t starts from the
source vertex in stage 1, goes to stage 2, then to stage 3 and so on, and
eventually terminates at the sink vertex in stage K, i.e. the last stage.
Consider the directed graph given below. Fig-3.1 Multistage Graph with Five Stages There are five stages in the graph i.e. k = 5 in this
graph. The five stages are listed below: From the graph given in Fig-3.1, it can be noticed that the
shortest path from the source vertex to sink vertex is "1 - 2 -7 - 10
-12". Tthe path from s to t starts from the source vertex in stage 1,
goes to stage 2, then to stage 3 and so on, and terminates at the sink vertex
in stage 5. i.e. the shortest path from s to t starts from the source vertex,
1, which is in stage1, travels through vertex 2 which is in stage 2, vertex 7
which is in stage 3, vertex 10 which is in stage 4 and terminates at the sink
vertex, 12, which is in stage 5. There
are many real-life problems that can be formulated as multistage graph
problem. Few examples include resource allocation for a project in a software
company or manufacturing company, project management, job scheduling in
operating system etc. Finding the Shortest Path from Source to Sink From the weights assigned in the graph, it is easy to
observe the following values: There are two approaches, namely, forward approach and
backward approach, to find the shortest path from the source node to the sink
node in a multistage graph. Forward approach:
Assume at stage k that we know the
min cost path from each node in stage k+1 to the goal state. MinCost(Q,Goal)=Min(Cost(Q,R1)+MinCost(R1,Goal),
Cost(Q,R2)+MinCost(R2,Goal), …, Cost(Q,Rn)+MinCost(Rn,Goal)) The cost is computed as follows
using the forward approach.
It
should be noted that in the calculation of cost (2,2), we have reused the
values of cost (3, 6) and cost (3, 7) and cost (3, 8), and thereby avoiding
the recomputation. A minimum cost path from s to t has the cost of 16. This
path can be determined easily if we record the decision made at each state
(vertex). Let the minimum-cost path be s = 1, v2, v3,
….vk-1, t. It is easy to see that v2=D(1,1) = 2, v3
= D(2, D(1,1)) = 7 and v4 = D (3, D(2, D(1,1))) = D(3,7) = 10. The
minimum-cost path from the source node, s, to the sink node, t, for the graph
shown in Figure 1 using the forward approach is “1-2-7-10-12”. The minimum
cost is 16. Backward approach: Assume at stage k that we know the
min cost path from start state to each node in stage k - 1. MinCost(Q,Goal)=Min(Cost(P1,
Q)+MinCost(Start, P1), Cost(P2, Q)+MinCost(Start, P2), …,
Cost(Pn)+MinCost(Start, Pn)) The backward approach is similar to forward
approach. The computation in forward approach start from V(k-2),
whereas, in backward approach it starts from V3.
Backward approach employs the following computation. Let
bp(i ,j ) be a minimum cost path from vertex s to a vertex j in Vi.
Let bcost (i,j) be the cost of bp( i , j ). Fig-3.2 Multistage Graph with Five Stages The computations by employing backward approach using
formula (2) are given below: Therefore , the minimum path using the backward approach
for the graph given in Figure 3.2 is
Unit-III/Lecture-04 |
|||||||||||||||||||||||||||||||||||
|
Problems based
on Multistage graph |
|||||||||||||||||||||||||||||||||||
|
:[RGPV/June-2014(7)] Q.1 Find a
minimum cost path from ‘S’ to ‘t’ in multistage graph using dynamic
programming? Solution: d(S, T) = min{1+d(A, T), 2+d(B, T), 5+d(C, T)} d(A,T) = min{4+d(D,T), 11+d(E,T)} d(B, T) = min{9+d(D, T), 5+d(E, T), 16+d(F, T)} = min{9+18, 5+13, 16+2} = 18. d(C, T) = min{ 2+d(F, T) } = 2+2 = 4 d(S, T) = min{1+d(A, T), 2+d(B, T), 5+d(C, T)} = min{1+22, 2+18, 5+4} =
9. Q.2 Find a
minimum cost path from ‘S’ to ‘t’ in multistage graph using dynamic
programming? [RGPV JUNE-2014]
Unit-III/Lecture-05 |
|||||||||||||||||||||||||||||||||||
|
Reliability
Design |
|||||||||||||||||||||||||||||||||||
|
Reliability Design: Reliability
means the ability of an apparatus, machine, or system to consistently perform
its intended or required function or mission, on demand and without degradation
or failure.
Fig-3.3(a)
Devices connected in series Fig-3.3(b)
Multiple Devices Connected in Parallel in each stage Multiple
copies of the same device type are connected in parallel (Fig-3.3(b)) through
the use of switching circuits. The switching circuits determine which devices
in any given group are functioning properly. They then make use of one such device
at each stage. If stage i contains mi copies of device Di then the probability that all mi have a malfunction is (1 - ri)mi . Hence the reliability of stage i becomes 1 - (1 - ri )mi. Thus, if ri = 0.99 and mi = 2 the stage reliability becomes
0.9999. In any practical situation, the stage reliability will be a little
less than 1 - (1 - ri )mi because the switching circuits themselves are not
fully reliable. Also, failures of copies of the same device may not be fully
independent (e.g. if failure is due to design defect). Let us assume that the
reliability of stage i is actually given by a function Φi(mi), 1<=i<=n. (It is quite conceivable that Φi(mi) may decrease after a
certain value of m ;). The reliability of the system of stages is ∏1<=i<=n Φi(mi). Our problem
is to use device duplication to maximize reliability. This maximization is to
be carried out under a cost constraint. Let ci be the cost of each unit of device i and let c
be the maximum allowable cost of the system being designed. We wish to
solve the following maximization problem: maximize ∏1<=i<=n Φi(mi) subject to
∑1<=i<=n
cimi
<=c mi>=1 and integer i, 1<=i<=n A dynamic
programming solution may be obtained in a manner similar to that used for the
knapsack problem. Since, we may assume each ci; > 0, each mi must be in the range 1<=mi<=ui where ui
= └( c + ci
- ∑1 to n cj )
/ ci┘ The upper
bound ui follows from the observation that mj>=1. An optimal solution m 1
, m 2, ••• , mn is the result of a sequence of
decisions, one decision for each mi. Letfi(x)
represent the maximum value of Φ(mj), 1<=j<=i
subject to the constraints ∑1<=j<=i cjmj <=x and 1<=mj<=uj,
1<=j<=i. Then, the value of an optimal solution is fn(c).
The last decision made requires one to choose mn from
one of { l, 2, 3, ... , un.}. Once a value for mn
has been chosen, the remaining decisions must be such as to use the
remaining funds c - cnmn in an optimal
way. The principal of optimality holds and fn(c)
= max 1<=mn<=un{ Φn(mn) fn-1(c – cnmn) } For any fi(x),i>=1 this
equation generalizes to fi(x)
= max 1<=mi<=ui{ Φi(mi) fi-1(c
– cimi) }
Unit-III/Lecture-06 |
|||||||||||||||||||||||||||||||||||
|
Problems based
on Reliability design |
|||||||||||||||||||||||||||||||||||
|
Problems based
on Reliability design: Q.1 Design a three stage system with device types D1,D2,D3. The costs
are Rs. 30, Rs. 15 and Rs. 20 respectively. The cost of the system is to be
no more than Rs. 105. The reliability of each device type is 0.9,0.8 and 0.5
respectively. Solution: We will
first compute u1, u2, u3 using following
formula. ui = (C + Ci
– sigma Cj )/ Ci For
computing ui u1 = 2(approx value) For
computing u2 u2 = 3(approx value) For
computing u3 u3 = 3 Hence (u1 ,u2 ,u3)
Computing
subsequences- S0
= (1,0) Let Si
consist of tuples of the form (f, x) =(r, c) S0
= {(1,0)} For device
D1 for 1 D1 r1=0.9,c1=30 S11
= {(0.9, 30)} For device D1
for 2 D1 m1=2(2
D1 device in parallel) Reliability
of stage 1 = 1-(1- r1) 2 Reliability
of stage 1 = 1-(1- 0.9) 2 = 0.99 Cost =30*2
= 60 S12
= {(0.99, 60) } S1 = {(0.9, 30), (0.99, 60) } S1 = {(0.9, 30), (0.99, 60) } For one Device D2:-
S21 = {(0.72,
45), (0.792, 75) } For two Device D2:- S22 = {(0.864, 60), (0.9504, 90) } For three Device D2:- S23 = {(0.8928, 75), (0.98208, 105) } S2
= {(0.72, 45), (0.792, 75),
(0.864, 60),(0.9504, 90),
(0.8928, 75), (0.98208, 105) } (0.792,
75), (0.9504, 90) is eleminated due to purging or dominance rule and (0.98208, 105) is eleminated due to access
cost 105. After this we got S2 = { (0.72, 45), (0.864, 60), (0.8928, 75) } For one Device D3:- S31 = {
(0.36, 65), (0.432, 80), (0.4464, 95) } For Two Device D3:- S32 = {
(0.54, 85), (0.648, 100)} For Three Device D3:- S33 = { (0.63, 105) } Now we are going to find S3 S3 = { (0.36, 65), (0.432, 80), (0.4464, 95), (0.54, 85), (0.648,
100), (0.63, 105) } Due to
purging rule after elimination we get S3 = { (0.36, 65), (0.432, 80), (0.54, 85), (0.648, 100),} Now The best
design has a reliability of 0.648 and a cost of 100. Tracing
back through Si ‘s We
determine that m1 = 1, m2 = 2, m3 = 2 Unit-III/Lecture-07 |
|||||||||||||||||||||||||||||||||||
|
Floyd-Warshall
algorithm |
|||||||||||||||||||||||||||||||||||
|
Floyd-Warshall
algorithm:[RGPV/June-2013(7),2014(7)] Floyd-Warshall algorithm is a procedure, which is used to
find the shortest (longest) paths among all pairs of nodes in a graph, which
does not contain any cycles of negative lenght. The main advantage of
Floyd-Warshall algorithm is its simplicity. Description Floyd-Warshall
algorithm uses a matrix of lengths D0 as its input. If there is an
edge between nodes i and j, than the matrix D0 contains its length
at the corresponding coordinates. The diagonal of the matrix contains only
zeros. If there is no edge between edges i and j, than the position (i, j)
contains positive infinity. In other
words, the matrix represents lengths of all paths between nodes that does not
contain any intermediate node. In each
iteration of Floyd-Warshall algorithm
is this matrix recalculated, so it contains lengths of paths among all pairs
of nodes using gradually enlarging set of intermediate nodes. The matrix D1,
which is created by the first iteration of the procedure, contains paths
among all nodes using exactly one (predefined) intermediate node. D2
contains lengths using two predefined intermediate nodes. Finally the matrix
Dn uses n intermediate nodes. This transformation can be described
using the following recurrent formula: Floyd’s
Algorithm: Define the
notation Dk[i, j], 1 £ i, j £ n, and 0 £ k £ n, that stands for the
shortest distance (via a shortest path) from node i to node j,
passing through nodes whose number (label) is £ k. Thus, when k = 0, we have D0[i, j]
= W[i][j] = the edge weight from node i to node j This is
because no nodes are numbered £ 0 (the nodes are numbered 1 through n). In general, when k ³ 1, Dk[i, j] =
min(Dk –1[i, j], Dk –1[i,
k] + Dk –1[k, j]) The reason
for this recurrence is that when computing Dk[i, j],
this shortest path either doesn’t go through node k, or it passes
through node k exactly once.
The former case yields the value Dk –1[i,
j]; the latter case can be illustrated as follows: Implementation
of Floyd’s Algorithm: Input:
The weight matrix W[1..n][1..n] for a
weighted directed graph, nodes are
labeled 1 through n. Output: The shortest distances between all pairs of the
nodes, expressed in an n ´ n matrix. Algorithm: Create a matrix D and initialize it to W. for k
= 1 to n do for
i = 1 to n do for j = 1 to n do D[i][j] = min(D[i][j], D[i][k] + D[k][j]) Note that one single matrix D is used to store Dk–1 and Dk, i.e., updating from Dk–1 to Dk is done
immediately. This causes no problems
because in the kth iteration, the value of Dk[i,
k] should be the same as it
was in Dk–1[i, k]; similarly for the value of Dk[k,
j]. The time complexity of the above algorithm
is O(n3) because
of the triple-nested loop; the space complexity is O(n2) because only one matrix is used. Example: We demonstrate Floyd’s algorithm for computing Dk[i,
j] for k = 0 through k = 4, for the following weighted directed graph: Solution:
|