
Sorting according to the given ratio of value by weight:
same way till all weights are compared. same way with second weight but here weight > W so take the fractional part of the item and add profit by Multiplying W with ratio of value per weight of the current weight. same way till all weights are compared or W=0. same way with second weight but here weight > W so ignore and go to next weight.Next weight is 2 so collect :(4,12),(2,2) and W=1. Compare W with first weight: so here 12 is less than W=15 so collect it and subtract the weight 12 from W=15–12=3.collected items:(value=4,weight= 12). For greedy method we want maximum possible value of the collected weights so we will sort in ascending order with weights. First sort the given data in ascending order according to the Weight. same way with second weight but here weight > W so ignore and go to next weight.Next weight is 1 so collect :(10,4),(2,1) and W=10. Compare W with first weight: so here 4 is less than W=15 so collect it and subtract the weight 4 from W=15–4=11.collected items:(value=10,weight= 4). Add the value of collected items.It is our final Profit.
Now while comparing if the problem is 0/1 knapsack type and the given weight becomes less than the current item than skip the item and continue but if the problem is of fractional type collect the value according the weight remaining(Multiply W with ratio of value per weight of the current weight). Start comparing the given weight with the sorted data and keep collecting items and subtracting the weight of item from given weight until the given weight becomes zero or compared with all weights. For greedy method we want maximum possible value of the collected weights so we will sort in descending order with values. First sort the given data in descending order according to the value. By sorting according to the ratio of value by weight:. We can solve this problem by mainly three ways: Here we will solve each of the knapsack problem by greedy method meaning taking decisions without thinking of the consequences.
Here items are divisible so we can collect parts of item also. We can have two categories of Knapsack problem: In Knapsack problem Value and Weight is given to collect most amount value with given weight limit that can be collected.
Knapsack Problem is also known as rucksack problem.It is a optimization problem.