The Problem

Ted Thief has just broken into the Fort Knox! He sees himself in a room with n piles of gold dust. Because the each pile has a different purity, each pile also has a different value (v[i]) and a different weight (c[i]). Ted has a knapsack that can only hold W kilograms.

Given n, v, c and W, calculate which piles Ted should completely put into his knapsack and which he should put only a fraction of.

Ted should take piles 2, 3, 4 and 5 completely and about 58% of pile 1.

You’re usually dealling with a knapsack problem when you’re give the cost and the benefits of certain objects and asked to obtain the maximum benefit so that the sum of the costs is smaller than a given value. You’ve got the fractional knapsack problem when you can take fractions (as opposed to all or nothing) of the objects.

The Algorithm

This is a standard greedy algorithm. In fact, it’s one of the classic examples.

The idea is to calculate for each object the ratio of value/cost, and sort them according to this ratio. Then you take the objects with the highest ratios and add them until you can’t add the next object as whole. Finally add as much as you can of the next object.

From this it’s obvious that you should add the objects: 5, 2, 3, 4 and then as much as possible of 1.
The output of my programme is this:Added object 5 (10$, 4Kg) completly in the bag. Space left: 11.
Added object 2 (2$, 1Kg) completly in the bag. Space left: 10.
Added object 3 (2$, 2Kg) completly in the bag. Space left: 8.
Added object 4 (1$, 1Kg) completly in the bag. Space left: 7.
Added 58% (4$, 12Kg) of object 1 in the bag.
Filled the bag with objects worth 15.48$.

The Programme

Now, you could implement the algorithm as stated, but for practical reasons you may wish to trade speed for simplicity. That’s what I’ve done here: instead of sorting the objects, I simply go through them every time searching for the best ratio. This modification turns an O(n*lg(n)) algorithm into an O(n2) one. For small values of n, this doesn’t matter and n is usually small.

really it is simple to understand.thank u very much if you have 0-1 knapsack ,and other type knapsack problems solutions plz mail to /post here me .also suggest me any lecturre notes or site to learn ” design & analysis of algorithims ” which is in my curriculum

@rajat: In the 0-1 problem you can think about that the products used to fill the thief’s bag are solid like a gold ingot — or you put the whole item inside the bag or nothing. In fractional one you can think about something like dust, what I mean is, the thief can take some part of each one of the items available to steal.

Does your blog have a contact page? I’m having problems locating it but, I’d like to send you an email.
I’ve got some creative ideas for your blog you might
be interested in hearing. Either way, great blog and I look forward to seeing it grow over time.

I think it is one of the a whole lot information for me. And i’m happy studying ones report. Nonetheless need to thoughts for some elementary issues, The web site type is usually great, a posts is at fact wonderful : Chemical. Good procedure, all the best