You are on page 1of 4

1

Consider the chain matrix multiplication of 6 matrices:


A1(4x3) A2(3x6) A3(6x7) A4(7x5) A5 (5x8) A6 (8x2)
Compute the cost table m in the dynamic programming algorithm for the
chain matrix multiplication

Solution:
M(1,2)=min(1<=k<2) m(1,1)+m(2,2)+p0 p1 p2
=0+0+4(3)(6)
=72
m(2,3)=mim(2<=k<3 m(2,2)+m(3,3)+p1 p2 p3
=0+0+3(6)(7)
=126
m(3,4)=min(3<=k<4) m(3,3)+m(4+4+p2 p3 p4
=0+0+6(7)(5)
=210
m(4,5)=min(4<=k<5) m(4,4)+m(5,5)=p3 p4 p5
=7(5)(8)
=280
m(5,6)=min(5<=k<6) m(5,5)+m(6,6)+p4 p5 p6
=0+0+5(8)(2)
=80

m(1,3)
for this there are 2 values of k, when k =1 then
m(1,3)=min(1<=k<3) m(1,1)+m(2,3)+p0 p1 p3
=0+126+4(3)(7)
=210
when k=2
we find out this case already for min(2<=k<3) =126
so value 126 is to put in the table

m(2,4)
for k=2
m(2,4)=min(2<=k<4) m(2,2)+m(3,4)+p1 p2 p4
=0+210+3(6)(5)
=210+90
=300
and for k=3
value is 210 ,we already find it
so 210 will be put in table

m(3,5)
for this case ,two values of k
k=3
m(3,5)=min(3<=k<5) m(3,3)+m(4,5)+p2 p3 p5
=0+280+416

=696
for k=4
the value is 280 we find it above
so in table we write 280

m(4,6)
again two values of k
k=4 then
m(4,6)=min(4<=k<6) m(4,4)+m(5,6)+p3 p4 p6
=0+80+7(5)(2)
=80+70
=150
if k=5
value will be 80
and this 80 value will be put in table
and in the same way I find out all the values and fill the table
0

72
0

126
126
0

210
210
210
0

280
280
280
280
0

80
80
80
80
80
0

A1 (6 x 5), A2 (5 x 2), A3 (2 x 8), A4 (8 x 4), A5 (4 x 3), and A6 (3 x 9).


Solution:
By the same above method , by putting values in formula I solve this
question
0

60
0

80
80
0

64
64
64
0

96
96
96
96
0

QUESTION
Find an optimal order, and its cost, for evaluating the product
A1(10x4)xA2(4x5)x A3(5x20)xA4(20x2)xA5(2x50)

Solution:
M(1,2)=min(1<=k<2) m(1,1)+m(2,2)+p0 p1 p2
=0+0+10(4)(5)

108
108
108
108
108
0

=200
m(2,3)=min(2<=k<3) m(2,2)+m(3,3)+ p1 p2 p3
=0+0+4(5)(20)
=400
m(3,4)=min(3<=k<4) m(3,3)+m(4,4)+p2 p3 p4
=0+0+5(20)(2)
=200
m(4,5)=min(4<=k<5) m(4,4)+m(5,5)+p3 p4 p5
=0+0+20(2)(50)
=2000
m(1,3)
for this case there are two values of k
k=1,k=2
so for k=1
m(1,3)=min(1<=k<3) m(1,1)+m(2,3)+p0 p1 p3
=0+400+10(4)(20)
=400+800
=1200..(1)
for k=2
we have already find the value above which is
=400..(2)
now I will see the both values from (1) and (2) and I select the value which is smaller
than other ie 400 and in the same way fill all the places
0

200
0

400
400
0

200
200
200
0

3
Compute the edit distance and edit scripts for following strings
1. NAUGHTY and STUDENTS.

Solution:

2000
2000
2000
2000
0

S
T
U
D
E
N
T

n
1
1
1
2
3
4
5
6

0
1
2
3
4
5
6
7

a
2
2
2
2
3
4
5
6

u
3
3
3
2
3
4
5
6

g
4
4
4
3
3
4
5
6

h
5
5
5
4
4
4
5
6

t
6
6
6
5
5
5
5
6

y
7
7
7
6
6
6
6
6

BABY and BINDS.

B
I
N
D
s

0
1
2
3
4
5

b
1
1
1
2
3
4

a
2
2
2
2
3
4

b
3
3
3
2
3
4

Y
4
4
4
3
3
3

You might also like