[Tutorial] Optimized solution for Knapsack problem
Difference between en8 and en9, changed 385 character(s)
Problem Statement↵
------------------↵
We are going to deal with the well known knapsack problem with an additional constraint. We are given a list of $N$ items and a knapsack of size $W$. Every item has a cost $c_i$ associated with it ($1 \leq i \leq N$). We can select some items from the list such sum of the cost of all the selected items does not exceed $W$. The goal is tell for all $w$ ($0 \leq w \leq W$), if we can select any number of items such that their total cost equals $w$. This is also known as the 0/1 knapsack problem. This can be easily solved in $O(NW)$ time complexity using standard knapsack approach.↵

The addition constraint we have is $\sum\limits_{i=1}^N c_i \sim O(N)$.↵

<br>↵
The bounded knapsack problem↵
------------------↵
The bounded knapsack problem is like the 0/1 knapsack problem, except in this we are also given a count for each item. In other words, each item has a count $s_i$ associated with it and we can select an item $s_i$ times ($1 \leq i \leq N$).↵

<br>↵
Solving bounded knapsack problem↵
------------------↵
The solution is simple. Let $dp[i][j]$ be the minimum count of $i$th item that has to be used to get a total cost of $j$ while using some number (possibly $0$) of first $i$ items. If a total cost of $j$ can not be obtained using first $i$ items, then $dp[i][j] = -1$. The following code is used to calculate $dp[i][j]$,↵

~~~~~↵
if(dp[i-1][j] >= 0)↵
    dp[i][j] = 0;↵
else if(dp[i][j - c[i]] >= 0 and dp[i][j - c[i]] < s[i])↵
    dp[i][j] = dp[i][j - c[i]] + 1;↵
else↵
    dp[i][j] = -1;↵
~~~~~↵

Here, `c[i]` is the cost and `s[i]` is the count for $i$th item. Also, $dp[0][j] = -1$ for all $1 \leq j \leq W$ and $dp[0][0] = 0$. Time complexity is $O(NW)$.↵

<br>↵
Optimizing 0/1 Knapsack↵
------------------↵
Now we can present a faster solution to our problem. Notice that number of items is $N$ and $\sum\limits_{i=1}^N c_i \sim O(N)$. Hence, there can only be $O(\sqrt{N})$ unique costs. So we convert our problem to a bounded knapsack problem with $O(\sqrt{N})$ unique items having some count. This can be solved in $O(W\sqrt{N})$ !!!↵

<br>↵
**PS:** I wrote this blog since I could not find a good source on the internet to learn about this approach. I hope there is no error since I really didn't read about this anywhere and worked out this approach myself.


**EDIT:** As some people have pointed out, this method has been discussed in some previous blogs (see comments for links). The only practise problem I could find is [755F](http://codeforces.com/contest/755/problem/F). Also, the method discussed in this blog is not the most optimal one possible and can be optimized further using a `bitset` (which I will discuss in a future blog).

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en9 English sdnr1 2018-05-24 09:40:32 385
en8 English sdnr1 2018-05-22 22:02:49 17
en7 English sdnr1 2018-05-21 17:50:32 2 (published)
en6 English sdnr1 2018-05-21 17:45:34 25
en5 English sdnr1 2018-05-21 17:43:48 768
en4 English sdnr1 2018-05-21 16:42:51 460
en3 English sdnr1 2018-05-21 16:23:38 305
en2 English sdnr1 2018-05-21 15:58:49 12
en1 English sdnr1 2018-05-21 15:47:22 1023 Initial revision (saved to drafts)