Teaching Kids Programming – 0/1 Knapsack Problem via Top Down Dynamic Programming Algorithm


Teaching Kids Programming: Videos on Data Structures and Algorithms

You are given two lists of integers weights and values which have the same length and an integer capacity. weights[i] and values[i] represent the weight and value of the ith item. Given that you can take at most capacity weights, and that you can only take at most one copy of each item, return the maximum amount of value you can get.

Constraints
n ≤ 250 where n is the length of weights and values
capacity ≤ 250

Example 1
Input
weights = [1, 2, 3]
values = [1, 5, 3]
capacity = 5
Output
8
Similar to original knapsack, but how do you ensure the specific element is only included once?

0/1 Knapsack Problem via Top Down Dynamic Programming Algorithm

This problem can be expressed in the following math terms:

Given N items: tex_13af851830f423424a0cc5781793e616 Teaching Kids Programming - 0/1 Knapsack Problem via Top Down Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video (i from 0 to N-1 inclusive) represents the weight for each item.
tex_5a69c9be37e072539414f87f53d4917e Teaching Kids Programming - 0/1 Knapsack Problem via Top Down Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video (i from 0 to N-1 inclusive) is the value for each item.
And tex_e2e72216005bdac47cc59d581b1ef91d Teaching Kids Programming - 0/1 Knapsack Problem via Top Down Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video where 0 means we do not pick i-th item and 1 means we pick it.

We want to maximize the profit tex_4ea6a5c68caf2140b3a5cb9c0a69e447 Teaching Kids Programming - 0/1 Knapsack Problem via Top Down Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video
subject to tex_d1421506604cb47ce434ce135f81a111 Teaching Kids Programming - 0/1 Knapsack Problem via Top Down Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video where C is the maximum capacity of the knapsack

The greedy approach (Picking the most valuable items) does not work: for example we could have a item which exceeds the capacity but very valuable. Another greedy approach would be to pick the items with the most value/weight ratio but this doesn’t work either considering the following:

weights = [1, 2, 3] and values = [2, 5, 3], capacity = 5
so the v2w wouild be [2, 2.5, 1] and we need to pick the first two items according to the strategy which will give us total value = 7 and remaining capacity is 5-(1+2)=2
The optimal would be to pick last two items total weight = 2+3=5 (less or equal to total capacity 5) and total value is 5+3=8

We can bruteforce – there are tex_7da98591b3d78eeec743ae932ec8bcb8 Teaching Kids Programming - 0/1 Knapsack Problem via Top Down Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video subsequences with each item we can pick or not pick. And it takes O(N) time to compute the total weight thus total time complexity is tex_d7260183e465ed3510aa03053e9d1d2c Teaching Kids Programming - 0/1 Knapsack Problem via Top Down Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video which is exponential.

We can use the Top Down Dynamic Programming Algorithm to compute the maximal value we can get – each item we have two choices pick or not pick, and we can remember the max values at that point – via the Recursion + Memoization aka @cache.

If the remaining capacity is larger than the current items’ weight – we can try to pick this item. dfs(i, c) means we are making a decision to pick i-th item when the knapsack has c remaining capacity.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
    def knapsack01(self, weights, values, capacity):
        
        @cache
        def dfs(i, c):
            if i < 0:
                return 0
            ans = 0
            if c >= weights[i]:
                ans = max(ans, dfs(i - 1, c - weights[i]) + values[i])
            ans = max(ans, dfs(i - 1, c))
            return ans
 
        return dfs(len(values) - 1, capacity)
class Solution:
    def knapsack01(self, weights, values, capacity):
        
        @cache
        def dfs(i, c):
            if i < 0:
                return 0
            ans = 0
            if c >= weights[i]:
                ans = max(ans, dfs(i - 1, c - weights[i]) + values[i])
            ans = max(ans, dfs(i - 1, c))
            return ans

        return dfs(len(values) - 1, capacity)

We can start from the index 0 – first item, and it would work too.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution:
    def knapsack01(self, weights, values, capacity):
        
        @cache
        def dfs(i, c):
            if i == len(values):
                return 0
            ans = 0
            if c >= weights[i]:
                ans = max(ans, dfs(i + 1, c - weights[i]) + values[i])
            ans = max(ans, dfs(i + 1, c))
            return ans
 
        return dfs(0, capacity)
class Solution:
    def knapsack01(self, weights, values, capacity):
        
        @cache
        def dfs(i, c):
            if i == len(values):
                return 0
            ans = 0
            if c >= weights[i]:
                ans = max(ans, dfs(i + 1, c - weights[i]) + values[i])
            ans = max(ans, dfs(i + 1, c))
            return ans

        return dfs(0, capacity)

The time complexity and space complexity is O(CN) as we are using @cache and each state is calculated only once.

Knapsack Problems

We can also solve this 0/1 Knapsack problem via Bottom Up Dynamic Programming Algorithm where we proactively compute the values and store in a two dimensional array in the bottom up manner e.g. i=0,j=0 dp[i][j]: Teaching Kids Programming – 0/1 Knapsack Problem via Bottom Up Dynamic Programming Algorithm

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
1033 words
Last Post: Teaching Kids Programming - Proof of Rule: Integer Divisible By 3
Next Post: Teaching Kids Programming - Using Bottom Up Dynamic Programming Algorithm to Solve 0/1 Knapsack Problem

The Permanent URL is: Teaching Kids Programming – 0/1 Knapsack Problem via Top Down Dynamic Programming Algorithm

Leave a Reply