prim_mst.h
Go to the documentation of this file.
1 
2 #include <set>
3 
4 #pragma once
5 
6 template<typename GraphType, typename OutputIter>
7 OutputIter prim_mst(const GraphType& G, OutputIter dest) {
8 
9  /* Maintain a set of edges that go out of the tree;
10  * In each iteration, pick the lightest of them and include it in Tree */
11 
12  std::set<typename GraphType::edge_t> protruding_edges;
13 
14  GraphType Tree;
15 
16  for(auto it = G.vbegin(); it != G.vend(); it++)
17  Tree.add_vertex(*it);
18 
19  const typename GraphType::vertex_type start = *G.vbegin();
20 
21  for(auto it = G.aebegin(start); it != G.aeend(start); it++)
22  protruding_edges.insert(*it);
23 
24  long long sz = G.vsize();
25  while(protruding_edges.size() != 0) {
26 
27  edge_t lightest = *edges.begin();
28  edges.erase(*edges.begin());
29 
30  vertex_t u = lightest.either();
31  vertex_t v = lightest.other(u);
32 
33  bool u_in_T = T.find(u) != T.end();
34  bool v_in_T = T.find(v) != T.end();
35 
36  if(u_in_T and !v_in_T) {
37  res++ = lightest;
38  T.insert(v);
39  for(auto it = G.aebegin(v); it != G.aeend(v); it++)
40  edges.insert(*it);
41  }
42  if(!u_in_T and v_in_T) {
43  res++ = lightest;
44  T.insert(u);
45  for(auto it = G.aebegin(v); it != G.aeend(v); it++)
46  edges.insert(*it);
47  }
48  }
49 
50  assert(Tree.esize() == Tree.vsize() - 1);
51 
52  return res;
53 }
54 
55 
56 struct edge_t {
57  long long u;
58  long long v;
59  long long int wt;
60  edge_t(long long a, long long b, long long weight) : u(a), v(b), wt(weight) { }
61  long long either() { return u; }
62  long long other(long long vertex) { return vertex == u ? v : u; }
63  bool operator==(const edge_t other) const { return u == other.u and v == other.v; }
64  bool operator<(const edge_t other) const { return wt < other.wt; }
65 };
66 
67 int main() {
68 
69  undirected_graph<long long, edge_t> g;
70 
71  long long n, m, u, v, wt;
72  cin >> n >> m;
73  while(m--) {
74  cin >> u >> v >> wt;
75  edge_t e(u, v, wt);
76  g.add_edge(e);
77  }
78 
79  vector<edge_t> mst;
80  prim_mst(g, back_inserter(mst));
81 
82  long long cost = 0;
83  for(edge_t edge : mst)
84  cost += edge.wt;
85 
86  cout << cost << endl;
87 }
88 
89 /*
90 
91 concept graph {
92  Iterators
93 
94  vbegin();
95  vend();
96  ebegin();
97  eend();
98  avbegin(vertex);
99  avend();
100  aebegin(vertex);
101  aeend(vertex);
102 
103  Interface
104  add_edge
105  add_vertex
106 };
107 
108 */
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