Skip to content

Commit 3d6f46a

Browse files
authored
Create fractional_knapsack.py
1 parent 048663f commit 3d6f46a

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Python3 program to solve fractional
2+
# Knapsack Problem
3+
4+
# Problem Statement : Given weights and values of n items,
5+
# we need to put these items in a
6+
# knapsack of capacity W to get the
7+
# maximum total value in the knapsack.
8+
9+
# -----------------------------------------------------------------
10+
11+
# Approach : Brute Force Method
12+
# Abhishek S, 2021
13+
14+
# -----------------------------------------------------------------
15+
16+
# Solution : Using the Brute Force method we can figure out the solution
17+
18+
19+
# This function returns the item values of the data class
20+
class ItemValue:
21+
22+
"""Item Value DataClass"""
23+
24+
def __init__(self, wt, val, ind):
25+
self.wt = wt
26+
self.val = val
27+
self.ind = ind
28+
self.cost = val // wt
29+
30+
def __lt__(self, other):
31+
return self.cost < other.cost
32+
33+
# Greedy Approach
34+
35+
# this function is the main architecture to solve the program.
36+
class FractionalKnapSack:
37+
38+
"""Time Complexity O(n log n)"""
39+
def getMaxValue(wt, val, capacity):
40+
"""function to get maximum value """
41+
iVal = []
42+
for i in range(len(wt)):
43+
iVal.append(ItemValue(wt[i], val[i], i))
44+
45+
# sorting items by value
46+
iVal.sort(reverse=True)
47+
48+
totalValue = 0
49+
for i in iVal:
50+
curWt = int(i.wt)
51+
curVal = int(i.val)
52+
if capacity - curWt >= 0:
53+
capacity -= curWt
54+
totalValue += curVal
55+
else:
56+
fraction = capacity / curWt
57+
totalValue += curVal * fraction
58+
capacity = int(capacity - (curWt * fraction))
59+
break
60+
return totalValue
61+
62+
63+
# Driver Code
64+
if __name__ == "__main__":
65+
wt = [10, 40, 20, 30]
66+
val = [60, 40, 100, 120]
67+
capacity = 50
68+
print ("- Fractional Knapsack Problem using Brute Force Method - ")
69+
print ("-----------------------------------------------------")
70+
print ()
71+
print ("The values given : ")
72+
print (val)
73+
print ("-----------------------------------------------------")
74+
print ("The corresponding weights are :")
75+
print (wt)
76+
print ("-----------------------------------------------------")
77+
print ("The maximum capacity can be : ")
78+
print (capacity)
79+
print ()
80+
print ("-----------------------------------------------------")
81+
print ()
82+
print ("Output : ")
83+
# Function call
84+
maxValue = FractionalKnapSack.getMaxValue(wt, val, capacity)
85+
print("Maximum value in Knapsack =", maxValue)
86+
87+
88+
# -----------------------------------------------------------------
89+
# Input given :
90+
# The values given :
91+
# [60, 40, 100, 120]
92+
# -----------------------------------------------------
93+
# The corresponding weights are :
94+
# [10, 40, 20, 30]
95+
# -----------------------------------------------------
96+
# The maximum capacity can be :
97+
# 50
98+
99+
# -----------------------------------------------------
100+
# Output :
101+
# Maximum total value of the Knapsack :
102+
# 240.0
103+
104+
# -----------------------------------------------------------------
105+
106+
# Code contributed by, Abhishek Sharma, 2021

0 commit comments

Comments
 (0)