-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathPrimMST.cpp
30 lines (26 loc) · 951 Bytes
/
PrimMST.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
#include "PrimMST.h"
#include <limits>
void PrimMST::visit(const EdgeWeightedGraph &G, int v) {
marked[v] = true;
for (const auto &e: G.adj(v)) {
int w = e.other(v);
if (marked[w]) continue; // v-w is ineligible
if (e.weight() < distTo[w]) {
edgeTo[w] = e;
distTo[w] = e.weight();
if (pq.contains(w)) pq.change(w, distTo[w]);
else pq.insert(w, distTo[w]);
} // Edge e is new best connection from tree to w.
}
}
PrimMST::PrimMST(const EdgeWeightedGraph &G) : edgeTo(G.V()), distTo(G.V(), std::numeric_limits<double>::infinity()),
marked(G.V()), pq(G.V()) {
distTo[0] = 0.0;
pq.insert(0, 0.0);
while (!pq.isEmpty()) visit(G, *pq.delMin());
}
std::list<Edge> PrimMST::edges() const {
std::list<Edge> mst;
for (int v = 1; v < edgeTo.size(); ++v) mst.push_front(*edgeTo[v]);
return mst;
}