55#include <gtest/gtest.h>
71 using Adjacency = std::vector<std::vector<bool>>;
74 Adjacency simple_adjacency(
const Spec & s,
unsigned long hidden = 0)
76 Adjacency adj(
size_t(s.
n), std::vector<bool>(
size_t(s.
n),
false));
77 for (
size_t i = 0; i < s.
arcs.size(); ++i)
79 const int u = s.
arcs[i][0], v = s.
arcs[i][1];
81 adj[size_t(u)][size_t(v)] = adj[size_t(v)][size_t(u)] =
true;
86 size_t num_edges(
const Adjacency & adj)
89 for (
size_t u = 0; u < adj.size(); ++u)
90 for (
size_t v = u + 1; v < adj.size(); ++v)
97 bool planar(
const Adjacency & adj)
99 const int n = int(adj.size());
100 auto all_joined = [&] (
const std::vector<int> &
nodes,
int a = -1,
int b = -1)
102 for (
size_t i = 0; i <
nodes.size(); ++i)
103 for (
size_t j = i + 1; j <
nodes.size(); ++j)
106 if ((u == a
and v == b) or (u == b
and v == a))
108 if (not adj[
size_t(u)][size_t(v)])
120 std::vector<int> five;
121 for (
int x = 0; x < n; ++x)
122 if (n == 5 or x !=
out)
124 if (five.size() != 5)
126 if (all_joined(five))
131 if (a < b
and adj[size_t(
out)][size_t(a)]
and adj[size_t(
out)][size_t(b)]
132 and all_joined(five, a, b))
138 for (
int x = 1; x < 6; ++x)
139 for (
int y = x + 1;
y < 6; ++
y)
141 const std::vector<int> side = {0, x,
y};
142 std::vector<int> other;
143 for (
int z = 1; z < 6; ++z)
144 if (z != x
and z !=
y)
166 const int n = int(adj.size());
167 std::map<int, std::vector<int>> rot;
168 for (
size_t i = 0; i <
r.embedding_rotation.size(); ++i)
170 const auto & e =
r.embedding_rotation[i];
171 const int x =
num(e.node);
172 if (x < 0 or x >= n or rot.count(x))
173 return "the node " + std::to_string(x) +
" is twice in the rotation";
174 auto & around = rot[x];
175 for (
size_t j = 0; j < e.cw_neighbors.size(); ++j)
176 around.push_back(
num(e.cw_neighbors[j]));
178 for (
int x = 0; x < n; ++x)
180 std::vector<int> want;
181 for (
int y = 0;
y < n; ++
y)
182 if (adj[
size_t(x)][
size_t(
y)])
184 std::vector<int> got = rot.count(x) ? rot[x] : std::vector<int>();
185 std::sort(got.begin(), got.end());
187 return "the rotation of " + std::to_string(x) +
" is not its neighbors";
192 std::set<std::pair<int, int>> seen;
193 const auto comp = [&]
197 for (
int u = 0; u < n; ++u)
198 for (
int v = u + 1; v < n; ++v)
199 if (adj[
size_t(u)][
size_t(v)])
200 s.
arcs.push_back({u, v});
203 std::map<int, int> faces_of;
205 for (
int u = 0; u < n; ++u)
208 if (seen.count({u, v}))
211 ++faces_of[comp[size_t(u)]];
213 while (seen.insert({a, b}).second)
215 const auto & around = rot[b];
216 const auto at = size_t(std::find(around.begin(), around.end(), a) - around.begin());
217 if (at == around.size())
218 return "the rotation of " + std::to_string(b) +
" misses " + std::to_string(a);
219 const int c = around[(at + 1) % around.size()];
223 if (a != u or b != v)
224 return "a face does not close";
227 std::map<int, int> nodes_of, edges_of;
228 for (
int u = 0; u < n; ++u)
230 ++nodes_of[comp[size_t(u)]];
231 for (
int v = u + 1; v < n; ++v)
232 edges_of[comp[
size_t(u)]] += adj[size_t(u)][size_t(v)];
234 size_t with_edges = 0;
235 for (
const auto & [c, e] : edges_of)
240 if (nodes_of[c] - e + faces_of[c] != 2)
241 return "Euler's formula fails in a component: " + std::to_string(nodes_of[c])
242 +
" nodes, " + std::to_string(e) +
" edges, " + std::to_string(faces_of[c]) +
" faces";
244 faces = faces - with_edges + 1;
256 const int n = int(adj.size());
257 if (not
r.has_nonplanar_certificate)
258 return "no certificate";
260 if (
r.certificate_type == Planarity_Certificate_Type::Minimal_NonPlanar_Obstruction)
262 Adjacency
sub(
size_t(n), std::vector<bool>(
size_t(n),
false));
263 for (
size_t i = 0; i <
r.certificate_obstruction_edges.size(); ++i)
265 const int u =
num(
r.certificate_obstruction_edges[i].src);
266 const int v =
num(
r.certificate_obstruction_edges[i].tgt);
267 if (not adj[
size_t(u)][
size_t(v)])
268 return "an edge of the obstruction is not of the graph";
269 sub[size_t(u)][size_t(v)] =
sub[size_t(v)][size_t(u)] =
true;
271 return planar(sub) ?
"the obstruction is planar" :
"";
274 const bool k5 =
r.certificate_type == Planarity_Certificate_Type::K5_Subdivision;
275 if (not k5
and r.certificate_type != Planarity_Certificate_Type::K33_Subdivision)
276 return std::string(
"a certificate of type ") +
to_string(
r.certificate_type);
277 std::set<int> branch;
278 for (
size_t i = 0; i <
r.certificate_branch_nodes.size(); ++i)
279 branch.insert(
num(
r.certificate_branch_nodes[i]));
280 if (branch.size() != (k5 ? 5U : 6U) or
r.certificate_paths.
size() != (k5 ? 10U : 9U))
281 return
"the certificate has " +
std::
to_string(branch.
size()) +
" branch nodes and "
285 std::set<std::pair<int, int>> joined;
286 std::map<int, int>
degree;
287 for (
size_t i = 0; i <
r.certificate_paths.size(); ++i)
289 const auto & p =
r.certificate_paths[i];
290 if (p.nodes.size() < 2)
291 return "a path of less than two nodes";
292 const int a =
num(p.nodes[0]), b =
num(p.nodes[p.nodes.size() - 1]);
293 if (not branch.count(a) or not branch.count(b) or a == b
294 or not joined.insert({std::min(a, b), std::max(a, b)}).second)
295 return "a path does not join two branch nodes, or joins them twice";
298 for (
size_t j = 0; j + 1 < p.nodes.size(); ++j)
299 if (not adj[
size_t(
num(p.nodes[j]))][
size_t(
num(p.nodes[j + 1]))])
300 return "a path takes an edge that is not of the graph";
301 for (
size_t j = 1; j + 1 < p.nodes.size(); ++j)
302 if (branch.count(
num(p.nodes[j])) or not inner.insert(
num(p.nodes[j])).second)
303 return "two paths share an inner node";
309 for (
const auto & [x, d] :
degree)
311 return
"a branch node of K3,3 is in " +
std::
to_string(d) +
" paths";
312 std::map<int, int> side;
313 side[*branch.begin()] = 0;
314 for (
bool grew =
true; grew; )
317 for (
const auto & [a, b] : joined)
318 for (const auto & [x,
y] : {std::pair(a, b), std::pair(b, a)})
319 if (side.count(x)
and not side.count(
y))
321 side[
y] = 1 - side[x];
325 for (
const auto & [a, b] : joined)
326 if (side[a] == side[b])
327 return
"the paths of K3,3 join two nodes of one side";
334 void check_planarity(
const Spec & s,
unsigned long hidden,
Check & answer,
339 const auto adj = simple_adjacency(s, hidden);
340 const bool want = planar(adj);
341 const std::string at = hidden == 0 ?
"" :
"hiding " + std::to_string(hidden) +
": ";
344 const bool got = is_planar_graph<GT, Filter>(b.g, Filter{hidden});
346 answer.
fail(s, at + (got ?
"says planar" :
"says not planar"));
350 options.compute_nonplanar_certificate =
true;
351 const auto r = planarity_test<GT, Filter>(b.g, Filter{hidden},
options);
355 size_t loops = 0,
shown = 0;
356 for (
size_t i = 0; i < s.
arcs.size(); ++i)
360 loops += s.
arcs[i][0] == s.
arcs[i][1];
362 if (
r.is_planar != want or
r.num_nodes !=
size_t(s.
n) or
r.num_input_arcs != shown
363 or
r.simplified_num_edges != num_edges(adj) or
r.ignored_loops != loops
364 or
r.ignored_parallel_arcs != shown - loops - num_edges(adj)
365 or
r.input_is_digraph != b.g.is_digraph())
366 counts.
fail(s, at +
"wrong counts of the input");
372 if (not
r.has_combinatorial_embedding)
373 embedding.
fail(s, at +
"no embedding");
374 else if (
auto e = embedding_error(
r, adj, faces); not e.empty())
375 embedding.
fail(s, at + e);
377 else if (
r.embedding_num_faces != (s.
n == 0 ? 0 : faces))
378 embedding.fail(s, at +
"it says " +
std::
to_string(
r.embedding_num_faces)
379 +
" faces, and the rotation gives " +
std::
to_string(faces));
384 if (
auto e = certificate_error(
r, adj); not e.empty())
385 certificate.
fail(s, at + e);
397 using GT = TypeParam;
398 Check answer(
"is_planar_graph()");
399 Check embedding(
"the embedding of a planar graph");
400 Check certificate(
"the certificate of a graph that is not planar");
401 Check counts(
"the counts of Planarity_Test_Result");
407 check_planarity<GT>(s, 0, answer, embedding, certificate, counts);
416 check_planarity<GT>(s, 0, answer, embedding, certificate, counts);
427 using GT = TypeParam;
429 Check answer(
"is_planar_graph()");
430 Check embedding(
"the embedding of a planar graph");
431 Check certificate(
"the certificate of a graph that is not planar");
432 Check counts(
"the counts of Planarity_Test_Result");
438 for (
unsigned long hidden = 0; hidden < (1UL << s.
num_arcs()); ++hidden)
439 check_planarity<GT>(s, hidden, answer, embedding, certificate, counts);
445 for (
int u = 0; u < 5; ++u)
446 for (
int v = u + 1; v < 5; ++v)
447 k5.arcs.push_back({u, v});
449 for (
int u = 0; u < 3; ++u)
450 for (
int v = 3; v < 6; ++v)
451 k33.arcs.push_back({u, v});
452 for (
const Spec * s : {&k5, &k33})
453 for (
unsigned long hidden = 0; hidden < (1UL << s->
num_arcs()); hidden += hidden < 64 ? 1 : 37)
454 check_planarity<GT>(*s, hidden, answer, embedding, certificate, counts);
Planarity testing on Aleph graphs.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
size_t size_t int32_t * out
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(ExhaustivePlanarity, SimpleGraphsMatchKuratowski)
TYPED_TEST_SUITE(ExhaustivePlanarity, All_Kinds)
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.
DynArray< Graph::Node * > nodes
size_t degree(const GT &g, typename GT::Node *p, SA &sa)
Degree of a node as an Eulerian walk counts it.
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
and
Check uniqueness with explicit hash + equality functors.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
double sub(double a, double b)
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.
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...
static struct argp_option options[]
Configuration options for planarity testing and auxiliary outputs.
bool compute_embedding
If true and the graph is found to be planar, computes a combinatorial embedding (rotation system and ...
Complete result of a planarity test execution.
Hides the arcs of a mask; copyable, assignable, noexcept.
A graph made from a spec, with its nodes and arcs by number.
A small multigraph: its nodes are 0 .
size_t num_arcs() const noexcept
std::vector< std::array< int, 2 > > arcs
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Generic graph and digraph implementations.