0
2
0
382
382
... | ... |
@@ -44,768 +44,768 @@ |
44 | 44 |
template <typename Graph> |
45 | 45 |
struct PlanarityVisitor : DfsVisitor<Graph> { |
46 | 46 |
|
47 | 47 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
48 | 48 |
|
49 | 49 |
typedef typename Graph::template NodeMap<Arc> PredMap; |
50 | 50 |
|
51 | 51 |
typedef typename Graph::template EdgeMap<bool> TreeMap; |
52 | 52 |
|
53 | 53 |
typedef typename Graph::template NodeMap<int> OrderMap; |
54 | 54 |
typedef std::vector<Node> OrderList; |
55 | 55 |
|
56 | 56 |
typedef typename Graph::template NodeMap<int> LowMap; |
57 | 57 |
typedef typename Graph::template NodeMap<int> AncestorMap; |
58 | 58 |
|
59 | 59 |
PlanarityVisitor(const Graph& graph, |
60 | 60 |
PredMap& pred_map, TreeMap& tree_map, |
61 | 61 |
OrderMap& order_map, OrderList& order_list, |
62 | 62 |
AncestorMap& ancestor_map, LowMap& low_map) |
63 | 63 |
: _graph(graph), _pred_map(pred_map), _tree_map(tree_map), |
64 | 64 |
_order_map(order_map), _order_list(order_list), |
65 | 65 |
_ancestor_map(ancestor_map), _low_map(low_map) {} |
66 | 66 |
|
67 | 67 |
void reach(const Node& node) { |
68 | 68 |
_order_map[node] = _order_list.size(); |
69 | 69 |
_low_map[node] = _order_list.size(); |
70 | 70 |
_ancestor_map[node] = _order_list.size(); |
71 | 71 |
_order_list.push_back(node); |
72 | 72 |
} |
73 | 73 |
|
74 | 74 |
void discover(const Arc& arc) { |
75 | 75 |
Node source = _graph.source(arc); |
76 | 76 |
Node target = _graph.target(arc); |
77 | 77 |
|
78 | 78 |
_tree_map[arc] = true; |
79 | 79 |
_pred_map[target] = arc; |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
void examine(const Arc& arc) { |
83 | 83 |
Node source = _graph.source(arc); |
84 | 84 |
Node target = _graph.target(arc); |
85 | 85 |
|
86 | 86 |
if (_order_map[target] < _order_map[source] && !_tree_map[arc]) { |
87 | 87 |
if (_low_map[source] > _order_map[target]) { |
88 | 88 |
_low_map[source] = _order_map[target]; |
89 | 89 |
} |
90 | 90 |
if (_ancestor_map[source] > _order_map[target]) { |
91 | 91 |
_ancestor_map[source] = _order_map[target]; |
92 | 92 |
} |
93 | 93 |
} |
94 | 94 |
} |
95 | 95 |
|
96 | 96 |
void backtrack(const Arc& arc) { |
97 | 97 |
Node source = _graph.source(arc); |
98 | 98 |
Node target = _graph.target(arc); |
99 | 99 |
|
100 | 100 |
if (_low_map[source] > _low_map[target]) { |
101 | 101 |
_low_map[source] = _low_map[target]; |
102 | 102 |
} |
103 | 103 |
} |
104 | 104 |
|
105 | 105 |
const Graph& _graph; |
106 | 106 |
PredMap& _pred_map; |
107 | 107 |
TreeMap& _tree_map; |
108 | 108 |
OrderMap& _order_map; |
109 | 109 |
OrderList& _order_list; |
110 | 110 |
AncestorMap& _ancestor_map; |
111 | 111 |
LowMap& _low_map; |
112 | 112 |
}; |
113 | 113 |
|
114 | 114 |
template <typename Graph, bool embedding = true> |
115 | 115 |
struct NodeDataNode { |
116 | 116 |
int prev, next; |
117 | 117 |
int visited; |
118 | 118 |
typename Graph::Arc first; |
119 | 119 |
bool inverted; |
120 | 120 |
}; |
121 | 121 |
|
122 | 122 |
template <typename Graph> |
123 | 123 |
struct NodeDataNode<Graph, false> { |
124 | 124 |
int prev, next; |
125 | 125 |
int visited; |
126 | 126 |
}; |
127 | 127 |
|
128 | 128 |
template <typename Graph> |
129 | 129 |
struct ChildListNode { |
130 | 130 |
typedef typename Graph::Node Node; |
131 | 131 |
Node first; |
132 | 132 |
Node prev, next; |
133 | 133 |
}; |
134 | 134 |
|
135 | 135 |
template <typename Graph> |
136 | 136 |
struct ArcListNode { |
137 | 137 |
typename Graph::Arc prev, next; |
138 | 138 |
}; |
139 | 139 |
|
140 |
template <typename Graph> |
|
141 |
class PlanarityChecking { |
|
142 |
private: |
|
143 |
|
|
144 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
|
145 |
|
|
146 |
const Graph& _graph; |
|
147 |
|
|
148 |
private: |
|
149 |
|
|
150 |
typedef typename Graph::template NodeMap<Arc> PredMap; |
|
151 |
|
|
152 |
typedef typename Graph::template EdgeMap<bool> TreeMap; |
|
153 |
|
|
154 |
typedef typename Graph::template NodeMap<int> OrderMap; |
|
155 |
typedef std::vector<Node> OrderList; |
|
156 |
|
|
157 |
typedef typename Graph::template NodeMap<int> LowMap; |
|
158 |
typedef typename Graph::template NodeMap<int> AncestorMap; |
|
159 |
|
|
160 |
typedef _planarity_bits::NodeDataNode<Graph> NodeDataNode; |
|
161 |
typedef std::vector<NodeDataNode> NodeData; |
|
162 |
|
|
163 |
typedef _planarity_bits::ChildListNode<Graph> ChildListNode; |
|
164 |
typedef typename Graph::template NodeMap<ChildListNode> ChildLists; |
|
165 |
|
|
166 |
typedef typename Graph::template NodeMap<std::list<int> > MergeRoots; |
|
167 |
|
|
168 |
typedef typename Graph::template NodeMap<bool> EmbedArc; |
|
169 |
|
|
170 |
public: |
|
171 |
|
|
172 |
PlanarityChecking(const Graph& graph) : _graph(graph) {} |
|
173 |
|
|
174 |
bool run() { |
|
175 |
typedef _planarity_bits::PlanarityVisitor<Graph> Visitor; |
|
176 |
|
|
177 |
PredMap pred_map(_graph, INVALID); |
|
178 |
TreeMap tree_map(_graph, false); |
|
179 |
|
|
180 |
OrderMap order_map(_graph, -1); |
|
181 |
OrderList order_list; |
|
182 |
|
|
183 |
AncestorMap ancestor_map(_graph, -1); |
|
184 |
LowMap low_map(_graph, -1); |
|
185 |
|
|
186 |
Visitor visitor(_graph, pred_map, tree_map, |
|
187 |
order_map, order_list, ancestor_map, low_map); |
|
188 |
DfsVisit<Graph, Visitor> visit(_graph, visitor); |
|
189 |
visit.run(); |
|
190 |
|
|
191 |
ChildLists child_lists(_graph); |
|
192 |
createChildLists(tree_map, order_map, low_map, child_lists); |
|
193 |
|
|
194 |
NodeData node_data(2 * order_list.size()); |
|
195 |
|
|
196 |
EmbedArc embed_arc(_graph, false); |
|
197 |
|
|
198 |
MergeRoots merge_roots(_graph); |
|
199 |
|
|
200 |
for (int i = order_list.size() - 1; i >= 0; --i) { |
|
201 |
|
|
202 |
Node node = order_list[i]; |
|
203 |
|
|
204 |
Node source = node; |
|
205 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) { |
|
206 |
Node target = _graph.target(e); |
|
207 |
|
|
208 |
if (order_map[source] < order_map[target] && tree_map[e]) { |
|
209 |
initFace(target, node_data, order_map, order_list); |
|
210 |
} |
|
211 |
} |
|
212 |
|
|
213 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) { |
|
214 |
Node target = _graph.target(e); |
|
215 |
|
|
216 |
if (order_map[source] < order_map[target] && !tree_map[e]) { |
|
217 |
embed_arc[target] = true; |
|
218 |
walkUp(target, source, i, pred_map, low_map, |
|
219 |
order_map, order_list, node_data, merge_roots); |
|
220 |
} |
|
221 |
} |
|
222 |
|
|
223 |
for (typename MergeRoots::Value::iterator it = |
|
224 |
merge_roots[node].begin(); |
|
225 |
it != merge_roots[node].end(); ++it) { |
|
226 |
int rn = *it; |
|
227 |
walkDown(rn, i, node_data, order_list, child_lists, |
|
228 |
ancestor_map, low_map, embed_arc, merge_roots); |
|
229 |
} |
|
230 |
merge_roots[node].clear(); |
|
231 |
|
|
232 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) { |
|
233 |
Node target = _graph.target(e); |
|
234 |
|
|
235 |
if (order_map[source] < order_map[target] && !tree_map[e]) { |
|
236 |
if (embed_arc[target]) { |
|
237 |
return false; |
|
238 |
} |
|
239 |
} |
|
240 |
} |
|
241 |
} |
|
242 |
|
|
243 |
return true; |
|
244 |
} |
|
245 |
|
|
246 |
private: |
|
247 |
|
|
248 |
void createChildLists(const TreeMap& tree_map, const OrderMap& order_map, |
|
249 |
const LowMap& low_map, ChildLists& child_lists) { |
|
250 |
|
|
251 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
|
252 |
Node source = n; |
|
253 |
|
|
254 |
std::vector<Node> targets; |
|
255 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
|
256 |
Node target = _graph.target(e); |
|
257 |
|
|
258 |
if (order_map[source] < order_map[target] && tree_map[e]) { |
|
259 |
targets.push_back(target); |
|
260 |
} |
|
261 |
} |
|
262 |
|
|
263 |
if (targets.size() == 0) { |
|
264 |
child_lists[source].first = INVALID; |
|
265 |
} else if (targets.size() == 1) { |
|
266 |
child_lists[source].first = targets[0]; |
|
267 |
child_lists[targets[0]].prev = INVALID; |
|
268 |
child_lists[targets[0]].next = INVALID; |
|
269 |
} else { |
|
270 |
radixSort(targets.begin(), targets.end(), mapToFunctor(low_map)); |
|
271 |
for (int i = 1; i < int(targets.size()); ++i) { |
|
272 |
child_lists[targets[i]].prev = targets[i - 1]; |
|
273 |
child_lists[targets[i - 1]].next = targets[i]; |
|
274 |
} |
|
275 |
child_lists[targets.back()].next = INVALID; |
|
276 |
child_lists[targets.front()].prev = INVALID; |
|
277 |
child_lists[source].first = targets.front(); |
|
278 |
} |
|
279 |
} |
|
280 |
} |
|
281 |
|
|
282 |
void walkUp(const Node& node, Node root, int rorder, |
|
283 |
const PredMap& pred_map, const LowMap& low_map, |
|
284 |
const OrderMap& order_map, const OrderList& order_list, |
|
285 |
NodeData& node_data, MergeRoots& merge_roots) { |
|
286 |
|
|
287 |
int na, nb; |
|
288 |
bool da, db; |
|
289 |
|
|
290 |
na = nb = order_map[node]; |
|
291 |
da = true; db = false; |
|
292 |
|
|
293 |
while (true) { |
|
294 |
|
|
295 |
if (node_data[na].visited == rorder) break; |
|
296 |
if (node_data[nb].visited == rorder) break; |
|
297 |
|
|
298 |
node_data[na].visited = rorder; |
|
299 |
node_data[nb].visited = rorder; |
|
300 |
|
|
301 |
int rn = -1; |
|
302 |
|
|
303 |
if (na >= int(order_list.size())) { |
|
304 |
rn = na; |
|
305 |
} else if (nb >= int(order_list.size())) { |
|
306 |
rn = nb; |
|
307 |
} |
|
308 |
|
|
309 |
if (rn == -1) { |
|
310 |
int nn; |
|
311 |
|
|
312 |
nn = da ? node_data[na].prev : node_data[na].next; |
|
313 |
da = node_data[nn].prev != na; |
|
314 |
na = nn; |
|
315 |
|
|
316 |
nn = db ? node_data[nb].prev : node_data[nb].next; |
|
317 |
db = node_data[nn].prev != nb; |
|
318 |
nb = nn; |
|
319 |
|
|
320 |
} else { |
|
321 |
|
|
322 |
Node rep = order_list[rn - order_list.size()]; |
|
323 |
Node parent = _graph.source(pred_map[rep]); |
|
324 |
|
|
325 |
if (low_map[rep] < rorder) { |
|
326 |
merge_roots[parent].push_back(rn); |
|
327 |
} else { |
|
328 |
merge_roots[parent].push_front(rn); |
|
329 |
} |
|
330 |
|
|
331 |
if (parent != root) { |
|
332 |
na = nb = order_map[parent]; |
|
333 |
da = true; db = false; |
|
334 |
} else { |
|
335 |
break; |
|
336 |
} |
|
337 |
} |
|
338 |
} |
|
339 |
} |
|
340 |
|
|
341 |
void walkDown(int rn, int rorder, NodeData& node_data, |
|
342 |
OrderList& order_list, ChildLists& child_lists, |
|
343 |
AncestorMap& ancestor_map, LowMap& low_map, |
|
344 |
EmbedArc& embed_arc, MergeRoots& merge_roots) { |
|
345 |
|
|
346 |
std::vector<std::pair<int, bool> > merge_stack; |
|
347 |
|
|
348 |
for (int di = 0; di < 2; ++di) { |
|
349 |
bool rd = di == 0; |
|
350 |
int pn = rn; |
|
351 |
int n = rd ? node_data[rn].next : node_data[rn].prev; |
|
352 |
|
|
353 |
while (n != rn) { |
|
354 |
|
|
355 |
Node node = order_list[n]; |
|
356 |
|
|
357 |
if (embed_arc[node]) { |
|
358 |
|
|
359 |
// Merging components on the critical path |
|
360 |
while (!merge_stack.empty()) { |
|
361 |
|
|
362 |
// Component root |
|
363 |
int cn = merge_stack.back().first; |
|
364 |
bool cd = merge_stack.back().second; |
|
365 |
merge_stack.pop_back(); |
|
366 |
|
|
367 |
// Parent of component |
|
368 |
int dn = merge_stack.back().first; |
|
369 |
bool dd = merge_stack.back().second; |
|
370 |
merge_stack.pop_back(); |
|
371 |
|
|
372 |
Node parent = order_list[dn]; |
|
373 |
|
|
374 |
// Erasing from merge_roots |
|
375 |
merge_roots[parent].pop_front(); |
|
376 |
|
|
377 |
Node child = order_list[cn - order_list.size()]; |
|
378 |
|
|
379 |
// Erasing from child_lists |
|
380 |
if (child_lists[child].prev != INVALID) { |
|
381 |
child_lists[child_lists[child].prev].next = |
|
382 |
child_lists[child].next; |
|
383 |
} else { |
|
384 |
child_lists[parent].first = child_lists[child].next; |
|
385 |
} |
|
386 |
|
|
387 |
if (child_lists[child].next != INVALID) { |
|
388 |
child_lists[child_lists[child].next].prev = |
|
389 |
child_lists[child].prev; |
|
390 |
} |
|
391 |
|
|
392 |
// Merging external faces |
|
393 |
{ |
|
394 |
int en = cn; |
|
395 |
cn = cd ? node_data[cn].prev : node_data[cn].next; |
|
396 |
cd = node_data[cn].next == en; |
|
397 |
|
|
398 |
} |
|
399 |
|
|
400 |
if (cd) node_data[cn].next = dn; else node_data[cn].prev = dn; |
|
401 |
if (dd) node_data[dn].prev = cn; else node_data[dn].next = cn; |
|
402 |
|
|
403 |
} |
|
404 |
|
|
405 |
bool d = pn == node_data[n].prev; |
|
406 |
|
|
407 |
if (node_data[n].prev == node_data[n].next && |
|
408 |
node_data[n].inverted) { |
|
409 |
d = !d; |
|
410 |
} |
|
411 |
|
|
412 |
// Embedding arc into external face |
|
413 |
if (rd) node_data[rn].next = n; else node_data[rn].prev = n; |
|
414 |
if (d) node_data[n].prev = rn; else node_data[n].next = rn; |
|
415 |
pn = rn; |
|
416 |
|
|
417 |
embed_arc[order_list[n]] = false; |
|
418 |
} |
|
419 |
|
|
420 |
if (!merge_roots[node].empty()) { |
|
421 |
|
|
422 |
bool d = pn == node_data[n].prev; |
|
423 |
|
|
424 |
merge_stack.push_back(std::make_pair(n, d)); |
|
425 |
|
|
426 |
int rn = merge_roots[node].front(); |
|
427 |
|
|
428 |
int xn = node_data[rn].next; |
|
429 |
Node xnode = order_list[xn]; |
|
430 |
|
|
431 |
int yn = node_data[rn].prev; |
|
432 |
Node ynode = order_list[yn]; |
|
433 |
|
|
434 |
bool rd; |
|
435 |
if (!external(xnode, rorder, child_lists, |
|
436 |
ancestor_map, low_map)) { |
|
437 |
rd = true; |
|
438 |
} else if (!external(ynode, rorder, child_lists, |
|
439 |
ancestor_map, low_map)) { |
|
440 |
rd = false; |
|
441 |
} else if (pertinent(xnode, embed_arc, merge_roots)) { |
|
442 |
rd = true; |
|
443 |
} else { |
|
444 |
rd = false; |
|
445 |
} |
|
446 |
|
|
447 |
merge_stack.push_back(std::make_pair(rn, rd)); |
|
448 |
|
|
449 |
pn = rn; |
|
450 |
n = rd ? xn : yn; |
|
451 |
|
|
452 |
} else if (!external(node, rorder, child_lists, |
|
453 |
ancestor_map, low_map)) { |
|
454 |
int nn = (node_data[n].next != pn ? |
|
455 |
node_data[n].next : node_data[n].prev); |
|
456 |
|
|
457 |
bool nd = n == node_data[nn].prev; |
|
458 |
|
|
459 |
if (nd) node_data[nn].prev = pn; |
|
460 |
else node_data[nn].next = pn; |
|
461 |
|
|
462 |
if (n == node_data[pn].prev) node_data[pn].prev = nn; |
|
463 |
else node_data[pn].next = nn; |
|
464 |
|
|
465 |
node_data[nn].inverted = |
|
466 |
(node_data[nn].prev == node_data[nn].next && nd != rd); |
|
467 |
|
|
468 |
n = nn; |
|
469 |
} |
|
470 |
else break; |
|
471 |
|
|
472 |
} |
|
473 |
|
|
474 |
if (!merge_stack.empty() || n == rn) { |
|
475 |
break; |
|
476 |
} |
|
477 |
} |
|
478 |
} |
|
479 |
|
|
480 |
void initFace(const Node& node, NodeData& node_data, |
|
481 |
const OrderMap& order_map, const OrderList& order_list) { |
|
482 |
int n = order_map[node]; |
|
483 |
int rn = n + order_list.size(); |
|
484 |
|
|
485 |
node_data[n].next = node_data[n].prev = rn; |
|
486 |
node_data[rn].next = node_data[rn].prev = n; |
|
487 |
|
|
488 |
node_data[n].visited = order_list.size(); |
|
489 |
node_data[rn].visited = order_list.size(); |
|
490 |
|
|
491 |
} |
|
492 |
|
|
493 |
bool external(const Node& node, int rorder, |
|
494 |
ChildLists& child_lists, AncestorMap& ancestor_map, |
|
495 |
LowMap& low_map) { |
|
496 |
Node child = child_lists[node].first; |
|
497 |
|
|
498 |
if (child != INVALID) { |
|
499 |
if (low_map[child] < rorder) return true; |
|
500 |
} |
|
501 |
|
|
502 |
if (ancestor_map[node] < rorder) return true; |
|
503 |
|
|
504 |
return false; |
|
505 |
} |
|
506 |
|
|
507 |
bool pertinent(const Node& node, const EmbedArc& embed_arc, |
|
508 |
const MergeRoots& merge_roots) { |
|
509 |
return !merge_roots[node].empty() || embed_arc[node]; |
|
510 |
} |
|
511 |
|
|
512 |
}; |
|
513 |
|
|
140 | 514 |
} |
141 | 515 |
|
142 | 516 |
/// \ingroup planar |
143 | 517 |
/// |
144 | 518 |
/// \brief Planarity checking of an undirected simple graph |
145 | 519 |
/// |
146 |
/// This class implements the Boyer-Myrvold algorithm for planarity |
|
147 |
/// checking of an undirected graph. This class is a simplified |
|
520 |
/// This function implements the Boyer-Myrvold algorithm for |
|
521 |
/// planarity checking of an undirected graph. It is a simplified |
|
148 | 522 |
/// version of the PlanarEmbedding algorithm class because neither |
149 | 523 |
/// the embedding nor the kuratowski subdivisons are not computed. |
150 |
template <typename Graph> |
|
151 |
class PlanarityChecking { |
|
152 |
private: |
|
153 |
|
|
154 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
|
155 |
|
|
156 |
const Graph& _graph; |
|
157 |
|
|
158 |
private: |
|
159 |
|
|
160 |
typedef typename Graph::template NodeMap<Arc> PredMap; |
|
161 |
|
|
162 |
typedef typename Graph::template EdgeMap<bool> TreeMap; |
|
163 |
|
|
164 |
typedef typename Graph::template NodeMap<int> OrderMap; |
|
165 |
typedef std::vector<Node> OrderList; |
|
166 |
|
|
167 |
typedef typename Graph::template NodeMap<int> LowMap; |
|
168 |
typedef typename Graph::template NodeMap<int> AncestorMap; |
|
169 |
|
|
170 |
typedef _planarity_bits::NodeDataNode<Graph> NodeDataNode; |
|
171 |
typedef std::vector<NodeDataNode> NodeData; |
|
172 |
|
|
173 |
typedef _planarity_bits::ChildListNode<Graph> ChildListNode; |
|
174 |
typedef typename Graph::template NodeMap<ChildListNode> ChildLists; |
|
175 |
|
|
176 |
typedef typename Graph::template NodeMap<std::list<int> > MergeRoots; |
|
177 |
|
|
178 |
typedef typename Graph::template NodeMap<bool> EmbedArc; |
|
179 |
|
|
180 |
public: |
|
181 |
|
|
182 |
/// \brief Constructor |
|
183 |
/// |
|
184 |
/// \note The graph should be simple, i.e. parallel and loop arc |
|
185 |
/// free. |
|
186 |
PlanarityChecking(const Graph& graph) : _graph(graph) {} |
|
187 |
|
|
188 |
/// \brief Runs the algorithm. |
|
189 |
/// |
|
190 |
/// Runs the algorithm. |
|
191 |
/// \return %True when the graph is planar. |
|
192 |
bool run() { |
|
193 |
typedef _planarity_bits::PlanarityVisitor<Graph> Visitor; |
|
194 |
|
|
195 |
PredMap pred_map(_graph, INVALID); |
|
196 |
TreeMap tree_map(_graph, false); |
|
197 |
|
|
198 |
OrderMap order_map(_graph, -1); |
|
199 |
OrderList order_list; |
|
200 |
|
|
201 |
AncestorMap ancestor_map(_graph, -1); |
|
202 |
LowMap low_map(_graph, -1); |
|
203 |
|
|
204 |
Visitor visitor(_graph, pred_map, tree_map, |
|
205 |
order_map, order_list, ancestor_map, low_map); |
|
206 |
DfsVisit<Graph, Visitor> visit(_graph, visitor); |
|
207 |
visit.run(); |
|
208 |
|
|
209 |
ChildLists child_lists(_graph); |
|
210 |
createChildLists(tree_map, order_map, low_map, child_lists); |
|
211 |
|
|
212 |
NodeData node_data(2 * order_list.size()); |
|
213 |
|
|
214 |
EmbedArc embed_arc(_graph, false); |
|
215 |
|
|
216 |
MergeRoots merge_roots(_graph); |
|
217 |
|
|
218 |
for (int i = order_list.size() - 1; i >= 0; --i) { |
|
219 |
|
|
220 |
Node node = order_list[i]; |
|
221 |
|
|
222 |
Node source = node; |
|
223 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) { |
|
224 |
Node target = _graph.target(e); |
|
225 |
|
|
226 |
if (order_map[source] < order_map[target] && tree_map[e]) { |
|
227 |
initFace(target, node_data, order_map, order_list); |
|
228 |
} |
|
229 |
} |
|
230 |
|
|
231 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) { |
|
232 |
Node target = _graph.target(e); |
|
233 |
|
|
234 |
if (order_map[source] < order_map[target] && !tree_map[e]) { |
|
235 |
embed_arc[target] = true; |
|
236 |
walkUp(target, source, i, pred_map, low_map, |
|
237 |
order_map, order_list, node_data, merge_roots); |
|
238 |
} |
|
239 |
} |
|
240 |
|
|
241 |
for (typename MergeRoots::Value::iterator it = |
|
242 |
merge_roots[node].begin(); it != merge_roots[node].end(); ++it) { |
|
243 |
int rn = *it; |
|
244 |
walkDown(rn, i, node_data, order_list, child_lists, |
|
245 |
ancestor_map, low_map, embed_arc, merge_roots); |
|
246 |
} |
|
247 |
merge_roots[node].clear(); |
|
248 |
|
|
249 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) { |
|
250 |
Node target = _graph.target(e); |
|
251 |
|
|
252 |
if (order_map[source] < order_map[target] && !tree_map[e]) { |
|
253 |
if (embed_arc[target]) { |
|
254 |
return false; |
|
255 |
} |
|
256 |
} |
|
257 |
} |
|
258 |
} |
|
259 |
|
|
260 |
return true; |
|
261 |
} |
|
262 |
|
|
263 |
private: |
|
264 |
|
|
265 |
void createChildLists(const TreeMap& tree_map, const OrderMap& order_map, |
|
266 |
const LowMap& low_map, ChildLists& child_lists) { |
|
267 |
|
|
268 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
|
269 |
Node source = n; |
|
270 |
|
|
271 |
std::vector<Node> targets; |
|
272 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
|
273 |
Node target = _graph.target(e); |
|
274 |
|
|
275 |
if (order_map[source] < order_map[target] && tree_map[e]) { |
|
276 |
targets.push_back(target); |
|
277 |
} |
|
278 |
} |
|
279 |
|
|
280 |
if (targets.size() == 0) { |
|
281 |
child_lists[source].first = INVALID; |
|
282 |
} else if (targets.size() == 1) { |
|
283 |
child_lists[source].first = targets[0]; |
|
284 |
child_lists[targets[0]].prev = INVALID; |
|
285 |
child_lists[targets[0]].next = INVALID; |
|
286 |
} else { |
|
287 |
radixSort(targets.begin(), targets.end(), mapToFunctor(low_map)); |
|
288 |
for (int i = 1; i < int(targets.size()); ++i) { |
|
289 |
child_lists[targets[i]].prev = targets[i - 1]; |
|
290 |
child_lists[targets[i - 1]].next = targets[i]; |
|
291 |
} |
|
292 |
child_lists[targets.back()].next = INVALID; |
|
293 |
child_lists[targets.front()].prev = INVALID; |
|
294 |
child_lists[source].first = targets.front(); |
|
295 |
} |
|
296 |
} |
|
297 |
} |
|
298 |
|
|
299 |
void walkUp(const Node& node, Node root, int rorder, |
|
300 |
const PredMap& pred_map, const LowMap& low_map, |
|
301 |
const OrderMap& order_map, const OrderList& order_list, |
|
302 |
NodeData& node_data, MergeRoots& merge_roots) { |
|
303 |
|
|
304 |
int na, nb; |
|
305 |
bool da, db; |
|
306 |
|
|
307 |
na = nb = order_map[node]; |
|
308 |
da = true; db = false; |
|
309 |
|
|
310 |
while (true) { |
|
311 |
|
|
312 |
if (node_data[na].visited == rorder) break; |
|
313 |
if (node_data[nb].visited == rorder) break; |
|
314 |
|
|
315 |
node_data[na].visited = rorder; |
|
316 |
node_data[nb].visited = rorder; |
|
317 |
|
|
318 |
int rn = -1; |
|
319 |
|
|
320 |
if (na >= int(order_list.size())) { |
|
321 |
rn = na; |
|
322 |
} else if (nb >= int(order_list.size())) { |
|
323 |
rn = nb; |
|
324 |
} |
|
325 |
|
|
326 |
if (rn == -1) { |
|
327 |
int nn; |
|
328 |
|
|
329 |
nn = da ? node_data[na].prev : node_data[na].next; |
|
330 |
da = node_data[nn].prev != na; |
|
331 |
na = nn; |
|
332 |
|
|
333 |
nn = db ? node_data[nb].prev : node_data[nb].next; |
|
334 |
db = node_data[nn].prev != nb; |
|
335 |
nb = nn; |
|
336 |
|
|
337 |
} else { |
|
338 |
|
|
339 |
Node rep = order_list[rn - order_list.size()]; |
|
340 |
Node parent = _graph.source(pred_map[rep]); |
|
341 |
|
|
342 |
if (low_map[rep] < rorder) { |
|
343 |
merge_roots[parent].push_back(rn); |
|
344 |
} else { |
|
345 |
merge_roots[parent].push_front(rn); |
|
346 |
} |
|
347 |
|
|
348 |
if (parent != root) { |
|
349 |
na = nb = order_map[parent]; |
|
350 |
da = true; db = false; |
|
351 |
} else { |
|
352 |
break; |
|
353 |
} |
|
354 |
} |
|
355 |
} |
|
356 |
} |
|
357 |
|
|
358 |
void walkDown(int rn, int rorder, NodeData& node_data, |
|
359 |
OrderList& order_list, ChildLists& child_lists, |
|
360 |
AncestorMap& ancestor_map, LowMap& low_map, |
|
361 |
EmbedArc& embed_arc, MergeRoots& merge_roots) { |
|
362 |
|
|
363 |
std::vector<std::pair<int, bool> > merge_stack; |
|
364 |
|
|
365 |
for (int di = 0; di < 2; ++di) { |
|
366 |
bool rd = di == 0; |
|
367 |
int pn = rn; |
|
368 |
int n = rd ? node_data[rn].next : node_data[rn].prev; |
|
369 |
|
|
370 |
while (n != rn) { |
|
371 |
|
|
372 |
Node node = order_list[n]; |
|
373 |
|
|
374 |
if (embed_arc[node]) { |
|
375 |
|
|
376 |
// Merging components on the critical path |
|
377 |
while (!merge_stack.empty()) { |
|
378 |
|
|
379 |
// Component root |
|
380 |
int cn = merge_stack.back().first; |
|
381 |
bool cd = merge_stack.back().second; |
|
382 |
merge_stack.pop_back(); |
|
383 |
|
|
384 |
// Parent of component |
|
385 |
int dn = merge_stack.back().first; |
|
386 |
bool dd = merge_stack.back().second; |
|
387 |
merge_stack.pop_back(); |
|
388 |
|
|
389 |
Node parent = order_list[dn]; |
|
390 |
|
|
391 |
// Erasing from merge_roots |
|
392 |
merge_roots[parent].pop_front(); |
|
393 |
|
|
394 |
Node child = order_list[cn - order_list.size()]; |
|
395 |
|
|
396 |
// Erasing from child_lists |
|
397 |
if (child_lists[child].prev != INVALID) { |
|
398 |
child_lists[child_lists[child].prev].next = |
|
399 |
child_lists[child].next; |
|
400 |
} else { |
|
401 |
child_lists[parent].first = child_lists[child].next; |
|
402 |
} |
|
403 |
|
|
404 |
if (child_lists[child].next != INVALID) { |
|
405 |
child_lists[child_lists[child].next].prev = |
|
406 |
child_lists[child].prev; |
|
407 |
} |
|
408 |
|
|
409 |
// Merging external faces |
|
410 |
{ |
|
411 |
int en = cn; |
|
412 |
cn = cd ? node_data[cn].prev : node_data[cn].next; |
|
413 |
cd = node_data[cn].next == en; |
|
414 |
|
|
415 |
} |
|
416 |
|
|
417 |
if (cd) node_data[cn].next = dn; else node_data[cn].prev = dn; |
|
418 |
if (dd) node_data[dn].prev = cn; else node_data[dn].next = cn; |
|
419 |
|
|
420 |
} |
|
421 |
|
|
422 |
bool d = pn == node_data[n].prev; |
|
423 |
|
|
424 |
if (node_data[n].prev == node_data[n].next && |
|
425 |
node_data[n].inverted) { |
|
426 |
d = !d; |
|
427 |
} |
|
428 |
|
|
429 |
// Embedding arc into external face |
|
430 |
if (rd) node_data[rn].next = n; else node_data[rn].prev = n; |
|
431 |
if (d) node_data[n].prev = rn; else node_data[n].next = rn; |
|
432 |
pn = rn; |
|
433 |
|
|
434 |
embed_arc[order_list[n]] = false; |
|
435 |
} |
|
436 |
|
|
437 |
if (!merge_roots[node].empty()) { |
|
438 |
|
|
439 |
bool d = pn == node_data[n].prev; |
|
440 |
|
|
441 |
merge_stack.push_back(std::make_pair(n, d)); |
|
442 |
|
|
443 |
int rn = merge_roots[node].front(); |
|
444 |
|
|
445 |
int xn = node_data[rn].next; |
|
446 |
Node xnode = order_list[xn]; |
|
447 |
|
|
448 |
int yn = node_data[rn].prev; |
|
449 |
Node ynode = order_list[yn]; |
|
450 |
|
|
451 |
bool rd; |
|
452 |
if (!external(xnode, rorder, child_lists, ancestor_map, low_map)) { |
|
453 |
rd = true; |
|
454 |
} else if (!external(ynode, rorder, child_lists, |
|
455 |
ancestor_map, low_map)) { |
|
456 |
rd = false; |
|
457 |
} else if (pertinent(xnode, embed_arc, merge_roots)) { |
|
458 |
rd = true; |
|
459 |
} else { |
|
460 |
rd = false; |
|
461 |
} |
|
462 |
|
|
463 |
merge_stack.push_back(std::make_pair(rn, rd)); |
|
464 |
|
|
465 |
pn = rn; |
|
466 |
n = rd ? xn : yn; |
|
467 |
|
|
468 |
} else if (!external(node, rorder, child_lists, |
|
469 |
ancestor_map, low_map)) { |
|
470 |
int nn = (node_data[n].next != pn ? |
|
471 |
node_data[n].next : node_data[n].prev); |
|
472 |
|
|
473 |
bool nd = n == node_data[nn].prev; |
|
474 |
|
|
475 |
if (nd) node_data[nn].prev = pn; |
|
476 |
else node_data[nn].next = pn; |
|
477 |
|
|
478 |
if (n == node_data[pn].prev) node_data[pn].prev = nn; |
|
479 |
else node_data[pn].next = nn; |
|
480 |
|
|
481 |
node_data[nn].inverted = |
|
482 |
(node_data[nn].prev == node_data[nn].next && nd != rd); |
|
483 |
|
|
484 |
n = nn; |
|
485 |
} |
|
486 |
else break; |
|
487 |
|
|
488 |
} |
|
489 |
|
|
490 |
if (!merge_stack.empty() || n == rn) { |
|
491 |
break; |
|
492 |
} |
|
493 |
} |
|
494 |
} |
|
495 |
|
|
496 |
void initFace(const Node& node, NodeData& node_data, |
|
497 |
const OrderMap& order_map, const OrderList& order_list) { |
|
498 |
int n = order_map[node]; |
|
499 |
int rn = n + order_list.size(); |
|
500 |
|
|
501 |
node_data[n].next = node_data[n].prev = rn; |
|
502 |
node_data[rn].next = node_data[rn].prev = n; |
|
503 |
|
|
504 |
node_data[n].visited = order_list.size(); |
|
505 |
node_data[rn].visited = order_list.size(); |
|
506 |
|
|
507 |
} |
|
508 |
|
|
509 |
bool external(const Node& node, int rorder, |
|
510 |
ChildLists& child_lists, AncestorMap& ancestor_map, |
|
511 |
LowMap& low_map) { |
|
512 |
Node child = child_lists[node].first; |
|
513 |
|
|
514 |
if (child != INVALID) { |
|
515 |
if (low_map[child] < rorder) return true; |
|
516 |
} |
|
517 |
|
|
518 |
if (ancestor_map[node] < rorder) return true; |
|
519 |
|
|
520 |
return false; |
|
521 |
} |
|
522 |
|
|
523 |
bool pertinent(const Node& node, const EmbedArc& embed_arc, |
|
524 |
const MergeRoots& merge_roots) { |
|
525 |
return !merge_roots[node].empty() || embed_arc[node]; |
|
526 |
} |
|
527 |
|
|
528 |
|
|
524 |
template <typename GR> |
|
525 |
bool checkPlanarity(const GR& graph) { |
|
526 |
_planarity_bits::PlanarityChecking<GR> pc(graph); |
|
527 |
return pc.run(); |
|
528 |
} |
|
529 | 529 |
|
530 | 530 |
/// \ingroup planar |
531 | 531 |
/// |
532 | 532 |
/// \brief Planar embedding of an undirected simple graph |
533 | 533 |
/// |
534 | 534 |
/// This class implements the Boyer-Myrvold algorithm for planar |
535 | 535 |
/// embedding of an undirected graph. The planar embedding is an |
536 | 536 |
/// ordering of the outgoing edges of the nodes, which is a possible |
537 | 537 |
/// configuration to draw the graph in the plane. If there is not |
538 | 538 |
/// such ordering then the graph contains a \f$ K_5 \f$ (full graph |
539 | 539 |
/// with 5 nodes) or a \f$ K_{3,3} \f$ (complete bipartite graph on |
540 | 540 |
/// 3 ANode and 3 BNode) subdivision. |
541 | 541 |
/// |
542 | 542 |
/// The current implementation calculates either an embedding or a |
543 | 543 |
/// Kuratowski subdivision. The running time of the algorithm is |
544 | 544 |
/// \f$ O(n) \f$. |
545 | 545 |
template <typename Graph> |
546 | 546 |
class PlanarEmbedding { |
547 | 547 |
private: |
548 | 548 |
|
549 | 549 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
550 | 550 |
|
551 | 551 |
const Graph& _graph; |
552 | 552 |
typename Graph::template ArcMap<Arc> _embedding; |
553 | 553 |
|
554 | 554 |
typename Graph::template EdgeMap<bool> _kuratowski; |
555 | 555 |
|
556 | 556 |
private: |
557 | 557 |
|
558 | 558 |
typedef typename Graph::template NodeMap<Arc> PredMap; |
559 | 559 |
|
560 | 560 |
typedef typename Graph::template EdgeMap<bool> TreeMap; |
561 | 561 |
|
562 | 562 |
typedef typename Graph::template NodeMap<int> OrderMap; |
563 | 563 |
typedef std::vector<Node> OrderList; |
564 | 564 |
|
565 | 565 |
typedef typename Graph::template NodeMap<int> LowMap; |
566 | 566 |
typedef typename Graph::template NodeMap<int> AncestorMap; |
567 | 567 |
|
568 | 568 |
typedef _planarity_bits::NodeDataNode<Graph> NodeDataNode; |
569 | 569 |
typedef std::vector<NodeDataNode> NodeData; |
570 | 570 |
|
571 | 571 |
typedef _planarity_bits::ChildListNode<Graph> ChildListNode; |
572 | 572 |
typedef typename Graph::template NodeMap<ChildListNode> ChildLists; |
573 | 573 |
|
574 | 574 |
typedef typename Graph::template NodeMap<std::list<int> > MergeRoots; |
575 | 575 |
|
576 | 576 |
typedef typename Graph::template NodeMap<Arc> EmbedArc; |
577 | 577 |
|
578 | 578 |
typedef _planarity_bits::ArcListNode<Graph> ArcListNode; |
579 | 579 |
typedef typename Graph::template ArcMap<ArcListNode> ArcLists; |
580 | 580 |
|
581 | 581 |
typedef typename Graph::template NodeMap<bool> FlipMap; |
582 | 582 |
|
583 | 583 |
typedef typename Graph::template NodeMap<int> TypeMap; |
584 | 584 |
|
585 | 585 |
enum IsolatorNodeType { |
586 | 586 |
HIGHX = 6, LOWX = 7, |
587 | 587 |
HIGHY = 8, LOWY = 9, |
588 | 588 |
ROOT = 10, PERTINENT = 11, |
589 | 589 |
INTERNAL = 12 |
590 | 590 |
}; |
591 | 591 |
|
592 | 592 |
public: |
593 | 593 |
|
594 | 594 |
/// \brief The map for store of embedding |
595 | 595 |
typedef typename Graph::template ArcMap<Arc> EmbeddingMap; |
596 | 596 |
|
597 | 597 |
/// \brief Constructor |
598 | 598 |
/// |
599 | 599 |
/// \note The graph should be simple, i.e. parallel and loop arc |
600 | 600 |
/// free. |
601 | 601 |
PlanarEmbedding(const Graph& graph) |
602 | 602 |
: _graph(graph), _embedding(_graph), _kuratowski(graph, false) {} |
603 | 603 |
|
604 | 604 |
/// \brief Runs the algorithm. |
605 | 605 |
/// |
606 | 606 |
/// Runs the algorithm. |
607 | 607 |
/// \param kuratowski If the parameter is false, then the |
608 | 608 |
/// algorithm does not compute a Kuratowski subdivision. |
609 | 609 |
///\return %True when the graph is planar. |
610 | 610 |
bool run(bool kuratowski = true) { |
611 | 611 |
typedef _planarity_bits::PlanarityVisitor<Graph> Visitor; |
612 | 612 |
|
613 | 613 |
PredMap pred_map(_graph, INVALID); |
614 | 614 |
TreeMap tree_map(_graph, false); |
615 | 615 |
|
616 | 616 |
OrderMap order_map(_graph, -1); |
617 | 617 |
OrderList order_list; |
618 | 618 |
|
619 | 619 |
AncestorMap ancestor_map(_graph, -1); |
620 | 620 |
LowMap low_map(_graph, -1); |
621 | 621 |
|
622 | 622 |
Visitor visitor(_graph, pred_map, tree_map, |
623 | 623 |
order_map, order_list, ancestor_map, low_map); |
624 | 624 |
DfsVisit<Graph, Visitor> visit(_graph, visitor); |
625 | 625 |
visit.run(); |
626 | 626 |
|
627 | 627 |
ChildLists child_lists(_graph); |
628 | 628 |
createChildLists(tree_map, order_map, low_map, child_lists); |
629 | 629 |
|
630 | 630 |
NodeData node_data(2 * order_list.size()); |
631 | 631 |
|
632 | 632 |
EmbedArc embed_arc(_graph, INVALID); |
633 | 633 |
|
634 | 634 |
MergeRoots merge_roots(_graph); |
635 | 635 |
|
636 | 636 |
ArcLists arc_lists(_graph); |
637 | 637 |
|
638 | 638 |
FlipMap flip_map(_graph, false); |
639 | 639 |
|
640 | 640 |
for (int i = order_list.size() - 1; i >= 0; --i) { |
641 | 641 |
|
642 | 642 |
Node node = order_list[i]; |
643 | 643 |
|
644 | 644 |
node_data[i].first = INVALID; |
645 | 645 |
|
646 | 646 |
Node source = node; |
647 | 647 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) { |
648 | 648 |
Node target = _graph.target(e); |
649 | 649 |
|
650 | 650 |
if (order_map[source] < order_map[target] && tree_map[e]) { |
651 | 651 |
initFace(target, arc_lists, node_data, |
652 | 652 |
pred_map, order_map, order_list); |
653 | 653 |
} |
654 | 654 |
} |
655 | 655 |
|
656 | 656 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) { |
657 | 657 |
Node target = _graph.target(e); |
658 | 658 |
|
659 | 659 |
if (order_map[source] < order_map[target] && !tree_map[e]) { |
660 | 660 |
embed_arc[target] = e; |
661 | 661 |
walkUp(target, source, i, pred_map, low_map, |
662 | 662 |
order_map, order_list, node_data, merge_roots); |
663 | 663 |
} |
664 | 664 |
} |
665 | 665 |
|
666 | 666 |
for (typename MergeRoots::Value::iterator it = |
667 | 667 |
merge_roots[node].begin(); it != merge_roots[node].end(); ++it) { |
668 | 668 |
int rn = *it; |
669 | 669 |
walkDown(rn, i, node_data, arc_lists, flip_map, order_list, |
670 | 670 |
child_lists, ancestor_map, low_map, embed_arc, merge_roots); |
671 | 671 |
} |
672 | 672 |
merge_roots[node].clear(); |
673 | 673 |
|
674 | 674 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) { |
675 | 675 |
Node target = _graph.target(e); |
676 | 676 |
|
677 | 677 |
if (order_map[source] < order_map[target] && !tree_map[e]) { |
678 | 678 |
if (embed_arc[target] != INVALID) { |
679 | 679 |
if (kuratowski) { |
680 | 680 |
isolateKuratowski(e, node_data, arc_lists, flip_map, |
681 | 681 |
order_map, order_list, pred_map, child_lists, |
682 | 682 |
ancestor_map, low_map, |
683 | 683 |
embed_arc, merge_roots); |
684 | 684 |
} |
685 | 685 |
return false; |
686 | 686 |
} |
687 | 687 |
} |
688 | 688 |
} |
689 | 689 |
} |
690 | 690 |
|
691 | 691 |
for (int i = 0; i < int(order_list.size()); ++i) { |
692 | 692 |
|
693 | 693 |
mergeRemainingFaces(order_list[i], node_data, order_list, order_map, |
694 | 694 |
child_lists, arc_lists); |
695 | 695 |
storeEmbedding(order_list[i], node_data, order_map, pred_map, |
696 | 696 |
arc_lists, flip_map); |
697 | 697 |
} |
698 | 698 |
|
699 | 699 |
return true; |
700 | 700 |
} |
701 | 701 |
|
702 | 702 |
/// \brief Gives back the successor of an arc |
703 | 703 |
/// |
704 | 704 |
/// Gives back the successor of an arc. This function makes |
705 | 705 |
/// possible to query the cyclic order of the outgoing arcs from |
706 | 706 |
/// a node. |
707 | 707 |
Arc next(const Arc& arc) const { |
708 | 708 |
return _embedding[arc]; |
709 | 709 |
} |
710 | 710 |
|
711 | 711 |
/// \brief Gives back the calculated embedding map |
712 | 712 |
/// |
713 | 713 |
/// The returned map contains the successor of each arc in the |
714 | 714 |
/// graph. |
715 |
const EmbeddingMap& |
|
715 |
const EmbeddingMap& embeddingMap() const { |
|
716 | 716 |
return _embedding; |
717 | 717 |
} |
718 | 718 |
|
719 | 719 |
/// \brief Gives back true if the undirected arc is in the |
720 | 720 |
/// kuratowski subdivision |
721 | 721 |
/// |
722 | 722 |
/// Gives back true if the undirected arc is in the kuratowski |
723 | 723 |
/// subdivision |
724 | 724 |
/// \note The \c run() had to be called with true value. |
725 | 725 |
bool kuratowski(const Edge& edge) { |
726 | 726 |
return _kuratowski[edge]; |
727 | 727 |
} |
728 | 728 |
|
729 | 729 |
private: |
730 | 730 |
|
731 | 731 |
void createChildLists(const TreeMap& tree_map, const OrderMap& order_map, |
732 | 732 |
const LowMap& low_map, ChildLists& child_lists) { |
733 | 733 |
|
734 | 734 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
735 | 735 |
Node source = n; |
736 | 736 |
|
737 | 737 |
std::vector<Node> targets; |
738 | 738 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) { |
739 | 739 |
Node target = _graph.target(e); |
740 | 740 |
|
741 | 741 |
if (order_map[source] < order_map[target] && tree_map[e]) { |
742 | 742 |
targets.push_back(target); |
743 | 743 |
} |
744 | 744 |
} |
745 | 745 |
|
746 | 746 |
if (targets.size() == 0) { |
747 | 747 |
child_lists[source].first = INVALID; |
748 | 748 |
} else if (targets.size() == 1) { |
749 | 749 |
child_lists[source].first = targets[0]; |
750 | 750 |
child_lists[targets[0]].prev = INVALID; |
751 | 751 |
child_lists[targets[0]].next = INVALID; |
752 | 752 |
} else { |
753 | 753 |
radixSort(targets.begin(), targets.end(), mapToFunctor(low_map)); |
754 | 754 |
for (int i = 1; i < int(targets.size()); ++i) { |
755 | 755 |
child_lists[targets[i]].prev = targets[i - 1]; |
756 | 756 |
child_lists[targets[i - 1]].next = targets[i]; |
757 | 757 |
} |
758 | 758 |
child_lists[targets.back()].next = INVALID; |
759 | 759 |
child_lists[targets.front()].prev = INVALID; |
760 | 760 |
child_lists[source].first = targets.front(); |
761 | 761 |
} |
762 | 762 |
} |
763 | 763 |
} |
764 | 764 |
|
765 | 765 |
void walkUp(const Node& node, Node root, int rorder, |
766 | 766 |
const PredMap& pred_map, const LowMap& low_map, |
767 | 767 |
const OrderMap& order_map, const OrderList& order_list, |
768 | 768 |
NodeData& node_data, MergeRoots& merge_roots) { |
769 | 769 |
|
770 | 770 |
int na, nb; |
771 | 771 |
bool da, db; |
772 | 772 |
|
773 | 773 |
na = nb = order_map[node]; |
774 | 774 |
da = true; db = false; |
775 | 775 |
|
776 | 776 |
while (true) { |
777 | 777 |
|
778 | 778 |
if (node_data[na].visited == rorder) break; |
779 | 779 |
if (node_data[nb].visited == rorder) break; |
780 | 780 |
|
781 | 781 |
node_data[na].visited = rorder; |
782 | 782 |
node_data[nb].visited = rorder; |
783 | 783 |
|
784 | 784 |
int rn = -1; |
785 | 785 |
|
786 | 786 |
if (na >= int(order_list.size())) { |
787 | 787 |
rn = na; |
788 | 788 |
} else if (nb >= int(order_list.size())) { |
789 | 789 |
rn = nb; |
790 | 790 |
} |
791 | 791 |
|
792 | 792 |
if (rn == -1) { |
793 | 793 |
int nn; |
794 | 794 |
|
795 | 795 |
nn = da ? node_data[na].prev : node_data[na].next; |
796 | 796 |
da = node_data[nn].prev != na; |
797 | 797 |
na = nn; |
798 | 798 |
|
799 | 799 |
nn = db ? node_data[nb].prev : node_data[nb].next; |
800 | 800 |
db = node_data[nn].prev != nb; |
801 | 801 |
nb = nn; |
802 | 802 |
|
803 | 803 |
} else { |
804 | 804 |
|
805 | 805 |
Node rep = order_list[rn - order_list.size()]; |
806 | 806 |
Node parent = _graph.source(pred_map[rep]); |
807 | 807 |
|
808 | 808 |
if (low_map[rep] < rorder) { |
809 | 809 |
merge_roots[parent].push_back(rn); |
810 | 810 |
} else { |
811 | 811 |
merge_roots[parent].push_front(rn); |
... | ... |
@@ -146,114 +146,117 @@ |
146 | 146 |
int deg = 0; |
147 | 147 |
for (IncEdgeIt e(graph, n); e != INVALID; ++e) { |
148 | 148 |
if (pe.kuratowski(e)) { |
149 | 149 |
++deg; |
150 | 150 |
} |
151 | 151 |
} |
152 | 152 |
++degs[deg]; |
153 | 153 |
} |
154 | 154 |
for (std::map<int, int>::iterator it = degs.begin(); it != degs.end(); ++it) { |
155 | 155 |
check(it->first == 0 || it->first == 2 || |
156 | 156 |
(it->first == 3 && it->second == 6) || |
157 | 157 |
(it->first == 4 && it->second == 5), |
158 | 158 |
"Wrong degree in Kuratowski graph"); |
159 | 159 |
} |
160 | 160 |
|
161 | 161 |
// Not full test |
162 | 162 |
check((degs[3] == 0) != (degs[4] == 0), "Wrong Kuratowski graph"); |
163 | 163 |
} |
164 | 164 |
|
165 | 165 |
bool intersect(Point<int> e1, Point<int> e2, Point<int> f1, Point<int> f2) { |
166 | 166 |
int l, r; |
167 | 167 |
if (std::min(e1.x, e2.x) > std::max(f1.x, f2.x)) return false; |
168 | 168 |
if (std::max(e1.x, e2.x) < std::min(f1.x, f2.x)) return false; |
169 | 169 |
if (std::min(e1.y, e2.y) > std::max(f1.y, f2.y)) return false; |
170 | 170 |
if (std::max(e1.y, e2.y) < std::min(f1.y, f2.y)) return false; |
171 | 171 |
|
172 | 172 |
l = (e2.x - e1.x) * (f1.y - e1.y) - (e2.y - e1.y) * (f1.x - e1.x); |
173 | 173 |
r = (e2.x - e1.x) * (f2.y - e1.y) - (e2.y - e1.y) * (f2.x - e1.x); |
174 | 174 |
if (!((l >= 0 && r <= 0) || (l <= 0 && r >= 0))) return false; |
175 | 175 |
l = (f2.x - f1.x) * (e1.y - f1.y) - (f2.y - f1.y) * (e1.x - f1.x); |
176 | 176 |
r = (f2.x - f1.x) * (e2.y - f1.y) - (f2.y - f1.y) * (e2.x - f1.x); |
177 | 177 |
if (!((l >= 0 && r <= 0) || (l <= 0 && r >= 0))) return false; |
178 | 178 |
return true; |
179 | 179 |
} |
180 | 180 |
|
181 | 181 |
bool collinear(Point<int> p, Point<int> q, Point<int> r) { |
182 | 182 |
int v; |
183 | 183 |
v = (q.x - p.x) * (r.y - p.y) - (q.y - p.y) * (r.x - p.x); |
184 | 184 |
if (v != 0) return false; |
185 | 185 |
v = (q.x - p.x) * (r.x - p.x) + (q.y - p.y) * (r.y - p.y); |
186 | 186 |
if (v < 0) return false; |
187 | 187 |
return true; |
188 | 188 |
} |
189 | 189 |
|
190 | 190 |
void checkDrawing(const Graph& graph, PD& pd) { |
191 | 191 |
for (Graph::NodeIt n(graph); n != INVALID; ++n) { |
192 | 192 |
Graph::NodeIt m(n); |
193 | 193 |
for (++m; m != INVALID; ++m) { |
194 | 194 |
check(pd[m] != pd[n], "Two nodes with identical coordinates"); |
195 | 195 |
} |
196 | 196 |
} |
197 | 197 |
|
198 | 198 |
for (Graph::EdgeIt e(graph); e != INVALID; ++e) { |
199 | 199 |
for (Graph::EdgeIt f(e); f != e; ++f) { |
200 | 200 |
Point<int> e1 = pd[graph.u(e)]; |
201 | 201 |
Point<int> e2 = pd[graph.v(e)]; |
202 | 202 |
Point<int> f1 = pd[graph.u(f)]; |
203 | 203 |
Point<int> f2 = pd[graph.v(f)]; |
204 | 204 |
|
205 | 205 |
if (graph.u(e) == graph.u(f)) { |
206 | 206 |
check(!collinear(e1, e2, f2), "Wrong drawing"); |
207 | 207 |
} else if (graph.u(e) == graph.v(f)) { |
208 | 208 |
check(!collinear(e1, e2, f1), "Wrong drawing"); |
209 | 209 |
} else if (graph.v(e) == graph.u(f)) { |
210 | 210 |
check(!collinear(e2, e1, f2), "Wrong drawing"); |
211 | 211 |
} else if (graph.v(e) == graph.v(f)) { |
212 | 212 |
check(!collinear(e2, e1, f1), "Wrong drawing"); |
213 | 213 |
} else { |
214 | 214 |
check(!intersect(e1, e2, f1, f2), "Wrong drawing"); |
215 | 215 |
} |
216 | 216 |
} |
217 | 217 |
} |
218 | 218 |
} |
219 | 219 |
|
220 | 220 |
void checkColoring(const Graph& graph, PC& pc, int num) { |
221 | 221 |
for (NodeIt n(graph); n != INVALID; ++n) { |
222 | 222 |
check(pc.colorIndex(n) >= 0 && pc.colorIndex(n) < num, |
223 | 223 |
"Wrong coloring"); |
224 | 224 |
} |
225 | 225 |
for (EdgeIt e(graph); e != INVALID; ++e) { |
226 | 226 |
check(pc.colorIndex(graph.u(e)) != pc.colorIndex(graph.v(e)), |
227 | 227 |
"Wrong coloring"); |
228 | 228 |
} |
229 | 229 |
} |
230 | 230 |
|
231 | 231 |
int main() { |
232 | 232 |
|
233 | 233 |
for (int i = 0; i < lgfn; ++i) { |
234 | 234 |
std::istringstream lgfs(lgf[i]); |
235 | 235 |
|
236 | 236 |
SmartGraph graph; |
237 | 237 |
graphReader(graph, lgfs).run(); |
238 | 238 |
|
239 | 239 |
check(simpleGraph(graph), "Test graphs must be simple"); |
240 | 240 |
|
241 | 241 |
PE pe(graph); |
242 |
|
|
242 |
bool planar = pe.run(); |
|
243 |
check(checkPlanarity(graph) == planar, "Planarity checking failed"); |
|
244 |
|
|
245 |
if (planar) { |
|
243 | 246 |
checkEmbedding(graph, pe); |
244 | 247 |
|
245 | 248 |
PlanarDrawing<Graph> pd(graph); |
246 |
pd.run(pe. |
|
249 |
pd.run(pe.embeddingMap()); |
|
247 | 250 |
checkDrawing(graph, pd); |
248 | 251 |
|
249 | 252 |
PlanarColoring<Graph> pc(graph); |
250 |
pc.runFiveColoring(pe. |
|
253 |
pc.runFiveColoring(pe.embeddingMap()); |
|
251 | 254 |
checkColoring(graph, pc, 5); |
252 | 255 |
|
253 | 256 |
} else { |
254 | 257 |
checkKuratowski(graph, pe); |
255 | 258 |
} |
256 | 259 |
} |
257 | 260 |
|
258 | 261 |
return 0; |
259 | 262 |
} |
0 comments (0 inline)