gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Doc improvements for planarity related tools (#62)
0 1 0
default
1 file changed with 94 insertions and 76 deletions:
↑ Collapse diff ↑
Show white space 4194304 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_PLANARITY_H
20 20
#define LEMON_PLANARITY_H
21 21

	
22 22
/// \ingroup planar
23 23
/// \file
24 24
/// \brief Planarity checking, embedding, drawing and coloring
25 25

	
26 26
#include <vector>
27 27
#include <list>
28 28

	
29 29
#include <lemon/dfs.h>
30 30
#include <lemon/bfs.h>
31 31
#include <lemon/radix_sort.h>
32 32
#include <lemon/maps.h>
33 33
#include <lemon/path.h>
34 34
#include <lemon/bucket_heap.h>
35 35
#include <lemon/adaptors.h>
36 36
#include <lemon/edge_set.h>
37 37
#include <lemon/color.h>
38 38
#include <lemon/dim2.h>
39 39

	
40 40
namespace lemon {
41 41

	
42 42
  namespace _planarity_bits {
43 43

	
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 140
    template <typename Graph>
141 141
    class PlanarityChecking {
142 142
    private:
143 143
      
144 144
      TEMPLATE_GRAPH_TYPEDEFS(Graph);
145 145

	
146 146
      const Graph& _graph;
147 147

	
148 148
    private:
149 149
      
150 150
      typedef typename Graph::template NodeMap<Arc> PredMap;
151 151
      
152 152
      typedef typename Graph::template EdgeMap<bool> TreeMap;
153 153
      
154 154
      typedef typename Graph::template NodeMap<int> OrderMap;
155 155
      typedef std::vector<Node> OrderList;
156 156

	
157 157
      typedef typename Graph::template NodeMap<int> LowMap;
158 158
      typedef typename Graph::template NodeMap<int> AncestorMap;
159 159

	
160 160
      typedef _planarity_bits::NodeDataNode<Graph> NodeDataNode;
161 161
      typedef std::vector<NodeDataNode> NodeData;
162 162

	
163 163
      typedef _planarity_bits::ChildListNode<Graph> ChildListNode;
164 164
      typedef typename Graph::template NodeMap<ChildListNode> ChildLists;
165 165

	
166 166
      typedef typename Graph::template NodeMap<std::list<int> > MergeRoots;
167 167

	
168 168
      typedef typename Graph::template NodeMap<bool> EmbedArc;
169 169

	
170 170
    public:
171 171

	
172 172
      PlanarityChecking(const Graph& graph) : _graph(graph) {}
173 173

	
174 174
      bool run() {
175 175
        typedef _planarity_bits::PlanarityVisitor<Graph> Visitor;
176 176

	
177 177
        PredMap pred_map(_graph, INVALID);
178 178
        TreeMap tree_map(_graph, false);
179 179

	
180 180
        OrderMap order_map(_graph, -1);
181 181
        OrderList order_list;
182 182

	
183 183
        AncestorMap ancestor_map(_graph, -1);
184 184
        LowMap low_map(_graph, -1);
185 185

	
186 186
        Visitor visitor(_graph, pred_map, tree_map,
187 187
                        order_map, order_list, ancestor_map, low_map);
188 188
        DfsVisit<Graph, Visitor> visit(_graph, visitor);
189 189
        visit.run();
190 190

	
191 191
        ChildLists child_lists(_graph);
192 192
        createChildLists(tree_map, order_map, low_map, child_lists);
193 193

	
194 194
        NodeData node_data(2 * order_list.size());
195 195

	
196 196
        EmbedArc embed_arc(_graph, false);
197 197

	
198 198
        MergeRoots merge_roots(_graph);
199 199

	
200 200
        for (int i = order_list.size() - 1; i >= 0; --i) {
201 201

	
202 202
          Node node = order_list[i];
203 203

	
204 204
          Node source = node;
205 205
          for (OutArcIt e(_graph, node); e != INVALID; ++e) {
206 206
            Node target = _graph.target(e);
207 207

	
208 208
            if (order_map[source] < order_map[target] && tree_map[e]) {
209 209
              initFace(target, node_data, order_map, order_list);
210 210
            }
211 211
          }
212 212

	
213 213
          for (OutArcIt e(_graph, node); e != INVALID; ++e) {
214 214
            Node target = _graph.target(e);
215 215

	
216 216
            if (order_map[source] < order_map[target] && !tree_map[e]) {
217 217
              embed_arc[target] = true;
218 218
              walkUp(target, source, i, pred_map, low_map,
219 219
                     order_map, order_list, node_data, merge_roots);
220 220
            }
221 221
          }
222 222

	
223 223
          for (typename MergeRoots::Value::iterator it =
224 224
                 merge_roots[node].begin(); 
225 225
               it != merge_roots[node].end(); ++it) {
226 226
            int rn = *it;
227 227
            walkDown(rn, i, node_data, order_list, child_lists,
228 228
                     ancestor_map, low_map, embed_arc, merge_roots);
229 229
          }
230 230
          merge_roots[node].clear();
231 231

	
232 232
          for (OutArcIt e(_graph, node); e != INVALID; ++e) {
233 233
            Node target = _graph.target(e);
234 234

	
235 235
            if (order_map[source] < order_map[target] && !tree_map[e]) {
236 236
              if (embed_arc[target]) {
237 237
                return false;
238 238
              }
239 239
            }
240 240
          }
241 241
        }
242 242

	
243 243
        return true;
244 244
      }
245 245

	
246 246
    private:
247 247

	
248 248
      void createChildLists(const TreeMap& tree_map, const OrderMap& order_map,
249 249
                            const LowMap& low_map, ChildLists& child_lists) {
250 250

	
251 251
        for (NodeIt n(_graph); n != INVALID; ++n) {
252 252
          Node source = n;
253 253

	
254 254
          std::vector<Node> targets;
255 255
          for (OutArcIt e(_graph, n); e != INVALID; ++e) {
256 256
            Node target = _graph.target(e);
257 257

	
258 258
            if (order_map[source] < order_map[target] && tree_map[e]) {
259 259
              targets.push_back(target);
260 260
            }
261 261
          }
262 262

	
263 263
          if (targets.size() == 0) {
264 264
            child_lists[source].first = INVALID;
265 265
          } else if (targets.size() == 1) {
266 266
            child_lists[source].first = targets[0];
267 267
            child_lists[targets[0]].prev = INVALID;
268 268
            child_lists[targets[0]].next = INVALID;
269 269
          } else {
270 270
            radixSort(targets.begin(), targets.end(), mapToFunctor(low_map));
271 271
            for (int i = 1; i < int(targets.size()); ++i) {
272 272
              child_lists[targets[i]].prev = targets[i - 1];
273 273
              child_lists[targets[i - 1]].next = targets[i];
274 274
            }
275 275
            child_lists[targets.back()].next = INVALID;
276 276
            child_lists[targets.front()].prev = INVALID;
277 277
            child_lists[source].first = targets.front();
278 278
          }
279 279
        }
280 280
      }
281 281

	
282 282
      void walkUp(const Node& node, Node root, int rorder,
283 283
                  const PredMap& pred_map, const LowMap& low_map,
284 284
                  const OrderMap& order_map, const OrderList& order_list,
285 285
                  NodeData& node_data, MergeRoots& merge_roots) {
286 286

	
287 287
        int na, nb;
288 288
        bool da, db;
289 289

	
290 290
        na = nb = order_map[node];
291 291
        da = true; db = false;
292 292

	
293 293
        while (true) {
294 294

	
295 295
          if (node_data[na].visited == rorder) break;
296 296
          if (node_data[nb].visited == rorder) break;
297 297

	
298 298
          node_data[na].visited = rorder;
299 299
          node_data[nb].visited = rorder;
300 300

	
301 301
          int rn = -1;
302 302

	
303 303
          if (na >= int(order_list.size())) {
304 304
            rn = na;
305 305
          } else if (nb >= int(order_list.size())) {
306 306
            rn = nb;
307 307
          }
308 308

	
309 309
          if (rn == -1) {
310 310
            int nn;
311 311

	
312 312
            nn = da ? node_data[na].prev : node_data[na].next;
313 313
            da = node_data[nn].prev != na;
314 314
            na = nn;
315 315

	
316 316
            nn = db ? node_data[nb].prev : node_data[nb].next;
317 317
            db = node_data[nn].prev != nb;
318 318
            nb = nn;
319 319

	
320 320
          } else {
321 321

	
322 322
            Node rep = order_list[rn - order_list.size()];
323 323
            Node parent = _graph.source(pred_map[rep]);
324 324

	
325 325
            if (low_map[rep] < rorder) {
326 326
              merge_roots[parent].push_back(rn);
327 327
            } else {
328 328
              merge_roots[parent].push_front(rn);
329 329
            }
330 330

	
331 331
            if (parent != root) {
332 332
              na = nb = order_map[parent];
333 333
              da = true; db = false;
334 334
            } else {
335 335
              break;
336 336
            }
337 337
          }
338 338
        }
339 339
      }
340 340

	
341 341
      void walkDown(int rn, int rorder, NodeData& node_data,
342 342
                    OrderList& order_list, ChildLists& child_lists,
343 343
                    AncestorMap& ancestor_map, LowMap& low_map,
344 344
                    EmbedArc& embed_arc, MergeRoots& merge_roots) {
345 345

	
346 346
        std::vector<std::pair<int, bool> > merge_stack;
347 347

	
348 348
        for (int di = 0; di < 2; ++di) {
349 349
          bool rd = di == 0;
350 350
          int pn = rn;
351 351
          int n = rd ? node_data[rn].next : node_data[rn].prev;
352 352

	
353 353
          while (n != rn) {
354 354

	
355 355
            Node node = order_list[n];
356 356

	
357 357
            if (embed_arc[node]) {
358 358

	
359 359
              // Merging components on the critical path
360 360
              while (!merge_stack.empty()) {
361 361

	
362 362
                // Component root
363 363
                int cn = merge_stack.back().first;
364 364
                bool cd = merge_stack.back().second;
365 365
                merge_stack.pop_back();
366 366

	
367 367
                // Parent of component
368 368
                int dn = merge_stack.back().first;
369 369
                bool dd = merge_stack.back().second;
370 370
                merge_stack.pop_back();
371 371

	
372 372
                Node parent = order_list[dn];
373 373

	
374 374
                // Erasing from merge_roots
375 375
                merge_roots[parent].pop_front();
376 376

	
377 377
                Node child = order_list[cn - order_list.size()];
378 378

	
379 379
                // Erasing from child_lists
380 380
                if (child_lists[child].prev != INVALID) {
381 381
                  child_lists[child_lists[child].prev].next =
382 382
                    child_lists[child].next;
383 383
                } else {
384 384
                  child_lists[parent].first = child_lists[child].next;
385 385
                }
386 386

	
387 387
                if (child_lists[child].next != INVALID) {
388 388
                  child_lists[child_lists[child].next].prev =
389 389
                    child_lists[child].prev;
390 390
                }
391 391

	
392 392
                // Merging external faces
393 393
                {
394 394
                  int en = cn;
395 395
                  cn = cd ? node_data[cn].prev : node_data[cn].next;
396 396
                  cd = node_data[cn].next == en;
397 397

	
398 398
                }
399 399

	
400 400
                if (cd) node_data[cn].next = dn; else node_data[cn].prev = dn;
401 401
                if (dd) node_data[dn].prev = cn; else node_data[dn].next = cn;
402 402

	
403 403
              }
404 404

	
405 405
              bool d = pn == node_data[n].prev;
406 406

	
407 407
              if (node_data[n].prev == node_data[n].next &&
408 408
                  node_data[n].inverted) {
409 409
                d = !d;
410 410
              }
411 411

	
412 412
              // Embedding arc into external face
413 413
              if (rd) node_data[rn].next = n; else node_data[rn].prev = n;
414 414
              if (d) node_data[n].prev = rn; else node_data[n].next = rn;
415 415
              pn = rn;
416 416

	
417 417
              embed_arc[order_list[n]] = false;
418 418
            }
419 419

	
420 420
            if (!merge_roots[node].empty()) {
421 421

	
422 422
              bool d = pn == node_data[n].prev;
423 423

	
424 424
              merge_stack.push_back(std::make_pair(n, d));
425 425

	
426 426
              int rn = merge_roots[node].front();
427 427

	
428 428
              int xn = node_data[rn].next;
429 429
              Node xnode = order_list[xn];
430 430

	
431 431
              int yn = node_data[rn].prev;
432 432
              Node ynode = order_list[yn];
433 433

	
434 434
              bool rd;
435 435
              if (!external(xnode, rorder, child_lists, 
436 436
                            ancestor_map, low_map)) {
437 437
                rd = true;
438 438
              } else if (!external(ynode, rorder, child_lists,
439 439
                                   ancestor_map, low_map)) {
440 440
                rd = false;
441 441
              } else if (pertinent(xnode, embed_arc, merge_roots)) {
442 442
                rd = true;
443 443
              } else {
444 444
                rd = false;
445 445
              }
446 446

	
447 447
              merge_stack.push_back(std::make_pair(rn, rd));
448 448

	
449 449
              pn = rn;
450 450
              n = rd ? xn : yn;
451 451

	
452 452
            } else if (!external(node, rorder, child_lists,
453 453
                                 ancestor_map, low_map)) {
454 454
              int nn = (node_data[n].next != pn ?
455 455
                        node_data[n].next : node_data[n].prev);
456 456

	
457 457
              bool nd = n == node_data[nn].prev;
458 458

	
459 459
              if (nd) node_data[nn].prev = pn;
460 460
              else node_data[nn].next = pn;
461 461

	
462 462
              if (n == node_data[pn].prev) node_data[pn].prev = nn;
463 463
              else node_data[pn].next = nn;
464 464

	
465 465
              node_data[nn].inverted =
466 466
                (node_data[nn].prev == node_data[nn].next && nd != rd);
467 467

	
468 468
              n = nn;
469 469
            }
470 470
            else break;
471 471

	
472 472
          }
473 473

	
474 474
          if (!merge_stack.empty() || n == rn) {
475 475
            break;
476 476
          }
477 477
        }
478 478
      }
479 479

	
480 480
      void initFace(const Node& node, NodeData& node_data,
481 481
                    const OrderMap& order_map, const OrderList& order_list) {
482 482
        int n = order_map[node];
483 483
        int rn = n + order_list.size();
484 484

	
485 485
        node_data[n].next = node_data[n].prev = rn;
486 486
        node_data[rn].next = node_data[rn].prev = n;
487 487

	
488 488
        node_data[n].visited = order_list.size();
489 489
        node_data[rn].visited = order_list.size();
490 490

	
491 491
      }
492 492

	
493 493
      bool external(const Node& node, int rorder,
494 494
                    ChildLists& child_lists, AncestorMap& ancestor_map,
495 495
                    LowMap& low_map) {
496 496
        Node child = child_lists[node].first;
497 497

	
498 498
        if (child != INVALID) {
499 499
          if (low_map[child] < rorder) return true;
500 500
        }
501 501

	
502 502
        if (ancestor_map[node] < rorder) return true;
503 503

	
504 504
        return false;
505 505
      }
506 506

	
507 507
      bool pertinent(const Node& node, const EmbedArc& embed_arc,
508 508
                     const MergeRoots& merge_roots) {
509 509
        return !merge_roots[node].empty() || embed_arc[node];
510 510
      }
511 511

	
512 512
    };
513 513

	
514 514
  }
515 515

	
516 516
  /// \ingroup planar
517 517
  ///
518 518
  /// \brief Planarity checking of an undirected simple graph
519 519
  ///
520 520
  /// This function implements the Boyer-Myrvold algorithm for
521
  /// planarity checking of an undirected graph. It is a simplified
521
  /// planarity checking of an undirected simple graph. It is a simplified
522 522
  /// version of the PlanarEmbedding algorithm class because neither
523
  /// the embedding nor the kuratowski subdivisons are not computed.
523
  /// the embedding nor the Kuratowski subdivisons are computed.
524 524
  template <typename GR>
525 525
  bool checkPlanarity(const GR& graph) {
526 526
    _planarity_bits::PlanarityChecking<GR> pc(graph);
527 527
    return pc.run();
528 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
  /// embedding of an undirected graph. The planar embedding is an
535
  /// embedding of an undirected simple 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
  /// such ordering then the graph contains a \f$ K_5 \f$ (full graph
539
  /// with 5 nodes) or a \f$ K_{3,3} \f$ (complete bipartite graph on
540
  /// 3 ANode and 3 BNode) subdivision.
538
  /// such ordering then the graph contains a K<sub>5</sub> (full graph
539
  /// with 5 nodes) or a K<sub>3,3</sub> (complete bipartite graph on
540
  /// 3 Red and 3 Blue nodes) subdivision.
541 541
  ///
542 542
  /// The current implementation calculates either an embedding or a
543
  /// Kuratowski subdivision. The running time of the algorithm is 
544
  /// \f$ O(n) \f$.
543
  /// Kuratowski subdivision. The running time of the algorithm is O(n).
544
  ///
545
  /// \see PlanarDrawing, checkPlanarity()
545 546
  template <typename Graph>
546 547
  class PlanarEmbedding {
547 548
  private:
548 549

	
549 550
    TEMPLATE_GRAPH_TYPEDEFS(Graph);
550 551

	
551 552
    const Graph& _graph;
552 553
    typename Graph::template ArcMap<Arc> _embedding;
553 554

	
554 555
    typename Graph::template EdgeMap<bool> _kuratowski;
555 556

	
556 557
  private:
557 558

	
558 559
    typedef typename Graph::template NodeMap<Arc> PredMap;
559 560

	
560 561
    typedef typename Graph::template EdgeMap<bool> TreeMap;
561 562

	
562 563
    typedef typename Graph::template NodeMap<int> OrderMap;
563 564
    typedef std::vector<Node> OrderList;
564 565

	
565 566
    typedef typename Graph::template NodeMap<int> LowMap;
566 567
    typedef typename Graph::template NodeMap<int> AncestorMap;
567 568

	
568 569
    typedef _planarity_bits::NodeDataNode<Graph> NodeDataNode;
569 570
    typedef std::vector<NodeDataNode> NodeData;
570 571

	
571 572
    typedef _planarity_bits::ChildListNode<Graph> ChildListNode;
572 573
    typedef typename Graph::template NodeMap<ChildListNode> ChildLists;
573 574

	
574 575
    typedef typename Graph::template NodeMap<std::list<int> > MergeRoots;
575 576

	
576 577
    typedef typename Graph::template NodeMap<Arc> EmbedArc;
577 578

	
578 579
    typedef _planarity_bits::ArcListNode<Graph> ArcListNode;
579 580
    typedef typename Graph::template ArcMap<ArcListNode> ArcLists;
580 581

	
581 582
    typedef typename Graph::template NodeMap<bool> FlipMap;
582 583

	
583 584
    typedef typename Graph::template NodeMap<int> TypeMap;
584 585

	
585 586
    enum IsolatorNodeType {
586 587
      HIGHX = 6, LOWX = 7,
587 588
      HIGHY = 8, LOWY = 9,
588 589
      ROOT = 10, PERTINENT = 11,
589 590
      INTERNAL = 12
590 591
    };
591 592

	
592 593
  public:
593 594

	
594
    /// \brief The map for store of embedding
595
    /// \brief The map type for storing the embedding
596
    ///
597
    /// The map type for storing the embedding.
598
    /// \see embeddingMap()
595 599
    typedef typename Graph::template ArcMap<Arc> EmbeddingMap;
596 600

	
597 601
    /// \brief Constructor
598 602
    ///
599
    /// \note The graph should be simple, i.e. parallel and loop arc
600
    /// free.
603
    /// Constructor.
604
    /// \pre The graph must be simple, i.e. it should not
605
    /// contain parallel or loop arcs.
601 606
    PlanarEmbedding(const Graph& graph)
602 607
      : _graph(graph), _embedding(_graph), _kuratowski(graph, false) {}
603 608

	
604
    /// \brief Runs the algorithm.
609
    /// \brief Run the algorithm.
605 610
    ///
606
    /// Runs the algorithm.
607
    /// \param kuratowski If the parameter is false, then the
611
    /// This function runs the algorithm.
612
    /// \param kuratowski If this parameter is set to \c false, then the
608 613
    /// algorithm does not compute a Kuratowski subdivision.
609
    ///\return %True when the graph is planar.
614
    /// \return \c true if the graph is planar.
610 615
    bool run(bool kuratowski = true) {
611 616
      typedef _planarity_bits::PlanarityVisitor<Graph> Visitor;
612 617

	
613 618
      PredMap pred_map(_graph, INVALID);
614 619
      TreeMap tree_map(_graph, false);
615 620

	
616 621
      OrderMap order_map(_graph, -1);
617 622
      OrderList order_list;
618 623

	
619 624
      AncestorMap ancestor_map(_graph, -1);
620 625
      LowMap low_map(_graph, -1);
621 626

	
622 627
      Visitor visitor(_graph, pred_map, tree_map,
623 628
                      order_map, order_list, ancestor_map, low_map);
624 629
      DfsVisit<Graph, Visitor> visit(_graph, visitor);
625 630
      visit.run();
626 631

	
627 632
      ChildLists child_lists(_graph);
628 633
      createChildLists(tree_map, order_map, low_map, child_lists);
629 634

	
630 635
      NodeData node_data(2 * order_list.size());
631 636

	
632 637
      EmbedArc embed_arc(_graph, INVALID);
633 638

	
634 639
      MergeRoots merge_roots(_graph);
635 640

	
636 641
      ArcLists arc_lists(_graph);
637 642

	
638 643
      FlipMap flip_map(_graph, false);
639 644

	
640 645
      for (int i = order_list.size() - 1; i >= 0; --i) {
641 646

	
642 647
        Node node = order_list[i];
643 648

	
644 649
        node_data[i].first = INVALID;
645 650

	
646 651
        Node source = node;
647 652
        for (OutArcIt e(_graph, node); e != INVALID; ++e) {
648 653
          Node target = _graph.target(e);
649 654

	
650 655
          if (order_map[source] < order_map[target] && tree_map[e]) {
651 656
            initFace(target, arc_lists, node_data,
652 657
                     pred_map, order_map, order_list);
653 658
          }
654 659
        }
655 660

	
656 661
        for (OutArcIt e(_graph, node); e != INVALID; ++e) {
657 662
          Node target = _graph.target(e);
658 663

	
659 664
          if (order_map[source] < order_map[target] && !tree_map[e]) {
660 665
            embed_arc[target] = e;
661 666
            walkUp(target, source, i, pred_map, low_map,
662 667
                   order_map, order_list, node_data, merge_roots);
663 668
          }
664 669
        }
665 670

	
666 671
        for (typename MergeRoots::Value::iterator it =
667 672
               merge_roots[node].begin(); it != merge_roots[node].end(); ++it) {
668 673
          int rn = *it;
669 674
          walkDown(rn, i, node_data, arc_lists, flip_map, order_list,
670 675
                   child_lists, ancestor_map, low_map, embed_arc, merge_roots);
671 676
        }
672 677
        merge_roots[node].clear();
673 678

	
674 679
        for (OutArcIt e(_graph, node); e != INVALID; ++e) {
675 680
          Node target = _graph.target(e);
676 681

	
677 682
          if (order_map[source] < order_map[target] && !tree_map[e]) {
678 683
            if (embed_arc[target] != INVALID) {
679 684
              if (kuratowski) {
680 685
                isolateKuratowski(e, node_data, arc_lists, flip_map,
681 686
                                  order_map, order_list, pred_map, child_lists,
682 687
                                  ancestor_map, low_map,
683 688
                                  embed_arc, merge_roots);
684 689
              }
685 690
              return false;
686 691
            }
687 692
          }
688 693
        }
689 694
      }
690 695

	
691 696
      for (int i = 0; i < int(order_list.size()); ++i) {
692 697

	
693 698
        mergeRemainingFaces(order_list[i], node_data, order_list, order_map,
694 699
                            child_lists, arc_lists);
695 700
        storeEmbedding(order_list[i], node_data, order_map, pred_map,
696 701
                       arc_lists, flip_map);
697 702
      }
698 703

	
699 704
      return true;
700 705
    }
701 706

	
702
    /// \brief Gives back the successor of an arc
707
    /// \brief Give back the successor of an arc
703 708
    ///
704
    /// Gives back the successor of an arc. This function makes
709
    /// This function gives back the successor of an arc. It makes
705 710
    /// possible to query the cyclic order of the outgoing arcs from
706 711
    /// a node.
707 712
    Arc next(const Arc& arc) const {
708 713
      return _embedding[arc];
709 714
    }
710 715

	
711
    /// \brief Gives back the calculated embedding map
716
    /// \brief Give back the calculated embedding map
712 717
    ///
713
    /// The returned map contains the successor of each arc in the
714
    /// graph.
718
    /// This function gives back the calculated embedding map, which
719
    /// contains the successor of each arc in the cyclic order of the
720
    /// outgoing arcs of its source node.
715 721
    const EmbeddingMap& embeddingMap() const {
716 722
      return _embedding;
717 723
    }
718 724

	
719
    /// \brief Gives back true if the undirected arc is in the
720
    /// kuratowski subdivision
725
    /// \brief Give back \c true if the given edge is in the Kuratowski
726
    /// subdivision
721 727
    ///
722
    /// Gives back true if the undirected arc is in the kuratowski
723
    /// subdivision
724
    /// \note The \c run() had to be called with true value.
725
    bool kuratowski(const Edge& edge) {
728
    /// This function gives back \c true if the given edge is in the found
729
    /// Kuratowski subdivision.
730
    /// \pre The \c run() function must be called with \c true parameter
731
    /// before using this function.
732
    bool kuratowski(const Edge& edge) const {
726 733
      return _kuratowski[edge];
727 734
    }
728 735

	
729 736
  private:
730 737

	
731 738
    void createChildLists(const TreeMap& tree_map, const OrderMap& order_map,
732 739
                          const LowMap& low_map, ChildLists& child_lists) {
733 740

	
734 741
      for (NodeIt n(_graph); n != INVALID; ++n) {
735 742
        Node source = n;
736 743

	
737 744
        std::vector<Node> targets;
738 745
        for (OutArcIt e(_graph, n); e != INVALID; ++e) {
739 746
          Node target = _graph.target(e);
740 747

	
741 748
          if (order_map[source] < order_map[target] && tree_map[e]) {
742 749
            targets.push_back(target);
743 750
          }
744 751
        }
745 752

	
746 753
        if (targets.size() == 0) {
747 754
          child_lists[source].first = INVALID;
748 755
        } else if (targets.size() == 1) {
749 756
          child_lists[source].first = targets[0];
750 757
          child_lists[targets[0]].prev = INVALID;
751 758
          child_lists[targets[0]].next = INVALID;
752 759
        } else {
753 760
          radixSort(targets.begin(), targets.end(), mapToFunctor(low_map));
754 761
          for (int i = 1; i < int(targets.size()); ++i) {
755 762
            child_lists[targets[i]].prev = targets[i - 1];
756 763
            child_lists[targets[i - 1]].next = targets[i];
757 764
          }
758 765
          child_lists[targets.back()].next = INVALID;
759 766
          child_lists[targets.front()].prev = INVALID;
760 767
          child_lists[source].first = targets.front();
761 768
        }
762 769
      }
763 770
    }
764 771

	
765 772
    void walkUp(const Node& node, Node root, int rorder,
766 773
                const PredMap& pred_map, const LowMap& low_map,
767 774
                const OrderMap& order_map, const OrderList& order_list,
768 775
                NodeData& node_data, MergeRoots& merge_roots) {
769 776

	
770 777
      int na, nb;
771 778
      bool da, db;
772 779

	
773 780
      na = nb = order_map[node];
774 781
      da = true; db = false;
775 782

	
776 783
      while (true) {
777 784

	
778 785
        if (node_data[na].visited == rorder) break;
779 786
        if (node_data[nb].visited == rorder) break;
780 787

	
781 788
        node_data[na].visited = rorder;
782 789
        node_data[nb].visited = rorder;
783 790

	
784 791
        int rn = -1;
785 792

	
786 793
        if (na >= int(order_list.size())) {
787 794
          rn = na;
788 795
        } else if (nb >= int(order_list.size())) {
789 796
          rn = nb;
790 797
        }
791 798

	
792 799
        if (rn == -1) {
793 800
          int nn;
794 801

	
795 802
          nn = da ? node_data[na].prev : node_data[na].next;
796 803
          da = node_data[nn].prev != na;
797 804
          na = nn;
798 805

	
799 806
          nn = db ? node_data[nb].prev : node_data[nb].next;
800 807
          db = node_data[nn].prev != nb;
801 808
          nb = nn;
802 809

	
803 810
        } else {
804 811

	
805 812
          Node rep = order_list[rn - order_list.size()];
806 813
          Node parent = _graph.source(pred_map[rep]);
807 814

	
808 815
          if (low_map[rep] < rorder) {
809 816
            merge_roots[parent].push_back(rn);
810 817
          } else {
811 818
            merge_roots[parent].push_front(rn);
812 819
          }
813 820

	
814 821
          if (parent != root) {
815 822
            na = nb = order_map[parent];
816 823
            da = true; db = false;
817 824
          } else {
818 825
            break;
819 826
          }
820 827
        }
821 828
      }
822 829
    }
823 830

	
824 831
    void walkDown(int rn, int rorder, NodeData& node_data,
825 832
                  ArcLists& arc_lists, FlipMap& flip_map,
826 833
                  OrderList& order_list, ChildLists& child_lists,
827 834
                  AncestorMap& ancestor_map, LowMap& low_map,
828 835
                  EmbedArc& embed_arc, MergeRoots& merge_roots) {
829 836

	
830 837
      std::vector<std::pair<int, bool> > merge_stack;
831 838

	
832 839
      for (int di = 0; di < 2; ++di) {
833 840
        bool rd = di == 0;
834 841
        int pn = rn;
835 842
        int n = rd ? node_data[rn].next : node_data[rn].prev;
836 843

	
837 844
        while (n != rn) {
838 845

	
839 846
          Node node = order_list[n];
840 847

	
841 848
          if (embed_arc[node] != INVALID) {
842 849

	
843 850
            // Merging components on the critical path
844 851
            while (!merge_stack.empty()) {
845 852

	
846 853
              // Component root
847 854
              int cn = merge_stack.back().first;
848 855
              bool cd = merge_stack.back().second;
849 856
              merge_stack.pop_back();
850 857

	
851 858
              // Parent of component
852 859
              int dn = merge_stack.back().first;
853 860
              bool dd = merge_stack.back().second;
854 861
              merge_stack.pop_back();
855 862

	
856 863
              Node parent = order_list[dn];
857 864

	
858 865
              // Erasing from merge_roots
859 866
              merge_roots[parent].pop_front();
860 867

	
861 868
              Node child = order_list[cn - order_list.size()];
862 869

	
863 870
              // Erasing from child_lists
864 871
              if (child_lists[child].prev != INVALID) {
865 872
                child_lists[child_lists[child].prev].next =
866 873
                  child_lists[child].next;
867 874
              } else {
868 875
                child_lists[parent].first = child_lists[child].next;
869 876
              }
870 877

	
871 878
              if (child_lists[child].next != INVALID) {
872 879
                child_lists[child_lists[child].next].prev =
873 880
                  child_lists[child].prev;
874 881
              }
875 882

	
876 883
              // Merging arcs + flipping
877 884
              Arc de = node_data[dn].first;
878 885
              Arc ce = node_data[cn].first;
879 886

	
880 887
              flip_map[order_list[cn - order_list.size()]] = cd != dd;
881 888
              if (cd != dd) {
882 889
                std::swap(arc_lists[ce].prev, arc_lists[ce].next);
883 890
                ce = arc_lists[ce].prev;
884 891
                std::swap(arc_lists[ce].prev, arc_lists[ce].next);
885 892
              }
886 893

	
887 894
              {
888 895
                Arc dne = arc_lists[de].next;
889 896
                Arc cne = arc_lists[ce].next;
890 897

	
891 898
                arc_lists[de].next = cne;
892 899
                arc_lists[ce].next = dne;
893 900

	
894 901
                arc_lists[dne].prev = ce;
895 902
                arc_lists[cne].prev = de;
896 903
              }
897 904

	
898 905
              if (dd) {
899 906
                node_data[dn].first = ce;
900 907
              }
901 908

	
902 909
              // Merging external faces
903 910
              {
904 911
                int en = cn;
905 912
                cn = cd ? node_data[cn].prev : node_data[cn].next;
906 913
                cd = node_data[cn].next == en;
907 914

	
908 915
                 if (node_data[cn].prev == node_data[cn].next &&
909 916
                    node_data[cn].inverted) {
910 917
                   cd = !cd;
911 918
                 }
912 919
              }
913 920

	
914 921
              if (cd) node_data[cn].next = dn; else node_data[cn].prev = dn;
915 922
              if (dd) node_data[dn].prev = cn; else node_data[dn].next = cn;
916 923

	
917 924
            }
918 925

	
919 926
            bool d = pn == node_data[n].prev;
920 927

	
921 928
            if (node_data[n].prev == node_data[n].next &&
922 929
                node_data[n].inverted) {
923 930
              d = !d;
924 931
            }
925 932

	
926 933
            // Add new arc
927 934
            {
928 935
              Arc arc = embed_arc[node];
929 936
              Arc re = node_data[rn].first;
930 937

	
931 938
              arc_lists[arc_lists[re].next].prev = arc;
932 939
              arc_lists[arc].next = arc_lists[re].next;
933 940
              arc_lists[arc].prev = re;
934 941
              arc_lists[re].next = arc;
935 942

	
936 943
              if (!rd) {
937 944
                node_data[rn].first = arc;
938 945
              }
939 946

	
940 947
              Arc rev = _graph.oppositeArc(arc);
941 948
              Arc e = node_data[n].first;
942 949

	
943 950
              arc_lists[arc_lists[e].next].prev = rev;
944 951
              arc_lists[rev].next = arc_lists[e].next;
945 952
              arc_lists[rev].prev = e;
946 953
              arc_lists[e].next = rev;
947 954

	
948 955
              if (d) {
949 956
                node_data[n].first = rev;
950 957
              }
951 958

	
952 959
            }
953 960

	
954 961
            // Embedding arc into external face
955 962
            if (rd) node_data[rn].next = n; else node_data[rn].prev = n;
956 963
            if (d) node_data[n].prev = rn; else node_data[n].next = rn;
957 964
            pn = rn;
958 965

	
959 966
            embed_arc[order_list[n]] = INVALID;
960 967
          }
961 968

	
962 969
          if (!merge_roots[node].empty()) {
963 970

	
964 971
            bool d = pn == node_data[n].prev;
965 972
            if (node_data[n].prev == node_data[n].next &&
966 973
                node_data[n].inverted) {
967 974
              d = !d;
968 975
            }
969 976

	
970 977
            merge_stack.push_back(std::make_pair(n, d));
971 978

	
972 979
            int rn = merge_roots[node].front();
973 980

	
974 981
            int xn = node_data[rn].next;
975 982
            Node xnode = order_list[xn];
976 983

	
977 984
            int yn = node_data[rn].prev;
978 985
            Node ynode = order_list[yn];
979 986

	
980 987
            bool rd;
981 988
            if (!external(xnode, rorder, child_lists, ancestor_map, low_map)) {
982 989
              rd = true;
983 990
            } else if (!external(ynode, rorder, child_lists,
984 991
                                 ancestor_map, low_map)) {
985 992
              rd = false;
986 993
            } else if (pertinent(xnode, embed_arc, merge_roots)) {
987 994
              rd = true;
988 995
            } else {
989 996
              rd = false;
990 997
            }
991 998

	
992 999
            merge_stack.push_back(std::make_pair(rn, rd));
993 1000

	
994 1001
            pn = rn;
995 1002
            n = rd ? xn : yn;
996 1003

	
997 1004
          } else if (!external(node, rorder, child_lists,
998 1005
                               ancestor_map, low_map)) {
999 1006
            int nn = (node_data[n].next != pn ?
1000 1007
                      node_data[n].next : node_data[n].prev);
1001 1008

	
1002 1009
            bool nd = n == node_data[nn].prev;
1003 1010

	
1004 1011
            if (nd) node_data[nn].prev = pn;
1005 1012
            else node_data[nn].next = pn;
1006 1013

	
1007 1014
            if (n == node_data[pn].prev) node_data[pn].prev = nn;
1008 1015
            else node_data[pn].next = nn;
1009 1016

	
1010 1017
            node_data[nn].inverted =
1011 1018
              (node_data[nn].prev == node_data[nn].next && nd != rd);
1012 1019

	
1013 1020
            n = nn;
1014 1021
          }
1015 1022
          else break;
1016 1023

	
1017 1024
        }
1018 1025

	
1019 1026
        if (!merge_stack.empty() || n == rn) {
1020 1027
          break;
1021 1028
        }
1022 1029
      }
1023 1030
    }
1024 1031

	
1025 1032
    void initFace(const Node& node, ArcLists& arc_lists,
1026 1033
                  NodeData& node_data, const PredMap& pred_map,
1027 1034
                  const OrderMap& order_map, const OrderList& order_list) {
1028 1035
      int n = order_map[node];
1029 1036
      int rn = n + order_list.size();
1030 1037

	
1031 1038
      node_data[n].next = node_data[n].prev = rn;
1032 1039
      node_data[rn].next = node_data[rn].prev = n;
1033 1040

	
1034 1041
      node_data[n].visited = order_list.size();
1035 1042
      node_data[rn].visited = order_list.size();
1036 1043

	
1037 1044
      node_data[n].inverted = false;
1038 1045
      node_data[rn].inverted = false;
1039 1046

	
1040 1047
      Arc arc = pred_map[node];
1041 1048
      Arc rev = _graph.oppositeArc(arc);
1042 1049

	
1043 1050
      node_data[rn].first = arc;
1044 1051
      node_data[n].first = rev;
1045 1052

	
1046 1053
      arc_lists[arc].prev = arc;
1047 1054
      arc_lists[arc].next = arc;
1048 1055

	
1049 1056
      arc_lists[rev].prev = rev;
1050 1057
      arc_lists[rev].next = rev;
1051 1058

	
1052 1059
    }
1053 1060

	
1054 1061
    void mergeRemainingFaces(const Node& node, NodeData& node_data,
1055 1062
                             OrderList& order_list, OrderMap& order_map,
1056 1063
                             ChildLists& child_lists, ArcLists& arc_lists) {
1057 1064
      while (child_lists[node].first != INVALID) {
1058 1065
        int dd = order_map[node];
1059 1066
        Node child = child_lists[node].first;
1060 1067
        int cd = order_map[child] + order_list.size();
1061 1068
        child_lists[node].first = child_lists[child].next;
1062 1069

	
1063 1070
        Arc de = node_data[dd].first;
1064 1071
        Arc ce = node_data[cd].first;
1065 1072

	
1066 1073
        if (de != INVALID) {
1067 1074
          Arc dne = arc_lists[de].next;
1068 1075
          Arc cne = arc_lists[ce].next;
1069 1076

	
1070 1077
          arc_lists[de].next = cne;
1071 1078
          arc_lists[ce].next = dne;
1072 1079

	
1073 1080
          arc_lists[dne].prev = ce;
1074 1081
          arc_lists[cne].prev = de;
1075 1082
        }
1076 1083

	
1077 1084
        node_data[dd].first = ce;
1078 1085

	
1079 1086
      }
1080 1087
    }
1081 1088

	
1082 1089
    void storeEmbedding(const Node& node, NodeData& node_data,
1083 1090
                        OrderMap& order_map, PredMap& pred_map,
1084 1091
                        ArcLists& arc_lists, FlipMap& flip_map) {
1085 1092

	
1086 1093
      if (node_data[order_map[node]].first == INVALID) return;
1087 1094

	
1088 1095
      if (pred_map[node] != INVALID) {
1089 1096
        Node source = _graph.source(pred_map[node]);
1090 1097
        flip_map[node] = flip_map[node] != flip_map[source];
1091 1098
      }
1092 1099

	
1093 1100
      Arc first = node_data[order_map[node]].first;
1094 1101
      Arc prev = first;
1095 1102

	
1096 1103
      Arc arc = flip_map[node] ?
1097 1104
        arc_lists[prev].prev : arc_lists[prev].next;
1098 1105

	
1099 1106
      _embedding[prev] = arc;
1100 1107

	
1101 1108
      while (arc != first) {
1102 1109
        Arc next = arc_lists[arc].prev == prev ?
1103 1110
          arc_lists[arc].next : arc_lists[arc].prev;
1104 1111
        prev = arc; arc = next;
1105 1112
        _embedding[prev] = arc;
1106 1113
      }
1107 1114
    }
1108 1115

	
1109 1116

	
1110 1117
    bool external(const Node& node, int rorder,
1111 1118
                  ChildLists& child_lists, AncestorMap& ancestor_map,
1112 1119
                  LowMap& low_map) {
1113 1120
      Node child = child_lists[node].first;
1114 1121

	
1115 1122
      if (child != INVALID) {
1116 1123
        if (low_map[child] < rorder) return true;
1117 1124
      }
1118 1125

	
1119 1126
      if (ancestor_map[node] < rorder) return true;
1120 1127

	
1121 1128
      return false;
1122 1129
    }
1123 1130

	
1124 1131
    bool pertinent(const Node& node, const EmbedArc& embed_arc,
1125 1132
                   const MergeRoots& merge_roots) {
1126 1133
      return !merge_roots[node].empty() || embed_arc[node] != INVALID;
1127 1134
    }
1128 1135

	
1129 1136
    int lowPoint(const Node& node, OrderMap& order_map, ChildLists& child_lists,
1130 1137
                 AncestorMap& ancestor_map, LowMap& low_map) {
1131 1138
      int low_point;
1132 1139

	
1133 1140
      Node child = child_lists[node].first;
1134 1141

	
1135 1142
      if (child != INVALID) {
1136 1143
        low_point = low_map[child];
1137 1144
      } else {
1138 1145
        low_point = order_map[node];
1139 1146
      }
1140 1147

	
1141 1148
      if (low_point > ancestor_map[node]) {
1142 1149
        low_point = ancestor_map[node];
1143 1150
      }
1144 1151

	
1145 1152
      return low_point;
1146 1153
    }
1147 1154

	
1148 1155
    int findComponentRoot(Node root, Node node, ChildLists& child_lists,
1149 1156
                          OrderMap& order_map, OrderList& order_list) {
1150 1157

	
1151 1158
      int order = order_map[root];
1152 1159
      int norder = order_map[node];
1153 1160

	
1154 1161
      Node child = child_lists[root].first;
1155 1162
      while (child != INVALID) {
1156 1163
        int corder = order_map[child];
1157 1164
        if (corder > order && corder < norder) {
1158 1165
          order = corder;
1159 1166
        }
1160 1167
        child = child_lists[child].next;
1161 1168
      }
1162 1169
      return order + order_list.size();
1163 1170
    }
1164 1171

	
1165 1172
    Node findPertinent(Node node, OrderMap& order_map, NodeData& node_data,
1166 1173
                       EmbedArc& embed_arc, MergeRoots& merge_roots) {
1167 1174
      Node wnode =_graph.target(node_data[order_map[node]].first);
1168 1175
      while (!pertinent(wnode, embed_arc, merge_roots)) {
1169 1176
        wnode = _graph.target(node_data[order_map[wnode]].first);
1170 1177
      }
1171 1178
      return wnode;
1172 1179
    }
1173 1180

	
1174 1181

	
1175 1182
    Node findExternal(Node node, int rorder, OrderMap& order_map,
1176 1183
                      ChildLists& child_lists, AncestorMap& ancestor_map,
1177 1184
                      LowMap& low_map, NodeData& node_data) {
1178 1185
      Node wnode =_graph.target(node_data[order_map[node]].first);
1179 1186
      while (!external(wnode, rorder, child_lists, ancestor_map, low_map)) {
1180 1187
        wnode = _graph.target(node_data[order_map[wnode]].first);
1181 1188
      }
1182 1189
      return wnode;
1183 1190
    }
1184 1191

	
1185 1192
    void markCommonPath(Node node, int rorder, Node& wnode, Node& znode,
1186 1193
                        OrderList& order_list, OrderMap& order_map,
1187 1194
                        NodeData& node_data, ArcLists& arc_lists,
1188 1195
                        EmbedArc& embed_arc, MergeRoots& merge_roots,
1189 1196
                        ChildLists& child_lists, AncestorMap& ancestor_map,
1190 1197
                        LowMap& low_map) {
1191 1198

	
1192 1199
      Node cnode = node;
1193 1200
      Node pred = INVALID;
1194 1201

	
1195 1202
      while (true) {
1196 1203

	
1197 1204
        bool pert = pertinent(cnode, embed_arc, merge_roots);
1198 1205
        bool ext = external(cnode, rorder, child_lists, ancestor_map, low_map);
1199 1206

	
1200 1207
        if (pert && ext) {
1201 1208
          if (!merge_roots[cnode].empty()) {
1202 1209
            int cn = merge_roots[cnode].back();
1203 1210

	
1204 1211
            if (low_map[order_list[cn - order_list.size()]] < rorder) {
1205 1212
              Arc arc = node_data[cn].first;
1206 1213
              _kuratowski.set(arc, true);
1207 1214

	
1208 1215
              pred = cnode;
1209 1216
              cnode = _graph.target(arc);
1210 1217

	
1211 1218
              continue;
1212 1219
            }
1213 1220
          }
1214 1221
          wnode = znode = cnode;
1215 1222
          return;
1216 1223

	
1217 1224
        } else if (pert) {
1218 1225
          wnode = cnode;
1219 1226

	
1220 1227
          while (!external(cnode, rorder, child_lists, ancestor_map, low_map)) {
1221 1228
            Arc arc = node_data[order_map[cnode]].first;
1222 1229

	
1223 1230
            if (_graph.target(arc) == pred) {
1224 1231
              arc = arc_lists[arc].next;
1225 1232
            }
1226 1233
            _kuratowski.set(arc, true);
1227 1234

	
1228 1235
            Node next = _graph.target(arc);
1229 1236
            pred = cnode; cnode = next;
1230 1237
          }
1231 1238

	
1232 1239
          znode = cnode;
1233 1240
          return;
1234 1241

	
1235 1242
        } else if (ext) {
1236 1243
          znode = cnode;
1237 1244

	
1238 1245
          while (!pertinent(cnode, embed_arc, merge_roots)) {
1239 1246
            Arc arc = node_data[order_map[cnode]].first;
1240 1247

	
1241 1248
            if (_graph.target(arc) == pred) {
1242 1249
              arc = arc_lists[arc].next;
1243 1250
            }
1244 1251
            _kuratowski.set(arc, true);
1245 1252

	
1246 1253
            Node next = _graph.target(arc);
1247 1254
            pred = cnode; cnode = next;
1248 1255
          }
1249 1256

	
1250 1257
          wnode = cnode;
1251 1258
          return;
1252 1259

	
1253 1260
        } else {
1254 1261
          Arc arc = node_data[order_map[cnode]].first;
1255 1262

	
1256 1263
          if (_graph.target(arc) == pred) {
1257 1264
            arc = arc_lists[arc].next;
1258 1265
          }
1259 1266
          _kuratowski.set(arc, true);
1260 1267

	
1261 1268
          Node next = _graph.target(arc);
1262 1269
          pred = cnode; cnode = next;
1263 1270
        }
1264 1271

	
1265 1272
      }
1266 1273

	
1267 1274
    }
1268 1275

	
1269 1276
    void orientComponent(Node root, int rn, OrderMap& order_map,
1270 1277
                         PredMap& pred_map, NodeData& node_data,
1271 1278
                         ArcLists& arc_lists, FlipMap& flip_map,
1272 1279
                         TypeMap& type_map) {
1273 1280
      node_data[order_map[root]].first = node_data[rn].first;
1274 1281
      type_map[root] = 1;
1275 1282

	
1276 1283
      std::vector<Node> st, qu;
1277 1284

	
1278 1285
      st.push_back(root);
1279 1286
      while (!st.empty()) {
1280 1287
        Node node = st.back();
1281 1288
        st.pop_back();
1282 1289
        qu.push_back(node);
1283 1290

	
1284 1291
        Arc arc = node_data[order_map[node]].first;
1285 1292

	
1286 1293
        if (type_map[_graph.target(arc)] == 0) {
1287 1294
          st.push_back(_graph.target(arc));
1288 1295
          type_map[_graph.target(arc)] = 1;
1289 1296
        }
1290 1297

	
1291 1298
        Arc last = arc, pred = arc;
1292 1299
        arc = arc_lists[arc].next;
1293 1300
        while (arc != last) {
1294 1301

	
1295 1302
          if (type_map[_graph.target(arc)] == 0) {
1296 1303
            st.push_back(_graph.target(arc));
1297 1304
            type_map[_graph.target(arc)] = 1;
1298 1305
          }
1299 1306

	
1300 1307
          Arc next = arc_lists[arc].next != pred ?
1301 1308
            arc_lists[arc].next : arc_lists[arc].prev;
1302 1309
          pred = arc; arc = next;
1303 1310
        }
1304 1311

	
1305 1312
      }
1306 1313

	
1307 1314
      type_map[root] = 2;
1308 1315
      flip_map[root] = false;
1309 1316

	
1310 1317
      for (int i = 1; i < int(qu.size()); ++i) {
1311 1318

	
1312 1319
        Node node = qu[i];
1313 1320

	
1314 1321
        while (type_map[node] != 2) {
1315 1322
          st.push_back(node);
1316 1323
          type_map[node] = 2;
1317 1324
          node = _graph.source(pred_map[node]);
1318 1325
        }
1319 1326

	
1320 1327
        bool flip = flip_map[node];
1321 1328

	
1322 1329
        while (!st.empty()) {
1323 1330
          node = st.back();
1324 1331
          st.pop_back();
1325 1332

	
1326 1333
          flip_map[node] = flip != flip_map[node];
1327 1334
          flip = flip_map[node];
1328 1335

	
1329 1336
          if (flip) {
1330 1337
            Arc arc = node_data[order_map[node]].first;
1331 1338
            std::swap(arc_lists[arc].prev, arc_lists[arc].next);
1332 1339
            arc = arc_lists[arc].prev;
1333 1340
            std::swap(arc_lists[arc].prev, arc_lists[arc].next);
1334 1341
            node_data[order_map[node]].first = arc;
1335 1342
          }
1336 1343
        }
1337 1344
      }
1338 1345

	
1339 1346
      for (int i = 0; i < int(qu.size()); ++i) {
1340 1347

	
1341 1348
        Arc arc = node_data[order_map[qu[i]]].first;
1342 1349
        Arc last = arc, pred = arc;
1343 1350

	
1344 1351
        arc = arc_lists[arc].next;
1345 1352
        while (arc != last) {
1346 1353

	
1347 1354
          if (arc_lists[arc].next == pred) {
1348 1355
            std::swap(arc_lists[arc].next, arc_lists[arc].prev);
1349 1356
          }
1350 1357
          pred = arc; arc = arc_lists[arc].next;
1351 1358
        }
1352 1359

	
1353 1360
      }
1354 1361
    }
1355 1362

	
1356 1363
    void setFaceFlags(Node root, Node wnode, Node ynode, Node xnode,
1357 1364
                      OrderMap& order_map, NodeData& node_data,
1358 1365
                      TypeMap& type_map) {
1359 1366
      Node node = _graph.target(node_data[order_map[root]].first);
1360 1367

	
1361 1368
      while (node != ynode) {
1362 1369
        type_map[node] = HIGHY;
1363 1370
        node = _graph.target(node_data[order_map[node]].first);
1364 1371
      }
1365 1372

	
1366 1373
      while (node != wnode) {
1367 1374
        type_map[node] = LOWY;
1368 1375
        node = _graph.target(node_data[order_map[node]].first);
1369 1376
      }
1370 1377

	
1371 1378
      node = _graph.target(node_data[order_map[wnode]].first);
1372 1379

	
1373 1380
      while (node != xnode) {
1374 1381
        type_map[node] = LOWX;
1375 1382
        node = _graph.target(node_data[order_map[node]].first);
1376 1383
      }
1377 1384
      type_map[node] = LOWX;
1378 1385

	
1379 1386
      node = _graph.target(node_data[order_map[xnode]].first);
1380 1387
      while (node != root) {
1381 1388
        type_map[node] = HIGHX;
1382 1389
        node = _graph.target(node_data[order_map[node]].first);
1383 1390
      }
1384 1391

	
1385 1392
      type_map[wnode] = PERTINENT;
1386 1393
      type_map[root] = ROOT;
1387 1394
    }
1388 1395

	
1389 1396
    void findInternalPath(std::vector<Arc>& ipath,
1390 1397
                          Node wnode, Node root, TypeMap& type_map,
1391 1398
                          OrderMap& order_map, NodeData& node_data,
1392 1399
                          ArcLists& arc_lists) {
1393 1400
      std::vector<Arc> st;
1394 1401

	
1395 1402
      Node node = wnode;
1396 1403

	
1397 1404
      while (node != root) {
1398 1405
        Arc arc = arc_lists[node_data[order_map[node]].first].next;
1399 1406
        st.push_back(arc);
1400 1407
        node = _graph.target(arc);
1401 1408
      }
1402 1409

	
1403 1410
      while (true) {
1404 1411
        Arc arc = st.back();
1405 1412
        if (type_map[_graph.target(arc)] == LOWX ||
1406 1413
            type_map[_graph.target(arc)] == HIGHX) {
1407 1414
          break;
1408 1415
        }
1409 1416
        if (type_map[_graph.target(arc)] == 2) {
1410 1417
          type_map[_graph.target(arc)] = 3;
1411 1418

	
1412 1419
          arc = arc_lists[_graph.oppositeArc(arc)].next;
1413 1420
          st.push_back(arc);
1414 1421
        } else {
1415 1422
          st.pop_back();
1416 1423
          arc = arc_lists[arc].next;
1417 1424

	
1418 1425
          while (_graph.oppositeArc(arc) == st.back()) {
1419 1426
            arc = st.back();
1420 1427
            st.pop_back();
1421 1428
            arc = arc_lists[arc].next;
1422 1429
          }
1423 1430
          st.push_back(arc);
1424 1431
        }
1425 1432
      }
1426 1433

	
1427 1434
      for (int i = 0; i < int(st.size()); ++i) {
1428 1435
        if (type_map[_graph.target(st[i])] != LOWY &&
1429 1436
            type_map[_graph.target(st[i])] != HIGHY) {
1430 1437
          for (; i < int(st.size()); ++i) {
1431 1438
            ipath.push_back(st[i]);
1432 1439
          }
1433 1440
        }
1434 1441
      }
1435 1442
    }
1436 1443

	
1437 1444
    void setInternalFlags(std::vector<Arc>& ipath, TypeMap& type_map) {
1438 1445
      for (int i = 1; i < int(ipath.size()); ++i) {
1439 1446
        type_map[_graph.source(ipath[i])] = INTERNAL;
1440 1447
      }
1441 1448
    }
1442 1449

	
1443 1450
    void findPilePath(std::vector<Arc>& ppath,
1444 1451
                      Node root, TypeMap& type_map, OrderMap& order_map,
1445 1452
                      NodeData& node_data, ArcLists& arc_lists) {
1446 1453
      std::vector<Arc> st;
1447 1454

	
1448 1455
      st.push_back(_graph.oppositeArc(node_data[order_map[root]].first));
1449 1456
      st.push_back(node_data[order_map[root]].first);
1450 1457

	
1451 1458
      while (st.size() > 1) {
1452 1459
        Arc arc = st.back();
1453 1460
        if (type_map[_graph.target(arc)] == INTERNAL) {
1454 1461
          break;
1455 1462
        }
1456 1463
        if (type_map[_graph.target(arc)] == 3) {
1457 1464
          type_map[_graph.target(arc)] = 4;
1458 1465

	
1459 1466
          arc = arc_lists[_graph.oppositeArc(arc)].next;
1460 1467
          st.push_back(arc);
1461 1468
        } else {
1462 1469
          st.pop_back();
1463 1470
          arc = arc_lists[arc].next;
1464 1471

	
1465 1472
          while (!st.empty() && _graph.oppositeArc(arc) == st.back()) {
1466 1473
            arc = st.back();
1467 1474
            st.pop_back();
1468 1475
            arc = arc_lists[arc].next;
1469 1476
          }
1470 1477
          st.push_back(arc);
1471 1478
        }
1472 1479
      }
1473 1480

	
1474 1481
      for (int i = 1; i < int(st.size()); ++i) {
1475 1482
        ppath.push_back(st[i]);
1476 1483
      }
1477 1484
    }
1478 1485

	
1479 1486

	
1480 1487
    int markExternalPath(Node node, OrderMap& order_map,
1481 1488
                         ChildLists& child_lists, PredMap& pred_map,
1482 1489
                         AncestorMap& ancestor_map, LowMap& low_map) {
1483 1490
      int lp = lowPoint(node, order_map, child_lists,
1484 1491
                        ancestor_map, low_map);
1485 1492

	
1486 1493
      if (ancestor_map[node] != lp) {
1487 1494
        node = child_lists[node].first;
1488 1495
        _kuratowski[pred_map[node]] = true;
1489 1496

	
1490 1497
        while (ancestor_map[node] != lp) {
1491 1498
          for (OutArcIt e(_graph, node); e != INVALID; ++e) {
1492 1499
            Node tnode = _graph.target(e);
1493 1500
            if (order_map[tnode] > order_map[node] && low_map[tnode] == lp) {
1494 1501
              node = tnode;
1495 1502
              _kuratowski[e] = true;
1496 1503
              break;
1497 1504
            }
1498 1505
          }
1499 1506
        }
1500 1507
      }
1501 1508

	
1502 1509
      for (OutArcIt e(_graph, node); e != INVALID; ++e) {
1503 1510
        if (order_map[_graph.target(e)] == lp) {
1504 1511
          _kuratowski[e] = true;
1505 1512
          break;
1506 1513
        }
1507 1514
      }
1508 1515

	
1509 1516
      return lp;
1510 1517
    }
1511 1518

	
1512 1519
    void markPertinentPath(Node node, OrderMap& order_map,
1513 1520
                           NodeData& node_data, ArcLists& arc_lists,
1514 1521
                           EmbedArc& embed_arc, MergeRoots& merge_roots) {
1515 1522
      while (embed_arc[node] == INVALID) {
1516 1523
        int n = merge_roots[node].front();
1517 1524
        Arc arc = node_data[n].first;
1518 1525

	
1519 1526
        _kuratowski.set(arc, true);
1520 1527

	
1521 1528
        Node pred = node;
1522 1529
        node = _graph.target(arc);
1523 1530
        while (!pertinent(node, embed_arc, merge_roots)) {
1524 1531
          arc = node_data[order_map[node]].first;
1525 1532
          if (_graph.target(arc) == pred) {
1526 1533
            arc = arc_lists[arc].next;
1527 1534
          }
1528 1535
          _kuratowski.set(arc, true);
1529 1536
          pred = node;
1530 1537
          node = _graph.target(arc);
1531 1538
        }
1532 1539
      }
1533 1540
      _kuratowski.set(embed_arc[node], true);
1534 1541
    }
1535 1542

	
1536 1543
    void markPredPath(Node node, Node snode, PredMap& pred_map) {
1537 1544
      while (node != snode) {
1538 1545
        _kuratowski.set(pred_map[node], true);
1539 1546
        node = _graph.source(pred_map[node]);
1540 1547
      }
1541 1548
    }
1542 1549

	
1543 1550
    void markFacePath(Node ynode, Node xnode,
1544 1551
                      OrderMap& order_map, NodeData& node_data) {
1545 1552
      Arc arc = node_data[order_map[ynode]].first;
1546 1553
      Node node = _graph.target(arc);
1547 1554
      _kuratowski.set(arc, true);
1548 1555

	
1549 1556
      while (node != xnode) {
1550 1557
        arc = node_data[order_map[node]].first;
1551 1558
        _kuratowski.set(arc, true);
1552 1559
        node = _graph.target(arc);
1553 1560
      }
1554 1561
    }
1555 1562

	
1556 1563
    void markInternalPath(std::vector<Arc>& path) {
1557 1564
      for (int i = 0; i < int(path.size()); ++i) {
1558 1565
        _kuratowski.set(path[i], true);
1559 1566
      }
1560 1567
    }
1561 1568

	
1562 1569
    void markPilePath(std::vector<Arc>& path) {
1563 1570
      for (int i = 0; i < int(path.size()); ++i) {
1564 1571
        _kuratowski.set(path[i], true);
1565 1572
      }
1566 1573
    }
1567 1574

	
1568 1575
    void isolateKuratowski(Arc arc, NodeData& node_data,
1569 1576
                           ArcLists& arc_lists, FlipMap& flip_map,
1570 1577
                           OrderMap& order_map, OrderList& order_list,
1571 1578
                           PredMap& pred_map, ChildLists& child_lists,
1572 1579
                           AncestorMap& ancestor_map, LowMap& low_map,
1573 1580
                           EmbedArc& embed_arc, MergeRoots& merge_roots) {
1574 1581

	
1575 1582
      Node root = _graph.source(arc);
1576 1583
      Node enode = _graph.target(arc);
1577 1584

	
1578 1585
      int rorder = order_map[root];
1579 1586

	
1580 1587
      TypeMap type_map(_graph, 0);
1581 1588

	
1582 1589
      int rn = findComponentRoot(root, enode, child_lists,
1583 1590
                                 order_map, order_list);
1584 1591

	
1585 1592
      Node xnode = order_list[node_data[rn].next];
1586 1593
      Node ynode = order_list[node_data[rn].prev];
1587 1594

	
1588 1595
      // Minor-A
1589 1596
      {
1590 1597
        while (!merge_roots[xnode].empty() || !merge_roots[ynode].empty()) {
1591 1598

	
1592 1599
          if (!merge_roots[xnode].empty()) {
1593 1600
            root = xnode;
1594 1601
            rn = merge_roots[xnode].front();
1595 1602
          } else {
1596 1603
            root = ynode;
1597 1604
            rn = merge_roots[ynode].front();
1598 1605
          }
1599 1606

	
1600 1607
          xnode = order_list[node_data[rn].next];
1601 1608
          ynode = order_list[node_data[rn].prev];
1602 1609
        }
1603 1610

	
1604 1611
        if (root != _graph.source(arc)) {
1605 1612
          orientComponent(root, rn, order_map, pred_map,
1606 1613
                          node_data, arc_lists, flip_map, type_map);
1607 1614
          markFacePath(root, root, order_map, node_data);
1608 1615
          int xlp = markExternalPath(xnode, order_map, child_lists,
1609 1616
                                     pred_map, ancestor_map, low_map);
1610 1617
          int ylp = markExternalPath(ynode, order_map, child_lists,
1611 1618
                                     pred_map, ancestor_map, low_map);
1612 1619
          markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
1613 1620
          Node lwnode = findPertinent(ynode, order_map, node_data,
1614 1621
                                      embed_arc, merge_roots);
1615 1622

	
1616 1623
          markPertinentPath(lwnode, order_map, node_data, arc_lists,
1617 1624
                            embed_arc, merge_roots);
1618 1625

	
1619 1626
          return;
1620 1627
        }
1621 1628
      }
1622 1629

	
1623 1630
      orientComponent(root, rn, order_map, pred_map,
1624 1631
                      node_data, arc_lists, flip_map, type_map);
1625 1632

	
1626 1633
      Node wnode = findPertinent(ynode, order_map, node_data,
1627 1634
                                 embed_arc, merge_roots);
1628 1635
      setFaceFlags(root, wnode, ynode, xnode, order_map, node_data, type_map);
1629 1636

	
1630 1637

	
1631 1638
      //Minor-B
1632 1639
      if (!merge_roots[wnode].empty()) {
1633 1640
        int cn = merge_roots[wnode].back();
1634 1641
        Node rep = order_list[cn - order_list.size()];
1635 1642
        if (low_map[rep] < rorder) {
1636 1643
          markFacePath(root, root, order_map, node_data);
1637 1644
          int xlp = markExternalPath(xnode, order_map, child_lists,
1638 1645
                                     pred_map, ancestor_map, low_map);
1639 1646
          int ylp = markExternalPath(ynode, order_map, child_lists,
1640 1647
                                     pred_map, ancestor_map, low_map);
1641 1648

	
1642 1649
          Node lwnode, lznode;
1643 1650
          markCommonPath(wnode, rorder, lwnode, lznode, order_list,
1644 1651
                         order_map, node_data, arc_lists, embed_arc,
1645 1652
                         merge_roots, child_lists, ancestor_map, low_map);
1646 1653

	
1647 1654
          markPertinentPath(lwnode, order_map, node_data, arc_lists,
1648 1655
                            embed_arc, merge_roots);
1649 1656
          int zlp = markExternalPath(lznode, order_map, child_lists,
1650 1657
                                     pred_map, ancestor_map, low_map);
1651 1658

	
1652 1659
          int minlp = xlp < ylp ? xlp : ylp;
1653 1660
          if (zlp < minlp) minlp = zlp;
1654 1661

	
1655 1662
          int maxlp = xlp > ylp ? xlp : ylp;
1656 1663
          if (zlp > maxlp) maxlp = zlp;
1657 1664

	
1658 1665
          markPredPath(order_list[maxlp], order_list[minlp], pred_map);
1659 1666

	
1660 1667
          return;
1661 1668
        }
1662 1669
      }
1663 1670

	
1664 1671
      Node pxnode, pynode;
1665 1672
      std::vector<Arc> ipath;
1666 1673
      findInternalPath(ipath, wnode, root, type_map, order_map,
1667 1674
                       node_data, arc_lists);
1668 1675
      setInternalFlags(ipath, type_map);
1669 1676
      pynode = _graph.source(ipath.front());
1670 1677
      pxnode = _graph.target(ipath.back());
1671 1678

	
1672 1679
      wnode = findPertinent(pynode, order_map, node_data,
1673 1680
                            embed_arc, merge_roots);
1674 1681

	
1675 1682
      // Minor-C
1676 1683
      {
1677 1684
        if (type_map[_graph.source(ipath.front())] == HIGHY) {
1678 1685
          if (type_map[_graph.target(ipath.back())] == HIGHX) {
1679 1686
            markFacePath(xnode, pxnode, order_map, node_data);
1680 1687
          }
1681 1688
          markFacePath(root, xnode, order_map, node_data);
1682 1689
          markPertinentPath(wnode, order_map, node_data, arc_lists,
1683 1690
                            embed_arc, merge_roots);
1684 1691
          markInternalPath(ipath);
1685 1692
          int xlp = markExternalPath(xnode, order_map, child_lists,
1686 1693
                                     pred_map, ancestor_map, low_map);
1687 1694
          int ylp = markExternalPath(ynode, order_map, child_lists,
1688 1695
                                     pred_map, ancestor_map, low_map);
1689 1696
          markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
1690 1697
          return;
1691 1698
        }
1692 1699

	
1693 1700
        if (type_map[_graph.target(ipath.back())] == HIGHX) {
1694 1701
          markFacePath(ynode, root, order_map, node_data);
1695 1702
          markPertinentPath(wnode, order_map, node_data, arc_lists,
1696 1703
                            embed_arc, merge_roots);
1697 1704
          markInternalPath(ipath);
1698 1705
          int xlp = markExternalPath(xnode, order_map, child_lists,
1699 1706
                                     pred_map, ancestor_map, low_map);
1700 1707
          int ylp = markExternalPath(ynode, order_map, child_lists,
1701 1708
                                     pred_map, ancestor_map, low_map);
1702 1709
          markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
1703 1710
          return;
1704 1711
        }
1705 1712
      }
1706 1713

	
1707 1714
      std::vector<Arc> ppath;
1708 1715
      findPilePath(ppath, root, type_map, order_map, node_data, arc_lists);
1709 1716

	
1710 1717
      // Minor-D
1711 1718
      if (!ppath.empty()) {
1712 1719
        markFacePath(ynode, xnode, order_map, node_data);
1713 1720
        markPertinentPath(wnode, order_map, node_data, arc_lists,
1714 1721
                          embed_arc, merge_roots);
1715 1722
        markPilePath(ppath);
1716 1723
        markInternalPath(ipath);
1717 1724
        int xlp = markExternalPath(xnode, order_map, child_lists,
1718 1725
                                   pred_map, ancestor_map, low_map);
1719 1726
        int ylp = markExternalPath(ynode, order_map, child_lists,
1720 1727
                                   pred_map, ancestor_map, low_map);
1721 1728
        markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
1722 1729
        return;
1723 1730
      }
1724 1731

	
1725 1732
      // Minor-E*
1726 1733
      {
1727 1734

	
1728 1735
        if (!external(wnode, rorder, child_lists, ancestor_map, low_map)) {
1729 1736
          Node znode = findExternal(pynode, rorder, order_map,
1730 1737
                                    child_lists, ancestor_map,
1731 1738
                                    low_map, node_data);
1732 1739

	
1733 1740
          if (type_map[znode] == LOWY) {
1734 1741
            markFacePath(root, xnode, order_map, node_data);
1735 1742
            markPertinentPath(wnode, order_map, node_data, arc_lists,
1736 1743
                              embed_arc, merge_roots);
1737 1744
            markInternalPath(ipath);
1738 1745
            int xlp = markExternalPath(xnode, order_map, child_lists,
1739 1746
                                       pred_map, ancestor_map, low_map);
1740 1747
            int zlp = markExternalPath(znode, order_map, child_lists,
1741 1748
                                       pred_map, ancestor_map, low_map);
1742 1749
            markPredPath(root, order_list[xlp < zlp ? xlp : zlp], pred_map);
1743 1750
          } else {
1744 1751
            markFacePath(ynode, root, order_map, node_data);
1745 1752
            markPertinentPath(wnode, order_map, node_data, arc_lists,
1746 1753
                              embed_arc, merge_roots);
1747 1754
            markInternalPath(ipath);
1748 1755
            int ylp = markExternalPath(ynode, order_map, child_lists,
1749 1756
                                       pred_map, ancestor_map, low_map);
1750 1757
            int zlp = markExternalPath(znode, order_map, child_lists,
1751 1758
                                       pred_map, ancestor_map, low_map);
1752 1759
            markPredPath(root, order_list[ylp < zlp ? ylp : zlp], pred_map);
1753 1760
          }
1754 1761
          return;
1755 1762
        }
1756 1763

	
1757 1764
        int xlp = markExternalPath(xnode, order_map, child_lists,
1758 1765
                                   pred_map, ancestor_map, low_map);
1759 1766
        int ylp = markExternalPath(ynode, order_map, child_lists,
1760 1767
                                   pred_map, ancestor_map, low_map);
1761 1768
        int wlp = markExternalPath(wnode, order_map, child_lists,
1762 1769
                                   pred_map, ancestor_map, low_map);
1763 1770

	
1764 1771
        if (wlp > xlp && wlp > ylp) {
1765 1772
          markFacePath(root, root, order_map, node_data);
1766 1773
          markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
1767 1774
          return;
1768 1775
        }
1769 1776

	
1770 1777
        markInternalPath(ipath);
1771 1778
        markPertinentPath(wnode, order_map, node_data, arc_lists,
1772 1779
                          embed_arc, merge_roots);
1773 1780

	
1774 1781
        if (xlp > ylp && xlp > wlp) {
1775 1782
          markFacePath(root, pynode, order_map, node_data);
1776 1783
          markFacePath(wnode, xnode, order_map, node_data);
1777 1784
          markPredPath(root, order_list[ylp < wlp ? ylp : wlp], pred_map);
1778 1785
          return;
1779 1786
        }
1780 1787

	
1781 1788
        if (ylp > xlp && ylp > wlp) {
1782 1789
          markFacePath(pxnode, root, order_map, node_data);
1783 1790
          markFacePath(ynode, wnode, order_map, node_data);
1784 1791
          markPredPath(root, order_list[xlp < wlp ? xlp : wlp], pred_map);
1785 1792
          return;
1786 1793
        }
1787 1794

	
1788 1795
        if (pynode != ynode) {
1789 1796
          markFacePath(pxnode, wnode, order_map, node_data);
1790 1797

	
1791 1798
          int minlp = xlp < ylp ? xlp : ylp;
1792 1799
          if (wlp < minlp) minlp = wlp;
1793 1800

	
1794 1801
          int maxlp = xlp > ylp ? xlp : ylp;
1795 1802
          if (wlp > maxlp) maxlp = wlp;
1796 1803

	
1797 1804
          markPredPath(order_list[maxlp], order_list[minlp], pred_map);
1798 1805
          return;
1799 1806
        }
1800 1807

	
1801 1808
        if (pxnode != xnode) {
1802 1809
          markFacePath(wnode, pynode, order_map, node_data);
1803 1810

	
1804 1811
          int minlp = xlp < ylp ? xlp : ylp;
1805 1812
          if (wlp < minlp) minlp = wlp;
1806 1813

	
1807 1814
          int maxlp = xlp > ylp ? xlp : ylp;
1808 1815
          if (wlp > maxlp) maxlp = wlp;
1809 1816

	
1810 1817
          markPredPath(order_list[maxlp], order_list[minlp], pred_map);
1811 1818
          return;
1812 1819
        }
1813 1820

	
1814 1821
        markFacePath(root, root, order_map, node_data);
1815 1822
        int minlp = xlp < ylp ? xlp : ylp;
1816 1823
        if (wlp < minlp) minlp = wlp;
1817 1824
        markPredPath(root, order_list[minlp], pred_map);
1818 1825
        return;
1819 1826
      }
1820 1827

	
1821 1828
    }
1822 1829

	
1823 1830
  };
1824 1831

	
1825 1832
  namespace _planarity_bits {
1826 1833

	
1827 1834
    template <typename Graph, typename EmbeddingMap>
1828 1835
    void makeConnected(Graph& graph, EmbeddingMap& embedding) {
1829 1836
      DfsVisitor<Graph> null_visitor;
1830 1837
      DfsVisit<Graph, DfsVisitor<Graph> > dfs(graph, null_visitor);
1831 1838
      dfs.init();
1832 1839

	
1833 1840
      typename Graph::Node u = INVALID;
1834 1841
      for (typename Graph::NodeIt n(graph); n != INVALID; ++n) {
1835 1842
        if (!dfs.reached(n)) {
1836 1843
          dfs.addSource(n);
1837 1844
          dfs.start();
1838 1845
          if (u == INVALID) {
1839 1846
            u = n;
1840 1847
          } else {
1841 1848
            typename Graph::Node v = n;
1842 1849

	
1843 1850
            typename Graph::Arc ue = typename Graph::OutArcIt(graph, u);
1844 1851
            typename Graph::Arc ve = typename Graph::OutArcIt(graph, v);
1845 1852

	
1846 1853
            typename Graph::Arc e = graph.direct(graph.addEdge(u, v), true);
1847 1854

	
1848 1855
            if (ue != INVALID) {
1849 1856
              embedding[e] = embedding[ue];
1850 1857
              embedding[ue] = e;
1851 1858
            } else {
1852 1859
              embedding[e] = e;
1853 1860
            }
1854 1861

	
1855 1862
            if (ve != INVALID) {
1856 1863
              embedding[graph.oppositeArc(e)] = embedding[ve];
1857 1864
              embedding[ve] = graph.oppositeArc(e);
1858 1865
            } else {
1859 1866
              embedding[graph.oppositeArc(e)] = graph.oppositeArc(e);
1860 1867
            }
1861 1868
          }
1862 1869
        }
1863 1870
      }
1864 1871
    }
1865 1872

	
1866 1873
    template <typename Graph, typename EmbeddingMap>
1867 1874
    void makeBiNodeConnected(Graph& graph, EmbeddingMap& embedding) {
1868 1875
      typename Graph::template ArcMap<bool> processed(graph);
1869 1876

	
1870 1877
      std::vector<typename Graph::Arc> arcs;
1871 1878
      for (typename Graph::ArcIt e(graph); e != INVALID; ++e) {
1872 1879
        arcs.push_back(e);
1873 1880
      }
1874 1881

	
1875 1882
      IterableBoolMap<Graph, typename Graph::Node> visited(graph, false);
1876 1883

	
1877 1884
      for (int i = 0; i < int(arcs.size()); ++i) {
1878 1885
        typename Graph::Arc pp = arcs[i];
1879 1886
        if (processed[pp]) continue;
1880 1887

	
1881 1888
        typename Graph::Arc e = embedding[graph.oppositeArc(pp)];
1882 1889
        processed[e] = true;
1883 1890
        visited.set(graph.source(e), true);
1884 1891

	
1885 1892
        typename Graph::Arc p = e, l = e;
1886 1893
        e = embedding[graph.oppositeArc(e)];
1887 1894

	
1888 1895
        while (e != l) {
1889 1896
          processed[e] = true;
1890 1897

	
1891 1898
          if (visited[graph.source(e)]) {
1892 1899

	
1893 1900
            typename Graph::Arc n =
1894 1901
              graph.direct(graph.addEdge(graph.source(p),
1895 1902
                                           graph.target(e)), true);
1896 1903
            embedding[n] = p;
1897 1904
            embedding[graph.oppositeArc(pp)] = n;
1898 1905

	
1899 1906
            embedding[graph.oppositeArc(n)] =
1900 1907
              embedding[graph.oppositeArc(e)];
1901 1908
            embedding[graph.oppositeArc(e)] =
1902 1909
              graph.oppositeArc(n);
1903 1910

	
1904 1911
            p = n;
1905 1912
            e = embedding[graph.oppositeArc(n)];
1906 1913
          } else {
1907 1914
            visited.set(graph.source(e), true);
1908 1915
            pp = p;
1909 1916
            p = e;
1910 1917
            e = embedding[graph.oppositeArc(e)];
1911 1918
          }
1912 1919
        }
1913 1920
        visited.setAll(false);
1914 1921
      }
1915 1922
    }
1916 1923

	
1917 1924

	
1918 1925
    template <typename Graph, typename EmbeddingMap>
1919 1926
    void makeMaxPlanar(Graph& graph, EmbeddingMap& embedding) {
1920 1927

	
1921 1928
      typename Graph::template NodeMap<int> degree(graph);
1922 1929

	
1923 1930
      for (typename Graph::NodeIt n(graph); n != INVALID; ++n) {
1924 1931
        degree[n] = countIncEdges(graph, n);
1925 1932
      }
1926 1933

	
1927 1934
      typename Graph::template ArcMap<bool> processed(graph);
1928 1935
      IterableBoolMap<Graph, typename Graph::Node> visited(graph, false);
1929 1936

	
1930 1937
      std::vector<typename Graph::Arc> arcs;
1931 1938
      for (typename Graph::ArcIt e(graph); e != INVALID; ++e) {
1932 1939
        arcs.push_back(e);
1933 1940
      }
1934 1941

	
1935 1942
      for (int i = 0; i < int(arcs.size()); ++i) {
1936 1943
        typename Graph::Arc e = arcs[i];
1937 1944

	
1938 1945
        if (processed[e]) continue;
1939 1946
        processed[e] = true;
1940 1947

	
1941 1948
        typename Graph::Arc mine = e;
1942 1949
        int mind = degree[graph.source(e)];
1943 1950

	
1944 1951
        int face_size = 1;
1945 1952

	
1946 1953
        typename Graph::Arc l = e;
1947 1954
        e = embedding[graph.oppositeArc(e)];
1948 1955
        while (l != e) {
1949 1956
          processed[e] = true;
1950 1957

	
1951 1958
          ++face_size;
1952 1959

	
1953 1960
          if (degree[graph.source(e)] < mind) {
1954 1961
            mine = e;
1955 1962
            mind = degree[graph.source(e)];
1956 1963
          }
1957 1964

	
1958 1965
          e = embedding[graph.oppositeArc(e)];
1959 1966
        }
1960 1967

	
1961 1968
        if (face_size < 4) {
1962 1969
          continue;
1963 1970
        }
1964 1971

	
1965 1972
        typename Graph::Node s = graph.source(mine);
1966 1973
        for (typename Graph::OutArcIt e(graph, s); e != INVALID; ++e) {
1967 1974
          visited.set(graph.target(e), true);
1968 1975
        }
1969 1976

	
1970 1977
        typename Graph::Arc oppe = INVALID;
1971 1978

	
1972 1979
        e = embedding[graph.oppositeArc(mine)];
1973 1980
        e = embedding[graph.oppositeArc(e)];
1974 1981
        while (graph.target(e) != s) {
1975 1982
          if (visited[graph.source(e)]) {
1976 1983
            oppe = e;
1977 1984
            break;
1978 1985
          }
1979 1986
          e = embedding[graph.oppositeArc(e)];
1980 1987
        }
1981 1988
        visited.setAll(false);
1982 1989

	
1983 1990
        if (oppe == INVALID) {
1984 1991

	
1985 1992
          e = embedding[graph.oppositeArc(mine)];
1986 1993
          typename Graph::Arc pn = mine, p = e;
1987 1994

	
1988 1995
          e = embedding[graph.oppositeArc(e)];
1989 1996
          while (graph.target(e) != s) {
1990 1997
            typename Graph::Arc n =
1991 1998
              graph.direct(graph.addEdge(s, graph.source(e)), true);
1992 1999

	
1993 2000
            embedding[n] = pn;
1994 2001
            embedding[graph.oppositeArc(n)] = e;
1995 2002
            embedding[graph.oppositeArc(p)] = graph.oppositeArc(n);
1996 2003

	
1997 2004
            pn = n;
1998 2005

	
1999 2006
            p = e;
2000 2007
            e = embedding[graph.oppositeArc(e)];
2001 2008
          }
2002 2009

	
2003 2010
          embedding[graph.oppositeArc(e)] = pn;
2004 2011

	
2005 2012
        } else {
2006 2013

	
2007 2014
          mine = embedding[graph.oppositeArc(mine)];
2008 2015
          s = graph.source(mine);
2009 2016
          oppe = embedding[graph.oppositeArc(oppe)];
2010 2017
          typename Graph::Node t = graph.source(oppe);
2011 2018

	
2012 2019
          typename Graph::Arc ce = graph.direct(graph.addEdge(s, t), true);
2013 2020
          embedding[ce] = mine;
2014 2021
          embedding[graph.oppositeArc(ce)] = oppe;
2015 2022

	
2016 2023
          typename Graph::Arc pn = ce, p = oppe;
2017 2024
          e = embedding[graph.oppositeArc(oppe)];
2018 2025
          while (graph.target(e) != s) {
2019 2026
            typename Graph::Arc n =
2020 2027
              graph.direct(graph.addEdge(s, graph.source(e)), true);
2021 2028

	
2022 2029
            embedding[n] = pn;
2023 2030
            embedding[graph.oppositeArc(n)] = e;
2024 2031
            embedding[graph.oppositeArc(p)] = graph.oppositeArc(n);
2025 2032

	
2026 2033
            pn = n;
2027 2034

	
2028 2035
            p = e;
2029 2036
            e = embedding[graph.oppositeArc(e)];
2030 2037

	
2031 2038
          }
2032 2039
          embedding[graph.oppositeArc(e)] = pn;
2033 2040

	
2034 2041
          pn = graph.oppositeArc(ce), p = mine;
2035 2042
          e = embedding[graph.oppositeArc(mine)];
2036 2043
          while (graph.target(e) != t) {
2037 2044
            typename Graph::Arc n =
2038 2045
              graph.direct(graph.addEdge(t, graph.source(e)), true);
2039 2046

	
2040 2047
            embedding[n] = pn;
2041 2048
            embedding[graph.oppositeArc(n)] = e;
2042 2049
            embedding[graph.oppositeArc(p)] = graph.oppositeArc(n);
2043 2050

	
2044 2051
            pn = n;
2045 2052

	
2046 2053
            p = e;
2047 2054
            e = embedding[graph.oppositeArc(e)];
2048 2055

	
2049 2056
          }
2050 2057
          embedding[graph.oppositeArc(e)] = pn;
2051 2058
        }
2052 2059
      }
2053 2060
    }
2054 2061

	
2055 2062
  }
2056 2063

	
2057 2064
  /// \ingroup planar
2058 2065
  ///
2059 2066
  /// \brief Schnyder's planar drawing algorithm
2060 2067
  ///
2061 2068
  /// The planar drawing algorithm calculates positions for the nodes
2062
  /// in the plane which coordinates satisfy that if the arcs are
2063
  /// represented with straight lines then they will not intersect
2069
  /// in the plane. These coordinates satisfy that if the edges are
2070
  /// represented with straight lines, then they will not intersect
2064 2071
  /// each other.
2065 2072
  ///
2066
  /// Scnyder's algorithm embeds the graph on \c (n-2,n-2) size grid,
2067
  /// i.e. each node will be located in the \c [0,n-2]x[0,n-2] square.
2073
  /// Scnyder's algorithm embeds the graph on an \c (n-2)x(n-2) size grid,
2074
  /// i.e. each node will be located in the \c [0..n-2]x[0..n-2] square.
2068 2075
  /// The time complexity of the algorithm is O(n).
2076
  ///
2077
  /// \see PlanarEmbedding
2069 2078
  template <typename Graph>
2070 2079
  class PlanarDrawing {
2071 2080
  public:
2072 2081

	
2073 2082
    TEMPLATE_GRAPH_TYPEDEFS(Graph);
2074 2083

	
2075
    /// \brief The point type for store coordinates
2084
    /// \brief The point type for storing coordinates
2076 2085
    typedef dim2::Point<int> Point;
2077
    /// \brief The map type for store coordinates
2086
    /// \brief The map type for storing the coordinates of the nodes
2078 2087
    typedef typename Graph::template NodeMap<Point> PointMap;
2079 2088

	
2080 2089

	
2081 2090
    /// \brief Constructor
2082 2091
    ///
2083 2092
    /// Constructor
2084
    /// \pre The graph should be simple, i.e. loop and parallel arc free.
2093
    /// \pre The graph must be simple, i.e. it should not
2094
    /// contain parallel or loop arcs.
2085 2095
    PlanarDrawing(const Graph& graph)
2086 2096
      : _graph(graph), _point_map(graph) {}
2087 2097

	
2088 2098
  private:
2089 2099

	
2090 2100
    template <typename AuxGraph, typename AuxEmbeddingMap>
2091 2101
    void drawing(const AuxGraph& graph,
2092 2102
                 const AuxEmbeddingMap& next,
2093 2103
                 PointMap& point_map) {
2094 2104
      TEMPLATE_GRAPH_TYPEDEFS(AuxGraph);
2095 2105

	
2096 2106
      typename AuxGraph::template ArcMap<Arc> prev(graph);
2097 2107

	
2098 2108
      for (NodeIt n(graph); n != INVALID; ++n) {
2099 2109
        Arc e = OutArcIt(graph, n);
2100 2110

	
2101 2111
        Arc p = e, l = e;
2102 2112

	
2103 2113
        e = next[e];
2104 2114
        while (e != l) {
2105 2115
          prev[e] = p;
2106 2116
          p = e;
2107 2117
          e = next[e];
2108 2118
        }
2109 2119
        prev[e] = p;
2110 2120
      }
2111 2121

	
2112 2122
      Node anode, bnode, cnode;
2113 2123

	
2114 2124
      {
2115 2125
        Arc e = ArcIt(graph);
2116 2126
        anode = graph.source(e);
2117 2127
        bnode = graph.target(e);
2118 2128
        cnode = graph.target(next[graph.oppositeArc(e)]);
2119 2129
      }
2120 2130

	
2121 2131
      IterableBoolMap<AuxGraph, Node> proper(graph, false);
2122 2132
      typename AuxGraph::template NodeMap<int> conn(graph, -1);
2123 2133

	
2124 2134
      conn[anode] = conn[bnode] = -2;
2125 2135
      {
2126 2136
        for (OutArcIt e(graph, anode); e != INVALID; ++e) {
2127 2137
          Node m = graph.target(e);
2128 2138
          if (conn[m] == -1) {
2129 2139
            conn[m] = 1;
2130 2140
          }
2131 2141
        }
2132 2142
        conn[cnode] = 2;
2133 2143

	
2134 2144
        for (OutArcIt e(graph, bnode); e != INVALID; ++e) {
2135 2145
          Node m = graph.target(e);
2136 2146
          if (conn[m] == -1) {
2137 2147
            conn[m] = 1;
2138 2148
          } else if (conn[m] != -2) {
2139 2149
            conn[m] += 1;
2140 2150
            Arc pe = graph.oppositeArc(e);
2141 2151
            if (conn[graph.target(next[pe])] == -2) {
2142 2152
              conn[m] -= 1;
2143 2153
            }
2144 2154
            if (conn[graph.target(prev[pe])] == -2) {
2145 2155
              conn[m] -= 1;
2146 2156
            }
2147 2157

	
2148 2158
            proper.set(m, conn[m] == 1);
2149 2159
          }
2150 2160
        }
2151 2161
      }
2152 2162

	
2153 2163

	
2154 2164
      typename AuxGraph::template ArcMap<int> angle(graph, -1);
2155 2165

	
2156 2166
      while (proper.trueNum() != 0) {
2157 2167
        Node n = typename IterableBoolMap<AuxGraph, Node>::TrueIt(proper);
2158 2168
        proper.set(n, false);
2159 2169
        conn[n] = -2;
2160 2170

	
2161 2171
        for (OutArcIt e(graph, n); e != INVALID; ++e) {
2162 2172
          Node m = graph.target(e);
2163 2173
          if (conn[m] == -1) {
2164 2174
            conn[m] = 1;
2165 2175
          } else if (conn[m] != -2) {
2166 2176
            conn[m] += 1;
2167 2177
            Arc pe = graph.oppositeArc(e);
2168 2178
            if (conn[graph.target(next[pe])] == -2) {
2169 2179
              conn[m] -= 1;
2170 2180
            }
2171 2181
            if (conn[graph.target(prev[pe])] == -2) {
2172 2182
              conn[m] -= 1;
2173 2183
            }
2174 2184

	
2175 2185
            proper.set(m, conn[m] == 1);
2176 2186
          }
2177 2187
        }
2178 2188

	
2179 2189
        {
2180 2190
          Arc e = OutArcIt(graph, n);
2181 2191
          Arc p = e, l = e;
2182 2192

	
2183 2193
          e = next[e];
2184 2194
          while (e != l) {
2185 2195

	
2186 2196
            if (conn[graph.target(e)] == -2 && conn[graph.target(p)] == -2) {
2187 2197
              Arc f = e;
2188 2198
              angle[f] = 0;
2189 2199
              f = next[graph.oppositeArc(f)];
2190 2200
              angle[f] = 1;
2191 2201
              f = next[graph.oppositeArc(f)];
2192 2202
              angle[f] = 2;
2193 2203
            }
2194 2204

	
2195 2205
            p = e;
2196 2206
            e = next[e];
2197 2207
          }
2198 2208

	
2199 2209
          if (conn[graph.target(e)] == -2 && conn[graph.target(p)] == -2) {
2200 2210
            Arc f = e;
2201 2211
            angle[f] = 0;
2202 2212
            f = next[graph.oppositeArc(f)];
2203 2213
            angle[f] = 1;
2204 2214
            f = next[graph.oppositeArc(f)];
2205 2215
            angle[f] = 2;
2206 2216
          }
2207 2217
        }
2208 2218
      }
2209 2219

	
2210 2220
      typename AuxGraph::template NodeMap<Node> apred(graph, INVALID);
2211 2221
      typename AuxGraph::template NodeMap<Node> bpred(graph, INVALID);
2212 2222
      typename AuxGraph::template NodeMap<Node> cpred(graph, INVALID);
2213 2223

	
2214 2224
      typename AuxGraph::template NodeMap<int> apredid(graph, -1);
2215 2225
      typename AuxGraph::template NodeMap<int> bpredid(graph, -1);
2216 2226
      typename AuxGraph::template NodeMap<int> cpredid(graph, -1);
2217 2227

	
2218 2228
      for (ArcIt e(graph); e != INVALID; ++e) {
2219 2229
        if (angle[e] == angle[next[e]]) {
2220 2230
          switch (angle[e]) {
2221 2231
          case 2:
2222 2232
            apred[graph.target(e)] = graph.source(e);
2223 2233
            apredid[graph.target(e)] = graph.id(graph.source(e));
2224 2234
            break;
2225 2235
          case 1:
2226 2236
            bpred[graph.target(e)] = graph.source(e);
2227 2237
            bpredid[graph.target(e)] = graph.id(graph.source(e));
2228 2238
            break;
2229 2239
          case 0:
2230 2240
            cpred[graph.target(e)] = graph.source(e);
2231 2241
            cpredid[graph.target(e)] = graph.id(graph.source(e));
2232 2242
            break;
2233 2243
          }
2234 2244
        }
2235 2245
      }
2236 2246

	
2237 2247
      cpred[anode] = INVALID;
2238 2248
      cpred[bnode] = INVALID;
2239 2249

	
2240 2250
      std::vector<Node> aorder, border, corder;
2241 2251

	
2242 2252
      {
2243 2253
        typename AuxGraph::template NodeMap<bool> processed(graph, false);
2244 2254
        std::vector<Node> st;
2245 2255
        for (NodeIt n(graph); n != INVALID; ++n) {
2246 2256
          if (!processed[n] && n != bnode && n != cnode) {
2247 2257
            st.push_back(n);
2248 2258
            processed[n] = true;
2249 2259
            Node m = apred[n];
2250 2260
            while (m != INVALID && !processed[m]) {
2251 2261
              st.push_back(m);
2252 2262
              processed[m] = true;
2253 2263
              m = apred[m];
2254 2264
            }
2255 2265
            while (!st.empty()) {
2256 2266
              aorder.push_back(st.back());
2257 2267
              st.pop_back();
2258 2268
            }
2259 2269
          }
2260 2270
        }
2261 2271
      }
2262 2272

	
2263 2273
      {
2264 2274
        typename AuxGraph::template NodeMap<bool> processed(graph, false);
2265 2275
        std::vector<Node> st;
2266 2276
        for (NodeIt n(graph); n != INVALID; ++n) {
2267 2277
          if (!processed[n] && n != cnode && n != anode) {
2268 2278
            st.push_back(n);
2269 2279
            processed[n] = true;
2270 2280
            Node m = bpred[n];
2271 2281
            while (m != INVALID && !processed[m]) {
2272 2282
              st.push_back(m);
2273 2283
              processed[m] = true;
2274 2284
              m = bpred[m];
2275 2285
            }
2276 2286
            while (!st.empty()) {
2277 2287
              border.push_back(st.back());
2278 2288
              st.pop_back();
2279 2289
            }
2280 2290
          }
2281 2291
        }
2282 2292
      }
2283 2293

	
2284 2294
      {
2285 2295
        typename AuxGraph::template NodeMap<bool> processed(graph, false);
2286 2296
        std::vector<Node> st;
2287 2297
        for (NodeIt n(graph); n != INVALID; ++n) {
2288 2298
          if (!processed[n] && n != anode && n != bnode) {
2289 2299
            st.push_back(n);
2290 2300
            processed[n] = true;
2291 2301
            Node m = cpred[n];
2292 2302
            while (m != INVALID && !processed[m]) {
2293 2303
              st.push_back(m);
2294 2304
              processed[m] = true;
2295 2305
              m = cpred[m];
2296 2306
            }
2297 2307
            while (!st.empty()) {
2298 2308
              corder.push_back(st.back());
2299 2309
              st.pop_back();
2300 2310
            }
2301 2311
          }
2302 2312
        }
2303 2313
      }
2304 2314

	
2305 2315
      typename AuxGraph::template NodeMap<int> atree(graph, 0);
2306 2316
      for (int i = aorder.size() - 1; i >= 0; --i) {
2307 2317
        Node n = aorder[i];
2308 2318
        atree[n] = 1;
2309 2319
        for (OutArcIt e(graph, n); e != INVALID; ++e) {
2310 2320
          if (apred[graph.target(e)] == n) {
2311 2321
            atree[n] += atree[graph.target(e)];
2312 2322
          }
2313 2323
        }
2314 2324
      }
2315 2325

	
2316 2326
      typename AuxGraph::template NodeMap<int> btree(graph, 0);
2317 2327
      for (int i = border.size() - 1; i >= 0; --i) {
2318 2328
        Node n = border[i];
2319 2329
        btree[n] = 1;
2320 2330
        for (OutArcIt e(graph, n); e != INVALID; ++e) {
2321 2331
          if (bpred[graph.target(e)] == n) {
2322 2332
            btree[n] += btree[graph.target(e)];
2323 2333
          }
2324 2334
        }
2325 2335
      }
2326 2336

	
2327 2337
      typename AuxGraph::template NodeMap<int> apath(graph, 0);
2328 2338
      apath[bnode] = apath[cnode] = 1;
2329 2339
      typename AuxGraph::template NodeMap<int> apath_btree(graph, 0);
2330 2340
      apath_btree[bnode] = btree[bnode];
2331 2341
      for (int i = 1; i < int(aorder.size()); ++i) {
2332 2342
        Node n = aorder[i];
2333 2343
        apath[n] = apath[apred[n]] + 1;
2334 2344
        apath_btree[n] = btree[n] + apath_btree[apred[n]];
2335 2345
      }
2336 2346

	
2337 2347
      typename AuxGraph::template NodeMap<int> bpath_atree(graph, 0);
2338 2348
      bpath_atree[anode] = atree[anode];
2339 2349
      for (int i = 1; i < int(border.size()); ++i) {
2340 2350
        Node n = border[i];
2341 2351
        bpath_atree[n] = atree[n] + bpath_atree[bpred[n]];
2342 2352
      }
2343 2353

	
2344 2354
      typename AuxGraph::template NodeMap<int> cpath(graph, 0);
2345 2355
      cpath[anode] = cpath[bnode] = 1;
2346 2356
      typename AuxGraph::template NodeMap<int> cpath_atree(graph, 0);
2347 2357
      cpath_atree[anode] = atree[anode];
2348 2358
      typename AuxGraph::template NodeMap<int> cpath_btree(graph, 0);
2349 2359
      cpath_btree[bnode] = btree[bnode];
2350 2360
      for (int i = 1; i < int(corder.size()); ++i) {
2351 2361
        Node n = corder[i];
2352 2362
        cpath[n] = cpath[cpred[n]] + 1;
2353 2363
        cpath_atree[n] = atree[n] + cpath_atree[cpred[n]];
2354 2364
        cpath_btree[n] = btree[n] + cpath_btree[cpred[n]];
2355 2365
      }
2356 2366

	
2357 2367
      typename AuxGraph::template NodeMap<int> third(graph);
2358 2368
      for (NodeIt n(graph); n != INVALID; ++n) {
2359 2369
        point_map[n].x =
2360 2370
          bpath_atree[n] + cpath_atree[n] - atree[n] - cpath[n] + 1;
2361 2371
        point_map[n].y =
2362 2372
          cpath_btree[n] + apath_btree[n] - btree[n] - apath[n] + 1;
2363 2373
      }
2364 2374

	
2365 2375
    }
2366 2376

	
2367 2377
  public:
2368 2378

	
2369
    /// \brief Calculates the node positions
2379
    /// \brief Calculate the node positions
2370 2380
    ///
2371
    /// This function calculates the node positions.
2372
    /// \return %True if the graph is planar.
2381
    /// This function calculates the node positions on the plane.
2382
    /// \return \c true if the graph is planar.
2373 2383
    bool run() {
2374 2384
      PlanarEmbedding<Graph> pe(_graph);
2375 2385
      if (!pe.run()) return false;
2376 2386

	
2377 2387
      run(pe);
2378 2388
      return true;
2379 2389
    }
2380 2390

	
2381
    /// \brief Calculates the node positions according to a
2391
    /// \brief Calculate the node positions according to a
2382 2392
    /// combinatorical embedding
2383 2393
    ///
2384
    /// This function calculates the node locations. The \c embedding
2385
    /// parameter should contain a valid combinatorical embedding, i.e.
2386
    /// a valid cyclic order of the arcs.
2394
    /// This function calculates the node positions on the plane.
2395
    /// The given \c embedding map should contain a valid combinatorical
2396
    /// embedding, i.e. a valid cyclic order of the arcs.
2397
    /// It can be computed using PlanarEmbedding.
2387 2398
    template <typename EmbeddingMap>
2388 2399
    void run(const EmbeddingMap& embedding) {
2389 2400
      typedef SmartEdgeSet<Graph> AuxGraph;
2390 2401

	
2391 2402
      if (3 * countNodes(_graph) - 6 == countEdges(_graph)) {
2392 2403
        drawing(_graph, embedding, _point_map);
2393 2404
        return;
2394 2405
      }
2395 2406

	
2396 2407
      AuxGraph aux_graph(_graph);
2397 2408
      typename AuxGraph::template ArcMap<typename AuxGraph::Arc>
2398 2409
        aux_embedding(aux_graph);
2399 2410

	
2400 2411
      {
2401 2412

	
2402 2413
        typename Graph::template EdgeMap<typename AuxGraph::Edge>
2403 2414
          ref(_graph);
2404 2415

	
2405 2416
        for (EdgeIt e(_graph); e != INVALID; ++e) {
2406 2417
          ref[e] = aux_graph.addEdge(_graph.u(e), _graph.v(e));
2407 2418
        }
2408 2419

	
2409 2420
        for (EdgeIt e(_graph); e != INVALID; ++e) {
2410 2421
          Arc ee = embedding[_graph.direct(e, true)];
2411 2422
          aux_embedding[aux_graph.direct(ref[e], true)] =
2412 2423
            aux_graph.direct(ref[ee], _graph.direction(ee));
2413 2424
          ee = embedding[_graph.direct(e, false)];
2414 2425
          aux_embedding[aux_graph.direct(ref[e], false)] =
2415 2426
            aux_graph.direct(ref[ee], _graph.direction(ee));
2416 2427
        }
2417 2428
      }
2418 2429
      _planarity_bits::makeConnected(aux_graph, aux_embedding);
2419 2430
      _planarity_bits::makeBiNodeConnected(aux_graph, aux_embedding);
2420 2431
      _planarity_bits::makeMaxPlanar(aux_graph, aux_embedding);
2421 2432
      drawing(aux_graph, aux_embedding, _point_map);
2422 2433
    }
2423 2434

	
2424 2435
    /// \brief The coordinate of the given node
2425 2436
    ///
2426
    /// The coordinate of the given node.
2437
    /// This function returns the coordinate of the given node.
2427 2438
    Point operator[](const Node& node) const {
2428 2439
      return _point_map[node];
2429 2440
    }
2430 2441

	
2431
    /// \brief Returns the grid embedding in a \e NodeMap.
2442
    /// \brief Return the grid embedding in a node map
2432 2443
    ///
2433
    /// Returns the grid embedding in a \e NodeMap of \c dim2::Point<int> .
2444
    /// This function returns the grid embedding in a node map of
2445
    /// \c dim2::Point<int> coordinates.
2434 2446
    const PointMap& coords() const {
2435 2447
      return _point_map;
2436 2448
    }
2437 2449

	
2438 2450
  private:
2439 2451

	
2440 2452
    const Graph& _graph;
2441 2453
    PointMap _point_map;
2442 2454

	
2443 2455
  };
2444 2456

	
2445 2457
  namespace _planarity_bits {
2446 2458

	
2447 2459
    template <typename ColorMap>
2448 2460
    class KempeFilter {
2449 2461
    public:
2450 2462
      typedef typename ColorMap::Key Key;
2451 2463
      typedef bool Value;
2452 2464

	
2453 2465
      KempeFilter(const ColorMap& color_map,
2454 2466
                  const typename ColorMap::Value& first,
2455 2467
                  const typename ColorMap::Value& second)
2456 2468
        : _color_map(color_map), _first(first), _second(second) {}
2457 2469

	
2458 2470
      Value operator[](const Key& key) const {
2459 2471
        return _color_map[key] == _first || _color_map[key] == _second;
2460 2472
      }
2461 2473

	
2462 2474
    private:
2463 2475
      const ColorMap& _color_map;
2464 2476
      typename ColorMap::Value _first, _second;
2465 2477
    };
2466 2478
  }
2467 2479

	
2468 2480
  /// \ingroup planar
2469 2481
  ///
2470 2482
  /// \brief Coloring planar graphs
2471 2483
  ///
2472 2484
  /// The graph coloring problem is the coloring of the graph nodes
2473
  /// that there are not adjacent nodes with the same color. The
2474
  /// planar graphs can be always colored with four colors, it is
2475
  /// proved by Appel and Haken and their proofs provide a quadratic
2485
  /// so that there are no adjacent nodes with the same color. The
2486
  /// planar graphs can always be colored with four colors, which is
2487
  /// proved by Appel and Haken. Their proofs provide a quadratic
2476 2488
  /// time algorithm for four coloring, but it could not be used to
2477
  /// implement efficient algorithm. The five and six coloring can be
2478
  /// made in linear time, but in this class the five coloring has
2489
  /// implement an efficient algorithm. The five and six coloring can be
2490
  /// made in linear time, but in this class, the five coloring has
2479 2491
  /// quadratic worst case time complexity. The two coloring (if
2480 2492
  /// possible) is solvable with a graph search algorithm and it is
2481 2493
  /// implemented in \ref bipartitePartitions() function in LEMON. To
2482
  /// decide whether the planar graph is three colorable is
2483
  /// NP-complete.
2494
  /// decide whether a planar graph is three colorable is NP-complete.
2484 2495
  ///
2485 2496
  /// This class contains member functions for calculate colorings
2486 2497
  /// with five and six colors. The six coloring algorithm is a simple
2487 2498
  /// greedy coloring on the backward minimum outgoing order of nodes.
2488
  /// This order can be computed as in each phase the node with least
2489
  /// outgoing arcs to unprocessed nodes is chosen. This order
2499
  /// This order can be computed by selecting the node with least
2500
  /// outgoing arcs to unprocessed nodes in each phase. This order
2490 2501
  /// guarantees that when a node is chosen for coloring it has at
2491 2502
  /// most five already colored adjacents. The five coloring algorithm
2492 2503
  /// use the same method, but if the greedy approach fails to color
2493 2504
  /// with five colors, i.e. the node has five already different
2494 2505
  /// colored neighbours, it swaps the colors in one of the connected
2495 2506
  /// two colored sets with the Kempe recoloring method.
2496 2507
  template <typename Graph>
2497 2508
  class PlanarColoring {
2498 2509
  public:
2499 2510

	
2500 2511
    TEMPLATE_GRAPH_TYPEDEFS(Graph);
2501 2512

	
2502
    /// \brief The map type for store color indexes
2513
    /// \brief The map type for storing color indices
2503 2514
    typedef typename Graph::template NodeMap<int> IndexMap;
2504
    /// \brief The map type for store colors
2515
    /// \brief The map type for storing colors
2516
    ///
2517
    /// The map type for storing colors.
2518
    /// \see Palette, Color
2505 2519
    typedef ComposeMap<Palette, IndexMap> ColorMap;
2506 2520

	
2507 2521
    /// \brief Constructor
2508 2522
    ///
2509
    /// Constructor
2510
    /// \pre The graph should be simple, i.e. loop and parallel arc free.
2523
    /// Constructor.
2524
    /// \pre The graph must be simple, i.e. it should not
2525
    /// contain parallel or loop arcs.
2511 2526
    PlanarColoring(const Graph& graph)
2512 2527
      : _graph(graph), _color_map(graph), _palette(0) {
2513 2528
      _palette.add(Color(1,0,0));
2514 2529
      _palette.add(Color(0,1,0));
2515 2530
      _palette.add(Color(0,0,1));
2516 2531
      _palette.add(Color(1,1,0));
2517 2532
      _palette.add(Color(1,0,1));
2518 2533
      _palette.add(Color(0,1,1));
2519 2534
    }
2520 2535

	
2521
    /// \brief Returns the \e NodeMap of color indexes
2536
    /// \brief Return the node map of color indices
2522 2537
    ///
2523
    /// Returns the \e NodeMap of color indexes. The values are in the
2524
    /// range \c [0..4] or \c [0..5] according to the coloring method.
2538
    /// This function returns the node map of color indices. The values are
2539
    /// in the range \c [0..4] or \c [0..5] according to the coloring method.
2525 2540
    IndexMap colorIndexMap() const {
2526 2541
      return _color_map;
2527 2542
    }
2528 2543

	
2529
    /// \brief Returns the \e NodeMap of colors
2544
    /// \brief Return the node map of colors
2530 2545
    ///
2531
    /// Returns the \e NodeMap of colors. The values are five or six
2532
    /// distinct \ref lemon::Color "colors".
2546
    /// This function returns the node map of colors. The values are among
2547
    /// five or six distinct \ref lemon::Color "colors".
2533 2548
    ColorMap colorMap() const {
2534 2549
      return composeMap(_palette, _color_map);
2535 2550
    }
2536 2551

	
2537
    /// \brief Returns the color index of the node
2552
    /// \brief Return the color index of the node
2538 2553
    ///
2539
    /// Returns the color index of the node. The values are in the
2540
    /// range \c [0..4] or \c [0..5] according to the coloring method.
2554
    /// This function returns the color index of the given node. The value is
2555
    /// in the range \c [0..4] or \c [0..5] according to the coloring method.
2541 2556
    int colorIndex(const Node& node) const {
2542 2557
      return _color_map[node];
2543 2558
    }
2544 2559

	
2545
    /// \brief Returns the color of the node
2560
    /// \brief Return the color of the node
2546 2561
    ///
2547
    /// Returns the color of the node. The values are five or six
2548
    /// distinct \ref lemon::Color "colors".
2562
    /// This function returns the color of the given node. The value is among
2563
    /// five or six distinct \ref lemon::Color "colors".
2549 2564
    Color color(const Node& node) const {
2550 2565
      return _palette[_color_map[node]];
2551 2566
    }
2552 2567

	
2553 2568

	
2554
    /// \brief Calculates a coloring with at most six colors
2569
    /// \brief Calculate a coloring with at most six colors
2555 2570
    ///
2556 2571
    /// This function calculates a coloring with at most six colors. The time
2557 2572
    /// complexity of this variant is linear in the size of the graph.
2558
    /// \return %True when the algorithm could color the graph with six color.
2559
    /// If the algorithm fails, then the graph could not be planar.
2560
    /// \note This function can return true if the graph is not
2561
    /// planar but it can be colored with 6 colors.
2573
    /// \return \c true if the algorithm could color the graph with six colors.
2574
    /// If the algorithm fails, then the graph is not planar.
2575
    /// \note This function can return \c true if the graph is not
2576
    /// planar, but it can be colored with at most six colors.
2562 2577
    bool runSixColoring() {
2563 2578

	
2564 2579
      typename Graph::template NodeMap<int> heap_index(_graph, -1);
2565 2580
      BucketHeap<typename Graph::template NodeMap<int> > heap(heap_index);
2566 2581

	
2567 2582
      for (NodeIt n(_graph); n != INVALID; ++n) {
2568 2583
        _color_map[n] = -2;
2569 2584
        heap.push(n, countOutArcs(_graph, n));
2570 2585
      }
2571 2586

	
2572 2587
      std::vector<Node> order;
2573 2588

	
2574 2589
      while (!heap.empty()) {
2575 2590
        Node n = heap.top();
2576 2591
        heap.pop();
2577 2592
        _color_map[n] = -1;
2578 2593
        order.push_back(n);
2579 2594
        for (OutArcIt e(_graph, n); e != INVALID; ++e) {
2580 2595
          Node t = _graph.runningNode(e);
2581 2596
          if (_color_map[t] == -2) {
2582 2597
            heap.decrease(t, heap[t] - 1);
2583 2598
          }
2584 2599
        }
2585 2600
      }
2586 2601

	
2587 2602
      for (int i = order.size() - 1; i >= 0; --i) {
2588 2603
        std::vector<bool> forbidden(6, false);
2589 2604
        for (OutArcIt e(_graph, order[i]); e != INVALID; ++e) {
2590 2605
          Node t = _graph.runningNode(e);
2591 2606
          if (_color_map[t] != -1) {
2592 2607
            forbidden[_color_map[t]] = true;
2593 2608
          }
2594 2609
        }
2595 2610
               for (int k = 0; k < 6; ++k) {
2596 2611
          if (!forbidden[k]) {
2597 2612
            _color_map[order[i]] = k;
2598 2613
            break;
2599 2614
          }
2600 2615
        }
2601 2616
        if (_color_map[order[i]] == -1) {
2602 2617
          return false;
2603 2618
        }
2604 2619
      }
2605 2620
      return true;
2606 2621
    }
2607 2622

	
2608 2623
  private:
2609 2624

	
2610 2625
    bool recolor(const Node& u, const Node& v) {
2611 2626
      int ucolor = _color_map[u];
2612 2627
      int vcolor = _color_map[v];
2613 2628
      typedef _planarity_bits::KempeFilter<IndexMap> KempeFilter;
2614 2629
      KempeFilter filter(_color_map, ucolor, vcolor);
2615 2630

	
2616 2631
      typedef FilterNodes<const Graph, const KempeFilter> KempeGraph;
2617 2632
      KempeGraph kempe_graph(_graph, filter);
2618 2633

	
2619 2634
      std::vector<Node> comp;
2620 2635
      Bfs<KempeGraph> bfs(kempe_graph);
2621 2636
      bfs.init();
2622 2637
      bfs.addSource(u);
2623 2638
      while (!bfs.emptyQueue()) {
2624 2639
        Node n = bfs.nextNode();
2625 2640
        if (n == v) return false;
2626 2641
        comp.push_back(n);
2627 2642
        bfs.processNextNode();
2628 2643
      }
2629 2644

	
2630 2645
      int scolor = ucolor + vcolor;
2631 2646
      for (int i = 0; i < static_cast<int>(comp.size()); ++i) {
2632 2647
        _color_map[comp[i]] = scolor - _color_map[comp[i]];
2633 2648
      }
2634 2649

	
2635 2650
      return true;
2636 2651
    }
2637 2652

	
2638 2653
    template <typename EmbeddingMap>
2639 2654
    void kempeRecoloring(const Node& node, const EmbeddingMap& embedding) {
2640 2655
      std::vector<Node> nodes;
2641 2656
      nodes.reserve(4);
2642 2657

	
2643 2658
      for (Arc e = OutArcIt(_graph, node); e != INVALID; e = embedding[e]) {
2644 2659
        Node t = _graph.target(e);
2645 2660
        if (_color_map[t] != -1) {
2646 2661
          nodes.push_back(t);
2647 2662
          if (nodes.size() == 4) break;
2648 2663
        }
2649 2664
      }
2650 2665

	
2651 2666
      int color = _color_map[nodes[0]];
2652 2667
      if (recolor(nodes[0], nodes[2])) {
2653 2668
        _color_map[node] = color;
2654 2669
      } else {
2655 2670
        color = _color_map[nodes[1]];
2656 2671
        recolor(nodes[1], nodes[3]);
2657 2672
        _color_map[node] = color;
2658 2673
      }
2659 2674
    }
2660 2675

	
2661 2676
  public:
2662 2677

	
2663
    /// \brief Calculates a coloring with at most five colors
2678
    /// \brief Calculate a coloring with at most five colors
2664 2679
    ///
2665 2680
    /// This function calculates a coloring with at most five
2666 2681
    /// colors. The worst case time complexity of this variant is
2667 2682
    /// quadratic in the size of the graph.
2683
    /// \param embedding This map should contain a valid combinatorical
2684
    /// embedding, i.e. a valid cyclic order of the arcs.
2685
    /// It can be computed using PlanarEmbedding.
2668 2686
    template <typename EmbeddingMap>
2669 2687
    void runFiveColoring(const EmbeddingMap& embedding) {
2670 2688

	
2671 2689
      typename Graph::template NodeMap<int> heap_index(_graph, -1);
2672 2690
      BucketHeap<typename Graph::template NodeMap<int> > heap(heap_index);
2673 2691

	
2674 2692
      for (NodeIt n(_graph); n != INVALID; ++n) {
2675 2693
        _color_map[n] = -2;
2676 2694
        heap.push(n, countOutArcs(_graph, n));
2677 2695
      }
2678 2696

	
2679 2697
      std::vector<Node> order;
2680 2698

	
2681 2699
      while (!heap.empty()) {
2682 2700
        Node n = heap.top();
2683 2701
        heap.pop();
2684 2702
        _color_map[n] = -1;
2685 2703
        order.push_back(n);
2686 2704
        for (OutArcIt e(_graph, n); e != INVALID; ++e) {
2687 2705
          Node t = _graph.runningNode(e);
2688 2706
          if (_color_map[t] == -2) {
2689 2707
            heap.decrease(t, heap[t] - 1);
2690 2708
          }
2691 2709
        }
2692 2710
      }
2693 2711

	
2694 2712
      for (int i = order.size() - 1; i >= 0; --i) {
2695 2713
        std::vector<bool> forbidden(5, false);
2696 2714
        for (OutArcIt e(_graph, order[i]); e != INVALID; ++e) {
2697 2715
          Node t = _graph.runningNode(e);
2698 2716
          if (_color_map[t] != -1) {
2699 2717
            forbidden[_color_map[t]] = true;
2700 2718
          }
2701 2719
        }
2702 2720
        for (int k = 0; k < 5; ++k) {
2703 2721
          if (!forbidden[k]) {
2704 2722
            _color_map[order[i]] = k;
2705 2723
            break;
2706 2724
          }
2707 2725
        }
2708 2726
        if (_color_map[order[i]] == -1) {
2709 2727
          kempeRecoloring(order[i], embedding);
2710 2728
        }
2711 2729
      }
2712 2730
    }
2713 2731

	
2714
    /// \brief Calculates a coloring with at most five colors
2732
    /// \brief Calculate a coloring with at most five colors
2715 2733
    ///
2716 2734
    /// This function calculates a coloring with at most five
2717 2735
    /// colors. The worst case time complexity of this variant is
2718 2736
    /// quadratic in the size of the graph.
2719
    /// \return %True when the graph is planar.
2737
    /// \return \c true if the graph is planar.
2720 2738
    bool runFiveColoring() {
2721 2739
      PlanarEmbedding<Graph> pe(_graph);
2722 2740
      if (!pe.run()) return false;
2723 2741

	
2724 2742
      runFiveColoring(pe.embeddingMap());
2725 2743
      return true;
2726 2744
    }
2727 2745

	
2728 2746
  private:
2729 2747

	
2730 2748
    const Graph& _graph;
2731 2749
    IndexMap _color_map;
2732 2750
    Palette _palette;
2733 2751
  };
2734 2752

	
2735 2753
}
2736 2754

	
2737 2755
#endif
0 comments (0 inline)