Solving Knapsack Problem: 1st & 2nd Algorithm

  • Context: MHB 
  • Thread starter Thread starter mathmari
  • Start date Start date
Click For Summary
SUMMARY

The discussion focuses on implementing the Knapsack problem using two algorithms: the 0/1 Knapsack algorithm and the fractional Knapsack algorithm. The first algorithm calculates the maximum benefit for a set of items with given weights and values, concluding that the optimal selection yields a total benefit of 7 by choosing items (2,3) and (3,4). The second algorithm explores the fractional approach, where the user attempts to maximize benefit while adhering to a weight limit of 5, leading to confusion regarding the selection of items and their respective weights.

PREREQUISITES
  • Understanding of dynamic programming concepts
  • Familiarity with the Knapsack problem and its variations
  • Basic knowledge of algorithmic complexity
  • Proficiency in mathematical notation and expressions
NEXT STEPS
  • Study the implementation of the 0/1 Knapsack algorithm in Python
  • Explore the fractional Knapsack problem and its greedy approach
  • Learn about dynamic programming optimization techniques
  • Investigate the time and space complexity of different Knapsack algorithms
USEFUL FOR

Students, software developers, and data scientists interested in algorithm design, optimization problems, and dynamic programming techniques.

mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hey! :o

I want to apply the Knapsack algorithm at the following:

Code:
n = 4 (# of elements)
W = 5 (max weight)
Elements (weight, benefit):
(2,3), (3,4), (4,5), (5,6)

The first version of the algorithm is the following:

Code:
K(0)=0
for w=1 to W
    K(w)=max_{w_i \leq w} {K(w-w_i)+v_i}

I have found the following:

$$K(1)=\max_{w_i \leq 1} \{K(1-w_i)+v_i\}=0 \\ K(2)=\max_{w_i \leq 2} \{K(2-w_i)+v_i\}=K(2-2)+3=K(0)+3=3 \\ K(3)=\max_{w_i \leq 3} \{K(3-w_i)+v_i \}=\max \{K(3-3)+4, K(3-2)+3\}=\max \{K(0)+4, K(1)+3\}=\max \{4, 3\}=4 \\ K(4)=\max_{w_i \leq 4} \{K(4-w_i)+v_i\}=\max \{K(4-4)+5, K(4-3)+4, K(4-2)+3\}=\max \{K(0)+5, K(1)+4, K(2)+3\}=\max \{5, 4, 6\}=6 \\ K(5)=\max_{w_i \leq 5} \{K(5-w_i)+v_i\}=\max \{K(5-5)+6, K(5-4)+5, K(5-3)+4, K(5-2)+3\}=\max \{K(0)+6, K(1)+5, K(2)+4, K(3)+3\}=\max \{6, 5, 7, 7\}=7$$

At $K(5)=7$, the $7$ came from the elements $(2,3)$ and $(3,4)$. So, the optimal choice is to take these two elements with benefit $3+4=7$. Is this correct?? (Wondering)
The second version of the Knapsack algorithm is the following:

Code:
K(0,j)=0, for all j=1,2, ... ,n 
K(w,0)=0, for all w=1,2, ... ,W 
for j<-1 to n 
     for w<-1 to W 
          if w>w_j 
             K(w,j)=max{K(w,j-1),K(w-w_j,j-1)+v_j} 
          else 
             K(w,j)=K(w,j-1)

I have found the following:

$$K(1,1)=K(1,0)=0 \\ K(2, 1)=K(2, 0)=0 \\ K(3, 1)=\max \{K(3, 0), K(3-2, 0)+3\}=\max \{0, 0+3\}=\max \{0, 3\}=3 \\ K(4, 1)=\max \{K(4, 0), K(4-2, 0)+3\}=\max \{0, 0+3\}=\max \{0, 3\}=3 \\ K(5, 1)=\max \{K(5, 0)+K(5-2, 0)+3\}=\max \{0, 0+3\}=\max \{0, 3\}=3 \\ \\
K(1, 2)=K(1, 1)=0 \\ K(2, 2)=K(2, 1)=0 \\ K(3, 2)=K(3, 1)=3 \\ K(4, 2)=\max \{K(4, 1), K(4-3, 1)+4\}=\max \{3, 0+4\}=\max \{3, 4\}=4 \\ K(5, 2)=\max \{K(5, 1), K(5-3, 1)+4\}=\max \{3, 0+4\}=\max \{3, 4\}=4 \\ \\
K(1, 3)=K(1, 2)=0 \\ K(2, 3)=K(2, 2)=0 \\ K(3, 3)=K(3, 2)=3 \\ K(4, 3)=K(4, 2)=4 \\ K(5, 3)=\max \{K(5, 2), K(5-4, 2)+5\}=\max \{4, 0+5\}=\max \{4, 5\}=5 \\ \\
K(1, 4)=K(1, 3)=0 \\ K(2, 4)=K(2, 3)=0 \\ K(3, 4)=K(3, 3)=3 \\ K(4, 4)=K(4, 3)=4 \\ K(5, 4)=K(5, 3)=5$$ Is this correct?? (Wondering) How can we find which elements we have to take?? (Wondering)
 
Technology news on Phys.org
I found the following algorithm for the fractional version of the Knapsack problem:

Code:
    Algorithm fractionalKnapsack(S, W) 

    Input: set S of items w/ benefit bi
    and weight wi
    ; max. weight W

    Output: amount xi of each item i
    to maximize benefit with
    weight at most W

    for each item i in S
         xi ← 0
         vi ← bi / wi {value}
    w ← 0 {total weight}
    while w < W
         remove item i with highest vi
         xi ← min{wi , W − w}
         w ← w + min{wi , W − w}

I tried to apply this algorithm and I got the following:

$$x_1 \leftarrow 0 \\ v_1 \leftarrow \frac{3}{2}=1.5 \\ x_2 \leftarrow 0 \\ v_2 \leftarrow \frac{4}{3}=1.33 \\ x_3 \leftarrow 0 \\ v_3 \leftarrow \frac{5}{4}=1.25 \\ x_4 \leftarrow 0 \\ v_4 \leftarrow \frac{6}{5}=1.2 \\ w \leftarrow 0 \\ 0<5 \checkmark \\ \ \ \ \ \ \text{ remove item } =1, \max v_i=1.5 \\ \ \ \ \ \ x_1 \leftarrow \min \{w_1, W-w\}=\min \{2, 5-0\}=\min \{2, 5\}=2 \\ \ \ \ \ \ w \leftarrow 2+\min \{w_1, W-w\}=0+2=2 \\ 2<5 \checkmark \\ \ \ \ \ \ \text{ remove item } i=2, \max v_i=1.33 \\ \ \ \ \ \ x_2 \leftarrow \min \{w_2 , W-w\}=\min \{3, 5-2\}=\min \{3, 3\}=3 \\ \ \ \ \ \ w \leftarrow w+\min \{w_2, W-w\}=2+3=5 \\ 5<5 \times$$ Does this mean that the optimal choice is to take two times the item $1$ and three times the item $2$ ?? (Wondering)

But isn't then the total weight greater than $5$ ?? (Wondering)
 
Last edited by a moderator:

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
4K