54#include <gtest/gtest.h>
74 std::vector<std::vector<bool>> simple_adjacency(
const Spec & s,
bool directed,
75 unsigned long hidden = 0)
77 std::vector<std::vector<bool>> adj(
size_t(s.
n), std::vector<bool>(
size_t(s.
n),
false));
78 for (
size_t i = 0; i < s.
arcs.size(); ++i)
80 const int u = s.
arcs[i][0], v = s.
arcs[i][1];
83 adj[size_t(u)][size_t(v)] =
true;
85 adj[size_t(v)][size_t(u)] =
true;
92 bool colorable(
const std::vector<std::vector<bool>> & adj,
int k,
93 std::vector<int> &
color,
int x = 0)
95 const int n = int(adj.size());
98 for (
int c = 0; c <
k; ++c)
101 for (
int y = 0;
y < x
and ok; ++
y)
102 ok =
color[
size_t(
y)] != c or not (adj[
size_t(x)][
size_t(
y)] or adj[
size_t(
y)][
size_t(x)]);
105 color[size_t(x)] = c;
106 if (colorable(adj,
k,
color, x + 1))
113 int chromatic(
const std::vector<std::vector<bool>> & adj)
115 std::vector<int>
color(adj.size(), -1);
117 while (not colorable(adj,
k,
color))
125 bool hamiltonian(
const std::vector<std::vector<bool>> & adj,
bool directed)
127 const int n = int(adj.size());
128 if (n < (directed ? 2 : 3))
130 std::vector<int> order(
static_cast<size_t>(n));
131 std::iota(order.begin(), order.end(), 0);
135 for (
int i = 0; i < n
and ok; ++i)
136 ok = adj[
size_t(order[
size_t(i)])][size_t(order[
size_t((i + 1) % n)])];
140 while (std::next_permutation(order.begin() + 1, order.end()));
146 int simple_degree(
const std::vector<std::vector<bool>> & adj,
int x,
bool out =
true)
149 for (
size_t y = 0;
y < adj.size(); ++
y)
150 d +=
out ? adj[
size_t(x)][
y] : adj[
y][size_t(x)];
157 bool ore(
const std::vector<std::vector<bool>> & adj,
bool directed)
159 const int n = int(adj.size());
162 for (
int u = 0; u < n; ++u)
163 for (
int v = directed ? 0 : u + 1; v < n; ++v)
164 if (u != v
and not adj[
size_t(u)][size_t(v)])
165 if (simple_degree(adj, u) + simple_degree(adj, v, not directed) < n)
172 bool dirac(
const std::vector<std::vector<bool>> & adj,
bool directed)
174 const int n = int(adj.size());
177 for (
int x = 0; x < n; ++x)
178 if (2 * simple_degree(adj, x) < n or (directed
and 2 * simple_degree(adj, x,
false) < n))
185 bool multigraph(
const Spec & s,
bool directed,
unsigned long hidden = 0)
187 std::set<std::pair<int, int>> seen;
188 for (
size_t i = 0; i < s.
arcs.size(); ++i)
192 int u = s.
arcs[i][0], v = s.
arcs[i][1];
195 if (not directed
and u > v)
197 if (not seen.insert({u, v}).second)
209 std::string coloring_error(
const Built<GT> & b,
const std::vector<std::vector<bool>> & adj,
210 const Colors<GT> & colors,
size_t num)
212 const int n = int(adj.size());
213 std::vector<long> c(
size_t(n), -1);
214 for (
int x = 0; x < n; ++x)
216 auto * p = colors.search(b.
nodes[
size_t(x)]);
218 return "the node " + std::to_string(x) +
" has no color";
219 c[size_t(x)] = long(p->second);
221 std::set<long> used(c.begin(), c.end());
222 if (used.size() !=
num or (
num > 0
and *used.rbegin() !=
long(
num) - 1))
223 return "it says " + std::to_string(
num) +
" colors, and uses " + std::to_string(used.size());
224 for (
int u = 0; u < n; ++u)
225 for (
int v = 0; v < n; ++v)
226 if ((adj[
size_t(u)][
size_t(v)] or adj[
size_t(v)][
size_t(u)])
and c[
size_t(u)] == c[
size_t(v)])
227 return "the adjacent " + std::to_string(u) +
" and " + std::to_string(v) +
" share a color";
234 void for_each_test_graph(
bool directed,
int max_simple,
F && f)
248 using GT = TypeParam;
250 Check greedy(
"greedy_coloring()");
251 Check welsh_powell(
"welsh_powell_coloring()");
252 Check dsatur(
"dsatur_coloring()");
253 Check exact(
"chromatic_number()");
254 Check valid(
"is_valid_coloring()");
255 Check functors(
"Greedy_Coloring, Welsh_Powell_Coloring and DSatur_Coloring");
257 for_each_test_graph(digraph, digraph ? 4 : 5, [&] (
const Spec & s)
262 const auto adj = simple_adjacency(s,
false);
272 c.
fail(s,
"colors a graph with a loop");
273 else if (
auto e = coloring_error(b, adj, colors,
num); not e.empty())
277 catch (
const std::domain_error &)
280 c.
fail(s,
"throws on a graph without loops");
289 if (not loop
and int(chi) != chromatic(adj))
290 exact.
fail(s,
"gives " + std::to_string(chi) +
", not " + std::to_string(chromatic(adj)));
305 valid.
fail(s,
"rejects a proper coloring");
306 for (
size_t i = 0; i < s.
arcs.size(); ++i)
308 Colors<GT> spoiled = colors;
309 spoiled.find(b.
nodes[
size_t(s.
arcs[i][1])]) = spoiled.find(b.
nodes[
size_t(s.
arcs[i][0])]);
311 valid.
fail(s,
"accepts one color at both ends of the arc " + std::to_string(i));
315 Colors<GT> partial = colors;
316 partial.remove(b.
nodes[0]);
318 valid.
fail(s,
"accepts a node without a color");
332 using GT = TypeParam;
335 Check ore_simple(
"Test_Hamiltonian_Sufficiency, simple graphs");
336 Check dirac_simple(
"Test_Dirac_Condition, simple graphs");
337 Check ore_multi(
"Test_Hamiltonian_Sufficiency, multigraphs and hidden arcs");
338 Check dirac_multi(
"Test_Dirac_Condition, multigraphs and hidden arcs");
339 Check sound(
"a condition that holds has a Hamilton cycle");
341 auto run = [&] (
const Spec & s,
bool masks)
344 for (
unsigned long hidden = 0; hidden < (1UL << s.
num_arcs()); ++hidden)
348 const auto adj = simple_adjacency(s, digraph, hidden);
349 const bool simple = hidden == 0
and not multigraph(s, digraph);
350 const std::string at = hidden == 0 ?
"" :
"hiding " + std::to_string(hidden) +
": ";
357 Check & o = simple ? ore_simple : ore_multi;
359 if (got_ore != ore(adj, digraph))
360 o.
fail(s, at + (got_ore ?
"holds" :
"does not hold"));
362 Check & d = simple ? dirac_simple : dirac_multi;
364 if (got_dirac != dirac(adj, digraph))
365 d.
fail(s, at + (got_dirac ?
"holds" :
"does not hold"));
371 if ((got_ore or got_dirac)
and not hamiltonian(adj, digraph))
372 sound.
fail(s,
"a condition holds on a graph without a Hamilton cycle");
377 for_each_test_graph(digraph, digraph ? 4 : 5, [&] (
const Spec & s) { run(s,
true); });
383 dense.directions =
false;
384 dense.reversed =
false;
Graph coloring algorithms and heuristics.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
size_t size_t int32_t * out
Functor wrapper for dsatur_coloring.
Generic key-value map implemented on top of a binary search tree.
Functor wrapper for greedy_coloring.
Tests Dirac's sufficient condition for Hamiltonian graphs.
Tests Ore's sufficient condition for Hamiltonian graphs.
Functor wrapper for welsh_powell_coloring.
bool is_digraph() const noexcept
Return true if the graph this is directed.
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.
TYPED_TEST_SUITE(ExhaustiveProperties, All_Kinds)
TYPED_TEST(ExhaustiveProperties, ColoringsAreProperAndTheChromaticNumberExact)
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.
size_t chromatic_number(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors, SA sa=SA())
Computes the exact chromatic number of a small graph.
size_t greedy_coloring(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors, SA sa=SA())
Greedy graph coloring in iteration order.
bool is_valid_coloring(const GT &g, const DynMapTree< typename GT::Node *, size_t > &colors, SA sa=SA())
Validates a graph coloring.
size_t dsatur_coloring(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors, SA sa=SA())
DSatur graph coloring (saturation-degree heuristic).
size_t welsh_powell_coloring(const GT &g, DynMapTree< typename GT::Node *, size_t > &colors, SA sa=SA())
Welsh-Powell graph coloring.
Hamiltonian sufficiency testing for graphs.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
static long & color(typename GT::Node *p)
constexpr bool shown(unsigned long hidden, int i) noexcept
Whether the arc number i is shown by the mask hidden.
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.
size_t for_each_simple_graph(int min_nodes, int max_nodes, bool directed, F &&f)
Calls f(spec) with every simple graph of n nodes, for n from min_nodes to max_nodes: each set of pair...
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.
::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 > >, 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 > > > All_Kinds
The six kinds, with int infos.
Default filter for the graph nodes.
Hides the arcs of a mask; copyable, assignable, noexcept.
A graph made from a spec, with its nodes and arcs by number.
std::vector< typename GT::Node * > nodes
Which multigraphs for_each_spec() gives.
A small multigraph: its nodes are 0 .
size_t num_arcs() const noexcept
std::vector< std::array< int, 2 > > arcs
bool has_loop() const noexcept
Generic graph and digraph implementations.