-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,31 @@ | ||
def knapsack(v, w, n, W): | ||
''' | ||
Function solves 0/1 Knapsack problem | ||
Function to calculate frequency of items to be added to knapsack to maximise value | ||
such that total weight is less than or equal to W | ||
:param v: set of values of n objects, first element is useless because relevant values are assumed to begin from index 1 | ||
:param w: set of weights of n objects, first element is useless because relevant weights are assumed to begin from index 1 | ||
:param n: number of items | ||
:pram W: maximum weight allowd by knapsack | ||
''' | ||
m = [[0 for j in range(W+1)] for i in range(n+1)] | ||
for j in range(W+1): | ||
m[0][j] = 0 | ||
|
||
for i in range(1, n+1): | ||
for j in range(W+1): | ||
if w[i]>j: | ||
m[i][j] = m[i-1][j] | ||
else: | ||
m[i][j] = max(m[i-1][j], m[i-1][j-w[i]]+v[i]) | ||
return m[n][W] | ||
|
||
def main(): | ||
v = [0, 1, 2, 3, 4, 5] | ||
w = [0, 5, 6, 2, 3, 4] | ||
n = 5 | ||
W = 8 | ||
print("Answer to 0/1 Knapsack problem(Maximum value possible in given weight limit): "+str(knapsack(v, w, n, W))) | ||
|
||
if __name__ == '__main__': | ||
main() |
5f84fd2
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.
Comment on 5f84fd2.
There are 25 results for the section all.pyjava. They have been shortened and will not be shown inline because they are more than 10.
Until GitMate provides an online UI to show a better overview, you can run coala locally for more details.
5f84fd2
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.
Comment on 5f84fd2.
There are 35 results for the section all.autopep8. They have been shortened and will not be shown inline because they are more than 10.
Until GitMate provides an online UI to show a better overview, you can run coala locally for more details.