Backport relevant parts of bugfixes [ad22262328b3], [61fdd06833a6] and [4add05447ca0] to branch 1.2 (#623)
1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
21 #include <lemon/planarity.h>
23 #include <lemon/smart_graph.h>
24 #include <lemon/lgf_reader.h>
25 #include <lemon/connectivity.h>
26 #include <lemon/dim2.h>
28 #include "test_tools.h"
30 using namespace lemon;
31 using namespace lemon::dim2;
34 const std::string lgf[lgfn] = {
116 typedef SmartGraph Graph;
117 GRAPH_TYPEDEFS(Graph);
119 typedef PlanarEmbedding<SmartGraph> PE;
120 typedef PlanarDrawing<SmartGraph> PD;
121 typedef PlanarColoring<SmartGraph> PC;
123 void checkEmbedding(const Graph& graph, PE& pe) {
126 Graph::ArcMap<int> face(graph, -1);
128 for (ArcIt a(graph); a != INVALID; ++a) {
131 while (face[b] == -1) {
133 b = pe.next(graph.oppositeArc(b));
135 check(face[b] == face_num, "Wrong face");
139 check(face_num + countNodes(graph) - countConnectedComponents(graph) ==
140 countEdges(graph) + 1, "Euler test does not passed");
143 void checkKuratowski(const Graph& graph, PE& pe) {
144 std::map<int, int> degs;
145 for (NodeIt n(graph); n != INVALID; ++n) {
147 for (IncEdgeIt e(graph, n); e != INVALID; ++e) {
148 if (pe.kuratowski(e)) {
154 for (std::map<int, int>::iterator it = degs.begin(); it != degs.end(); ++it) {
155 check(it->first == 0 || it->first == 2 ||
156 (it->first == 3 && it->second == 6) ||
157 (it->first == 4 && it->second == 5),
158 "Wrong degree in Kuratowski graph");
162 check((degs[3] == 0) != (degs[4] == 0), "Wrong Kuratowski graph");
165 bool intersect(Point<int> e1, Point<int> e2, Point<int> f1, Point<int> f2) {
167 if (std::min(e1.x, e2.x) > std::max(f1.x, f2.x)) return false;
168 if (std::max(e1.x, e2.x) < std::min(f1.x, f2.x)) return false;
169 if (std::min(e1.y, e2.y) > std::max(f1.y, f2.y)) return false;
170 if (std::max(e1.y, e2.y) < std::min(f1.y, f2.y)) return false;
172 l = (e2.x - e1.x) * (f1.y - e1.y) - (e2.y - e1.y) * (f1.x - e1.x);
173 r = (e2.x - e1.x) * (f2.y - e1.y) - (e2.y - e1.y) * (f2.x - e1.x);
174 if (!((l >= 0 && r <= 0) || (l <= 0 && r >= 0))) return false;
175 l = (f2.x - f1.x) * (e1.y - f1.y) - (f2.y - f1.y) * (e1.x - f1.x);
176 r = (f2.x - f1.x) * (e2.y - f1.y) - (f2.y - f1.y) * (e2.x - f1.x);
177 if (!((l >= 0 && r <= 0) || (l <= 0 && r >= 0))) return false;
181 bool collinear(Point<int> p, Point<int> q, Point<int> r) {
183 v = (q.x - p.x) * (r.y - p.y) - (q.y - p.y) * (r.x - p.x);
184 if (v != 0) return false;
185 v = (q.x - p.x) * (r.x - p.x) + (q.y - p.y) * (r.y - p.y);
186 if (v < 0) return false;
190 void checkDrawing(const Graph& graph, PD& pd) {
191 for (Graph::NodeIt n(graph); n != INVALID; ++n) {
193 for (++m; m != INVALID; ++m) {
194 check(pd[m] != pd[n], "Two nodes with identical coordinates");
198 for (Graph::EdgeIt e(graph); e != INVALID; ++e) {
199 for (Graph::EdgeIt f(e); f != e; ++f) {
200 Point<int> e1 = pd[graph.u(e)];
201 Point<int> e2 = pd[graph.v(e)];
202 Point<int> f1 = pd[graph.u(f)];
203 Point<int> f2 = pd[graph.v(f)];
205 if (graph.u(e) == graph.u(f)) {
206 check(!collinear(e1, e2, f2), "Wrong drawing");
207 } else if (graph.u(e) == graph.v(f)) {
208 check(!collinear(e1, e2, f1), "Wrong drawing");
209 } else if (graph.v(e) == graph.u(f)) {
210 check(!collinear(e2, e1, f2), "Wrong drawing");
211 } else if (graph.v(e) == graph.v(f)) {
212 check(!collinear(e2, e1, f1), "Wrong drawing");
214 check(!intersect(e1, e2, f1, f2), "Wrong drawing");
220 void checkColoring(const Graph& graph, PC& pc, int num) {
221 for (NodeIt n(graph); n != INVALID; ++n) {
222 check(pc.colorIndex(n) >= 0 && pc.colorIndex(n) < num,
225 for (EdgeIt e(graph); e != INVALID; ++e) {
226 check(pc.colorIndex(graph.u(e)) != pc.colorIndex(graph.v(e)),
233 for (int i = 0; i < lgfn; ++i) {
234 std::istringstream lgfs(lgf[i]);
237 graphReader(graph, lgfs).run();
239 check(simpleGraph(graph), "Test graphs must be simple");
242 bool planar = pe.run();
243 check(checkPlanarity(graph) == planar, "Planarity checking failed");
246 checkEmbedding(graph, pe);
248 PlanarDrawing<Graph> pd(graph);
249 pd.run(pe.embeddingMap());
250 checkDrawing(graph, pd);
252 PlanarColoring<Graph> pc(graph);
253 pc.runFiveColoring(pe.embeddingMap());
254 checkColoring(graph, pc, 5);
257 checkKuratowski(graph, pe);