-
Notifications
You must be signed in to change notification settings - Fork 527
/
kruskals_algorithm.cpp
123 lines (100 loc) · 3.05 KB
/
kruskals_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Structure to represent an edge
struct Edge {
int src, dest, weight;
};
// Structure to represent a connected, undirected, and weighted graph
class Graph {
public:
int V, E; // V is the number of vertices, E is the number of edges
vector<Edge> edges; // All edges
// Constructor
Graph(int V, int E) {
this->V = V;
this->E = E;
}
// Function to add an edge to the graph
void addEdge(int u, int v, int w) {
edges.push_back({u, v, w});
}
// Kruskal's algorithm to find MST
void kruskalMST();
};
// Structure to represent a subset for Union-Find
struct Subset {
int parent;
int rank;
};
// Function to find the parent of a node (with path compression optimization)
int find(Subset subsets[], int i) {
if (subsets[i].parent != i) {
subsets[i].parent = find(subsets, subsets[i].parent);
}
return subsets[i].parent;
}
// Function to unite two subsets by rank
void Union(Subset subsets[], int x, int y) {
int rootX = find(subsets, x);
int rootY = find(subsets, y);
if (subsets[rootX].rank < subsets[rootY].rank) {
subsets[rootX].parent = rootY;
} else if (subsets[rootX].rank > subsets[rootY].rank) {
subsets[rootY].parent = rootX;
} else {
subsets[rootY].parent = rootX;
subsets[rootX].rank++;
}
}
// Compare function for sorting edges by weight
bool compareEdges(Edge a, Edge b) {
return a.weight < b.weight;
}
// Kruskal's algorithm to find MST
void Graph::kruskalMST() {
vector<Edge> result; // Store the result MST
int e = 0; // Number of edges in MST
int i = 0; // Initial index for sorted edges
// Step 1: Sort all edges in non-decreasing order of their weights
sort(edges.begin(), edges.end(), compareEdges);
// Allocate memory for subsets for union-find
Subset* subsets = new Subset[V];
for (int v = 0; v < V; v++) {
subsets[v].parent = v;
subsets[v].rank = 0;
}
// Step 2: Pick the smallest edge and check if it forms a cycle
while (e < V - 1 && i < E) {
Edge nextEdge = edges[i++];
int x = find(subsets, nextEdge.src);
int y = find(subsets, nextEdge.dest);
// If including this edge doesn't form a cycle, include it in the result
if (x != y) {
result.push_back(nextEdge);
Union(subsets, x, y);
e++;
}
}
// Step 3: Print the result
cout << "Edges in the Minimum Spanning Tree:\n";
for (auto& edge : result) {
cout << edge.src << " -- " << edge.dest << " == " << edge.weight << endl;
}
delete[] subsets; // Free memory
}
int main() {
int V = 4; // Number of vertices
int E = 5; // Number of edges
Graph g(V, E);
// Adding edges to the graph (src, dest, weight)
g.addEdge(0, 1, 10);
g.addEdge(0, 2, 6);
g.addEdge(0, 3, 5);
g.addEdge(1, 3, 15);
g.addEdge(2, 3, 4);
// Perform Kruskal's algorithm to find the MST
g.kruskalMST();
return 0;
}