; Matrix Chain Homework Solution
Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out
Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>

Matrix Chain Homework Solution

VIEWS: 37 PAGES: 3

  • pg 1
									lCS311 Spring 2008 Homework Solution Ch.15, part 2: Matrix Chain 1. (30 pts) Using the dynamic programming approach using bottom-up algorithm, find the optimal position of parenthesis of a matrix-chain product whose sequence of dimensions is {30, 10, 20, 10, 40}. Prove your result by drawing it in the style of Fig. 15.3, and also label the order in which the cells are filled in.

p = <30, 10, 20, 10, 40> where i = j the cell will be filled with a 0 m[1,2] = m[1,1] + m[2,2] + 30*10*20 = 6000 s[1,2] = 1 m[2,3] = m[2,2] + m[3,3] + 10*20*10 = 2000 s[2,3] = 2 m[3,4] = m[3,3] + m[4,4] + 20*10*40 = 8000 s[3,4] = 3 m[1,3] = min m[1,1] + m[2,3] + 30*10*20= 2000 + 3000 = 5000 m[1,2] + m[3,3] + 30*10*20= 6000 + 6000 = 12000 = 5000 s[1,3] = 1 m[2,4] = min m[2,2] + m[3,4] + 10*20*40= 8000 + 8000 = 16000 m[2,3] + m[4,4] + 10*10*40= 2000 + 4000 = 6000 = 6000 s[2,4] = 3 m[1,4] = min m[1,1] + m[2,4] + 30*10*40 = 6000 + 12000 = 18000 m[1,2] + m[3,4] + 30*20*40 = 6000 +8000 +24000 = 38000 m[1,3] + m[4,4] + 30*10*40 = 5000 + 12000 = 17000 = 17000 s[1,4] = 3 the order of calls to fill the m and s matrix are as above. starting from the top going down. m Matrix

j

1 0

2 6000 0

3 5000 2000 0 3 1 2

4 17000 6000 8000 0 4 3 3 3

1 2 3 4 1 2 3 4

i

s Matrix j 1

2 1

i

Optimal parens: (M1(M2M3))M4

What is the savings achieved using dynamic programming versus using the parenthesis in the order ((M1M2)M3)M4 ? ((M1 M2) M3) M4 requires 6000+ 6000 + 12000 = 24000 multiplications. So, there is about 25% savings. Is it worth it running the dynamic programming algorithm on this problem? Hm, that’s a good question. We need to go through the loop on the order of 4^3 in order to reduce by 7000 multiplications. Is that worth it?

2. For problem 1, write recursive and memoized algorithms based on the dynamic programming solution. Prove your result by drawing it in the style of Fig. 15.3, and also label the order in which the cells are filled in. Discuss the differences between solutions in problems 1 and 2. This is already on the web. The last quiz was about that as well. Warning: this problem is a good candidate for the next exam, where you would be given a recursive formula like this one and asked to write recursive, memorized, and iterative solutions. (We don’t need to know the application of the formula, we are practicing coding a formula.) Ch. 16 1. (20 pts) Construct a variable length optimal prefix code using the Huffman’s code _from our textbook_. Trace the code by hand and show the priority queue. Notice that the textbook skips showing the queue. Encode the word and show the resulting encoded message. Start with the array in the order below. LILIAN Character Frequency Fixed length code Prefix code L:2 L 2 00 I 2 01 A 1 11 N 1 10

I:2 A:1 N:1 BuildMinHeap: N:1 I:2 A:1 L:2 Extract min twice: A:1 I:2 L:2 L:2 I:2 z1:2

z1: N:1 A:1

Extract min twice: Z1:2 z2:4 I:2 L:2 z1:2 I2: Z2:4 Huffman tree: Z3:6 I:2 z2:4 L:2 z1:2 N:1 A:1 If we put 0s on the left (it can be 1s too, does not matter): I: 0 L: 10 N: 110 A: 111 Total: 2x1 + 2x2 + 3x2 = 12 bits Fixed length is any combo of 2-bit words. Total: 12 bits. How much savings does Huffman code provide compared to a fixed-length code? No savings.

What can you conclude regarding the efficiency of a variable-length prefix code? It is useful if there is a great variation in letter frequency. Otherwise, no.


								
To top