Kruskal’s Algorithm is a greedy algorithm used for finding the minimum spanning tree (MST) of a connected, weighted graph. The MST is the tree that spans all the nodes in the graph with the minimum possible total edge weight.
The Kruskal’s Algorithm works as follows:
Example:
Consider the following graph with 6 vertices and 9 edges, where each edge is weighted as follows:
A ----- 4 ----- B
| |
2| 5|
| |
C —– 3 —– D
\ /
1\ /2
\ /
\ /
\ /
E
To find the MST using Kruskal’s Algorithm, we first sort the edges in non-decreasing order of their weight:
Edges: (C, E) (A, C) (C, D) (A, B) (B, D) (D, E) (B, E) (A, D) (B, C)
Weights: 1 2 3 4 5 2 5 6 7
Next, we create a forest of trees consisting of each vertex as a separate tree:
A B C D E
Then, we start checking the edges in the sorted order and adding them to the MST if they connect different trees. The first edge (C, E) joins tree C to tree E:
A B C D
|
E
The second edge (A, C) joins tree A to tree C:
B C
|
A
D
|
E
The third edge (C, D) joins tree C to tree D:
B C
|
A D
|
E
The fourth edge (A, B) joins tree A to tree B:
A
/|\
|
C
|
B
D
|
E
The fifth edge (B, D) joins tree B to tree D:
A
/|\
|
C
|
B
|
D
|
E
The sixth edge (D, E) joins tree D to tree E:
A
/|\
|
C
/|\
|
B
|
D --- E
The seventh edge (B, E) cannot be added as it would create a cycle.
The eighth edge (A, D) cannot be added as it connects two nodes already in the same tree.
The ninth edge (B, C) cannot be added as it connects two nodes already in the same tree.
Therefore, the MST consists of the edges (C, E), (A, C), (C, D), (A, B), (B, D), and (D, E), with a total edge weight of 17.
What is Kruskal’s Algorithm used for?
Answer: Kruskal’s Algorithm is a popular algorithm used to find the minimum spanning tree of a graph.
What is the basic idea behind Kruskal’s Algorithm?
Answer: Kruskal’s Algorithm works by sorting the edges of a graph according to their weights and then adding them to the minimum spanning tree in increasing order of their weights.
How does Kruskal’s Algorithm ensure that the minimum spanning tree is found?
Answer: Kruskal’s Algorithm ensures that the minimum spanning tree is found by keeping track of the edges added to the tree and ensuring that they do not form a cycle.
What is the time complexity of Kruskal’s Algorithm?
Answer: The time complexity of Kruskal’s Algorithm is O(E log E), where E is the number of edges in the graph.
What is the difference between Kruskal’s Algorithm and Prim’s Algorithm?
Answer: Kruskal’s Algorithm and Prim’s Algorithm are both used to find the minimum spanning tree of a graph, but Kruskal’s Algorithm is based on sorting the edges of the graph and adding them to the tree in increasing order, while Prim’s Algorithm is based on starting with a single vertex and adding the closest unvisited vertex to the tree at each step.