Daa R20 Unit 3
Daa R20 Unit 3
Daa R20 Unit 3
Principle of Optimality
An optimal sequence of decisions has the property that whatever the initial
state and decisions are, the remaining decisions must constitute an optimal
decision sequence with regard to the state resulting from the first decision.
In other words, principle of optimality is satisfied when an optimal solution is found
for a problem then optimal solutions are also found for its sub-problems also.
General Method
The Idea of Developing a DP Algorithm
Let G=(V,E) be a directed graph with n vertices.The cost of the vertices are
represented by an adjacency matrix such that 1≤i≤n and 1≤j≤n
cost(i, i) = 0,
cost(i, j) is the length of the edge <i, j> if <i, j> E and
The graph allows edges with negative cost value, but negative valued cycles are not
allowed. All pairs shortest path problem is to find a matrix A such that A[i][j] is
the length of the shortest path from i to j.
Consider a shortest path from i to j, i≠j. The path originates at i goes through
possibly many vertices and terminates at j. Assume that there is no cycles. If there
is a cycle we can remove
them without increase in the cost because there is no cycle with negative cost.
Algorithm makes n passes over A. Let A0 , A1 , .. An represent the matrix on each pass.
Let Ak-1 [i,j] represent the smallest path from i to j passing through no
intermediate vertex greater than k- 1. This will be the result after k- 1 iterations.
Hence kth iteration explores whether k lies on optimal path. A shortest path from i
to j passes through no vertex greater thank, either it goes through kor does not.
If it does not, then no intermediate vertex has index greater thank- 1. Then Ak [i,j] = Ak-
1
[i,j]
Ak [i,j] = min{ Ak-1 [i,j], Ak-1 [i,k] + Ak-1 [k,j]}, k>=1 (A0 [i,j]= cost(i, j))
Algorithm
Void All Paths ( float cost[ ][ ], float A[ ][ ], int n)
A[i][j] = cost[i][j];
The 0/1 knapsack problem is similar to the knapsack problem as in the Greedy
method except that the xi’s are restricted to have a value of either 0 or 1.
Maximize ∑ pi xi
The decisions on the xi are made in the order xn,xn- 1 …x1. Following a decision on xn,
we may be in one of two possible states the capacity remaining in the knapsack is
m and no profit has accrued or the capacity remaining is m-wn and profit of pn has
accrued). It is clear that the remaining decisions xn-1 ,…..,x1 must be optimal w.r.t
the problem state resulting from the decision xn. Otherwise xn,…..x1 will not be
optimal.
f n(m)=max{fn-1(m),fn-1(m-wn)+pn }
in general,
f i(y)=max{fn-1(y),fn-1(m-wi)+pi }
Si+1 may obtained by merging Si and S1 i. This merge corresponding to taking the
make of two functions fi-1(x) and fi-1(x-wi)+pi in the object function of 0/1
Knapsack problem
So, if one of Si-1 and S1i has a pair (pj, wj) and the other has a pair (pk,wk) and pj ≤
pkwhile wj≥wk. Then the pair (pj ,wj) is discarded this rule is called purging or
dominance rule when generating Si all the pairs (p,w)with w>m may also be purged
If we try to remove ith item then profit of ith item is reduced from total profit and
weight of ith item is reduced from total weight which belongs to the profit and weight
of (i- 1)th item
Initially westart with S0= {0, 0} i.e profit =0 & weight=0 (bag is empty)
Addition
Take any two sets Si and Si 1 consisting of pairs(Pj , Wj) and (Pk ,Wk). The purging
rule states that if Pj≤ Pk and Wj ≥ Wk then (Pj, Wj) will be deleted. In other words,
remove those pairs
that achieve less profit using up more weight.
In other words, the given profit, weight pairs are: (1,2) (2,3) and (4,3)
Addition:
Merging operation:
Si = Si-1 + Si-1 1
S12 = {(2,3),(3,5)}
S2 = S1 U S12 = {(0,0)(1,2)}U{(2,3),(3,5)}
S2 = {(0,0),(1,2),(2,3),(3,5)}
= {(0,0),(1,2),(2,3),(3,5)}+{(4,3)}
S13 = {(4,3),(5,5),(6,6),(7,8)}
= {(0,0),(1,2),(2,3),(3,5)}U{(4,3),5,5),(6,6),(7,8)}
={(0,0),(1,2),(2,3),(3,5),(4,3),(5,5),(6,6),(7,8)}
Tuple (7,8) is Discarded because the weight (8) exceeds max capacity of Knapsack
m=6 so, S3 ={(0,0),(1,2),(2,3),(3,5),(4,3),(5,5),(6,6)
This purging rule has to be applied for every Si. In this example, no purging was
necessary in S1 and S2.
S3 = {(0,0),(1,2),(2,3),(4,3),(5,5),(6,6)}
S3 = {(0,0),(1,2),(4,3),(5,5),(6,6)}
The searching process is done starting with the last tuple (P1, W1) of Sn. A set of
0/1 values
‘
for xi s such that ∑ pi xi=P1 and ∑ wi xi= W 1 . This can be determined by carrying
out a search through thes i s.
If (P1 ,W1) Sn- 1 , we can set xn=0. Otherwise (if (P1, W1) not in S n- 1), we set Xn
to 1. Then, we will continue this search recursively through all Sn- 1, Sn-2, …., S0 with
either (P1, W1) in the case xn = 0 or (P1-pn,W1-wn) in the case xn=1.
In the above example, last tuple in S3 is (6,6) but (6, 6) S2. Hence x3=1.
Therefore the optimal sol is {x1=0,x2=1,x3=1} meaning weight 2 and weight 3 are
taken and weight 1 is ignored.
APPLICATION:
1. Suppose we have to route a postal van to pick up mail from the mail boxes
located at ‘n’ different sites.
2. An n+1 vertex graph can be used to represent the situation.
3. One vertex represents the post office from which the postal van starts and
return.
4. Edge <i,j> is assigned a cost equal to the distance from site ‘i’ to site ‘j’.
5. The route taken by the postal van is a tour and we are finding a tour of
minimum length.
6. Every tour consists of an edge <1,k> for some k = V- {} and a path from
vertex k to vertex 1.
7. The path from vertex k to vertex 1 goes through each vertex in V- {1,k} exactly
once.
8. the function which is used to find the
path is g(1,V- {1}) = min{ cij + g(j,S- {j})}
1. Find g(i,Φ) =ci1 , 1≤ i <n, hence we can use equation(2) to obtain g(i,s) for all s
to size 1.
2. That we have to start with s=1,(ie) there will be only one vertex in set ‘s’.
3. Then s=2, and we have to proceed until |s| <n- 1.
4. for example consider the graph.
Fromeq g(i,s) =min{cij +g(j,s- {j}) we obtain g(i,S) for all S of size 1,2…
When |s|<n- 1, the values of I and S for which g(i,S) is needed such that
i 1, 1 sand i s.
|s| =0
g(1,Φ) = c11 => 0; g(2,Φ) = c21 => 5; g(3,Φ) = c31 => 6; g(4,Φ) = c41 => 8
|s| =1
i =2 to 4
i 1, 1 sand i s.
g(2,{3,4}) = min{c23+g(3{4}),c24+g(4,{3})}
g(3,{2,4}) =min{c32+g(2{4}),c34+g(4,{2})}
= min{13+18,12+13} = min{31,25} = 25
g(4,{2,3}) = min{c42+g(2{3}),c43+g(3,{2})}
|s| = 3
optimal cost is 35
if the following are Two Binary Search Trees (B S T ) For given set S. here
assumption is each word has same probability no unsuccessful search
fo
for
do
d
whil
if whil
int
if
(a) (b)
We require 4 comparisons to find out an identifier in worst case in fig(a).But in fig (b)
only 3 comparisons on avg the two trees need 12/5 ,11/5 comparisons
In General, we can consider different words with different frequencies (probabilities)
and un successful searches also
Let Given set of words are {a1 , a2, a3, …….an } With a1< a2 <….. an
∑p(i) + ∑q(i) =1
1≤i≤n 0≤i≤n
To obtain a cost function for BST add external nodes in the place of every
empty sub true. These nodes called external nodes.
for
for
do int
do wh
int
if wh
if
If BST represents identi ers then there will be exately ‘n ‘ internal nodes and n+
1
external nodes every internal node represent a point where successful search may
terminate and every external node represents a point where an unsuccessful
search may terminates
For unsuccessful search terminate at external node the words which are not in BST
can be
partioned into n+1 Equalnce classes. Equvalence class E0 represents least among all
the
identifers in BST& The equalence classes ‘En ’ represents greatest among all
Elements in BST Hence search terment at level Ei- 1
1≤i≤n 0≤i≤n
Let P(i)=q(i)=1/7, for all i in the BST The following word set(a1,a2 ,a3)= (do,it,while)
Whil do
if
do Whil if
if
Whil
do
(a (b) (c
) )
d
While
do Whil
i
if
(d) (e
)
= 13/7
Cost (c) =cost(d)=cost(e)=15/7
Tree b is optimal
cost(Tree a)=2.05
cost (Tree c)= 1*0.5 + 2*0.1 + +3*0.05+3*0.15+3*0.11+2*0.05 +1 * 0.05
=1.75
ak a
a1 a3
E0…….Ek-1 Ek ………..En
1≤i≤k- 1 0≤i≤k- 1
l=i+1
1≤k≤n
In General,
i≤k≤j
The above Eq can be solved for c (0,n) by first computing all c(i,j) such that j- 1 = 1
Next we compute all c(i,j) of such that j-i=2 thenc(i,j) with j-i=3 and so on.we
record all the values of r(i,j) of each tree then optimal BST can be computed from
these r (i,j)
w(i,i) = q(i
)
r(i,i) = 0 for all 0≤i<4
COST 16 17 9 18 15 7 5 7 4 2 5 0
DISTANCE 2/3 7 6 8 8 10 10 10 12 12 12 12