Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph_exhaustive_planarity_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
47#include <algorithm>
48#include <map>
49#include <random>
50#include <set>
51#include <string>
52#include <utility>
53#include <vector>
54
55#include <gtest/gtest.h>
56
57#include <tpl_graph.H>
58#include <Planarity_Test.H>
59
61#include "graph_harness.H"
62#include "graph_oracles.H"
63
64using namespace Aleph;
65using namespace graph_harness;
66namespace oracle = graph_oracles;
68
69namespace
70{
71 using Adjacency = std::vector<std::vector<bool>>;
72
74 Adjacency simple_adjacency(const Spec & s, unsigned long hidden = 0)
75 {
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)
78 {
79 const int u = s.arcs[i][0], v = s.arcs[i][1];
80 if (u != v and graph_filters::shown(hidden, int(i)))
81 adj[size_t(u)][size_t(v)] = adj[size_t(v)][size_t(u)] = true;
82 }
83 return adj;
84 }
85
86 size_t num_edges(const Adjacency & adj)
87 {
88 size_t m = 0;
89 for (size_t u = 0; u < adj.size(); ++u)
90 for (size_t v = u + 1; v < adj.size(); ++v)
91 m += adj[u][v];
92 return m;
93 }
94
97 bool planar(const Adjacency & adj)
98 {
99 const int n = int(adj.size());
100 auto all_joined = [&] (const std::vector<int> & nodes, int a = -1, int b = -1)
101 {
102 for (size_t i = 0; i < nodes.size(); ++i)
103 for (size_t j = i + 1; j < nodes.size(); ++j)
104 {
105 const int u = nodes[i], v = nodes[j];
106 if ((u == a and v == b) or (u == b and v == a))
107 continue;
108 if (not adj[size_t(u)][size_t(v)])
109 return false;
110 }
111 return true;
112 };
113
114 // K5 on each set of 5 nodes; with 6, the node left out may split an
115 // edge
116 for (int out = 0; out < n; ++out)
117 {
118 if (n == 5 and out > 0)
119 break;
120 std::vector<int> five;
121 for (int x = 0; x < n; ++x)
122 if (n == 5 or x != out)
123 five.push_back(x);
124 if (five.size() != 5)
125 continue;
126 if (all_joined(five))
127 return false;
128 if (n == 6)
129 for (int a : five)
130 for (int b : 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))
133 return false;
134 }
135
136 // K3,3 on each split of 6 nodes in two triples
137 if (n == 6)
138 for (int x = 1; x < 6; ++x)
139 for (int y = x + 1; y < 6; ++y)
140 {
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)
145 other.push_back(z);
146 bool all = true;
147 for (int u : side)
148 for (int v : other)
149 all = all and adj[size_t(u)][size_t(v)];
150 if (all)
151 return false;
152 }
153 return true;
154 }
155
162 template <class GT>
163 std::string embedding_error(const Planarity_Test_Result<GT> & r, const Adjacency & adj,
164 size_t & faces)
165 {
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)
169 {
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]));
177 }
178 for (int x = 0; x < n; ++x)
179 {
180 std::vector<int> want;
181 for (int y = 0; y < n; ++y)
182 if (adj[size_t(x)][size_t(y)])
183 want.push_back(y);
184 std::vector<int> got = rot.count(x) ? rot[x] : std::vector<int>();
185 std::sort(got.begin(), got.end());
186 if (got != want)
187 return "the rotation of " + std::to_string(x) + " is not its neighbors";
188 }
189
190 // The faces, as the orbits of the darts: after u -> v comes v -> w,
191 // with w the neighbor of v after u in its rotation
192 std::set<std::pair<int, int>> seen;
193 const auto comp = [&]
194 {
195 Spec s;
196 s.n = n;
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});
201 return oracle::component_of(s);
202 }();
203 std::map<int, int> faces_of;
204 faces = 0;
205 for (int u = 0; u < n; ++u)
206 for (int v : rot[u])
207 {
208 if (seen.count({u, v}))
209 continue;
210 ++faces;
211 ++faces_of[comp[size_t(u)]];
212 int a = u, b = v;
213 while (seen.insert({a, b}).second)
214 {
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()];
220 a = b;
221 b = c;
222 }
223 if (a != u or b != v)
224 return "a face does not close";
225 }
226
227 std::map<int, int> nodes_of, edges_of;
228 for (int u = 0; u < n; ++u)
229 {
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)];
233 }
234 size_t with_edges = 0;
235 for (const auto & [c, e] : edges_of)
236 {
237 if (e == 0)
238 continue;
239 ++with_edges;
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";
243 }
244 faces = faces - with_edges + 1;
245 return "";
246 }
247
253 template <class GT>
254 std::string certificate_error(const Planarity_Test_Result<GT> & r, const Adjacency & adj)
255 {
256 const int n = int(adj.size());
257 if (not r.has_nonplanar_certificate)
258 return "no certificate";
259
260 if (r.certificate_type == Planarity_Certificate_Type::Minimal_NonPlanar_Obstruction)
261 {
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)
264 {
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;
270 }
271 return planar(sub) ? "the obstruction is planar" : "";
272 }
273
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 "
282 + std::to_string(r.certificate_paths.size()) + " paths";
283
284 std::set<int> inner;
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)
288 {
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";
296 ++degree[a];
297 ++degree[b];
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";
304 }
305
306 if (k5)
307 return ""; // 10 distinct pairs of 5 nodes: all of them
308 // The 9 pairs are those of K3,3: every branch node in 3, two sides
309 for (const auto & [x, d] : degree)
310 if (d != 3)
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; )
315 {
316 grew = false;
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))
320 {
321 side[y] = 1 - side[x];
322 grew = true;
323 }
324 }
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";
328 return "";
329 }
330
333 template <class GT>
334 void check_planarity(const Spec & s, unsigned long hidden, Check & answer,
335 Check & embedding, Check & certificate, Check & counts)
336 {
337 using Filter = Hide_Mask<GT>;
338 Built<GT> b(s);
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) + ": ";
342
343 answer.run();
344 const bool got = is_planar_graph<GT, Filter>(b.g, Filter{hidden});
345 if (got != want)
346 answer.fail(s, at + (got ? "says planar" : "says not planar"));
347
350 options.compute_nonplanar_certificate = true;
351 const auto r = planarity_test<GT, Filter>(b.g, Filter{hidden}, options);
352
353 // What it says of the input
354 counts.run();
355 size_t loops = 0, shown = 0;
356 for (size_t i = 0; i < s.arcs.size(); ++i)
357 if (graph_filters::shown(hidden, int(i)))
358 {
359 ++shown;
360 loops += s.arcs[i][0] == s.arcs[i][1];
361 }
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");
367
368 if (want)
369 {
370 embedding.run();
371 size_t faces = 0;
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);
376 // The graph without nodes has no faces, though 1 + C gives one
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));
380 }
381 else
382 {
383 certificate.run();
384 if (auto e = certificate_error(r, adj); not e.empty())
385 certificate.fail(s, at + e);
386 }
387 }
388}
389
390template <class GT>
391class ExhaustivePlanarity : public ::testing::Test {};
392
394
395TYPED_TEST(ExhaustivePlanarity, SimpleGraphsMatchKuratowski)
396{
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");
402
403 // Every simple graph of up to 5 nodes; a digraph takes each pair once,
404 // from the smaller node
405 for_each_simple_graph(0, 5, false, [&] (const Spec & s)
406 {
407 check_planarity<GT>(s, 0, answer, embedding, certificate, counts);
408 });
409
410 // Simple graphs of 6 nodes and 8 edges or more, where K3,3 and the
411 // split K5 are: a sample of them, or all with heavy
412 std::mt19937 rng(seed(733));
413 for_each_simple_graph(6, 6, false, [&] (const Spec & s)
414 {
415 if (s.num_arcs() >= 8 and (heavy() or rng() % 48 == 0))
416 check_planarity<GT>(s, 0, answer, embedding, certificate, counts);
417 });
418
419 answer.expect();
420 embedding.expect();
421 certificate.expect();
422 counts.expect();
423}
424
425TYPED_TEST(ExhaustivePlanarity, MultigraphsAndHiddenArcsMatchKuratowski)
426{
427 using GT = TypeParam;
428 const bool digraph = GT().is_digraph();
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");
433
434 // Every graph of 4 nodes or less is planar: loops, parallel arcs and
435 // hidden arcs are what these are for
436 for_each_spec(domain(3, 3), digraph, [&] (const Spec & s)
437 {
438 for (unsigned long hidden = 0; hidden < (1UL << s.num_arcs()); ++hidden)
439 check_planarity<GT>(s, hidden, answer, embedding, certificate, counts);
440 });
441
442 // K5 and K3,3 with each set of hidden arcs: hiding one makes them planar
443 Spec k5, k33;
444 k5.n = 5;
445 for (int u = 0; u < 5; ++u)
446 for (int v = u + 1; v < 5; ++v)
447 k5.arcs.push_back({u, v});
448 k33.n = 6;
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);
455
456 answer.expect();
457 embedding.expect();
458 certificate.expect();
459 counts.expect();
460}
Planarity testing on Aleph graphs.
List_Graph< Graph_Node< Node_Info >, Graph_Arc< Arc_Info > > GT
size_t size_t int32_t * out
Definition ca-c-api.h:120
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
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.
static mt19937 rng
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
Definition graphpic.C:406
static mpfr_t y
Definition mpfr_mul_d.c:3
size_t degree(const GT &g, typename GT::Node *p, SA &sa)
Degree of a node as an Eulerian walk counts it.
Definition eulerian.H:129
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.
Definition ah-date.H:140
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...
STL namespace.
static struct argp_option options[]
Definition ntreepic.C:1890
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)
ValueArg< size_t > seed
Definition testHash.C:53
Gsl_Compatible_Rng r
Generic graph and digraph implementations.