0% found this document useful (0 votes)
5 views4 pages

DSA Lab Manual(MST)

The document outlines Kruskal's algorithm for finding the Minimum Spanning Tree (MST) of a weighted, undirected graph using C++. It details the algorithm's steps, data structures, C++ implementation, and provides an explanation of the code along with sample output. Additionally, it discusses the time and space complexity of the algorithm.

Uploaded by

Irfan Ul Haq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

DSA Lab Manual(MST)

The document outlines Kruskal's algorithm for finding the Minimum Spanning Tree (MST) of a weighted, undirected graph using C++. It details the algorithm's steps, data structures, C++ implementation, and provides an explanation of the code along with sample output. Additionally, it discusses the time and space complexity of the algorithm.

Uploaded by

Irfan Ul Haq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Kruskal’s Algorithm for Minimum Spanning Tree (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:

 Basic knowledge of graphs and trees.


 Understanding of Disjoint Set Union (DSU) or Union-Find data structure.
 Knowledge of sorting algorithms (for sorting edges by weight).

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:

1. Sort all the edges of the graph in increasing order of weights.


2. Initialize a Disjoint Set data structure to manage the connected components.
3. For each edge (u, v) in the sorted edge list:
o If u and v belong to different components (i.e., they are not connected), add the edge to
the MST and unite their components.
o If they belong to the same component, skip this edge to avoid forming a cycle.
4. Repeat the process until all vertices are connected.

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>

using namespace std;

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];
}

void unionSets(int x, int y) {


int rootX = find(x);
int rootY = find(y);

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 addEdge(int u, int v, int weight) {


edges.push_back({u, v, weight});
}

void kruskal() {
sort(edges.begin(), edges.end());

DisjointSet ds(vertices);
vector<Edge> mst;

for (auto edge : edges) {


int u = edge.u;
int v = edge.v;

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;
}

8. Explanation of the Code:

 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

10. Time Complexity:

 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)).

11. Space Complexity:

 O(V + E), where V is the number of vertices and E is the number of edges.

You might also like