-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMST.cc
159 lines (140 loc) · 3.5 KB
/
MST.cc
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
#include "Graph.hh"
#include "PQ.hh"
#include "UF.hh"
struct KruskalMST {
int wt{0};
ns::deque<Edge> mst;
KruskalMST(const EdgeWeightedGraph &G) {
MinPQ<Edge> pq(G.edges());
UF uf(G.V);
while (!pq.empty() && mst.size() < G.V - 1) {
Edge e{pq.delMin()};
int rootV{uf.find(e.v)}, rootW{uf.find(e.w)};
if (rootV == rootW)
continue;
uf.join(rootV, rootW);
mst.push_back(e), wt += e.weight;
}
}
ns::deque<Edge> edges() const { return mst; }
int weight() const { return wt; }
};
// priority first search minimun spanning forest
struct PrimMST {
ns::deque<Edge *> edgeTo;
ns::deque<int> distTo;
ns::deque<bool> marked;
IndexMinPQ<int> pq;
PrimMST(const EdgeWeightedGraph &G)
: edgeTo(G.V, nullptr), distTo(G.V, 0xffff), marked(G.V, false), pq(G.V) {
for (int v = 0; v < G.V; ++v)
if (!marked[v])
search(G, v);
}
void search(const EdgeWeightedGraph &G, int s) {
distTo[s] = 0;
pq.insert(s, distTo[s]);
while (!pq.empty()) {
int v{pq.delMin()};
scan(G, v);
}
}
void scan(const EdgeWeightedGraph &G, int v) {
marked[v] = true;
for (const auto &e : G.adj[v]) {
int w{e.other(v)};
if (marked[w])
continue;
if (e.weight < distTo[w]) {
distTo[w] = e.weight;
delete edgeTo[w];
edgeTo[w] = new Edge(e);
if (pq.contains(w))
pq.decreaseKey(w, distTo[w]);
else
pq.insert(w, distTo[w]);
}
}
}
~PrimMST() {
for (auto e : edgeTo)
delete e;
}
ns::deque<Edge> edges() const {
ns::deque<Edge> mst;
for (int v(0), sz(edgeTo.size()); v < sz; ++v)
if (auto e{edgeTo[v]})
mst.push_back(*e);
return mst;
}
int weight() const {
int weight = 0;
for (const auto &e : edges())
weight += e.weight;
return weight;
}
};
struct LazyPrimMST {
int wt;
ns::deque<Edge> mst;
ns::deque<bool> marked;
MinPQ<Edge> pq;
LazyPrimMST(const EdgeWeightedGraph &G) : wt{0}, marked(G.V, false), pq(G.E) {
for (int v = 0; v < G.V; ++v)
if (!marked[v])
search(G, v);
}
void search(const EdgeWeightedGraph &G, int s) {
scan(G, s);
while (!pq.empty()) {
Edge e{pq.delMin()};
int v{e.v}, w{e.w};
if (marked[v] && marked[w])
continue;
mst.push_back(e);
wt += e.weight;
if (!marked[v])
scan(G, v);
if (!marked[w])
scan(G, w);
}
}
void scan(const EdgeWeightedGraph &G, int v) {
marked[v] = true;
for (const auto &e : G.adj[v])
if (!marked[e.other(v)])
pq.insert(e);
}
ns::deque<Edge> edges() const { return mst; }
int weight() const { return wt; }
};
template <class MST> void printMST(const MST &mst) {
ns::deque<Edge> deck{mst.edges()};
for (const auto &e : deck)
std::print("[{}->{}|{}]\t", e.v, e.w, e.weight);
std::print("\nweight\t{}\n", mst.weight());
}
int main() {
constexpr int v{16}, e{32};
for (int k = 0; k < 16; k++) {
EdgeWeightedGraph EWG(v);
generateGraph(EWG, e);
std::print("EdgeWeightedGraph\n");
printGraph(EWG);
std::print("\n");
std::print("KruskalMST\n");
KruskalMST KMST(EWG);
printMST(KMST);
std::print("\n");
std::print("PrimMST\n");
PrimMST PMST(EWG);
printMST(PMST);
std::print("\n");
assert(KMST.weight() == PMST.weight());
std::print("LazyPrimMST\n");
LazyPrimMST LPMST(EWG);
printMST(LPMST);
std::print("\n");
assert(PMST.weight() == LPMST.weight());
}
}