Greedy Approach
Greedy Approach
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. 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.
The components that can be used in the greedy algorithm are:
o Candidate set: A solution that is created from the set is known as a candidate set.
o Selection function: This function is used to choose the candidate or subset which can be
added in the solution.
o Feasibility function: A function that is used to determine whether the candidate or
subset can be used to contribute to the solution or not.
o Objective function: A function is used to assign the value to the solution or the partial
solution.
o Solution function: This function is used to intimate whether the complete function has
been reached or not.
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)
Compute a schedule where the greatest number of activities takes place.
Solution: The solution to the above Activity scheduling problem using a greedy strategy is
illustrated below:
Arranging the activities in increasing order of end time
Now, schedule A1
Next schedule A3 as A1 and A3 are non-interfering.
Next skip A2 as it is interfering.
Next, schedule A4 as A1 A3 and A4 are non-interfering, then next, schedule A6 as A1 A3 A4 and
A6 are non-interfering.
Skip A5 as it is interfering.
Next, schedule A7 as A1 A3 A4 A6 and A7 are non-interfering.
Next, schedule A9 as A1 A3 A4 A6 A7 and A9 are non-interfering.
Skip A8 as it is interfering.
Next, schedule A10 as A1 A3 A4 A6 A7 A9 and A10 are non-interfering.
Thus the final Activity schedule is:
Fractional Knapsack Problem
The fractional knapsack problem is also one of the techniques which are used to solve the
knapsack problem. In fractional knapsack, the items are broken in order to maximize the profit.
The problem in which we break the item is known as a Fractional knapsack problem.
This problem can be solved with the help of using two techniques:
o Brute-force approach: The brute-force approach tries all the possible solutions with all
the different fractions but it is a time-consuming approach.
o Greedy approach: In Greedy approach, we calculate the ratio of profit/weight, and
accordingly, we will select the item. The item with the highest ratio would be selected
first.
There are basically three approaches to solve the problem:
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.
Example:
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
W (Weight of the knapsack): 15
n (no of items): 7
First approach:
First approach:
Object Profit Weight Remaining weight
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
The total profit would be equal to (15 + 10 + 9 + 8 + 5.25) = 47.25
Second approach:
The second approach is to select the item based on the minimum weight.
Object Profit Weight Remaining 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
In this case, the total profit would be equal to (5 + 7 + 4 + 10 + 9 + 7 + 3) = 46
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
In this case, we first calculate the profit/weight ratio.
Object 1: 5/1 = 5
Object 2: 10/3 = 3. 33
Object 3: 15/5 = 3
Object 4: 7/4 = 1.7
Object 5: 8/1 = 8
Object 6: 9/3 = 3
Object 7: 4/2 = 2
P:w: 5 3.3 3 1.7 8 3 2
In this approach, we will select the objects based on the maximum profit/weight ratio. Since the
P/W of object 5 is maximum so we select object 5.
Object Profit Weight Remaining weight
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:
Object Profit Weight Remaining weight
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.
Object Profit Weight Remaining weight
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.
Object Profit Weight Remaining weight
5 8 1 15 - 1 = 14
1 5 1 14 - 1 = 13
2 10 3 13 - 3 = 10
3 15 5 10 - 5 = 5
After object 3, object 6 has the maximum profit/weight ratio, i.e., 3. So we select object 6 having
profit/weight ratio as 3.
Object Profit Weight Remaining weight
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
After object 6, object 7 has the maximum profit/weight ratio, i.e., 2. So we select object 7 having
profit/weight ratio as 2.
Object Profit Weight Remaining weight
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.