Teaching Kids Programming – 0/1 Knapsack Space Optimised 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?

Space-optimised Dynamic Programming Algorithm to Solve 0/1 Knapsack

Given tex_7ef280cc103443dd89d9c71c62062ae7 Teaching Kids Programming - 0/1 Knapsack Space Optimised Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video items to pack in a knapsack with capacity tex_0b052539c069727cbdf2c2a2d2bfe458 Teaching Kids Programming - 0/1 Knapsack Space Optimised Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video . Each item has weight tex_13af851830f423424a0cc5781793e616 Teaching Kids Programming - 0/1 Knapsack Space Optimised Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video and value tex_5a69c9be37e072539414f87f53d4917e Teaching Kids Programming - 0/1 Knapsack Space Optimised Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video . We want to pack the items to gain the maximum value but the total weights from the chosen items should not exceed the knapsack capacity.

Let’s state this mathematically:

We want to maximize the tex_819508cac9165e4b84c1d6bfee2b6ea5 Teaching Kids Programming - 0/1 Knapsack Space Optimised Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video ,
subject to tex_eca22ac25b3af2c4d6e7a3083c14e718 Teaching Kids Programming - 0/1 Knapsack Space Optimised Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video ,
and tex_e2e72216005bdac47cc59d581b1ef91d Teaching Kids Programming - 0/1 Knapsack Space Optimised Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video

When tex_8dc32faf6e38ca39e5b88e7bee1f84e8 Teaching Kids Programming - 0/1 Knapsack Space Optimised Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video means that we don’t pick the i-th item while tex_fa7b4554c5e909c1877b9366b1a7eea5 Teaching Kids Programming - 0/1 Knapsack Space Optimised Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video means we pack i-th item in the knapsack.

The Dynamic Programming Equation is:

tex_5d09b991193609efc249b32af4365752 Teaching Kids Programming - 0/1 Knapsack Space Optimised Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video for tex_50ca2e63a7796840b5a02c35a3f4e6de Teaching Kids Programming - 0/1 Knapsack Space Optimised Dynamic Programming Algorithm algorithms dynamic programming Dynamic Programming Knapsack Problems math python teaching kids programming youtube video .

The top down DP uses the Recursion with Memoization aka the magic @cache keyword which asks the computer to remember the intermediate values. The bottom up DP uses two dimensional array dp[i][c] to store the values proactively. For Top Down DP, we call the dp(n-1, c) which will be expanded top down, while the bottom up DP computes the dp[0][0..c] (first row) and then compute the second row dp[1], the third row dp[2] until we get the value dp[n-1][c].

However, dp[i] is based on dp[i-1] only, and thus we can compress the space. That will optimise the space usage from O(CN) to O(C). The inner loop j (capacity) should be inversely enumerated (downwards) to avoid dp[0..j] is overwritten.

1
2
3
4
5
6
7
8
class Solution:
    def solve(self, weights, values, capacity):
        n = len(values)
        dp = [0 for _ in range(capacity + 1)]
        for i in range(n):
            for j in range(capacity, weights[i] - 1, -1):
                dp[j] = max(dp[j], dp[j - weights[i]] + values[i])
        return dp[capacity]
class Solution:
    def solve(self, weights, values, capacity):
        n = len(values)
        dp = [0 for _ in range(capacity + 1)]
        for i in range(n):
            for j in range(capacity, weights[i] - 1, -1):
                dp[j] = max(dp[j], dp[j - weights[i]] + values[i])
        return dp[capacity]

The time complexity is still O(CN).

Knapsack Problems

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
947 words
Last Post: Teaching Kids Programming - Using Bottom Up Dynamic Programming Algorithm to Solve 0/1 Knapsack Problem
Next Post: Teaching Kids Programming - Split String with Same Distinct Counts (Sliding Window)

The Permanent URL is: Teaching Kids Programming – 0/1 Knapsack Space Optimised Dynamic Programming Algorithm

Leave a Reply