Suppose I wish to solve 0-1 knapsack but both weights and values of elements can get large. (Weight[i] < 1e5 and Value[i] < 1e9)
Also, number of items is ~2000 and size of knapsack is ~4e6.
Obviously dp[index][weight] exceeds memory and time expectations.
However, somewhat magically memoization dp (values stored in std::map or std::unordered_map) works well. I can see why it may be bit faster: after all, in recursive dp, we compute only the states we need.
But, is it actually significantly faster? In other words, the usual computation requires 8e9 operations, but how much speedup can we expect, on average, by computing dp this way?
Since computing a state takes constant time, the question boils down to - how many (expected / on average) states can be there to compute? (normally it's 8e9)
You can assume:
We use
std::unordered_map.Our hash function works well enough.
Recursive Overheads can be ignored.
Thanks!