6 template<
typename GraphType,
typename OutputIter>
7 OutputIter
prim_mst(
const GraphType& G, OutputIter dest) {
12 std::set<typename GraphType::edge_t> protruding_edges;
16 for(
auto it = G.vbegin(); it != G.vend(); it++)
19 const typename GraphType::vertex_type start = *G.vbegin();
21 for(
auto it = G.aebegin(start); it != G.aeend(start); it++)
22 protruding_edges.insert(*it);
24 long long sz = G.vsize();
25 while(protruding_edges.size() != 0) {
27 edge_t lightest = *edges.begin();
28 edges.erase(*edges.begin());
30 vertex_t u = lightest.
either();
31 vertex_t v = lightest.
other(u);
33 bool u_in_T = T.find(u) != T.end();
34 bool v_in_T = T.find(v) != T.end();
36 if(u_in_T and !v_in_T) {
39 for(
auto it = G.aebegin(v); it != G.aeend(v); it++)
42 if(!u_in_T and v_in_T) {
45 for(
auto it = G.aebegin(v); it != G.aeend(v); it++)
50 assert(Tree.esize() == Tree.vsize() - 1);
60 edge_t(
long long a,
long long b,
long long weight) : u(a), v(b), wt(weight) { }
62 long long other(
long long vertex) {
return vertex == u ? v :
u; }
69 undirected_graph<long long, edge_t> g;
71 long long n, m, u, v, wt;
Definition: dijkstra.cpp:104
OutputIter prim_mst(const GraphType &G, OutputIter dest)
Definition: prim_mst.h:7
bool operator<(const edge_t other) const
Definition: prim_mst.h:64
long long weight()
Definition: dijkstra.cpp:113
bool operator==(const edge_t other) const
Definition: prim_mst.h:63
long long other(long long vertex)
Definition: prim_mst.h:62
long long v
Definition: dijkstra.cpp:106
edge_t(long long a, long long b, long long weight)
Definition: prim_mst.h:60
A generic tree data structre.
Definition: linked_impl.h:57
int main()
Definition: prim_mst.h:67
long long u
Definition: dijkstra.cpp:105
long long int wt
Definition: dijkstra.cpp:107
long long other()
Definition: dijkstra.cpp:112
long long either()
Definition: dijkstra.cpp:111