The Wayback Machine - https://web.archive.org/web/20250512030107/https://github.com/TheAlgorithms/Python/pull/4566
Skip to content

Added Branch And Bound algorithm to solve 0-1 Knapsack Problem #4566

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

Closed
wants to merge 3 commits into from
Closed

Added Branch And Bound algorithm to solve 0-1 Knapsack Problem #4566

wants to merge 3 commits into from

Conversation

yuanjie8629
Copy link

@yuanjie8629 yuanjie8629 commented Jul 22, 2021

Describe your change:

Added Branch And Bound Algorithm to solve 0-1 Knapsack Problem

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • . #4774

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 have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.
@ghost ghost added documentation This PR modified documentation files require type hints https://docs.python.org/3/library/typing.html labels Jul 22, 2021
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

@ghost ghost added the awaiting reviews This PR is ready to be reviewed label Jul 22, 2021
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

@ghost ghost removed the require type hints https://docs.python.org/3/library/typing.html label Jul 25, 2021
Copy link

@nivedha02 nivedha02 left a comment

Choose a reason for hiding this comment

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

well done

Copy link

@nivedha02 nivedha02 left a comment

Choose a reason for hiding this comment

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

Great work

@stale
Copy link

stale bot commented Sep 19, 2021

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Used to mark an issue or pull request stale. label Sep 19, 2021
@poyea
Copy link
Member

poyea commented Sep 22, 2021

@algorithms-keeper review-all

@stale stale bot removed the stale Used to mark an issue or pull request stale. label Sep 22, 2021
As 0/1 Knapsack is about maximizing the total value, we cannot directly use the LC Branch and Bound technique to solve this. Instead, we convert this into a minimization problem by taking negative of the given values.
Follow the steps below to solve the problem:

1. Sort the items based on their value/weight(V/W) ratio.
Copy link
Member

Choose a reason for hiding this comment

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

It'd be helpful if we make it `V` and `W`. Same for all other variables like minLB -> `minLB` -> minLB


## Tests

To be completed.
Copy link
Member

Choose a reason for hiding this comment

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

I think you've completed?

Comment on lines +6 to +22
"""
We are planning for a picnic.
There are some items we would like to carry.
Our knapsack is only able to carry 15 pounds of items.
The items are given with some ratings from 1 to 10 to indicates how strongly we want to include the particular item in knapsack for the picnic.
Once the item is included in the knapsack, the same item cannot be carried again.

There are total of 6 items as follows:
1. Ant Repellent,1,2
2. Blanket,4,3
3. Brownies,3,10
4. Frisbee,1,6
5. Salad,5,4
6. Watermelon,10,10

Calculate the maximum rating and total weight that the knapsack can take.
"""
Copy link
Member

Choose a reason for hiding this comment

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

Or we can put these in README, making the code itself "self-contained"

def __init__(self, name: str, weight: float, rating: float) -> None:
self._name = name
self._weight = weight
self._rating = rating
Copy link
Member

Choose a reason for hiding this comment

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

ratingmeans value I guess? See if we can align it with the definition in README.

@cclauss cclauss requested a review from poyea October 26, 2021 17:13
@stale
Copy link

stale bot commented Apr 28, 2022

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Used to mark an issue or pull request stale. label Apr 28, 2022
@cclauss
Copy link
Member

cclauss commented Oct 1, 2022

In preparation for that denial-of-service attack that is Hacktoberfest, I am closing all pull requests that do not have passing tests.

@cclauss cclauss closed this Oct 1, 2022
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 documentation This PR modified documentation files stale Used to mark an issue or pull request stale.
4 participants