-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMST.cpp
246 lines (210 loc) · 4.16 KB
/
MST.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
int V, E;
int input[1000][2];
double dinput[1000];
/*Begin Edge class */
class Edge {
int v, w;
public:
double weight;
Edge(int iv, int iw, double iweight) {
v = iv;
w = iw;
weight = iweight;
};
int either() {
return v;
};
int other(int vertex) {
if (vertex == v) return w;
else return v;
};
static int cmp(Edge me, Edge that) {
if (me.weight < that.weight) return -1;
else if (me.weight > that.weight) return 1;
else return 0;
};
bool operator<(const Edge &that) const {
return weight > that.weight;
};
/*bool operator()(const Edge &e1, const Edge &e2) const{
return e1.weight > e2.weight;
};*/
bool operator()(const Edge &lhs, const Edge &rhs) const
{
return lhs.weight > rhs.weight;
}
void printE() {
cout << v << " " << w << " " << weight << endl;
};
};
/*End Edge class*/
/*Begin EWGraph class*/
class EWGraph {
int V;
public:
vector<Edge> adj[1000];
vector<Edge> edges;
EWGraph(int iV) {
V = iV;
edges.clear();
for (int i=0; i<V; i++) {
vector<Edge> tmp;
adj[i] = tmp;
}
};
void addEdge(Edge e) {
int v = e.either();
int w = e.other(v);
adj[v].push_back(e);
adj[w].push_back(e);
edges.push_back(e);
};
int getV() {
return V;
};
};
/*End EWGraph*/
/*Union Find class*/
class UF {
int id[100001];
int N;
int uF_root(int p);
int sz[100001];
public:
UF(int n);
void uF_union(int p, int q);
bool uF_find(int p, int q);
};
UF::UF(int n) {
memset(id,0, sizeof(id));
memset(sz,0, sizeof(id));
N = n;
for (int i=0; i<N; i++) {
id[i] = i;
}
};
int UF::uF_root(int x) {
while (x != id[x]) {
id[x] = id[id[x]];
x = id[x];
}
return x;
};
bool UF::uF_find(int p, int q) {
return uF_root(p) == uF_root(q) ;
};
void UF::uF_union(int p, int q) {
int qr = uF_root(q);
int pr = uF_root(p);
if (qr==pr) return;
if(sz[qr] < sz[pr]) {
id[qr] = pr;
sz[pr]+=sz[qr];
} else {
id[pr] = qr;
sz[qr]+=sz[pr];
}
//id[pr] = qr;
}
/*End Union Find*/
struct Node {
int v, w, weight;
};
struct comp
{
bool operator()(const Node &lhs, const Node &rhs) const
{
return lhs.weight > rhs.weight;
}
};
/*Begin Kruskal*/
class KruskalMST {
public:
queue<Edge> mst;
KruskalMST(EWGraph G) {
while(!mst.empty()) {
mst.pop();
}
priority_queue<Edge> pq;
for (vector<Edge>::iterator it = G.edges.begin(); it!=G.edges.end(); it++) {
pq.push(*it);
}
UF uf(G.getV());
while (!pq.empty() && mst.size() < G.getV()-1) {
Edge e = pq.top();
pq.pop();
int v = e.either();
int w = e.other(v);
if (!uf.uF_find(v,w)) {
uf.uF_union(v,w);
mst.push(e);
}
}
}
};
/*End Kruskal*/
/*Begin Lazy Prim*/
class LazyPrimMST {
bool marked[1000];
priority_queue<Edge> pq;
void visit(EWGraph G, int v) {
marked[v] = true;
for (int i=0; i< G.adj[v].size(); i++) {
if (!marked[G.adj[v][i].other(v)]) {
pq.push(G.adj[v][i]);
}
}
};
public:
queue<Edge> mst;
LazyPrimMST(EWGraph G) {
while(!pq.empty()) pq.pop();
while(!mst.empty()) mst.pop();
memset(marked, false, sizeof(marked));
visit(G,0);
while (!pq.empty()) {
Edge e = pq.top();
pq.pop();
int v = e.either();
int w = e.other(v);
if (!marked[v] && !marked[w]) continue;
mst.push(e);
if (!marked[v]) visit(G, v);
if (!marked[w]) visit(G, w);
}
}
};
/*End Lazy Prim*/
int main() {
scanf_s("%d %d", &V, &E);
for (int i=0; i<E; i++) {
scanf_s("%d %d", &input[i][0], &input[i][1]);
scanf_s("%lf", &dinput[i]);
}
EWGraph G(V);
for (int i=0; i< E; i++) {
Edge e(input[i][0], input[i][1], dinput[i]);
G.addEdge(e);
}
cout << "Kruskal MST:" << endl;
KruskalMST KMST(G);
while (!KMST.mst.empty()) {
Edge tmp = KMST.mst.front();
KMST.mst.pop();
tmp.printE();
};
cout << "Prim lazy MST:" << endl;
LazyPrimMST LPMST(G);
while (!LPMST.mst.empty()) {
Edge tmp = LPMST.mst.front();
LPMST.mst.pop();
tmp.printE();
}
return 0;
}