0% found this document useful (0 votes)
69 views

Experiment - 8: Aim: WAP To Implement Knapsack Problem Using Greedy Algorithm. Theory

The document describes using a greedy algorithm to solve the knapsack problem. It explains that the knapsack problem involves putting items with weights and values into a knapsack without exceeding its capacity to maximize the total value. The greedy algorithm calculates the value-to-weight ratio for each item, sorts them by this ratio, and selects the highest ratio items to fill the knapsack until it is full or an item no longer fits. The code implements this approach by creating an ItemValue class, sorting items by ratio, and iterating through items to add them to the knapsack until it is full.

Uploaded by

akshat sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Experiment - 8: Aim: WAP To Implement Knapsack Problem Using Greedy Algorithm. Theory

The document describes using a greedy algorithm to solve the knapsack problem. It explains that the knapsack problem involves putting items with weights and values into a knapsack without exceeding its capacity to maximize the total value. The greedy algorithm calculates the value-to-weight ratio for each item, sorts them by this ratio, and selects the highest ratio items to fill the knapsack until it is full or an item no longer fits. The code implements this approach by creating an ItemValue class, sorting items by ratio, and iterating through items to add them to the knapsack until it is full.

Uploaded by

akshat sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

EXPERIMENT -8

Aim: WAP to implement Knapsack Problem using greedy algorithm.


Theory:
Weights and values of n items, we need to put these items in a knapsack of capacity W to get
the maximum total value in the knapsack.
In the 0-1 knapsack problem, we are not allowed to break items. We either take the whole item
or don’t take it. 
An efficient solution is to use Greedy approach. The basic idea of the greedy approach is to
calculate the ratio value/weight for each item and sort the item on basis of this ratio. Then take
the item with the highest ratio and add them until we can’t add the next item as a whole and at
the end add the next item as much as we can. Which will always be the optimal solution to this
problem.
A simple code with our own comparison function can be written as follows, please see sort
function more closely, the third argument to sort function is our comparison function which
sorts the item according to value/weight ratio in non-decreasing order. 
After sorting we need to loop over these items and add them in our knapsack satisfying above-
mentioned criteria.

CODING:
class ItemValue:
def __init__(self, wt, val, ind):
self.wt = wt
self.val = val
self.ind = ind
self.cost = val
def __lt__(self, other):
return self.cost < other.cost
class FractionalKnapSack:
@staticmethod
def getMaxValue(wt, val, capacity):
iVal = []
for i in range(len(wt)):
iVal.append(ItemValue(wt[i], val[i], i))
iVal.sort(reverse=True)
totalValue = 0
for i in iVal:
curWt = int(i.wt)
curVal = int(i.val)
if capacity - curWt >= 0:
capacity -= curWt
totalValue += curVal
else:
fraction = capacity / curWt
totalValue += curVal * fraction
capacity = int(capacity - (curWt * fraction))
break
return totalValue
if __name__ == "__main__":
wt = [10, 40, 20, 30]
val = [60, 40, 100, 120]
capacity = 50
maxValue = FractionalKnapSack.getMaxValue(wt, val, capacity)
print("Maximum value in Knapsack =", maxValue)
OUTPUT:

RESULT:
The program is successfully done and compiled in python.

You might also like