GraphLibrary  0.0.1
A simple library that implements various graph algorithms.
 All Classes Namespaces Files Functions Variables Typedefs Macros
Kruskal.hpp
Go to the documentation of this file.
1 #ifndef GL_KRUSKAL_HPP
2 #define GL_KRUSKAL_HPP
3 
4 #include "../structures/DisjointSets.hpp"
5 #include "../algorithms/TransitiveClosure.hpp"
6 
7 namespace gl::algorithm {
8 
10 // Class declaration
12 
18 template <class Graph>
19 class Kruskal {
20  using Edge = typename Graph::Edge;
21  using idx_t = typename Graph::idx_t;
22  using val_t = typename Graph::val_t;
24 
25 public:
26  Kruskal();
27  explicit Kruskal(const Graph&);
28 
29  Kruskal(const Kruskal &) = default;
30  Kruskal(Kruskal &&) noexcept = default;
31  Kruskal &operator=(const Kruskal &) = default;
32  Kruskal &operator=(Kruskal &&) noexcept = default;
33  ~Kruskal() = default;
34 
41  std::function<std::pair<bool,gl::Color>(const idx_t src, const idx_t dest)> EdgeSelector(const gl::Color& trueColor = gl::Color("red"), const gl::Color& falseColor = gl::Color("black")) const;
48  std::function<std::pair<bool,gl::Color>(const idx_t node)> NodeSelector(const gl::Color& trueColor = gl::Color("red"), const gl::Color& falseColor = gl::Color("white")) const;
53  void compute(const Graph& graph);
58  val_t getCost() const;
63  Graph getMST() const;
64 
65 private:
66  bool isInitialized_ = false;
69 };
70 
72 // Member function implementations
74 
75 template <class Graph>
77 
78 template <class Graph>
80  compute(graph);
81 }
82 
83 template <class Graph>
84 void Kruskal<Graph>::compute(const Graph& graph)
85 {
86  // assert graph connectedness
87  GL_ASSERT(gl::algorithm::transitiveClosure(graph,0).size() == graph.numNodes(),(std::string("Kruskal::compute | '")+graph.getGraphLabel()+std::string("' is not connected.\n")))
88 
89  std::vector<Edge> edges;
90  Graph result(graph.numNodes(),std::string(std::string("MST of ")+graph.getGraphLabel()));
91  val_t cost {0};
92  DisjointSets disjointSets(graph.numNodes());
93 
94  // get vector of all edges in the graph
95  for (auto it = graph.edge_cbegin(); it != graph.edge_cend(); ++it)
96  {
97  edges.push_back(*it);
98  }
99  // sort edges by increasing weight
100  std::sort(edges.begin(),edges.end(),[](const Edge& lhs, const Edge& rhs)
101  {
102  return lhs.weight() < rhs.weight();
103  });
104 
105  // construct MST
106  for(auto edge : edges)
107  {
108  idx_t one = edge.source();
109  idx_t two = edge.dest();
110 
111  idx_t setOne = disjointSets.find(one);
112  idx_t setTwo = disjointSets.find(two);
113 
114  if (setOne != setTwo)
115  {
116  result.setEdge(edge);
117  cost += edge.weight();
118  disjointSets.merge(one,two);
119  }
120  }
121 
122  result_ = result;
123  cost_ = cost;
124  isInitialized_ = true;
125 }
126 
127 template <class Graph>
128 std::function<std::pair<bool,gl::Color>(const typename Graph::idx_t src, const typename Graph::idx_t dest)> Kruskal<Graph>::EdgeSelector (const gl::Color& trueColor, const gl::Color& falseColor) const
129 {
130  GL_ASSERT(isInitialized_,"Kruskal::EdgeSelector | Kruskal has not been initialized with a graph.")
131  return [trueColor, falseColor, this](const idx_t src, const idx_t dest) -> std::pair<bool,gl::Color> {
132  if (result_.hasEdge(src,dest))
133  return {true,trueColor};
134  else
135  return {false,falseColor};
136  };
137 }
138 
139 template <class Graph>
140 std::function<std::pair<bool,gl::Color>(const typename Graph::idx_t node)> Kruskal<Graph>::NodeSelector (const gl::Color& trueColor, const gl::Color& falseColor) const
141 {
142  GL_ASSERT(isInitialized_,"Kruskal::NodeSelector | Kruskal has not been initialized with a graph.")
143  return [trueColor, falseColor, this](const idx_t node) -> std::pair<bool,gl::Color> {
144  if (0 <= node && node < result_.numNodes())
145  return {true,trueColor};
146  else
147  return {false,falseColor};
148  };
149 }
150 
151 template <class Graph>
153 {
154  GL_ASSERT(isInitialized_,"Kruskal::getCost | Kruskal has not been initialized with a graph.")
155  return cost_;
156 }
157 
158 template <class Graph>
160 {
161  GL_ASSERT(isInitialized_,"Kruskal::getMST | Kruskal has not been initialized with a graph.")
162  auto result = result_;
163  return result;
164 }
165 
166 } // namespace gl::algorithm
167 
168 #endif // GL_Kruskal_HPP
val_t cost_
MST cost (sum of all edge weights)
Definition: Kruskal.hpp:68
SCALAR val_t
Value type.
Definition: Graph.hpp:43
ConstEdgeIterator edge_cbegin() const
ConstEdgeIterator to the first edge.
Definition: Graph.hpp:967
Graph result_
MST graph.
Definition: Kruskal.hpp:67
Stores an RGBA Color.
Definition: Color.hpp:21
Graph getMST() const
Returns a graph that only contains the edges of the MST.
Definition: Kruskal.hpp:159
gl::index_type idx_t
Index type.
Definition: Graph.hpp:44
Graph::ordered_list_t transitiveClosure(const Graph &graph, const typename Graph::idx_t node)
Implements an algorithm that finds the (reachability based) transitive closure of a node...
Definition: TransitiveClosure.hpp:17
Kruskal()
Default constructor.
Definition: Kruskal.hpp:76
void setEdge(const idx_t &start, const idx_t &end, const val_t &weight=1, const Color &color=Color())
Sets an edge including start/end points and weight.
Definition: Graph.hpp:510
Stores and implements a Graph.
Definition: Graph.hpp:39
typename Graph::Edge Edge
Definition: Kruskal.hpp:20
ConstEdgeIterator edge_cend() const
ConstEdgeIterator to the last edge.
Definition: Graph.hpp:989
std::function< std::pair< bool, gl::Color >const idx_t src, const idx_t dest)> EdgeSelector(const gl::Color &trueColor=gl::Color("red"), const gl::Color &falseColor=gl::Color("black")) const
Provides a Selector Object to color the edges in the MST.
Definition: Kruskal.hpp:128
std::string getGraphLabel() const
Returns the label of the graph.
Definition: Graph.hpp:390
#define GL_ASSERT(EXPR, ERROR_MSG)
Custom assert that supports attaching a message.
Definition: gl_assert.hpp:14
bool isInitialized_
Boolean storing initialization status.
Definition: Kruskal.hpp:66
idx_t numNodes() const
Returns the number of nodes currently in the graph.
Definition: Graph.hpp:406
typename Graph::val_t val_t
Definition: Kruskal.hpp:22
gl::Edge< val_t > Edge
Edge type.
Definition: Graph.hpp:45
std::function< std::pair< bool, gl::Color >const idx_t node)> NodeSelector(const gl::Color &trueColor=gl::Color("red"), const gl::Color &falseColor=gl::Color("white")) const
Provides a Selector Object to color the nodes in the MST.
Definition: Kruskal.hpp:140
Class that computes a Minimum Spanning Tree using Kruskal's algorithm.
Definition: Kruskal.hpp:19
void compute(const Graph &graph)
Computation. This is where the MST is computed based on a * greedy heuristic that only adds the cheap...
Definition: Kruskal.hpp:84
std::vector< bool > visit_list_t
Visited-List type.
Definition: Graph.hpp:50
Represents disjoint sets.
Definition: DisjointSets.hpp:15
val_t getCost() const
Computes the cost of the MST (sum of all edge weights in the MST).
Definition: Kruskal.hpp:152
typename Graph::visit_list_t visit_list_t
Definition: Kruskal.hpp:23
typename Graph::idx_t idx_t
Definition: Kruskal.hpp:21