- #1
evinda
Gold Member
MHB
- 3,836
- 0
Hey! (Wave)I am looking at the Knapsack problem. (Nerd)
We have a sack,that has a capacity of objects with total cost $W$.
There are $n$ objects with weights $ w_1, w_2, \dots ,w_n$ and values $ v_1, v_2, \dots , v_n$.
Which is the most valuable combination of elements,that fit in the sack,given that there is an infinite number of each object?
Let $K(w)$ the value of the optimal combination of objects with total weight $ w$.
We want to find $ K(W)$.We have to express it,with respect to the subproblems.
According to my notes,we use the following algorithm:
Could you explain me why we use the formula:$$K(w)=\max_{w_i \leq w} \{ K(w-w_i)+v_i\}$$ to find the value of the optimal combination of objects with total weight $w$ ? (Thinking)
We have a sack,that has a capacity of objects with total cost $W$.
There are $n$ objects with weights $ w_1, w_2, \dots ,w_n$ and values $ v_1, v_2, \dots , v_n$.
Which is the most valuable combination of elements,that fit in the sack,given that there is an infinite number of each object?
Let $K(w)$ the value of the optimal combination of objects with total weight $ w$.
We want to find $ K(W)$.We have to express it,with respect to the subproblems.
According to my notes,we use the following algorithm:
Code:
K(0)=0
for w=1 to W
K(w)=max_{wi <= w} {K(w-wi)+vi}
Could you explain me why we use the formula:$$K(w)=\max_{w_i \leq w} \{ K(w-w_i)+v_i\}$$ to find the value of the optimal combination of objects with total weight $w$ ? (Thinking)