-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkruskals_mst_algorithm.cpp
93 lines (69 loc) · 1.91 KB
/
kruskals_mst_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
#include<bits/stdc++.h>
using namespace std;
struct Edge {
int src, dst, weight;
};
class DisjointSet {
unordered_map<int, int> parent;
public:
void makeSet(int V){
// create V disjoint sets (one for each vertex)
for(int i = 0; i < V; i++)
parent[i] = i;
}
// Find the root of the set in which element k belongs
int Find(int k){
// if k is root
if (parent[k] == k)
return k;
// recur for parent until we find root
return Find(parent[k]);
}
void Union(int a, int b){
// find root of the sets in which elements x and y belongs
int x = Find(a);
int y = Find(b);
parent[x] = y;
}
};
vector<Edge> Kruskals(vector<Edge> edges, int V){
// stores edges present in MST
vector<Edge> MST;
// initialize DisjointSet class
DisjointSet ds;
// create singleton set for each element of universe
ds.makeSet(V);
// MST contains exactly V-1 edges
while (MST.size() != V - 1){
// consider next edge with minimum weight from the graph
Edge next_edge = edges.back();
edges.pop_back();
// find root of the sets to which two endpoint
// vertices of next_edge belongs
int x = ds.Find(next_edge.src);
int y = ds.Find(next_edge.dst);
// if both endpoints have different parents, they belong to
// different connected components and can be included in MST
if (x != y)
{
MST.push_back(next_edge);
ds.Union(x, y);
}
}
return MST;
}
bool comp(Edge a, Edge b){
return (a.weight > b.weight);
}
int main(){
vector<Edge> edges {{ 0, 1, 4 }, { 0, 7, 8 }, { 1, 2, 8 }, { 1, 7, 11 }, { 2, 3, 7 }, { 2, 8, 2 }, { 2, 5, 4 }, { 3, 4, 9 }, { 3, 5, 14 }, { 4, 5, 10 }, { 5, 6, 2 }, {6, 7, 1}, {6, 8, 6}, {7, 8, 7} };
// sort edges by increasing weight
sort(edges.begin(), edges.end(), comp);
int V = 9; // vertices
// construct graph
vector<Edge> e = Kruskals(edges, V);
for (auto &edge: e)
cout << "(" << edge.src << ", " << edge.dst << ", "
<< edge.weight << ")" << endl;
return 0;
}