Skip to content

Commit

Permalink
max_sub_array.py: Add Maximum Subarray
Browse files Browse the repository at this point in the history
Divide and Conquer method to solve maximum subarray problem

closes NITSkmOS#215
  • Loading branch information
halderjoydeep committed Oct 7, 2018
1 parent e72f3e1 commit 7ce94f9
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions knapsack_problem/Python/knapsack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
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 vist- <https://en.wikipedia.org/wiki/Knapsack_problem>
"""


def knapsack(profit, weight, capacity):
"""
: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 7ce94f9

Please sign in to comment.