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] = {
146 typedef SmartGraph Graph;
147 GRAPH_TYPEDEFS(Graph);
149 typedef PlanarEmbedding<SmartGraph> PE;
150 typedef PlanarDrawing<SmartGraph> PD;
151 typedef PlanarColoring<SmartGraph> PC;
153 void checkEmbedding(const Graph& graph, PE& pe) {
156 Graph::ArcMap<int> face(graph, -1);
158 for (ArcIt a(graph); a != INVALID; ++a) {
161 while (face[b] == -1) {
163 b = pe.next(graph.oppositeArc(b));
165 check(face[b] == face_num, "Wrong face");
171 check(face_num + countNodes(graph) - countConnectedComponents(graph) ==
172 countEdges(graph) + 1, "Euler test does not passed");
176 void checkKuratowski(const Graph& graph, PE& pe) {
177 std::map<int, int> degs;
178 for (NodeIt n(graph); n != INVALID; ++n) {
180 for (IncEdgeIt e(graph, n); e != INVALID; ++e) {
181 if (pe.kuratowski(e)) {
187 for (std::map<int, int>::iterator it = degs.begin(); it != degs.end(); ++it) {
188 check(it->first == 0 || it->first == 2 ||
189 (it->first == 3 && it->second == 6) ||
190 (it->first == 4 && it->second == 5),
191 "Wrong degree in Kuratowski graph");
195 check((degs[3] == 0) != (degs[4] == 0), "Wrong Kuratowski graph");
198 bool intersect(Point<int> e1, Point<int> e2, Point<int> f1, Point<int> f2) {
200 if (std::min(e1.x, e2.x) > std::max(f1.x, f2.x)) return false;
201 if (std::max(e1.x, e2.x) < std::min(f1.x, f2.x)) return false;
202 if (std::min(e1.y, e2.y) > std::max(f1.y, f2.y)) return false;
203 if (std::max(e1.y, e2.y) < std::min(f1.y, f2.y)) return false;
205 l = (e2.x - e1.x) * (f1.y - e1.y) - (e2.y - e1.y) * (f1.x - e1.x);
206 r = (e2.x - e1.x) * (f2.y - e1.y) - (e2.y - e1.y) * (f2.x - e1.x);
207 if (!((l >= 0 && r <= 0) || (l <= 0 && r >= 0))) return false;
208 l = (f2.x - f1.x) * (e1.y - f1.y) - (f2.y - f1.y) * (e1.x - f1.x);
209 r = (f2.x - f1.x) * (e2.y - f1.y) - (f2.y - f1.y) * (e2.x - f1.x);
210 if (!((l >= 0 && r <= 0) || (l <= 0 && r >= 0))) return false;
214 bool collinear(Point<int> p, Point<int> q, Point<int> r) {
216 v = (q.x - p.x) * (r.y - p.y) - (q.y - p.y) * (r.x - p.x);
217 if (v != 0) return false;
218 v = (q.x - p.x) * (r.x - p.x) + (q.y - p.y) * (r.y - p.y);
219 if (v < 0) return false;
223 void checkDrawing(const Graph& graph, PD& pd) {
224 for (Graph::NodeIt n(graph); n != INVALID; ++n) {
226 for (++m; m != INVALID; ++m) {
227 check(pd[m] != pd[n], "Two nodes with identical coordinates");
231 for (Graph::EdgeIt e(graph); e != INVALID; ++e) {
232 for (Graph::EdgeIt f(e); f != e; ++f) {
233 Point<int> e1 = pd[graph.u(e)];
234 Point<int> e2 = pd[graph.v(e)];
235 Point<int> f1 = pd[graph.u(f)];
236 Point<int> f2 = pd[graph.v(f)];
238 if (graph.u(e) == graph.u(f)) {
239 check(!collinear(e1, e2, f2), "Wrong drawing");
240 } else if (graph.u(e) == graph.v(f)) {
241 check(!collinear(e1, e2, f1), "Wrong drawing");
242 } else if (graph.v(e) == graph.u(f)) {
243 check(!collinear(e2, e1, f2), "Wrong drawing");
244 } else if (graph.v(e) == graph.v(f)) {
245 check(!collinear(e2, e1, f1), "Wrong drawing");
247 check(!intersect(e1, e2, f1, f2), "Wrong drawing");
253 void checkColoring(const Graph& graph, PC& pc, int num) {
254 for (NodeIt n(graph); n != INVALID; ++n) {
255 check(pc.colorIndex(n) >= 0 && pc.colorIndex(n) < num,
258 for (EdgeIt e(graph); e != INVALID; ++e) {
259 check(pc.colorIndex(graph.u(e)) != pc.colorIndex(graph.v(e)),
266 for (int i = 0; i < lgfn; ++i) {
267 std::istringstream lgfs(lgf[i]);
270 graphReader(graph, lgfs).run();
272 check(simpleGraph(graph), "Test graphs must be simple");
275 bool planar = pe.run();
276 check(checkPlanarity(graph) == planar, "Planarity checking failed");
279 checkEmbedding(graph, pe);
282 PlanarDrawing<Graph> pd(graph);
283 pd.run(pe.embeddingMap());
284 checkDrawing(graph, pd);
288 PlanarDrawing<Graph> pd(graph);
290 checkDrawing(graph, pd);
294 PlanarColoring<Graph> pc(graph);
295 pc.runFiveColoring(pe.embeddingMap());
296 checkColoring(graph, pc, 5);
300 PlanarColoring<Graph> pc(graph);
301 pc.runFiveColoring();
302 checkColoring(graph, pc, 5);
306 checkKuratowski(graph, pe);