Algorithm Optimization for Knapsack Problem

Slide Note
Embed
Share

The homework assignment involves analyzing the performance of two different versions of the Knapsack algorithm by making specific choices regarding item selection. Additionally, a modification to the algorithm is proposed to handle the knapsack problem with unlimited supplies of items, tracking the number of item copies needed. The comparison and adjustments aim to enhance the efficiency of solving the Knapsack Problem.


Uploaded on Sep 13, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Algorithms Homework Assignment #4 Willy Chang, Wei-Shao Tang

  2. HW#4 No.2 In algorithm Knapsack, we first checked whether the i-th item is unnecessary (by checking P[i-1, j]). If there is a solution with the i-1 items, we take this solution. We can also make the opposite choice, which is to take the solution with the i-th item if it exists (i.e., check P[i-1, j-ki] first). Which version do you think will have a better performance? (8pt) Redraw Fig. 5.11 (see slides) to reflect this choice.(12pt)

  3. Algorithm Original_Knapsack (S, K); 1 begin 2 P[0, 0].exist := true; 3 for k := 1 to K do 4 P[0, k].exist := false; 5 for i := 1 to n do 6 for k := 0 to K do 7 P[i, k].exist := false; 8 if P[i 1, k].exist then 9 P[i, k].exist := true; 10 P[i, k].belong := false; 11 else if k - S[i] 0 then 12 if P[i-1, k - S[i]].exist then 13 P[i, k].exist := true; 14 P[i, k].belong := true; 15 end Algorithm Modified_Knapsack (S, K); 1 begin 2 P[0, 0].exist := true; 3 for k := 1 to K do 4 P[0, k].exist := false; 5 for i := 1 to n do 6 for k := 0 to K do 7 P[i, k].exist := false; 8 if k - S[i] 0 then 9 if P[i 1, k-S[i]].exist then 10 P[i, k].exist := true; 11 P[i, k].belong := true; 12 else if P[i 1, k].exist then 13 P[i, k].exist := true; 14 P[i, k].belong := false; 15 end

  4. HW#4 No.2 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 O - - - - - - - - - - - - - - - - k1=2 O - I - - - - - - - - - - - - - - k2=3 O - O I - I - - - - - - - - - - - k3=5 O - O O - I - I I - I - - - - - - k4=6 O - O O - O I O I I O I - I I - I The original algorithm will have a better performance by avoiding the overhead of comparison. (checking k - S[i] 0) 9 10 11 12 13 14 15 if k - S[i] 0 then if P[i 1, k-S[i]].exist then P[i, k].exist := true; P[i, k].belong := true; else if P[i 1, k].exist then P[i, k].exist := true; P[i, k].belong := false;

  5. HW#4 No.3 The Knapsack Problem that we discussed in class is defined as follows: Given a set S of n items, where the ith item has an integer size S[i], and an integer K, find a subset of the items whose sizes sum to exactly K or determine that no such subset exists. We have described in class an algorithm to solve the problem. Modify the algorithm to solve a variation of the knapsack problem where each item has an unlimited supply. In your algorithm, please change the type of P[i, k].belong into integer and use it to record the number of copies of item i needed.

  6. HW#4 No.3 8 9 10 11 12 13 14 15 16 17 end Algorithm Knapsack (S, K); 1 begin 2 P[0, 0].exist := true; 3 P[0, 0].belong := 0; 4 for k := 1 to K do 5 P[0, k].exist := false; 6 for i := 1 to n do 7 for k := 0 to K do P[i, k].exist := false; if P[i 1, k]:exist then P[i, k].exist := true; P[i, k].belong := 0; else if k - S[i] 0 then if P[i, k - S[i]].exist then P[i, k].exist := true; P[i, k].belong := P[i, k - S[i]].belong + 1; 0 1 2 3 4 5 6 0 - - - - - - k1=2 k2=3 0 - 1 - 2 - 3 0 - 0 1 0 1 2

Related