Skip to content

Commit 4dff22f

Browse files
authored
Merge pull request #146 from NoobCoderReturns/patch-1
Added kruskals_algorithm.cpp (C++ solution using DSU and Greedy Approach)
2 parents 4bca4a4 + 634b4a1 commit 4dff22f

1 file changed

Lines changed: 116 additions & 0 deletions

File tree

CPP/graph/kruskals_algorithm.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Problem: Kruskal's Algorithm - Minimum Spanning Tree (MST)
2+
// Language: C++
3+
// Topic: Graphs / Greedy Algorithms / Disjoint Set Union
4+
// Description:
5+
// Kruskal’s algorithm finds the Minimum Spanning Tree (MST) of a connected, weighted graph.
6+
// It sorts all edges in ascending order of weight and adds them one by one,
7+
// avoiding cycles using the Disjoint Set Union (Union-Find) data structure.
8+
// Time Complexity: O(E * logE), where E = number of edges
9+
// Space Complexity: O(V), where V = number of vertices
10+
11+
#include <bits/stdc++.h>
12+
using namespace std;
13+
14+
class DSU {
15+
public:
16+
vector<int> parent, rank;
17+
18+
DSU(int n) {
19+
parent.resize(n);
20+
rank.resize(n, 0);
21+
for (int i = 0; i < n; i++)
22+
parent[i] = i;
23+
}
24+
25+
int find(int x) {
26+
if (x == parent[x])
27+
return x;
28+
return parent[x] = find(parent[x]); // Path compression
29+
}
30+
31+
bool unite(int x, int y) {
32+
int px = find(x);
33+
int py = find(y);
34+
35+
if (px == py)
36+
return false; // Same component, adding this edge creates a cycle
37+
38+
// Union by rank
39+
if (rank[px] < rank[py])
40+
parent[px] = py;
41+
else if (rank[py] < rank[px])
42+
parent[py] = px;
43+
else {
44+
parent[py] = px;
45+
rank[px]++;
46+
}
47+
48+
return true;
49+
}
50+
};
51+
52+
class Solution {
53+
public:
54+
// Each edge is represented as {u, v, weight}
55+
int kruskalMST(int V, vector<vector<int>>& edges) {
56+
// Sort edges by weight
57+
sort(edges.begin(), edges.end(),
58+
[](const vector<int>& a, const vector<int>& b) {
59+
return a[2] < b[2];
60+
});
61+
62+
DSU dsu(V);
63+
int mstWeight = 0;
64+
vector<vector<int>> mstEdges;
65+
66+
for (auto& e : edges) {
67+
int u = e[0], v = e[1], w = e[2];
68+
69+
if (dsu.unite(u, v)) {
70+
mstWeight += w;
71+
mstEdges.push_back({u, v, w});
72+
}
73+
}
74+
75+
// Print MST edges
76+
cout << "Edges in the Minimum Spanning Tree:\n";
77+
for (auto& e : mstEdges)
78+
cout << e[0] << " - " << e[1] << " : " << e[2] << "\n";
79+
80+
return mstWeight;
81+
}
82+
};
83+
84+
int main() {
85+
/*
86+
Example Graph:
87+
Vertices = 4
88+
Edges = 5
89+
Edge list: (u, v, w)
90+
0 -- 1 (10)
91+
0 -- 2 (6)
92+
0 -- 3 (5)
93+
1 -- 3 (15)
94+
2 -- 3 (4)
95+
96+
Expected MST:
97+
Edges -> (2,3,4), (0,3,5), (0,1,10)
98+
Total Weight = 19
99+
*/
100+
101+
int V = 4;
102+
vector<vector<int>> edges = {
103+
{0, 1, 10},
104+
{0, 2, 6},
105+
{0, 3, 5},
106+
{1, 3, 15},
107+
{2, 3, 4}
108+
};
109+
110+
Solution sol;
111+
int totalWeight = sol.kruskalMST(V, edges);
112+
113+
cout << "\nTotal weight of MST: " << totalWeight << endl;
114+
115+
return 0;
116+
}

0 commit comments

Comments
 (0)