4 #include "../structures/DisjointSets.hpp"
5 #include "../algorithms/TransitiveClosure.hpp"
7 namespace gl::algorithm {
18 template <
class Graph>
75 template <class
Graph>
78 template <
class Graph>
83 template <
class Graph>
89 std::vector<Edge> edges;
100 std::sort(edges.begin(),edges.end(),[](
const Edge& lhs,
const Edge& rhs)
102 return lhs.weight() < rhs.weight();
106 for(
auto edge : edges)
108 idx_t one = edge.source();
109 idx_t two = edge.dest();
111 idx_t setOne = disjointSets.find(one);
112 idx_t setTwo = disjointSets.find(two);
114 if (setOne != setTwo)
117 cost += edge.weight();
118 disjointSets.merge(one,two);
124 isInitialized_ =
true;
127 template <
class Graph>
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};
135 return {
false,falseColor};
139 template <
class Graph>
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};
147 return {
false,falseColor};
151 template <
class Graph>
154 GL_ASSERT(isInitialized_,
"Kruskal::getCost | Kruskal has not been initialized with a graph.")
158 template <
class Graph>
161 GL_ASSERT(isInitialized_,
"Kruskal::getMST | Kruskal has not been initialized with a graph.")
162 auto result = result_;
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