-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfractional_knapsack.py
More file actions
43 lines (33 loc) · 916 Bytes
/
fractional_knapsack.py
File metadata and controls
43 lines (33 loc) · 916 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Structure for an item which stores weight and
# corresponding value of Item
class Item:
def __init__(self, profit, weight):
self.profit = profit
self.weight = weight
# Main greedy function to solve problem
def fractionalKnapsack(W, arr):
# Sorting Item on basis of ratio
arr.sort(key=lambda x: (x.profit/x.weight), reverse=True)
# Result(value in Knapsack)
finalvalue = 0.0
# Looping through all Items
for item in arr:
# If adding Item won't overflow,
# add it completely
if item.weight <= W:
W -= item.weight
finalvalue += item.profit
# If we can't add current Item,
# add fractional part of it
else:
finalvalue += item.profit * W / item.weight
break
# Returning final value
return finalvalue
# Driver Code
if __name__ == "__main__":
W = 50
arr = [Item(60, 10), Item(100, 20), Item(120, 30)]
# Function call
max_val = fractionalKnapsack(W, arr)
print(max_val)