It is a method to solve the optimization problem
Optimization problem means either there is a minimum or maximum solution
Suppose we have a P problem which is we want to travel from A point to B point, and there are multiple solutions S1, S2, S3........Sn. So, if there is a constraint or condition, we must travel within 12 hours. Only S2 and S3 solutions can give us minimum time to travel. Therefore, S2 and S3 will be our feasible solutions. From these minimum time-required solutions, we have S3, which can give us a minimum cost to travel from A to B. So, S3 is our optimal solution.
Greedy Method
Dynamic Programming
Branch and Bound method
Suppose there is an array a with 5 possible solution the algorithm for selecting feasible solution according to greedy method is,
n=5
a=[a1, a2, a3, a4, a5]
Algorithm Greedy(a, n){
for(i=1 to n do){
x=Select(a);
if(Feasible(x)) then,
Solution:=Solution + x;
}
}
Fractional Knapsack
0/1 Knapsack problem
Fractional Knapsack:
The Greedy method is an effective solution. The greedy approach's primary principle is to calculate each item's value/weight ratio and sort the items based on this ratio. Then take the item with the highest ratio and add it until we can't add the next item as a whole, and then add as much of the next item as possible, which is always the best approach to this problem.
Follow the given steps to solve the problem using the above approach:
Calculate the ratio(value/weight) for each item.
Sort all the items in decreasing order of the ratio.
Initialize res =0, curr_cap = given_cap.
Do the following for every item “i” in the sorted order:
If the weight of the current item is less than or equal to the remaining capacity, then add the value of that item to the result.
Else add the current item as much as we can and break out of the loop
Return res
Suppose we have 15 kg of sack and 7 item which have to be loaded into that 15 kg sack. We have to select object or item so that we can achieve maximum profit.
So, here m= 15 kg and n=7. Let x is the objects which is given us to fill up the sack. based on the maximum profit per weight we will choose the max values of P/W.
Here we are selecting 2/3 of x2 or object 1 (in the Table) and from rest of the objects we are selecting whole object except x4 or object 3 (in the Table).
The 0/1 knapsack dilemma denotes that all or none of the things in a knapsack are filled. For example, consider two goods weighing 2kg and 3kg, respectively. If we select the 2kg item, we cannot select a 1kg item from the 2kg item (the item is not divisible); we must select the full 2kg item. It is a 0/1 knapsack dilemma in which we either entirely pick the item or pick that item. Dynamic programming is used to solve the 0/1 knapsack issue.
Example of 0/1 Knapsack:
Suppose, m= 8 kg the total capacity of our sack and there in n = 4 items
Profit, P = {1, 2, 5, 6}
Weight, w= {2, 3, 4, 5}
Let's solve this problem with dynamic programming-
We need a table to solve this problem. The colored portion is the given data. Column V indicates the objects, and from the right side of the V column, the sack capacity is taken as column name 0 to 8 (as our total capacity is 8 kg ). Initially, for 0 objects, the filling up items profit is also 0, and for 0 capacity in our sack, the profit will also be 0. So we fill up that row and column with 0.
First, we must see that the items are sorted based on weight. Then, we consider the first item profit is P1=1, and weight is W1=2. So, we can fill our sack with a 2 kg item with a profit of 1. We will have a maximum profit of 1, whatever the sack size is. So we fill this row with 1, which is the maximum profit we can have. As the weight of this item is 2, there will be no profit for a 1 kg sack value.
Now we will consider the next item. Remind it! Don't consider only the next product. We have to consider the previous one also. So for the 2nd item, we have a profit of 2 for a weight of 3. So, if we have a 3 kg sack and these 2 items, we can have maximum profit 2. let's fill the [2, 3] position means column 2, row 3 position with 2. The profit is unchanged for 1 and 2 kg in the sack because we have the weights 2 and 3. Now, if the sack capacity is 4, the max profit will be 2 only as we don't have the 4 kg weight items. But we have noticed one thing! We have two items with a weight of 5 (2+3) kg and a profit of 3(1+2) units of max profit. So, we fill from kg 5 to 8 with the max profit of 3.
We can feel there is a consistency in the process. YES!! So let's have a formula for rest of the table values.
Let's find the value of the position [4, 1] in the table. i is row value and w is column value. We can see that this formula will take max value after the comparison with the value of v with specified position. The value will be for v[4, 1] will be 0. Let's apply in the table!!
So, maximum profit is 8 units.
If we select x4 the profit will be 6
8 - 6 = 2 units remaining profit; We are selecting x4 and fill it in the sack
then we have x1, x2, x3 with the profit of 1, 2, and 5
So we can take only x2 as the remaining profit is 2.
So we will select x2 and x4 to fill up the sack and get the maximum profit. Solved!
Let p is the profit P={1, 2, 5, 6}
weight W={2, 3, 4, 5}
m= 8 kg
n= 4 units