-
- Notifications
You must be signed in to change notification settings - Fork 48.9k
Add unbounded_knapsack.py #7999
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
Conversation
@cclauss could you please review my code? I could work on it more if necessary, but I think it looks good |
There was a problem hiding this 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 ;)
@cclauss could you please review my code? I could work on it more if necessary, but I think it looks good |
There was a problem hiding this 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
andvalues
that were never-endingcycles
? https://docs.python.org/3/library/itertools.html#itertools.cycle
| ||
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] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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): |
| ||
# n is the number of items | ||
n = len(items_weights) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed with zip()
.
# 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)] |
There was a problem hiding this comment.
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.
dp = [0 for x in range(capacity + 1)] | |
dp = [0] * (capacity + 1) |
>>> c = 70 | ||
>>> items_values = [40, 900, 120] | ||
>>> items_weights = [10, 20, 30] | ||
>>> knapsack(c, items_weights, items_values) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
>>> 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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def knapsack(capacity: int, items_weights: list[int], items_values: list[int]) -> int: | |
def knapsack(capacity: int, weights: list[int], values: list[int]) -> int: |
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/
Checklist:
Fixes: #{$ISSUE_NO}
.