Anda di halaman 1dari 5

Assignment No.

02

Total Marks: 20

Semester: Spring 2016


CS502: Fundamentals of Algorithms

Due Date: 21/07/2016

Instructions
Please read the following instructions carefully before submitting assignment:

It should be clear that your assignment will not get any credit (zero marks) if:
o

The assignment is submitted after due date.

The submitted assignment is other than .doc file.

The submitted assignment does NOT open or file is corrupted.

The assignment is copied (from other student or ditto copy from any other

source).

Objective

The objective of this assignment is to:

Learn solving Chain Matrix Multiplication Problem using Dynamic Programming


approach

Submission

You are required to submit your solution through LMS as MS Word document.

For any query about the assignment, contact at CS502@vu.edu.pk


GOOD LUCK

Problem Statement:
Consider the following four matrices A1, A2, A3, and A4 along with their dimensions.
A1 = 4*3
A2 = 3*2
A3 = 2*3
A4 = 3*5
Questions:
1. Determine the optimal multiplication order for above matrices A1, A2, A3, and A4
using Dynamic Programming approach.
Answer:

four matrices A1, A2, A3, and A4 along with their dimensions.
A1 = 4*3
A2 = 3*2
A3 = 2*3
A4 = 3*5
Four Mtrices
m[1, 1]

m[1,2]
m[2,2]

m[2,3]
m[3,3]

m[3,4]
m[4,4]

We want to find the optimal multiplication order for


A1

(43)

A2

(32)

A3

(23)

A4

(35)

P0 4
P1 3
P2 2
P3 3
P4 5
We will compute the entries of the m matrix starting with the base condition. We first fill that
main
diagonal:

0
0
0
0
Next, we compute the entries in the first super diagonal, i.e., the diagonal above the main
diagonal:
m[1, 2] = m[1, 1] +m[2, 2] + p0 p1 p2 = 0 + 0 + 4 3 2 = 24
m[2, 3] = m[2, 2] +m[3, 3] + p1 p2 p3 = 0 + 0 + 3 2 3 = 18
m[3, 4] = m[3, 3] +m[4, 4] + p2 p3 p4 = 0 + 0 + 2 3 5 = 30
The matrix m now looks as follows:
0

24
0

18
0

30
0

We now proceed to the second super diagonal. This time, however, we will need to try two
possible
values for k. For example, there are two possible splits for computing m[1, 3]; we will choose
the split
that yields the minimum:
m[1, 3] = m[1, 1] +m[2, 3] + p0 p1 p3 = 0 + 18 + 4 3 3 = 54
m[1, 3] = m[1, 2] +m[3, 3] + p0 p2 p3 = 24 + 0 + 4 2 3 = 48
the minimum m[1, 3] = 48 occurs with k = 2

m[2, 4] = m[2, 2] +m[3, 4] + p1 p2 p4 = 0 + 30 + 3 2 5 = 60


m[2, 4] = m[2, 3] +m[4, 4] + p1 p3 p4 = 18 + 0 + 3 3 5 = 63
minimum m[2, 4] = 60 at k = 1
With the second super diagonal computed, the m matrix looks as follow:
0
24
48
0

18

60

30
0

We repeat the process for the remaining diagonals. However, the number of possible splits
(values of k)
increases:
m[1, 4] = m[1, 1] +m[2, 4] + p0 p1 p4 = 0 + 60 + 4 3 5 = 120
m[1, 4] = m[1, 2] +m[3, 4] + p0 p2 p4 = 24 + 30 + 4 2 5 = 94
m[1, 4] = m[1, 3] +m[4, 4] + p0 p3 p4 = 48 + 0 + 4 3 5 = 108
minimum m[1, 4] = 94 at k = 2
The matrix m at this stage is:
0
24
48
94
0

18

60

30
0

Here is the order in which m entries are calculated


0
1
4
0

3
0

and the split k values that led to a minimum m[i, j] value


0
1
1
0

3
0

2. Represent optimal order as a binary tree.


Based on the computation, the minimum cost for multiplying the four matrices is 94 and the
optimal
order for multiplication is
(( A1 A2 )( A2 A4 ))
This can be represented as a binary tree
2

A1

A2 A3

A4