_SyYtiasus
Dynamic Programming : General Method, Applications-Optimal Binary Search Trees, 0/1
Knapsack Problem, All Pairs Shortest Path Problem, Traveling Salesperson Problem, Reliability
LEARNING OBJECTIVES
¥ General Method of Dynamic Programming
Design.
¥ Applications of Dynamic Programming
> Optimal Binary Search Trees
v
0/1 Knapsack Problem
> All Pairs Shortest Path Problem
> Travelling Sales Person Problem
Basic Concept of Reliability Design.
NTRODUCTION
Dynamic programmiing is a method where a given problem is divided into subproblems. The
‘applications of dynamic programming are optimal binary search trees, 0/1 knapsack problem,
all pe
shortest path problem, fravelling sales person problem. A tree having optimum cost
is known as optimal binary search tree. The'concept of merging and purging is used in 0/1
knapsack algorithm. i:
ln the travelling sales person problem, the main requirement is to minimize the travelling cost
by communicating with the nodes. The main problem is to design a system which is composed
°f several devices connected in series. :
: ; ‘pc Anyone found guilty fs ABLE fo face LEGAL proceadings,
Qaeeeeeaecem e i a)DESIGN AND. ANALYSIS OF ALGORITHM
7
BH Se s Soe
aim ing. Find one proble,
.d of dynamic programmi woe
al ip not hold. Explain why the principle does no,
hs
GENERAL METHOD |
Q1. Write a detailed note on the ger
for which the principle of optimality
hold. én
optimality in dynamic programming. How to apply this to the shortes,
leof
State the prin
path problem. :
(Refer Only Topic: Principle of Optimality)
Foose 200), og)
Answer:
fe Bppantc Programming thod where a given problem is divided into subproblems. If necessary,
Pez Dynamic programming is a miethoy lems are then solved and stored in a tab
in divided into sub subproblems. The subproblems .
one er asain solving the same subproblem (previously solved) arses, then the stored solution i;
Por nein ete pe el i eee
fetched from the table instea ah
to determine the problem solutions that can be considered as the result of a series of decisions.
A combination of four sequential steps occurs in dynamic programming,
1. The structure of the solution is to be characterized.
2. Representing the optimal solution wherever needed in the subproblems (defining recursively).
3. The optimal solution has to be constructed from the information.
4, The final solution has to be constructed from the information.
Principle of Optimality
According tothe principle of optimality, the decisions resulting from the first decisions has a property that
they are in optimal sequence irrespective of the initial state and decisions. The significance of this rinciple is that,
ptimal seq Pr
italways give the optimal solution from many feasible solutions. Also, optimal solution will be achieved from the
initial state itself,
tn short it canbe said that, when an optimal solution is found for a problem, then optimal solutions are also
found for its subproblems as well
Principle of optimality is applied to the shortest path problem such that if kis a node/vertex on the shortest
> Pat from ito then the subpaths from ito k and from k to j must also be optimal Therefore, it can be said that the
“optimal solution to the instance contains optimal solutions toall the subinstances.
‘The problem for which principle of optimality does not bi
{ ‘why itdoes not hold is explained with.the help of example,
‘old is longest simple path problem. The reason
Ponds tothe path P+ R-+ 9 ~ S, henceit
anoth£° Obviously the optimal solution of te
“Path that goes from P.to R, which is P*3 Dynamic Programming
uni. 89
a. Differentiate between dynamic programming and divide and conquer,
wrever ‘Aug-22(R18), 0510)
Dynamic Programming | + Divide-and-Conquer
Dynamic Programming is afourstep process, | 1. | Divide-and-Conquer is a three step process,
Structure of solution is characterized i) The problem is divided into many
, tb-problems,
Gi) Optimal solutions are represented as (ii) Sub-problem are solved recursively
subproblem recursively,
(ii) The value of optimal solution is (iii) The solutions of subproblems are merged
generated in bottom-up fashion, to form solution for original problem
{v) Final solution is generated from
computed information.
Any duplicates in solutions are avoided. 2. | There is possibility for existence of duplicate
solutions,
Itis highly efficient. 3. | Iris tess efficient.
{| It uses bottom-up approach(iterative), 4, | Ituses top-down approach(recursive).
‘The subproblems are dependent on each| 5. | The subproblems are independent of each other.
other.
Example: Matrix chain multiplication, 6. | Example: Merge sort,
Q3, Given a sequence of n real numbers A(1),...., A(n), write a procedure to determine a
contiguously subsequence A(i), .. A() for which the sum of elements in the subsequences
is maximized. 4 \
Answer: -
‘The maximum contiguous subsequence problem takes an array (I...) of real numbers as input. Here, the
glist find the contiguous subsequence from position i to position j which has the maximum sum of numbers
ftom! = ito, .e,, max SALI] in such a way that the quality is maximized. Diagrammaticaly itis represented as
&
shown in figure.
@ ®
Figure
The given problem will be interesting to solve if there are negative numbers in the array, otherwise the
‘whole array must be taken as the solution. The problem can be solved by using dynamic programming approach
in linear time, For this the subproblems chosen are MIj] that denote the maximium sum of all possible windows
‘tading at position j. Hence, the recursive formula for M{j] can be written as,
MU] = max{(MUj-11+ AU). AU)
‘To determine the optimal window ending at position j, either problem in which duplicate instances’ can be
‘sed for each type of item. This is because there is coin denominations for each of the value Yj, Thus, an instance
of knapsack problem can be constructed where in each item has size s;, which is equal to the value of #* coin
‘enomination arid every item will be assigned a value of ~1. Hence, the optinial way to make change for the
‘mount C with a few coins as possible is similar to the optimal way to exactly fill up the capacity in the knapsack
Of capacity C. ‘The reason. for similarity is that since every item has a value of -1, then the knapsack problem can
‘sed as few items as possiple corresponds for using few coins
{Toforniulate making change problem using dynamie programming, the following procedure is caried out,
ons to subproblems, where MU] represents the minimum number of coins
MU at oni i er loping nearer mal
MU) = min(myj= Vi +1)
Com = Taizor tis book lea CRIMINAL act Anyone found guilty Is LIABLE to face LEGAL. Proceedings. )90.
For this the following altematives’ can be
considered,
(Extend the optimal window ending at position
j= 1 (ie. (MUj= 11+ AUD).
Start a new window having only AU
‘The altematives are sufficient, since when
‘computing an optimal way to make the window end at
position j, either the previous window is extended oF
not extended. If previous window is to be extended, the
best window to extend is the optimal window ending
at position j~ 1. The time complexity of this algorithm
‘will be O(n) because n subproblems need to be solved,
each of which takes O(1) time,
S(BAPPLICATIONS
Q4. Explain how matrix chain multiplication
problem can be solved using dynamic
programming with suitable example.
Mayidune-19(R16), 29
(OR)
Write an algorithm of matrix. chain
multiplication,
(Refer Only Top
Ww
gorithm)
NowsDec.-18(R16), a8(b)
Matrix Chain Multiplication
For the multiplication of ‘n’ matrices 'a series
of operations need to be performed. A dynamic
programming approach gives the optimal sequence or
‘order of operations for this problem,
‘Now perform the multiplication of ‘n" matrices
Ay, AayonAyy Which can be accomplished by a series of
‘matrix multiplication,
ALK ADK Ag Kin Ay «
The above order of multiplication cannot ‘be
changed since matrix multiplication is associative
rather it can be parenthesized.
Example
Consider three matrices
Aguas Bass Crus
Multiplication cost of,
(ABC) = (5 x43) 4 (583 x5) =135
Muhtplication cost. of, :
_ BO) =(4%3%5)+(5%4%5)= 160,
“haste PCT, it can be’ seen from the ‘above
. ince meth diferent evaluation ‘Sequences implies
The problem i sii
of the expression, eigen eae
‘minimizes the total number of sci
Performed. In the dynamic programming approach,
Brobltn is divided into several subproblem and ier
the solutions ofthese subproblems are combined ot
the solution ofthe original problem. As the orders
4 SIA PUBLISHERS AND DISTRIBUTORS
DESIGN AND_ ANALYSIS OF ALGORITHMS,
‘multiplica ;
parenthesized to achieve opt
‘n’ matrices. ie
Let A;.) denote the multiplication of matrices
through i to and the order of A, 518 rsx ry.
Inorder to break the problem into. severa,
number of subproblems of same kind as the original
problem and start parenthesizing the. subsequences
1s the situation needs. Consider the highest level of
parenthesizing where two matrix multiplications are
assumed 10. Ag,p = Atte Ate tcc
Now, the questions of interest are how to split
the order of matrices? And how to parenthesize the
subsequences Ay... 4nd Akgt
‘The answer to the first question is to try out all
possible choices of k and pick out the best among them,
but it does not lead to the exponential growth,
The ariswer to the second question is to try out
repeatedly by parenthesizing depending upon the need,
‘Another important thing to note about this matrix
chain multiplication problem is. that, it is possible to
characterize on optimal solution to a particular problem
in terms of optimal ‘solutions to its subproblems. It
does mean that “principle of optimality” applies to this
problem.
Dynathic programming approach computes
the solutions of the subproblems and stores them into
a table thereby avoiding the work of recomputing the
solution whenever that subproblem is encountered.
\ Let Ny denotes the minimum number of
multiplications needed to compute 4,, ; where 1 < i$
isn.
If the sequence contains only one matrix ie.,ifi
=/ then the cost of operation is zero,
ie, Mi fl=0
If i< j then the multiplication for Ajj is
computed by considering each k.
Bing = Acct Anyi.j Where i$ kSj.
Mi. Al and Nik-+1, J] age the optimum
‘multiplications needed to compute A; , and Asst.)
Sincé 7.1% 7 is the order of A, and rx ris the order
OF Asst... the total number ef operations needed 10
multiply Aa and Avs ji8 ry. ry 1
+ NUi.j] can be computed as,
Mf = min NU, yoy where
i jand oe A Me tr re
‘The algorithm for te’chain matrix multiplication
using dynamic Programming approach is given below.
Algorithm
Algorithm MATRIXMUL (A)
1. begin BV
"2. integmeemnrepenereme
unit3 : Dynamic Programming 91
ened
3, fori 1 tondo
4 Nfi, j] © 05 if i
5 forbé2tondo
6 fori <1 ton—b+1 do
1.
8.
9
.¢., only one matrix,
jis;
Nfis oo
fork i toj-t do
10. Nfiyj] © min (NG, K] + NUKE, ) 414 ot
nn end
12 end
13, end
14, end MATRIXMUL.
Inthe above algorithm, there are three nested loops and each loop can iterate almost ‘n’ times which implies
that above algorithm runs in O(n’) times.
Q5. Find the minimum number of operations required for the following chain matrix
multiplication using dynamic programming.
‘A(30,40) * B(40,5) x C(5,15) = D(15,6).
Answer:
Given that,
‘AGO, 40) x B(40, 5) x C(S, 15) x DUS, 6)
‘The operations can be determined based on the ways, in which matrices can be arranged. These matrices
can be arranged in the following ways. They are,
© — (A@(CD)): Initially, CD is solved, then other expressions and then all of them are added up.
= 5X15X6440x 5x 6+ 30x 40x 6 ;
(The above values are obtained by taking common values from each of the matrices.)
5x 15x 15x6
5x 15x6
40x5x5x15x6
40x5x6
30x 40 x 40x 5 x6
30x40 x6
+ (A(B(CD))) = 5 x 15 x 6 +40 x 5x 6 + 30x 40x 6
450 + 1200 +7200
‘8850 operations.
® @@opy é
= 40x5x 15 +40x 15 x6 + 30x 40x6
“= 3000 + 3600 + 7200
cD
BCI
= {3,800 operations.
(i) (AB) (CD)
= 30x40 x 545% 15 x6 +30x5X6
= 7350 operations.
(wenn) :
= 40x5x 15 +30x 40x 15 + 30x 15 x6
= 3000 + 18000 + 2700
= 23700 operations.
(AB)ED)
= 30x 40x5+30K5 x 15 +30x 15 x6
6000 + 2250 + 2700
10950 operations..,
Warning araipncaacopviog otis Bouts» ERIMINAL Act Anyone found pully MLIABLE W tae LEGAL proecdingy
WwDESIGN AND ANALYSIS OF ALGORITH,
You are given a list of words, W,, Wo, Ws, +. Wy and their conga ing brobabitis
occurrence py, Pz, Ps; «1 Pr The problem is to arrange shone oe ny ean
tree in a way that minimizes the expected total access Ste Be a vn Igorithm
implement it. Also prove the complexity of the algorithm deri
(oR) Feb/March-22{
ob IMarch-22(R1
Explain about OBST. 1R18), a
(oR) (
Enumerate on optimal binary search trees. ) April-18(R18), a6
(OR)
Write an algorithm of Optimal Binary Search Trees.
(Refer Only Topic: Algorithm for Optimal Binary Search Tree)
Answer:
Given, Wy, Wa, ....Wq are the list of words with fixed probabilities Py, P2y.Py» To arrange, these word
in a binary search tree so that the access time gets minimized, the number of comparison required to access eae}
‘d+ 1 at depth. If W, isplaced at d; then minimize S P(1 + d)). Hence, the solution to the problem i
-A7IRA5), a7
element i
to G6nstruct an optimal binary search tree:
Optimal Binary Search Tree 2
__Let (a1, dx, ... dq} denote set of identifiers where a, < a< a5. Also, p(i) denote the probability by which
identifier “a? can be searched and q(i) denote the probability of element ‘x’ to be searched where a;-< x < a),
and 0 < i < n.In other words, p(i) denote the probability of successful search and q(i) denote the probability of
lunsucceessful search. A tree with minimum costis obtained by adding p(i) and.q(i) ie.,
A tree having optimum cost is known as optimal binary search tree.
Algorithm for Optimal Binary Search Tree
procedure OBST (P, Q, n)
begin
real P(n), Q(: n), C(O:
integer ROO: n,
ForiOton-1 do
begin / Initialize
WG.D, RG i), CG, iD) — (QO), 0, OWE, i+ 1), + 4
RULI+ 1, CG i+ DQG +QG HPA +1,
141, 0) 4Q0+1)+PG+ Dy j
1 0: n), WO: n, O: n); : i ‘
+ "17 Final optimal tee with m nodes Au)
end
“ (Wn, n), Rin, n), C(a, n)) — (Qn), 0, 0);
Form 2tondo * i fi
Foie 0n-mao j
jeitm: BS ;
WO.) Wj 47H) 4.94 . cS
K a value'of lin the range RG. j_ * fu»
i M Using Kouth's at Ds1sRG+1,)), that minimize (CG, 1-1) + CQ, j)};
Gi) — WG.) + CG, kK 1) 4 OM, 9: 5
RG) ks Xk, j);
end.
end ies
End OBST
aes “PSI PUBL
HERS AND DISTRIBUTORS PVE LOTComplexity
Bach Cli, j) ean be computed in time O(n). The total time forall CC, fis O(n ~ m).Om) = Cam ~ 2)
J Olnm = m2) = On")
uottg: Dynamic Programming 93,
‘Time
seen
SMA TH INA 44 In 9m Y= (n= E+
+ 2m 4 3notn? = (14224 Ft 2)
in? En?
Tay = MONONA Dnt DOH
+ ln)
L_ OAnm — m2) can also be simplified as,
isnin
= ndm-¥
= Mom + Dow _ mlm-+ DQ + 1 _m
z 6 3
Here, maximum frequency count is m?,
So, time complexity is O(m').
Q7. Use the function OBST to compute w(i,), r(iJ), and c(i,), 0 si
.
a on for: hapsack is,
Manly (MF, Mom). py Knapsack is,
* Somplete item should be
woke
?* item. As the n® item is
M) and so on upto F(M).
tis tried to be removed then the profit of
‘otal weight which belongs to the p
tem is reduced from the total profit and weight of
rofit and weight of (i~ 1)" item.
MP1P3Ps)
(0.0)} + {.2)}
(a2)
(0.0)}-+ {(1.2)}
= ((1.2K0.0)}
+ (Ps)
(0,0) 1.2)} + {(2.3)}
+3
[(0,0.(1.2)} + {(2.313.5)}
(0.0 1.2\(2.3)(3.5)} mer
SF (Pao)
(0,0)(1,2(2.3V3.5)} + 16.9)]
= (5,9)6.6)7.78.9))
Satis
(0\1.2)2.3)3.5)) ${CNCATTIEN)
= ((0.090,2)(2.3)3.515AN6.17. 7989]
Ye 1 end Si aaa w,) and othef has a pair (P;, wy) and P; < Py while w
one of S*-" and a * Ne
O "is discarded fs %)
He (0,0)4,2)2,3)6.)6.916. 97D : {
‘ js checked in order to obtain solution,
Afr applying purge rule, following condition
nA act Anyone found pats UABLE tice LEGAL procecdin
we then the pairoe
DESIGN AND ANALYSIS OF ALGORITHM
ye CC
TE(P,, w) € S* and (P;, wv) 5"
then,
Xn
Otherwise x,
(oes
(6.6) ¢ S*
wel
(6.6) - (5.4) = (12) € 5?
(6, 6) €S! + False
m=O
Gayest
(1.2) ¢ S° Tue
0
6 .
‘Maximum profitis 2(P;x3) ph Paxy + P3Xs
X142K045x1
+556 - i
In the above problem, (P;, w;) = (6, 6) that implies (P;, w;) ¢ S?, Here, condition becomes true 50 x3 =|
3*item is placed entirely in bag. To get next item to be placed in bag subtract (Ps, ws) from (6, 6) because w,
is placed in bag. If (1, 2) does not belongs to S*-! = $1 Here, condition becomes false so x3 = 0. No quantity of
second item ean be placed in bag. In order to get next item to be placed in bag, check (Py, wy) (1, 2), 1,2) €.9,
here condition becomes true so x =1.
Optimal solution is, (xy, 2,3) = (1, 0, 1)
4 ‘Maximum profit is £(P; x) = 6
~ O/I Knapsack Algorithm
Procedure DKP (p, w,n, M)
8° < ((0,0)}
fori 1ton—1do
Si'& ((P;, w) (P—p, W - w,) € Sand W, B, then
9. x,-0;
10, else si
11 mel;
end DKP nae
1:
2.
a
4,
5.
2) whiere w} is the largest w in any
x
‘Time complexity of knapsack problem is On)
“Time complexity of O/1 knapsack problem is O( 24
- QM, Define
z= merging and purging rules of 0/1 knapsack Problems.
from the pairs Sand S{ as follows,suppose S° =_{(0, 0)} and Sy
{(1,2)} then $= ((,0)) Ga.
)} = {(0, 0), (1, 2)}
poring Merging
Porging ule deals with the elimination of «tuple giving less profit while increasing more weight. Formally
jg rule can be described as follows,
Ifthe set contains two tuples (p17) and (py, wy) and if the condi
ten the pair (Pj W3) can be discarded because the
Py < pe and w)2 wy are satisfied,
(Pp. w9) gives more profit with no additional weight.
sample
Let = ((0, 0), (1, 2), (2,3), 3, 5), (4, 3), (5, 5), 6.6)
‘Then obviously, tuple (3, 5) can be discarded because of the purging rule.
G5) ang (43)
7) pa
Since, pj < Px and w; 2 wy Hence (3, 5) is discarded,
12, Solve the following 0/1 knapsack problem using dynamic programming
N= 3,(W1:WaWs) = (2,3,3),(P4.P2.Ps) = (1,2,4)andm=6 ‘Aug-22(R18), a5(a)
Answer = :
Given that, h
m =6,n=3
wipspsy = (1,2,4)
or p2383) = (2,3,3)
sins) = (4,3)
Solving this problem involves the following few steps,
(@ Addition Operation
SS + paw
{Merging Operation
Stas ys}
(ii) Purging Rule or Dominance Rule
Take any two sets $é and Sj consisting of pairs (pjv)) and (pj,w,), The purging rule states that ifp; wy is not satisfied. So, no discarding of the tuple wall be done.
In this ease also w
SI =S2 + pyey
= ((0,0)(1, 2)(2,3(3,5)) + ((4,3))
SP = ((4.315,5)(6,6)7,8))
S) =sht4s}
3 '
{(0,0)(1,2)(2,3)(3,5)} + {(4,3)(5,5)(6,6)(7,8)}
= ((0,0)(1,2)(2,3)(3, 514, 315, 5)(6,6)(7, 8)}
By using the purging rule, here the tuple (3,5) will be deleted. This is because 3 < 5 and 5 > 3.
#8? = { (0,0)(1,2) (2,3) (4,3) (5,5) (6.6) (7,8) }
. In addition to the above steps, the next and the final step is to find the optimal solution. This solution can te
} determined by performing searching process for knowing x, values based on the following conditions,
@ — epiwvy ES, then x, =0
Gi) HE Diy ES, then (paw = (p ~ pw — Wy) and.x, = 1
This searching process is done on the last tuple of set S
Since, in our problem, capacity of the Knapsect{
1m) is 6 , therefore, the last tuple is (6,6). ,
Now, implementing the above conditions on (6,6):(6,6) € S?, but (6,6) ¢ S?. Therefore, itcan be writes
8 (6 ~ py6 — v,)'Wy- This tuple is obtained from the pair where, p3 = 4,w3=3. Thus, (6,6) — (4,3) = 2,3)
Hence, x5 = 1.(2,3) € $? and does not belong to $2-!
(2,3) also does not belong to S!-! = 5°, which means x,
“Optimal solution = (1, 1,1}.
Q13. Solve the following 0/4 Knapsack Problem using dynamic
W2MaWe) =(10,15,6,9) and (PPaPsip4) = (2,5,8,1), i
Answer:
Programming n = 4,m=30,(¥
Augsep-21(R18), 0)
m=30
__ WhiMawsWva) = (10,15,6,9)
PiPspspy = (2,58,1) i
Solvig this problem invcves the fo ing fe
Aaa lowing few'steps,
S1=5' + yy
| Gi) Merging Operation
| - Stasity gf é
fe (ii) Purging Rule or Dominance Rule
| + © Take any two sets $/ and g
| Wj 2 Wp, then (PW) will be deleted, This
Gee is 4% SIA Pus82S 4 —vep
= (0.9) +(2.10)}
= {2.10}
seS4S)
5 =SUS?
= {(0.0)} v {(2.10)}
= (0.02.10)
Ste
= {0.02.10} + ((5.19)}
{(5.15),(7,23)}
Sus}
t = {0.0)\(2,10)} U {(5,15),(7.25)}
{€.0),(2.10),(5.15),(7,25)}
=S4@.
= {(0.0).2,10).(5.15),(7.25)) + (8.6)
{(8.6).(10.16),(13,21),15,31))
2S?
0.0).2,10)45,15),(7.25)} U {(8,6),(10,16),(13,21),(15,31)}
0.0).(2,10) (5,15),(7.25),(8,6),(10,16).(13,21).(15.31))
According to purging rule, the pairs (7,25),(5, 15),(2, 10) are discarded.
G25) and (8,6)
Pi Pie
SSP WDM,
7<8 5>6
Hence (7,25) can be discarded.
Similarly this condition is also applied for pairs (5, 15),(2, 10).
Hence, S* = {(0,0),(8,6).(10, 16),(13,21),(15,31)}
f= S+ pews
= {(0,0),8,6),(10,16),(13,21),05,31)) + {(1,9)}
(1,9).(9,15),(11,25),(14,30),(16,40)}
St
= {(0,0),(8,6),(10,16),(13,21),(15,31)} U {(1,9),9,15),(11,25),(14,30),(16,40)}
(0,0),(8,6),(10,16),(13,21),(15,31),(1,9),9,15),(11,25),(14,30),(16,40)}
‘After applying the purging rule, the pairs (10,16),(13,21) and (15,31) can be eliminated.
S*= {(0,0) (8,6), (1,9),(9,15),(11,25), (14,30), (16,40) }
Pow) € S* and
Pom) € 5" then
Jn= lotherwisex,
‘The maximum capacity, m = 30
(14,30) € s¢
(14,30) ¢ $?
uel
Now, subtract the profit and weight of fourth object i.e. p44) from (14,30).
(14,30) ~ (1,9) = (13,21)
(13,21) € 3?
(03,21) ¢ S?
wal
— ayr
DESIGN AND. ANALYSIS OF Ai ry
108 ) from (13,21)
Os
third abject i
Similar!
(13,21) = (8.6)
(5,15) €
(5.15) € St
Now, (5:15) = (5.18) = (0.0)
(00) € 8!
(oo ¢ 8°
a 40) that implies yarn #53. Here, condition becomes trues x.
ce next item to be placed in bag. subtract (Ps.W/4) from (14.30) bec.
condition become true $0 x3 = 1 ie., ws can be placed. Based on tie x,
m in bag, ie., (ai) = (0.0) that implies (p,m) © 515?
In the above problem, gr.) (
ced entirely in bag,
we ts placed in bag. As (13.21) € 9%,
condition, wis also placed. In onder to get next iten
ie, 4" item is pl
condition becomes false $0.4
+. The optimal solution is (0.1.14).
Q14. Solve the solution for 0/1 knapsack problem using dynamic programming:
(PuPaPssPa) = (11, 24, 34, 33) (4, W2,W3,W4) = (2, 11, 22, 15),M=40,n = 4
Feb March-228, ce
Answer :
Given that,
Py
Bee
‘The weights are considered in (1,2,4,3) order. Consider the weights of 1,24
=2411415=28
But given, M
Consider the weight of object 3 as,
(40 ~ 28)
= 054
Weight = 2415+ 11 + ((0.54)22]
40
11421 +054G1) +33
= 81.74
Solving this problem involves the following few steps,
@) Addition Operation
SES + pian,
Gi) Merging Operation
S=s4145)
Gil) Purging Rule o Dominance Rule
‘Take any two sets $and 5} consisting of pairs
Profit
(Pp) and.
wy 2 ws, then (Pp) will be deleted, This Pri
“/ ) his rule must be applied on the oe, come le sasha ifp m:
(0. O.(1L, 1221, 11432, 13,81, 22),(42, 24),(52, 33),(63, 35)(33, 15).(44, 17)65. 24).
(64, 37),(75,39)
Here. =
Start with the last pair in S*i.e., (75, 39)
39) S* but eS?
249.052, 49)(63,95)}
nan-1=4—
The obtained pair (42,24) € S° but ¢ S?
So, set x, =x3=1 ’
Updating the values as,
p=p-py=42-31=11 ;
24-22=2
3-152
(11, 2) € both S? and S!
So, set x, 0 ‘
Now, —1=2-1=1
Updating the values as,
p=p-py=11-11=0
wawawy=2-2
hit he problem sie sO so the process is terminat
0.1).
oe of the given Knapsack problem i
: a path Probie
Pes er Qiiiaiase
cortest path problem and write its algorithm using
ve cee Se Ota peo working with a numerical example.
www;
nen
ed. The optimal solution for the given problem is
8. Explai
dynamic programming. Prove ion)
ing.
, lem using dynamic programmi
Discuss about all pairs shortest Prove es
we (OR): DESIGN AND ANALYSIS OF. ALGOR
110
t path probl Hy
rs shortest pi roblem,
With the help of suitable example explain the all pair: nae
ve RI
(oR) :
Explain in detail about all pair shortest problem. Aor gy
(oR) ‘
Write an algorithm of all pairs shortest path problem.
(Refer Only Topic: Algorithn of All Pairs Shortest Path) Fen March 22) ogy
Answer:
All Pairs Shortest Path
‘The problem is to find a shoriest path from i to j for every pair of vertices j and fi.e., finding shortest pay
between all the pairs of the vertices in the graph. The weight of a path is measured by addi
constituent edge
ling the weights of,
‘The same problem can be solved by running a sin
number of vertices of the graph, But
algorithm. All pairs algorithm uses adj
graph,
igle source shortest path algorithm
all pairs shortest path algorithm is faster than si
jacency matrix representation rather than adjacen
7 times as n is tg
ingle source shortest pas
cy list representation oft
Assuming the vertices by 1,
Tepresents the edge weights ofthe graph,
ie, We,
* "fora directed graph G = (¥, £) and matrix Wyyy be the input which
0° itimj
Wan =4 The weight of drecied edge (i), if and (ek
0 ifi#jand iy) EE
‘Then all pais shortest path algorithm gives ot matrix D,
nen = (dij) \
‘Where, dy denotes the shortest path from vertex ito ver
If kis the inte
tex jin the graph G.
“mediate node between i and j, then the sub ath from j j
otherwise length between i and j would not be ‘minimum, pa from 10k and &toj stonld be one,
Being k a intermediate vettex wit highest index
through no vertex with index greater than k= |
‘makes the i k path a shortest Path from i to kin G, going
‘The path from k to jis similar as i to k path,
sate Using oa. 2 esa the length of a shortest path from ito j, 80ing through no ve; tex of index gretet
PGi) = min{ min (DVI, k) 4 DK, Dhcosti}
Clearly D°(id) = cost(ig) SiS n,1 in(Dy-s (oD De
ined after K iteration
(i,j) is checked by the
+d bD
113
Moreover, the &® column values of D remains
unchanged i.e., D(k, k) remains 0. Hence, it is not
necessary to save these values while updating D.
Algorithm.
Procedure FloydAlgo (L (n, 0)
Array D(n, n), Pn, n); Ln, 9)
Fori-1 ton do
DG,
Mnitialize matrix D to matrix LP (i,j) = Null:
Initialize matrix P to null to
J/obtain the shortest path.
Fork= 1 tondo
* Fori=1tondo
Forj=1tondo
if DG, k) + Dj) 1
A’ denotes the number of iterations to he taken.
These number of iterations depends on the number of
vertices. In this problem there are 6 vertices
AMAA) = tminfA HO, DAL IC +A TOD)
= min +0) =0
ANG2) = min(Al '(1QAT HD) + AP 29)
min(AM(1 2)AML1) + AML.2))
min(10,0 + 10) = 10
min(A'"(1,3)4'-M1,1) + Ab M13)
min(AXU 3)A%1) + A%™CL3))
= min(0,0 +0) =0
ANd)
AMA) = min(A Ha) AM 1) eal
= min(30,0 + 30) = 30
4))
ANG,S) = min(Al10,5)A''11) + A S))
min(45,0 + 45)
AN(1,6) = min(A'1(1,6)A'(1,1) #A'"1.6))
min(0,0 + 0) = 0
A'Q,1) = min(A!'(2.1) A121) + AHL)
‘min(10,10 + 0) = 10
ANQ,2)
min(A'1(2,2)A*-42,1) + AML2))
= min(0,(10 + 10) =0
= min(a 42,3)!
= min(50,(10 + 0)) = 10
D+a'0,3))
ANQ4) = min(Ah12,4),.A' QD) +A" 4)
= min(0,110 + 30) =0
AN2,5) = min(A'42,5)A'42,1) APS)
min(40,10 + 45)) = 40
A'(2,6) = min(A''2.6),A42,1) + AY 16))
= min(25,10 + 0)) = 10
AIGA) = min(A BDA + Al LD)
= min(0.0-4+ 0) =0
A1G,2) = min(A'"3,.9.A G1) 4AM H2))
= min(50,(0% 10) = 10
Chen aa er aaa
ERIMINAL act; Anyone found guilty ls LIABLE to face LEGAL proceedings. )min (A'-"(3,3).4!-1(3,1) + A'.3))
= min(0,0 +0) =0
min (A'"(3,4).4 13,1) 4 AAD)
nin(0,(0 + 30)) =0
in(A'Y3,5)AI(3,1) + AICS)
= min(35.(0 + 45)) = 35
AXGS)
A°G.6) = min(A"“13,6).4'-13,1) + A110)
= min(15,(0 +0) = 0
AX41)
min(A'4,.A'(41) A)
1) FAT(12))
2A
= min(0,(30 + 10) =
min(A''(4,3).A1(4,1) + AI(1,3))
= min(0,0 +0)
min(A'1(4,4).4°-104,1) + a™-,4))
min(0,G0 +30) =0
ANGS) = min(a'1(4,.5)4'14,1) + AIM
min(0,(30 + 45)) =0
min(A'-1(4,6).A!-1(4,1) + A'“\(1,6))
smin(20,(30 + 0)) = 20
min(41165,1)A™(5,1) +4111)
= min(45,(45 + 0) = 45
min(A™\(5,2),A-(5,1) +A'-(1,2))
= min(40,(45 + 10)) = 40
min(A'-"(5,3),A'-15,1) +4!-"(1,3))
min(35,(45 + 0)) =35
in(A*"(5,4),A-1(5,1) + A!-1(1,4))
‘min(0,(45 + 30)) =0
AMG5) = min(A“"(5,5)A'45,1) + AI-1(1,5))
= min(0,(45 + 45)) =0
AM5,6) = min(A'"(5,6),A'1(5,1) + A!-1(1,6))
= min(55,(45 + 0)) = 45
min(A?*(6,1)A!-16,1) + AI-I(1,1))
min(0,0 +0)=0
min(A!-16,2)A!-¥(6,1) + A'-\1,2))
min(25,(0 + 10) = 10
4'6,3) = min(A!-16,3)A'16,1) + A'-1(1,3))
= min(15,(0 + 0))
.5))
AM5.2)
AW53)
AMSA)
AMG) min(A'~"(6,4)A'"(6,1) + A!-W(1,4))
min(20,(0 + 30) = 20
4'(6,5) = min(A!1(6,5) A! (6.1) +A'1,5))
= min(55,(0 + 45) = 45
te SIA PUBLISHERS AND DISTRIBUTO|
> |
DESIGN AND ANALYSIS OF ALGORTi yg,
= min(0.(0 + 0)
AB © Db ey
afo o 0 3 4
nfw 0 © 0 » 4
clo w 0 ‘oH a 4
wep [oo 0 one
a] © 5, 0 0 o4y
FLO 1 0. 120m aecey
in(A?N1DAH1,2) + 42-19.)
min(0,(10 + 10))=0
min(A?(1,2)A7-\1,2) + A2-H2.9))
min(10,110 + 0) = 10
A%(1,3) = min(A?1(1,3)A2-"(1,2) + A213»)
min(0,(10 + 10)) =0
min(A?1(1,4),A?-\(1,2) +4240.)
min(30,(10 + 0)) = 10
A%(15) = min{A?1(1,5).A2-\(1,2) + A-1(2,5))
= min(45,(10 + 40)) = 45
Ax)
AX(1,6) = min(A?1(1,6),A21(1,2) +A212.6))
min(0,(10 + 10) =0
A%(2,1) = min(A?1(2,1),A2-1(2,2) + A1(2,1))
in(10,(0 + 10)) = 10
iin(A-\(2,2),A?-1(2,2) +:A2-4(2,2))
min(0,(0 + 0))=0
A72,3) = min(A-"(2,3),A2-12,2) + A2-12,3))
= min(10,0 + 10)) =10
A%(2,4) = min(A?-1(2,4),A2-1(2,2) + A2-1(2,4))
= min(0,0 + 0)) =0
A%2,2),
2,5) = min(A2-1(2,5),A2-12,2) + A215)
= min(40,(0 + 40)) = 40
min(A?-1(2,6),A-1(2,2) + A2-"(2,6))
= min(10,(0 + 10)) =10
A7(2,6)
A781
imin(A?1G3,1),42-1(3,2) + A212.)
min(0,(10 + 10)) =
2).
A%3,2) = min(A?"(3,2),42-1(3,2) + A?!
‘min(10,(10 + 0)) = 10
478.3) = min(A2-13,3)42-43,2) + A*12.3))
min(0,(10 + 10)) =0
A°G.A) = min(A2-13,4),42-1(3.2) 442124)
min(0,(10 + 0) =
RS PVT. LTD.pita: Dynamic Programming
423.5) = min(A2""@,5).A713.2) + A-1(2,5))
+ = min(35,(10 + 40)) = 35
min (A?-1(3,6),A?-1(3,2) + A?-"(2,6))
#8.) =
= min(0,(10 + 10)) =0
A%4,1) = min(A2"(4,1).A2-"4,2) + AP12,1))
= min(30,0 + 10) = 10
A%42) = min(A2-"(4,2),42-"(4,2) + A2-12,2))
= min(0,0 +0
4443) = min(A2“143)A2-1(42) + 42-42.)
: = min(0,(0 + 10)) =0
min(A-"(4,4)42-142) +4?40,4))
min(0,0+0))=0 _
Axa)
min (A?-1(4,5),A?“"(4,2) + A?-1(2,5))
‘min(0,(0 + 40)) =0
AMAS)
A(4,6) = min(A?-1(4,6),A2-(4,2) + A2“"(2,6))
= min(20,(0 + 10)) = 10
min(A?1(5,1),4?-Y5,2) + A712,1))
= min(45,(40.+ 10)) = 45
AX5,1)
445.2) = min(A21(5,2),4?-15,2) +A?-12,2))
= min(40,40 + 0)) = 40
min(A2“4(5,3),A?1(5,2) + A?12,3))
= min(35,(40 + 10)) = 35,
A%5,3)
(a*-1(5,4),A™15,2) +4424)
min(0,(40 + 0) =0
A%54)
(5,5) = min(A2-1(5,5).A2-"5,2) + A742,5))
min(0,(40 + 40) =
A%5,6) = min(A2-1(5,6),A2-15,2) + A2"2.6))
= min(45,(40 + 10) = 45
6,1) = min(A2-46,1).A?"62) + A712.)
= min(0,(10 + 10)) =
A%6,2) = mmin(A®-"62)A-162) +A712,2))
min(10,(10 + 0) = 10
min(42-1(6,3),A?-"(6,2) + At12,3))
= min(0,00 + 10))=0
4X63)
117
2-1(6,4),A2=(6,2) +A? 12,4)
min(20,(10 + 0)) = 10
Aa)
min(A2-(6,5)A?1(6,2) + A?-"(2,5))
= min(45,(10 + 40)) = 45
465)
6,6) = min(A?-"(6,6),A?"(6,2) + A?"(2.6))
= min(0,(10-+ 10) =0
A Bp CD E F
afo wm 0 0 4 ©
aim o 0 9 9 10
awec{o 0 0 0 35 @
pio 0 6 o W
elas 3 0 6 4
Lo w 0 ww 4 0
AML.) = min(AS-H,.A4,3) +42413,1))
= min(0,0 + 0) =0
A302) = min (A31(1,2),A-4(1,3) + AP 13,2)
= min(10,(0+ 10) = 10
AX(1,3) = min(A“"(1,3)A3-\(1,3) + A21G,3))
= min(0,0 +0) =
A%(1,4) = min(A?1(1,4).A7-"(1,3) +A"B.4))
= min(10,(0 + 0) =0
AX(L,5) = min(A31(1,5).4>-"(1,3) +4°"1G,5))
= min(45,(0 + 35) = 35
A%(1,6) = min(43-(1,6).A7-1(1,3) +431G,6))
1(2,3) + A5-"(3,1))
= min(10,(10 + 0)) = 10
A¥(2,2) = min(A?1(2,2),A?-2,3) + A-1(3,2)),
= min(0,(10 + 10) =0
,A%2,3) = min(4?-1(2,3).4?1@,3) + A3-1G,3))
= min(10,(10 +0) = 10
A324) = min(A3“"(2,4),A9-"(2,3) + A2-"(3,4))
= min(0,(10 + 0)) 0
30,8) = min(A212.5)A212,3) +4*1G,5))
= min(40,(10 + 35) = 35
A226) = min(A?-"2,6),A°-"(2,3) + 4313,6))
= min(10,(10 + 0) = 10
| Gen ya is oR ERMINAL Aron nd Hy LADLE es LEGAL proceeding)11s i
ABD = min GYAN AS +A GD)
= min(0,0 + 0) =0
AS13,3) 4A "3.2))
0
ANG.2) = min(A |
= min(100 + 10)
AX33) = min(A®
= min(O.0-+0
AXA) = min(A IQ,
AX3) =
= min(35,(0 + 35)) = 35
A5G.6) = min(
= min(0,0 +0
ANG) = min(A14,1),4""(4.3) + A1G,))
= min(10,0 +0) =0
AN) = min(a1(4.2)A-44,3) + A-13,2))
= min(0.0 + 10) =0
A543) = min(A1(4) Da +40),
= min(0,0 + 0) =
A%(4,4) = min(4*""(4,4),43-1(4,3),4 A 13,4))
= min(0,(0 + 0)) =0
AX4S) = min(A*1(4,5).42-"(4,3) + A3-1G,5))
= min(0,0 + 35) =0
AX46) = Re Gonna aii
= min(10,0 + 0)) =
ANS 1) = min(A2"5,1),4-15, 3) +A313,1))
= min(45,(35 + 0)) = 35
A%5,2) = min(A“"5,2),4-(5, 3) +A313,2))
<= min(40,(35 + 10)) = 40.
ANS) = min(A™5,3)A71G5,3) 4 49-1'3))
= min(35,35 + 0)) =35
(4°15,4),42-165,3) + A3-1 13,4))
min(0,35 +0) =0
A%5,4)
AGS) = min(APY5,5) A315, 3) +A*“1(3,5))
min(0,G5 +35)
(A*15,6).A
min(45,G5 +0) =35
(A216, Dat 3)+41G,1))
min(0,(0 + 0) =
45.6) =
3) AN '3.6)
AG)
| AMG.) mal™n4™46 44a)
= min(10,0 + 10)) = 10
DESIGN AND ANALYSIS OF ALGORITHNg
min(0.0 + 0)) =0
min(A*"(6,4),A3"(6,3) 4 43 84
n(10,(0 + 0)) = 0
n(A'H6.91A™"68) +A
min(45,(0 + 35)) = 35
n(A3""(6,6),A>~ '63)+4™Ia9)
(0,(0 +0) =0
)
))
Be Det Rep
afo>, 0 o- 35
Bl] 0 W- 0 35 Wg
wc] o 0 0 a5 4g
Df 0% 9 t9 Mg 84! GEG
BE fas 4 35 0 0 is
# LO fi) 30 0 seg
AXAD) = min(A10,),A",4) +414,1))
in(0,(0 + 0)) = 0
iin(A4“1(1,2),A*-1(1,4) +ASN(42))
in(10,(0 + 0)) = 0
in(A*11,3),A41(1,4) 4 A43))
in(0,(0 +.0)) =0
AM(14) = min(A-\1,4),a-1C, 4) +Ah144)
= min(0,0 +0) =0
AM1,2)
AM(L3)
A4(15) = min(a“1(1,5) 4 14) +1045)
= min(35,(0+ 0) =0
ANL6) = min(a-"(1,6)4*10,4) + a*-446)
= min(0,(0.4 0) =0
A°Q) = mina.) 410.4) 44°14.)
= min(10,0 + 0) =0
= min(A‘12,2)44-1¢9.4) 4 4-1¢4))
= min(0,(0 + 0))
min(A*1(2,3),4-102,4) 4 44-1(4,3))
= min(10,(0.4. 0)) <9
AGA) = seo 2,4) ¢A144))
= min(0,(0 + 0)) =
inf ate syae oe 4) +a "s x
= min(35,0 + 0)) =
min(A*\2,6),44- 4) rata o)
min(10,(0 +0)) =
A4Q,2)
AM2.3) =
42,5)
in(AM13,1) 442 “a 4) + AtH4,1))
= min(0,04 0)
rmin(4 #13, aa
= min(1o00 4 0
(3.4) +A*-"(4,2))
=0