deba@797
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
deba@797
|
2 |
*
|
deba@797
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
deba@797
|
4 |
*
|
deba@797
|
5 |
* Copyright (C) 2003-2009
|
deba@797
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@797
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@797
|
8 |
*
|
deba@797
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@797
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@797
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@797
|
12 |
*
|
deba@797
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@797
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@797
|
15 |
* purpose.
|
deba@797
|
16 |
*
|
deba@797
|
17 |
*/
|
deba@797
|
18 |
|
deba@797
|
19 |
#include <iostream>
|
deba@797
|
20 |
|
deba@797
|
21 |
#include <lemon/planarity.h>
|
deba@797
|
22 |
|
deba@797
|
23 |
#include <lemon/smart_graph.h>
|
deba@797
|
24 |
#include <lemon/lgf_reader.h>
|
deba@797
|
25 |
#include <lemon/connectivity.h>
|
deba@797
|
26 |
#include <lemon/dim2.h>
|
deba@797
|
27 |
|
deba@797
|
28 |
#include "test_tools.h"
|
deba@797
|
29 |
|
deba@797
|
30 |
using namespace lemon;
|
deba@797
|
31 |
using namespace lemon::dim2;
|
deba@797
|
32 |
|
deba@1182
|
33 |
const int lgfn = 8;
|
deba@797
|
34 |
const std::string lgf[lgfn] = {
|
deba@797
|
35 |
"@nodes\n"
|
deba@797
|
36 |
"label\n"
|
deba@1182
|
37 |
"@edges\n"
|
deba@1182
|
38 |
" label\n",
|
deba@1182
|
39 |
|
deba@1182
|
40 |
"@nodes\n"
|
deba@1182
|
41 |
"label\n"
|
deba@1182
|
42 |
"0\n"
|
deba@1182
|
43 |
"@edges\n"
|
deba@1182
|
44 |
" label\n",
|
deba@1182
|
45 |
|
deba@1182
|
46 |
"@nodes\n"
|
deba@1182
|
47 |
"label\n"
|
deba@1182
|
48 |
"0\n"
|
deba@1182
|
49 |
"1\n"
|
deba@1182
|
50 |
"@edges\n"
|
deba@1182
|
51 |
" label\n"
|
deba@1182
|
52 |
"0 1 0\n",
|
deba@1182
|
53 |
|
deba@1182
|
54 |
"@nodes\n"
|
deba@1182
|
55 |
"label\n"
|
deba@1182
|
56 |
"0\n"
|
deba@1182
|
57 |
"1\n"
|
deba@1182
|
58 |
"2\n"
|
deba@1182
|
59 |
"@edges\n"
|
deba@1182
|
60 |
" label\n"
|
deba@1182
|
61 |
"0 1 0\n"
|
deba@1182
|
62 |
"1 2 1\n"
|
deba@1182
|
63 |
"2 0 2\n",
|
deba@1182
|
64 |
|
deba@1182
|
65 |
"@nodes\n"
|
deba@1182
|
66 |
"label\n"
|
deba@797
|
67 |
"0\n"
|
deba@797
|
68 |
"1\n"
|
deba@797
|
69 |
"2\n"
|
deba@797
|
70 |
"3\n"
|
deba@797
|
71 |
"4\n"
|
deba@797
|
72 |
"@edges\n"
|
deba@797
|
73 |
" label\n"
|
deba@797
|
74 |
"0 1 0\n"
|
deba@797
|
75 |
"0 2 0\n"
|
deba@797
|
76 |
"0 3 0\n"
|
deba@797
|
77 |
"0 4 0\n"
|
deba@797
|
78 |
"1 2 0\n"
|
deba@797
|
79 |
"1 3 0\n"
|
deba@797
|
80 |
"1 4 0\n"
|
deba@797
|
81 |
"2 3 0\n"
|
deba@797
|
82 |
"2 4 0\n"
|
deba@797
|
83 |
"3 4 0\n",
|
deba@797
|
84 |
|
deba@797
|
85 |
"@nodes\n"
|
deba@797
|
86 |
"label\n"
|
deba@797
|
87 |
"0\n"
|
deba@797
|
88 |
"1\n"
|
deba@797
|
89 |
"2\n"
|
deba@797
|
90 |
"3\n"
|
deba@797
|
91 |
"4\n"
|
deba@797
|
92 |
"@edges\n"
|
deba@797
|
93 |
" label\n"
|
deba@797
|
94 |
"0 1 0\n"
|
deba@797
|
95 |
"0 2 0\n"
|
deba@797
|
96 |
"0 3 0\n"
|
deba@797
|
97 |
"0 4 0\n"
|
deba@797
|
98 |
"1 2 0\n"
|
deba@797
|
99 |
"1 3 0\n"
|
deba@797
|
100 |
"2 3 0\n"
|
deba@797
|
101 |
"2 4 0\n"
|
deba@797
|
102 |
"3 4 0\n",
|
deba@797
|
103 |
|
deba@797
|
104 |
"@nodes\n"
|
deba@797
|
105 |
"label\n"
|
deba@797
|
106 |
"0\n"
|
deba@797
|
107 |
"1\n"
|
deba@797
|
108 |
"2\n"
|
deba@797
|
109 |
"3\n"
|
deba@797
|
110 |
"4\n"
|
deba@797
|
111 |
"5\n"
|
deba@797
|
112 |
"@edges\n"
|
deba@797
|
113 |
" label\n"
|
deba@797
|
114 |
"0 3 0\n"
|
deba@797
|
115 |
"0 4 0\n"
|
deba@797
|
116 |
"0 5 0\n"
|
deba@797
|
117 |
"1 3 0\n"
|
deba@797
|
118 |
"1 4 0\n"
|
deba@797
|
119 |
"1 5 0\n"
|
deba@797
|
120 |
"2 3 0\n"
|
deba@797
|
121 |
"2 4 0\n"
|
deba@797
|
122 |
"2 5 0\n",
|
deba@797
|
123 |
|
deba@797
|
124 |
"@nodes\n"
|
deba@797
|
125 |
"label\n"
|
deba@797
|
126 |
"0\n"
|
deba@797
|
127 |
"1\n"
|
deba@797
|
128 |
"2\n"
|
deba@797
|
129 |
"3\n"
|
deba@797
|
130 |
"4\n"
|
deba@797
|
131 |
"5\n"
|
deba@797
|
132 |
"@edges\n"
|
deba@797
|
133 |
" label\n"
|
deba@797
|
134 |
"0 3 0\n"
|
deba@797
|
135 |
"0 4 0\n"
|
deba@797
|
136 |
"0 5 0\n"
|
deba@797
|
137 |
"1 3 0\n"
|
deba@797
|
138 |
"1 4 0\n"
|
deba@797
|
139 |
"1 5 0\n"
|
deba@797
|
140 |
"2 3 0\n"
|
deba@797
|
141 |
"2 5 0\n"
|
deba@797
|
142 |
};
|
deba@797
|
143 |
|
deba@797
|
144 |
|
deba@797
|
145 |
|
deba@797
|
146 |
typedef SmartGraph Graph;
|
deba@797
|
147 |
GRAPH_TYPEDEFS(Graph);
|
deba@797
|
148 |
|
deba@797
|
149 |
typedef PlanarEmbedding<SmartGraph> PE;
|
deba@797
|
150 |
typedef PlanarDrawing<SmartGraph> PD;
|
deba@797
|
151 |
typedef PlanarColoring<SmartGraph> PC;
|
deba@797
|
152 |
|
deba@797
|
153 |
void checkEmbedding(const Graph& graph, PE& pe) {
|
deba@797
|
154 |
int face_num = 0;
|
deba@797
|
155 |
|
deba@797
|
156 |
Graph::ArcMap<int> face(graph, -1);
|
deba@797
|
157 |
|
deba@797
|
158 |
for (ArcIt a(graph); a != INVALID; ++a) {
|
deba@797
|
159 |
if (face[a] == -1) {
|
deba@797
|
160 |
Arc b = a;
|
deba@797
|
161 |
while (face[b] == -1) {
|
deba@797
|
162 |
face[b] = face_num;
|
deba@797
|
163 |
b = pe.next(graph.oppositeArc(b));
|
deba@797
|
164 |
}
|
deba@797
|
165 |
check(face[b] == face_num, "Wrong face");
|
deba@797
|
166 |
++face_num;
|
deba@797
|
167 |
}
|
deba@797
|
168 |
}
|
deba@1182
|
169 |
|
deba@1182
|
170 |
if (face_num != 0) {
|
deba@1182
|
171 |
check(face_num + countNodes(graph) - countConnectedComponents(graph) ==
|
deba@1182
|
172 |
countEdges(graph) + 1, "Euler test does not passed");
|
deba@1182
|
173 |
}
|
deba@797
|
174 |
}
|
deba@797
|
175 |
|
deba@797
|
176 |
void checkKuratowski(const Graph& graph, PE& pe) {
|
deba@797
|
177 |
std::map<int, int> degs;
|
deba@797
|
178 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
deba@797
|
179 |
int deg = 0;
|
deba@797
|
180 |
for (IncEdgeIt e(graph, n); e != INVALID; ++e) {
|
deba@797
|
181 |
if (pe.kuratowski(e)) {
|
deba@797
|
182 |
++deg;
|
deba@797
|
183 |
}
|
deba@797
|
184 |
}
|
deba@797
|
185 |
++degs[deg];
|
deba@797
|
186 |
}
|
deba@797
|
187 |
for (std::map<int, int>::iterator it = degs.begin(); it != degs.end(); ++it) {
|
deba@797
|
188 |
check(it->first == 0 || it->first == 2 ||
|
deba@797
|
189 |
(it->first == 3 && it->second == 6) ||
|
deba@797
|
190 |
(it->first == 4 && it->second == 5),
|
deba@797
|
191 |
"Wrong degree in Kuratowski graph");
|
deba@797
|
192 |
}
|
deba@797
|
193 |
|
deba@797
|
194 |
// Not full test
|
deba@797
|
195 |
check((degs[3] == 0) != (degs[4] == 0), "Wrong Kuratowski graph");
|
deba@797
|
196 |
}
|
deba@797
|
197 |
|
deba@797
|
198 |
bool intersect(Point<int> e1, Point<int> e2, Point<int> f1, Point<int> f2) {
|
deba@797
|
199 |
int l, r;
|
deba@797
|
200 |
if (std::min(e1.x, e2.x) > std::max(f1.x, f2.x)) return false;
|
deba@797
|
201 |
if (std::max(e1.x, e2.x) < std::min(f1.x, f2.x)) return false;
|
deba@797
|
202 |
if (std::min(e1.y, e2.y) > std::max(f1.y, f2.y)) return false;
|
deba@797
|
203 |
if (std::max(e1.y, e2.y) < std::min(f1.y, f2.y)) return false;
|
deba@797
|
204 |
|
deba@797
|
205 |
l = (e2.x - e1.x) * (f1.y - e1.y) - (e2.y - e1.y) * (f1.x - e1.x);
|
deba@797
|
206 |
r = (e2.x - e1.x) * (f2.y - e1.y) - (e2.y - e1.y) * (f2.x - e1.x);
|
deba@797
|
207 |
if (!((l >= 0 && r <= 0) || (l <= 0 && r >= 0))) return false;
|
deba@797
|
208 |
l = (f2.x - f1.x) * (e1.y - f1.y) - (f2.y - f1.y) * (e1.x - f1.x);
|
deba@797
|
209 |
r = (f2.x - f1.x) * (e2.y - f1.y) - (f2.y - f1.y) * (e2.x - f1.x);
|
deba@797
|
210 |
if (!((l >= 0 && r <= 0) || (l <= 0 && r >= 0))) return false;
|
deba@797
|
211 |
return true;
|
deba@797
|
212 |
}
|
deba@797
|
213 |
|
deba@797
|
214 |
bool collinear(Point<int> p, Point<int> q, Point<int> r) {
|
deba@797
|
215 |
int v;
|
deba@797
|
216 |
v = (q.x - p.x) * (r.y - p.y) - (q.y - p.y) * (r.x - p.x);
|
deba@797
|
217 |
if (v != 0) return false;
|
deba@797
|
218 |
v = (q.x - p.x) * (r.x - p.x) + (q.y - p.y) * (r.y - p.y);
|
deba@797
|
219 |
if (v < 0) return false;
|
deba@797
|
220 |
return true;
|
deba@797
|
221 |
}
|
deba@797
|
222 |
|
deba@797
|
223 |
void checkDrawing(const Graph& graph, PD& pd) {
|
deba@797
|
224 |
for (Graph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@797
|
225 |
Graph::NodeIt m(n);
|
deba@797
|
226 |
for (++m; m != INVALID; ++m) {
|
deba@797
|
227 |
check(pd[m] != pd[n], "Two nodes with identical coordinates");
|
deba@797
|
228 |
}
|
deba@797
|
229 |
}
|
deba@797
|
230 |
|
deba@797
|
231 |
for (Graph::EdgeIt e(graph); e != INVALID; ++e) {
|
deba@797
|
232 |
for (Graph::EdgeIt f(e); f != e; ++f) {
|
deba@797
|
233 |
Point<int> e1 = pd[graph.u(e)];
|
deba@797
|
234 |
Point<int> e2 = pd[graph.v(e)];
|
deba@797
|
235 |
Point<int> f1 = pd[graph.u(f)];
|
deba@797
|
236 |
Point<int> f2 = pd[graph.v(f)];
|
deba@797
|
237 |
|
deba@797
|
238 |
if (graph.u(e) == graph.u(f)) {
|
deba@797
|
239 |
check(!collinear(e1, e2, f2), "Wrong drawing");
|
deba@797
|
240 |
} else if (graph.u(e) == graph.v(f)) {
|
deba@797
|
241 |
check(!collinear(e1, e2, f1), "Wrong drawing");
|
deba@797
|
242 |
} else if (graph.v(e) == graph.u(f)) {
|
deba@797
|
243 |
check(!collinear(e2, e1, f2), "Wrong drawing");
|
deba@797
|
244 |
} else if (graph.v(e) == graph.v(f)) {
|
deba@797
|
245 |
check(!collinear(e2, e1, f1), "Wrong drawing");
|
deba@797
|
246 |
} else {
|
deba@797
|
247 |
check(!intersect(e1, e2, f1, f2), "Wrong drawing");
|
deba@797
|
248 |
}
|
deba@797
|
249 |
}
|
deba@797
|
250 |
}
|
deba@797
|
251 |
}
|
deba@797
|
252 |
|
deba@797
|
253 |
void checkColoring(const Graph& graph, PC& pc, int num) {
|
deba@797
|
254 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
deba@797
|
255 |
check(pc.colorIndex(n) >= 0 && pc.colorIndex(n) < num,
|
deba@797
|
256 |
"Wrong coloring");
|
deba@797
|
257 |
}
|
deba@797
|
258 |
for (EdgeIt e(graph); e != INVALID; ++e) {
|
deba@797
|
259 |
check(pc.colorIndex(graph.u(e)) != pc.colorIndex(graph.v(e)),
|
deba@797
|
260 |
"Wrong coloring");
|
deba@797
|
261 |
}
|
deba@797
|
262 |
}
|
deba@797
|
263 |
|
deba@797
|
264 |
int main() {
|
deba@797
|
265 |
|
deba@797
|
266 |
for (int i = 0; i < lgfn; ++i) {
|
deba@797
|
267 |
std::istringstream lgfs(lgf[i]);
|
deba@797
|
268 |
|
deba@797
|
269 |
SmartGraph graph;
|
deba@797
|
270 |
graphReader(graph, lgfs).run();
|
deba@797
|
271 |
|
deba@797
|
272 |
check(simpleGraph(graph), "Test graphs must be simple");
|
deba@797
|
273 |
|
deba@797
|
274 |
PE pe(graph);
|
deba@798
|
275 |
bool planar = pe.run();
|
deba@798
|
276 |
check(checkPlanarity(graph) == planar, "Planarity checking failed");
|
deba@798
|
277 |
|
deba@798
|
278 |
if (planar) {
|
deba@797
|
279 |
checkEmbedding(graph, pe);
|
deba@797
|
280 |
|
deba@1181
|
281 |
{
|
deba@1181
|
282 |
PlanarDrawing<Graph> pd(graph);
|
deba@1181
|
283 |
pd.run(pe.embeddingMap());
|
deba@1181
|
284 |
checkDrawing(graph, pd);
|
deba@1181
|
285 |
}
|
deba@797
|
286 |
|
deba@1181
|
287 |
{
|
deba@1181
|
288 |
PlanarDrawing<Graph> pd(graph);
|
deba@1181
|
289 |
pd.run();
|
deba@1181
|
290 |
checkDrawing(graph, pd);
|
deba@1181
|
291 |
}
|
deba@1181
|
292 |
|
deba@1181
|
293 |
{
|
deba@1181
|
294 |
PlanarColoring<Graph> pc(graph);
|
deba@1181
|
295 |
pc.runFiveColoring(pe.embeddingMap());
|
deba@1181
|
296 |
checkColoring(graph, pc, 5);
|
deba@1181
|
297 |
}
|
deba@1181
|
298 |
|
deba@1181
|
299 |
{
|
deba@1181
|
300 |
PlanarColoring<Graph> pc(graph);
|
deba@1181
|
301 |
pc.runFiveColoring();
|
deba@1181
|
302 |
checkColoring(graph, pc, 5);
|
deba@1181
|
303 |
}
|
deba@797
|
304 |
|
deba@797
|
305 |
} else {
|
deba@797
|
306 |
checkKuratowski(graph, pe);
|
deba@797
|
307 |
}
|
deba@797
|
308 |
}
|
deba@797
|
309 |
|
deba@797
|
310 |
return 0;
|
deba@797
|
311 |
}
|