Port planarity related algorithms from SVN 3509 (#62)
authorBalazs Dezso <deba@inf.elte.hu>
Wed, 09 Sep 2009 15:32:03 +0200
changeset 79730cb42e3e43a
parent 712 6d5f547e5bfb
child 798 58c330ad0b5c
Port planarity related algorithms from SVN 3509 (#62)
lemon/Makefile.am
lemon/planarity.h
test/CMakeLists.txt
test/Makefile.am
test/planarity_test.cc
     1.1 --- a/lemon/Makefile.am	Mon Aug 31 20:27:38 2009 +0200
     1.2 +++ b/lemon/Makefile.am	Wed Sep 09 15:32:03 2009 +0200
     1.3 @@ -104,6 +104,7 @@
     1.4  	lemon/network_simplex.h \
     1.5  	lemon/pairing_heap.h \
     1.6  	lemon/path.h \
     1.7 +	lemon/planarity.h \
     1.8  	lemon/preflow.h \
     1.9  	lemon/radix_heap.h \
    1.10  	lemon/radix_sort.h \
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/lemon/planarity.h	Wed Sep 09 15:32:03 2009 +0200
     2.3 @@ -0,0 +1,2737 @@
     2.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     2.5 + *
     2.6 + * This file is a part of LEMON, a generic C++ optimization library.
     2.7 + *
     2.8 + * Copyright (C) 2003-2009
     2.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    2.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    2.11 + *
    2.12 + * Permission to use, modify and distribute this software is granted
    2.13 + * provided that this copyright notice appears in all copies. For
    2.14 + * precise terms see the accompanying LICENSE file.
    2.15 + *
    2.16 + * This software is provided "AS IS" with no warranty of any kind,
    2.17 + * express or implied, and with no claim as to its suitability for any
    2.18 + * purpose.
    2.19 + *
    2.20 + */
    2.21 +
    2.22 +#ifndef LEMON_PLANARITY_H
    2.23 +#define LEMON_PLANARITY_H
    2.24 +
    2.25 +/// \ingroup planar
    2.26 +/// \file
    2.27 +/// \brief Planarity checking, embedding, drawing and coloring
    2.28 +
    2.29 +#include <vector>
    2.30 +#include <list>
    2.31 +
    2.32 +#include <lemon/dfs.h>
    2.33 +#include <lemon/bfs.h>
    2.34 +#include <lemon/radix_sort.h>
    2.35 +#include <lemon/maps.h>
    2.36 +#include <lemon/path.h>
    2.37 +#include <lemon/bucket_heap.h>
    2.38 +#include <lemon/adaptors.h>
    2.39 +#include <lemon/edge_set.h>
    2.40 +#include <lemon/color.h>
    2.41 +#include <lemon/dim2.h>
    2.42 +
    2.43 +namespace lemon {
    2.44 +
    2.45 +  namespace _planarity_bits {
    2.46 +
    2.47 +    template <typename Graph>
    2.48 +    struct PlanarityVisitor : DfsVisitor<Graph> {
    2.49 +
    2.50 +      TEMPLATE_GRAPH_TYPEDEFS(Graph);
    2.51 +
    2.52 +      typedef typename Graph::template NodeMap<Arc> PredMap;
    2.53 +
    2.54 +      typedef typename Graph::template EdgeMap<bool> TreeMap;
    2.55 +
    2.56 +      typedef typename Graph::template NodeMap<int> OrderMap;
    2.57 +      typedef std::vector<Node> OrderList;
    2.58 +
    2.59 +      typedef typename Graph::template NodeMap<int> LowMap;
    2.60 +      typedef typename Graph::template NodeMap<int> AncestorMap;
    2.61 +
    2.62 +      PlanarityVisitor(const Graph& graph,
    2.63 +                       PredMap& pred_map, TreeMap& tree_map,
    2.64 +                       OrderMap& order_map, OrderList& order_list,
    2.65 +                       AncestorMap& ancestor_map, LowMap& low_map)
    2.66 +        : _graph(graph), _pred_map(pred_map), _tree_map(tree_map),
    2.67 +          _order_map(order_map), _order_list(order_list),
    2.68 +          _ancestor_map(ancestor_map), _low_map(low_map) {}
    2.69 +
    2.70 +      void reach(const Node& node) {
    2.71 +        _order_map[node] = _order_list.size();
    2.72 +        _low_map[node] = _order_list.size();
    2.73 +        _ancestor_map[node] = _order_list.size();
    2.74 +        _order_list.push_back(node);
    2.75 +      }
    2.76 +
    2.77 +      void discover(const Arc& arc) {
    2.78 +        Node source = _graph.source(arc);
    2.79 +        Node target = _graph.target(arc);
    2.80 +
    2.81 +        _tree_map[arc] = true;
    2.82 +        _pred_map[target] = arc;
    2.83 +      }
    2.84 +
    2.85 +      void examine(const Arc& arc) {
    2.86 +        Node source = _graph.source(arc);
    2.87 +        Node target = _graph.target(arc);
    2.88 +
    2.89 +        if (_order_map[target] < _order_map[source] && !_tree_map[arc]) {
    2.90 +          if (_low_map[source] > _order_map[target]) {
    2.91 +            _low_map[source] = _order_map[target];
    2.92 +          }
    2.93 +          if (_ancestor_map[source] > _order_map[target]) {
    2.94 +            _ancestor_map[source] = _order_map[target];
    2.95 +          }
    2.96 +        }
    2.97 +      }
    2.98 +
    2.99 +      void backtrack(const Arc& arc) {
   2.100 +        Node source = _graph.source(arc);
   2.101 +        Node target = _graph.target(arc);
   2.102 +
   2.103 +        if (_low_map[source] > _low_map[target]) {
   2.104 +          _low_map[source] = _low_map[target];
   2.105 +        }
   2.106 +      }
   2.107 +
   2.108 +      const Graph& _graph;
   2.109 +      PredMap& _pred_map;
   2.110 +      TreeMap& _tree_map;
   2.111 +      OrderMap& _order_map;
   2.112 +      OrderList& _order_list;
   2.113 +      AncestorMap& _ancestor_map;
   2.114 +      LowMap& _low_map;
   2.115 +    };
   2.116 +
   2.117 +    template <typename Graph, bool embedding = true>
   2.118 +    struct NodeDataNode {
   2.119 +      int prev, next;
   2.120 +      int visited;
   2.121 +      typename Graph::Arc first;
   2.122 +      bool inverted;
   2.123 +    };
   2.124 +
   2.125 +    template <typename Graph>
   2.126 +    struct NodeDataNode<Graph, false> {
   2.127 +      int prev, next;
   2.128 +      int visited;
   2.129 +    };
   2.130 +
   2.131 +    template <typename Graph>
   2.132 +    struct ChildListNode {
   2.133 +      typedef typename Graph::Node Node;
   2.134 +      Node first;
   2.135 +      Node prev, next;
   2.136 +    };
   2.137 +
   2.138 +    template <typename Graph>
   2.139 +    struct ArcListNode {
   2.140 +      typename Graph::Arc prev, next;
   2.141 +    };
   2.142 +
   2.143 +  }
   2.144 +
   2.145 +  /// \ingroup planar
   2.146 +  ///
   2.147 +  /// \brief Planarity checking of an undirected simple graph
   2.148 +  ///
   2.149 +  /// This class implements the Boyer-Myrvold algorithm for planarity
   2.150 +  /// checking of an undirected graph. This class is a simplified
   2.151 +  /// version of the PlanarEmbedding algorithm class because neither
   2.152 +  /// the embedding nor the kuratowski subdivisons are not computed.
   2.153 +  template <typename Graph>
   2.154 +  class PlanarityChecking {
   2.155 +  private:
   2.156 +
   2.157 +    TEMPLATE_GRAPH_TYPEDEFS(Graph);
   2.158 +
   2.159 +    const Graph& _graph;
   2.160 +
   2.161 +  private:
   2.162 +
   2.163 +    typedef typename Graph::template NodeMap<Arc> PredMap;
   2.164 +
   2.165 +    typedef typename Graph::template EdgeMap<bool> TreeMap;
   2.166 +
   2.167 +    typedef typename Graph::template NodeMap<int> OrderMap;
   2.168 +    typedef std::vector<Node> OrderList;
   2.169 +
   2.170 +    typedef typename Graph::template NodeMap<int> LowMap;
   2.171 +    typedef typename Graph::template NodeMap<int> AncestorMap;
   2.172 +
   2.173 +    typedef _planarity_bits::NodeDataNode<Graph> NodeDataNode;
   2.174 +    typedef std::vector<NodeDataNode> NodeData;
   2.175 +
   2.176 +    typedef _planarity_bits::ChildListNode<Graph> ChildListNode;
   2.177 +    typedef typename Graph::template NodeMap<ChildListNode> ChildLists;
   2.178 +
   2.179 +    typedef typename Graph::template NodeMap<std::list<int> > MergeRoots;
   2.180 +
   2.181 +    typedef typename Graph::template NodeMap<bool> EmbedArc;
   2.182 +
   2.183 +  public:
   2.184 +
   2.185 +    /// \brief Constructor
   2.186 +    ///
   2.187 +    /// \note The graph should be simple, i.e. parallel and loop arc
   2.188 +    /// free.
   2.189 +    PlanarityChecking(const Graph& graph) : _graph(graph) {}
   2.190 +
   2.191 +    /// \brief Runs the algorithm.
   2.192 +    ///
   2.193 +    /// Runs the algorithm.
   2.194 +    /// \return %True when the graph is planar.
   2.195 +    bool run() {
   2.196 +      typedef _planarity_bits::PlanarityVisitor<Graph> Visitor;
   2.197 +
   2.198 +      PredMap pred_map(_graph, INVALID);
   2.199 +      TreeMap tree_map(_graph, false);
   2.200 +
   2.201 +      OrderMap order_map(_graph, -1);
   2.202 +      OrderList order_list;
   2.203 +
   2.204 +      AncestorMap ancestor_map(_graph, -1);
   2.205 +      LowMap low_map(_graph, -1);
   2.206 +
   2.207 +      Visitor visitor(_graph, pred_map, tree_map,
   2.208 +                      order_map, order_list, ancestor_map, low_map);
   2.209 +      DfsVisit<Graph, Visitor> visit(_graph, visitor);
   2.210 +      visit.run();
   2.211 +
   2.212 +      ChildLists child_lists(_graph);
   2.213 +      createChildLists(tree_map, order_map, low_map, child_lists);
   2.214 +
   2.215 +      NodeData node_data(2 * order_list.size());
   2.216 +
   2.217 +      EmbedArc embed_arc(_graph, false);
   2.218 +
   2.219 +      MergeRoots merge_roots(_graph);
   2.220 +
   2.221 +      for (int i = order_list.size() - 1; i >= 0; --i) {
   2.222 +
   2.223 +        Node node = order_list[i];
   2.224 +
   2.225 +        Node source = node;
   2.226 +        for (OutArcIt e(_graph, node); e != INVALID; ++e) {
   2.227 +          Node target = _graph.target(e);
   2.228 +
   2.229 +          if (order_map[source] < order_map[target] && tree_map[e]) {
   2.230 +            initFace(target, node_data, order_map, order_list);
   2.231 +          }
   2.232 +        }
   2.233 +
   2.234 +        for (OutArcIt e(_graph, node); e != INVALID; ++e) {
   2.235 +          Node target = _graph.target(e);
   2.236 +
   2.237 +          if (order_map[source] < order_map[target] && !tree_map[e]) {
   2.238 +            embed_arc[target] = true;
   2.239 +            walkUp(target, source, i, pred_map, low_map,
   2.240 +                   order_map, order_list, node_data, merge_roots);
   2.241 +          }
   2.242 +        }
   2.243 +
   2.244 +        for (typename MergeRoots::Value::iterator it =
   2.245 +               merge_roots[node].begin(); it != merge_roots[node].end(); ++it) {
   2.246 +          int rn = *it;
   2.247 +          walkDown(rn, i, node_data, order_list, child_lists,
   2.248 +                   ancestor_map, low_map, embed_arc, merge_roots);
   2.249 +        }
   2.250 +        merge_roots[node].clear();
   2.251 +
   2.252 +        for (OutArcIt e(_graph, node); e != INVALID; ++e) {
   2.253 +          Node target = _graph.target(e);
   2.254 +
   2.255 +          if (order_map[source] < order_map[target] && !tree_map[e]) {
   2.256 +            if (embed_arc[target]) {
   2.257 +              return false;
   2.258 +            }
   2.259 +          }
   2.260 +        }
   2.261 +      }
   2.262 +
   2.263 +      return true;
   2.264 +    }
   2.265 +
   2.266 +  private:
   2.267 +
   2.268 +    void createChildLists(const TreeMap& tree_map, const OrderMap& order_map,
   2.269 +                          const LowMap& low_map, ChildLists& child_lists) {
   2.270 +
   2.271 +      for (NodeIt n(_graph); n != INVALID; ++n) {
   2.272 +        Node source = n;
   2.273 +
   2.274 +        std::vector<Node> targets;
   2.275 +        for (OutArcIt e(_graph, n); e != INVALID; ++e) {
   2.276 +          Node target = _graph.target(e);
   2.277 +
   2.278 +          if (order_map[source] < order_map[target] && tree_map[e]) {
   2.279 +            targets.push_back(target);
   2.280 +          }
   2.281 +        }
   2.282 +
   2.283 +        if (targets.size() == 0) {
   2.284 +          child_lists[source].first = INVALID;
   2.285 +        } else if (targets.size() == 1) {
   2.286 +          child_lists[source].first = targets[0];
   2.287 +          child_lists[targets[0]].prev = INVALID;
   2.288 +          child_lists[targets[0]].next = INVALID;
   2.289 +        } else {
   2.290 +          radixSort(targets.begin(), targets.end(), mapToFunctor(low_map));
   2.291 +          for (int i = 1; i < int(targets.size()); ++i) {
   2.292 +            child_lists[targets[i]].prev = targets[i - 1];
   2.293 +            child_lists[targets[i - 1]].next = targets[i];
   2.294 +          }
   2.295 +          child_lists[targets.back()].next = INVALID;
   2.296 +          child_lists[targets.front()].prev = INVALID;
   2.297 +          child_lists[source].first = targets.front();
   2.298 +        }
   2.299 +      }
   2.300 +    }
   2.301 +
   2.302 +    void walkUp(const Node& node, Node root, int rorder,
   2.303 +                const PredMap& pred_map, const LowMap& low_map,
   2.304 +                const OrderMap& order_map, const OrderList& order_list,
   2.305 +                NodeData& node_data, MergeRoots& merge_roots) {
   2.306 +
   2.307 +      int na, nb;
   2.308 +      bool da, db;
   2.309 +
   2.310 +      na = nb = order_map[node];
   2.311 +      da = true; db = false;
   2.312 +
   2.313 +      while (true) {
   2.314 +
   2.315 +        if (node_data[na].visited == rorder) break;
   2.316 +        if (node_data[nb].visited == rorder) break;
   2.317 +
   2.318 +        node_data[na].visited = rorder;
   2.319 +        node_data[nb].visited = rorder;
   2.320 +
   2.321 +        int rn = -1;
   2.322 +
   2.323 +        if (na >= int(order_list.size())) {
   2.324 +          rn = na;
   2.325 +        } else if (nb >= int(order_list.size())) {
   2.326 +          rn = nb;
   2.327 +        }
   2.328 +
   2.329 +        if (rn == -1) {
   2.330 +          int nn;
   2.331 +
   2.332 +          nn = da ? node_data[na].prev : node_data[na].next;
   2.333 +          da = node_data[nn].prev != na;
   2.334 +          na = nn;
   2.335 +
   2.336 +          nn = db ? node_data[nb].prev : node_data[nb].next;
   2.337 +          db = node_data[nn].prev != nb;
   2.338 +          nb = nn;
   2.339 +
   2.340 +        } else {
   2.341 +
   2.342 +          Node rep = order_list[rn - order_list.size()];
   2.343 +          Node parent = _graph.source(pred_map[rep]);
   2.344 +
   2.345 +          if (low_map[rep] < rorder) {
   2.346 +            merge_roots[parent].push_back(rn);
   2.347 +          } else {
   2.348 +            merge_roots[parent].push_front(rn);
   2.349 +          }
   2.350 +
   2.351 +          if (parent != root) {
   2.352 +            na = nb = order_map[parent];
   2.353 +            da = true; db = false;
   2.354 +          } else {
   2.355 +            break;
   2.356 +          }
   2.357 +        }
   2.358 +      }
   2.359 +    }
   2.360 +
   2.361 +    void walkDown(int rn, int rorder, NodeData& node_data,
   2.362 +                  OrderList& order_list, ChildLists& child_lists,
   2.363 +                  AncestorMap& ancestor_map, LowMap& low_map,
   2.364 +                  EmbedArc& embed_arc, MergeRoots& merge_roots) {
   2.365 +
   2.366 +      std::vector<std::pair<int, bool> > merge_stack;
   2.367 +
   2.368 +      for (int di = 0; di < 2; ++di) {
   2.369 +        bool rd = di == 0;
   2.370 +        int pn = rn;
   2.371 +        int n = rd ? node_data[rn].next : node_data[rn].prev;
   2.372 +
   2.373 +        while (n != rn) {
   2.374 +
   2.375 +          Node node = order_list[n];
   2.376 +
   2.377 +          if (embed_arc[node]) {
   2.378 +
   2.379 +            // Merging components on the critical path
   2.380 +            while (!merge_stack.empty()) {
   2.381 +
   2.382 +              // Component root
   2.383 +              int cn = merge_stack.back().first;
   2.384 +              bool cd = merge_stack.back().second;
   2.385 +              merge_stack.pop_back();
   2.386 +
   2.387 +              // Parent of component
   2.388 +              int dn = merge_stack.back().first;
   2.389 +              bool dd = merge_stack.back().second;
   2.390 +              merge_stack.pop_back();
   2.391 +
   2.392 +              Node parent = order_list[dn];
   2.393 +
   2.394 +              // Erasing from merge_roots
   2.395 +              merge_roots[parent].pop_front();
   2.396 +
   2.397 +              Node child = order_list[cn - order_list.size()];
   2.398 +
   2.399 +              // Erasing from child_lists
   2.400 +              if (child_lists[child].prev != INVALID) {
   2.401 +                child_lists[child_lists[child].prev].next =
   2.402 +                  child_lists[child].next;
   2.403 +              } else {
   2.404 +                child_lists[parent].first = child_lists[child].next;
   2.405 +              }
   2.406 +
   2.407 +              if (child_lists[child].next != INVALID) {
   2.408 +                child_lists[child_lists[child].next].prev =
   2.409 +                  child_lists[child].prev;
   2.410 +              }
   2.411 +
   2.412 +              // Merging external faces
   2.413 +              {
   2.414 +                int en = cn;
   2.415 +                cn = cd ? node_data[cn].prev : node_data[cn].next;
   2.416 +                cd = node_data[cn].next == en;
   2.417 +
   2.418 +              }
   2.419 +
   2.420 +              if (cd) node_data[cn].next = dn; else node_data[cn].prev = dn;
   2.421 +              if (dd) node_data[dn].prev = cn; else node_data[dn].next = cn;
   2.422 +
   2.423 +            }
   2.424 +
   2.425 +            bool d = pn == node_data[n].prev;
   2.426 +
   2.427 +            if (node_data[n].prev == node_data[n].next &&
   2.428 +                node_data[n].inverted) {
   2.429 +              d = !d;
   2.430 +            }
   2.431 +
   2.432 +            // Embedding arc into external face
   2.433 +            if (rd) node_data[rn].next = n; else node_data[rn].prev = n;
   2.434 +            if (d) node_data[n].prev = rn; else node_data[n].next = rn;
   2.435 +            pn = rn;
   2.436 +
   2.437 +            embed_arc[order_list[n]] = false;
   2.438 +          }
   2.439 +
   2.440 +          if (!merge_roots[node].empty()) {
   2.441 +
   2.442 +            bool d = pn == node_data[n].prev;
   2.443 +
   2.444 +            merge_stack.push_back(std::make_pair(n, d));
   2.445 +
   2.446 +            int rn = merge_roots[node].front();
   2.447 +
   2.448 +            int xn = node_data[rn].next;
   2.449 +            Node xnode = order_list[xn];
   2.450 +
   2.451 +            int yn = node_data[rn].prev;
   2.452 +            Node ynode = order_list[yn];
   2.453 +
   2.454 +            bool rd;
   2.455 +            if (!external(xnode, rorder, child_lists, ancestor_map, low_map)) {
   2.456 +              rd = true;
   2.457 +            } else if (!external(ynode, rorder, child_lists,
   2.458 +                                 ancestor_map, low_map)) {
   2.459 +              rd = false;
   2.460 +            } else if (pertinent(xnode, embed_arc, merge_roots)) {
   2.461 +              rd = true;
   2.462 +            } else {
   2.463 +              rd = false;
   2.464 +            }
   2.465 +
   2.466 +            merge_stack.push_back(std::make_pair(rn, rd));
   2.467 +
   2.468 +            pn = rn;
   2.469 +            n = rd ? xn : yn;
   2.470 +
   2.471 +          } else if (!external(node, rorder, child_lists,
   2.472 +                               ancestor_map, low_map)) {
   2.473 +            int nn = (node_data[n].next != pn ?
   2.474 +                      node_data[n].next : node_data[n].prev);
   2.475 +
   2.476 +            bool nd = n == node_data[nn].prev;
   2.477 +
   2.478 +            if (nd) node_data[nn].prev = pn;
   2.479 +            else node_data[nn].next = pn;
   2.480 +
   2.481 +            if (n == node_data[pn].prev) node_data[pn].prev = nn;
   2.482 +            else node_data[pn].next = nn;
   2.483 +
   2.484 +            node_data[nn].inverted =
   2.485 +              (node_data[nn].prev == node_data[nn].next && nd != rd);
   2.486 +
   2.487 +            n = nn;
   2.488 +          }
   2.489 +          else break;
   2.490 +
   2.491 +        }
   2.492 +
   2.493 +        if (!merge_stack.empty() || n == rn) {
   2.494 +          break;
   2.495 +        }
   2.496 +      }
   2.497 +    }
   2.498 +
   2.499 +    void initFace(const Node& node, NodeData& node_data,
   2.500 +                  const OrderMap& order_map, const OrderList& order_list) {
   2.501 +      int n = order_map[node];
   2.502 +      int rn = n + order_list.size();
   2.503 +
   2.504 +      node_data[n].next = node_data[n].prev = rn;
   2.505 +      node_data[rn].next = node_data[rn].prev = n;
   2.506 +
   2.507 +      node_data[n].visited = order_list.size();
   2.508 +      node_data[rn].visited = order_list.size();
   2.509 +
   2.510 +    }
   2.511 +
   2.512 +    bool external(const Node& node, int rorder,
   2.513 +                  ChildLists& child_lists, AncestorMap& ancestor_map,
   2.514 +                  LowMap& low_map) {
   2.515 +      Node child = child_lists[node].first;
   2.516 +
   2.517 +      if (child != INVALID) {
   2.518 +        if (low_map[child] < rorder) return true;
   2.519 +      }
   2.520 +
   2.521 +      if (ancestor_map[node] < rorder) return true;
   2.522 +
   2.523 +      return false;
   2.524 +    }
   2.525 +
   2.526 +    bool pertinent(const Node& node, const EmbedArc& embed_arc,
   2.527 +                   const MergeRoots& merge_roots) {
   2.528 +      return !merge_roots[node].empty() || embed_arc[node];
   2.529 +    }
   2.530 +
   2.531 +  };
   2.532 +
   2.533 +  /// \ingroup planar
   2.534 +  ///
   2.535 +  /// \brief Planar embedding of an undirected simple graph
   2.536 +  ///
   2.537 +  /// This class implements the Boyer-Myrvold algorithm for planar
   2.538 +  /// embedding of an undirected graph. The planar embedding is an
   2.539 +  /// ordering of the outgoing edges of the nodes, which is a possible
   2.540 +  /// configuration to draw the graph in the plane. If there is not
   2.541 +  /// such ordering then the graph contains a \f$ K_5 \f$ (full graph
   2.542 +  /// with 5 nodes) or a \f$ K_{3,3} \f$ (complete bipartite graph on
   2.543 +  /// 3 ANode and 3 BNode) subdivision.
   2.544 +  ///
   2.545 +  /// The current implementation calculates either an embedding or a
   2.546 +  /// Kuratowski subdivision. The running time of the algorithm is 
   2.547 +  /// \f$ O(n) \f$.
   2.548 +  template <typename Graph>
   2.549 +  class PlanarEmbedding {
   2.550 +  private:
   2.551 +
   2.552 +    TEMPLATE_GRAPH_TYPEDEFS(Graph);
   2.553 +
   2.554 +    const Graph& _graph;
   2.555 +    typename Graph::template ArcMap<Arc> _embedding;
   2.556 +
   2.557 +    typename Graph::template EdgeMap<bool> _kuratowski;
   2.558 +
   2.559 +  private:
   2.560 +
   2.561 +    typedef typename Graph::template NodeMap<Arc> PredMap;
   2.562 +
   2.563 +    typedef typename Graph::template EdgeMap<bool> TreeMap;
   2.564 +
   2.565 +    typedef typename Graph::template NodeMap<int> OrderMap;
   2.566 +    typedef std::vector<Node> OrderList;
   2.567 +
   2.568 +    typedef typename Graph::template NodeMap<int> LowMap;
   2.569 +    typedef typename Graph::template NodeMap<int> AncestorMap;
   2.570 +
   2.571 +    typedef _planarity_bits::NodeDataNode<Graph> NodeDataNode;
   2.572 +    typedef std::vector<NodeDataNode> NodeData;
   2.573 +
   2.574 +    typedef _planarity_bits::ChildListNode<Graph> ChildListNode;
   2.575 +    typedef typename Graph::template NodeMap<ChildListNode> ChildLists;
   2.576 +
   2.577 +    typedef typename Graph::template NodeMap<std::list<int> > MergeRoots;
   2.578 +
   2.579 +    typedef typename Graph::template NodeMap<Arc> EmbedArc;
   2.580 +
   2.581 +    typedef _planarity_bits::ArcListNode<Graph> ArcListNode;
   2.582 +    typedef typename Graph::template ArcMap<ArcListNode> ArcLists;
   2.583 +
   2.584 +    typedef typename Graph::template NodeMap<bool> FlipMap;
   2.585 +
   2.586 +    typedef typename Graph::template NodeMap<int> TypeMap;
   2.587 +
   2.588 +    enum IsolatorNodeType {
   2.589 +      HIGHX = 6, LOWX = 7,
   2.590 +      HIGHY = 8, LOWY = 9,
   2.591 +      ROOT = 10, PERTINENT = 11,
   2.592 +      INTERNAL = 12
   2.593 +    };
   2.594 +
   2.595 +  public:
   2.596 +
   2.597 +    /// \brief The map for store of embedding
   2.598 +    typedef typename Graph::template ArcMap<Arc> EmbeddingMap;
   2.599 +
   2.600 +    /// \brief Constructor
   2.601 +    ///
   2.602 +    /// \note The graph should be simple, i.e. parallel and loop arc
   2.603 +    /// free.
   2.604 +    PlanarEmbedding(const Graph& graph)
   2.605 +      : _graph(graph), _embedding(_graph), _kuratowski(graph, false) {}
   2.606 +
   2.607 +    /// \brief Runs the algorithm.
   2.608 +    ///
   2.609 +    /// Runs the algorithm.
   2.610 +    /// \param kuratowski If the parameter is false, then the
   2.611 +    /// algorithm does not compute a Kuratowski subdivision.
   2.612 +    ///\return %True when the graph is planar.
   2.613 +    bool run(bool kuratowski = true) {
   2.614 +      typedef _planarity_bits::PlanarityVisitor<Graph> Visitor;
   2.615 +
   2.616 +      PredMap pred_map(_graph, INVALID);
   2.617 +      TreeMap tree_map(_graph, false);
   2.618 +
   2.619 +      OrderMap order_map(_graph, -1);
   2.620 +      OrderList order_list;
   2.621 +
   2.622 +      AncestorMap ancestor_map(_graph, -1);
   2.623 +      LowMap low_map(_graph, -1);
   2.624 +
   2.625 +      Visitor visitor(_graph, pred_map, tree_map,
   2.626 +                      order_map, order_list, ancestor_map, low_map);
   2.627 +      DfsVisit<Graph, Visitor> visit(_graph, visitor);
   2.628 +      visit.run();
   2.629 +
   2.630 +      ChildLists child_lists(_graph);
   2.631 +      createChildLists(tree_map, order_map, low_map, child_lists);
   2.632 +
   2.633 +      NodeData node_data(2 * order_list.size());
   2.634 +
   2.635 +      EmbedArc embed_arc(_graph, INVALID);
   2.636 +
   2.637 +      MergeRoots merge_roots(_graph);
   2.638 +
   2.639 +      ArcLists arc_lists(_graph);
   2.640 +
   2.641 +      FlipMap flip_map(_graph, false);
   2.642 +
   2.643 +      for (int i = order_list.size() - 1; i >= 0; --i) {
   2.644 +
   2.645 +        Node node = order_list[i];
   2.646 +
   2.647 +        node_data[i].first = INVALID;
   2.648 +
   2.649 +        Node source = node;
   2.650 +        for (OutArcIt e(_graph, node); e != INVALID; ++e) {
   2.651 +          Node target = _graph.target(e);
   2.652 +
   2.653 +          if (order_map[source] < order_map[target] && tree_map[e]) {
   2.654 +            initFace(target, arc_lists, node_data,
   2.655 +                     pred_map, order_map, order_list);
   2.656 +          }
   2.657 +        }
   2.658 +
   2.659 +        for (OutArcIt e(_graph, node); e != INVALID; ++e) {
   2.660 +          Node target = _graph.target(e);
   2.661 +
   2.662 +          if (order_map[source] < order_map[target] && !tree_map[e]) {
   2.663 +            embed_arc[target] = e;
   2.664 +            walkUp(target, source, i, pred_map, low_map,
   2.665 +                   order_map, order_list, node_data, merge_roots);
   2.666 +          }
   2.667 +        }
   2.668 +
   2.669 +        for (typename MergeRoots::Value::iterator it =
   2.670 +               merge_roots[node].begin(); it != merge_roots[node].end(); ++it) {
   2.671 +          int rn = *it;
   2.672 +          walkDown(rn, i, node_data, arc_lists, flip_map, order_list,
   2.673 +                   child_lists, ancestor_map, low_map, embed_arc, merge_roots);
   2.674 +        }
   2.675 +        merge_roots[node].clear();
   2.676 +
   2.677 +        for (OutArcIt e(_graph, node); e != INVALID; ++e) {
   2.678 +          Node target = _graph.target(e);
   2.679 +
   2.680 +          if (order_map[source] < order_map[target] && !tree_map[e]) {
   2.681 +            if (embed_arc[target] != INVALID) {
   2.682 +              if (kuratowski) {
   2.683 +                isolateKuratowski(e, node_data, arc_lists, flip_map,
   2.684 +                                  order_map, order_list, pred_map, child_lists,
   2.685 +                                  ancestor_map, low_map,
   2.686 +                                  embed_arc, merge_roots);
   2.687 +              }
   2.688 +              return false;
   2.689 +            }
   2.690 +          }
   2.691 +        }
   2.692 +      }
   2.693 +
   2.694 +      for (int i = 0; i < int(order_list.size()); ++i) {
   2.695 +
   2.696 +        mergeRemainingFaces(order_list[i], node_data, order_list, order_map,
   2.697 +                            child_lists, arc_lists);
   2.698 +        storeEmbedding(order_list[i], node_data, order_map, pred_map,
   2.699 +                       arc_lists, flip_map);
   2.700 +      }
   2.701 +
   2.702 +      return true;
   2.703 +    }
   2.704 +
   2.705 +    /// \brief Gives back the successor of an arc
   2.706 +    ///
   2.707 +    /// Gives back the successor of an arc. This function makes
   2.708 +    /// possible to query the cyclic order of the outgoing arcs from
   2.709 +    /// a node.
   2.710 +    Arc next(const Arc& arc) const {
   2.711 +      return _embedding[arc];
   2.712 +    }
   2.713 +
   2.714 +    /// \brief Gives back the calculated embedding map
   2.715 +    ///
   2.716 +    /// The returned map contains the successor of each arc in the
   2.717 +    /// graph.
   2.718 +    const EmbeddingMap& embedding() const {
   2.719 +      return _embedding;
   2.720 +    }
   2.721 +
   2.722 +    /// \brief Gives back true if the undirected arc is in the
   2.723 +    /// kuratowski subdivision
   2.724 +    ///
   2.725 +    /// Gives back true if the undirected arc is in the kuratowski
   2.726 +    /// subdivision
   2.727 +    /// \note The \c run() had to be called with true value.
   2.728 +    bool kuratowski(const Edge& edge) {
   2.729 +      return _kuratowski[edge];
   2.730 +    }
   2.731 +
   2.732 +  private:
   2.733 +
   2.734 +    void createChildLists(const TreeMap& tree_map, const OrderMap& order_map,
   2.735 +                          const LowMap& low_map, ChildLists& child_lists) {
   2.736 +
   2.737 +      for (NodeIt n(_graph); n != INVALID; ++n) {
   2.738 +        Node source = n;
   2.739 +
   2.740 +        std::vector<Node> targets;
   2.741 +        for (OutArcIt e(_graph, n); e != INVALID; ++e) {
   2.742 +          Node target = _graph.target(e);
   2.743 +
   2.744 +          if (order_map[source] < order_map[target] && tree_map[e]) {
   2.745 +            targets.push_back(target);
   2.746 +          }
   2.747 +        }
   2.748 +
   2.749 +        if (targets.size() == 0) {
   2.750 +          child_lists[source].first = INVALID;
   2.751 +        } else if (targets.size() == 1) {
   2.752 +          child_lists[source].first = targets[0];
   2.753 +          child_lists[targets[0]].prev = INVALID;
   2.754 +          child_lists[targets[0]].next = INVALID;
   2.755 +        } else {
   2.756 +          radixSort(targets.begin(), targets.end(), mapToFunctor(low_map));
   2.757 +          for (int i = 1; i < int(targets.size()); ++i) {
   2.758 +            child_lists[targets[i]].prev = targets[i - 1];
   2.759 +            child_lists[targets[i - 1]].next = targets[i];
   2.760 +          }
   2.761 +          child_lists[targets.back()].next = INVALID;
   2.762 +          child_lists[targets.front()].prev = INVALID;
   2.763 +          child_lists[source].first = targets.front();
   2.764 +        }
   2.765 +      }
   2.766 +    }
   2.767 +
   2.768 +    void walkUp(const Node& node, Node root, int rorder,
   2.769 +                const PredMap& pred_map, const LowMap& low_map,
   2.770 +                const OrderMap& order_map, const OrderList& order_list,
   2.771 +                NodeData& node_data, MergeRoots& merge_roots) {
   2.772 +
   2.773 +      int na, nb;
   2.774 +      bool da, db;
   2.775 +
   2.776 +      na = nb = order_map[node];
   2.777 +      da = true; db = false;
   2.778 +
   2.779 +      while (true) {
   2.780 +
   2.781 +        if (node_data[na].visited == rorder) break;
   2.782 +        if (node_data[nb].visited == rorder) break;
   2.783 +
   2.784 +        node_data[na].visited = rorder;
   2.785 +        node_data[nb].visited = rorder;
   2.786 +
   2.787 +        int rn = -1;
   2.788 +
   2.789 +        if (na >= int(order_list.size())) {
   2.790 +          rn = na;
   2.791 +        } else if (nb >= int(order_list.size())) {
   2.792 +          rn = nb;
   2.793 +        }
   2.794 +
   2.795 +        if (rn == -1) {
   2.796 +          int nn;
   2.797 +
   2.798 +          nn = da ? node_data[na].prev : node_data[na].next;
   2.799 +          da = node_data[nn].prev != na;
   2.800 +          na = nn;
   2.801 +
   2.802 +          nn = db ? node_data[nb].prev : node_data[nb].next;
   2.803 +          db = node_data[nn].prev != nb;
   2.804 +          nb = nn;
   2.805 +
   2.806 +        } else {
   2.807 +
   2.808 +          Node rep = order_list[rn - order_list.size()];
   2.809 +          Node parent = _graph.source(pred_map[rep]);
   2.810 +
   2.811 +          if (low_map[rep] < rorder) {
   2.812 +            merge_roots[parent].push_back(rn);
   2.813 +          } else {
   2.814 +            merge_roots[parent].push_front(rn);
   2.815 +          }
   2.816 +
   2.817 +          if (parent != root) {
   2.818 +            na = nb = order_map[parent];
   2.819 +            da = true; db = false;
   2.820 +          } else {
   2.821 +            break;
   2.822 +          }
   2.823 +        }
   2.824 +      }
   2.825 +    }
   2.826 +
   2.827 +    void walkDown(int rn, int rorder, NodeData& node_data,
   2.828 +                  ArcLists& arc_lists, FlipMap& flip_map,
   2.829 +                  OrderList& order_list, ChildLists& child_lists,
   2.830 +                  AncestorMap& ancestor_map, LowMap& low_map,
   2.831 +                  EmbedArc& embed_arc, MergeRoots& merge_roots) {
   2.832 +
   2.833 +      std::vector<std::pair<int, bool> > merge_stack;
   2.834 +
   2.835 +      for (int di = 0; di < 2; ++di) {
   2.836 +        bool rd = di == 0;
   2.837 +        int pn = rn;
   2.838 +        int n = rd ? node_data[rn].next : node_data[rn].prev;
   2.839 +
   2.840 +        while (n != rn) {
   2.841 +
   2.842 +          Node node = order_list[n];
   2.843 +
   2.844 +          if (embed_arc[node] != INVALID) {
   2.845 +
   2.846 +            // Merging components on the critical path
   2.847 +            while (!merge_stack.empty()) {
   2.848 +
   2.849 +              // Component root
   2.850 +              int cn = merge_stack.back().first;
   2.851 +              bool cd = merge_stack.back().second;
   2.852 +              merge_stack.pop_back();
   2.853 +
   2.854 +              // Parent of component
   2.855 +              int dn = merge_stack.back().first;
   2.856 +              bool dd = merge_stack.back().second;
   2.857 +              merge_stack.pop_back();
   2.858 +
   2.859 +              Node parent = order_list[dn];
   2.860 +
   2.861 +              // Erasing from merge_roots
   2.862 +              merge_roots[parent].pop_front();
   2.863 +
   2.864 +              Node child = order_list[cn - order_list.size()];
   2.865 +
   2.866 +              // Erasing from child_lists
   2.867 +              if (child_lists[child].prev != INVALID) {
   2.868 +                child_lists[child_lists[child].prev].next =
   2.869 +                  child_lists[child].next;
   2.870 +              } else {
   2.871 +                child_lists[parent].first = child_lists[child].next;
   2.872 +              }
   2.873 +
   2.874 +              if (child_lists[child].next != INVALID) {
   2.875 +                child_lists[child_lists[child].next].prev =
   2.876 +                  child_lists[child].prev;
   2.877 +              }
   2.878 +
   2.879 +              // Merging arcs + flipping
   2.880 +              Arc de = node_data[dn].first;
   2.881 +              Arc ce = node_data[cn].first;
   2.882 +
   2.883 +              flip_map[order_list[cn - order_list.size()]] = cd != dd;
   2.884 +              if (cd != dd) {
   2.885 +                std::swap(arc_lists[ce].prev, arc_lists[ce].next);
   2.886 +                ce = arc_lists[ce].prev;
   2.887 +                std::swap(arc_lists[ce].prev, arc_lists[ce].next);
   2.888 +              }
   2.889 +
   2.890 +              {
   2.891 +                Arc dne = arc_lists[de].next;
   2.892 +                Arc cne = arc_lists[ce].next;
   2.893 +
   2.894 +                arc_lists[de].next = cne;
   2.895 +                arc_lists[ce].next = dne;
   2.896 +
   2.897 +                arc_lists[dne].prev = ce;
   2.898 +                arc_lists[cne].prev = de;
   2.899 +              }
   2.900 +
   2.901 +              if (dd) {
   2.902 +                node_data[dn].first = ce;
   2.903 +              }
   2.904 +
   2.905 +              // Merging external faces
   2.906 +              {
   2.907 +                int en = cn;
   2.908 +                cn = cd ? node_data[cn].prev : node_data[cn].next;
   2.909 +                cd = node_data[cn].next == en;
   2.910 +
   2.911 +                 if (node_data[cn].prev == node_data[cn].next &&
   2.912 +                    node_data[cn].inverted) {
   2.913 +                   cd = !cd;
   2.914 +                 }
   2.915 +              }
   2.916 +
   2.917 +              if (cd) node_data[cn].next = dn; else node_data[cn].prev = dn;
   2.918 +              if (dd) node_data[dn].prev = cn; else node_data[dn].next = cn;
   2.919 +
   2.920 +            }
   2.921 +
   2.922 +            bool d = pn == node_data[n].prev;
   2.923 +
   2.924 +            if (node_data[n].prev == node_data[n].next &&
   2.925 +                node_data[n].inverted) {
   2.926 +              d = !d;
   2.927 +            }
   2.928 +
   2.929 +            // Add new arc
   2.930 +            {
   2.931 +              Arc arc = embed_arc[node];
   2.932 +              Arc re = node_data[rn].first;
   2.933 +
   2.934 +              arc_lists[arc_lists[re].next].prev = arc;
   2.935 +              arc_lists[arc].next = arc_lists[re].next;
   2.936 +              arc_lists[arc].prev = re;
   2.937 +              arc_lists[re].next = arc;
   2.938 +
   2.939 +              if (!rd) {
   2.940 +                node_data[rn].first = arc;
   2.941 +              }
   2.942 +
   2.943 +              Arc rev = _graph.oppositeArc(arc);
   2.944 +              Arc e = node_data[n].first;
   2.945 +
   2.946 +              arc_lists[arc_lists[e].next].prev = rev;
   2.947 +              arc_lists[rev].next = arc_lists[e].next;
   2.948 +              arc_lists[rev].prev = e;
   2.949 +              arc_lists[e].next = rev;
   2.950 +
   2.951 +              if (d) {
   2.952 +                node_data[n].first = rev;
   2.953 +              }
   2.954 +
   2.955 +            }
   2.956 +
   2.957 +            // Embedding arc into external face
   2.958 +            if (rd) node_data[rn].next = n; else node_data[rn].prev = n;
   2.959 +            if (d) node_data[n].prev = rn; else node_data[n].next = rn;
   2.960 +            pn = rn;
   2.961 +
   2.962 +            embed_arc[order_list[n]] = INVALID;
   2.963 +          }
   2.964 +
   2.965 +          if (!merge_roots[node].empty()) {
   2.966 +
   2.967 +            bool d = pn == node_data[n].prev;
   2.968 +            if (node_data[n].prev == node_data[n].next &&
   2.969 +                node_data[n].inverted) {
   2.970 +              d = !d;
   2.971 +            }
   2.972 +
   2.973 +            merge_stack.push_back(std::make_pair(n, d));
   2.974 +
   2.975 +            int rn = merge_roots[node].front();
   2.976 +
   2.977 +            int xn = node_data[rn].next;
   2.978 +            Node xnode = order_list[xn];
   2.979 +
   2.980 +            int yn = node_data[rn].prev;
   2.981 +            Node ynode = order_list[yn];
   2.982 +
   2.983 +            bool rd;
   2.984 +            if (!external(xnode, rorder, child_lists, ancestor_map, low_map)) {
   2.985 +              rd = true;
   2.986 +            } else if (!external(ynode, rorder, child_lists,
   2.987 +                                 ancestor_map, low_map)) {
   2.988 +              rd = false;
   2.989 +            } else if (pertinent(xnode, embed_arc, merge_roots)) {
   2.990 +              rd = true;
   2.991 +            } else {
   2.992 +              rd = false;
   2.993 +            }
   2.994 +
   2.995 +            merge_stack.push_back(std::make_pair(rn, rd));
   2.996 +
   2.997 +            pn = rn;
   2.998 +            n = rd ? xn : yn;
   2.999 +
  2.1000 +          } else if (!external(node, rorder, child_lists,
  2.1001 +                               ancestor_map, low_map)) {
  2.1002 +            int nn = (node_data[n].next != pn ?
  2.1003 +                      node_data[n].next : node_data[n].prev);
  2.1004 +
  2.1005 +            bool nd = n == node_data[nn].prev;
  2.1006 +
  2.1007 +            if (nd) node_data[nn].prev = pn;
  2.1008 +            else node_data[nn].next = pn;
  2.1009 +
  2.1010 +            if (n == node_data[pn].prev) node_data[pn].prev = nn;
  2.1011 +            else node_data[pn].next = nn;
  2.1012 +
  2.1013 +            node_data[nn].inverted =
  2.1014 +              (node_data[nn].prev == node_data[nn].next && nd != rd);
  2.1015 +
  2.1016 +            n = nn;
  2.1017 +          }
  2.1018 +          else break;
  2.1019 +
  2.1020 +        }
  2.1021 +
  2.1022 +        if (!merge_stack.empty() || n == rn) {
  2.1023 +          break;
  2.1024 +        }
  2.1025 +      }
  2.1026 +    }
  2.1027 +
  2.1028 +    void initFace(const Node& node, ArcLists& arc_lists,
  2.1029 +                  NodeData& node_data, const PredMap& pred_map,
  2.1030 +                  const OrderMap& order_map, const OrderList& order_list) {
  2.1031 +      int n = order_map[node];
  2.1032 +      int rn = n + order_list.size();
  2.1033 +
  2.1034 +      node_data[n].next = node_data[n].prev = rn;
  2.1035 +      node_data[rn].next = node_data[rn].prev = n;
  2.1036 +
  2.1037 +      node_data[n].visited = order_list.size();
  2.1038 +      node_data[rn].visited = order_list.size();
  2.1039 +
  2.1040 +      node_data[n].inverted = false;
  2.1041 +      node_data[rn].inverted = false;
  2.1042 +
  2.1043 +      Arc arc = pred_map[node];
  2.1044 +      Arc rev = _graph.oppositeArc(arc);
  2.1045 +
  2.1046 +      node_data[rn].first = arc;
  2.1047 +      node_data[n].first = rev;
  2.1048 +
  2.1049 +      arc_lists[arc].prev = arc;
  2.1050 +      arc_lists[arc].next = arc;
  2.1051 +
  2.1052 +      arc_lists[rev].prev = rev;
  2.1053 +      arc_lists[rev].next = rev;
  2.1054 +
  2.1055 +    }
  2.1056 +
  2.1057 +    void mergeRemainingFaces(const Node& node, NodeData& node_data,
  2.1058 +                             OrderList& order_list, OrderMap& order_map,
  2.1059 +                             ChildLists& child_lists, ArcLists& arc_lists) {
  2.1060 +      while (child_lists[node].first != INVALID) {
  2.1061 +        int dd = order_map[node];
  2.1062 +        Node child = child_lists[node].first;
  2.1063 +        int cd = order_map[child] + order_list.size();
  2.1064 +        child_lists[node].first = child_lists[child].next;
  2.1065 +
  2.1066 +        Arc de = node_data[dd].first;
  2.1067 +        Arc ce = node_data[cd].first;
  2.1068 +
  2.1069 +        if (de != INVALID) {
  2.1070 +          Arc dne = arc_lists[de].next;
  2.1071 +          Arc cne = arc_lists[ce].next;
  2.1072 +
  2.1073 +          arc_lists[de].next = cne;
  2.1074 +          arc_lists[ce].next = dne;
  2.1075 +
  2.1076 +          arc_lists[dne].prev = ce;
  2.1077 +          arc_lists[cne].prev = de;
  2.1078 +        }
  2.1079 +
  2.1080 +        node_data[dd].first = ce;
  2.1081 +
  2.1082 +      }
  2.1083 +    }
  2.1084 +
  2.1085 +    void storeEmbedding(const Node& node, NodeData& node_data,
  2.1086 +                        OrderMap& order_map, PredMap& pred_map,
  2.1087 +                        ArcLists& arc_lists, FlipMap& flip_map) {
  2.1088 +
  2.1089 +      if (node_data[order_map[node]].first == INVALID) return;
  2.1090 +
  2.1091 +      if (pred_map[node] != INVALID) {
  2.1092 +        Node source = _graph.source(pred_map[node]);
  2.1093 +        flip_map[node] = flip_map[node] != flip_map[source];
  2.1094 +      }
  2.1095 +
  2.1096 +      Arc first = node_data[order_map[node]].first;
  2.1097 +      Arc prev = first;
  2.1098 +
  2.1099 +      Arc arc = flip_map[node] ?
  2.1100 +        arc_lists[prev].prev : arc_lists[prev].next;
  2.1101 +
  2.1102 +      _embedding[prev] = arc;
  2.1103 +
  2.1104 +      while (arc != first) {
  2.1105 +        Arc next = arc_lists[arc].prev == prev ?
  2.1106 +          arc_lists[arc].next : arc_lists[arc].prev;
  2.1107 +        prev = arc; arc = next;
  2.1108 +        _embedding[prev] = arc;
  2.1109 +      }
  2.1110 +    }
  2.1111 +
  2.1112 +
  2.1113 +    bool external(const Node& node, int rorder,
  2.1114 +                  ChildLists& child_lists, AncestorMap& ancestor_map,
  2.1115 +                  LowMap& low_map) {
  2.1116 +      Node child = child_lists[node].first;
  2.1117 +
  2.1118 +      if (child != INVALID) {
  2.1119 +        if (low_map[child] < rorder) return true;
  2.1120 +      }
  2.1121 +
  2.1122 +      if (ancestor_map[node] < rorder) return true;
  2.1123 +
  2.1124 +      return false;
  2.1125 +    }
  2.1126 +
  2.1127 +    bool pertinent(const Node& node, const EmbedArc& embed_arc,
  2.1128 +                   const MergeRoots& merge_roots) {
  2.1129 +      return !merge_roots[node].empty() || embed_arc[node] != INVALID;
  2.1130 +    }
  2.1131 +
  2.1132 +    int lowPoint(const Node& node, OrderMap& order_map, ChildLists& child_lists,
  2.1133 +                 AncestorMap& ancestor_map, LowMap& low_map) {
  2.1134 +      int low_point;
  2.1135 +
  2.1136 +      Node child = child_lists[node].first;
  2.1137 +
  2.1138 +      if (child != INVALID) {
  2.1139 +        low_point = low_map[child];
  2.1140 +      } else {
  2.1141 +        low_point = order_map[node];
  2.1142 +      }
  2.1143 +
  2.1144 +      if (low_point > ancestor_map[node]) {
  2.1145 +        low_point = ancestor_map[node];
  2.1146 +      }
  2.1147 +
  2.1148 +      return low_point;
  2.1149 +    }
  2.1150 +
  2.1151 +    int findComponentRoot(Node root, Node node, ChildLists& child_lists,
  2.1152 +                          OrderMap& order_map, OrderList& order_list) {
  2.1153 +
  2.1154 +      int order = order_map[root];
  2.1155 +      int norder = order_map[node];
  2.1156 +
  2.1157 +      Node child = child_lists[root].first;
  2.1158 +      while (child != INVALID) {
  2.1159 +        int corder = order_map[child];
  2.1160 +        if (corder > order && corder < norder) {
  2.1161 +          order = corder;
  2.1162 +        }
  2.1163 +        child = child_lists[child].next;
  2.1164 +      }
  2.1165 +      return order + order_list.size();
  2.1166 +    }
  2.1167 +
  2.1168 +    Node findPertinent(Node node, OrderMap& order_map, NodeData& node_data,
  2.1169 +                       EmbedArc& embed_arc, MergeRoots& merge_roots) {
  2.1170 +      Node wnode =_graph.target(node_data[order_map[node]].first);
  2.1171 +      while (!pertinent(wnode, embed_arc, merge_roots)) {
  2.1172 +        wnode = _graph.target(node_data[order_map[wnode]].first);
  2.1173 +      }
  2.1174 +      return wnode;
  2.1175 +    }
  2.1176 +
  2.1177 +
  2.1178 +    Node findExternal(Node node, int rorder, OrderMap& order_map,
  2.1179 +                      ChildLists& child_lists, AncestorMap& ancestor_map,
  2.1180 +                      LowMap& low_map, NodeData& node_data) {
  2.1181 +      Node wnode =_graph.target(node_data[order_map[node]].first);
  2.1182 +      while (!external(wnode, rorder, child_lists, ancestor_map, low_map)) {
  2.1183 +        wnode = _graph.target(node_data[order_map[wnode]].first);
  2.1184 +      }
  2.1185 +      return wnode;
  2.1186 +    }
  2.1187 +
  2.1188 +    void markCommonPath(Node node, int rorder, Node& wnode, Node& znode,
  2.1189 +                        OrderList& order_list, OrderMap& order_map,
  2.1190 +                        NodeData& node_data, ArcLists& arc_lists,
  2.1191 +                        EmbedArc& embed_arc, MergeRoots& merge_roots,
  2.1192 +                        ChildLists& child_lists, AncestorMap& ancestor_map,
  2.1193 +                        LowMap& low_map) {
  2.1194 +
  2.1195 +      Node cnode = node;
  2.1196 +      Node pred = INVALID;
  2.1197 +
  2.1198 +      while (true) {
  2.1199 +
  2.1200 +        bool pert = pertinent(cnode, embed_arc, merge_roots);
  2.1201 +        bool ext = external(cnode, rorder, child_lists, ancestor_map, low_map);
  2.1202 +
  2.1203 +        if (pert && ext) {
  2.1204 +          if (!merge_roots[cnode].empty()) {
  2.1205 +            int cn = merge_roots[cnode].back();
  2.1206 +
  2.1207 +            if (low_map[order_list[cn - order_list.size()]] < rorder) {
  2.1208 +              Arc arc = node_data[cn].first;
  2.1209 +              _kuratowski.set(arc, true);
  2.1210 +
  2.1211 +              pred = cnode;
  2.1212 +              cnode = _graph.target(arc);
  2.1213 +
  2.1214 +              continue;
  2.1215 +            }
  2.1216 +          }
  2.1217 +          wnode = znode = cnode;
  2.1218 +          return;
  2.1219 +
  2.1220 +        } else if (pert) {
  2.1221 +          wnode = cnode;
  2.1222 +
  2.1223 +          while (!external(cnode, rorder, child_lists, ancestor_map, low_map)) {
  2.1224 +            Arc arc = node_data[order_map[cnode]].first;
  2.1225 +
  2.1226 +            if (_graph.target(arc) == pred) {
  2.1227 +              arc = arc_lists[arc].next;
  2.1228 +            }
  2.1229 +            _kuratowski.set(arc, true);
  2.1230 +
  2.1231 +            Node next = _graph.target(arc);
  2.1232 +            pred = cnode; cnode = next;
  2.1233 +          }
  2.1234 +
  2.1235 +          znode = cnode;
  2.1236 +          return;
  2.1237 +
  2.1238 +        } else if (ext) {
  2.1239 +          znode = cnode;
  2.1240 +
  2.1241 +          while (!pertinent(cnode, embed_arc, merge_roots)) {
  2.1242 +            Arc arc = node_data[order_map[cnode]].first;
  2.1243 +
  2.1244 +            if (_graph.target(arc) == pred) {
  2.1245 +              arc = arc_lists[arc].next;
  2.1246 +            }
  2.1247 +            _kuratowski.set(arc, true);
  2.1248 +
  2.1249 +            Node next = _graph.target(arc);
  2.1250 +            pred = cnode; cnode = next;
  2.1251 +          }
  2.1252 +
  2.1253 +          wnode = cnode;
  2.1254 +          return;
  2.1255 +
  2.1256 +        } else {
  2.1257 +          Arc arc = node_data[order_map[cnode]].first;
  2.1258 +
  2.1259 +          if (_graph.target(arc) == pred) {
  2.1260 +            arc = arc_lists[arc].next;
  2.1261 +          }
  2.1262 +          _kuratowski.set(arc, true);
  2.1263 +
  2.1264 +          Node next = _graph.target(arc);
  2.1265 +          pred = cnode; cnode = next;
  2.1266 +        }
  2.1267 +
  2.1268 +      }
  2.1269 +
  2.1270 +    }
  2.1271 +
  2.1272 +    void orientComponent(Node root, int rn, OrderMap& order_map,
  2.1273 +                         PredMap& pred_map, NodeData& node_data,
  2.1274 +                         ArcLists& arc_lists, FlipMap& flip_map,
  2.1275 +                         TypeMap& type_map) {
  2.1276 +      node_data[order_map[root]].first = node_data[rn].first;
  2.1277 +      type_map[root] = 1;
  2.1278 +
  2.1279 +      std::vector<Node> st, qu;
  2.1280 +
  2.1281 +      st.push_back(root);
  2.1282 +      while (!st.empty()) {
  2.1283 +        Node node = st.back();
  2.1284 +        st.pop_back();
  2.1285 +        qu.push_back(node);
  2.1286 +
  2.1287 +        Arc arc = node_data[order_map[node]].first;
  2.1288 +
  2.1289 +        if (type_map[_graph.target(arc)] == 0) {
  2.1290 +          st.push_back(_graph.target(arc));
  2.1291 +          type_map[_graph.target(arc)] = 1;
  2.1292 +        }
  2.1293 +
  2.1294 +        Arc last = arc, pred = arc;
  2.1295 +        arc = arc_lists[arc].next;
  2.1296 +        while (arc != last) {
  2.1297 +
  2.1298 +          if (type_map[_graph.target(arc)] == 0) {
  2.1299 +            st.push_back(_graph.target(arc));
  2.1300 +            type_map[_graph.target(arc)] = 1;
  2.1301 +          }
  2.1302 +
  2.1303 +          Arc next = arc_lists[arc].next != pred ?
  2.1304 +            arc_lists[arc].next : arc_lists[arc].prev;
  2.1305 +          pred = arc; arc = next;
  2.1306 +        }
  2.1307 +
  2.1308 +      }
  2.1309 +
  2.1310 +      type_map[root] = 2;
  2.1311 +      flip_map[root] = false;
  2.1312 +
  2.1313 +      for (int i = 1; i < int(qu.size()); ++i) {
  2.1314 +
  2.1315 +        Node node = qu[i];
  2.1316 +
  2.1317 +        while (type_map[node] != 2) {
  2.1318 +          st.push_back(node);
  2.1319 +          type_map[node] = 2;
  2.1320 +          node = _graph.source(pred_map[node]);
  2.1321 +        }
  2.1322 +
  2.1323 +        bool flip = flip_map[node];
  2.1324 +
  2.1325 +        while (!st.empty()) {
  2.1326 +          node = st.back();
  2.1327 +          st.pop_back();
  2.1328 +
  2.1329 +          flip_map[node] = flip != flip_map[node];
  2.1330 +          flip = flip_map[node];
  2.1331 +
  2.1332 +          if (flip) {
  2.1333 +            Arc arc = node_data[order_map[node]].first;
  2.1334 +            std::swap(arc_lists[arc].prev, arc_lists[arc].next);
  2.1335 +            arc = arc_lists[arc].prev;
  2.1336 +            std::swap(arc_lists[arc].prev, arc_lists[arc].next);
  2.1337 +            node_data[order_map[node]].first = arc;
  2.1338 +          }
  2.1339 +        }
  2.1340 +      }
  2.1341 +
  2.1342 +      for (int i = 0; i < int(qu.size()); ++i) {
  2.1343 +
  2.1344 +        Arc arc = node_data[order_map[qu[i]]].first;
  2.1345 +        Arc last = arc, pred = arc;
  2.1346 +
  2.1347 +        arc = arc_lists[arc].next;
  2.1348 +        while (arc != last) {
  2.1349 +
  2.1350 +          if (arc_lists[arc].next == pred) {
  2.1351 +            std::swap(arc_lists[arc].next, arc_lists[arc].prev);
  2.1352 +          }
  2.1353 +          pred = arc; arc = arc_lists[arc].next;
  2.1354 +        }
  2.1355 +
  2.1356 +      }
  2.1357 +    }
  2.1358 +
  2.1359 +    void setFaceFlags(Node root, Node wnode, Node ynode, Node xnode,
  2.1360 +                      OrderMap& order_map, NodeData& node_data,
  2.1361 +                      TypeMap& type_map) {
  2.1362 +      Node node = _graph.target(node_data[order_map[root]].first);
  2.1363 +
  2.1364 +      while (node != ynode) {
  2.1365 +        type_map[node] = HIGHY;
  2.1366 +        node = _graph.target(node_data[order_map[node]].first);
  2.1367 +      }
  2.1368 +
  2.1369 +      while (node != wnode) {
  2.1370 +        type_map[node] = LOWY;
  2.1371 +        node = _graph.target(node_data[order_map[node]].first);
  2.1372 +      }
  2.1373 +
  2.1374 +      node = _graph.target(node_data[order_map[wnode]].first);
  2.1375 +
  2.1376 +      while (node != xnode) {
  2.1377 +        type_map[node] = LOWX;
  2.1378 +        node = _graph.target(node_data[order_map[node]].first);
  2.1379 +      }
  2.1380 +      type_map[node] = LOWX;
  2.1381 +
  2.1382 +      node = _graph.target(node_data[order_map[xnode]].first);
  2.1383 +      while (node != root) {
  2.1384 +        type_map[node] = HIGHX;
  2.1385 +        node = _graph.target(node_data[order_map[node]].first);
  2.1386 +      }
  2.1387 +
  2.1388 +      type_map[wnode] = PERTINENT;
  2.1389 +      type_map[root] = ROOT;
  2.1390 +    }
  2.1391 +
  2.1392 +    void findInternalPath(std::vector<Arc>& ipath,
  2.1393 +                          Node wnode, Node root, TypeMap& type_map,
  2.1394 +                          OrderMap& order_map, NodeData& node_data,
  2.1395 +                          ArcLists& arc_lists) {
  2.1396 +      std::vector<Arc> st;
  2.1397 +
  2.1398 +      Node node = wnode;
  2.1399 +
  2.1400 +      while (node != root) {
  2.1401 +        Arc arc = arc_lists[node_data[order_map[node]].first].next;
  2.1402 +        st.push_back(arc);
  2.1403 +        node = _graph.target(arc);
  2.1404 +      }
  2.1405 +
  2.1406 +      while (true) {
  2.1407 +        Arc arc = st.back();
  2.1408 +        if (type_map[_graph.target(arc)] == LOWX ||
  2.1409 +            type_map[_graph.target(arc)] == HIGHX) {
  2.1410 +          break;
  2.1411 +        }
  2.1412 +        if (type_map[_graph.target(arc)] == 2) {
  2.1413 +          type_map[_graph.target(arc)] = 3;
  2.1414 +
  2.1415 +          arc = arc_lists[_graph.oppositeArc(arc)].next;
  2.1416 +          st.push_back(arc);
  2.1417 +        } else {
  2.1418 +          st.pop_back();
  2.1419 +          arc = arc_lists[arc].next;
  2.1420 +
  2.1421 +          while (_graph.oppositeArc(arc) == st.back()) {
  2.1422 +            arc = st.back();
  2.1423 +            st.pop_back();
  2.1424 +            arc = arc_lists[arc].next;
  2.1425 +          }
  2.1426 +          st.push_back(arc);
  2.1427 +        }
  2.1428 +      }
  2.1429 +
  2.1430 +      for (int i = 0; i < int(st.size()); ++i) {
  2.1431 +        if (type_map[_graph.target(st[i])] != LOWY &&
  2.1432 +            type_map[_graph.target(st[i])] != HIGHY) {
  2.1433 +          for (; i < int(st.size()); ++i) {
  2.1434 +            ipath.push_back(st[i]);
  2.1435 +          }
  2.1436 +        }
  2.1437 +      }
  2.1438 +    }
  2.1439 +
  2.1440 +    void setInternalFlags(std::vector<Arc>& ipath, TypeMap& type_map) {
  2.1441 +      for (int i = 1; i < int(ipath.size()); ++i) {
  2.1442 +        type_map[_graph.source(ipath[i])] = INTERNAL;
  2.1443 +      }
  2.1444 +    }
  2.1445 +
  2.1446 +    void findPilePath(std::vector<Arc>& ppath,
  2.1447 +                      Node root, TypeMap& type_map, OrderMap& order_map,
  2.1448 +                      NodeData& node_data, ArcLists& arc_lists) {
  2.1449 +      std::vector<Arc> st;
  2.1450 +
  2.1451 +      st.push_back(_graph.oppositeArc(node_data[order_map[root]].first));
  2.1452 +      st.push_back(node_data[order_map[root]].first);
  2.1453 +
  2.1454 +      while (st.size() > 1) {
  2.1455 +        Arc arc = st.back();
  2.1456 +        if (type_map[_graph.target(arc)] == INTERNAL) {
  2.1457 +          break;
  2.1458 +        }
  2.1459 +        if (type_map[_graph.target(arc)] == 3) {
  2.1460 +          type_map[_graph.target(arc)] = 4;
  2.1461 +
  2.1462 +          arc = arc_lists[_graph.oppositeArc(arc)].next;
  2.1463 +          st.push_back(arc);
  2.1464 +        } else {
  2.1465 +          st.pop_back();
  2.1466 +          arc = arc_lists[arc].next;
  2.1467 +
  2.1468 +          while (!st.empty() && _graph.oppositeArc(arc) == st.back()) {
  2.1469 +            arc = st.back();
  2.1470 +            st.pop_back();
  2.1471 +            arc = arc_lists[arc].next;
  2.1472 +          }
  2.1473 +          st.push_back(arc);
  2.1474 +        }
  2.1475 +      }
  2.1476 +
  2.1477 +      for (int i = 1; i < int(st.size()); ++i) {
  2.1478 +        ppath.push_back(st[i]);
  2.1479 +      }
  2.1480 +    }
  2.1481 +
  2.1482 +
  2.1483 +    int markExternalPath(Node node, OrderMap& order_map,
  2.1484 +                         ChildLists& child_lists, PredMap& pred_map,
  2.1485 +                         AncestorMap& ancestor_map, LowMap& low_map) {
  2.1486 +      int lp = lowPoint(node, order_map, child_lists,
  2.1487 +                        ancestor_map, low_map);
  2.1488 +
  2.1489 +      if (ancestor_map[node] != lp) {
  2.1490 +        node = child_lists[node].first;
  2.1491 +        _kuratowski[pred_map[node]] = true;
  2.1492 +
  2.1493 +        while (ancestor_map[node] != lp) {
  2.1494 +          for (OutArcIt e(_graph, node); e != INVALID; ++e) {
  2.1495 +            Node tnode = _graph.target(e);
  2.1496 +            if (order_map[tnode] > order_map[node] && low_map[tnode] == lp) {
  2.1497 +              node = tnode;
  2.1498 +              _kuratowski[e] = true;
  2.1499 +              break;
  2.1500 +            }
  2.1501 +          }
  2.1502 +        }
  2.1503 +      }
  2.1504 +
  2.1505 +      for (OutArcIt e(_graph, node); e != INVALID; ++e) {
  2.1506 +        if (order_map[_graph.target(e)] == lp) {
  2.1507 +          _kuratowski[e] = true;
  2.1508 +          break;
  2.1509 +        }
  2.1510 +      }
  2.1511 +
  2.1512 +      return lp;
  2.1513 +    }
  2.1514 +
  2.1515 +    void markPertinentPath(Node node, OrderMap& order_map,
  2.1516 +                           NodeData& node_data, ArcLists& arc_lists,
  2.1517 +                           EmbedArc& embed_arc, MergeRoots& merge_roots) {
  2.1518 +      while (embed_arc[node] == INVALID) {
  2.1519 +        int n = merge_roots[node].front();
  2.1520 +        Arc arc = node_data[n].first;
  2.1521 +
  2.1522 +        _kuratowski.set(arc, true);
  2.1523 +
  2.1524 +        Node pred = node;
  2.1525 +        node = _graph.target(arc);
  2.1526 +        while (!pertinent(node, embed_arc, merge_roots)) {
  2.1527 +          arc = node_data[order_map[node]].first;
  2.1528 +          if (_graph.target(arc) == pred) {
  2.1529 +            arc = arc_lists[arc].next;
  2.1530 +          }
  2.1531 +          _kuratowski.set(arc, true);
  2.1532 +          pred = node;
  2.1533 +          node = _graph.target(arc);
  2.1534 +        }
  2.1535 +      }
  2.1536 +      _kuratowski.set(embed_arc[node], true);
  2.1537 +    }
  2.1538 +
  2.1539 +    void markPredPath(Node node, Node snode, PredMap& pred_map) {
  2.1540 +      while (node != snode) {
  2.1541 +        _kuratowski.set(pred_map[node], true);
  2.1542 +        node = _graph.source(pred_map[node]);
  2.1543 +      }
  2.1544 +    }
  2.1545 +
  2.1546 +    void markFacePath(Node ynode, Node xnode,
  2.1547 +                      OrderMap& order_map, NodeData& node_data) {
  2.1548 +      Arc arc = node_data[order_map[ynode]].first;
  2.1549 +      Node node = _graph.target(arc);
  2.1550 +      _kuratowski.set(arc, true);
  2.1551 +
  2.1552 +      while (node != xnode) {
  2.1553 +        arc = node_data[order_map[node]].first;
  2.1554 +        _kuratowski.set(arc, true);
  2.1555 +        node = _graph.target(arc);
  2.1556 +      }
  2.1557 +    }
  2.1558 +
  2.1559 +    void markInternalPath(std::vector<Arc>& path) {
  2.1560 +      for (int i = 0; i < int(path.size()); ++i) {
  2.1561 +        _kuratowski.set(path[i], true);
  2.1562 +      }
  2.1563 +    }
  2.1564 +
  2.1565 +    void markPilePath(std::vector<Arc>& path) {
  2.1566 +      for (int i = 0; i < int(path.size()); ++i) {
  2.1567 +        _kuratowski.set(path[i], true);
  2.1568 +      }
  2.1569 +    }
  2.1570 +
  2.1571 +    void isolateKuratowski(Arc arc, NodeData& node_data,
  2.1572 +                           ArcLists& arc_lists, FlipMap& flip_map,
  2.1573 +                           OrderMap& order_map, OrderList& order_list,
  2.1574 +                           PredMap& pred_map, ChildLists& child_lists,
  2.1575 +                           AncestorMap& ancestor_map, LowMap& low_map,
  2.1576 +                           EmbedArc& embed_arc, MergeRoots& merge_roots) {
  2.1577 +
  2.1578 +      Node root = _graph.source(arc);
  2.1579 +      Node enode = _graph.target(arc);
  2.1580 +
  2.1581 +      int rorder = order_map[root];
  2.1582 +
  2.1583 +      TypeMap type_map(_graph, 0);
  2.1584 +
  2.1585 +      int rn = findComponentRoot(root, enode, child_lists,
  2.1586 +                                 order_map, order_list);
  2.1587 +
  2.1588 +      Node xnode = order_list[node_data[rn].next];
  2.1589 +      Node ynode = order_list[node_data[rn].prev];
  2.1590 +
  2.1591 +      // Minor-A
  2.1592 +      {
  2.1593 +        while (!merge_roots[xnode].empty() || !merge_roots[ynode].empty()) {
  2.1594 +
  2.1595 +          if (!merge_roots[xnode].empty()) {
  2.1596 +            root = xnode;
  2.1597 +            rn = merge_roots[xnode].front();
  2.1598 +          } else {
  2.1599 +            root = ynode;
  2.1600 +            rn = merge_roots[ynode].front();
  2.1601 +          }
  2.1602 +
  2.1603 +          xnode = order_list[node_data[rn].next];
  2.1604 +          ynode = order_list[node_data[rn].prev];
  2.1605 +        }
  2.1606 +
  2.1607 +        if (root != _graph.source(arc)) {
  2.1608 +          orientComponent(root, rn, order_map, pred_map,
  2.1609 +                          node_data, arc_lists, flip_map, type_map);
  2.1610 +          markFacePath(root, root, order_map, node_data);
  2.1611 +          int xlp = markExternalPath(xnode, order_map, child_lists,
  2.1612 +                                     pred_map, ancestor_map, low_map);
  2.1613 +          int ylp = markExternalPath(ynode, order_map, child_lists,
  2.1614 +                                     pred_map, ancestor_map, low_map);
  2.1615 +          markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
  2.1616 +          Node lwnode = findPertinent(ynode, order_map, node_data,
  2.1617 +                                      embed_arc, merge_roots);
  2.1618 +
  2.1619 +          markPertinentPath(lwnode, order_map, node_data, arc_lists,
  2.1620 +                            embed_arc, merge_roots);
  2.1621 +
  2.1622 +          return;
  2.1623 +        }
  2.1624 +      }
  2.1625 +
  2.1626 +      orientComponent(root, rn, order_map, pred_map,
  2.1627 +                      node_data, arc_lists, flip_map, type_map);
  2.1628 +
  2.1629 +      Node wnode = findPertinent(ynode, order_map, node_data,
  2.1630 +                                 embed_arc, merge_roots);
  2.1631 +      setFaceFlags(root, wnode, ynode, xnode, order_map, node_data, type_map);
  2.1632 +
  2.1633 +
  2.1634 +      //Minor-B
  2.1635 +      if (!merge_roots[wnode].empty()) {
  2.1636 +        int cn = merge_roots[wnode].back();
  2.1637 +        Node rep = order_list[cn - order_list.size()];
  2.1638 +        if (low_map[rep] < rorder) {
  2.1639 +          markFacePath(root, root, order_map, node_data);
  2.1640 +          int xlp = markExternalPath(xnode, order_map, child_lists,
  2.1641 +                                     pred_map, ancestor_map, low_map);
  2.1642 +          int ylp = markExternalPath(ynode, order_map, child_lists,
  2.1643 +                                     pred_map, ancestor_map, low_map);
  2.1644 +
  2.1645 +          Node lwnode, lznode;
  2.1646 +          markCommonPath(wnode, rorder, lwnode, lznode, order_list,
  2.1647 +                         order_map, node_data, arc_lists, embed_arc,
  2.1648 +                         merge_roots, child_lists, ancestor_map, low_map);
  2.1649 +
  2.1650 +          markPertinentPath(lwnode, order_map, node_data, arc_lists,
  2.1651 +                            embed_arc, merge_roots);
  2.1652 +          int zlp = markExternalPath(lznode, order_map, child_lists,
  2.1653 +                                     pred_map, ancestor_map, low_map);
  2.1654 +
  2.1655 +          int minlp = xlp < ylp ? xlp : ylp;
  2.1656 +          if (zlp < minlp) minlp = zlp;
  2.1657 +
  2.1658 +          int maxlp = xlp > ylp ? xlp : ylp;
  2.1659 +          if (zlp > maxlp) maxlp = zlp;
  2.1660 +
  2.1661 +          markPredPath(order_list[maxlp], order_list[minlp], pred_map);
  2.1662 +
  2.1663 +          return;
  2.1664 +        }
  2.1665 +      }
  2.1666 +
  2.1667 +      Node pxnode, pynode;
  2.1668 +      std::vector<Arc> ipath;
  2.1669 +      findInternalPath(ipath, wnode, root, type_map, order_map,
  2.1670 +                       node_data, arc_lists);
  2.1671 +      setInternalFlags(ipath, type_map);
  2.1672 +      pynode = _graph.source(ipath.front());
  2.1673 +      pxnode = _graph.target(ipath.back());
  2.1674 +
  2.1675 +      wnode = findPertinent(pynode, order_map, node_data,
  2.1676 +                            embed_arc, merge_roots);
  2.1677 +
  2.1678 +      // Minor-C
  2.1679 +      {
  2.1680 +        if (type_map[_graph.source(ipath.front())] == HIGHY) {
  2.1681 +          if (type_map[_graph.target(ipath.back())] == HIGHX) {
  2.1682 +            markFacePath(xnode, pxnode, order_map, node_data);
  2.1683 +          }
  2.1684 +          markFacePath(root, xnode, order_map, node_data);
  2.1685 +          markPertinentPath(wnode, order_map, node_data, arc_lists,
  2.1686 +                            embed_arc, merge_roots);
  2.1687 +          markInternalPath(ipath);
  2.1688 +          int xlp = markExternalPath(xnode, order_map, child_lists,
  2.1689 +                                     pred_map, ancestor_map, low_map);
  2.1690 +          int ylp = markExternalPath(ynode, order_map, child_lists,
  2.1691 +                                     pred_map, ancestor_map, low_map);
  2.1692 +          markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
  2.1693 +          return;
  2.1694 +        }
  2.1695 +
  2.1696 +        if (type_map[_graph.target(ipath.back())] == HIGHX) {
  2.1697 +          markFacePath(ynode, root, order_map, node_data);
  2.1698 +          markPertinentPath(wnode, order_map, node_data, arc_lists,
  2.1699 +                            embed_arc, merge_roots);
  2.1700 +          markInternalPath(ipath);
  2.1701 +          int xlp = markExternalPath(xnode, order_map, child_lists,
  2.1702 +                                     pred_map, ancestor_map, low_map);
  2.1703 +          int ylp = markExternalPath(ynode, order_map, child_lists,
  2.1704 +                                     pred_map, ancestor_map, low_map);
  2.1705 +          markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
  2.1706 +          return;
  2.1707 +        }
  2.1708 +      }
  2.1709 +
  2.1710 +      std::vector<Arc> ppath;
  2.1711 +      findPilePath(ppath, root, type_map, order_map, node_data, arc_lists);
  2.1712 +
  2.1713 +      // Minor-D
  2.1714 +      if (!ppath.empty()) {
  2.1715 +        markFacePath(ynode, xnode, order_map, node_data);
  2.1716 +        markPertinentPath(wnode, order_map, node_data, arc_lists,
  2.1717 +                          embed_arc, merge_roots);
  2.1718 +        markPilePath(ppath);
  2.1719 +        markInternalPath(ipath);
  2.1720 +        int xlp = markExternalPath(xnode, order_map, child_lists,
  2.1721 +                                   pred_map, ancestor_map, low_map);
  2.1722 +        int ylp = markExternalPath(ynode, order_map, child_lists,
  2.1723 +                                   pred_map, ancestor_map, low_map);
  2.1724 +        markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
  2.1725 +        return;
  2.1726 +      }
  2.1727 +
  2.1728 +      // Minor-E*
  2.1729 +      {
  2.1730 +
  2.1731 +        if (!external(wnode, rorder, child_lists, ancestor_map, low_map)) {
  2.1732 +          Node znode = findExternal(pynode, rorder, order_map,
  2.1733 +                                    child_lists, ancestor_map,
  2.1734 +                                    low_map, node_data);
  2.1735 +
  2.1736 +          if (type_map[znode] == LOWY) {
  2.1737 +            markFacePath(root, xnode, order_map, node_data);
  2.1738 +            markPertinentPath(wnode, order_map, node_data, arc_lists,
  2.1739 +                              embed_arc, merge_roots);
  2.1740 +            markInternalPath(ipath);
  2.1741 +            int xlp = markExternalPath(xnode, order_map, child_lists,
  2.1742 +                                       pred_map, ancestor_map, low_map);
  2.1743 +            int zlp = markExternalPath(znode, order_map, child_lists,
  2.1744 +                                       pred_map, ancestor_map, low_map);
  2.1745 +            markPredPath(root, order_list[xlp < zlp ? xlp : zlp], pred_map);
  2.1746 +          } else {
  2.1747 +            markFacePath(ynode, root, order_map, node_data);
  2.1748 +            markPertinentPath(wnode, order_map, node_data, arc_lists,
  2.1749 +                              embed_arc, merge_roots);
  2.1750 +            markInternalPath(ipath);
  2.1751 +            int ylp = markExternalPath(ynode, order_map, child_lists,
  2.1752 +                                       pred_map, ancestor_map, low_map);
  2.1753 +            int zlp = markExternalPath(znode, order_map, child_lists,
  2.1754 +                                       pred_map, ancestor_map, low_map);
  2.1755 +            markPredPath(root, order_list[ylp < zlp ? ylp : zlp], pred_map);
  2.1756 +          }
  2.1757 +          return;
  2.1758 +        }
  2.1759 +
  2.1760 +        int xlp = markExternalPath(xnode, order_map, child_lists,
  2.1761 +                                   pred_map, ancestor_map, low_map);
  2.1762 +        int ylp = markExternalPath(ynode, order_map, child_lists,
  2.1763 +                                   pred_map, ancestor_map, low_map);
  2.1764 +        int wlp = markExternalPath(wnode, order_map, child_lists,
  2.1765 +                                   pred_map, ancestor_map, low_map);
  2.1766 +
  2.1767 +        if (wlp > xlp && wlp > ylp) {
  2.1768 +          markFacePath(root, root, order_map, node_data);
  2.1769 +          markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
  2.1770 +          return;
  2.1771 +        }
  2.1772 +
  2.1773 +        markInternalPath(ipath);
  2.1774 +        markPertinentPath(wnode, order_map, node_data, arc_lists,
  2.1775 +                          embed_arc, merge_roots);
  2.1776 +
  2.1777 +        if (xlp > ylp && xlp > wlp) {
  2.1778 +          markFacePath(root, pynode, order_map, node_data);
  2.1779 +          markFacePath(wnode, xnode, order_map, node_data);
  2.1780 +          markPredPath(root, order_list[ylp < wlp ? ylp : wlp], pred_map);
  2.1781 +          return;
  2.1782 +        }
  2.1783 +
  2.1784 +        if (ylp > xlp && ylp > wlp) {
  2.1785 +          markFacePath(pxnode, root, order_map, node_data);
  2.1786 +          markFacePath(ynode, wnode, order_map, node_data);
  2.1787 +          markPredPath(root, order_list[xlp < wlp ? xlp : wlp], pred_map);
  2.1788 +          return;
  2.1789 +        }
  2.1790 +
  2.1791 +        if (pynode != ynode) {
  2.1792 +          markFacePath(pxnode, wnode, order_map, node_data);
  2.1793 +
  2.1794 +          int minlp = xlp < ylp ? xlp : ylp;
  2.1795 +          if (wlp < minlp) minlp = wlp;
  2.1796 +
  2.1797 +          int maxlp = xlp > ylp ? xlp : ylp;
  2.1798 +          if (wlp > maxlp) maxlp = wlp;
  2.1799 +
  2.1800 +          markPredPath(order_list[maxlp], order_list[minlp], pred_map);
  2.1801 +          return;
  2.1802 +        }
  2.1803 +
  2.1804 +        if (pxnode != xnode) {
  2.1805 +          markFacePath(wnode, pynode, order_map, node_data);
  2.1806 +
  2.1807 +          int minlp = xlp < ylp ? xlp : ylp;
  2.1808 +          if (wlp < minlp) minlp = wlp;
  2.1809 +
  2.1810 +          int maxlp = xlp > ylp ? xlp : ylp;
  2.1811 +          if (wlp > maxlp) maxlp = wlp;
  2.1812 +
  2.1813 +          markPredPath(order_list[maxlp], order_list[minlp], pred_map);
  2.1814 +          return;
  2.1815 +        }
  2.1816 +
  2.1817 +        markFacePath(root, root, order_map, node_data);
  2.1818 +        int minlp = xlp < ylp ? xlp : ylp;
  2.1819 +        if (wlp < minlp) minlp = wlp;
  2.1820 +        markPredPath(root, order_list[minlp], pred_map);
  2.1821 +        return;
  2.1822 +      }
  2.1823 +
  2.1824 +    }
  2.1825 +
  2.1826 +  };
  2.1827 +
  2.1828 +  namespace _planarity_bits {
  2.1829 +
  2.1830 +    template <typename Graph, typename EmbeddingMap>
  2.1831 +    void makeConnected(Graph& graph, EmbeddingMap& embedding) {
  2.1832 +      DfsVisitor<Graph> null_visitor;
  2.1833 +      DfsVisit<Graph, DfsVisitor<Graph> > dfs(graph, null_visitor);
  2.1834 +      dfs.init();
  2.1835 +
  2.1836 +      typename Graph::Node u = INVALID;
  2.1837 +      for (typename Graph::NodeIt n(graph); n != INVALID; ++n) {
  2.1838 +        if (!dfs.reached(n)) {
  2.1839 +          dfs.addSource(n);
  2.1840 +          dfs.start();
  2.1841 +          if (u == INVALID) {
  2.1842 +            u = n;
  2.1843 +          } else {
  2.1844 +            typename Graph::Node v = n;
  2.1845 +
  2.1846 +            typename Graph::Arc ue = typename Graph::OutArcIt(graph, u);
  2.1847 +            typename Graph::Arc ve = typename Graph::OutArcIt(graph, v);
  2.1848 +
  2.1849 +            typename Graph::Arc e = graph.direct(graph.addEdge(u, v), true);
  2.1850 +
  2.1851 +            if (ue != INVALID) {
  2.1852 +              embedding[e] = embedding[ue];
  2.1853 +              embedding[ue] = e;
  2.1854 +            } else {
  2.1855 +              embedding[e] = e;
  2.1856 +            }
  2.1857 +
  2.1858 +            if (ve != INVALID) {
  2.1859 +              embedding[graph.oppositeArc(e)] = embedding[ve];
  2.1860 +              embedding[ve] = graph.oppositeArc(e);
  2.1861 +            } else {
  2.1862 +              embedding[graph.oppositeArc(e)] = graph.oppositeArc(e);
  2.1863 +            }
  2.1864 +          }
  2.1865 +        }
  2.1866 +      }
  2.1867 +    }
  2.1868 +
  2.1869 +    template <typename Graph, typename EmbeddingMap>
  2.1870 +    void makeBiNodeConnected(Graph& graph, EmbeddingMap& embedding) {
  2.1871 +      typename Graph::template ArcMap<bool> processed(graph);
  2.1872 +
  2.1873 +      std::vector<typename Graph::Arc> arcs;
  2.1874 +      for (typename Graph::ArcIt e(graph); e != INVALID; ++e) {
  2.1875 +        arcs.push_back(e);
  2.1876 +      }
  2.1877 +
  2.1878 +      IterableBoolMap<Graph, typename Graph::Node> visited(graph, false);
  2.1879 +
  2.1880 +      for (int i = 0; i < int(arcs.size()); ++i) {
  2.1881 +        typename Graph::Arc pp = arcs[i];
  2.1882 +        if (processed[pp]) continue;
  2.1883 +
  2.1884 +        typename Graph::Arc e = embedding[graph.oppositeArc(pp)];
  2.1885 +        processed[e] = true;
  2.1886 +        visited.set(graph.source(e), true);
  2.1887 +
  2.1888 +        typename Graph::Arc p = e, l = e;
  2.1889 +        e = embedding[graph.oppositeArc(e)];
  2.1890 +
  2.1891 +        while (e != l) {
  2.1892 +          processed[e] = true;
  2.1893 +
  2.1894 +          if (visited[graph.source(e)]) {
  2.1895 +
  2.1896 +            typename Graph::Arc n =
  2.1897 +              graph.direct(graph.addEdge(graph.source(p),
  2.1898 +                                           graph.target(e)), true);
  2.1899 +            embedding[n] = p;
  2.1900 +            embedding[graph.oppositeArc(pp)] = n;
  2.1901 +
  2.1902 +            embedding[graph.oppositeArc(n)] =
  2.1903 +              embedding[graph.oppositeArc(e)];
  2.1904 +            embedding[graph.oppositeArc(e)] =
  2.1905 +              graph.oppositeArc(n);
  2.1906 +
  2.1907 +            p = n;
  2.1908 +            e = embedding[graph.oppositeArc(n)];
  2.1909 +          } else {
  2.1910 +            visited.set(graph.source(e), true);
  2.1911 +            pp = p;
  2.1912 +            p = e;
  2.1913 +            e = embedding[graph.oppositeArc(e)];
  2.1914 +          }
  2.1915 +        }
  2.1916 +        visited.setAll(false);
  2.1917 +      }
  2.1918 +    }
  2.1919 +
  2.1920 +
  2.1921 +    template <typename Graph, typename EmbeddingMap>
  2.1922 +    void makeMaxPlanar(Graph& graph, EmbeddingMap& embedding) {
  2.1923 +
  2.1924 +      typename Graph::template NodeMap<int> degree(graph);
  2.1925 +
  2.1926 +      for (typename Graph::NodeIt n(graph); n != INVALID; ++n) {
  2.1927 +        degree[n] = countIncEdges(graph, n);
  2.1928 +      }
  2.1929 +
  2.1930 +      typename Graph::template ArcMap<bool> processed(graph);
  2.1931 +      IterableBoolMap<Graph, typename Graph::Node> visited(graph, false);
  2.1932 +
  2.1933 +      std::vector<typename Graph::Arc> arcs;
  2.1934 +      for (typename Graph::ArcIt e(graph); e != INVALID; ++e) {
  2.1935 +        arcs.push_back(e);
  2.1936 +      }
  2.1937 +
  2.1938 +      for (int i = 0; i < int(arcs.size()); ++i) {
  2.1939 +        typename Graph::Arc e = arcs[i];
  2.1940 +
  2.1941 +        if (processed[e]) continue;
  2.1942 +        processed[e] = true;
  2.1943 +
  2.1944 +        typename Graph::Arc mine = e;
  2.1945 +        int mind = degree[graph.source(e)];
  2.1946 +
  2.1947 +        int face_size = 1;
  2.1948 +
  2.1949 +        typename Graph::Arc l = e;
  2.1950 +        e = embedding[graph.oppositeArc(e)];
  2.1951 +        while (l != e) {
  2.1952 +          processed[e] = true;
  2.1953 +
  2.1954 +          ++face_size;
  2.1955 +
  2.1956 +          if (degree[graph.source(e)] < mind) {
  2.1957 +            mine = e;
  2.1958 +            mind = degree[graph.source(e)];
  2.1959 +          }
  2.1960 +
  2.1961 +          e = embedding[graph.oppositeArc(e)];
  2.1962 +        }
  2.1963 +
  2.1964 +        if (face_size < 4) {
  2.1965 +          continue;
  2.1966 +        }
  2.1967 +
  2.1968 +        typename Graph::Node s = graph.source(mine);
  2.1969 +        for (typename Graph::OutArcIt e(graph, s); e != INVALID; ++e) {
  2.1970 +          visited.set(graph.target(e), true);
  2.1971 +        }
  2.1972 +
  2.1973 +        typename Graph::Arc oppe = INVALID;
  2.1974 +
  2.1975 +        e = embedding[graph.oppositeArc(mine)];
  2.1976 +        e = embedding[graph.oppositeArc(e)];
  2.1977 +        while (graph.target(e) != s) {
  2.1978 +          if (visited[graph.source(e)]) {
  2.1979 +            oppe = e;
  2.1980 +            break;
  2.1981 +          }
  2.1982 +          e = embedding[graph.oppositeArc(e)];
  2.1983 +        }
  2.1984 +        visited.setAll(false);
  2.1985 +
  2.1986 +        if (oppe == INVALID) {
  2.1987 +
  2.1988 +          e = embedding[graph.oppositeArc(mine)];
  2.1989 +          typename Graph::Arc pn = mine, p = e;
  2.1990 +
  2.1991 +          e = embedding[graph.oppositeArc(e)];
  2.1992 +          while (graph.target(e) != s) {
  2.1993 +            typename Graph::Arc n =
  2.1994 +              graph.direct(graph.addEdge(s, graph.source(e)), true);
  2.1995 +
  2.1996 +            embedding[n] = pn;
  2.1997 +            embedding[graph.oppositeArc(n)] = e;
  2.1998 +            embedding[graph.oppositeArc(p)] = graph.oppositeArc(n);
  2.1999 +
  2.2000 +            pn = n;
  2.2001 +
  2.2002 +            p = e;
  2.2003 +            e = embedding[graph.oppositeArc(e)];
  2.2004 +          }
  2.2005 +
  2.2006 +          embedding[graph.oppositeArc(e)] = pn;
  2.2007 +
  2.2008 +        } else {
  2.2009 +
  2.2010 +          mine = embedding[graph.oppositeArc(mine)];
  2.2011 +          s = graph.source(mine);
  2.2012 +          oppe = embedding[graph.oppositeArc(oppe)];
  2.2013 +          typename Graph::Node t = graph.source(oppe);
  2.2014 +
  2.2015 +          typename Graph::Arc ce = graph.direct(graph.addEdge(s, t), true);
  2.2016 +          embedding[ce] = mine;
  2.2017 +          embedding[graph.oppositeArc(ce)] = oppe;
  2.2018 +
  2.2019 +          typename Graph::Arc pn = ce, p = oppe;
  2.2020 +          e = embedding[graph.oppositeArc(oppe)];
  2.2021 +          while (graph.target(e) != s) {
  2.2022 +            typename Graph::Arc n =
  2.2023 +              graph.direct(graph.addEdge(s, graph.source(e)), true);
  2.2024 +
  2.2025 +            embedding[n] = pn;
  2.2026 +            embedding[graph.oppositeArc(n)] = e;
  2.2027 +            embedding[graph.oppositeArc(p)] = graph.oppositeArc(n);
  2.2028 +
  2.2029 +            pn = n;
  2.2030 +
  2.2031 +            p = e;
  2.2032 +            e = embedding[graph.oppositeArc(e)];
  2.2033 +
  2.2034 +          }
  2.2035 +          embedding[graph.oppositeArc(e)] = pn;
  2.2036 +
  2.2037 +          pn = graph.oppositeArc(ce), p = mine;
  2.2038 +          e = embedding[graph.oppositeArc(mine)];
  2.2039 +          while (graph.target(e) != t) {
  2.2040 +            typename Graph::Arc n =
  2.2041 +              graph.direct(graph.addEdge(t, graph.source(e)), true);
  2.2042 +
  2.2043 +            embedding[n] = pn;
  2.2044 +            embedding[graph.oppositeArc(n)] = e;
  2.2045 +            embedding[graph.oppositeArc(p)] = graph.oppositeArc(n);
  2.2046 +
  2.2047 +            pn = n;
  2.2048 +
  2.2049 +            p = e;
  2.2050 +            e = embedding[graph.oppositeArc(e)];
  2.2051 +
  2.2052 +          }
  2.2053 +          embedding[graph.oppositeArc(e)] = pn;
  2.2054 +        }
  2.2055 +      }
  2.2056 +    }
  2.2057 +
  2.2058 +  }
  2.2059 +
  2.2060 +  /// \ingroup planar
  2.2061 +  ///
  2.2062 +  /// \brief Schnyder's planar drawing algorithm
  2.2063 +  ///
  2.2064 +  /// The planar drawing algorithm calculates positions for the nodes
  2.2065 +  /// in the plane which coordinates satisfy that if the arcs are
  2.2066 +  /// represented with straight lines then they will not intersect
  2.2067 +  /// each other.
  2.2068 +  ///
  2.2069 +  /// Scnyder's algorithm embeds the graph on \c (n-2,n-2) size grid,
  2.2070 +  /// i.e. each node will be located in the \c [0,n-2]x[0,n-2] square.
  2.2071 +  /// The time complexity of the algorithm is O(n).
  2.2072 +  template <typename Graph>
  2.2073 +  class PlanarDrawing {
  2.2074 +  public:
  2.2075 +
  2.2076 +    TEMPLATE_GRAPH_TYPEDEFS(Graph);
  2.2077 +
  2.2078 +    /// \brief The point type for store coordinates
  2.2079 +    typedef dim2::Point<int> Point;
  2.2080 +    /// \brief The map type for store coordinates
  2.2081 +    typedef typename Graph::template NodeMap<Point> PointMap;
  2.2082 +
  2.2083 +
  2.2084 +    /// \brief Constructor
  2.2085 +    ///
  2.2086 +    /// Constructor
  2.2087 +    /// \pre The graph should be simple, i.e. loop and parallel arc free.
  2.2088 +    PlanarDrawing(const Graph& graph)
  2.2089 +      : _graph(graph), _point_map(graph) {}
  2.2090 +
  2.2091 +  private:
  2.2092 +
  2.2093 +    template <typename AuxGraph, typename AuxEmbeddingMap>
  2.2094 +    void drawing(const AuxGraph& graph,
  2.2095 +                 const AuxEmbeddingMap& next,
  2.2096 +                 PointMap& point_map) {
  2.2097 +      TEMPLATE_GRAPH_TYPEDEFS(AuxGraph);
  2.2098 +
  2.2099 +      typename AuxGraph::template ArcMap<Arc> prev(graph);
  2.2100 +
  2.2101 +      for (NodeIt n(graph); n != INVALID; ++n) {
  2.2102 +        Arc e = OutArcIt(graph, n);
  2.2103 +
  2.2104 +        Arc p = e, l = e;
  2.2105 +
  2.2106 +        e = next[e];
  2.2107 +        while (e != l) {
  2.2108 +          prev[e] = p;
  2.2109 +          p = e;
  2.2110 +          e = next[e];
  2.2111 +        }
  2.2112 +        prev[e] = p;
  2.2113 +      }
  2.2114 +
  2.2115 +      Node anode, bnode, cnode;
  2.2116 +
  2.2117 +      {
  2.2118 +        Arc e = ArcIt(graph);
  2.2119 +        anode = graph.source(e);
  2.2120 +        bnode = graph.target(e);
  2.2121 +        cnode = graph.target(next[graph.oppositeArc(e)]);
  2.2122 +      }
  2.2123 +
  2.2124 +      IterableBoolMap<AuxGraph, Node> proper(graph, false);
  2.2125 +      typename AuxGraph::template NodeMap<int> conn(graph, -1);
  2.2126 +
  2.2127 +      conn[anode] = conn[bnode] = -2;
  2.2128 +      {
  2.2129 +        for (OutArcIt e(graph, anode); e != INVALID; ++e) {
  2.2130 +          Node m = graph.target(e);
  2.2131 +          if (conn[m] == -1) {
  2.2132 +            conn[m] = 1;
  2.2133 +          }
  2.2134 +        }
  2.2135 +        conn[cnode] = 2;
  2.2136 +
  2.2137 +        for (OutArcIt e(graph, bnode); e != INVALID; ++e) {
  2.2138 +          Node m = graph.target(e);
  2.2139 +          if (conn[m] == -1) {
  2.2140 +            conn[m] = 1;
  2.2141 +          } else if (conn[m] != -2) {
  2.2142 +            conn[m] += 1;
  2.2143 +            Arc pe = graph.oppositeArc(e);
  2.2144 +            if (conn[graph.target(next[pe])] == -2) {
  2.2145 +              conn[m] -= 1;
  2.2146 +            }
  2.2147 +            if (conn[graph.target(prev[pe])] == -2) {
  2.2148 +              conn[m] -= 1;
  2.2149 +            }
  2.2150 +
  2.2151 +            proper.set(m, conn[m] == 1);
  2.2152 +          }
  2.2153 +        }
  2.2154 +      }
  2.2155 +
  2.2156 +
  2.2157 +      typename AuxGraph::template ArcMap<int> angle(graph, -1);
  2.2158 +
  2.2159 +      while (proper.trueNum() != 0) {
  2.2160 +        Node n = typename IterableBoolMap<AuxGraph, Node>::TrueIt(proper);
  2.2161 +        proper.set(n, false);
  2.2162 +        conn[n] = -2;
  2.2163 +
  2.2164 +        for (OutArcIt e(graph, n); e != INVALID; ++e) {
  2.2165 +          Node m = graph.target(e);
  2.2166 +          if (conn[m] == -1) {
  2.2167 +            conn[m] = 1;
  2.2168 +          } else if (conn[m] != -2) {
  2.2169 +            conn[m] += 1;
  2.2170 +            Arc pe = graph.oppositeArc(e);
  2.2171 +            if (conn[graph.target(next[pe])] == -2) {
  2.2172 +              conn[m] -= 1;
  2.2173 +            }
  2.2174 +            if (conn[graph.target(prev[pe])] == -2) {
  2.2175 +              conn[m] -= 1;
  2.2176 +            }
  2.2177 +
  2.2178 +            proper.set(m, conn[m] == 1);
  2.2179 +          }
  2.2180 +        }
  2.2181 +
  2.2182 +        {
  2.2183 +          Arc e = OutArcIt(graph, n);
  2.2184 +          Arc p = e, l = e;
  2.2185 +
  2.2186 +          e = next[e];
  2.2187 +          while (e != l) {
  2.2188 +
  2.2189 +            if (conn[graph.target(e)] == -2 && conn[graph.target(p)] == -2) {
  2.2190 +              Arc f = e;
  2.2191 +              angle[f] = 0;
  2.2192 +              f = next[graph.oppositeArc(f)];
  2.2193 +              angle[f] = 1;
  2.2194 +              f = next[graph.oppositeArc(f)];
  2.2195 +              angle[f] = 2;
  2.2196 +            }
  2.2197 +
  2.2198 +            p = e;
  2.2199 +            e = next[e];
  2.2200 +          }
  2.2201 +
  2.2202 +          if (conn[graph.target(e)] == -2 && conn[graph.target(p)] == -2) {
  2.2203 +            Arc f = e;
  2.2204 +            angle[f] = 0;
  2.2205 +            f = next[graph.oppositeArc(f)];
  2.2206 +            angle[f] = 1;
  2.2207 +            f = next[graph.oppositeArc(f)];
  2.2208 +            angle[f] = 2;
  2.2209 +          }
  2.2210 +        }
  2.2211 +      }
  2.2212 +
  2.2213 +      typename AuxGraph::template NodeMap<Node> apred(graph, INVALID);
  2.2214 +      typename AuxGraph::template NodeMap<Node> bpred(graph, INVALID);
  2.2215 +      typename AuxGraph::template NodeMap<Node> cpred(graph, INVALID);
  2.2216 +
  2.2217 +      typename AuxGraph::template NodeMap<int> apredid(graph, -1);
  2.2218 +      typename AuxGraph::template NodeMap<int> bpredid(graph, -1);
  2.2219 +      typename AuxGraph::template NodeMap<int> cpredid(graph, -1);
  2.2220 +
  2.2221 +      for (ArcIt e(graph); e != INVALID; ++e) {
  2.2222 +        if (angle[e] == angle[next[e]]) {
  2.2223 +          switch (angle[e]) {
  2.2224 +          case 2:
  2.2225 +            apred[graph.target(e)] = graph.source(e);
  2.2226 +            apredid[graph.target(e)] = graph.id(graph.source(e));
  2.2227 +            break;
  2.2228 +          case 1:
  2.2229 +            bpred[graph.target(e)] = graph.source(e);
  2.2230 +            bpredid[graph.target(e)] = graph.id(graph.source(e));
  2.2231 +            break;
  2.2232 +          case 0:
  2.2233 +            cpred[graph.target(e)] = graph.source(e);
  2.2234 +            cpredid[graph.target(e)] = graph.id(graph.source(e));
  2.2235 +            break;
  2.2236 +          }
  2.2237 +        }
  2.2238 +      }
  2.2239 +
  2.2240 +      cpred[anode] = INVALID;
  2.2241 +      cpred[bnode] = INVALID;
  2.2242 +
  2.2243 +      std::vector<Node> aorder, border, corder;
  2.2244 +
  2.2245 +      {
  2.2246 +        typename AuxGraph::template NodeMap<bool> processed(graph, false);
  2.2247 +        std::vector<Node> st;
  2.2248 +        for (NodeIt n(graph); n != INVALID; ++n) {
  2.2249 +          if (!processed[n] && n != bnode && n != cnode) {
  2.2250 +            st.push_back(n);
  2.2251 +            processed[n] = true;
  2.2252 +            Node m = apred[n];
  2.2253 +            while (m != INVALID && !processed[m]) {
  2.2254 +              st.push_back(m);
  2.2255 +              processed[m] = true;
  2.2256 +              m = apred[m];
  2.2257 +            }
  2.2258 +            while (!st.empty()) {
  2.2259 +              aorder.push_back(st.back());
  2.2260 +              st.pop_back();
  2.2261 +            }
  2.2262 +          }
  2.2263 +        }
  2.2264 +      }
  2.2265 +
  2.2266 +      {
  2.2267 +        typename AuxGraph::template NodeMap<bool> processed(graph, false);
  2.2268 +        std::vector<Node> st;
  2.2269 +        for (NodeIt n(graph); n != INVALID; ++n) {
  2.2270 +          if (!processed[n] && n != cnode && n != anode) {
  2.2271 +            st.push_back(n);
  2.2272 +            processed[n] = true;
  2.2273 +            Node m = bpred[n];
  2.2274 +            while (m != INVALID && !processed[m]) {
  2.2275 +              st.push_back(m);
  2.2276 +              processed[m] = true;
  2.2277 +              m = bpred[m];
  2.2278 +            }
  2.2279 +            while (!st.empty()) {
  2.2280 +              border.push_back(st.back());
  2.2281 +              st.pop_back();
  2.2282 +            }
  2.2283 +          }
  2.2284 +        }
  2.2285 +      }
  2.2286 +
  2.2287 +      {
  2.2288 +        typename AuxGraph::template NodeMap<bool> processed(graph, false);
  2.2289 +        std::vector<Node> st;
  2.2290 +        for (NodeIt n(graph); n != INVALID; ++n) {
  2.2291 +          if (!processed[n] && n != anode && n != bnode) {
  2.2292 +            st.push_back(n);
  2.2293 +            processed[n] = true;
  2.2294 +            Node m = cpred[n];
  2.2295 +            while (m != INVALID && !processed[m]) {
  2.2296 +              st.push_back(m);
  2.2297 +              processed[m] = true;
  2.2298 +              m = cpred[m];
  2.2299 +            }
  2.2300 +            while (!st.empty()) {
  2.2301 +              corder.push_back(st.back());
  2.2302 +              st.pop_back();
  2.2303 +            }
  2.2304 +          }
  2.2305 +        }
  2.2306 +      }
  2.2307 +
  2.2308 +      typename AuxGraph::template NodeMap<int> atree(graph, 0);
  2.2309 +      for (int i = aorder.size() - 1; i >= 0; --i) {
  2.2310 +        Node n = aorder[i];
  2.2311 +        atree[n] = 1;
  2.2312 +        for (OutArcIt e(graph, n); e != INVALID; ++e) {
  2.2313 +          if (apred[graph.target(e)] == n) {
  2.2314 +            atree[n] += atree[graph.target(e)];
  2.2315 +          }
  2.2316 +        }
  2.2317 +      }
  2.2318 +
  2.2319 +      typename AuxGraph::template NodeMap<int> btree(graph, 0);
  2.2320 +      for (int i = border.size() - 1; i >= 0; --i) {
  2.2321 +        Node n = border[i];
  2.2322 +        btree[n] = 1;
  2.2323 +        for (OutArcIt e(graph, n); e != INVALID; ++e) {
  2.2324 +          if (bpred[graph.target(e)] == n) {
  2.2325 +            btree[n] += btree[graph.target(e)];
  2.2326 +          }
  2.2327 +        }
  2.2328 +      }
  2.2329 +
  2.2330 +      typename AuxGraph::template NodeMap<int> apath(graph, 0);
  2.2331 +      apath[bnode] = apath[cnode] = 1;
  2.2332 +      typename AuxGraph::template NodeMap<int> apath_btree(graph, 0);
  2.2333 +      apath_btree[bnode] = btree[bnode];
  2.2334 +      for (int i = 1; i < int(aorder.size()); ++i) {
  2.2335 +        Node n = aorder[i];
  2.2336 +        apath[n] = apath[apred[n]] + 1;
  2.2337 +        apath_btree[n] = btree[n] + apath_btree[apred[n]];
  2.2338 +      }
  2.2339 +
  2.2340 +      typename AuxGraph::template NodeMap<int> bpath_atree(graph, 0);
  2.2341 +      bpath_atree[anode] = atree[anode];
  2.2342 +      for (int i = 1; i < int(border.size()); ++i) {
  2.2343 +        Node n = border[i];
  2.2344 +        bpath_atree[n] = atree[n] + bpath_atree[bpred[n]];
  2.2345 +      }
  2.2346 +
  2.2347 +      typename AuxGraph::template NodeMap<int> cpath(graph, 0);
  2.2348 +      cpath[anode] = cpath[bnode] = 1;
  2.2349 +      typename AuxGraph::template NodeMap<int> cpath_atree(graph, 0);
  2.2350 +      cpath_atree[anode] = atree[anode];
  2.2351 +      typename AuxGraph::template NodeMap<int> cpath_btree(graph, 0);
  2.2352 +      cpath_btree[bnode] = btree[bnode];
  2.2353 +      for (int i = 1; i < int(corder.size()); ++i) {
  2.2354 +        Node n = corder[i];
  2.2355 +        cpath[n] = cpath[cpred[n]] + 1;
  2.2356 +        cpath_atree[n] = atree[n] + cpath_atree[cpred[n]];
  2.2357 +        cpath_btree[n] = btree[n] + cpath_btree[cpred[n]];
  2.2358 +      }
  2.2359 +
  2.2360 +      typename AuxGraph::template NodeMap<int> third(graph);
  2.2361 +      for (NodeIt n(graph); n != INVALID; ++n) {
  2.2362 +        point_map[n].x =
  2.2363 +          bpath_atree[n] + cpath_atree[n] - atree[n] - cpath[n] + 1;
  2.2364 +        point_map[n].y =
  2.2365 +          cpath_btree[n] + apath_btree[n] - btree[n] - apath[n] + 1;
  2.2366 +      }
  2.2367 +
  2.2368 +    }
  2.2369 +
  2.2370 +  public:
  2.2371 +
  2.2372 +    /// \brief Calculates the node positions
  2.2373 +    ///
  2.2374 +    /// This function calculates the node positions.
  2.2375 +    /// \return %True if the graph is planar.
  2.2376 +    bool run() {
  2.2377 +      PlanarEmbedding<Graph> pe(_graph);
  2.2378 +      if (!pe.run()) return false;
  2.2379 +
  2.2380 +      run(pe);
  2.2381 +      return true;
  2.2382 +    }
  2.2383 +
  2.2384 +    /// \brief Calculates the node positions according to a
  2.2385 +    /// combinatorical embedding
  2.2386 +    ///
  2.2387 +    /// This function calculates the node locations. The \c embedding
  2.2388 +    /// parameter should contain a valid combinatorical embedding, i.e.
  2.2389 +    /// a valid cyclic order of the arcs.
  2.2390 +    template <typename EmbeddingMap>
  2.2391 +    void run(const EmbeddingMap& embedding) {
  2.2392 +      typedef SmartEdgeSet<Graph> AuxGraph;
  2.2393 +
  2.2394 +      if (3 * countNodes(_graph) - 6 == countEdges(_graph)) {
  2.2395 +        drawing(_graph, embedding, _point_map);
  2.2396 +        return;
  2.2397 +      }
  2.2398 +
  2.2399 +      AuxGraph aux_graph(_graph);
  2.2400 +      typename AuxGraph::template ArcMap<typename AuxGraph::Arc>
  2.2401 +        aux_embedding(aux_graph);
  2.2402 +
  2.2403 +      {
  2.2404 +
  2.2405 +        typename Graph::template EdgeMap<typename AuxGraph::Edge>
  2.2406 +          ref(_graph);
  2.2407 +
  2.2408 +        for (EdgeIt e(_graph); e != INVALID; ++e) {
  2.2409 +          ref[e] = aux_graph.addEdge(_graph.u(e), _graph.v(e));
  2.2410 +        }
  2.2411 +
  2.2412 +        for (EdgeIt e(_graph); e != INVALID; ++e) {
  2.2413 +          Arc ee = embedding[_graph.direct(e, true)];
  2.2414 +          aux_embedding[aux_graph.direct(ref[e], true)] =
  2.2415 +            aux_graph.direct(ref[ee], _graph.direction(ee));
  2.2416 +          ee = embedding[_graph.direct(e, false)];
  2.2417 +          aux_embedding[aux_graph.direct(ref[e], false)] =
  2.2418 +            aux_graph.direct(ref[ee], _graph.direction(ee));
  2.2419 +        }
  2.2420 +      }
  2.2421 +      _planarity_bits::makeConnected(aux_graph, aux_embedding);
  2.2422 +      _planarity_bits::makeBiNodeConnected(aux_graph, aux_embedding);
  2.2423 +      _planarity_bits::makeMaxPlanar(aux_graph, aux_embedding);
  2.2424 +      drawing(aux_graph, aux_embedding, _point_map);
  2.2425 +    }
  2.2426 +
  2.2427 +    /// \brief The coordinate of the given node
  2.2428 +    ///
  2.2429 +    /// The coordinate of the given node.
  2.2430 +    Point operator[](const Node& node) const {
  2.2431 +      return _point_map[node];
  2.2432 +    }
  2.2433 +
  2.2434 +    /// \brief Returns the grid embedding in a \e NodeMap.
  2.2435 +    ///
  2.2436 +    /// Returns the grid embedding in a \e NodeMap of \c dim2::Point<int> .
  2.2437 +    const PointMap& coords() const {
  2.2438 +      return _point_map;
  2.2439 +    }
  2.2440 +
  2.2441 +  private:
  2.2442 +
  2.2443 +    const Graph& _graph;
  2.2444 +    PointMap _point_map;
  2.2445 +
  2.2446 +  };
  2.2447 +
  2.2448 +  namespace _planarity_bits {
  2.2449 +
  2.2450 +    template <typename ColorMap>
  2.2451 +    class KempeFilter {
  2.2452 +    public:
  2.2453 +      typedef typename ColorMap::Key Key;
  2.2454 +      typedef bool Value;
  2.2455 +
  2.2456 +      KempeFilter(const ColorMap& color_map,
  2.2457 +                  const typename ColorMap::Value& first,
  2.2458 +                  const typename ColorMap::Value& second)
  2.2459 +        : _color_map(color_map), _first(first), _second(second) {}
  2.2460 +
  2.2461 +      Value operator[](const Key& key) const {
  2.2462 +        return _color_map[key] == _first || _color_map[key] == _second;
  2.2463 +      }
  2.2464 +
  2.2465 +    private:
  2.2466 +      const ColorMap& _color_map;
  2.2467 +      typename ColorMap::Value _first, _second;
  2.2468 +    };
  2.2469 +  }
  2.2470 +
  2.2471 +  /// \ingroup planar
  2.2472 +  ///
  2.2473 +  /// \brief Coloring planar graphs
  2.2474 +  ///
  2.2475 +  /// The graph coloring problem is the coloring of the graph nodes
  2.2476 +  /// that there are not adjacent nodes with the same color. The
  2.2477 +  /// planar graphs can be always colored with four colors, it is
  2.2478 +  /// proved by Appel and Haken and their proofs provide a quadratic
  2.2479 +  /// time algorithm for four coloring, but it could not be used to
  2.2480 +  /// implement efficient algorithm. The five and six coloring can be
  2.2481 +  /// made in linear time, but in this class the five coloring has
  2.2482 +  /// quadratic worst case time complexity. The two coloring (if
  2.2483 +  /// possible) is solvable with a graph search algorithm and it is
  2.2484 +  /// implemented in \ref bipartitePartitions() function in LEMON. To
  2.2485 +  /// decide whether the planar graph is three colorable is
  2.2486 +  /// NP-complete.
  2.2487 +  ///
  2.2488 +  /// This class contains member functions for calculate colorings
  2.2489 +  /// with five and six colors. The six coloring algorithm is a simple
  2.2490 +  /// greedy coloring on the backward minimum outgoing order of nodes.
  2.2491 +  /// This order can be computed as in each phase the node with least
  2.2492 +  /// outgoing arcs to unprocessed nodes is chosen. This order
  2.2493 +  /// guarantees that when a node is chosen for coloring it has at
  2.2494 +  /// most five already colored adjacents. The five coloring algorithm
  2.2495 +  /// use the same method, but if the greedy approach fails to color
  2.2496 +  /// with five colors, i.e. the node has five already different
  2.2497 +  /// colored neighbours, it swaps the colors in one of the connected
  2.2498 +  /// two colored sets with the Kempe recoloring method.
  2.2499 +  template <typename Graph>
  2.2500 +  class PlanarColoring {
  2.2501 +  public:
  2.2502 +
  2.2503 +    TEMPLATE_GRAPH_TYPEDEFS(Graph);
  2.2504 +
  2.2505 +    /// \brief The map type for store color indexes
  2.2506 +    typedef typename Graph::template NodeMap<int> IndexMap;
  2.2507 +    /// \brief The map type for store colors
  2.2508 +    typedef ComposeMap<Palette, IndexMap> ColorMap;
  2.2509 +
  2.2510 +    /// \brief Constructor
  2.2511 +    ///
  2.2512 +    /// Constructor
  2.2513 +    /// \pre The graph should be simple, i.e. loop and parallel arc free.
  2.2514 +    PlanarColoring(const Graph& graph)
  2.2515 +      : _graph(graph), _color_map(graph), _palette(0) {
  2.2516 +      _palette.add(Color(1,0,0));
  2.2517 +      _palette.add(Color(0,1,0));
  2.2518 +      _palette.add(Color(0,0,1));
  2.2519 +      _palette.add(Color(1,1,0));
  2.2520 +      _palette.add(Color(1,0,1));
  2.2521 +      _palette.add(Color(0,1,1));
  2.2522 +    }
  2.2523 +
  2.2524 +    /// \brief Returns the \e NodeMap of color indexes
  2.2525 +    ///
  2.2526 +    /// Returns the \e NodeMap of color indexes. The values are in the
  2.2527 +    /// range \c [0..4] or \c [0..5] according to the coloring method.
  2.2528 +    IndexMap colorIndexMap() const {
  2.2529 +      return _color_map;
  2.2530 +    }
  2.2531 +
  2.2532 +    /// \brief Returns the \e NodeMap of colors
  2.2533 +    ///
  2.2534 +    /// Returns the \e NodeMap of colors. The values are five or six
  2.2535 +    /// distinct \ref lemon::Color "colors".
  2.2536 +    ColorMap colorMap() const {
  2.2537 +      return composeMap(_palette, _color_map);
  2.2538 +    }
  2.2539 +
  2.2540 +    /// \brief Returns the color index of the node
  2.2541 +    ///
  2.2542 +    /// Returns the color index of the node. The values are in the
  2.2543 +    /// range \c [0..4] or \c [0..5] according to the coloring method.
  2.2544 +    int colorIndex(const Node& node) const {
  2.2545 +      return _color_map[node];
  2.2546 +    }
  2.2547 +
  2.2548 +    /// \brief Returns the color of the node
  2.2549 +    ///
  2.2550 +    /// Returns the color of the node. The values are five or six
  2.2551 +    /// distinct \ref lemon::Color "colors".
  2.2552 +    Color color(const Node& node) const {
  2.2553 +      return _palette[_color_map[node]];
  2.2554 +    }
  2.2555 +
  2.2556 +
  2.2557 +    /// \brief Calculates a coloring with at most six colors
  2.2558 +    ///
  2.2559 +    /// This function calculates a coloring with at most six colors. The time
  2.2560 +    /// complexity of this variant is linear in the size of the graph.
  2.2561 +    /// \return %True when the algorithm could color the graph with six color.
  2.2562 +    /// If the algorithm fails, then the graph could not be planar.
  2.2563 +    /// \note This function can return true if the graph is not
  2.2564 +    /// planar but it can be colored with 6 colors.
  2.2565 +    bool runSixColoring() {
  2.2566 +
  2.2567 +      typename Graph::template NodeMap<int> heap_index(_graph, -1);
  2.2568 +      BucketHeap<typename Graph::template NodeMap<int> > heap(heap_index);
  2.2569 +
  2.2570 +      for (NodeIt n(_graph); n != INVALID; ++n) {
  2.2571 +        _color_map[n] = -2;
  2.2572 +        heap.push(n, countOutArcs(_graph, n));
  2.2573 +      }
  2.2574 +
  2.2575 +      std::vector<Node> order;
  2.2576 +
  2.2577 +      while (!heap.empty()) {
  2.2578 +        Node n = heap.top();
  2.2579 +        heap.pop();
  2.2580 +        _color_map[n] = -1;
  2.2581 +        order.push_back(n);
  2.2582 +        for (OutArcIt e(_graph, n); e != INVALID; ++e) {
  2.2583 +          Node t = _graph.runningNode(e);
  2.2584 +          if (_color_map[t] == -2) {
  2.2585 +            heap.decrease(t, heap[t] - 1);
  2.2586 +          }
  2.2587 +        }
  2.2588 +      }
  2.2589 +
  2.2590 +      for (int i = order.size() - 1; i >= 0; --i) {
  2.2591 +        std::vector<bool> forbidden(6, false);
  2.2592 +        for (OutArcIt e(_graph, order[i]); e != INVALID; ++e) {
  2.2593 +          Node t = _graph.runningNode(e);
  2.2594 +          if (_color_map[t] != -1) {
  2.2595 +            forbidden[_color_map[t]] = true;
  2.2596 +          }
  2.2597 +        }
  2.2598 +               for (int k = 0; k < 6; ++k) {
  2.2599 +          if (!forbidden[k]) {
  2.2600 +            _color_map[order[i]] = k;
  2.2601 +            break;
  2.2602 +          }
  2.2603 +        }
  2.2604 +        if (_color_map[order[i]] == -1) {
  2.2605 +          return false;
  2.2606 +        }
  2.2607 +      }
  2.2608 +      return true;
  2.2609 +    }
  2.2610 +
  2.2611 +  private:
  2.2612 +
  2.2613 +    bool recolor(const Node& u, const Node& v) {
  2.2614 +      int ucolor = _color_map[u];
  2.2615 +      int vcolor = _color_map[v];
  2.2616 +      typedef _planarity_bits::KempeFilter<IndexMap> KempeFilter;
  2.2617 +      KempeFilter filter(_color_map, ucolor, vcolor);
  2.2618 +
  2.2619 +      typedef FilterNodes<const Graph, const KempeFilter> KempeGraph;
  2.2620 +      KempeGraph kempe_graph(_graph, filter);
  2.2621 +
  2.2622 +      std::vector<Node> comp;
  2.2623 +      Bfs<KempeGraph> bfs(kempe_graph);
  2.2624 +      bfs.init();
  2.2625 +      bfs.addSource(u);
  2.2626 +      while (!bfs.emptyQueue()) {
  2.2627 +        Node n = bfs.nextNode();
  2.2628 +        if (n == v) return false;
  2.2629 +        comp.push_back(n);
  2.2630 +        bfs.processNextNode();
  2.2631 +      }
  2.2632 +
  2.2633 +      int scolor = ucolor + vcolor;
  2.2634 +      for (int i = 0; i < static_cast<int>(comp.size()); ++i) {
  2.2635 +        _color_map[comp[i]] = scolor - _color_map[comp[i]];
  2.2636 +      }
  2.2637 +
  2.2638 +      return true;
  2.2639 +    }
  2.2640 +
  2.2641 +    template <typename EmbeddingMap>
  2.2642 +    void kempeRecoloring(const Node& node, const EmbeddingMap& embedding) {
  2.2643 +      std::vector<Node> nodes;
  2.2644 +      nodes.reserve(4);
  2.2645 +
  2.2646 +      for (Arc e = OutArcIt(_graph, node); e != INVALID; e = embedding[e]) {
  2.2647 +        Node t = _graph.target(e);
  2.2648 +        if (_color_map[t] != -1) {
  2.2649 +          nodes.push_back(t);
  2.2650 +          if (nodes.size() == 4) break;
  2.2651 +        }
  2.2652 +      }
  2.2653 +
  2.2654 +      int color = _color_map[nodes[0]];
  2.2655 +      if (recolor(nodes[0], nodes[2])) {
  2.2656 +        _color_map[node] = color;
  2.2657 +      } else {
  2.2658 +        color = _color_map[nodes[1]];
  2.2659 +        recolor(nodes[1], nodes[3]);
  2.2660 +        _color_map[node] = color;
  2.2661 +      }
  2.2662 +    }
  2.2663 +
  2.2664 +  public:
  2.2665 +
  2.2666 +    /// \brief Calculates a coloring with at most five colors
  2.2667 +    ///
  2.2668 +    /// This function calculates a coloring with at most five
  2.2669 +    /// colors. The worst case time complexity of this variant is
  2.2670 +    /// quadratic in the size of the graph.
  2.2671 +    template <typename EmbeddingMap>
  2.2672 +    void runFiveColoring(const EmbeddingMap& embedding) {
  2.2673 +
  2.2674 +      typename Graph::template NodeMap<int> heap_index(_graph, -1);
  2.2675 +      BucketHeap<typename Graph::template NodeMap<int> > heap(heap_index);
  2.2676 +
  2.2677 +      for (NodeIt n(_graph); n != INVALID; ++n) {
  2.2678 +        _color_map[n] = -2;
  2.2679 +        heap.push(n, countOutArcs(_graph, n));
  2.2680 +      }
  2.2681 +
  2.2682 +      std::vector<Node> order;
  2.2683 +
  2.2684 +      while (!heap.empty()) {
  2.2685 +        Node n = heap.top();
  2.2686 +        heap.pop();
  2.2687 +        _color_map[n] = -1;
  2.2688 +        order.push_back(n);
  2.2689 +        for (OutArcIt e(_graph, n); e != INVALID; ++e) {
  2.2690 +          Node t = _graph.runningNode(e);
  2.2691 +          if (_color_map[t] == -2) {
  2.2692 +            heap.decrease(t, heap[t] - 1);
  2.2693 +          }
  2.2694 +        }
  2.2695 +      }
  2.2696 +
  2.2697 +      for (int i = order.size() - 1; i >= 0; --i) {
  2.2698 +        std::vector<bool> forbidden(5, false);
  2.2699 +        for (OutArcIt e(_graph, order[i]); e != INVALID; ++e) {
  2.2700 +          Node t = _graph.runningNode(e);
  2.2701 +          if (_color_map[t] != -1) {
  2.2702 +            forbidden[_color_map[t]] = true;
  2.2703 +          }
  2.2704 +        }
  2.2705 +        for (int k = 0; k < 5; ++k) {
  2.2706 +          if (!forbidden[k]) {
  2.2707 +            _color_map[order[i]] = k;
  2.2708 +            break;
  2.2709 +          }
  2.2710 +        }
  2.2711 +        if (_color_map[order[i]] == -1) {
  2.2712 +          kempeRecoloring(order[i], embedding);
  2.2713 +        }
  2.2714 +      }
  2.2715 +    }
  2.2716 +
  2.2717 +    /// \brief Calculates a coloring with at most five colors
  2.2718 +    ///
  2.2719 +    /// This function calculates a coloring with at most five
  2.2720 +    /// colors. The worst case time complexity of this variant is
  2.2721 +    /// quadratic in the size of the graph.
  2.2722 +    /// \return %True when the graph is planar.
  2.2723 +    bool runFiveColoring() {
  2.2724 +      PlanarEmbedding<Graph> pe(_graph);
  2.2725 +      if (!pe.run()) return false;
  2.2726 +
  2.2727 +      runFiveColoring(pe.embeddingMap());
  2.2728 +      return true;
  2.2729 +    }
  2.2730 +
  2.2731 +  private:
  2.2732 +
  2.2733 +    const Graph& _graph;
  2.2734 +    IndexMap _color_map;
  2.2735 +    Palette _palette;
  2.2736 +  };
  2.2737 +
  2.2738 +}
  2.2739 +
  2.2740 +#endif
     3.1 --- a/test/CMakeLists.txt	Mon Aug 31 20:27:38 2009 +0200
     3.2 +++ b/test/CMakeLists.txt	Wed Sep 09 15:32:03 2009 +0200
     3.3 @@ -33,6 +33,7 @@
     3.4    min_cost_arborescence_test
     3.5    min_cost_flow_test
     3.6    path_test
     3.7 +  planarity_test
     3.8    preflow_test
     3.9    radix_sort_test
    3.10    random_test
     4.1 --- a/test/Makefile.am	Mon Aug 31 20:27:38 2009 +0200
     4.2 +++ b/test/Makefile.am	Wed Sep 09 15:32:03 2009 +0200
     4.3 @@ -31,6 +31,7 @@
     4.4  	test/min_cost_arborescence_test \
     4.5  	test/min_cost_flow_test \
     4.6  	test/path_test \
     4.7 +	test/planarity_test \
     4.8  	test/preflow_test \
     4.9  	test/radix_sort_test \
    4.10  	test/random_test \
    4.11 @@ -79,6 +80,7 @@
    4.12  test_min_cost_arborescence_test_SOURCES = test/min_cost_arborescence_test.cc
    4.13  test_min_cost_flow_test_SOURCES = test/min_cost_flow_test.cc
    4.14  test_path_test_SOURCES = test/path_test.cc
    4.15 +test_planarity_test_SOURCES = test/planarity_test.cc
    4.16  test_preflow_test_SOURCES = test/preflow_test.cc
    4.17  test_radix_sort_test_SOURCES = test/radix_sort_test.cc
    4.18  test_suurballe_test_SOURCES = test/suurballe_test.cc
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/planarity_test.cc	Wed Sep 09 15:32:03 2009 +0200
     5.3 @@ -0,0 +1,259 @@
     5.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
     5.5 + *
     5.6 + * This file is a part of LEMON, a generic C++ optimization library.
     5.7 + *
     5.8 + * Copyright (C) 2003-2009
     5.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    5.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    5.11 + *
    5.12 + * Permission to use, modify and distribute this software is granted
    5.13 + * provided that this copyright notice appears in all copies. For
    5.14 + * precise terms see the accompanying LICENSE file.
    5.15 + *
    5.16 + * This software is provided "AS IS" with no warranty of any kind,
    5.17 + * express or implied, and with no claim as to its suitability for any
    5.18 + * purpose.
    5.19 + *
    5.20 + */
    5.21 +
    5.22 +#include <iostream>
    5.23 +
    5.24 +#include <lemon/planarity.h>
    5.25 +
    5.26 +#include <lemon/smart_graph.h>
    5.27 +#include <lemon/lgf_reader.h>
    5.28 +#include <lemon/connectivity.h>
    5.29 +#include <lemon/dim2.h>
    5.30 +
    5.31 +#include "test_tools.h"
    5.32 +
    5.33 +using namespace lemon;
    5.34 +using namespace lemon::dim2;
    5.35 +
    5.36 +const int lgfn = 4;
    5.37 +const std::string lgf[lgfn] = {
    5.38 +  "@nodes\n"
    5.39 +  "label\n"
    5.40 +  "0\n"
    5.41 +  "1\n"
    5.42 +  "2\n"
    5.43 +  "3\n"
    5.44 +  "4\n"
    5.45 +  "@edges\n"
    5.46 +  "     label\n"
    5.47 +  "0 1  0\n"
    5.48 +  "0 2  0\n"
    5.49 +  "0 3  0\n"
    5.50 +  "0 4  0\n"
    5.51 +  "1 2  0\n"
    5.52 +  "1 3  0\n"
    5.53 +  "1 4  0\n"
    5.54 +  "2 3  0\n"
    5.55 +  "2 4  0\n"
    5.56 +  "3 4  0\n",
    5.57 +
    5.58 +  "@nodes\n"
    5.59 +  "label\n"
    5.60 +  "0\n"
    5.61 +  "1\n"
    5.62 +  "2\n"
    5.63 +  "3\n"
    5.64 +  "4\n"
    5.65 +  "@edges\n"
    5.66 +  "     label\n"
    5.67 +  "0 1  0\n"
    5.68 +  "0 2  0\n"
    5.69 +  "0 3  0\n"
    5.70 +  "0 4  0\n"
    5.71 +  "1 2  0\n"
    5.72 +  "1 3  0\n"
    5.73 +  "2 3  0\n"
    5.74 +  "2 4  0\n"
    5.75 +  "3 4  0\n",
    5.76 +
    5.77 +  "@nodes\n"
    5.78 +  "label\n"
    5.79 +  "0\n"
    5.80 +  "1\n"
    5.81 +  "2\n"
    5.82 +  "3\n"
    5.83 +  "4\n"
    5.84 +  "5\n"
    5.85 +  "@edges\n"
    5.86 +  "     label\n"
    5.87 +  "0 3  0\n"
    5.88 +  "0 4  0\n"
    5.89 +  "0 5  0\n"
    5.90 +  "1 3  0\n"
    5.91 +  "1 4  0\n"
    5.92 +  "1 5  0\n"
    5.93 +  "2 3  0\n"
    5.94 +  "2 4  0\n"
    5.95 +  "2 5  0\n",
    5.96 +
    5.97 +  "@nodes\n"
    5.98 +  "label\n"
    5.99 +  "0\n"
   5.100 +  "1\n"
   5.101 +  "2\n"
   5.102 +  "3\n"
   5.103 +  "4\n"
   5.104 +  "5\n"
   5.105 +  "@edges\n"
   5.106 +  "     label\n"
   5.107 +  "0 3  0\n"
   5.108 +  "0 4  0\n"
   5.109 +  "0 5  0\n"
   5.110 +  "1 3  0\n"
   5.111 +  "1 4  0\n"
   5.112 +  "1 5  0\n"
   5.113 +  "2 3  0\n"
   5.114 +  "2 5  0\n"
   5.115 +};
   5.116 +
   5.117 +
   5.118 +
   5.119 +typedef SmartGraph Graph;
   5.120 +GRAPH_TYPEDEFS(Graph);
   5.121 +
   5.122 +typedef PlanarEmbedding<SmartGraph> PE;
   5.123 +typedef PlanarDrawing<SmartGraph> PD;
   5.124 +typedef PlanarColoring<SmartGraph> PC;
   5.125 +
   5.126 +void checkEmbedding(const Graph& graph, PE& pe) {
   5.127 +  int face_num = 0;
   5.128 +
   5.129 +  Graph::ArcMap<int> face(graph, -1);
   5.130 +
   5.131 +  for (ArcIt a(graph); a != INVALID; ++a) {
   5.132 +    if (face[a] == -1) {
   5.133 +      Arc b = a;
   5.134 +      while (face[b] == -1) {
   5.135 +        face[b] = face_num;
   5.136 +        b = pe.next(graph.oppositeArc(b));
   5.137 +      }
   5.138 +      check(face[b] == face_num, "Wrong face");
   5.139 +      ++face_num;
   5.140 +    }
   5.141 +  }
   5.142 +  check(face_num + countNodes(graph) - countConnectedComponents(graph) ==
   5.143 +        countEdges(graph) + 1, "Euler test does not passed");
   5.144 +}
   5.145 +
   5.146 +void checkKuratowski(const Graph& graph, PE& pe) {
   5.147 +  std::map<int, int> degs;
   5.148 +  for (NodeIt n(graph); n != INVALID; ++n) {
   5.149 +    int deg = 0;
   5.150 +    for (IncEdgeIt e(graph, n); e != INVALID; ++e) {
   5.151 +      if (pe.kuratowski(e)) {
   5.152 +        ++deg;
   5.153 +      }
   5.154 +    }
   5.155 +    ++degs[deg];
   5.156 +  }
   5.157 +  for (std::map<int, int>::iterator it = degs.begin(); it != degs.end(); ++it) {
   5.158 +    check(it->first == 0 || it->first == 2 ||
   5.159 +          (it->first == 3 && it->second == 6) ||
   5.160 +          (it->first == 4 && it->second == 5),
   5.161 +          "Wrong degree in Kuratowski graph");
   5.162 +  }
   5.163 +
   5.164 +  // Not full test
   5.165 +  check((degs[3] == 0) != (degs[4] == 0), "Wrong Kuratowski graph");
   5.166 +}
   5.167 +
   5.168 +bool intersect(Point<int> e1, Point<int> e2, Point<int> f1, Point<int> f2) {
   5.169 +  int l, r;
   5.170 +  if (std::min(e1.x, e2.x) > std::max(f1.x, f2.x)) return false;
   5.171 +  if (std::max(e1.x, e2.x) < std::min(f1.x, f2.x)) return false;
   5.172 +  if (std::min(e1.y, e2.y) > std::max(f1.y, f2.y)) return false;
   5.173 +  if (std::max(e1.y, e2.y) < std::min(f1.y, f2.y)) return false;
   5.174 +
   5.175 +  l = (e2.x - e1.x) * (f1.y - e1.y) - (e2.y - e1.y) * (f1.x - e1.x);
   5.176 +  r = (e2.x - e1.x) * (f2.y - e1.y) - (e2.y - e1.y) * (f2.x - e1.x);
   5.177 +  if (!((l >= 0 && r <= 0) || (l <= 0 && r >= 0))) return false;
   5.178 +  l = (f2.x - f1.x) * (e1.y - f1.y) - (f2.y - f1.y) * (e1.x - f1.x);
   5.179 +  r = (f2.x - f1.x) * (e2.y - f1.y) - (f2.y - f1.y) * (e2.x - f1.x);
   5.180 +  if (!((l >= 0 && r <= 0) || (l <= 0 && r >= 0))) return false;
   5.181 +  return true;
   5.182 +}
   5.183 +
   5.184 +bool collinear(Point<int> p, Point<int> q, Point<int> r) {
   5.185 +  int v;
   5.186 +  v = (q.x - p.x) * (r.y - p.y) - (q.y - p.y) * (r.x - p.x);
   5.187 +  if (v != 0) return false;
   5.188 +  v = (q.x - p.x) * (r.x - p.x) + (q.y - p.y) * (r.y - p.y);
   5.189 +  if (v < 0) return false;
   5.190 +  return true;
   5.191 +}
   5.192 +
   5.193 +void checkDrawing(const Graph& graph, PD& pd) {
   5.194 +  for (Graph::NodeIt n(graph); n != INVALID; ++n) {
   5.195 +    Graph::NodeIt m(n);
   5.196 +    for (++m; m != INVALID; ++m) {
   5.197 +      check(pd[m] != pd[n], "Two nodes with identical coordinates");
   5.198 +    }
   5.199 +  }
   5.200 +
   5.201 +  for (Graph::EdgeIt e(graph); e != INVALID; ++e) {
   5.202 +    for (Graph::EdgeIt f(e); f != e; ++f) {
   5.203 +      Point<int> e1 = pd[graph.u(e)];
   5.204 +      Point<int> e2 = pd[graph.v(e)];
   5.205 +      Point<int> f1 = pd[graph.u(f)];
   5.206 +      Point<int> f2 = pd[graph.v(f)];
   5.207 +
   5.208 +      if (graph.u(e) == graph.u(f)) {
   5.209 +        check(!collinear(e1, e2, f2), "Wrong drawing");
   5.210 +      } else if (graph.u(e) == graph.v(f)) {
   5.211 +        check(!collinear(e1, e2, f1), "Wrong drawing");
   5.212 +      } else if (graph.v(e) == graph.u(f)) {
   5.213 +        check(!collinear(e2, e1, f2), "Wrong drawing");
   5.214 +      } else if (graph.v(e) == graph.v(f)) {
   5.215 +        check(!collinear(e2, e1, f1), "Wrong drawing");
   5.216 +      } else {
   5.217 +        check(!intersect(e1, e2, f1, f2), "Wrong drawing");
   5.218 +      }
   5.219 +    }
   5.220 +  }
   5.221 +}
   5.222 +
   5.223 +void checkColoring(const Graph& graph, PC& pc, int num) {
   5.224 +  for (NodeIt n(graph); n != INVALID; ++n) {
   5.225 +    check(pc.colorIndex(n) >= 0 && pc.colorIndex(n) < num,
   5.226 +          "Wrong coloring");
   5.227 +  }
   5.228 +  for (EdgeIt e(graph); e != INVALID; ++e) {
   5.229 +    check(pc.colorIndex(graph.u(e)) != pc.colorIndex(graph.v(e)),
   5.230 +          "Wrong coloring");
   5.231 +  }
   5.232 +}
   5.233 +
   5.234 +int main() {
   5.235 +
   5.236 +  for (int i = 0; i < lgfn; ++i) {
   5.237 +    std::istringstream lgfs(lgf[i]);
   5.238 +
   5.239 +    SmartGraph graph;
   5.240 +    graphReader(graph, lgfs).run();
   5.241 +
   5.242 +    check(simpleGraph(graph), "Test graphs must be simple");
   5.243 +
   5.244 +    PE pe(graph);
   5.245 +    if (pe.run()) {
   5.246 +      checkEmbedding(graph, pe);
   5.247 +
   5.248 +      PlanarDrawing<Graph> pd(graph);
   5.249 +      pd.run(pe.embedding());
   5.250 +      checkDrawing(graph, pd);
   5.251 +
   5.252 +      PlanarColoring<Graph> pc(graph);
   5.253 +      pc.runFiveColoring(pe.embedding());
   5.254 +      checkColoring(graph, pc, 5);
   5.255 +
   5.256 +    } else {
   5.257 +      checkKuratowski(graph, pe);
   5.258 +    }
   5.259 +  }
   5.260 +
   5.261 +  return 0;
   5.262 +}