0% found this document useful (0 votes)
42 views13 pages

Lecture 12

1) The document describes an algorithm to solve the fractional knapsack problem in a greedy manner. 2) It sorts items by value per unit weight (vi/wi) in decreasing order and selects items in that order until the total weight reaches the knapsack capacity W. 3) The algorithm runs in O(nlogn) time where n is the number of items. It proves that the greedy approach of selecting items by value per unit weight provides an optimal solution to the fractional knapsack problem.

Uploaded by

f20201862
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)
42 views13 pages

Lecture 12

1) The document describes an algorithm to solve the fractional knapsack problem in a greedy manner. 2) It sorts items by value per unit weight (vi/wi) in decreasing order and selects items in that order until the total weight reaches the knapsack capacity W. 3) The algorithm runs in O(nlogn) time where n is the number of items. It proves that the greedy approach of selecting items by value per unit weight provides an optimal solution to the fractional knapsack problem.

Uploaded by

f20201862
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/ 13

BITS, PILANI – K. K.

BIRLA GOA CAMPUS

Design & Analysis of Algorithms


(CS F364)

Lecture No. 12

1
Greedy - Exercise
Suppose you were to drive from Mumbai to Pune along the
express highway. Suppose your car’s petrol tank holds enough
petrol to travel M miles. Suppose you have a map that gives
distances between petrol stations along the route. Let d1 < d2
< …< dn be the locations of all the petrol stations along the
route where di is the distance of petrol station from Mumbai.
Assume that distance between neighboring petrol stations is
less than M miles. Your goal is to accomplish the journey
making as few petrol stops as possible along the way.
Design an efficient algorithm to determine at which petrol
stations to take a halt. Prove that your algorithm gives optimal
solution and also analyze the time complexity of the
algorithm.
The Fractional Knapsack Problem
Given: A set S of n items, with each item i having
wi - a positive “weight”
vi - a positive “benefit”
Goal:
Choose items with maximum total benefit but with weight
at most W.
And we are allowed to take fractional amounts
The Fractional Knapsack Problem
Possible Greedy Strategies:
• Pick the items in increasing order of weights
• Pick the items in decreasing order of benefits
• Pick the items by decreasing order of value per
pound
Note:
1st two strategies do not give optimal solution
Counterexamples - Exercise
Greedy Algorithm
We can solve the fractional knapsack problem with a
greedy algorithm:
• Compute the value per pound (vi/wi) for each item
• Sort (decreasing) the items by value per pound
• Greedy strategy of always taking as much as possible
of the item remaining which has highest value per
pound
Time Complexity:
If there are n items, this greedy algorithm takes
O(nlogn) time
Greedy Choice Property
• Theorem
Consider a knapsack instance P, and let item 1
be item of highest value density
Then there exists an optimal solution to P that
uses as much of item 1 as possible (that is,
min(w1, W)).
Proof:
Suppose we have a optimal solution Q that uses
weight w <min(w1, W) of item 1.
Let w′ = min(w1, W) − w
Greedy Choice Property
Q must contain at least weight w′ of some other
item(s), since it never pays to leave the knapsack
partly empty.
Construct Q* from Q by removing w′ worth of
other items and replacing with w′ worth of item
1
Because item 1 has max value per weight, So
Q* has total value at least as big as Q.
Alternate Proof - 1
Alternate Proof:
Assume the objects are sorted in order of cost
per pound.
Let vi be the value for item i and let wi be its
weight.
Let xi be the fraction of object i selected by
greedy and let V(X) be the total value obtained
by greedy
Alternate Proof - 1
Alternate Proof
Alternate Proof - 1
Alternate Proof - 1
Alternate proof (2) - Proof by contradiction
We can also prove by contradiction.
We start by assuming that there is an optimal solution where we did
not take as much of item i as possible and we also assume that our
knapsack is full (If it is not full, just add more of item i).
Since item i has the highest value to weight ratio, there must exist an
𝒗𝒋 𝒗
item j in our knapsack such that < 𝒊
𝒘𝒋 𝒘𝒊
We can take item j of weight x from our knapsack and we can add item
i of weight x to our knapsack (Since we take out x weight and put in x
weight, we are still within capacity).
𝒗𝒊 𝒗𝒋
The change in value of our knapsack is x − 𝒙 >𝟎
𝒘𝒊 𝒘𝒋
Therefore, we arrive at a contradiction because the ”so-called” optimal
solution in our starting assumption, can in fact be improved by taking
out some of item j and adding more of item i. Hence, it is not optimal.

You might also like