0% found this document useful (0 votes)
90 views16 pages

Daa Assignment 3

The document contains a student's assignment submission which includes their name, class, and roll number. It then provides responses to multiple questions involving concepts like the principle of optimality, single source vs all pairs shortest path algorithms, dominance rules for merging sets in knapsack problems, and calculating lower and upper bounds for nodes in a knapsack problem solved using dynamic programming.

Uploaded by

Logan Xmen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views16 pages

Daa Assignment 3

The document contains a student's assignment submission which includes their name, class, and roll number. It then provides responses to multiple questions involving concepts like the principle of optimality, single source vs all pairs shortest path algorithms, dominance rules for merging sets in knapsack problems, and calculating lower and upper bounds for nodes in a knapsack problem solved using dynamic programming.

Uploaded by

Logan Xmen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

DAA ASSIGNMENT-3

NAME : SONU

CLASS: BTECH(CE-42)

ROLL NO: 18001003125

Q1 a)
Definition:

A problem is said to satisfy the Principle of Optimality if the subsolutions of an


optimal solution of the problem are themesleves optimal solutions for their
subproblems.

Examples:

 The shortest path problem satisfies the Principle of Optimality.


 This is because if a,x1,x2,...,xn,b is a shortest path from node a to node b in a graph,
then the portion of xi to xj on that path is a shortest path from xi to xj.
 The longest path problem, on the other hand, does not satisfy the Principle of
Optimality. Take for example the undirected graph G of nodes a, b, c, d, and e, and
edges (a,b) (b,c) (c,d) (d,e) and (e,a). That is, G is a ring. The longest (noncyclic) path
from a to d to a,b,c,d. The sub-path from b to c on that path is simply the edge b,c.
But that is not the longest path from b to c. Rather, b,a,e,d,c is the longest path. Thus,
the subpath on a longest path is not necessarily a longest path.

Q1 b)
Single source shortest path algorithms basically finds the shortest distance between a
single node usually specified and all other nodes example is Dijkstra algorithm .

While all pair shortest path algorithms find the shortest distance between any pair of
nodes in the graph example is Floyd–Warshall algorithm.

Q1 c)
answer
It states that when we merge two sets the condition is that with increase of
weight,profit should be increased 

Dominance rule is also called 'Merge-Purse'.

eg. Si - Merger-purge(S(i-1),Si)

     let S0 = {(0,0)} and S1 = {(10,9)}

    S' = {(0,0),(10,9)}

    This rule is used int 0 1 Knapsack in dp

Q2)(a)
g(2, Φ) = 3 g(3, Φ) = 4 g(4, Φ) = 8 g(5, Φ) = 10 g(2, {3}) = 10
g(2, {4}) = 22 g(2, {5}) = 19 g(3, {2}) = 11 g(3, {Φ}) = 14 g(3, {5}) = 28
g(4, {2}) = 6 g(4, {3}) =9 g(4, {5}) = 21 g(5, {2}) = 17 g(5, {3}) = 13
g(5, {4}) = 16 g(3,{4,5}) = min{c34 + g(4,{5})c35 + g(5,{4})}
g(3,{4,5}) = min(6+21,18+16) = 27 g(4,{3,5}) = min(5+28,11+13) = 24
g(5,{3,4}) = min(9+14,8+9) = 17 g(2,{5,4}) = min(21+14,26+9) = 25
g(4,{2,5}) = min(19+3,11+17) = 22 g(5,{2,4}) = min(22+14,8+6) = 14
g(2,{3,5}) = min(6+28,13+9) = 22 g(3,{2,5}) = min(19+8,18+17) = 27
g(5,{3,2}) = min(10+14,11+9) = 20 g(2,{3,4}) = min(6+14,14+9) = 20
g(3,{2,4}) = min(8+22,6+6) = 12 g(4,{2,3}) = min(3+10,5+11) = 13
g(2,{3,4,5}) = min(6+27,14+24,9+17) = 26
g(3,{2,4,5}) = min(8+25,6+22,18+14) = 28
g(4,{2,3,5}) = min(3+22,5+27,11+20) = 25
g(5,{2,3,4}) = min(20+14,12+9,8+13) = 21
g(1,{2,3,4,5}) = min(6+26,3+28,10+25,8+21) = 29

(b) g(2, Φ) = 15 g(3, Φ) = 3 g(4, Φ) = 19 g(5, Φ) = 16


g(2, {5}) = 18 g(3, {5}) = 20 g(2, {3}) = 19 g(3, {2}) = 20 g(2, {4}) = 23
g(3,{4}} = 21 g(4, {2}) = 21 g(5, {2}) = 19 g(4, {3}) =9 g(5, {3}) = 20
g(4, {5}) = 19 g(5, {4}) = 35 g(3,{4,5}) = min(2+19,4+35) = 21
g(4,{3,5}) = min(18+20,20+3) = 23 g(5,{3,4}) = min(7+21,16+21) = 28
g(2,{5,4}) = min(4+19,2+35) = 23 g(4,{2,5}) = min(6+18,3+19) = 22
g(5,{2,4}) = min(23+4,16+21) = 27 g(2,{3,5}) = min(16+20,2+20) = 22 g(3,
{2,5}) = min(5+18,4+19) = 23g(5,{3,2}) = min(4+19,7+20) = 23
g(2,{3,4}) = min(16+21,4+21) = 25 g(3,{2,4}) = min(5+23,2+21) = 23
g(4,{2,3}) = min(6+19,18+20) = 25
g(2,{3,4,5}) = min(16+21,4+23,2+28) = 27
g(3,{2,4,5}) = min(5+23,2+22,4+27) = 24
g(4,{2,3,5}) = min(6+22,18+23,3+23) = 26
g(5,{2,3,4}) = min(25+4,23+7,25+15) = 29
g(1,{2,3,4,5}) = min(20+27,30+24,10+26,11+29) = 36

Q3)(a) Solution: Let us use the fixed tuple formulation.


The search begins with root node as E-node.

cp - current profit cw- current weight


k – k number of decisions m - Capacity of knapsack

Algorithm Ubound(cp,cw,k,m)
{
b := cp; c := cw;
for i:= k+1 to n do
{
if ( c + w[i] <= m ) then
{
c := c + w[i]; b := b - p[i];
}
}
return b;
}
Function U(.) for Knapsack problem
Node 1 Ubound(0,0,0,15)      

 
i=1 i=2 i=3 i=4
 
c=2 c=6 c=12  

 
b=-10 b=-20 b=-32  

 
U(1)=-32      

 
cˆ(1) = -32+3/9 x 18= -38    

 
To calculate lower bound we allow fractions  

Node2 Ubound(-10,2,1,15) x1=1


 

 
i=2 i=3 i=4  

 
c=6 c=12    

 
b= -20 b= -32    

 
U(2)=-32      

 
cˆ(2) = -32+3/9 x 18= -38    

 
To calculate lower bound we allow fractions  

Node 3 Ubound(0,0,1,15)      

 
i=2 i=3 i =4
 

 
c=4 c = 10    

 
b= -10 b = -22    

 
U(3)=-22      

 
cˆ(3) = -22+5/9 x 18= -32    

 
To calculate lower bound we allow fractions  

Node 4 Ubound(-20,6,2,15)    

 
i=3 i=4    

 
c=12      

b=-32 U(4)=-32
cˆ(4) = -32+3/9 x 18= -38
To calculate lower bound we allow fractions
Node 5 Ubound(-10,2,2,15)
 
i=3 i=4
 
c=8  

 
b=-22  

 
U(5)=-22  

 
cˆ(5) = -22+7/9 x 18= -36
To calculate lower bound we allow
 
fractions
Node 6 Ubound(-32,12,3,15)
 
i=4

 
U(6)=-32
 
cˆ(6) = -32+3/9 x 18= -38
To calculate lower bound we allow
 
fractions
Node 7 Ubound(-20,6,3,15)
 
i=4
 
c=15
 
b=-38
 
U(7)=-38
 
cˆ(7) = -38
Node 8 Ubound(-38,15,4,15)

 
U(8)=-38
 
cˆ(8) = -38
Node 9 Ubound(-20,6,4,15)

 
U(1)=-20
 
cˆ(1) = -20

Node 8 is a solution node (solution vector)


X1=1
X2=1
X3=0
X4=1

p1x1+p2x2+p3x3+p4x4
10x1+10=1+12x0+18x1
=38

We need to consider all n items. The tuple size is n.

(b) Convert the profits to –ve. , (p1,p2,p3,p4,p5)=(-10,-15,-6,-8,-4). Calculate the


lower bound and upper bound for each node.
Place the first item in knapsack. i.e. weight =4. Remaining weight is 12-4=8 Place
second item i.e. with weight 6. Remaining weight = 8-6=2.
Since fractions are not allowed in calculation of upper bound, so we cannot place the
third and fourth items. place fifth item .

Profit earned=-10-15-4=-29=upper bound

To calculate lower bound, place third item in bag considering fractions are allowed
∴Lower bound = −1 2
−10 5 − ⋅ 6 = −29
 

   
3  

∴ uˆ (1) = −29, cˆ(1) = −29.

For node 2, x1=1 means, we should place first item in the bag.
cˆ(2 = −1 2
) −10 5 − ⋅ 6 = −29
 

     
3  

uˆ(2) =
−10 −15 − 4 = −29

For node 3, x1=0


3
cˆ(3) = −15 − 6 − ⋅ 8 = −15 − 6 − 6 = −27 4
uˆ(3) = −15 − 6 − 4 = −25
Select the minimum of lower bounds i.e c( x) = min{c(lchild ( x)), c(rchild ( x))}
c( x) = min{cˆ(2)), cˆ(3))} = min{−29, −27} =
0 4 8 −29 = cˆ(2) Chose node 2.

 0 5 12 The path is 1-2-4-7-9-10


The solution for knapsack problem is
(x1,x2,x3,x4,x5)=(1,1,0,0,1)
  0 7 Maximum profit = 10+15+4=29.

5 9 13 0
Q4)
Ans 4) :-

0 4 8  1 2 3 4

 0 5 12
1 using
  0 7
2 =A0 A[I,j]=min{Ak-1[I,j];Ak-1[I,k];Ak-1[k,j];
5 9 1 0
3 3

A1[2,3]=min{5,  +8}=5

+  }=12

A2[2,4]=min{12+

A3[3,2]=min{  ,  +4}= 

=A1 A4[3,4]=min{7,  +  }=7;

A5[4,2]=min{  ,5+4}=9

A6[4,3]=min{  ,5+8}
0 4 8 16
 0 5 12
  0 7
5 9 13 0
0 4 8 15
 0 5 12
  0 7
5 9 13 0
A2[1,3]=min{8,4+5}=8

A2[3,1]=min{  , 4+12}=16

= A2 A2[3,1]=min{  ,  +  }= 

A2[3,4]=min{7,  +12}=7

A2[4,1]=min{5,9+  }=5
A2[4,3]=min{13,+9+5}=13
A3[1,2]=min{4,8+  }=4
A3[1,4]=min{16,8+7}=15

A3[2,1]=min{  ,5+  }= 
= A3 A3[2,4]=min{12,5+7}=12

A3[4,1]=min{5,13+  }=5

A3[4,2]=min{9,13+  }=9

0 4 8 15
A4[]=min{4,5+19}=4
17 0 5 12 A4[]=min{8,15+13}=8

12 16 0 7 A4[]=min{  ,12+5}=17

5 9 13 0 =A4 A4[]=min{5,12+13}=5

A4[]=min{  ,7+5}=12

A4[]=min{  ,7+9}=16

1 2 3 4 5

 6 3 10 8
1 using
3  6 14 9
A[I,j]=min{Ak-1[I,j];Ak-1[I,k];Ak-1[k,j];
4 8  6 18
2
8 3 5  11

10 14 9 8 
3

6 3 10 8 A1[2,2]=min{  , 3+6}=9

A1[2,3]=min{6,3+3}=6
3 9 6 13 9 A1[2,4]=min{14,3+10}=13
= A1 A1[2,5]=min{9,3+8}=9
4 8 7 6 12
A1[3,2]=min{8,4+6}8

8 3 5 11 18

10 14 9 8 18 Q5)
Sol: Use the recursive function. The results are
shown in the following two table. (Table 1 and Table 2) And the optimal binary
search tree is in the Figure drawn in the last of this question.
The question is continued with the ahead figures.
The above figure is of DFS and this figure is of 2-8 nodes.
Directed Graph G
 
0 1 2 3 4 5 6 7
                 

10.06 0.16 0.28 0.42 0.49 0.64 0.81 1.00



0.06 0.18 0.32 0.39 0.54 0.71 0.9
3   
0.06 0.2 0.27 0.42 0.59 0.78
4     
0.06 0.13 0.28 0.45 0.64
5       
0.05 0.2 0.37 0.56
6         
0.05 0.22 0.41
7           
0.05 0.24

8             
0.05

Table 1: The table the recursive result of w[i,j]. The column index is the index for
dummy keys(qj  ), and the row index is for pi
 

 
0
   
1  
2  
3  
4  
5  
6  
7
10.06 0.28 0.62 1.02 1.34 1.83 1.42 3.12

0.06 0.30 0.68 0.93 1.41 1.96 2.61
3   
0.06 0.32 0.57 1.04 1.48 2.13
4     
0.06 0.24 0.57 0.99 1.55
5       
0.05 0.30 0.72 1.20
6         
0.05 0.32 0.78
7           
0.05 0.34
8             
0.05

Table 2: The table the recursive result of e[i,j]. The column index is the index for
dummy keys(qj  ), and the row index is for pi
Q6)
j-1 ↓ i 0 1 2 3 4 5

0 w=0.6 w=0.06 w=0.06 w=0.06 w=0.05 w=0.05
c=0 r=0 c=0 r=0 c=0 r=0 c=0 r=0 c=0 r=0 c=0 r=0
1 w=0.16 w=0.18 w=0.2 w=0.13 w=0.2
c=0.16 c=0.18 c=0.2 r=3 c=0.13 c=0.2 r=5
r=1 r=2 r=4
2 w=0.28 w=0.32 w=0.27 w=0.28
c=0.44 c=0.5 r=3 c=0.4 r=3 c=0.41
r=2 r=5
3 w=0.42 w=0.39 w=0.42
c=0.78 c=0.7 r=3 c=0.82
r=2
4 w=0.49 w=0.54
c=1.05 c=1.13
r=2 r=3
5 w=0.64
c=1.49
r=3

C[i,j] = min{c[i,k-1]+c[k,j]} + w[i,j] i<k≤j


C[0,2] = min{ 0+0.18, 0.16+0} + 0.28
= 0.16 +0.28 =0.44 k=2
C[1,3] = min{ 0+0.2 , 0.18 + 0} + 0.32
=0.18 +0.32 =0.5 k=3
C[2,4] = min{ 0 +0.13, 0.2 +0} + 0.27
= 0.13 +0.27 = 0.4 k=3
C[3,5] = min{ 0 +0.2, 0.13 + 0} +0.28
=0.13 +0.28 = 0.41 k=5
C[0,3] = min{ 0+0.5, 0.16+0.2,0.44+0} +0.42
=0.36 +0.42 =0.78 k=2
C[1,4] =min{0+0.4,0.18 +0.13,0.5 + 0} +0.39
=0.31+0.39 =0.7 k=3
C[2,5] = min{0+0.41, 0.2+0.2, 0.4+0} +0.42
=0.4 + 0.42 =0.82
C[0,4] =min{0+0.7, 0.16+0.4, 0.44+0.13, 0.78+0} + 0.49
=0.56 +0.49 = 1.05 k=2
C[1,5] =min{0+0.82,0.18+0.41,0.5+0.2,0.7+0} + 0.54
=0.59+0.54 = 1.13
C[0,5] = min{0+1.13, 0.16 +0.82,0.44+0.41,0.78+0.2,1.05+0} + 0.64
= 0.85 +0.64 = 1.49 k=3

Minimum cost of OBST = 1.49

2 5

1 4
Figure 6: Optimal binary search tree.

You might also like