You are on page 1of 5

Assignment No#2 of cs502

MC170401669
Q#1: Consider the following five matrices A, B, C, D and E along with their dimensions;
A B C D E
(6×5) (5×1) (1×7) (7×4) (4×2)

Determine the Optimal Multiplication Order for above matrices using Dynamic Programming approach
and also present the sequence (i.e. optimal order) in Binary Tree.

SOL: The first super diagonal:

m [A, B] = m [A, A] + m [B, B] + P0. P1. P2

=0+0+6.5.1

=30

m [B, C] =m [B, B] + m [C, C] + P1. P2. P3

=0+0+5.1.7

=35

m [C, D] =m [C, C] +m [D, D] + P2. P3. P4

=0+0+1.7.4

=28

m [D, E] =m [D, D] +m [E, E] +P3. P4. P5

=0+0+7.4.2=56
0 30
0 35
0 28
0 56
0
Now we will compute the second super diagonal. We will need two possible values for K but we wii
choose the minimum value.

m [A, C] = m [A, A] + m [B, C] + P0. P1. P3

= 0+35+6.5.7 = 245

m [A, C] =m [A, B] + m [C, C] + P0. P2. P3

= 30+0+6.1.7 = 72

The minimum m [A, C] = 72 Occurs with K =1

Similarly, for m [B, D]

m [B, D] = m [B, B] + m [C, D] +P1. P2. P4

= 0+28+5.1.4 = 48

m [B, D] = m [B, C] + m [D, D] + P1. P3. P4

= 35+0+4.1.7 = 63

Minimum m [B, D] = 48 at K=3

For m [C, E]

m [C, E] = m[C, C] + m [D, E] + P2. P3. P5

= 0+56+1.7.4 = 84

m [C, E] = m[C, D] + m [E, E] + P2. P4. P5

= 28+0+ 1.4.2 =36

Minimum m [C, E] =36 at K =3

SECOND SUPER DIAGONAL COMPUTED, m MATRIX LOOK AS FOLLOWS:

0 30 72
0 35 48
0 28 36
0 56
0
The values of k will increases:

m [A, D] = m [A, A] + m [B, D] + P0. P1. P4

= 0+48+ 6.5.4 =168

m [A, D] = m [A, B] + m [C, D] + P0. P2. P4

=30+28+6.1.7

=100

m [A, D] = m [A, C] + m [D, D] + P0. P3. P4

=72+0+6.7.4

=240

Minimum m [A, D] = 100 at K = 3

m [B, E] = m [B, B] + m [C, E] + P1. P2. P5

m [B, E] = 0+36+5.1.2

=46

m [B, E] = m [B, C] + m [D, E] + P1. P3. P5

= 35+56+5.7.2 = 161

m [B, E] = m [B, D] + m [E, E] + P1. P4. P5

=48+0+5.4.2 = 88

Minimum m [B, E] = 46 at K =3

The Matrix m looks like follows:

0 30 72 100
0 35 48 46
0 28 36
0 56
0

Now we will compute m [A, E]

m [A, E] = m [A, A] +m [B, E] + P0. P1. P5

=0+46+6.5.2 = 106

m [A, E] = m [ A, B] + m [C, E] + P0. P2. P5

= 30+36+6.1.2 = 78
m [A, E] = m [ A, C] + m [D, E] + P0. P3. P5

= 72+56+6.7.2 =212

m[A, E] = m [A, D]+[E, E] +P0. P4. P5

=100+0+6.4.2 =148

Minimum m [A, E] = 78 at K= 3

Thus, we have final cost matrix

0 30 72 100 78
0 0 35 48 46
0 0 0 28 36
0 0 0 0 56
0 0 0 0 0

Here in the order in which m entries are calculated

0 1 5 8 10
0 0 2 6 9
0 0 0 3 7
0 0 0 0 4
0 0 0 0 0

Split K values that led a Minimum m[I,j] values

0 1 1 3 3
0 0 2 3 3
0 0 0 3 3
0 0 0 0 4
0 0 0 0 0

Multiplying the five matrices and the optimal order for multiplication is

((A(BC)) (DE))

This is represented as binary tree.


3

1 4

D E

A 2

B C

Q#2:

A) List down In and Out- Degrees of vertices of the given directed graph.

Vertex In Degree Out Degree


A 3 2
B 3 0
C 2 1
D 1 2
E 0 4

A) How many cycles are there in the given directed graph, list all of them. Further, is there
any Hamiltonian cycle in it (yes/no)?
ANS: there are five cycles in the given directed graph.
A A
A B
A C
B C
C B
No, this graph is not Hamiltonian, because there is no vertex visits exactly once.

You might also like