forked from strivedi4u/hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kruskal's_algorithm.cpp
94 lines (79 loc) · 1.94 KB
/
Kruskal's_algorithm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Edge {
int u, v, weight;
};
class DisjointSet {
public:
DisjointSet(int n) {
parent.resize(n);
rank.resize(n, 0);
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
int find(int u) {
if (u != parent[u]) {
parent[u] = find(parent[u]);
}
return parent[u];
}
void unionSets(int u, int v) {
int rootU = find(u);
int rootV = find(v);
if (rootU != rootV) {
if (rank[rootU] < rank[rootV]) {
parent[rootU] = rootV;
} else if (rank[rootU] > rank[rootV]) {
parent[rootV] = rootU;
} else {
parent[rootV] = rootU;
rank[rootU]++;
}
}
}
private:
vector<int> parent, rank;
};
class KruskalMST {
public:
void addEdge(int u, int v, int weight) {
edges.push_back({u, v, weight});
}
void findMST(int n) {
sort(edges.begin(), edges.end(), [](Edge a, Edge b) {
return a.weight < b.weight;
});
DisjointSet ds(n);
vector<Edge> mst;
for (const auto &edge : edges) {
if (ds.find(edge.u) != ds.find(edge.v)) {
ds.unionSets(edge.u, edge.v);
mst.push_back(edge);
}
}
for (const auto &edge : mst) {
cout << edge.u << " - " << edge.v << ": " << edge.weight << endl;
}
}
private:
vector<Edge> edges;
};
int main() {
int n, m;
cout << "Enter the number of vertices: ";
cin >> n;
cout << "Enter the number of edges: ";
cin >> m;
KruskalMST mst;
cout << "Enter the edges (u, v, weight):" << endl;
for (int i = 0; i < m; i++) {
int u, v, weight;
cin >> u >> v >> weight;
mst.addEdge(u, v, weight);
}
mst.findMST(n);
return 0;
}