0% found this document useful (0 votes)
10 views3 pages

Sa, Jsanxka

The lab report focuses on implementing a greedy algorithm for the fractional knapsack problem, aiming to maximize profit by selecting items based on their value-to-weight ratio. The algorithm reads item data from a file, calculates ratios, sorts items, and fills the bag while tracking profit. The author discusses their testing process, insights gained, and concludes that the algorithm operates with a time complexity of O(n).

Uploaded by

Sonit Marwah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Sa, Jsanxka

The lab report focuses on implementing a greedy algorithm for the fractional knapsack problem, aiming to maximize profit by selecting items based on their value-to-weight ratio. The algorithm reads item data from a file, calculates ratios, sorts items, and fills the bag while tracking profit. The author discusses their testing process, insights gained, and concludes that the algorithm operates with a time complexity of O(n).

Uploaded by

Sonit Marwah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CAP5.

01053: Introduction to Algorithms


ATLAS SkillTech University
Instructor: Ashwin Ganesan

Lab report

Name: VRUSHANK BADLE


App ID: 1050035

Title of Lab: GREEDY FRACTIONAL KNAPSACK


Date: 17 Jan 2024
Problem description

There are n items with price vi and weight wi. A robber has a bag with capacity w. We have to
create an algorithm to completely fill his bag after collecting the items in appropriate ratio such that
we get maximum profit.

Algorithm description

The input is path of the file containing number of items, capacity, item price and item weight. The
function starts by reading the file and separating n and w, and calculating the price of a single unit
of each item using formula vi/wi, and appending the value to a list. Then it sorts the list in
descending order wrt their ratio. Then it uses greedy method to get maximum amount from the most
precious item, followed by second. Then it calculates profit by multiplying amount collected to their
respective ratio. While adding if current capacity is less than item amount, it takes the item amount
equal to the current capacity. Else we add entire item amount to the capacity, till either the item
amount gets to 0 or the capacity is full. If it’s the first case, then we move on to next item, if it’s the
later then we exit the loop and return profit, round to 4 decimals.

Program

def knapsack(file_path):

with open(file_path, 'r') as file:

l1 = file.read().split('\n')

l2 = [item.split() for item in l1]


n = int(l2[0][0])
cap = int(l2[0][1])

d1 = []
l2 = l2[1:]

for i in range(n):
d1.append( ((int(l2[i][0]) / int(l2[i][1])), int(l2[i][0]), int(l2[i][1])))

d1.sort(reverse=True)

profit = 0
i=0

while cap > 0 and i != len(d1):

if cap < d1[i][2]:


profit += d1[i][0] * cap
cap -= cap

else:
cap -= d1[i][2]
profit += d1[i][1]

if cap < d1[i][2] :


i += 1

return round(profit, 4)

Sample runs

Analysis and conclusions


Summarize your experience doing this experiment. Some questions to consider:
• What were the main objectives of this experiment? Did you achieve the objectives?
◦ Main objectives were to solve the problem using greedy algorithm.
• Explain how you tested your program to check for any errors. For example, what were some
test inputs and the corresponding outputs? What were some modules or functions you
implemented and what test cases were used to debug them?
◦ The test cases I used are given in sample runs. To debug the code and check for errors, I
used print statements at required place.
• What are the main new things you learnt from this experiment.
◦ If we use list.sort() on a list containing tuples with multiple values, it automatically sorts
wrt to the first value of each tuple.
• What is the time complexity of your algorithm? Express your answer in asymptotic notation.
Can you do better?
◦ The algorithm runs in O(n) time complexity. I don’t think we can do better since we
have to first calculate ratio for each value then collect each item again for calculating
profit. Which means we go through the n items twice.

Acknowledgements

Prof. Ashwin Ganesan

I would love to solve more problems like this and get your feedback for improvement.
Thank You

You might also like