Kruskal's Algorithm is used to find the minimum spanning tree for a connected weighted graph. The main target of the algorithm is to find the subset of edges by using which, we can traverse every vertex of the graph.
The Kruskal's algorithm is given as follows.
Algorithm
- Step 1: Create a forest in such a way that each graph is a separate tree.
- Step 2: Create a priority queue Q that contains all the edges of the graph.
- Step 3: Repeat Steps 4 and 5 while Q is NOT EMPTY
- Step 4: Remove an edge from Q
- Step 5: IF the edge obtained in Step 4 connects two different trees, then Add it to the forest (for combining two trees into one tree).
ELSE
Discard the edge - Step 6: END
Example :
Apply the Kruskal's algorithm on the graph given as follows.
Answer:
Sort the edges according to their weights.
Start constructing the tree;
Add AB to the MST(Minimum Spanning Tree);
Step 1:
Add DE to the MST;
Step 2:
Add BC to the MST;
Step 3:
The next step is to add AE, but we can't add that as it will cause a cycle.
The next edge to be added is AC, but it can't be added as it will cause a cycle.
The next edge to be added is AD, but it can't be added as it will contain a cycle.Hence, the final MST is the one which is shown in the step 3.
the cost of MST = 1 + 2 + 3 + 4 = 10.
No comments:
Post a Comment