Dynamic Programming: The Knapsack Problem!
One fun problem is the Knapsack problem. In this post, I'm going to talk about the following version: The problem: Given a set of items with specific values and integer weights, and a knapsack of capacity C, find the maximum value of the set of items we can fit into the knapsack. The approach: Let's call K(i, j) the max value of the subset of items from 1 through i that we can fit into a knapsack of capacity j. We can reduce this problem into a subproblem in several ways... 1) If i = 0, then we don't have any items, so the max value will be 0 . 2) If j = 0, then we can't put any items into our knapsack because it has zero capacity. Then, the max value will be 0 . 3) If the weight of the ith item is bigger than our capacity j, then we can just chuck our ith item since we can't put it into our knapsack at all. We can just work with the first i-1 items. Then K(i, j) is reduced to K(i-1, j) . 4) If we can fit our ith item into our knapsack ...