Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Enhancement of the knapsack algorithm with memorization and generalisation #9295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
MaximSmolskiy merged 15 commits into TheAlgorithms:master from Jiang15:wei_knapsack
Aug 26, 2025

Conversation

Copy link
Contributor

@Jiang15 Jiang15 commented Oct 1, 2023

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a Enchancement of Knapsack algorithm: "Fixes Enhancement of the knapsack algorithm #9266 ".

@algorithms-keeper algorithms-keeper bot added documentation This PR modified documentation files awaiting reviews This PR is ready to be reviewed labels Oct 1, 2023
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Oct 1, 2023
Copy link
Contributor Author

Jiang15 commented Oct 5, 2023

I forgot to add the description of the PR:)

  • In knapsack/knapsack.py, I added a flag - allow_repetition. It can be set to true to allow picking the same item multiple times, and vice versa, only once if it is false.
  • I refactored the naive recursion function to be more readable and added @lru_cache to optimize the time complexity.
  • And simple documentation changes in knapsack/README.MD from 0-1 to 0-N knapsack problem.

I added test cases with allow_repetition = true/false in tests and tests are all passed.

Jiang15 reacted with eyes emoji

Copy link
Contributor

@jeffreyyancey jeffreyyancey left a comment

Choose a reason for hiding this comment

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

A few suggestions, however only two things are blocking an approval from me.

  1. cap and c need to be full names as per the guidelines.
  2. Add a test case that demonstrates that repetition is working.

without_new_value = knapsack(capacity, weights, values, counter - 1)
return max(new_value_included, without_new_value)
@lru_cache
def knapsack_recur(cap: int, c: int) -> int:
Copy link
Contributor

@jeffreyyancey jeffreyyancey Oct 8, 2024

Choose a reason for hiding this comment

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

Using the lru_cache is a nice improvement. I might be worth exploring adding allow_repitition as a parameter, since lru_cache uses these parameters as a part of its memorization key.

without_new_value = knapsack(capacity, weights, values, counter - 1)
return max(new_value_included, without_new_value)
@lru_cache
def knapsack_recur(cap: int, c: int) -> int:
Copy link
Contributor

@jeffreyyancey jeffreyyancey Oct 8, 2024

Choose a reason for hiding this comment

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

cap and c should be given full descriptive names.

300

Given the repetition is allowed,
the result is 300 cause the values of 60*5 (pick 5 times)
which is the limit of the capacity.
"""

Copy link
Contributor

@jeffreyyancey jeffreyyancey Oct 8, 2024

Choose a reason for hiding this comment

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

You might want to consider a few things before getting into the algorithm:

  1. Handle any input constraints. Ensure the inputs make sense and raise an exception for the user if they don't
  • non-positive numbers
  • a capacity lower than the weight of any single object
  • etc
  1. Before getting into the main algorithm, you can go ahead and remove any items that have a weight above the capacity of the knapsack.

300

Given the repetition is allowed,
the result is 300 cause the values of 60*5 (pick 5 times)
Copy link
Contributor

@jeffreyyancey jeffreyyancey Oct 8, 2024

Choose a reason for hiding this comment

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

The algorithm has been updated to allow repetition. An example/testcase where repetition is used would be appropriate.

@algorithms-keeper algorithms-keeper bot added tests are failing Do not merge until tests pass and removed tests are failing Do not merge until tests pass labels Aug 26, 2025
@algorithms-keeper algorithms-keeper bot added tests are failing Do not merge until tests pass labels Aug 26, 2025
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Aug 26, 2025
@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Aug 26, 2025
@MaximSmolskiy MaximSmolskiy merged commit 4961b3a into TheAlgorithms:master Aug 26, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@MaximSmolskiy MaximSmolskiy MaximSmolskiy approved these changes

+1 more reviewer

@jeffreyyancey jeffreyyancey jeffreyyancey requested changes

Reviewers whose approvals may not affect merge requirements
Assignees
No one assigned
Labels
documentation This PR modified documentation files
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Enhancement of the knapsack algorithm

AltStyle によって変換されたページ (->オリジナル) /