Greedy Algorithm in daa
Greedy Algorithm in daa
The greedy method is one of the strategies like Divide and conquer used to
solve the problems. This method is used for solving optimization problems.
An optimization problem is a problem that demands either maximum or
minimum results. Let's understand through some terms.
This technique is basically used to determine the feasible solution that may
or may not be optimal. The feasible solution is a subset that satisfies the
given criteria. The optimal solution is the solution which is the best and the
most favorable solution in the subset. In the case of feasible, if more than
one solution satisfies the given criteria then those solutions will be
considered as the feasible, whereas the optimal solution is the best solution
among all the solutions.
o To construct the solution in an optimal way, this algorithm creates two sets
where one set contains all the chosen items, and another set contains the
rejected items.
o A Greedy algorithm makes good local choices in the hope that the solution
should be either feasible or optimal.
The above is the greedy algorithm. Initially, the solution is assigned with zero
value. We pass the array and number of elements in the greedy algorithm.
Inside the for loop, we select the element one by one and checks whether
the solution is feasible or not. If the solution is feasible, then we perform the
union.
P:A→B
The problem is that we have to travel this journey from A to B. There are
various solutions to go from A to B. We can go from A to B by walk, car,
bike, train, aeroplane, etc. There is a constraint in the journey that we
have to travel this journey within 12 hrs. If I go by train or aeroplane then
only, I can cover this distance within 12 hrs. There are many solutions to this
problem but there are only two solutions that satisfy the constraint.
If we say that we have to cover the journey at the minimum cost. This means
that we have to travel this distance as minimum as possible, so this problem
is known as a minimization problem. Till now, we have two feasible solutions,
i.e., one by train and another one by air. Since travelling by train will lead to
the minimum cost so it is an optimal solution. An optimal solution is also the
feasible solution, but providing the best result so that solution is the optimal
solution with the minimum cost. There would be only one optimal solution.
The problem that requires either minimum or maximum result then that
problem is known as an optimization problem. Greedy method is one of the
strategies used for solving the optimization problems.
It follows the local optimum choice at each stage with a intend of finding the
global optimum. Let's understand through an example.
Example: Given 10 activities along with their start and end time as
S = (A1 A2 A3 A4 A5 A6 A7 A8 A9 A10)
Si = (1,2,3,4,7,8,9,9,11,12)
fi = (3,5,4,7,10,9,11,13,12,14)
Solution
Skip A5 as it is interfering.
This problem can be solved with the help of using two techniques:
o The first approach is to select the item based on the maximum profit.
o The second approach is to select the item based on the minimum weight.
o The third approach is to calculate the ratio of profit/weight.
Objects: 1 2 3 4 5 6 7
Profit (P): 10 15 7 8 9 4
Weight(w): 1 3 5 4 1 3 2
First approach:
First approach:
3 15 5 15 - 5 = 10
2 10 3 10 - 3 = 7
6 9 3 7-3=4
5 8 1 4-1=3
7 7 * ¾ = 5.25 3 3-3=0
Second approach:
The second approach is to select the item based on the minimum weight.
1 5 1 15 - 1 = 14
5 7 1 14 - 1 = 13
7 4 2 13 - 2 = 11
2 10 3 11 - 3 = 8
6 9 3 8-3=5
4 7 4 5-4=1
3 15 * 1/5 = 3 1 1-1=0
Third approach:
In the third approach, we will calculate the ratio of profit/weight.
Objects: 1 2 3 4 5 6 7
Profit (P): 5 10 15 7 8 9 4
Weight(w): 1 3 5 4 1 3 2
Object 1: 5/1 = 5
Object 2: 10/3 = 3. 33
Object 3: 15/5 = 3
Object 5: 8/1 = 8
Object 6: 9/3 = 3
Object 7: 4/2 = 2
5 8 1 15 - 8 = 7
After object 5, object 1 has the maximum profit/weight ratio, i.e., 5. So, we
select object 1 shown in the below table:
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
After object 1, object 2 has the maximum profit/weight ratio, i.e., 3.3. So, we
select object 2 having profit/weight ratio as 3.3.
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
2 10 3 13 - 3 = 10
After object 2, object 3 has the maximum profit/weight ratio, i.e., 3. So, we
select object 3 having profit/weight ratio as 3.
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
2 10 3 13 - 3 = 10
3 15 5 10 - 5 = 5
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
2 10 3 13 - 3 = 10
3 15 5 10 - 5 = 5
6 9 3 5-3=2
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
2 10 3 13 - 3 = 10
3 15 5 10 - 5 = 5
6 9 3 5-3=2
7 4 2 2-2=0
As we can observe in the above table that the remaining weight is zero
which means that the knapsack is full. We cannot add more objects in the
knapsack. Therefore, the total profit would be equal to (8 + 5 + 10 + 15 + 9
+ 4), i.e., 51.
In the first approach, the maximum profit is 47.25. The maximum profit in
the second approach is 46. The maximum profit in the third approach is 51.
Therefore, we can say that the third approach, i.e., maximum profit/weight
ratio is the best approach among all the approaches.