{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Assignment 06 - Knapsack (Solution)

# Assignment 06 - Knapsack (Solution) - 2(1 2(1 4(0 6(1 6(1...

This preview shows page 1. Sign up to view the full content.

The Hong Kong University of Science & Technology COMP 271: Design and Analysis of Algorithms Fall 2007 Solution to Assignment 6 Question 1 : There are 5 gifts with weights 3, 1, 2, 6 and 4; and values 4, 2, 5, 10 and 9 respectively. Use dynamic programming to find the most valuable subset of gifts subject to the constraint the total weight cannot exceed 6. Show the entire table for bottom-up computation, together with the keep array. Solution: The information about these gifts is shown in the table. i 1 2 3 4 5 v i 4 2 5 10 9 w i 3 1 2 6 4 Then the table for bottom-up computation is as follows, V[i,w] 0 1 2 3 4 5 6 i=0 0 0 0 0 0 0 0 1 0(0) 0(0) 0(0) 4(1) 4(1) 4(1) 4(1) 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 2(1) 2(1) 4(0) 6(1) 6(1) 6(1) 3 0(0) 2(0) 5(1) 7(1) 7(1) 9(1) 11(1) 4 0(0) 2(0) 5(0) 7(0) 7(0) 9(0) 11(0) 5 0(0) 2(0) 5(0) 7(0) 9(1) 11(1) 14(1) ±rom the table, we have, V [5 , 6] = max { V [4 , 6] , v 5 + V [4 , 2] } = max { 11 , 9 + 5 } , keep [5 , 6] = 1; V [4 , 2] = max { V [3 , 2] , v 4 + V [3 ,-4] } = max { 5 ,-∞} , keep [4 , 2] = 0; V [3 , 2] = max { V [2 , 2] , v 3 + V [2 , 0] } = max { 2 , 5 } , keep [3 , 2] = 1 . Therefore the most valuable subset of the gifts subject to the constraint is { the 3rd gift, the 5th gift } . 1...
View Full Document

{[ snackBarMessage ]}