13#include <gtest/gtest.h>
43size_t count_painted_arcs(
const G &g)
46 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
54int tree_total_weight(
const G &tree)
57 for (
auto it = tree.
get_arc_it(); it.has_curr(); it.next_ne())
58 total += it.get_current_arc_ne()->
get_info();
64int painted_total_weight(
const G &g)
67 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
69 auto arc = it.get_current_arc_ne();
71 total += arc->get_info();
80 using Distance_Type = int;
81 static const Distance_Type Zero_Distance = 0;
82 static const Distance_Type Max_Distance = std::numeric_limits<int>::max();
85 Scaled_Dist() =
default;
86 explicit Scaled_Dist(
int f) : factor(f) {}
88 int operator()(
typename G::Arc *a)
const noexcept
90 return a->get_info() * factor;
110bool is_tree_connected(
const G &tree)
135 auto tgt = it.get_tgt_node_ne();
151TEST(KruskalTest, SimpleConnectedGraph) {
176 ASSERT_EQ(tree_total_weight(tree), 6);
180TEST(KruskalTest, SingleNodeGraph) {
194TEST(KruskalTest, TwoNodesOneArc) {
207 ASSERT_EQ(tree_total_weight(tree), 5);
211TEST(KruskalTest, LinearChain) {
215 std::vector<Node*>
nodes;
232 ASSERT_EQ(tree_total_weight(tree), 10);
236TEST(KruskalTest, CompleteGraphK4) {
259 ASSERT_EQ(tree_total_weight(tree), 6);
267 const int num_leaves = 5;
268 std::vector<Node*> leaves;
270 for (
int i = 1; i <= num_leaves; ++i)
273 leaves.push_back(leaf);
282 ASSERT_EQ(tree.
get_num_nodes(),
static_cast<size_t>(num_leaves + 1));
283 ASSERT_EQ(tree.
get_num_arcs(),
static_cast<size_t>(num_leaves));
285 ASSERT_EQ(tree_total_weight(tree), 15);
289TEST(KruskalTest, GridGraph3x3) {
292 std::vector<std::vector<Node*>> grid(3, std::vector<Node*>(3));
296 for (
int i = 0; i < 3; ++i)
297 for (
int j = 0; j < 3; ++j)
301 for (
int i = 0; i < 3; ++i)
302 for (
int j = 0; j < 2; ++j)
303 g.
insert_arc(grid[i][j], grid[i][j+1], i + 1);
306 for (
int i = 0; i < 2; ++i)
307 for (
int j = 0; j < 3; ++j)
308 g.
insert_arc(grid[i][j], grid[i+1][j], j + 5);
317 ASSERT_TRUE(is_tree_connected(tree));
321TEST(KruskalTest, DiamondGraph) {
342 ASSERT_EQ(tree_total_weight(tree), 4);
346TEST(KruskalTest, AllEqualWeights) {
370 ASSERT_EQ(tree_total_weight(tree), 15);
374TEST(KruskalTest, UniqueWeights) {
395 ASSERT_EQ(tree_total_weight(tree), 7);
399TEST(KruskalTest, ZeroWeightEdges) {
415 ASSERT_EQ(tree_total_weight(tree), 0);
419TEST(KruskalTest, LargeWeights) {
436 ASSERT_EQ(tree_total_weight(tree), 2500000);
440TEST(KruskalTest, ParallelEdges) {
458 ASSERT_EQ(tree_total_weight(tree), 5);
478 ASSERT_EQ(tree_total_weight(tree), 2);
486 std::vector<Node*>
nodes;
503 ASSERT_EQ(tree_total_weight(tree), 10);
527 ASSERT_EQ(painted_total_weight(g), 6);
531TEST(KruskalTest, TreeBuildingMode) {
548 ASSERT_TRUE(is_tree_connected(tree));
552TEST(KruskalTest, TreeHasCorrectArcCount) {
556 std::vector<Node*>
nodes;
576TEST(KruskalTest, AllNodesInTree) {
593 std::set<int> node_infos;
594 for (
typename GT::Node_Iterator it(tree); it.has_curr(); it.next_ne())
595 node_infos.insert(it.get_curr()->get_info());
597 ASSERT_TRUE(node_infos.count(0));
598 ASSERT_TRUE(node_infos.count(1));
599 ASSERT_TRUE(node_infos.count(2));
600 ASSERT_TRUE(node_infos.count(3));
604TEST(KruskalTest, TreeIsConnected) {
608 std::vector<Node*>
nodes;
614 std::mt19937
rng(12345);
626 ASSERT_TRUE(is_tree_connected(tree));
630TEST(KruskalTest, BitFlagsCorrectlySet) {
650TEST(KruskalTest, NodeMappingCorrect) {
665 std::set<int> tree_infos;
666 for (
typename GT::Node_Iterator it(tree); it.has_curr(); it.next_ne())
667 tree_infos.insert(it.get_curr()->get_info());
669 ASSERT_TRUE(tree_infos.count(100));
670 ASSERT_TRUE(tree_infos.count(200));
671 ASSERT_TRUE(tree_infos.count(300));
675TEST(KruskalTest, ArcMappingCorrect) {
690 std::set<int> tree_weights;
691 for (
typename GT::Arc_Iterator it(tree); it.has_curr(); it.next_ne())
692 tree_weights.insert(it.get_curr()->get_info());
694 ASSERT_TRUE(tree_weights.count(5));
695 ASSERT_TRUE(tree_weights.count(7));
699TEST(KruskalTest, DigraphRejection) {
704 auto n0 = dg.insert_node(0);
705 auto n1 = dg.insert_node(1);
706 dg.insert_arc(n0, n1, 1);
709 EXPECT_THROW(kruskal(dg), std::domain_error);
726TEST(KruskalTest, CustomDistanceFunctor) {
737 Scaled_Dist<GT> dist(2);
745 ASSERT_EQ(tree_total_weight(tree), 5);
749TEST(KruskalTest, ArrayGraphType) {
766 ASSERT_EQ(tree_total_weight(tree), 3);
770TEST(KruskalTest, MultipleCallsSameInstance) {
796TEST(KruskalTest, LargeConnectedGraph) {
800 std::vector<Node*>
nodes;
810 std::mt19937
rng(99999);
811 std::uniform_int_distribution<int> ndist(0,
num_nodes - 1);
812 std::uniform_int_distribution<int> wdist(1, 1000);
828 ASSERT_TRUE(is_tree_connected(tree));
832TEST(KruskalTest, PaintThenBuildTree) {
847 int painted_weight = painted_total_weight(g);
852 int tree_weight = tree_total_weight(tree);
855 ASSERT_EQ(painted_weight, tree_weight);
859TEST(KruskalTest, DisconnectedGraphCreatesForest) {
882TEST(KruskalTest, VerifyMinimumWeightProperty) {
903 ASSERT_EQ(tree_total_weight(tree), 8);
907 ASSERT_LT(tree_total_weight(tree), 1 + 3 + 6);
908 ASSERT_LT(tree_total_weight(tree), 1 + 5 + 4);
909 ASSERT_LT(tree_total_weight(tree), 1 + 10 + 6);
913TEST(KruskalTest, RepeatedEdgesSameNodes) {
931 ASSERT_EQ(tree_total_weight(tree), 3);
935TEST(KruskalTest, IsPaintedGetter) {
961TEST(KruskalTest, DistanceAndFilterInVariables) {
976 Scaled_Dist<GT> reversed(-1);
979 kruskal(reversed, up_to_4);
984 ASSERT_EQ(tree_total_weight(tree), 4 + 3 + 2);
987 ASSERT_EQ(count_painted_arcs(g), 3u);
988 ASSERT_EQ(painted_total_weight(g), 4 + 3 + 2);
992TEST(KruskalTest, CopyKeepsItsOwnDistance) {
1006 using Reversed_Kruskal =
1008 std::optional<Reversed_Kruskal> original;
1010 Reversed_Kruskal
copy(*original);
1019 ASSERT_EQ(tree_total_weight(tree), 4 + 3 + 2);
1023TEST(KruskalTest, PaintedMinSpanningTreeUsesTheDistanceItWasGiven) {
1043 using Distance_Type = int;
1045 explicit Plus_Dist(
int e) noexcept : extra(e) {}
1046 int operator()(
Arc *a)
const noexcept {
return a->get_info() + extra; }
1054 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
1056 auto arc = it.get_current_arc_ne();
1058 ASSERT_EQ(plain(arc), painted);
1059 ASSERT_EQ(doubled(arc), painted);
1060 ASSERT_EQ(plus_one(arc), painted);
1062 ASSERT_EQ(plain.
dist, 1 + 2 + 4);
1063 ASSERT_EQ(doubled.
dist, 2 * (1 + 2 + 4));
1064 ASSERT_EQ(plus_one.
dist, (1 + 1) + (2 + 1) + (4 + 1));
1068TEST(KruskalTest, PaintedMinSpanningTreeLetsWhatTheDistanceThrowsThrough) {
1085 using Distance_Type = int;
1087 explicit Limited_Dist(
int l) noexcept : limit(
l) {}
1088 int operator()(
Arc *a)
const
1090 if (a->get_info() > limit)
1091 throw std::domain_error(
"weight above the limit");
1092 return a->get_info();
1098 ASSERT_TRUE(
filter(light));
1099 ASSERT_THROW(
filter(heavy), std::domain_error);
1100 ASSERT_FALSE(
filter(closing));
1101 ASSERT_EQ(
filter.dist, 1);
1107struct KruskalOnEveryGraph :
public ::testing::Test {};
1115std::vector<typename G::Node *> heavy_square(
G & g)
1117 std::vector<typename G::Node *> n;
1118 for (
int i = 0; i < 4; ++i)
1144 EXPECT_EQ(tree.get_num_arcs(), 3u);
1145 EXPECT_EQ(tree_total_weight(tree), 3);
1146 EXPECT_EQ(painted_total_weight(g), 3);
1152TEST(KruskalTest, LeavesTheArcsOfTheGraphInTheirOrder)
1155 std::mt19937 gen(7);
1156 std::vector<Node *> n;
1157 for (
int i = 0; i < 60; ++i)
1159 for (
int i = 1; i < 60; ++i)
1160 g.
insert_arc(n[i], n[gen() % i],
int(gen() % 4));
1161 for (
int k = 0;
k < 400; ++
k)
1162 g.
insert_arc(n[gen() % 60], n[gen() % 60],
int(gen() % 4));
1164 std::vector<Arc *> order;
1165 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
1166 order.push_back(it.get_current_arc_ne());
1168 auto tree_arcs = [&g, &order]
1172 std::vector<Arc *>
arcs, now;
1173 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
1175 now.push_back(it.get_current_arc_ne());
1177 arcs.push_back(now.back());
1179 EXPECT_EQ(now, order);
1183 const auto first = tree_arcs();
1184 EXPECT_EQ(first.size(), 59u);
1185 EXPECT_EQ(tree_arcs(), first);
1191TEST(KruskalTest, TakesEqualArcsInTheOrderOfTheGraph)
1194 std::mt19937 gen(11);
1195 std::vector<Node *> n;
1196 for (
int i = 0; i < 200; ++i)
1198 for (
int k = 0;
k < 2000; ++
k)
1199 g.
insert_arc(n[gen() % 200], n[gen() % 200],
int(gen() % 3));
1202 std::vector<Arc *> sorted;
1203 for (
int w = 0;
w < 3; ++
w)
1204 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
1205 if (it.get_current_arc_ne()->get_info() ==
w)
1206 sorted.push_back(it.get_current_arc_ne());
1209 std::vector<int> parent(200);
1210 for (
int i = 0; i < 200; ++i)
1212 auto find = [&parent](
int x)
1214 while (parent[x] != x)
1215 x = parent[x] = parent[parent[x]];
1219 for (
auto a : sorted)
1231 std::set<Arc *> painted;
1232 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
1234 painted.insert(it.get_current_arc_ne());
1241TEST(KruskalTest, MapsANewTreeAfterTheOldOneIsGone)
1244 std::vector<Node *> n;
1245 for (
int i = 0; i < 3; ++i)
1254 kruskal(g, old_tree);
1261 auto tp = mapped_node<GT>(p);
1262 ASSERT_NE(tp,
nullptr);
1263 EXPECT_EQ(mapped_node<GT>(tp), p);
1264 EXPECT_EQ(tp->get_info(), p->get_info());
1266 for (
auto it = tree.
get_arc_it(); it.has_curr(); it.next_ne())
1268 auto ta = it.get_current_arc_ne();
1269 auto ga = mapped_arc<GT>(ta);
1270 ASSERT_NE(ga,
nullptr);
1271 EXPECT_EQ(mapped_arc<GT>(ga), ta);
1272 EXPECT_EQ(ga->get_info(), ta->get_info());
1279 ::testing::InitGoogleTest(&argc, argv);
1280 return RUN_ALL_TESTS();
Kruskal's minimum spanning tree algorithm.
WeightedDigraph::Node Node
virtual Node * insert_node(Node *p)
Inserts the node p, made with new, which the graph takes.
Arc * insert_arc(Node *src, Node *tgt, void *a)
Generic directed graph (digraph) wrapper template.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
T remove_first()
See remove().
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
Arc for graphs implemented with simple adjacency lists.
constexpr bool is_empty() const noexcept
Computes the minimum spanning tree of a graph using Kruskal's algorithm.
bool is_painted() const noexcept
Returns true if the spanning tree has been painted on the graph.
Node * get_first_node() const
Return any node in the graph.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Graph_Node< int > Node
The graph type.
Graph_Arc< int > Arc
The node class type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Minimal std::expected-style result type for C++20.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
void reset_nodes() const
Reset all the nodes of graph (the control bits, the state, the counter and the cookie)
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
#define IS_NODE_VISITED(p, bit)
Determine whether the control bit is set or not to one.
#define IS_ARC_VISITED(p, bit)
Determine whether the bit field is or not set to one.
#define NODE_BITS(p)
Get the control bits of a node.
Main namespace for Aleph-w library functions.
Container2< typename Container1::Item_Type > filter(Container1 &container, Operation &operation)
Filter elements that satisfy operation.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Itor find(const Itor &beg, const Itor &end, const T &value)
Find the first element equal to a value.
Arc of graph implemented with double-linked adjacency lists.
Filtered iterator of adjacent arcs of a node.
Filter of painter arcs with that are set the Spanning_Tree control bit.
Distance::Distance_Type dist
Accumulative distance from the first seen arc until the last seen.
bool operator()(typename GT::Arc *a) const
TYPED_TEST_SUITE(KruskalOnEveryGraph, Graph_Kinds)
TYPED_TEST(KruskalOnEveryGraph, TakesTheLightestArcs)
Array-based graph implementation.
Generic graph and digraph implementations.
Simple graph implementation with adjacency lists.