{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

06 - Knapsack &amp; Matrix Chain Multiplication (Solution)

# 06 - Knapsack &amp; Matrix Chain Multiplication...

This preview shows pages 1–4. Sign up to view the full content.

The Hong Kong University of Science & Technology COMP 271: Design and Analysis of Algorithms Fall 2007 Tutorial 6: Knapsack & MCM Below is the suggest solution to the tutorial questions. Question 1 : What is the optimal way to compute A 1 A 2 A 3 A 4 A 5 , where the dimensions of the matrices are: A 1 : 10 × 20, A 2 : 20 × 2, A 3 : 2 × 40, A 4 : 40 × 5, A 5 : 5 × 30? Show the table for the bottom-up computation. Solution: Initially, we set: m [1 , 1] = 0 m [2 , 2] = 0 m [3 , 3] = 0 m [4 , 4] = 0 m [5 , 5] = 0 Next level: m [1 , 2] = min ( m [1 , 1] + m [2 , 2] + p 0 p 1 p 2 ) = min ( 0 + 0 + 400 = 400 ) = 400 s [1 , 2] = 1 m [2 , 3] = min ( m [2 , 2] + m [3 , 3] + p 1 p 2 p 3 ) = min ( 0 + 0 + 1600 = 1600 ) = 1600 s [2 , 3] = 2 m [3 , 4] = min ( m [3 , 3] + m [4 , 4] + p 2 p 3 p 4 ) = min ( 0 + 0 + 400 = 400 ) = 400 s [3 , 4] = 3 m [4 , 5] = min ( m [4 , 4] + m [5 , 5] + p 3 p 4 p 5 ) = min ( 0 + 0 + 6000 = 6000 ) = 6000 s [4 , 5] = 4 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Next level: m [1 , 3] = min m [1 , 1] + m [2 , 3] + p 0 p 1 p 3 , m [1 , 2] + m [3 , 3] + p 0 p 2 p 3 = min 0 + 1600 + 8000 = 9600 , 400 + 0 + 800 = 1200 = 1200 s [1 , 3] = 2 m [2 , 4] = min m [2 , 2] + m [3 , 4] + p 1 p 2 p 4 , m [2 , 3] + m [4 , 4] + p 1 p 3 p 4 = min 0 + 400 + 200 = 600 , 1600 + 0 + 4000 = 5600 = 600 s [2 , 4] = 2 m [3 , 5] = min m [3 , 3] + m [4 , 5] + p 2 p 3 p 5 , m [3 , 4] + m [5 , 5] + p 2 p 4 p 5 = min 0 + 6000 + 2400 = 8400 , 400 + 0 + 300 = 700 = 700 s [3 , 5] = 4 Next level: m [1 , 4] = min m [1 , 1] + m [2 , 4] + p 0 p 1 p 4 , m [1 , 2] + m [3 , 4] + p 0 p 2 p 4 , m [1 , 3] + m [4 , 4] + p 0 p 3 p 4 = min 0 + 600 + 1000 = 1600 , 400 + 400 + 100 = 900 , 1200 + 0 + 2000 = 3200 = 900 s [1 , 4] = 2 m [2 , 5] = min m [2 , 2] + m [3 , 5] + p 1 p 2 p 5 , m [2 , 3] + m [4 , 5] + p 1 p 3 p 5 , m [2 , 4] + m [5 , 5] + p 1 p 4 p 5 = min 0 + 700 + 1200 = 1900 , 1600 + 6000 + 24000 = 31600 , 600 + 0 + 3000 = 3600 = 1900 s [2 , 5] = 2 (1) 2
And the final level: m [1 , 5] = min m [1 ,

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}