-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathKruskal's Algorithm.cpp
More file actions
120 lines (110 loc) · 2.02 KB
/
Kruskal's Algorithm.cpp
File metadata and controls
120 lines (110 loc) · 2.02 KB
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
/*input
9 14
1 2 4
2 3 8
3 4 7
4 5 9
5 6 10
6 7 2
7 8 1
8 1 8
8 2 11
8 9 7
7 9 6
9 3 2
3 6 4
4 6 14
*/
/* Algorithm to find Minimum Cost Spanning Tree.
Time Complexity: O(ElogE)
*/
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
std::vector < std::pair < int, std::pair < int, int > > > v;
class Disjoint {
std::map < int, int > parent;
std::map < int, int > rank;
public:
void make_set(std::vector < int > &v) {
for(int i: v) {
parent[i] = i;
rank[i] = 0;
}
}
int Find(int a)
{
if(parent[a] != a) {
parent[a] = Find(parent[a]);
}
return parent[a];
}
void Union(int a, int b)
{
int x = Find(a);
int y = Find(b);
if(x == y) {
return ;
}
if(rank[x] > rank[y]) {
parent[y] = x;
}
else if(rank[y] > rank[x]) {
parent[x] = y;
}
else {
parent[x] = y;
rank[y] ++;
}
}
};
int Kruskal(int n)
{
std::vector < int > vp;
Disjoint ds;
for(int i = 1; i <= n; i ++) {
vp.push_back(i);
}
int mst = 0;
ds.make_set(vp);
for(auto x : v) {
int u = x.second.first;
int v = x.second.second;
int set_u = ds.Find(u);
int set_v = ds.Find(v);
if(set_u != set_v) {
std::cout << u << " " << v << "\n";
ds.Union(set_u, set_v);
mst += x.first;
}
}
return mst;
}
int main()
{
int n, m;
std::cin >> n >> m;
for(int i = 0; i < m; i ++) {
int x, y, z;
std::cin >> x >> y >> z;
v.push_back({z, {x, y}});
}
std::sort(v.begin(), v.end());
std::cout << "Spanning Tree is \n";
int minCost = Kruskal(n);
std::cout << "Minimum cost is " << minCost << "\n";
return 0;
}
/* Expected Output
Spanning Tree is
7 8
6 7
9 3
1 2
3 6
3 4
2 3
4 5
Minimum cost is 37
*/