06 - Knapsack & Matrix Chain Multiplication (Solution)

06 - Knapsack & Matrix Chain Multiplication...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 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 Next level: m [1 , 3] = min m [1 , 1] + m [2 , 3] + p p 1 p 3 , m [1 , 2] + m [3 , 3] + p 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 p 1 p 4 , m [1 , 2] + m [3...
View Full Document

Page1 / 7

06 - Knapsack & Matrix Chain Multiplication...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online