Teaching Kids Programming – Using Bottom Up Dynamic Programming Algorithm to Solve 0/1 Knapsack Problem


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 Bottom Up Dynamic Programming Algorithm

We can describe the 0/1 Knapsack problem via the following Math terms:

Given a knapsack with capacity tex_0b052539c069727cbdf2c2a2d2bfe458 Teaching Kids Programming - Using Bottom Up Dynamic Programming Algorithm to Solve 0/1 Knapsack Problem algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video and tex_7ef280cc103443dd89d9c71c62062ae7 Teaching Kids Programming - Using Bottom Up Dynamic Programming Algorithm to Solve 0/1 Knapsack Problem algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video items to pack with weights tex_13af851830f423424a0cc5781793e616 Teaching Kids Programming - Using Bottom Up Dynamic Programming Algorithm to Solve 0/1 Knapsack Problem algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video and values tex_5a69c9be37e072539414f87f53d4917e Teaching Kids Programming - Using Bottom Up Dynamic Programming Algorithm to Solve 0/1 Knapsack Problem algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video .
We want to pack items into the knapsack subject to total weights not exceeding the capacity.

tex_d1421506604cb47ce434ce135f81a111 Teaching Kids Programming - Using Bottom Up Dynamic Programming Algorithm to Solve 0/1 Knapsack Problem algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video
tex_e2e72216005bdac47cc59d581b1ef91d Teaching Kids Programming - Using Bottom Up Dynamic Programming Algorithm to Solve 0/1 Knapsack Problem algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video meaning the quantity of each item we are to pick.
We want to meantime maximize the values tex_4ea6a5c68caf2140b3a5cb9c0a69e447 Teaching Kids Programming - Using Bottom Up Dynamic Programming Algorithm to Solve 0/1 Knapsack Problem algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video .

The Dynamic Programming Equation is:

If tex_58f6e18d7a991c1c491a30dbe3c4e21e Teaching Kids Programming - Using Bottom Up Dynamic Programming Algorithm to Solve 0/1 Knapsack Problem algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video the capacity j is enough to pick item i. tex_4fe57ae522758e1a1b75c4b08c6f7c92 Teaching Kids Programming - Using Bottom Up Dynamic Programming Algorithm to Solve 0/1 Knapsack Problem algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video
Otherwise tex_4ac0fad32634c879c1b87b32135a1bd1 Teaching Kids Programming - Using Bottom Up Dynamic Programming Algorithm to Solve 0/1 Knapsack Problem algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video

We can solve this DP using Top Down Dynamic Programming aka Recursion with Memoization: Teaching Kids Programming – 0/1 Knapsack Problem via Top Down Dynamic Programming Algorithm

Alternatively, we can proactively store the values in a two dimensional array. We need to compute the values of the first row e.g. dp[0][i] for i in range(c). If i (capacity) is larger or equal than the first item’s weight, we can set the value to v[0]. Otherwise, the value gain would be zero.

Then, we start filling the values from the second row up to the last row e.g. dp[n-1].

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
    def knapsack01(self, weights, values, capacity):
        n = len(weights)
 
        dp = [[0 for _ in range(capacity + 1)] for _ in range(n)]
        for i in range(capacity + 1):
            dp[0][i] = 0 if weights[0] > i else values[0]
 
        for i in range(1, n):
            for j in range(1, capacity + 1):
                dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weights[i]] + values[i] if j >= weights[i] else 0)
 
        return dp[n - 1][capacity]
class Solution:
    def knapsack01(self, weights, values, capacity):
        n = len(weights)

        dp = [[0 for _ in range(capacity + 1)] for _ in range(n)]
        for i in range(capacity + 1):
            dp[0][i] = 0 if weights[0] > i else values[0]

        for i in range(1, n):
            for j in range(1, capacity + 1):
                dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weights[i]] + values[i] if j >= weights[i] else 0)

        return dp[n - 1][capacity]

The time and space complexity is O(NC) – same as Top Down Dynamic Programming Implementation. However, practically speaking, the Bottom Up Dynamic Programming is faster.

Knapsack Problems

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
913 words
Last Post: Teaching Kids Programming - 0/1 Knapsack Problem via Top Down Dynamic Programming Algorithm
Next Post: Teaching Kids Programming - 0/1 Knapsack Space Optimised Dynamic Programming Algorithm

The Permanent URL is: Teaching Kids Programming – Using Bottom Up Dynamic Programming Algorithm to Solve 0/1 Knapsack Problem

Leave a Reply