From c5fb8dbc07210f902a6c6b9af356e2e3e96df3c0 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Tue, 24 Oct 2023 17:58:07 +0530 Subject: [PATCH 1/2] Enhance readability of ZeroOneKnapsack.js --- Dynamic-Programming/ZeroOneKnapsack.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/Dynamic-Programming/ZeroOneKnapsack.js b/Dynamic-Programming/ZeroOneKnapsack.js index 3913d016b3..73a626bb54 100644 --- a/Dynamic-Programming/ZeroOneKnapsack.js +++ b/Dynamic-Programming/ZeroOneKnapsack.js @@ -3,24 +3,34 @@ * https://en.wikipedia.org/wiki/Knapsack_problem */ +// Memoization Approach (Top Down) for calculating Zero One Knapsack +// Time Complexity: O(n*cap) +// Space Complexity: O(n*cap) const zeroOneKnapsack = (arr, n, cap, cache) => { + // Base Case () if (cap === 0 || n === 0) { cache[n][cap] = 0 return cache[n][cap] } + + // Lookup (value already calculated) if (cache[n][cap] !== -1) { return cache[n][cap] } + + // Exclude the nth item + let notPick = zeroOneKnapsack(arr, n - 1, cap, cache) + + // Include the nth item + let pick = 0 if (arr[n - 1][0] <= cap) { - cache[n][cap] = Math.max( - arr[n - 1][1] + zeroOneKnapsack(arr, n - 1, cap - arr[n - 1][0], cache), - zeroOneKnapsack(arr, n - 1, cap, cache) - ) - return cache[n][cap] - } else { - cache[n][cap] = zeroOneKnapsack(arr, n - 1, cap, cache) - return cache[n][cap] + // If weight of the nth item is within the capacity + pick = + arr[n - 1][1] + zeroOneKnapsack(arr, n - 1, cap - arr[n - 1][0], cache) } + + cache[n][cap] = Math.max(pick, notPick) + return cache[n][cap] } const example = () => { From c12bb16d2ca44a8253dd2441272a2ba1ec6914a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sun, 29 Oct 2023 17:37:09 +0100 Subject: [PATCH 2/2] Update ZeroOneKnapsack.js --- Dynamic-Programming/ZeroOneKnapsack.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Dynamic-Programming/ZeroOneKnapsack.js b/Dynamic-Programming/ZeroOneKnapsack.js index 73a626bb54..52a06aa130 100644 --- a/Dynamic-Programming/ZeroOneKnapsack.js +++ b/Dynamic-Programming/ZeroOneKnapsack.js @@ -1,13 +1,11 @@ /** * A Dynamic Programming based solution for calculating Zero One Knapsack * https://en.wikipedia.org/wiki/Knapsack_problem + * + * Time and Space Complexity: O(n*cap) */ - -// Memoization Approach (Top Down) for calculating Zero One Knapsack -// Time Complexity: O(n*cap) -// Space Complexity: O(n*cap) const zeroOneKnapsack = (arr, n, cap, cache) => { - // Base Case () + // Base Case: No capacity or no items if (cap === 0 || n === 0) { cache[n][cap] = 0 return cache[n][cap] @@ -18,10 +16,10 @@ const zeroOneKnapsack = (arr, n, cap, cache) => { return cache[n][cap] } - // Exclude the nth item + // Profit when excluding the nth item let notPick = zeroOneKnapsack(arr, n - 1, cap, cache) - // Include the nth item + // Profit when including the nth item let pick = 0 if (arr[n - 1][0] <= cap) { // If weight of the nth item is within the capacity @@ -29,7 +27,7 @@ const zeroOneKnapsack = (arr, n, cap, cache) => { arr[n - 1][1] + zeroOneKnapsack(arr, n - 1, cap - arr[n - 1][0], cache) } - cache[n][cap] = Math.max(pick, notPick) + cache[n][cap] = Math.max(pick, notPick) // maximize profit return cache[n][cap] }