Skip to content

Add knapsack variant to count optimal-value subsets#14536

Open
nickzerjeski wants to merge 3 commits intoTheAlgorithms:masterfrom
nickzerjeski:feat-14463-knapsack-count
Open

Add knapsack variant to count optimal-value subsets#14536
nickzerjeski wants to merge 3 commits intoTheAlgorithms:masterfrom
nickzerjeski:feat-14463-knapsack-count

Conversation

@nickzerjeski
Copy link
Copy Markdown

Summary

This PR adds a new knapsack variation that returns both:

  • the maximum achievable value, and
  • the number of distinct subsets that achieve that maximum.

It implements issue #14463 on top of the existing recursive memoized approach in knapsack/knapsack.py.

What Changed

  • Added knapsack_with_count(...) -> tuple[int, int] in knapsack/knapsack.py.
  • Reused memoized recursion (functools.lru_cache) and extended DP state to track (value, count).
  • Added tie handling so when include/exclude choices produce equal best values, counts are aggregated.
  • Preserved support for both:
    • 0-1 knapsack (allow_repetition=False)
    • 0-N / unbounded knapsack (allow_repetition=True)
  • Added type hints for the new API and for allow_repetition in knapsack().
  • Added doctests for knapsack_with_count.
  • Added unit tests covering:
    • baseline max-value/count behavior
    • tie scenarios for both no-repetition and repetition variants.

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

Copilot AI review requested due to automatic review settings April 11, 2026 14:16
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_count in knapsack/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.

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)
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Apr 11, 2026
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Apr 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add implementation to count number of subsets achieving maximum knapsack value

2 participants