Skip to content

Conversation

kenzonobre
Copy link

Describe your change:

Add unbounded knapsack algorithm, which is an extension of the 0-1 knapsack problem.

0-1 Knapsack Problem :
https://en.wikipedia.org/wiki/Knapsack_problem
https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/

Unbounded knapsack :
https://www.geeksforgeeks.org/unbounded-knapsack-repetition-items-allowed/

  • 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 commit message contains Fixes: #{$ISSUE_NO}.
@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Nov 21, 2022
@kenzonobre
Copy link
Author

@cclauss could you please review my code? I could work on it more if necessary, but I think it looks good

Copy link
Contributor

@CaedenPH CaedenPH left a comment

Choose a reason for hiding this comment

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

Looks good to me, but I'm sure Cclauss will find some problems ;)

@kenzonobre
Copy link
Author

@cclauss could you please review my code? I could work on it more if necessary, but I think it looks good

Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

  • How is this different than all our other knapsack algorithms?
  • Is it faster? Can we prove that with a benchmark?
  • How is this algorithm unbound if the first parameter is capacity?
  • Please add a test that demonstrates how would this unbound algorithm would deal with weights and values that were never-ending cycles? https://docs.python.org/3/library/itertools.html#itertools.cycle
Comment on lines +63 to +71

for i in range(n):

# weight of the i-th item
weight = items_weights[i]

# value of the i-th item
value = items_values[i]

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for i in range(n):
# weight of the i-th item
weight = items_weights[i]
# value of the i-th item
value = items_values[i]
for weight, value in zip(weights, values):
Comment on lines +54 to +57

# n is the number of items
n = len(items_weights)

Copy link
Member

@cclauss cclauss Jun 6, 2023

Choose a reason for hiding this comment

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

Not needed with zip().

Suggested change
# n is the number of items
n = len(items_weights)
# So dp[c] stores the maximum value of items that can be put in a knapsack
# of a capacity c.
# Initially, dp[c] starts with 0.
dp = [0 for x in range(capacity + 1)]
Copy link
Member

Choose a reason for hiding this comment

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

What does dp stand for? Please make the code more self documenting by expanding the acronym.

Suggested change
dp = [0 for x in range(capacity + 1)]
dp = [0] * (capacity + 1)
Comment on lines +27 to +30
>>> c = 70
>>> items_values = [40, 900, 120]
>>> items_weights = [10, 20, 30]
>>> knapsack(c, items_weights, items_values)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
>>> c = 70
>>> items_values = [40, 900, 120]
>>> items_weights = [10, 20, 30]
>>> knapsack(c, items_weights, items_values)
>>> knapsack(capacity=70, weights=(40, 900, 120), values=(10, 20, 30))

Please repeat for the other tests.

Please add tests for:
capacity = 0
capacity - -5
len(values) != len(weights)
len(values) == 5 and len(weights) == 0
etc.

"""


def knapsack(capacity: int, items_weights: list[int], items_values: list[int]) -> int:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def knapsack(capacity: int, items_weights: list[int], items_values: list[int]) -> int:
def knapsack(capacity: int, weights: list[int], values: list[int]) -> int:
@cclauss cclauss closed this Sep 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed

3 participants