Kruskal's Algorithm

Distribute Education like Computer Geek

Introduction to Kruskal’s Algorithm

  1. Kruskal’s Algorithm is a fundamental method in the field of graph theory and network optimization.
  2. Developed by Joseph Kruskal, this algorithm is employed to find the Minimum Spanning Tree (MST) of a connected, undirected graph.
  3. The primary objective of the algorithm is to discover the smallest set of edges that connects all the vertices in the graph without forming any cycles.

.

Definition of Kruskal’s Algorithm

Kruskal’s Algorithm is a greedy algorithm used to find the Minimum Spanning Tree (MST) of a connected, undirected graph.

The primary objective of the algorithm is to discover the smallest set of edges that connects all the vertices in the graph without forming any cycles.

.

Algorithm of Kruskal(Graph G)

    Sort edges of G in non-descending order by weight

    Initialize an empty MST and set of trees (forest).

    for each edge in G:

        if adding edge to T does not form a cycle:

            Add edge to T

            Merge the trees

    return T

.

Working of Kruskal’s Algorithm
  1. Sort Edges – Begin by sorting all the edges in non-decreasing order based on their weights.
  2. Initialize Forest – Create a forest (a collection of trees), where each vertex is initially a separate tree.
  3. Iterate Through Edges – Starting with the smallest edge, check if adding it to the forest forms a cycle. If not, include it in the MST.
  4. Union Operation – If the edge is added, perform a union operation to merge the two trees into one.
  5. Repeat – Repeat steps 3 and 4 until all vertices are part of a single tree.

.

Imagine you have a network of houses, and you want to build roads between them with the least total distance.

Network of homes and roads

We can simplify this by arranging the alphabets in place of houses.

Graph of Prim's and Kruskal's Algorithm

.

Sort Edges
We have to sort the edges to apply kruskal’s theorem. In this graph there are 16 edges.
Edge Structure– (starting vertex, stopping vertex, weight)
The sorted edges are

Sorted edges in Graph

.

Iterate through Edges

Step 1 – In the sorted edges, first edge AE and its weight is 1. Adding it to the MST does not forms a cycle.

Add AE to MST.

MST Edge 1

.

Step 2 – Next Edge is FJ and its weight is 1.

Adding it to the MST does not forms a cycle.

Add FJ to MST.

MST of Kruskal's Algorithm

.

Step 3 – Next Edge is AB and its weight is 2.

Adding it to the MST does not forms a cycle.

Add AB to MST.

MST of Kruskal's Algorithm 3

Step 4 – Next Edge is BD and its weight is 2.

Adding it to the MST does not forms a cycle.

Add BD to MST.

MST of Kruskal's Algorithm 4

Step 5 – Next Edge is DE and its weight is 2.

Adding it to the MST forms a cycle. So, do not add DE to MST.

Step 6 – Next Edge is GH and its weight is 2.

Adding it to the MST does not forms a cycle.

Add GH to MST.

MST of Kruskal's Algorithm 5

Step 7 – Next Edge is EG and its weight is 2.

Adding it to the MST does not forms a cycle.

Add EG to MST.

MST of Kruskal's Algorithm 6

.

Step 8 – Next Edge is HF and its weight is 2.

Adding it to the MST does not forms a cycle.

Add HF to MST.

MST of Kruskal's Algorithm 7

.

Step 9 – Next Edge is BC and its weight is 3.

Adding it to the MST does not forms a cycle.

Add BC to MST.

MST of Kruskal's Algorithm 8

Step 10 – Next Edge is EF and its weight is 3.

Adding it to the MST forms a cycle. So, do not add EF to MST.

Step 11 – Next Edge is HI and its weight is 3.

Adding it to the MST does not forms a cycle.

Add HI to MST.

MST Edge 9

.

Step 12 – In this step, we found that all the vertices which are in graph has been discovered by us. So, no new operation is needed.

  1. All the vertices are connected in a single tree.
  2. There is no new forest of vertices (disconnected vertices).
  3. The edges weight is 1 + 1 + 2 + 2 + 2 + 2 + 2 + 3 + 3 = 18 that is the minimum spanning tree.

.

Source Code of Kruskal’s Algorithm

Sparse Graphs

Definition – Sparse graphs are graphs where the number of edges is much smaller compared to the maximum possible edges between the vertices.

In other words, there are relatively few existing connections among the vertices.

.

Kruskal’s Algorithm in Sparse Graphs

Edge-Focused Approach

Kruskal’s algorithm primarily focuses on sorting edges based on their weights and adding them to the Minimum Spanning Tree (MST) as long as they do not form cycles.

In sparse graphs, where there are fewer existing edges, the edge-focused approach of Kruskal’s algorithm becomes advantageous.

.

Efficiency in Sparse Connectivity

As sparse graphs have a limited number of existing connections, the sorting and processing of edges become more efficient.

.

Comparison with Dense Graphs

In dense graphs, where the number of edges is closer to the maximum possible (nearly complete graphs), Kruskal’s algorithm might become less efficient. This is due to the sorting step becoming more time-consuming as the number of edges increases.

.

Time Complexity of Kruskal’s Algorithm
  1. Time to sort the edges = O(E log E)
  2. In Union find operation in worst case is equal to O(log V), where V is the number of vertices.
  3. The dominant factor in the time complexity is usually the sorting step, so the overall time complexity is often expressed as O(E log E).

.

Space Complexity of Kruskal’s Algorithm

The overall space complexity of Kruskal’s algorithm can be expressed as O(E + V), where E is the number of edges, and V is the number of vertices.

.

Application of Kruskal’s Algorithm
  1. In the design of computer networks or telecommunication networks, Kruskal’s algorithm can be used to find the minimum cost infrastructure that connects all the nodes.
  2. Kruskal’s algorithm can be used in transportation planning to find the minimum cost road or railway network that connects different regions or cities.
  3. Kruskal’s algorithm can be used in image processing for segmenting images into regions with similar characteristics while minimizing the total cost of segmentation.
  4. In bioinformatics, Kruskal’s algorithm can be used to analyze the evolutionary relationships between different species based on genetic data.
  5. Kruskal’s algorithm can be used in data analysis to find the hierarchy of clusters in a dataset, where the goal is to group similar data points together.

Test Yourself

Q1- Explain the key difference between Kruskal’s algorithm and Prim’s algorithm for finding minimum spanning trees.

Kruskal’s algorithm is based on sorting edges by weight and adding them to the MST as long as no cycles are formed. It will create a forest of trees or disjoint set of trees.

Prim’s algorithm grows the MST from an initial vertex by adding the lightest edge connecting the growing MST to an external vertex.

Q2- Can Kruskal’s algorithm handle graphs with negative-weight edges? Explain why or why not.

No, Kruskal’s algorithm cannot handle graphs with negative-weight edges. It relies on the greedy approach, and negative edges can create a situation, where adding an edge later might be more beneficial, leading to an incorrect result.

Q3- Discuss a scenario where Kruskal’s algorithm might be more suitable than Prim’s algorithm.

Kruskal’s algorithm and Prim’s algorithm are both used to find the Minimum Spanning Tree (MST) in a graph, but they approach the problem in different ways.

Consider a scenario where you are tasked with designing a communication network for a city with numerous buildings, and the network needs to connect different buildings with minimal cost. The graph representing the city’s layout is sparse, meaning there are relatively few direct connections between buildings compared to the total number of buildings.
Kruskal’s algorithm operates on edges rather than vertices. In a sparse graph, where there are many buildings but relatively few direct connections, Kruskal’s algorithm can be more efficient.
Prim’s algorithm, on the other hand, is more focused on vertices and tends to perform well in dense graphs. In a sparse graph scenario, where there are fewer existing connections, the edge-based approach of Kruskal’s algorithm can lead to a more efficient solution.

Q4- How does Kruskal’s algorithm ensure that it does not form cycles in the process of building the Minimum Spanning Tree (MST)?

Kruskal’s algorithm uses the Union-Find data structure to keep track of disjoint sets. This data structure provides two essential operations:

Find (or Find-Set)

Given an element, this operation identifies the representative (root) of the set to which the element belongs.

Union

Merges two sets into a single set.

 

Before adding an edge to the MST, it checks if the source and destination vertices belong to the same set. If they do, adding the edge would create a cycle, so the edge is skipped.

Q5- Under what circumstances can Kruskal’s algorithm be less efficient than Prim’s algorithm?

Kruskal’s algorithm can be less efficient when the graph is dense (many edges), as the sorting step becomes more time-consuming. In such cases, Prim’s algorithm, which focuses on vertices, might perform better.

Q6- Discuss the impact of edge weights on the performance of Kruskal’s algorithm.

If all weights are distinct, the MST is unique.

If weights are not distinct, multiple MSTs may exist. In case of equal weights, Kruskal’s algorithm may choose different edges, leading to different MSTs.

So, if edge weights are distinct, sorting them can be done efficiently, resulting in a straightforward implementation with a time complexity of O(E log E).

Q7- In what type of graph would Kruskal’s algorithm have the same time complexity as Prim’s algorithm?

Kruskal’s algorithm has the same time complexity as Prim’s algorithm in a dense graph with a large number of vertices, where the sorting step dominates the overall complexity.

Q8- What is the time complexity of Kruskal’s algorithm in terms of sorting edges?
1. O(E log V)
2. O(V log E)
3. O(E log E)
4. O(V^2)

Ans – (3)

Explanation

The dominant factor is the sorting step, which takes O(E log E) time.

Q9- In Kruskal’s algorithm, when is an edge added to the MST?
1. If it has the maximum weight and does not form a cycle.
2. If it has the minimum weight and does not form a cycle.
3. If it connects the most vertices
4. If it forms a cycle

Ans – (2)

Explanation – Kruskal’s algorithm adds edges to the MST in non-decreasing order of weight.

Q10- What is the primary drawback of Kruskal’s algorithm in terms of space complexity?
 1. Requires a large amount of memory
 2. Requires a dynamic data structure
 3. Requires extra storage for subsets
 4. Requires a priority queue

Ans – (3)

Explanation – Extra storage is needed for the Union-Find data structure.

Q11- Kruskal’s algorithm can be less efficient than Prim’s algorithm in a dense graph. (True/False)

Ans – (True)

Explanation – Kruskal’s algorithm can be less efficient in a dense graph due to the sorting step.

Q12- In Kruskal’s algorithm, what is checked before adding an edge to the MST?
1. Edge weight
2. Source vertex
3. Destination vertex
4. Formation of a cycle

Ans – (4)

Explanation – The algorithm checks if adding the edge would create a cycle in the MST.

BOOKS

Algorithm T H CoremanAlgorithm by Udit AgarwalAlgorithm Ellis HorowitzData Structure & Algorithm Made Easy Narasimha

Â