-
-
Notifications
You must be signed in to change notification settings - Fork 766
fixes #110 #148
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
Merged
fixes #110 #148
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Knapsack Problem (Greedy algorithm) | ||
|
||
#### Problem Statement | ||
|
||
Given a set of items, each with weight and a value, determine the number of each item included in a collection so that the total weight is less than or equal to the given limit and the total value is as large as possible. | ||
|
||
##### Greedy method will always provide an optimal solution with fractional knapsack problem. | ||
|
||
#### Time Complexity | ||
|
||
O(log n) Worst Case | ||
Swastyy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
O(1) Best Case (If middle element of initial array is the target element) | ||
Swastyy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#### Space Complexity | ||
|
||
O(1) For iterative approach | ||
O(1) For recursive approach *if tail call optimization is used*, O(log n) due to recursion call stack, otherwise | ||
|
||
#### Example | ||
|
||
``` | ||
Lets assume the capacity of knapsack, W = 60 | ||
value = [280, 100, 120, 120] | ||
weight = [40, 10, 20, 24] | ||
|
||
Ratio(V/W) = 7,10,6,5 | ||
Say those items as A,B,C,D | ||
next, the items should be sorted in descending order based on the ratio of value by weight to get maximum profit | ||
First and foremost, B was picked since its weight is smaller than the knapsack's capacity. The next item, A, is chosen since the knapsack's available capacity is more than A's weight. C is now the next item on the list. However, the entire item cannot be chosen because the knapsack's remaining capacity is less than C's weight. | ||
As a result, the C proportion (60–50)/20) | ||
The knapsack's capacity is now equal to the specified items. | ||
As a result, no more items can be chosen. | ||
|
||
10 + 40 + 20*(10/20) = 60 is the total weight of the chosen goods. | ||
|
||
100+280+120*(10/20)=380+60=440 is the total profit. | ||
|
||
This is the most suitable option. | ||
|
||
We won't be able to make more money by combining diverse things. | ||
|
||
``` | ||
|
||
#### Code Implementation Links | ||
|
||
- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/greedy_algorithms/knapsack.cpp) | ||
- [Python](https://github.com/TheAlgorithms/Python/tree/master/knapsack) | ||
- [C-Sharp](https://github.com/TheAlgorithms/C-Sharp/tree/master/Algorithms/Knapsack) | ||
|
||
#### Video Explanation | ||
|
||
[A CS50 video explaining the Greedy Algorithm](https://www.youtube.com/watch?v=Ou9OA0yQCYA) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 supposed to be a heading. Use a different way of emphasis. Please explicitly state that it often finds a suboptimal solution for nonfractional knapsack problems.