Docsity

Kruskal's Spanning Tree Algorithm, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
Kruskal's Spanning Tree Algorithm
Minimum Spanning Tree
Kruskal's algorithm to find the minimum cost spanning
tree uses the greedy approach. This algorithm treats the
graph as a forest and every node it has as an individual
tree. A tree connects to another only and only if, it has
the least cost among all available options and does not
violate MST properties.
To understand Kruskal's algorithm let us consider the
following example −
Step 1 - Remove all loops and Parallel Edges
Remove all loops and parallel edges from the given graph.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Kruskal's Spanning Tree Algorithm and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Kruskal's Spanning Tree Algorithm

Minimum Spanning Tree

Kruskal's algorithm to find the minimum cost spanning

tree uses the greedy approach. This algorithm treats the

graph as a forest and every node it has as an individual

tree. A tree connects to another only and only if, it has

the least cost among all available options and does not

violate MST properties.

To understand Kruskal's algorithm let us consider the

following example −

Step 1 - Remove all loops and Parallel Edges

Remove all loops and parallel edges from the given graph.

In case of parallel edges, keep the one which has the least cost associated and remove all others.

6 vertices 6-1=5edges

Step 2 - Arrange all edges in their increasing order of

weight

The next step is to create a set of edges and weight, and arrange them in an ascending order of weightage (cost). C,B omit

Step 3 - Add the edge which has the least weightage

Now we start adding edges to the graph beginning from the one which has the least weight. Throughout, we shall keep checking that the spanning properties remain intact. In case, by adding one edge, the spanning tree property does not hold then we shall consider not to include the edge in the graph. The least cost is 2 and edges involved are B,D and D,T. We add them. Adding them does not violate spanning tree properties, so we continue to our next edge selection. Next cost is 3, and associated edges are A,C and C,D. We add them again −

By adding edge S,A we have included all the nodes of the graph and we now have minimum cost spanning tree. Below are the steps for finding MST using Kruskal’s algorithm

1. Sort all the edges in non-decreasing order of their

weight.

2. Pick the smallest edge. Check if it forms a cycle with

the spanning tree formed so far. If cycle is not formed,

include this edge. Else, discard it.

3. Repeat step#2 until there are (V-1) edges in the

spanning tree.

The algorithm is a Greedy Algorithm. The Greedy Choice is to pick the smallest weight edge that does not cause a cycle in the MST constructed so far. Let us understand it with an example: Consider the below input graph.

9 vertices the resultant of MST Kruskals alg 9-1=8 edges ADVERTISING The graph contains 9 vertices and 14 edges. So, the minimum spanning tree formed will be having (9 – 1) = 8 edges. After sorting: Weight Src Dest all 1 7 6 yes 2 8 2 yes 2 6 5 4 0 1 4 2 5 6 8 6 no 7 2 3 7 7 8 8 0 7 8 1 2 9 3 4 10 5 4 11 1 7 14 3 5

5. Pick edge 2 - 5: No cycle is formed, include it. 6. Pick edge 8 - 6: Since including this edge results in cycle, discard it. 7. Pick edge 2 - 3: No cycle is formed, include it. 8. Pick edge 7 - 8: Since including this edge results in cycle, discard it. 9. Pick edge 0 - 7: No cycle is formed, include it.

10. Pick edge 1 - 2: Since including this edge results in cycle, discard it. 11. Pick edge 3 - 4: No cycle is formed, include it. Since the number of edges included equals (V – 1), the algorithm stops here.