Kruskal's AlgorithmKruskal'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. Kruskal's algorithm follows greedy approach which finds an optimum solution at every stage instead of focusing on a global optimum. The Kruskal's algorithm is given as follows. Algorithm
Example :Apply the Kruskal's algorithm on the graph given as follows. Solution:the weight of the edges given as :
Sort the edges according to their weights.
Start constructing the tree; Add AB to the MST; Add DE to the MST; Add BC to the MST; 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 4. the cost of MST = 1 + 2 + 3 + 4 = 10. C Program:Output Enter Nodes and edges5 5 Enter the value of X, Y and edges5 4 3 Enter the value of X, Y and edges2 3 1 Enter the value of X, Y and edges1 2 3 Enter the value of X, Y and edges5 4 3 Enter the value of X, Y and edges23 3 4 Minimum cost is 11
Next TopicLinear Search
