1 Greedy Algorithms
1 Greedy Algorithms
1. Introduction
• Definition:
A greedy algorithm is an algorithmic paradigm that builds up a solution piece by piece,
always choosing the option that seems best at the moment. It is primarily used for
optimization problems.
• Key Idea:
At every step, the algorithm makes the "locally optimal" choice in the hope that this
leads to a "globally optimal" solution.
Knapsack Algorithm
1 60 10 6.0
2 100 20 5.0
3 120 30 4.0
1 60 10 6.0
2 100 20 5.0
3 120 30 4.0
J1 100 2
J2 50 1
J3 20 2
J4 200 1
J5 150 3
J4 200 1
J5 150 3
J1 100 2
J2 50 1
J3 20 2
1 J4
2 J1
3 J5
J1 100 3
J2 50 1
J3 200 3
J4 30 2
J3 200 3
J1 100 3
J2 50 1
J4 30 2
1 J2
2 J1
3 J3
J1 40 2
J2 20 1
J3 30 2
J4 10 1
Q=c
for i<-1 to n-1
do
{
character Frequency
a 5
b 9
c 12
d 13
e 16
f 45
Step 1. Build a min heap that contains 6 nodes where each node represents root of a tree with single
node.
Step 2 Extract two minimum frequency nodes from min heap. Add a new internal node with
frequency 5 + 9 = 14.
Illustration of step 2
Now min heap contains 5 nodes where 4 nodes are roots of trees with single element each, and one
heap node is root of tree with 3 elements
character Frequency
c 12
d 13
Internal Node 14
e 16
f 45
Step 3: Extract two minimum frequency nodes from heap. Add a new internal node with frequency 12
+ 13 = 25
Illustration of step 3
Now min heap contains 4 nodes where 2 nodes are roots of trees with single element each, and two
heap nodes are root of tree with more than one nodes
character Frequency
Internal Node 14
e 16
Internal Node 25
f 45
Step 4: Extract two minimum frequency nodes. Add a new internal node with frequency 14 + 16 = 30
Illustration of step 4
Now min heap contains 3 nodes.
character Frequency
Internal Node 25
Internal Node 30
f 45
Step 5: Extract two minimum frequency nodes. Add a new internal node with frequency 25 + 30 = 55
Illustration of step 5
Now min heap contains 2 nodes.
character Frequency
f 45
Internal Node 55
Step 6: Extract two minimum frequency nodes. Add a new internal node with frequency 45 + 55 =
100
Illustration of step 6
Now min heap contains only one node.
character Frequency
Internal Node 100
Since the heap contains only one node, the algorithm stops here.
Steps to print codes from Huffman Tree:
Traverse the tree formed starting from the root. Maintain an auxiliary array. While moving to the left
child, write 0 to the array. While moving to the right child, write 1 to the array. Print the array when a
leaf node is encountered.
Edge B→D A→B C→F F→E B→C G→F A→G C→D D→E C→G
Cost 5 6 9 10 11 12 15 17 22 25
Similarly, the next least cost edge is B → A = 6; so we add it onto the output graph.
Minimum cost = 5 + 6 = 11
Visited array, v = {B, D, A}
The next least cost edge is C → F = 9; add it onto the output graph.
Minimum Cost = 5 + 6 + 9 = 20
Visited array, v = {B, D, A, C, F}
The last edge from the least cost array to be added in the output graph is F → G = 12.
Minimum cost = 5 + 6 + 9 + 10 + 11 + 12 = 53
Visited array, v = {B, D, A, C, F, E, G}
The obtained result is the minimum spanning tree of the given graph with cost = 53.
Prims Minimal Spanning Tree
Prim's minimal spanning tree algorithm is one of the efficient methods to find the minimum
spanning tree of a graph. A minimum spanning tree is a sub graph that connects all the vertices
present in the main graph with the least possible edges and minimum cost (sum of the weights
assigned to each edge).
The algorithm, similar to any shortest path algorithm, begins from a vertex that is set as a root
and walks through all the vertices in the graph by determining the least cost adjacent edges.
Prim's Algorithm
To execute the prim's algorithm, the inputs taken by the algorithm are the graph G {V, E},
where V is the set of vertices and E is the set of edges, and the source vertex S. A minimum
spanning tree of graph G is obtained as an output.
Algorithm
• Declare an array visited[] to store the visited vertices and firstly, add the arbitrary root,
say S, to the visited array.
• Check whether the adjacent vertices of the last visited vertex are present in
the visited[] array or not.
• If the vertices are not in the visited[] array, compare the cost of edges and add the least
cost edge to the output spanning tree.
• The adjacent unvisited vertex with the least cost edge is added into the visited[] array
and the least cost edge is added to the minimum spanning tree output.
• Steps 2 and 4 are repeated for all the unvisited vertices in the graph to obtain the full
minimum spanning tree output for the given graph.
• Calculate the cost of the minimum spanning tree obtained.
Examples
• Find the minimum spanning tree using prims method (greedy approach) for the graph
given below with S as the arbitrary root.
Solution
Step 1
Create a visited array to store all the visited vertices into it.
V={}
The arbitrary root is mentioned to be S, so among all the edges that are connected to S we
need to find the least cost edge.
S→B=8
V = {S, B}
Step 2
Since B is the last visited, check for the least cost edge that is connected to the vertex B.
B →A=9
B → C = 16
B → E = 14
Hence, B → A is the edge added to the spanning tree.
V = {S, B, A}
Step 3
Since A is the last visited, check for the least cost edge that is connected to the vertex A.
A → C = 22
A→B=9
A → E = 11
But A → B is already in the spanning tree, check for the next least cost edge. Hence, A → E
is added to the spanning tree.
V = {S, B, A, E}
Step 4
Since E is the last visited, check for the least cost edge that is connected to the vertex E.
E → C = 18
E→D=3
Therefore, E → D is added to the spanning tree.
V = {S, B, A, E, D}
Step 5
Since D is the last visited, check for the least cost edge that is connected to the vertex D.
D → C = 15
E→D=3
Therefore, D → C is added to the spanning tree.
V = {S, B, A, E, D, C}
A1 1 3
A2 2 5
A3 3 9
A4 6 8
A5 5 7
A1 1 3
Activity Start Time Finish Time
A2 2 5
A5 5 7
A4 6 8
A3 3 9
Step Considered Activity Start Time Finish Time Can be Selected? Selected Activities
1 A1 1 3 ✅ Yes (1 ≥ 0) {A1}
2 A2 2 5 ❌ No (2 < 3) {A1}