DSA Lab Manual(MST)
DSA Lab Manual(MST)
1. Objective:
To implement Kruskal's algorithm for finding the Minimum Spanning Tree (MST) of a weighted,
undirected graph using C++.
2. Introduction:
Kruskal’s algorithm is a greedy algorithm that finds the Minimum Spanning Tree (MST) of a
graph. It works by sorting the edges of the graph in increasing order of their weights and adding
the edges one by one to the MST, ensuring that no cycles are formed.
3. Pre-requisites:
4. Data Structures:
Graph Representation: An edge list, where each edge is represented by a pair of vertices and a
weight.
Disjoint Set (Union-Find): Used to detect cycles efficiently by keeping track of the connected
components.
5. Algorithm:
6. Pseudocode:
Kruskal (Graph):
1. Sort all the edges in increasing order of weight.
2. Create a Disjoint Set (Union-Find) for all vertices.
3. Initialize MST as an empty set.
4. For each edge (u, v, weight) in the sorted edge list:
a. If find(u)! = find(v), then:
i. Add (u, v, weight) to MST.
ii. Union (u, v).
5. Return MST.
7. C++ Implementation:
#include <iostream>
#include <vector>
#include <algorithm>
class DisjointSet {
public:
vector<int> parent, rank;
DisjointSet(int n) {
parent.resize(n);
rank.resize(n, 0);
for (int i = 0; i < n; i++)
parent[i] = i;
}
int find(int x) {
if (parent[x] != x)
parent[x] = find(parent[x]); // Path compression
return parent[x];
}
if (rootX != rootY) {
if (rank[rootX] < rank[rootY])
parent[rootX] = rootY;
else if (rank[rootX] > rank[rootY])
parent[rootY] = rootX;
else {
parent[rootY] = rootX;
rank[rootX]++;
}
}
}
};
struct Edge {
int u, v, weight;
bool operator<(const Edge &e) const {
return weight < e.weight;
}
};
class KruskalMST {
public:
vector<Edge> edges;
int vertices;
KruskalMST(int V) {
vertices = V;
}
void kruskal() {
sort(edges.begin(), edges.end());
DisjointSet ds(vertices);
vector<Edge> mst;
if (ds.find(u) != ds.find(v)) {
mst.push_back(edge);
ds.unionSets(u, v);
}
}
// Output MST
cout << "Edges in the Minimum Spanning Tree:\n";
int totalWeight = 0;
for (auto edge : mst) {
cout << edge.u << " - " << edge.v << " : " << edge.weight
<< endl;
totalWeight += edge.weight;
}
cout << "Total weight of MST: " << totalWeight << endl;
}
};
int main() {
KruskalMST graph(4);
graph.addEdge(0, 1, 10);
graph.addEdge(0, 2, 6);
graph.addEdge(0, 3, 5);
graph.addEdge(1, 3, 15);
graph.addEdge(2, 3, 4);
graph.kruskal();
return 0;
}
DisjointSet Class: Implements the union-find data structure with path compression and union by
rank for efficient cycle detection.
Edge Struct: Represents an edge with two vertices (u and v) and its weight. It also implements
the comparison operator (<) to allow sorting by edge weight.
KruskalMST Class: Manages the graph and implements Kruskal’s algorithm. The kruskal()
function sorts edges, applies the union-find structure, and adds edges to the MST while avoiding
cycles.
Main Function: Initializes the graph, adds edges, and calls the kruskal() function to find and
display the MST.
9. Sample Output:
Edges in the Minimum Spanning Tree:
2 - 3 : 4
0 - 3 : 5
0 - 1 : 10
Total weight of MST: 19
Sorting the edges: O(E log E), where E is the number of edges.
Each find and union operation: O(α(V)), where V is the number of vertices and α is the inverse
Ackermann function, which grows very slowly.
Total complexity: O(E log E + E α(V)).
O(V + E), where V is the number of vertices and E is the number of edges.