Daa Assignment 3
Daa Assignment 3
NAME : SONU
CLASS: BTECH(CE-42)
Q1 a)
Definition:
Examples:
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
eg. Si - Merger-purge(S(i-1),Si)
S' = {(0,0),(10,9)}
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
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
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
p1x1+p2x2+p3x3+p4x4
10x1+10=1+12x0+18x1
=38
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
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
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}=
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
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
2
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
2 5
1 4
Figure 6: Optimal binary search tree.