From 12d4f1e5db0dd95ac3fe617b1b20f9c7dae4db55 Mon Sep 17 00:00:00 2001 From: Dhruv Agarwal <92450489+Dhruv127@users.noreply.github.com> Date: Sun, 1 Oct 2023 18:32:09 +0530 Subject: [PATCH] Create knapsack_solver_dp.py --- knapsack/knapsack_solver_dp.py | 49 ++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 knapsack/knapsack_solver_dp.py diff --git a/knapsack/knapsack_solver_dp.py b/knapsack/knapsack_solver_dp.py new file mode 100644 index 000000000000..75ed0a74dfd3 --- /dev/null +++ b/knapsack/knapsack_solver_dp.py @@ -0,0 +1,49 @@ +""" +Optimized Dynamic Programming Solution for the 0-1 Knapsack Problem + +This implementation finds the maximum value that can be put in a knapsack of capacity `capacity`, +given a list of item weights and their corresponding values. + +Reference: https://en.wikipedia.org/wiki/Knapsack_problem + +Usage: +>>> knapsack(50, [10, 20, 30], [60, 100, 120]) +220 +""" + +def knapsack(capacity: int, weights: list[int], values: list[int]) -> int: + """ + Returns the maximum value that can be put in a knapsack of capacity `capacity`, + whereby each weight `weights[i]` has a corresponding value `values[i]`. + + :param capacity: The capacity of the knapsack. + :param weights: A list of weights for items. + :param values: A list of values corresponding to items. + :return: The maximum value that can be obtained. + >>> knapsack(50, [10, 20, 30], [60, 100, 120]) + 220 + """ + n = len(values) + + # Create a DP table with dimensions (n+1) x (capacity+1) + dp = [[0 for _ in range(capacity + 1)] for _ in range(n + 1)] + + for i in range(n + 1): + for w in range(capacity + 1): + if i == 0 or w == 0: + dp[i][w] = 0 + elif weights[i - 1] <= w: + # If the current item can fit in the knapsack + dp[i][w] = max(values[i - 1] + dp[i - 1][w - weights[i - 1]], dp[i - 1][w]) + else: + # If the current item is too heavy + dp[i][w] = dp[i - 1][w] + + return dp[n][capacity] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() +