56#include <gtest/gtest.h>
79 void for_each_weighting(
Spec s,
long long lo,
long long hi,
F && f)
81 s.
w.assign(s.
arcs.size(), lo);
84 f(
static_cast<const Spec &
>(s));
86 while (i < s.
w.size()
and s.
w[i] == hi)
97 std::string forest_error(
const Spec & s,
const std::vector<int> &
arcs,
98 unsigned long hidden,
int start = -1)
100 std::vector<int> orig;
107 for (
size_t i = 0; i <
shown.arcs.size(); ++i)
108 if (start == -1 or comp[
size_t(
shown.arcs[i][0])] == comp[
size_t(start)])
111 part.
w.push_back(
shown.w[i]);
115 nodes = int(std::count(comp.begin(), comp.end(), comp[
size_t(start)]));
122 if (i < 0 or
size_t(i) >= s.
arcs.size())
123 return "an arc that is not of the graph";
125 return "the hidden arc " + std::to_string(i);
126 const auto & a = s.
arcs[size_t(i)];
127 if (start != -1
and comp[
size_t(a[0])] != comp[
size_t(start)])
128 return "the arc " + std::to_string(i) +
" out of the component of the start";
129 if (not uf.join(a[0], a[1]))
130 return "the arc " + std::to_string(i) +
" closes a cycle";
131 total += s.
w[size_t(i)];
133 if (
int(
arcs.size()) != want_arcs)
134 return std::to_string(
arcs.size()) +
" arcs, not " + std::to_string(want_arcs);
136 return "weight " + std::to_string(total) +
", not " + std::to_string(want);
142 std::vector<int> painted_arcs(
const GT & g)
145 for (
auto it = g.
get_arc_it(); it.has_curr(); it.next_ne())
147 r.push_back(
num(it.get_curr()));
154 std::vector<int> tree_arcs(
const GT & g,
const GT & tree)
158 for (
auto it = g.
get_node_it(); it.has_curr(); it.next_ne())
160 auto * p = it.get_curr();
161 auto * t = mapped_node<GT>(p);
162 if (t ==
nullptr or
num(t) !=
num(p) or mapped_node<GT>(t) != p)
166 for (
auto it = tree.
get_arc_it(); it.has_curr(); it.next_ne())
168 auto * ta = it.get_curr();
169 auto * ga = mapped_arc<GT>(ta);
170 if (ga ==
nullptr or
num(ga) !=
num(ta) or mapped_arc<GT>(ga) != ta)
172 r.push_back(
num(ga));
180 void check_spanning_trees(
const Spec & s,
unsigned long hidden,
bool every_start,
189 const std::string hiding = hidden == 0 ?
"" :
"hiding " + std::to_string(hidden) +
": ";
195 if (
auto e = forest_error(s, tree_arcs(b.g, tree), hidden); not e.empty())
196 kruskal.
fail(s, hiding + e);
202 if (
auto e = forest_error(s, painted_arcs(b.g), hidden); not e.empty())
203 kruskal_paint.
fail(s, hiding + e);
206 std::vector<int> starts;
207 for (
int x = 0; x < s.
n; ++x)
208 if (every_start or x == 0 or x == s.
n - 1)
210 for (
int start : starts)
212 const std::string from = hiding +
"from " + std::to_string(start) +
": ";
217 (b.g, b.nodes[
size_t(start)], tree);
218 if (
auto e = forest_error(s, tree_arcs(b.g, tree), hidden, start); not e.empty())
219 prim.
fail(s, from + e);
225 if (
auto e = forest_error(s, painted_arcs(b.g), hidden, start); not e.empty())
226 prim_paint.
fail(s, from + e);
239 using GT = TypeParam;
240 Check kruskal(
"Kruskal_Min_Spanning_Tree, the tree");
241 Check kruskal_paint(
"Kruskal_Min_Spanning_Tree, the painting");
242 Check prim(
"Prim_Min_Spanning_Tree, the tree");
243 Check prim_paint(
"Prim_Min_Spanning_Tree, the painting");
247 for_each_weighting(spec, -1, 1, [&] (
const Spec & s)
249 check_spanning_trees<GT>(s, 0,
true, kruskal, kruskal_paint, prim, prim_paint);
261 using GT = TypeParam;
262 Check kruskal(
"Kruskal_Min_Spanning_Tree, the tree");
263 Check kruskal_paint(
"Kruskal_Min_Spanning_Tree, the painting");
264 Check prim(
"Prim_Min_Spanning_Tree, the tree");
265 Check prim_paint(
"Prim_Min_Spanning_Tree, the painting");
272 for (
size_t i = 0; i < s.
arcs.size(); ++i)
273 s.
w.push_back(
static_cast<long long>(s.
arcs.size() - i));
274 for (
unsigned long hidden = 1; hidden < (1UL << s.
num_arcs()); ++hidden)
275 check_spanning_trees<GT>(s, hidden,
false, kruskal, kruskal_paint, prim, prim_paint);
286 using GT = TypeParam;
287 Check kruskal(
"Kruskal_Min_Spanning_Tree, the tree");
288 Check kruskal_paint(
"Kruskal_Min_Spanning_Tree, the painting");
289 Check prim(
"Prim_Min_Spanning_Tree, the tree");
290 Check prim_paint(
"Prim_Min_Spanning_Tree, the painting");
293 const int graphs =
heavy() ? 3000 : 300;
294 for (
int k = 0;
k < graphs; ++
k)
296 const int n = 5 + int(
rng() % 4);
297 const int m = n - 2 + int(
rng() %
unsigned(n + 2));
300 check_spanning_trees<GT>(s, 0,
true, kruskal, kruskal_paint, prim, prim_paint);
316 using GT = TypeParam;
342 unsigned long hidden)
344 std::vector<int> orig;
347 std::multiset<std::pair<int, int>> joined;
349 joined.
insert({std::min(a[0], a[1]), std::max(a[0], a[1])});
352 std::vector<Tree_Node<int> *> todo = {troot};
354 return "the root is " + std::to_string(troot->
get_key());
355 while (not todo.empty())
360 if (x < 0 or x >= s.
n or not seen.insert(x).second
361 or comp[size_t(x)] != comp[size_t(
root)])
362 return "the node " + std::to_string(x) +
" is twice or out of the component";
365 const auto it = joined.find({std::min(x, c->get_key()), std::max(x, c->get_key())});
366 if (it == joined.end())
367 return "no shown arc joins " + std::to_string(x) +
" and "
368 + std::to_string(c->get_key());
373 const auto size = std::count(comp.begin(), comp.end(), comp[
size_t(
root)]);
374 if (std::ptrdiff_t(seen.size()) !=
size)
375 return std::to_string(seen.size()) +
" nodes, not " + std::to_string(
size);
382 using GT = TypeParam;
384 Check function(
"graph_to_tree_node()");
385 Check functor(
"Graph_To_Tree_Node");
386 Check hides_cycle(
"Graph_To_Tree_Node, a filter that hides a cycle");
394 const std::string from =
"from " + std::to_string(
root) +
": ";
399 = graph_to_tree_node<GT, int, Copy_Key<GT>>(b.
g, b.
nodes[size_t(
root)]);
401 function.
fail(s, from +
"converts a graph with a cycle");
402 else if (
auto e = tree_node_error(s, t,
root, 0); not e.empty())
403 function.
fail(s, from + e);
406 catch (
const std::domain_error &)
409 function.
fail(s, from +
"rejects a forest");
412 for (
unsigned long hidden = 0; hidden < (1UL << s.
num_arcs()); ++hidden)
414 std::vector<int> orig;
416 Check &
check = cyclic
and not shown_cyclic ? hides_cycle : functor;
418 const std::string at = from +
"hiding " + std::to_string(hidden) +
": ";
424 check.fail(s, at +
"converts a graph with a cycle");
425 else if (
auto e = tree_node_error(s, t,
root, hidden); not e.empty())
426 check.fail(s, at + e);
429 catch (
const std::domain_error &)
431 if (not shown_cyclic)
432 check.fail(s, at +
"rejects a forest");
454 int src,
unsigned long hidden)
456 std::vector<int> orig;
459 std::multiset<std::pair<int, int>> joined;
461 if (a[0] != a[1]
and comp[size_t(a[0])] == comp[size_t(src)])
462 joined.
insert({std::min(a[0], a[1]), std::max(a[0], a[1])});
465 std::map<Df_Low_Tree_Node *, Df_Low_Tree_Node *> parent;
466 std::vector<Df_Low_Tree_Node *>
preorder;
467 std::vector<Df_Low_Tree_Node *> todo = {
root};
468 parent[
root] =
nullptr;
469 while (not todo.empty())
471 auto * t = todo.back();
474 std::vector<Df_Low_Tree_Node *> children;
477 children.push_back(c);
480 for (
auto it = children.rbegin(); it != children.rend(); ++it)
488 const int x = std::stoi(
k.label);
489 if (x < 0 or x >= s.
n or not seen.insert(x).second or comp[
size_t(x)] != comp[
size_t(src)])
490 return "the node " +
k.label +
" is twice or out of the component";
492 return "the df of " +
k.label +
" is " + std::to_string(
k.df) +
", not "
494 if (
auto * p = parent[
preorder[i]]; p !=
nullptr)
496 const int y = std::stoi(p->get_key().label);
497 const auto it = joined.find({std::min(x,
y), std::max(x,
y)});
498 if (it == joined.end())
499 return "no arc joins " +
k.label +
" and its parent " + p->get_key().label;
503 if (std::ptrdiff_t(seen.size()) != std::count(comp.begin(), comp.end(), comp[
size_t(src)])
504 or std::stoi(
root->get_key().label) != src)
505 return "the tree does not span the component";
509 std::map<Df_Low_Tree_Node *, long> reach;
510 for (
auto it = non_tree.
get_it(); it.has_curr(); it.next_ne())
512 auto [d, a] = it.get_curr();
513 bool ancestor =
false;
514 for (
auto * p = parent[d]; p !=
nullptr and not ancestor; p = parent[p])
516 const int x = std::stoi(d->get_key().label),
y = std::stoi(a->get_key().label);
517 const auto j = joined.find({std::min(x,
y), std::max(x,
y)});
518 if (not ancestor or j == joined.end())
519 return "the arc out of the tree " + std::to_string(x) +
"-" + std::to_string(
y)
520 +
" is not from a descendant to an ancestor, or is not left";
522 reach[d] = reach.count(d) ? std::min(reach[d], a->get_key().df) : a->get_key().
df;
524 if (not joined.empty())
525 return "an arc out of the tree is missing";
528 std::map<Df_Low_Tree_Node *, long>
low;
534 l = std::min(
l, reach[t]);
535 for (
auto * c = t->get_left_child(); c !=
nullptr; c = c->get_right_sibling())
536 l = std::min(
l,
low[c]);
538 const long want = t ==
root ? -1 :
l;
539 if (t->get_key().low != want)
540 return "the low of " + t->get_key().label +
" is " + std::to_string(t->get_key().low)
541 +
", not " + std::to_string(want);
549 using GT = TypeParam;
551 Check df_low(
"build_df_low_tree()");
556 for (
unsigned long hidden = 0; hidden < (1UL << s.
num_arcs()); ++hidden)
557 for (
int src = 0; src < s.
n; ++src)
564 if (
auto e = df_low_error(s,
root, non_tree, src, hidden); not e.empty())
565 df_low.
fail(s,
"from " + std::to_string(src) +
" hiding " + std::to_string(hidden)
Kruskal's minimum spanning tree algorithm.
Prim's algorithm for minimum spanning trees.
void erase() noexcept
See del().
size_t size() const noexcept
Return the current dimension of array.
Doubly-linked list (defined in tpl_dynList.H).
Functor class to convert a tree graph to Tree_Node structure.
Computes the minimum spanning tree of a graph using Kruskal's algorithm.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Computes the minimum spanning tree of a graph using Prim's algorithm.
Forward declaration used by CRTP helpers before the full node definition.
Tree_Node * get_left_child() const noexcept
Returns the leftmost child of this.
Tree_Node * get_right_sibling() const noexcept
Returns the right sibling of this.
T & get_key() noexcept
Returns a modifiable reference to the node contents.
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.
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
The failures of one property over a domain.
void expect() const
Expects what the mark says, and prints the first failures.
void fail(const Spec &s, const std::string &what)
Counts a failure on spec, with what went wrong.
void run() noexcept
Counts a run of the property.
Pictures of depth-first trees with their df and low numbers.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
TYPED_TEST_SUITE(ExhaustiveTrees, Undirected_Kinds)
TYPED_TEST(ExhaustiveTrees, SpanningTreesAreMinimumForEveryWeighting)
The kinds of arc filter and of distance that the graph algorithms must accept.
Every small multigraph, on every kind of graph, for the tests.
Answers computed from a Spec of graph_harness.H, without Aleph.
Convert spanning tree graphs to Tree_Node structures.
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
Df_Low_Tree_Node * build_df_low_tree(const GT &g, typename GT::Node *src, DynList< Df_Low_Non_Tree_Arc > &non_tree_arcs, Node_Label node_label=Node_Label(), SA sa=SA())
Build the depth-first spanning tree of an undirected graph, with the df and low numbers of its nodes.
#define IS_ARC_VISITED(p, bit)
Determine whether the bit field is or not set to one.
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
static long & low(typename GT::Node *p)
Internal helper: low-link value stored directly in NODE_COOKIE(p).
and
Check uniqueness with explicit hash + equality functors.
static long & df(typename GT::Node *p)
Internal helper: DFS discovery time stored in NODE_COUNTER(p).
constexpr bool shown(unsigned long hidden, int i) noexcept
Whether the arc number i is shown by the mask hidden.
::testing::Types< Aleph::List_Digraph< Aleph::Graph_Node< int >, Aleph::Graph_Arc< int > >, Aleph::List_SDigraph< Aleph::Graph_Snode< int >, Aleph::Graph_Sarc< int > >, Aleph::Array_Digraph< Aleph::Graph_Anode< int >, Aleph::Graph_Aarc< int > > > Directed_Kinds
The directed kinds, with int infos.
::testing::Types< Aleph::List_Graph< Aleph::Graph_Node< int >, Aleph::Graph_Arc< int > >, Aleph::List_SGraph< Aleph::Graph_Snode< int >, Aleph::Graph_Sarc< int > >, Aleph::Array_Graph< Aleph::Graph_Anode< int >, Aleph::Graph_Aarc< int > > > Undirected_Kinds
The kinds of graph that are not directed, with int infos.
size_t for_each_spec(const Domain &d, bool directed, F &&f)
Calls f(spec) with every spec of d, smallest first, and returns how many there were.
Spec random_spec(std::mt19937 &rng, int n, int m, bool directed, bool loops=true, bool parallel=true)
A random spec of n nodes and m arcs, with loops and parallel arcs if asked; parallel is ignored when ...
int num(Node *p)
The number of a node of a built graph.
Domain domain(int max_nodes, int max_arcs)
The domain of max_nodes nodes and max_arcs arcs, of one more of each in heavy mode,...
bool heavy()
Whether the mode is heavy, which asks for the larger domains.
void random_weights(std::mt19937 &rng, Spec &s, long long lo, long long hi)
Gives each arc of s a weight from lo to hi.
bool has_cycle(const Spec &s)
Whether the multigraph, not directed, has a cycle.
long long min_spanning_forest_weight(const Spec &s)
The weight of a minimum spanning forest: Kruskal on the list of arcs.
std::vector< int > component_of(const Spec &s, int skip_node=-1, int skip_arc=-1)
The number of the component of each node, as that of its root, of the graph without the node skip_nod...
int components(const Spec &s, int skip_node=-1, int skip_arc=-1)
The number of components, without the node skip_node and the arc skip_arc.
Spec without(const Spec &s, unsigned long hidden, std::vector< int > &orig)
The spec without the arcs whose numbers are set in hidden, with orig[i] the number in s of its i-th a...
Key of a node of a depth-first tree with df and low numbers.
Default label of a graph node: its information, written with operator <<.
Hides the arcs of a mask; copyable, assignable, noexcept.
The weight of an arc, read by its number from a vector.
A graph made from a spec, with its nodes and arcs by number.
std::vector< typename GT::Node * > nodes
A small multigraph: its nodes are 0 .
std::vector< long long > w
size_t num_arcs() const noexcept
std::vector< std::array< int, 2 > > arcs
Union-find over the nodes 0 .. n - 1.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Generic graph and digraph implementations.