13#include <gtest/gtest.h>
40int tree_total_weight(
const G &tree)
43 for (
auto it = tree.
get_arc_it(); it.has_curr(); it.next_ne())
44 total += it.get_current_arc_ne()->
get_info();
50bool is_tree_connected(
const G &tree)
75 auto tgt = it.get_tgt_node_ne();
102 EXPECT_THROW(prim(g, tree), std::exception);
131 EXPECT_EQ(tree_total_weight(tree), 10);
152 EXPECT_EQ(tree_total_weight(tree), 3);
153 EXPECT_TRUE(is_tree_connected(tree));
177 EXPECT_EQ(tree_total_weight(tree), 6);
178 EXPECT_TRUE(is_tree_connected(tree));
185TEST(PrimVsKruskal, SameWeightOnSimpleGraph)
202 GT prim_tree, kruskal_tree;
208 kruskal(g, kruskal_tree);
210 EXPECT_EQ(tree_total_weight(prim_tree), tree_total_weight(kruskal_tree));
211 EXPECT_TRUE(is_tree_connected(prim_tree));
212 EXPECT_TRUE(is_tree_connected(kruskal_tree));
215TEST(PrimVsKruskal, RandomGraph)
217 std::mt19937
rng(42);
218 std::uniform_int_distribution<int> weight_dist(1, 100);
222 std::vector<Node *>
nodes;
224 for (
int i = 0; i <
N; ++i)
228 for (
int i = 1; i <
N; ++i)
232 for (
int i = 0; i <
N * 2; ++i)
240 GT prim_tree, kruskal_tree;
246 kruskal(g, kruskal_tree);
248 EXPECT_EQ(tree_total_weight(prim_tree), tree_total_weight(kruskal_tree));
255TEST(PrimEdgeCases, AllEqualWeights)
277 EXPECT_EQ(tree_total_weight(tree), 3);
278 EXPECT_TRUE(is_tree_connected(tree));
281TEST(PrimEdgeCases, LinearGraph)
302 EXPECT_EQ(tree_total_weight(tree), 10);
303 EXPECT_TRUE(is_tree_connected(tree));
312 for (
int i = 1; i <= 5; ++i)
324 EXPECT_EQ(tree_total_weight(tree), 15);
325 EXPECT_TRUE(is_tree_connected(tree));
334 std::mt19937
rng(12345);
335 std::uniform_int_distribution<int> weight_dist(1, 1000);
339 std::vector<Node *>
nodes;
341 for (
int i = 0; i <
N; ++i)
345 for (
int i = 1; i <
N; ++i)
349 for (
int i = 0; i <
N * 3; ++i)
357 GT prim_tree, kruskal_tree;
363 kruskal(g, kruskal_tree);
366 EXPECT_EQ(prim_tree.
get_num_arcs(),
static_cast<size_t>(
N - 1));
367 EXPECT_TRUE(is_tree_connected(prim_tree));
370 EXPECT_EQ(tree_total_weight(prim_tree), tree_total_weight(kruskal_tree));
375 std::mt19937
rng(54321);
376 std::uniform_int_distribution<int> weight_dist(1, 100);
380 std::vector<Node *>
nodes;
382 for (
int i = 0; i <
N; ++i)
386 for (
int i = 0; i <
N; ++i)
387 for (
int j = i + 1; j <
N; ++j)
390 GT prim_tree, kruskal_tree;
396 kruskal(g, kruskal_tree);
399 EXPECT_EQ(prim_tree.
get_num_arcs(),
static_cast<size_t>(
N - 1));
400 EXPECT_TRUE(is_tree_connected(prim_tree));
403 EXPECT_EQ(tree_total_weight(prim_tree), tree_total_weight(kruskal_tree));
410TEST(PrimMapping, NodesAreMapped)
425 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
427 auto gnode = it.get_curr();
428 auto tnode = mapped_node<GT>(gnode);
429 EXPECT_NE(tnode,
nullptr);
430 EXPECT_EQ(gnode->get_info(), tnode->get_info());
438TEST(PrimTextbook, ClassicExample)
468 EXPECT_EQ(tree_total_weight(tree), 12);
469 EXPECT_TRUE(is_tree_connected(tree));
480struct Reversible_Distance
482 using Distance_Type = int;
486 int operator()(
Arc *a)
const
488 return reversed ? 100 - a->get_info() : a->get_info();
505template <
class Distance>
506int tree_distance(
const GT &tree,
const Distance &d)
509 for (
auto it = tree.
get_arc_it(); it.has_curr(); it.next_ne())
510 total += d(it.get_current_arc_ne());
515template <
class Distance>
516int painted_distance(
const GT &g,
const Distance &d)
519 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
521 total += d(it.get_current_arc_ne());
530TEST(PrimDistance, UsesTheDistanceItWasGiven)
540 const Reversible_Distance reversed{
true};
545 EXPECT_EQ(tree_distance(tree, reversed), 98 + 95);
548 EXPECT_EQ(painted_distance(g, reversed), 98 + 95);
552TEST(PrimDistance, MatchesKruskalWithTheGivenDistance)
555 std::uniform_int_distribution<int> weight_dist(1, 99);
556 const Reversible_Distance reversed{
true};
558 for (
int iter = 0; iter < 20; ++iter)
562 std::vector<Node *>
nodes;
563 for (
int i = 0; i <
N; ++i)
565 for (
int i = 1; i <
N; ++i)
567 for (
int i = 0; i < 2 *
N; ++i)
569 const int a =
rng() %
N;
570 const int b =
rng() %
N;
575 GT prim_tree, kruskal_tree;
579 kruskal(Reversible_Distance{
true});
580 kruskal(g, kruskal_tree);
582 EXPECT_EQ(tree_distance(prim_tree, reversed),
583 tree_distance(kruskal_tree, reversed)) <<
"graph " << iter;
586 EXPECT_EQ(painted_distance(g, reversed),
587 tree_distance(kruskal_tree, reversed)) <<
"graph " << iter;
592TEST(PrimDistance, TakesADistanceWithoutADefaultConstructor)
607 EXPECT_EQ(tree_distance(tree, twice), 2 * (1 + 2));
610 EXPECT_EQ(painted_distance(g, twice), 2 * (1 + 2));
619size_t painted_arcs(
const GT &g)
622 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
632TEST(PrimPainting, PaintsTheArcsOfTheTree)
654 EXPECT_EQ(painted_arcs(g), 5u);
658 EXPECT_EQ(painted_arcs(g), 5u);
665TEST(PrimPainting, LeavesOutALoopOnTheFirstNode)
678 EXPECT_EQ(painted_arcs(g), 2u);
683 EXPECT_EQ(tree_total_weight(tree), 2 + 3);
688TEST(PrimPainting, LeavesTheCookiesNullWithALoopOnTheFirstNode)
700 EXPECT_EQ(painted_arcs(g), 2u);
709struct Watching_Distance
711 using Distance_Type = int;
713 Arc *watched =
nullptr;
714 bool *read =
nullptr;
716 int operator()(
Arc *a)
const
720 return a->get_info();
728TEST(PrimMapping, LeavesOutALoopOnTheFirstNode)
741 prim(Watching_Distance{loop, &read});
744 EXPECT_EQ(tree.get_num_arcs(), 2u);
745 EXPECT_EQ(tree_total_weight(tree), 1 + 2);
747 EXPECT_EQ(painted_arcs(g), 2u);
752 EXPECT_EQ(painted_arcs(g), 2u);
770 if (++calls == fail_at)
771 throw std::runtime_error(
"arc weight not available");
786 return a->get_info();
795TEST(PrimExceptions, DistanceThatThrowsLeavesTheGraphReusable)
797 std::mt19937
rng(11);
798 std::uniform_int_distribution<int> weight_dist(1, 50);
801 std::vector<Node *>
nodes;
802 for (
int i = 0; i <
N; ++i)
804 for (
int i = 1; i <
N; ++i)
806 for (
int i = 0; i < 2 *
N; ++i)
808 const int a =
rng() %
N;
809 const int b =
rng() %
N;
817 const int weight = tree_total_weight(
expected);
822 const std::vector<std::function<void()>> methods = {
826 EXPECT_EQ(tree_total_weight(tree), weight);
834 for (
size_t m = 0;
m < methods.size(); ++
m)
838 const long calls = failure.calls;
839 ASSERT_GT(calls, 0) <<
"method " <<
m;
841 for (
long k = 1;
k <= calls; ++
k)
845 EXPECT_THROW(methods[
m](), std::runtime_error)
846 <<
"method " <<
m <<
", call " <<
k;
850 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
866int fragile_creations = 0;
867int fragile_fail_at = 0;
873 Fragile_Node(
const int & info) :
Graph_Node<int>(info)
875 if (fragile_fail_at != 0
and ++fragile_creations == fragile_fail_at)
876 throw std::bad_alloc();
886TEST(PrimExceptions, ANodeOfTheTreeThatCannotBeCreatedLeaksNothing)
889 std::vector<Fragile_Node *>
nodes;
890 for (
int i = 0; i < 5; ++i)
891 nodes.push_back(g.insert_node(i));
892 for (
int i = 1; i < 5; ++i)
897 for (
int k = 1;
k <= 5; ++
k)
904 fragile_creations = 0;
907 EXPECT_THROW(prim(g,
nodes[0], tree), std::bad_alloc) <<
"node " <<
k;
916 prim(g,
nodes[0], again);
917 EXPECT_EQ(tree_total_weight(again), 1 + 2 + 3 + 4) <<
"node " <<
k;
928struct PrimMappingTest :
public ::testing::Test {};
930using Representations =
946 std::uniform_int_distribution<int> weight_dist(1, 30);
949 std::vector<typename G::Node *>
nodes;
950 for (
int i = 0; i <
N; ++i)
952 for (
int i = 1; i <
N; ++i)
954 for (
int i = 0; i < 2 *
N; ++i)
956 const int a =
rng() %
N;
957 const int b =
rng() %
N;
964 prim(g,
nodes[0], tree);
968 std::set<typename G::Node *> tree_nodes;
969 for (
auto it = tree.
get_node_it(); it.has_curr(); it.next_ne())
970 tree_nodes.insert(it.get_curr());
971 std::set<typename G::Arc *> graph_arcs;
972 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
973 graph_arcs.insert(it.get_curr());
977 auto t = mapped_node<G>(p);
978 ASSERT_TRUE(tree_nodes.contains(t));
979 EXPECT_EQ(mapped_node<G>(t), p);
980 EXPECT_EQ(t->get_info(), p->get_info());
983 for (
auto it = tree.
get_arc_it(); it.has_curr(); it.next_ne())
985 auto ta = it.get_curr();
986 auto a = mapped_arc<G>(ta);
987 ASSERT_TRUE(graph_arcs.contains(a));
988 EXPECT_EQ(mapped_arc<G>(a), ta);
Kruskal's minimum spanning tree algorithm.
Prim's algorithm for minimum spanning trees.
WeightedDigraph::Node Node
Default distance accessor for arc weights.
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.
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)
Graph class implemented with singly-linked adjacency lists.
Number Theoretic Transform over Z / MOD Z.
Computes the minimum spanning tree of a graph using Prim's algorithm.
Minimal std::expected-style result type for C++20.
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
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)
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
DynArray< Graph::Node * > nodes
#define IS_NODE_VISITED(p, bit)
Determine whether the control bit is set or not to one.
#define NODE_COOKIE(p)
Return the node cookie
#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.
and
Check uniqueness with explicit hash + equality functors.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Spec reversed(const Spec &s)
The spec with each arc reversed; the arcs keep their numbers.
TYPED_TEST(PrimMappingTest, NodesAndArcsMapBothWays)
TYPED_TEST_SUITE(PrimMappingTest, Representations)
Arc of graph implemented with double-linked adjacency lists.
Node belonging to a graph implemented with a double linked adjacency list.
Filtered iterator of adjacent arcs of a node.
int operator()(IntGraph::Arc *a) const
int operator()(IntGraph::Arc *a) const
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.
Simple graph implementation with adjacency lists.