Greedy Algorithm
Greedy algorithm is an approach to solve optimization problems (such as minimizing and
maximizing a certain quantity) by making locally optimal choices at each step which may then
yield a globally optimal solution
Advantages
Analyzing the run time for greedy algorithms will generally be much easier than for other
techniques (like Divide and conquer).
Disadvantages
The difficult part is that for greedy algorithms you have to work much harder to understand
correctness issues.
Different Types of Greedy Algorithm
• Knapsack Problem
• Minimum Spanning Tree
• Prim's Minimal Spanning Tree Algorithm
• Kruskal's Minimal Spanning Tree Algorithm
• Dijkstra's Minimal Spanning Tree Algorithm
• Huffman Coding
Architecture Of the Greedy Algorithm
The basic architecture of the implementation of any greedy algorithm is as follows:
Input to Algorithm: Function = F, Set of Inputs = C
Output: Solution Set Which Maximizes or Minimizes Function
Steps:
1. Initialize an empty solution set, S = {}
2. While (C is not empty):
# step 1: choose an item from C
current_choice = Select(C)
# step 2: if current_choice is feasable, add to S
if(isFeasable(current_choice)):
S.add(C)
# step 3: remove the current_choice from C
C.remove(current_choice)
3. Return S
Here
1. Select() function makes a greedy choice from the input set C
2. isFeasable() function checks whether the choice made by Select() function leads to an
optimal value of F
Shortest paths on a special graph
Problem: Find a shortest path from v0 to v3.
• The greedy method can solve this problem.
• The shortest path: 1 + 2 + 4 = 7.
Shortest paths on a multi-stage graph
Problem: Find a shortest path from v0 to v3 in the multi-stage graph
• Greedy method: v0v1,2v2,1v3 = 23
• Optimal: v0v1,1v2,2v3 = 7
• The greedy method does not work.
Knapsack Problem: Example
Knapsack problem use greedy approach to find optimal solution.
Let us consider that the capacity of the knapsack M = 20 and the list of provided items are shown
in the following table −
Item Obj 1 Obj 2 Obj 3
Profit 25 24 15
Weight 18 15 10
1. First Approach: Greedy for Profit
a. Pick Obj 1 as its profit is 25
18 Profit =25
Obj 1
b. Pick Obj 2 as its profit is 24
2
Obj 2 Profit = (2/15) *24=3.2
18 Total= 25+3.2=28.2
Obj 1
2. Second Approach: Greedy for Wight
1. Pick Obj 3 as its weight is 10
Profit =15
10
Obj 3
Pick Obj 2 as its wight is 15