DAA U-4 Dynamic Programming

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

Dynamic Programming(DP)

Dynamic Programming is a design principle which is used to solve problems with


overlapping sub problems

Ø It is used when the solution to a problem can be viewed as the result of a sequence of
decisions. It avoid duplicate calculation in many cases by keeping a table of known
results and fills up as sub instances are solved.

Ø In DynamicProgramming We usually start with the smallest and hence the simplest
sub- instances.by combining their solutions, we obtain the answers to sub-instances of
increasing size, until finally we arrive at the solution of the original instances.

Ø It follows a bottom-up technique by which it start with smaller and hence simplest sub
instances. Combine their solutions to get answer to sub instances of bigger size until we
arrive at solution for original instance.

How DP differ from Greedy

Ø Dynamic programming differs from Greedy method because Greedy method makes
only one decision sequence but dynamic programming makes more than one decision
However, sequences containing sub-optimal sub-sequences can not be optimal and so
will not be generated.

Ø Divide and conquer is a top-down method.

Ø When a problem is solved by divide and conquer, we immediately attack the complete
instance, which we then divide into smaller and smaller sub-instances as the algorithm
progresses.

Ø The difference between Dynamic Programming and Divide and Conquer is that the sub
problems in Divide and Conquer are considered to be disjoint and distinct where as in
Dynamic Programming they are overlapping.

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.

Ch Murty
Ø i.e 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
1. The structure of the solution to be charactirized.
2. Representing the optimal solution wherever needed in the sub-problem(defining
recursively)
3. The optimal solution has to be costructed from the information
4. The final solution has to be costructed from the information

All pairs Shortest Path


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

cost(i, j) = ∞ if <i, j> E.

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.

Initially we set A[i][j] = c[i][j].

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 than k, either it goes through k or does not.

If it does, Ak[i,j] = Ak-1[i,k] + Ak-1[k,j]

If it does not, then no intermediate vertex has index greater than k-1. Then Ak[i,j] = Ak-1[i,j]
Combining these two conditions we get

Ak[i,j] = min{ Ak-1[i,j], Ak-1[i,k] + Ak-1[k,j]}, k>=1 (A0[i,j]= cost(i, j))

Consider the directed graph given below.

Cost adjacency matrix for the graph is as given below

Copy the cost values to the matrix A. So we have A0 as

Matrix A after each iteration is as given below.

Recurrence relation is:

(i,j) = min { (i,j), (i,k) + (k,j) } , k>=1

Algorithm

Void AllPaths ( float cost[ ][ ], float A[ ][ ], int n)

// cost[1: n, 1:n] is the cost adjacency matrix of a graph with


// n vertices; A[i, j] is the cost of a shortest path from vertex
// i to vertex j. cost[i, i] = 0.0, for 1≤ i≤n

for (int i=1; i<=n; i++)

for (int j=1; j<=n; j++)

A[i][j] = cost[i][j];

for (int k=1; k<=n; k++)

for (i=1; i<=n; i++)

for (j=1; j<=n; j++)

A[i][j] = min{A[i][j], A[i][k] + A[k][j]);

The above is the algorithm to compute lengths of shortest paths.

Time complexity of the algorithm is O(n3).

TRAVELLING SALESMAN PROBLEM


Ø Let G = (V,E) be a directed graph with edge cost 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.

Ø The traveling salesman problem is to find a tour of minimum cost.


Ø A tour of G is a directed simple cycle that includes every vertex in V.
Ø The cost of the tour is the sum of cost of the edges on the tour.
Ø The tour is the shortest path that starts and ends at the same vertex i.e 1.
Ø We know that the tour of the simple graph starts and ends at vertex 1. Every tour
consists of an edge <i, k> for some k ∈ V-{1} and a path from k to 1. Path from k to
1 goes through each vertex in V- {1, k} exactly once. If tour is optimal, path from k
to 1 muat be shortest k to 1 path going through all vertices in V-{1, k}. Hence the
principle of optimality holds.
Let g(i, S) be length of shortest path starting at i, going through all vertices in S
ending at 1. Function g (1, V-{1}) is the length of optimal salesman tour. From the
principle of optimality
Use eq. (1) to get g (i, S) for |S| =1. Then find g(i, S) with |S|=2 and so on.

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})}

9. g(i,s) be the length of a shortest path starting at vertex i, going


through all vertices in S,and terminating at vertex 1.

10. the function g(1,v-{1}) is the length of an optimal tour.

STEPS TO FIND THE PATH:

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.
G (i,s) set of nodes/vertex have to visited.

starting position

g(i,s) =min{cij +g(j,s-{j})

s = 0.

i =1 to n.

g(1,Φ) = c11 => 0

g(2,Φ) = c21 => 5

g(3,Φ) = c31 => 6

g(4,Φ) = c41 => 8

s =1

i =2 to 4

g(2,{3}) = c23 + g(3,Φ)

= 9+6 =15

g(2,{4}) = c24 + g(4,Φ)

= 10+8 =18

g(3,{2}) = c32 + g(2,Φ)

= 13+5 =18

g(3,{4}) = c34 + g(4,Φ)

= 12+8 =20

g(4,{2}) = c42 + g(2,Φ)

= 8+5 =13
g(4,{3}) = c43 + g(3,Φ)

= 9+6 =15

s =2

i ≠ 1, 1∈ s and i ∈ s.

g(2,{3,4}) = min{c23+g(3{4}),c24+g(4,{3})}

min{9+20,10+15}

min{29,25}

=25

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})}

min{8+15,9+18}

min{23,27}

=23

s = 3

g(1,{2,3,4})=min{c12+g(2{3,4}),c13+g(3,{2,4}),c14+g(4,{2,3})}

min{10+25,15+25,20+23}

min{35,35,43}

=35

optimal cost is 35

the shortest path is,

g(1,{2,3,4}) = c12 + g(2,{3,4}) => 1->2


g(2,{3,4}) = c24 + g(4,{3}) => 1->2->4

g(4,{3}) = c43 + g(3{Φ}) => 1->2->4->3->1

so, the optimal tour is 1 à 2 à 4 à3 à 1

0/1 KNAPSACK
The 0/1 knapsack problem is similar to the knapsack problem as in the Greedy method
expect that the xi’s are restricted to have a values either 0 or 1( the decisions on me 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) let fi(y) be the value of optimal solution to
KNAP (i,j,m) we can represent the 0/1 knapsack problem as

∑ pi xi should be max subject to one l≤i≤n

cond that ∑ wi xi ≤ m(capacity) and xi=0 or 1 where l≤i≤j

The 0/1 knapsack problem is KNAP( l,n,m) The principal of optimality holds in the
solution of 0/1 knapsack problem in dynamic programming approach The dynamic
programming technique can also be used to represent the solution starts in tuples .Each tuple
contains member (pi,wi)a where pi is the profit earned on object i and wi is the weight of me
object i, one solution to the knapsack problem can be obtained by making sequence of
decisions on one var x1,x2 ….xn.

A decision on variable xi involves deciding which of values 0 or 1 is to be assigned to it. If it


is assumed mat the decision on xn one of me two passable status is arrived at

The capacity of knapsack remains the same and no profit is gained or capacity of knapsack
remains M-wn and a profit of pn is occurred .It is clear that the remain decision must be
optimal w.r.t problem state resulting from the decision on xn….x1 will not be optimal

The following is the recurrenced relation

S1 i= {(P, W)/ (P-Pi, W-Wi) € Si}

Where Si is the set of all pairs for fi including (0,0) and fi is completely defined by the pairs
(pi,wi)

Si may obtained by mergeing 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≤pk white
wj≤wk. Then the pair (pj ,wj) is discarded this rule is called purging or dominance rule when
generating Si all me pairs (p,w)with w>m may also be purged
Si 1 = {(pi , wi) / (p-pi , w-wi)€ Si }

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

S00= {0, 0}

Addition Si 1 = Si-1 + (Pi, Wi)

Merging or union Si = Si-1 U S1i

Purging (Dominance) Rule

Take any 2 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.

Finding optimal Solution

To find optimal sol by performing searching process for knowing values based on.

(i) if (Pi ,Wi) € Si-1 then Xn=0


(ii) if (Pi ,Wi) does not € Si-1 then (Pi ,Wi)= (P-Pn),(W-
Wn) and Xn=1

The searching process is done on last tuple of Si

problem : m=6 n=3 (W1,W2,W3)= (2,3,3), and (P1,P2,P3)=(1,2,4)

Solution: Initially take S00 ={0,0}

From the given data (P1, W1) = (1,2)

(P2, W2) = (2,3)

(P3,W3) = (4,3)

Addition :

S1 1 = S0 + {P1, W1)

S1 1 = {(0,0)} + {(1,2)}

S1 1 = {(1,2)}

Merging operation :

S1 = Si-1 + S1 1

S1 = S0 + S1 1 there fore S1 = {(0,0)U{(1,2)} = {(0,0) (1,2)}


Applying purging rule: S0, S1 1

(Pj,Wj) = (0,0); (Pk,Wk) = (1,2)

Here 0≤1 but 0≥2-------------- false

no deletion of tuple

S12 = S1 + (P2 , W2)

= {(0,0),(1,2)}+{2,3)}

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)}

No deletion

S13 = S2 + (P3, W3)

= {(0,0),(1,2),(2,3),(3,5)}+{(4,3)}

S1 3 = {(4,3),(5,5),(6,6),(7,8)}

S3 = S3-1 U S13 = S12 U S13

= {(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 exceeds max capacity of Knapsack

So. S3 ={(0,0),(1,2),(2,3),(3,5),(4,3),(5,5),(6,6)

(3, 5) will be deleted because 3≤5 and 5≥3

S3 = {(0,0),(1,2),(2,3),(4,3),(5,5),(6,6)}

(2,3) will be deleted because 2≤4 and 3≥3.

S3 = {(0,0),(1,2),(4,3),(5,5),(6,6)}

To find optimal sol

The searching process is done on last tuple of si


in our problem capacity of Knapsack is 6 so the last tuple (6,6) Implements above condition
on(6,6)

(6, 6) є S3 but (6, 6) does not € S2

So it can be written as (6-pn, 6-wn)

(6, 6) - (4,3)=(2,3)

Hence x3=1

(2, 3) € S2

(2,3) does not € S1 So x2=1

(2,3) does not € S0 which means x1=0

(2,3) - (2,3)=(0,0)

(0,0)єS1 and (0,0)єS0

∑pixi give Max profit. which means x1=0

Therefore the optimal sol is {0,1,1}

Informal knapsack algorithm


1. Algorithm DKP(p, w, n, m)
2. {
3. S0 := {0,0)};
4. for i :=1to n-1 do
5. {
6. Si-1 :={(P,W)(P-pi,W-wi)€ Si-1 and W≤m};
7. Si :=Marge Purge(Si-1, Si-11);
8. }
9. (PX,WX) :=last pair in Sn-1
10. (PY,WY) :=(P’ +pn,W’ +wn) where W’ is the largest W in
11. any pair in Sn-1 such that W+ wn≤m;
12. //Trace back for xn,xn-1….,x1.
13. if(PX > PY) then xn :=0;
14. else xn :=1;
15. Trace Back For(xn-1,…..x1);
16. }
Matrix Chain Multiplication

For the multiplication of ‘n’matrices a series of operations are to be performed.


A dynamic programming approach gives optimal sequence or order of
operations for this problem.

Here we have to multiply ‘n’ matrices A1, A2, A3……An, which can be
accomplished by a series of matrix multiplication.

A1*A2*A3*……*An,

We cannot change the above order of multiplication since matrix


multiplication is associative but we can parenthesize the above multiplication.

For ex,

Consider 3 matrices

A5x4, B4X3, C3x5

Multiplication cost of

((AB)C)=(5x4x3)+(5x3x5)=135.

Multiplication cost of

(A(BC))=(4x3x5)+(5x4x5)=160.

Therefore, we can see from above that different evalution sequence implies
different cost of opreation.

The problem is to determine the parenthesizing of the exp defining


A1xA2xA3x….xAn that minimizes the total no of scalar multiplications
performed.

As we cannot change the order of multiplication, we parenthesize the exp to


achive optimum order of multiplying ‘n’marices.

Let Ai-j denote the multiplication of matrices through i to j and the order of
Ai-j is ri-1 x rj.
We make a no of decitions, our job is to break the problem into several no of
sub-problem of same kind as the original problem and start the parenthasizing
the subsequences as the situation needs. Let us consider the highest level of

Parenthasizing where two matrix multiplications are considered

i.e A1..n=A1..k ,Ak+1..n

How we can split the order of matrices? And how can we parenthasizing the

Subsequences A1..k and Ak+1..n.

Let Nij denotes the minimum no of multiplications needed to compute Ai..j


where 1≤i≤j≤n.

If the sequence contains only one matrix i.e if i=j then

the cost of operation os zero. i.e N[i,j]=0

if i<j then the multiplicaton for Ai..j is computed by considering each k.

Ai..j= A1..k,Ak+1..j where i k j.

N[i,k] and N[k+1,j] are the optimum multiplication needed to compute Ai..k and
Ak+1…j

Since ri-1 * rk is the order of Ai..k and rk * rj is the order of Ak+1..j, the total no of
operations needed to multiply Ai..k and Ak+1..j is ri-1*rk.rj

N[i,j] can be computed as

N[i,j]=min N[i,k]+N[k+1,j]+ri-1,rk,rj where i<j and i≤ k<j.

Algorithem MATRIXMUL(A)

Begin

for iß1 to n do

N[i,j]ß0// if i=j i.e only one matrix

for bß2 to n do
Jßi+b-1

N[i,j]ß

for kßI to j-1 do

N[i.j]ß Min[N[i,k]+N[k+1,j]+ri-1,rk,rj]

end

end

end

end MATRIXMUL

ALGORITHM:

Procedure cmatrix(n,d[0..n])

For s=0 to n-1 do

if(s= 0)

for I=1 to n do

m(i,j) =0

if(s==1)

for i=1 to n-1 do

m(i,i+1) =d(i-1)*d(i)*d(i+1)

else

m=∝

for i=1 to n-s do


for k=i to i+s do

if (min>[m(i,k) +m(k+1,i+s)+d(i-1)*d(k)*d(i+s)])

min=[m(i,k) +m(k+1,i+s)+d(i-1)*d(k)*d(i+s)]

m(i,i+s) =min

Reliability Design

The reliability design is to design a system that is composed of several devices connected in
series

D1 D2 D3 Dn

Let ri be the reliability of device Di (i.e ri is the probability that device will work properly)
then

The reliability of the entire system is ∏ ri . even the individual device are very reliable (i.e ri
are very close to one) the reliability of the system may not be very good.

For Ex : if n=10 and ri =0.99 1<=i<=10 then ∏ ri =0.904 Hence it is desirable to maintain
duplicate devices and multiple copies of the same device type are connected in parallel
through the use of switching Circuits

D1 D2 D3 Dn

D1 D3 Dn

D1 D2 D3

D3 Dn
If stage c contain mi copies of device Di from the probability that all mi have a malfunction
is (1- ri)mi

Let the reliability of stage i is given by a function ∏ Φi (mi ),1<=n

Then, reliability of the system of n stages is ∏ Φi (mi )

1<=i<=n

Our goal is to use device duplication to maximize reliability under a cost constraint

let Ci be the cost of each unit of device i and let C be the max allowable cost of the system
being designed

The problem can be represented as

Maximize ∏ Φi (mi )

1<=i<=n

Under ∑ cimi <=c

mi >=1 and int 1<=i<=n

the dynamic programming solution can be obtained in the following manner

1) For each ci >0 each mi must be in the range 1<= mi <ui where

ui =[(c+ci -∑n j=1 cj )/ci ]

which is an upper bound (i.e max no of copies)

2) An optional solution m1 , m2 ,m3 …….mn is the result of a sequence of


decisions for each mi
3) Let Si consists of tuples in the form of (f,x) where f=fi (x) there is at most one
tuple for each different x that results from a sequence of decisions on m1 , m2
,m3 …….mn

The dominance rule (f1,x1 ) dominanates(f2,x2) iff f1>=f2 and x1<=x2 holds.then
the dominated tuples can be discarded from Si .
We begin with S0 ={(1,0)} we can obtain Si form Si-1 by trying all possible
values for mi and combining the resulting tuples together.

problem

Let the costs of a 3 stage system with device types D1,D2 and D3 then costs are
Rs30, Rs15, Rs 20 respectively.the cost of the system is to be done no more than
$105 the reliability of each device is 0.9,0.8,0.5 respectivly

Sol :

The given data is


Total cost C=105,r1=0.9, r2=0.8, r3=0.5 c1=30,c2=15,c3=20

Step1 : calculate ui(upper bound for no.of copies)

ui =[(c+ci -∑n j=1 cj )/ci ]

u1 =[(105+30-65)/30]
=2.3
=2
u2 =[(105+15-65)/15]
=3.6
=3
U3=[(105+20-65)/20]
=3

Step2:

S0 ={(1,0)}

Φ1m1 =1-(1-r1)1=(1-(1-0.9)=0.9

S11={(0.9,30)} and S21 {(0.99,60)}


(stage 1 consists of 1 copy) (stage 2 consists of 1 copies)

S1= S11 U S21


={(0.9,30),(0.99,60)}

We can not calculate S13 since Upper bound is 2 i.e u1 =2


S2 1 = {(0.72,45) , (0.792,7.5)} ø2(m2)=1-(1-0.8)=1-0.2=0.8

( (0.9 & 0.8,30+15) , (0.99 & 0.8,60+15))

S2 2 = {(0.9 ,0.96, 30+2*15) , (0.9504,60+2*15)

r = (1-0.8)2 0.99*0.96

= 0.96

={(0.864,60) , (0.95,90)}

discarded

(0.95 , 90) has been eliminated because mis leaves only Rs 10 .This is not to allow
m3=1

S2 3 = {(0.992 * 0.9 , 30+3*15) , (0.992*0.99,60+3*15)}

r = (1- (1-0.8)3

= 1 – (0.2)3

= 1 – 0.008 = 0.992

= {(0.892,75) , (0.982 ,105)

105 reached but D3 is not considered until now so for consideration discard it

S2 = { (0.72 , 45) , (0.792 ,75) , (0.864 , 60) , (0.892 , 75)}

By Dominance Rule

S3 1 = { (0.36 , 65) , (0.432,80) , 0.4464,9.5)}

( (0.72 * 0.5,45+20) , (0.864*0.5 ,60+20) , (0.892 * 0.5 , 75+20))

S2 3 = {(0.54 , 45 +20 *2) , (0.645,100) (0.669,115)

(0.86 * 0.75 , 60+20*2)

r = (1-(1-0.5 )2

= 1 - 0.25

= 0.75
S2 3 = {0 .54 , 85) , (0.648,100)

S3 3 = {0.875*0.75 , 45+3*20) , (0.75 ,120) , (0.78,135)}

=1 – (1-0.5)3

= 1- 0.125

=0.875

S3 3 = (0.63,105)

S3 = S31 U S23 U S33

= { (0.36,65) , (0.43,80) , (0.446,95) , (0.54,85) , (0.645,100) , (0.63,105)

S3 = { (0.36,65) , (0.432,80) , (0.54 ,85) , (0.648,100)

By consider me type with max reliability & min cost following the method of back
tracking we can datamin me total no of individual copies

In me above Example (0.648,100) shows the max reliability (0.645,100) belongs


3 3
S => S 2

i.e no of copies of device D3 are m3=2

(0.648, 100) pain obtained from (0.864,60)

(0.86,60) € S2 => (0.86,60) € S22

i.e no copies of D2 are 2 i.e m2=2

(0.864,60) pain obtained from (0.9,30)

(0.9,30) belongs to s11

=> (0.9,30)€ S11 i.e no of copies of devies D1,m1=1

There fore m1=1 , m2=2, m3=2


Optimal Binary Search Tree :
Consider a fixed set of words and their probabilities. The problem is to arrange these
words in a binary search tree

Let us consider S is the set of words

S= {if, for, int, while, do}

if the following are Two Binary Search Tree B S T For given set S The assumption are
each word has same probability no unsucrssful search

for
for

do int
do
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 compressing 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

Let P(i) be the probability for searching of ai

q(i) be the probability for unsuccessful search so clearly

∑p(i) + ∑q(i) =1

1≤i≤n 0≤i≤n

So Let us construct optimal Bin search true


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 whil

int
if whil

if

If BST represents ‘n’ identifiers 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

If a successful search terminute at an internal node at level L, then L interations are


required. so expected cost for me internal node for ai is P(i) * Level (ai)

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

∑ p(i)*level (ai) + ∑q(i) * (level (Ei)-1)

1≤i≤n 0≤i≤n

we can obtain optimal BST for me above value is min


prob:

Let P(i)=q(i)=1/7, for all i ;in me BST The following word set(a1,a2,a3)= (do,it,while)

do
Whil if

if do Whil if

Whil
do

(a) (b) (c)

do
While

do While

if
if

(d) (e)

with Equal probability p(i) = q(i)(e)=1/7 for all i

we have cost (a) = 1/7* 1+1/7*2+1/7*3+1/7*2+1/7*2+1/7*1

= 15/7
Cost (b) = 1/7*1+1/7*2+1/7*2+1/7*2+1/7*2+1/7*2+1/7*2

= 13/7

Cost (c) =cost(d)=cost(e)=15/7

Tree b is optimal

with p(1) = 0.5,p(2) = 0.1,p(3) = 0.05,q(0)=0.05,q(1)=0.1,q(2)=0.05,q(3)=0.05

cost(true 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

= 0.5+0.2 + 0.15 + 0.45 + 0.3 + 0.10 + 0.05

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy