Add knapsack variant to count optimal-value subsets#14536
Open
nickzerjeski wants to merge 3 commits intoTheAlgorithms:masterfrom
Open
Add knapsack variant to count optimal-value subsets#14536nickzerjeski 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 knapsack API that returns both the optimal (max) achievable value and the number of distinct optimal selections, extending the existing memoized recursive approach in knapsack/knapsack.py.
Changes:
- Added
knapsack_with_count(...)-> tuple[int, int]that tracks(max_value, count)and aggregates counts on ties. - Added doctests for
knapsack_with_countinknapsack/knapsack.py. - Added unit tests for baseline and tie-counting behavior (0-1 and unbounded variants).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| knapsack/knapsack.py | Introduces knapsack_with_count and adds typing for allow_repetition in knapsack() |
| knapsack/tests/test_knapsack.py | Adds tests validating returned (max_value, count) for standard and tie scenarios |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+72
to
+77
| """ | ||
| Return both the maximum knapsack value and the number of optimal subsets. | ||
|
|
||
| The return value is ``(max_value, number_of_optimal_subsets)``. | ||
| If multiple choices produce the same maximum value, their counts are added. | ||
|
|
knapsack/knapsack.py
Outdated
Comment on lines
+92
to
+93
| @lru_cache | ||
| def knapsack_recur(remaining_capacity: int, item_count: int) -> tuple[int, int]: |
Comment on lines
+61
to
+70
| def test_knapsack_with_count(self): | ||
| """ | ||
| test for maximum value and number of optimal subsets | ||
| """ | ||
| cap = 50 | ||
| val = [60, 100, 120] | ||
| w = [10, 20, 30] | ||
| c = len(val) | ||
| assert k.knapsack_with_count(cap, w, val, c) == (220, 1) | ||
| assert k.knapsack_with_count(cap, w, val, c, True) == (300, 1) |
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 new knapsack variation that returns both:
It implements issue #14463 on top of the existing recursive memoized approach in
knapsack/knapsack.py.What Changed
knapsack_with_count(...) -> tuple[int, int]inknapsack/knapsack.py.functools.lru_cache) and extended DP state to track(value, count).0-1knapsack (allow_repetition=False)0-N/ unbounded knapsack (allow_repetition=True)allow_repetitioninknapsack().knapsack_with_count.Issue Discussion Alignment
I reviewed issue comments and incorporated the suggested memoized
(value, count)approach, including aggregation of counts when optimal values tie.Credit: approach inspiration from issue comment by @Mrudul1234.
Validation
python -m doctest -v knapsack/knapsack.py(pass)python -m unittest knapsack.tests.test_knapsack -v(pass)Issue
Closes #14463