Add space optimized 0/1 knapsack implementation (O(W) space)#14537
Open
nickzerjeski wants to merge 3 commits intoTheAlgorithms:masterfrom
Open
Add space optimized 0/1 knapsack implementation (O(W) space)#14537nickzerjeski wants to merge 3 commits intoTheAlgorithms:masterfrom
nickzerjeski wants to merge 3 commits intoTheAlgorithms:masterfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new space-optimized 0/1 knapsack solver to the existing dynamic programming implementations, reducing auxiliary memory from a 2D table to a single 1D DP array while keeping the same time complexity.
Changes:
- Added
knapsack_space_optimized(capacity, weights, values, num_items) -> intusing 1D DP with reverse-capacity iteration. - Added basic input validation for negative
capacity/num_itemsandnum_itemsbounds. - Added doctests for a few representative successful scenarios.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+66
to
+72
| for item_index in range(num_items): | ||
| item_weight = weights[item_index] | ||
| item_value = values[item_index] | ||
| for current_capacity in range(capacity, item_weight - 1, -1): | ||
| dp[current_capacity] = max( | ||
| dp[current_capacity], item_value + dp[current_capacity - item_weight] | ||
| ) |
Comment on lines
+58
to
+63
| if num_items < 0: | ||
| raise ValueError("The number of items cannot be negative.") | ||
| if capacity < 0: | ||
| raise ValueError("The knapsack capacity cannot be negative.") | ||
| if num_items > len(weights) or num_items > len(values): | ||
| raise ValueError("The number of items exceeds the provided input lengths.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds a space-optimized 0/1 knapsack implementation using a single 1D DP array, reducing auxiliary space from
O(n * W)toO(W)while preservingO(n * W)time complexity.What Changed
knapsack_space_optimized(capacity, weights, values, num_items) -> inttodynamic_programming/knapsack.py.num_itemsbounds.Issue Comments Review
I reviewed issue #14462 comments before implementation. There are currently no comments, so this follows the issue specification directly.
Validation
python -m doctest -v dynamic_programming/knapsack.py(pass)220.Issue
Closes #14462