Kruskals Algorithm
Kruskals Algorithm
Kruskal’s Algorithm
Spanning trees
• A spanning tree is a subset of Graph G, which has all the vertices covered with minimum
possible number of edges. Hence, a spanning tree does not have cycles and it cannot be
disconnected.
• A disconnected graph does not have any spanning tree, as it cannot be spanned to all its
vertices.
• Every connected and undirected Graph G has at least one spanning tree.
• Minimum Spanning Tree: The spanning tree of a graph whose sum of weights of edges is
minimum.
– A graph may have more than 1 minimum spanning tree.
Properties of spanning trees
• A connected graph G can have more than one spanning tree.
• All possible spanning trees of graph G, have the same number of edges and vertices.
• The spanning tree does not have any cycle (loops).
• Removing one edge from the spanning tree will make the graph disconnected, i.e. the
spanning tree is minimally connected.
• Adding one edge to the spanning tree will create a circuit or loop, i.e. the spanning tree
is maximally acyclic.
Properties of spanning trees
• Complete undirected graph can have maximum nn-2 number of spanning trees.
• Spanning tree has n-1 edges, where n is the number of nodes (vertices).
• From a complete graph, by removing maximum e - n + 1 edges, we can construct a spanning
tree.
Applications of spanning trees
• Spanning trees have direct applications in the design of networks, including computer
networks, telecommunications networks, transportation networks, water supply networks,
and electrical grids
• Civil Network Planning
• Cluster Analysis
• Approximation algorithms for NP-hard problems.
– traveling salesperson problem, Steiner tree
Minimum spanning trees
• A single graph can have many different spanning trees.
• A minimum spanning tree (MST) or minimum weight spanning tree for a weighted, connected
and undirected graph is a spanning tree with weight less than or equal to the weight of every
other spanning tree.
• The weight of a spanning tree is the sum of weights given to each edge of the spanning tree.
• We can construct MST using two algorithms.
• Kruskal’s algorithm Greedy algorithms
• Prim’s algorithm
• A greedy algorithm is used in optimization problems. The algorithm makes the optimal choice
at each step as it attempts to find the overall optimal way to solve the entire problem.
Minimum Spanning trees
8 8 8
A B A B A B
7
11 6 11 6 6
1 1
C D C D C D
Cost = 25 Cost = 15
Kruskal’s algorithm
Step 1. Remove all loops and parallel edges (of higher weight).
Step 2. Sort all the edges in non-decreasing order of their weight.
Step 3. 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.
Step 4. Repeat step 2 until there are (V-1) edges in the spanning tree.
Example – 1
A D
3 3
4
4 4 F
C
2 3
2
B E
Solution – 1
Weights in Source node Destination node
ascending order
2 B C
2 C E
3 C D
3 D F
3 E F
4 A B
4 A C
4 C F
A D
3
4
C F
2 3
2
B E
4 9
2
11 14 4
0 8 4
7
6 10
8
1 2
7 6 5
Solution – 2
Weights in Source node Destination node
ascending order
1 7 6
2 8 2
2 6 5
4 0 1
4 2 5
6 8 6
7 2 3
7 7 8
8 0 7
8 1 2
9 3 4
10 5 4
11 1 7
14 3 5
8 7
1 2 3
4 9
2
0 8 4
4
1 2
7 6 5