Skip to content

Commit

Permalink
knapsack.py : Add KnapSack Problem
Browse files Browse the repository at this point in the history
Greedy approach to solve the knapsack problem

closes NITSkmOS#305
  • Loading branch information
halderjoydeep committed Oct 8, 2018
1 parent e72f3e1 commit c5dd9f5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This repository contains examples of various algorithms written on different pro
| [Shell Sort](https://en.wikipedia.org/wiki/Shellsort) | [:octocat:](shell_sort/C) | | | [:octocat:](shell_sort/Python) |
| [Heap Sort](https://en.wikipedia.org/wiki/Heapsort) | | | | [:octocat:](heap_sort/python) |
| [Maximum Subarray Problem](https://en.wikipedia.org/wiki/Maximum_subarray_problem) | | | | [:octocat:](/maximum_subarray/Python)|
| [Knapsack Problem](https://en.wikipedia.org/wiki/Knapsack_problem) | | | | [:octocat:](knapsack_problem/Python)|


## Implemented Data Structures
Expand Down
60 changes: 60 additions & 0 deletions knapsack_problem/Python/knapsack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
def knapsack(profit, weight, capacity):
"""
Here Knapsack problem has been implemented using Greedy Approach
where the weight and and corresponding profit of some items has been
given. And also the maximum capacity of a sack(bag) is given. we have
to take items so that capacity does not exceed and we get maximum profit.
For more information visit: <https://en.wikipedia.org/wiki/Knapsack_problem>
:param profit: array of profit of the items
:param weight: array of weight of the items
:param capacity: capacity of the sack
:return: maximum profit and the fraction of items
"""

# array of profit/weight ratio
ratio = [v / w for v, w in zip(profit, weight)]

# a list of (0, 1, ..., n-1)
index = list(range(len(profit)))

# index is sorted according to ratio in descending order
index.sort(key=lambda i: ratio[i], reverse=True)

# max_profit is the maximum profit gained
max_profit = 0

# fraction is the fraction in which items should be taken
fraction = [0] * len(profit)

for i in index:
if weight[i] <= capacity:
fraction[i] = 1
max_profit += profit[i]
capacity -= weight[i]
else:
fraction[i] = capacity / weight[i]
max_profit += profit[i] * fraction[i]
break

return max_profit, fraction


def main():
# profit is array of profit of the items
# weight is array of weight of the items
# capacity is capacity of the sack

profit = [50, 60, 80]
weight = [10, 30, 20]
capacity = 50

# max_profit is the maximum profit gained
# fraction is the fraction in which items should be taken
max_profit, fraction = knapsack(profit, weight, capacity)
print('Maximum profit:', max_profit)
print('Items should be taken in fraction of:', fraction)


if __name__ == '__main__':
main()

0 comments on commit c5dd9f5

Please sign in to comment.