1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/planarity.h Thu Dec 10 17:18:25 2009 +0100
1.3 @@ -0,0 +1,2737 @@
1.4 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
1.5 + *
1.6 + * This file is a part of LEMON, a generic C++ optimization library.
1.7 + *
1.8 + * Copyright (C) 2003-2009
1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 + *
1.12 + * Permission to use, modify and distribute this software is granted
1.13 + * provided that this copyright notice appears in all copies. For
1.14 + * precise terms see the accompanying LICENSE file.
1.15 + *
1.16 + * This software is provided "AS IS" with no warranty of any kind,
1.17 + * express or implied, and with no claim as to its suitability for any
1.18 + * purpose.
1.19 + *
1.20 + */
1.21 +
1.22 +#ifndef LEMON_PLANARITY_H
1.23 +#define LEMON_PLANARITY_H
1.24 +
1.25 +/// \ingroup planar
1.26 +/// \file
1.27 +/// \brief Planarity checking, embedding, drawing and coloring
1.28 +
1.29 +#include <vector>
1.30 +#include <list>
1.31 +
1.32 +#include <lemon/dfs.h>
1.33 +#include <lemon/bfs.h>
1.34 +#include <lemon/radix_sort.h>
1.35 +#include <lemon/maps.h>
1.36 +#include <lemon/path.h>
1.37 +#include <lemon/bucket_heap.h>
1.38 +#include <lemon/adaptors.h>
1.39 +#include <lemon/edge_set.h>
1.40 +#include <lemon/color.h>
1.41 +#include <lemon/dim2.h>
1.42 +
1.43 +namespace lemon {
1.44 +
1.45 + namespace _planarity_bits {
1.46 +
1.47 + template <typename Graph>
1.48 + struct PlanarityVisitor : DfsVisitor<Graph> {
1.49 +
1.50 + TEMPLATE_GRAPH_TYPEDEFS(Graph);
1.51 +
1.52 + typedef typename Graph::template NodeMap<Arc> PredMap;
1.53 +
1.54 + typedef typename Graph::template EdgeMap<bool> TreeMap;
1.55 +
1.56 + typedef typename Graph::template NodeMap<int> OrderMap;
1.57 + typedef std::vector<Node> OrderList;
1.58 +
1.59 + typedef typename Graph::template NodeMap<int> LowMap;
1.60 + typedef typename Graph::template NodeMap<int> AncestorMap;
1.61 +
1.62 + PlanarityVisitor(const Graph& graph,
1.63 + PredMap& pred_map, TreeMap& tree_map,
1.64 + OrderMap& order_map, OrderList& order_list,
1.65 + AncestorMap& ancestor_map, LowMap& low_map)
1.66 + : _graph(graph), _pred_map(pred_map), _tree_map(tree_map),
1.67 + _order_map(order_map), _order_list(order_list),
1.68 + _ancestor_map(ancestor_map), _low_map(low_map) {}
1.69 +
1.70 + void reach(const Node& node) {
1.71 + _order_map[node] = _order_list.size();
1.72 + _low_map[node] = _order_list.size();
1.73 + _ancestor_map[node] = _order_list.size();
1.74 + _order_list.push_back(node);
1.75 + }
1.76 +
1.77 + void discover(const Arc& arc) {
1.78 + Node source = _graph.source(arc);
1.79 + Node target = _graph.target(arc);
1.80 +
1.81 + _tree_map[arc] = true;
1.82 + _pred_map[target] = arc;
1.83 + }
1.84 +
1.85 + void examine(const Arc& arc) {
1.86 + Node source = _graph.source(arc);
1.87 + Node target = _graph.target(arc);
1.88 +
1.89 + if (_order_map[target] < _order_map[source] && !_tree_map[arc]) {
1.90 + if (_low_map[source] > _order_map[target]) {
1.91 + _low_map[source] = _order_map[target];
1.92 + }
1.93 + if (_ancestor_map[source] > _order_map[target]) {
1.94 + _ancestor_map[source] = _order_map[target];
1.95 + }
1.96 + }
1.97 + }
1.98 +
1.99 + void backtrack(const Arc& arc) {
1.100 + Node source = _graph.source(arc);
1.101 + Node target = _graph.target(arc);
1.102 +
1.103 + if (_low_map[source] > _low_map[target]) {
1.104 + _low_map[source] = _low_map[target];
1.105 + }
1.106 + }
1.107 +
1.108 + const Graph& _graph;
1.109 + PredMap& _pred_map;
1.110 + TreeMap& _tree_map;
1.111 + OrderMap& _order_map;
1.112 + OrderList& _order_list;
1.113 + AncestorMap& _ancestor_map;
1.114 + LowMap& _low_map;
1.115 + };
1.116 +
1.117 + template <typename Graph, bool embedding = true>
1.118 + struct NodeDataNode {
1.119 + int prev, next;
1.120 + int visited;
1.121 + typename Graph::Arc first;
1.122 + bool inverted;
1.123 + };
1.124 +
1.125 + template <typename Graph>
1.126 + struct NodeDataNode<Graph, false> {
1.127 + int prev, next;
1.128 + int visited;
1.129 + };
1.130 +
1.131 + template <typename Graph>
1.132 + struct ChildListNode {
1.133 + typedef typename Graph::Node Node;
1.134 + Node first;
1.135 + Node prev, next;
1.136 + };
1.137 +
1.138 + template <typename Graph>
1.139 + struct ArcListNode {
1.140 + typename Graph::Arc prev, next;
1.141 + };
1.142 +
1.143 + template <typename Graph>
1.144 + class PlanarityChecking {
1.145 + private:
1.146 +
1.147 + TEMPLATE_GRAPH_TYPEDEFS(Graph);
1.148 +
1.149 + const Graph& _graph;
1.150 +
1.151 + private:
1.152 +
1.153 + typedef typename Graph::template NodeMap<Arc> PredMap;
1.154 +
1.155 + typedef typename Graph::template EdgeMap<bool> TreeMap;
1.156 +
1.157 + typedef typename Graph::template NodeMap<int> OrderMap;
1.158 + typedef std::vector<Node> OrderList;
1.159 +
1.160 + typedef typename Graph::template NodeMap<int> LowMap;
1.161 + typedef typename Graph::template NodeMap<int> AncestorMap;
1.162 +
1.163 + typedef _planarity_bits::NodeDataNode<Graph> NodeDataNode;
1.164 + typedef std::vector<NodeDataNode> NodeData;
1.165 +
1.166 + typedef _planarity_bits::ChildListNode<Graph> ChildListNode;
1.167 + typedef typename Graph::template NodeMap<ChildListNode> ChildLists;
1.168 +
1.169 + typedef typename Graph::template NodeMap<std::list<int> > MergeRoots;
1.170 +
1.171 + typedef typename Graph::template NodeMap<bool> EmbedArc;
1.172 +
1.173 + public:
1.174 +
1.175 + PlanarityChecking(const Graph& graph) : _graph(graph) {}
1.176 +
1.177 + bool run() {
1.178 + typedef _planarity_bits::PlanarityVisitor<Graph> Visitor;
1.179 +
1.180 + PredMap pred_map(_graph, INVALID);
1.181 + TreeMap tree_map(_graph, false);
1.182 +
1.183 + OrderMap order_map(_graph, -1);
1.184 + OrderList order_list;
1.185 +
1.186 + AncestorMap ancestor_map(_graph, -1);
1.187 + LowMap low_map(_graph, -1);
1.188 +
1.189 + Visitor visitor(_graph, pred_map, tree_map,
1.190 + order_map, order_list, ancestor_map, low_map);
1.191 + DfsVisit<Graph, Visitor> visit(_graph, visitor);
1.192 + visit.run();
1.193 +
1.194 + ChildLists child_lists(_graph);
1.195 + createChildLists(tree_map, order_map, low_map, child_lists);
1.196 +
1.197 + NodeData node_data(2 * order_list.size());
1.198 +
1.199 + EmbedArc embed_arc(_graph, false);
1.200 +
1.201 + MergeRoots merge_roots(_graph);
1.202 +
1.203 + for (int i = order_list.size() - 1; i >= 0; --i) {
1.204 +
1.205 + Node node = order_list[i];
1.206 +
1.207 + Node source = node;
1.208 + for (OutArcIt e(_graph, node); e != INVALID; ++e) {
1.209 + Node target = _graph.target(e);
1.210 +
1.211 + if (order_map[source] < order_map[target] && tree_map[e]) {
1.212 + initFace(target, node_data, order_map, order_list);
1.213 + }
1.214 + }
1.215 +
1.216 + for (OutArcIt e(_graph, node); e != INVALID; ++e) {
1.217 + Node target = _graph.target(e);
1.218 +
1.219 + if (order_map[source] < order_map[target] && !tree_map[e]) {
1.220 + embed_arc[target] = true;
1.221 + walkUp(target, source, i, pred_map, low_map,
1.222 + order_map, order_list, node_data, merge_roots);
1.223 + }
1.224 + }
1.225 +
1.226 + for (typename MergeRoots::Value::iterator it =
1.227 + merge_roots[node].begin();
1.228 + it != merge_roots[node].end(); ++it) {
1.229 + int rn = *it;
1.230 + walkDown(rn, i, node_data, order_list, child_lists,
1.231 + ancestor_map, low_map, embed_arc, merge_roots);
1.232 + }
1.233 + merge_roots[node].clear();
1.234 +
1.235 + for (OutArcIt e(_graph, node); e != INVALID; ++e) {
1.236 + Node target = _graph.target(e);
1.237 +
1.238 + if (order_map[source] < order_map[target] && !tree_map[e]) {
1.239 + if (embed_arc[target]) {
1.240 + return false;
1.241 + }
1.242 + }
1.243 + }
1.244 + }
1.245 +
1.246 + return true;
1.247 + }
1.248 +
1.249 + private:
1.250 +
1.251 + void createChildLists(const TreeMap& tree_map, const OrderMap& order_map,
1.252 + const LowMap& low_map, ChildLists& child_lists) {
1.253 +
1.254 + for (NodeIt n(_graph); n != INVALID; ++n) {
1.255 + Node source = n;
1.256 +
1.257 + std::vector<Node> targets;
1.258 + for (OutArcIt e(_graph, n); e != INVALID; ++e) {
1.259 + Node target = _graph.target(e);
1.260 +
1.261 + if (order_map[source] < order_map[target] && tree_map[e]) {
1.262 + targets.push_back(target);
1.263 + }
1.264 + }
1.265 +
1.266 + if (targets.size() == 0) {
1.267 + child_lists[source].first = INVALID;
1.268 + } else if (targets.size() == 1) {
1.269 + child_lists[source].first = targets[0];
1.270 + child_lists[targets[0]].prev = INVALID;
1.271 + child_lists[targets[0]].next = INVALID;
1.272 + } else {
1.273 + radixSort(targets.begin(), targets.end(), mapToFunctor(low_map));
1.274 + for (int i = 1; i < int(targets.size()); ++i) {
1.275 + child_lists[targets[i]].prev = targets[i - 1];
1.276 + child_lists[targets[i - 1]].next = targets[i];
1.277 + }
1.278 + child_lists[targets.back()].next = INVALID;
1.279 + child_lists[targets.front()].prev = INVALID;
1.280 + child_lists[source].first = targets.front();
1.281 + }
1.282 + }
1.283 + }
1.284 +
1.285 + void walkUp(const Node& node, Node root, int rorder,
1.286 + const PredMap& pred_map, const LowMap& low_map,
1.287 + const OrderMap& order_map, const OrderList& order_list,
1.288 + NodeData& node_data, MergeRoots& merge_roots) {
1.289 +
1.290 + int na, nb;
1.291 + bool da, db;
1.292 +
1.293 + na = nb = order_map[node];
1.294 + da = true; db = false;
1.295 +
1.296 + while (true) {
1.297 +
1.298 + if (node_data[na].visited == rorder) break;
1.299 + if (node_data[nb].visited == rorder) break;
1.300 +
1.301 + node_data[na].visited = rorder;
1.302 + node_data[nb].visited = rorder;
1.303 +
1.304 + int rn = -1;
1.305 +
1.306 + if (na >= int(order_list.size())) {
1.307 + rn = na;
1.308 + } else if (nb >= int(order_list.size())) {
1.309 + rn = nb;
1.310 + }
1.311 +
1.312 + if (rn == -1) {
1.313 + int nn;
1.314 +
1.315 + nn = da ? node_data[na].prev : node_data[na].next;
1.316 + da = node_data[nn].prev != na;
1.317 + na = nn;
1.318 +
1.319 + nn = db ? node_data[nb].prev : node_data[nb].next;
1.320 + db = node_data[nn].prev != nb;
1.321 + nb = nn;
1.322 +
1.323 + } else {
1.324 +
1.325 + Node rep = order_list[rn - order_list.size()];
1.326 + Node parent = _graph.source(pred_map[rep]);
1.327 +
1.328 + if (low_map[rep] < rorder) {
1.329 + merge_roots[parent].push_back(rn);
1.330 + } else {
1.331 + merge_roots[parent].push_front(rn);
1.332 + }
1.333 +
1.334 + if (parent != root) {
1.335 + na = nb = order_map[parent];
1.336 + da = true; db = false;
1.337 + } else {
1.338 + break;
1.339 + }
1.340 + }
1.341 + }
1.342 + }
1.343 +
1.344 + void walkDown(int rn, int rorder, NodeData& node_data,
1.345 + OrderList& order_list, ChildLists& child_lists,
1.346 + AncestorMap& ancestor_map, LowMap& low_map,
1.347 + EmbedArc& embed_arc, MergeRoots& merge_roots) {
1.348 +
1.349 + std::vector<std::pair<int, bool> > merge_stack;
1.350 +
1.351 + for (int di = 0; di < 2; ++di) {
1.352 + bool rd = di == 0;
1.353 + int pn = rn;
1.354 + int n = rd ? node_data[rn].next : node_data[rn].prev;
1.355 +
1.356 + while (n != rn) {
1.357 +
1.358 + Node node = order_list[n];
1.359 +
1.360 + if (embed_arc[node]) {
1.361 +
1.362 + // Merging components on the critical path
1.363 + while (!merge_stack.empty()) {
1.364 +
1.365 + // Component root
1.366 + int cn = merge_stack.back().first;
1.367 + bool cd = merge_stack.back().second;
1.368 + merge_stack.pop_back();
1.369 +
1.370 + // Parent of component
1.371 + int dn = merge_stack.back().first;
1.372 + bool dd = merge_stack.back().second;
1.373 + merge_stack.pop_back();
1.374 +
1.375 + Node parent = order_list[dn];
1.376 +
1.377 + // Erasing from merge_roots
1.378 + merge_roots[parent].pop_front();
1.379 +
1.380 + Node child = order_list[cn - order_list.size()];
1.381 +
1.382 + // Erasing from child_lists
1.383 + if (child_lists[child].prev != INVALID) {
1.384 + child_lists[child_lists[child].prev].next =
1.385 + child_lists[child].next;
1.386 + } else {
1.387 + child_lists[parent].first = child_lists[child].next;
1.388 + }
1.389 +
1.390 + if (child_lists[child].next != INVALID) {
1.391 + child_lists[child_lists[child].next].prev =
1.392 + child_lists[child].prev;
1.393 + }
1.394 +
1.395 + // Merging external faces
1.396 + {
1.397 + int en = cn;
1.398 + cn = cd ? node_data[cn].prev : node_data[cn].next;
1.399 + cd = node_data[cn].next == en;
1.400 +
1.401 + }
1.402 +
1.403 + if (cd) node_data[cn].next = dn; else node_data[cn].prev = dn;
1.404 + if (dd) node_data[dn].prev = cn; else node_data[dn].next = cn;
1.405 +
1.406 + }
1.407 +
1.408 + bool d = pn == node_data[n].prev;
1.409 +
1.410 + if (node_data[n].prev == node_data[n].next &&
1.411 + node_data[n].inverted) {
1.412 + d = !d;
1.413 + }
1.414 +
1.415 + // Embedding arc into external face
1.416 + if (rd) node_data[rn].next = n; else node_data[rn].prev = n;
1.417 + if (d) node_data[n].prev = rn; else node_data[n].next = rn;
1.418 + pn = rn;
1.419 +
1.420 + embed_arc[order_list[n]] = false;
1.421 + }
1.422 +
1.423 + if (!merge_roots[node].empty()) {
1.424 +
1.425 + bool d = pn == node_data[n].prev;
1.426 +
1.427 + merge_stack.push_back(std::make_pair(n, d));
1.428 +
1.429 + int rn = merge_roots[node].front();
1.430 +
1.431 + int xn = node_data[rn].next;
1.432 + Node xnode = order_list[xn];
1.433 +
1.434 + int yn = node_data[rn].prev;
1.435 + Node ynode = order_list[yn];
1.436 +
1.437 + bool rd;
1.438 + if (!external(xnode, rorder, child_lists,
1.439 + ancestor_map, low_map)) {
1.440 + rd = true;
1.441 + } else if (!external(ynode, rorder, child_lists,
1.442 + ancestor_map, low_map)) {
1.443 + rd = false;
1.444 + } else if (pertinent(xnode, embed_arc, merge_roots)) {
1.445 + rd = true;
1.446 + } else {
1.447 + rd = false;
1.448 + }
1.449 +
1.450 + merge_stack.push_back(std::make_pair(rn, rd));
1.451 +
1.452 + pn = rn;
1.453 + n = rd ? xn : yn;
1.454 +
1.455 + } else if (!external(node, rorder, child_lists,
1.456 + ancestor_map, low_map)) {
1.457 + int nn = (node_data[n].next != pn ?
1.458 + node_data[n].next : node_data[n].prev);
1.459 +
1.460 + bool nd = n == node_data[nn].prev;
1.461 +
1.462 + if (nd) node_data[nn].prev = pn;
1.463 + else node_data[nn].next = pn;
1.464 +
1.465 + if (n == node_data[pn].prev) node_data[pn].prev = nn;
1.466 + else node_data[pn].next = nn;
1.467 +
1.468 + node_data[nn].inverted =
1.469 + (node_data[nn].prev == node_data[nn].next && nd != rd);
1.470 +
1.471 + n = nn;
1.472 + }
1.473 + else break;
1.474 +
1.475 + }
1.476 +
1.477 + if (!merge_stack.empty() || n == rn) {
1.478 + break;
1.479 + }
1.480 + }
1.481 + }
1.482 +
1.483 + void initFace(const Node& node, NodeData& node_data,
1.484 + const OrderMap& order_map, const OrderList& order_list) {
1.485 + int n = order_map[node];
1.486 + int rn = n + order_list.size();
1.487 +
1.488 + node_data[n].next = node_data[n].prev = rn;
1.489 + node_data[rn].next = node_data[rn].prev = n;
1.490 +
1.491 + node_data[n].visited = order_list.size();
1.492 + node_data[rn].visited = order_list.size();
1.493 +
1.494 + }
1.495 +
1.496 + bool external(const Node& node, int rorder,
1.497 + ChildLists& child_lists, AncestorMap& ancestor_map,
1.498 + LowMap& low_map) {
1.499 + Node child = child_lists[node].first;
1.500 +
1.501 + if (child != INVALID) {
1.502 + if (low_map[child] < rorder) return true;
1.503 + }
1.504 +
1.505 + if (ancestor_map[node] < rorder) return true;
1.506 +
1.507 + return false;
1.508 + }
1.509 +
1.510 + bool pertinent(const Node& node, const EmbedArc& embed_arc,
1.511 + const MergeRoots& merge_roots) {
1.512 + return !merge_roots[node].empty() || embed_arc[node];
1.513 + }
1.514 +
1.515 + };
1.516 +
1.517 + }
1.518 +
1.519 + /// \ingroup planar
1.520 + ///
1.521 + /// \brief Planarity checking of an undirected simple graph
1.522 + ///
1.523 + /// This function implements the Boyer-Myrvold algorithm for
1.524 + /// planarity checking of an undirected graph. It is a simplified
1.525 + /// version of the PlanarEmbedding algorithm class because neither
1.526 + /// the embedding nor the kuratowski subdivisons are not computed.
1.527 + template <typename GR>
1.528 + bool checkPlanarity(const GR& graph) {
1.529 + _planarity_bits::PlanarityChecking<GR> pc(graph);
1.530 + return pc.run();
1.531 + }
1.532 +
1.533 + /// \ingroup planar
1.534 + ///
1.535 + /// \brief Planar embedding of an undirected simple graph
1.536 + ///
1.537 + /// This class implements the Boyer-Myrvold algorithm for planar
1.538 + /// embedding of an undirected graph. The planar embedding is an
1.539 + /// ordering of the outgoing edges of the nodes, which is a possible
1.540 + /// configuration to draw the graph in the plane. If there is not
1.541 + /// such ordering then the graph contains a \f$ K_5 \f$ (full graph
1.542 + /// with 5 nodes) or a \f$ K_{3,3} \f$ (complete bipartite graph on
1.543 + /// 3 ANode and 3 BNode) subdivision.
1.544 + ///
1.545 + /// The current implementation calculates either an embedding or a
1.546 + /// Kuratowski subdivision. The running time of the algorithm is
1.547 + /// \f$ O(n) \f$.
1.548 + template <typename Graph>
1.549 + class PlanarEmbedding {
1.550 + private:
1.551 +
1.552 + TEMPLATE_GRAPH_TYPEDEFS(Graph);
1.553 +
1.554 + const Graph& _graph;
1.555 + typename Graph::template ArcMap<Arc> _embedding;
1.556 +
1.557 + typename Graph::template EdgeMap<bool> _kuratowski;
1.558 +
1.559 + private:
1.560 +
1.561 + typedef typename Graph::template NodeMap<Arc> PredMap;
1.562 +
1.563 + typedef typename Graph::template EdgeMap<bool> TreeMap;
1.564 +
1.565 + typedef typename Graph::template NodeMap<int> OrderMap;
1.566 + typedef std::vector<Node> OrderList;
1.567 +
1.568 + typedef typename Graph::template NodeMap<int> LowMap;
1.569 + typedef typename Graph::template NodeMap<int> AncestorMap;
1.570 +
1.571 + typedef _planarity_bits::NodeDataNode<Graph> NodeDataNode;
1.572 + typedef std::vector<NodeDataNode> NodeData;
1.573 +
1.574 + typedef _planarity_bits::ChildListNode<Graph> ChildListNode;
1.575 + typedef typename Graph::template NodeMap<ChildListNode> ChildLists;
1.576 +
1.577 + typedef typename Graph::template NodeMap<std::list<int> > MergeRoots;
1.578 +
1.579 + typedef typename Graph::template NodeMap<Arc> EmbedArc;
1.580 +
1.581 + typedef _planarity_bits::ArcListNode<Graph> ArcListNode;
1.582 + typedef typename Graph::template ArcMap<ArcListNode> ArcLists;
1.583 +
1.584 + typedef typename Graph::template NodeMap<bool> FlipMap;
1.585 +
1.586 + typedef typename Graph::template NodeMap<int> TypeMap;
1.587 +
1.588 + enum IsolatorNodeType {
1.589 + HIGHX = 6, LOWX = 7,
1.590 + HIGHY = 8, LOWY = 9,
1.591 + ROOT = 10, PERTINENT = 11,
1.592 + INTERNAL = 12
1.593 + };
1.594 +
1.595 + public:
1.596 +
1.597 + /// \brief The map for store of embedding
1.598 + typedef typename Graph::template ArcMap<Arc> EmbeddingMap;
1.599 +
1.600 + /// \brief Constructor
1.601 + ///
1.602 + /// \note The graph should be simple, i.e. parallel and loop arc
1.603 + /// free.
1.604 + PlanarEmbedding(const Graph& graph)
1.605 + : _graph(graph), _embedding(_graph), _kuratowski(graph, false) {}
1.606 +
1.607 + /// \brief Runs the algorithm.
1.608 + ///
1.609 + /// Runs the algorithm.
1.610 + /// \param kuratowski If the parameter is false, then the
1.611 + /// algorithm does not compute a Kuratowski subdivision.
1.612 + ///\return %True when the graph is planar.
1.613 + bool run(bool kuratowski = true) {
1.614 + typedef _planarity_bits::PlanarityVisitor<Graph> Visitor;
1.615 +
1.616 + PredMap pred_map(_graph, INVALID);
1.617 + TreeMap tree_map(_graph, false);
1.618 +
1.619 + OrderMap order_map(_graph, -1);
1.620 + OrderList order_list;
1.621 +
1.622 + AncestorMap ancestor_map(_graph, -1);
1.623 + LowMap low_map(_graph, -1);
1.624 +
1.625 + Visitor visitor(_graph, pred_map, tree_map,
1.626 + order_map, order_list, ancestor_map, low_map);
1.627 + DfsVisit<Graph, Visitor> visit(_graph, visitor);
1.628 + visit.run();
1.629 +
1.630 + ChildLists child_lists(_graph);
1.631 + createChildLists(tree_map, order_map, low_map, child_lists);
1.632 +
1.633 + NodeData node_data(2 * order_list.size());
1.634 +
1.635 + EmbedArc embed_arc(_graph, INVALID);
1.636 +
1.637 + MergeRoots merge_roots(_graph);
1.638 +
1.639 + ArcLists arc_lists(_graph);
1.640 +
1.641 + FlipMap flip_map(_graph, false);
1.642 +
1.643 + for (int i = order_list.size() - 1; i >= 0; --i) {
1.644 +
1.645 + Node node = order_list[i];
1.646 +
1.647 + node_data[i].first = INVALID;
1.648 +
1.649 + Node source = node;
1.650 + for (OutArcIt e(_graph, node); e != INVALID; ++e) {
1.651 + Node target = _graph.target(e);
1.652 +
1.653 + if (order_map[source] < order_map[target] && tree_map[e]) {
1.654 + initFace(target, arc_lists, node_data,
1.655 + pred_map, order_map, order_list);
1.656 + }
1.657 + }
1.658 +
1.659 + for (OutArcIt e(_graph, node); e != INVALID; ++e) {
1.660 + Node target = _graph.target(e);
1.661 +
1.662 + if (order_map[source] < order_map[target] && !tree_map[e]) {
1.663 + embed_arc[target] = e;
1.664 + walkUp(target, source, i, pred_map, low_map,
1.665 + order_map, order_list, node_data, merge_roots);
1.666 + }
1.667 + }
1.668 +
1.669 + for (typename MergeRoots::Value::iterator it =
1.670 + merge_roots[node].begin(); it != merge_roots[node].end(); ++it) {
1.671 + int rn = *it;
1.672 + walkDown(rn, i, node_data, arc_lists, flip_map, order_list,
1.673 + child_lists, ancestor_map, low_map, embed_arc, merge_roots);
1.674 + }
1.675 + merge_roots[node].clear();
1.676 +
1.677 + for (OutArcIt e(_graph, node); e != INVALID; ++e) {
1.678 + Node target = _graph.target(e);
1.679 +
1.680 + if (order_map[source] < order_map[target] && !tree_map[e]) {
1.681 + if (embed_arc[target] != INVALID) {
1.682 + if (kuratowski) {
1.683 + isolateKuratowski(e, node_data, arc_lists, flip_map,
1.684 + order_map, order_list, pred_map, child_lists,
1.685 + ancestor_map, low_map,
1.686 + embed_arc, merge_roots);
1.687 + }
1.688 + return false;
1.689 + }
1.690 + }
1.691 + }
1.692 + }
1.693 +
1.694 + for (int i = 0; i < int(order_list.size()); ++i) {
1.695 +
1.696 + mergeRemainingFaces(order_list[i], node_data, order_list, order_map,
1.697 + child_lists, arc_lists);
1.698 + storeEmbedding(order_list[i], node_data, order_map, pred_map,
1.699 + arc_lists, flip_map);
1.700 + }
1.701 +
1.702 + return true;
1.703 + }
1.704 +
1.705 + /// \brief Gives back the successor of an arc
1.706 + ///
1.707 + /// Gives back the successor of an arc. This function makes
1.708 + /// possible to query the cyclic order of the outgoing arcs from
1.709 + /// a node.
1.710 + Arc next(const Arc& arc) const {
1.711 + return _embedding[arc];
1.712 + }
1.713 +
1.714 + /// \brief Gives back the calculated embedding map
1.715 + ///
1.716 + /// The returned map contains the successor of each arc in the
1.717 + /// graph.
1.718 + const EmbeddingMap& embeddingMap() const {
1.719 + return _embedding;
1.720 + }
1.721 +
1.722 + /// \brief Gives back true if the undirected arc is in the
1.723 + /// kuratowski subdivision
1.724 + ///
1.725 + /// Gives back true if the undirected arc is in the kuratowski
1.726 + /// subdivision
1.727 + /// \note The \c run() had to be called with true value.
1.728 + bool kuratowski(const Edge& edge) {
1.729 + return _kuratowski[edge];
1.730 + }
1.731 +
1.732 + private:
1.733 +
1.734 + void createChildLists(const TreeMap& tree_map, const OrderMap& order_map,
1.735 + const LowMap& low_map, ChildLists& child_lists) {
1.736 +
1.737 + for (NodeIt n(_graph); n != INVALID; ++n) {
1.738 + Node source = n;
1.739 +
1.740 + std::vector<Node> targets;
1.741 + for (OutArcIt e(_graph, n); e != INVALID; ++e) {
1.742 + Node target = _graph.target(e);
1.743 +
1.744 + if (order_map[source] < order_map[target] && tree_map[e]) {
1.745 + targets.push_back(target);
1.746 + }
1.747 + }
1.748 +
1.749 + if (targets.size() == 0) {
1.750 + child_lists[source].first = INVALID;
1.751 + } else if (targets.size() == 1) {
1.752 + child_lists[source].first = targets[0];
1.753 + child_lists[targets[0]].prev = INVALID;
1.754 + child_lists[targets[0]].next = INVALID;
1.755 + } else {
1.756 + radixSort(targets.begin(), targets.end(), mapToFunctor(low_map));
1.757 + for (int i = 1; i < int(targets.size()); ++i) {
1.758 + child_lists[targets[i]].prev = targets[i - 1];
1.759 + child_lists[targets[i - 1]].next = targets[i];
1.760 + }
1.761 + child_lists[targets.back()].next = INVALID;
1.762 + child_lists[targets.front()].prev = INVALID;
1.763 + child_lists[source].first = targets.front();
1.764 + }
1.765 + }
1.766 + }
1.767 +
1.768 + void walkUp(const Node& node, Node root, int rorder,
1.769 + const PredMap& pred_map, const LowMap& low_map,
1.770 + const OrderMap& order_map, const OrderList& order_list,
1.771 + NodeData& node_data, MergeRoots& merge_roots) {
1.772 +
1.773 + int na, nb;
1.774 + bool da, db;
1.775 +
1.776 + na = nb = order_map[node];
1.777 + da = true; db = false;
1.778 +
1.779 + while (true) {
1.780 +
1.781 + if (node_data[na].visited == rorder) break;
1.782 + if (node_data[nb].visited == rorder) break;
1.783 +
1.784 + node_data[na].visited = rorder;
1.785 + node_data[nb].visited = rorder;
1.786 +
1.787 + int rn = -1;
1.788 +
1.789 + if (na >= int(order_list.size())) {
1.790 + rn = na;
1.791 + } else if (nb >= int(order_list.size())) {
1.792 + rn = nb;
1.793 + }
1.794 +
1.795 + if (rn == -1) {
1.796 + int nn;
1.797 +
1.798 + nn = da ? node_data[na].prev : node_data[na].next;
1.799 + da = node_data[nn].prev != na;
1.800 + na = nn;
1.801 +
1.802 + nn = db ? node_data[nb].prev : node_data[nb].next;
1.803 + db = node_data[nn].prev != nb;
1.804 + nb = nn;
1.805 +
1.806 + } else {
1.807 +
1.808 + Node rep = order_list[rn - order_list.size()];
1.809 + Node parent = _graph.source(pred_map[rep]);
1.810 +
1.811 + if (low_map[rep] < rorder) {
1.812 + merge_roots[parent].push_back(rn);
1.813 + } else {
1.814 + merge_roots[parent].push_front(rn);
1.815 + }
1.816 +
1.817 + if (parent != root) {
1.818 + na = nb = order_map[parent];
1.819 + da = true; db = false;
1.820 + } else {
1.821 + break;
1.822 + }
1.823 + }
1.824 + }
1.825 + }
1.826 +
1.827 + void walkDown(int rn, int rorder, NodeData& node_data,
1.828 + ArcLists& arc_lists, FlipMap& flip_map,
1.829 + OrderList& order_list, ChildLists& child_lists,
1.830 + AncestorMap& ancestor_map, LowMap& low_map,
1.831 + EmbedArc& embed_arc, MergeRoots& merge_roots) {
1.832 +
1.833 + std::vector<std::pair<int, bool> > merge_stack;
1.834 +
1.835 + for (int di = 0; di < 2; ++di) {
1.836 + bool rd = di == 0;
1.837 + int pn = rn;
1.838 + int n = rd ? node_data[rn].next : node_data[rn].prev;
1.839 +
1.840 + while (n != rn) {
1.841 +
1.842 + Node node = order_list[n];
1.843 +
1.844 + if (embed_arc[node] != INVALID) {
1.845 +
1.846 + // Merging components on the critical path
1.847 + while (!merge_stack.empty()) {
1.848 +
1.849 + // Component root
1.850 + int cn = merge_stack.back().first;
1.851 + bool cd = merge_stack.back().second;
1.852 + merge_stack.pop_back();
1.853 +
1.854 + // Parent of component
1.855 + int dn = merge_stack.back().first;
1.856 + bool dd = merge_stack.back().second;
1.857 + merge_stack.pop_back();
1.858 +
1.859 + Node parent = order_list[dn];
1.860 +
1.861 + // Erasing from merge_roots
1.862 + merge_roots[parent].pop_front();
1.863 +
1.864 + Node child = order_list[cn - order_list.size()];
1.865 +
1.866 + // Erasing from child_lists
1.867 + if (child_lists[child].prev != INVALID) {
1.868 + child_lists[child_lists[child].prev].next =
1.869 + child_lists[child].next;
1.870 + } else {
1.871 + child_lists[parent].first = child_lists[child].next;
1.872 + }
1.873 +
1.874 + if (child_lists[child].next != INVALID) {
1.875 + child_lists[child_lists[child].next].prev =
1.876 + child_lists[child].prev;
1.877 + }
1.878 +
1.879 + // Merging arcs + flipping
1.880 + Arc de = node_data[dn].first;
1.881 + Arc ce = node_data[cn].first;
1.882 +
1.883 + flip_map[order_list[cn - order_list.size()]] = cd != dd;
1.884 + if (cd != dd) {
1.885 + std::swap(arc_lists[ce].prev, arc_lists[ce].next);
1.886 + ce = arc_lists[ce].prev;
1.887 + std::swap(arc_lists[ce].prev, arc_lists[ce].next);
1.888 + }
1.889 +
1.890 + {
1.891 + Arc dne = arc_lists[de].next;
1.892 + Arc cne = arc_lists[ce].next;
1.893 +
1.894 + arc_lists[de].next = cne;
1.895 + arc_lists[ce].next = dne;
1.896 +
1.897 + arc_lists[dne].prev = ce;
1.898 + arc_lists[cne].prev = de;
1.899 + }
1.900 +
1.901 + if (dd) {
1.902 + node_data[dn].first = ce;
1.903 + }
1.904 +
1.905 + // Merging external faces
1.906 + {
1.907 + int en = cn;
1.908 + cn = cd ? node_data[cn].prev : node_data[cn].next;
1.909 + cd = node_data[cn].next == en;
1.910 +
1.911 + if (node_data[cn].prev == node_data[cn].next &&
1.912 + node_data[cn].inverted) {
1.913 + cd = !cd;
1.914 + }
1.915 + }
1.916 +
1.917 + if (cd) node_data[cn].next = dn; else node_data[cn].prev = dn;
1.918 + if (dd) node_data[dn].prev = cn; else node_data[dn].next = cn;
1.919 +
1.920 + }
1.921 +
1.922 + bool d = pn == node_data[n].prev;
1.923 +
1.924 + if (node_data[n].prev == node_data[n].next &&
1.925 + node_data[n].inverted) {
1.926 + d = !d;
1.927 + }
1.928 +
1.929 + // Add new arc
1.930 + {
1.931 + Arc arc = embed_arc[node];
1.932 + Arc re = node_data[rn].first;
1.933 +
1.934 + arc_lists[arc_lists[re].next].prev = arc;
1.935 + arc_lists[arc].next = arc_lists[re].next;
1.936 + arc_lists[arc].prev = re;
1.937 + arc_lists[re].next = arc;
1.938 +
1.939 + if (!rd) {
1.940 + node_data[rn].first = arc;
1.941 + }
1.942 +
1.943 + Arc rev = _graph.oppositeArc(arc);
1.944 + Arc e = node_data[n].first;
1.945 +
1.946 + arc_lists[arc_lists[e].next].prev = rev;
1.947 + arc_lists[rev].next = arc_lists[e].next;
1.948 + arc_lists[rev].prev = e;
1.949 + arc_lists[e].next = rev;
1.950 +
1.951 + if (d) {
1.952 + node_data[n].first = rev;
1.953 + }
1.954 +
1.955 + }
1.956 +
1.957 + // Embedding arc into external face
1.958 + if (rd) node_data[rn].next = n; else node_data[rn].prev = n;
1.959 + if (d) node_data[n].prev = rn; else node_data[n].next = rn;
1.960 + pn = rn;
1.961 +
1.962 + embed_arc[order_list[n]] = INVALID;
1.963 + }
1.964 +
1.965 + if (!merge_roots[node].empty()) {
1.966 +
1.967 + bool d = pn == node_data[n].prev;
1.968 + if (node_data[n].prev == node_data[n].next &&
1.969 + node_data[n].inverted) {
1.970 + d = !d;
1.971 + }
1.972 +
1.973 + merge_stack.push_back(std::make_pair(n, d));
1.974 +
1.975 + int rn = merge_roots[node].front();
1.976 +
1.977 + int xn = node_data[rn].next;
1.978 + Node xnode = order_list[xn];
1.979 +
1.980 + int yn = node_data[rn].prev;
1.981 + Node ynode = order_list[yn];
1.982 +
1.983 + bool rd;
1.984 + if (!external(xnode, rorder, child_lists, ancestor_map, low_map)) {
1.985 + rd = true;
1.986 + } else if (!external(ynode, rorder, child_lists,
1.987 + ancestor_map, low_map)) {
1.988 + rd = false;
1.989 + } else if (pertinent(xnode, embed_arc, merge_roots)) {
1.990 + rd = true;
1.991 + } else {
1.992 + rd = false;
1.993 + }
1.994 +
1.995 + merge_stack.push_back(std::make_pair(rn, rd));
1.996 +
1.997 + pn = rn;
1.998 + n = rd ? xn : yn;
1.999 +
1.1000 + } else if (!external(node, rorder, child_lists,
1.1001 + ancestor_map, low_map)) {
1.1002 + int nn = (node_data[n].next != pn ?
1.1003 + node_data[n].next : node_data[n].prev);
1.1004 +
1.1005 + bool nd = n == node_data[nn].prev;
1.1006 +
1.1007 + if (nd) node_data[nn].prev = pn;
1.1008 + else node_data[nn].next = pn;
1.1009 +
1.1010 + if (n == node_data[pn].prev) node_data[pn].prev = nn;
1.1011 + else node_data[pn].next = nn;
1.1012 +
1.1013 + node_data[nn].inverted =
1.1014 + (node_data[nn].prev == node_data[nn].next && nd != rd);
1.1015 +
1.1016 + n = nn;
1.1017 + }
1.1018 + else break;
1.1019 +
1.1020 + }
1.1021 +
1.1022 + if (!merge_stack.empty() || n == rn) {
1.1023 + break;
1.1024 + }
1.1025 + }
1.1026 + }
1.1027 +
1.1028 + void initFace(const Node& node, ArcLists& arc_lists,
1.1029 + NodeData& node_data, const PredMap& pred_map,
1.1030 + const OrderMap& order_map, const OrderList& order_list) {
1.1031 + int n = order_map[node];
1.1032 + int rn = n + order_list.size();
1.1033 +
1.1034 + node_data[n].next = node_data[n].prev = rn;
1.1035 + node_data[rn].next = node_data[rn].prev = n;
1.1036 +
1.1037 + node_data[n].visited = order_list.size();
1.1038 + node_data[rn].visited = order_list.size();
1.1039 +
1.1040 + node_data[n].inverted = false;
1.1041 + node_data[rn].inverted = false;
1.1042 +
1.1043 + Arc arc = pred_map[node];
1.1044 + Arc rev = _graph.oppositeArc(arc);
1.1045 +
1.1046 + node_data[rn].first = arc;
1.1047 + node_data[n].first = rev;
1.1048 +
1.1049 + arc_lists[arc].prev = arc;
1.1050 + arc_lists[arc].next = arc;
1.1051 +
1.1052 + arc_lists[rev].prev = rev;
1.1053 + arc_lists[rev].next = rev;
1.1054 +
1.1055 + }
1.1056 +
1.1057 + void mergeRemainingFaces(const Node& node, NodeData& node_data,
1.1058 + OrderList& order_list, OrderMap& order_map,
1.1059 + ChildLists& child_lists, ArcLists& arc_lists) {
1.1060 + while (child_lists[node].first != INVALID) {
1.1061 + int dd = order_map[node];
1.1062 + Node child = child_lists[node].first;
1.1063 + int cd = order_map[child] + order_list.size();
1.1064 + child_lists[node].first = child_lists[child].next;
1.1065 +
1.1066 + Arc de = node_data[dd].first;
1.1067 + Arc ce = node_data[cd].first;
1.1068 +
1.1069 + if (de != INVALID) {
1.1070 + Arc dne = arc_lists[de].next;
1.1071 + Arc cne = arc_lists[ce].next;
1.1072 +
1.1073 + arc_lists[de].next = cne;
1.1074 + arc_lists[ce].next = dne;
1.1075 +
1.1076 + arc_lists[dne].prev = ce;
1.1077 + arc_lists[cne].prev = de;
1.1078 + }
1.1079 +
1.1080 + node_data[dd].first = ce;
1.1081 +
1.1082 + }
1.1083 + }
1.1084 +
1.1085 + void storeEmbedding(const Node& node, NodeData& node_data,
1.1086 + OrderMap& order_map, PredMap& pred_map,
1.1087 + ArcLists& arc_lists, FlipMap& flip_map) {
1.1088 +
1.1089 + if (node_data[order_map[node]].first == INVALID) return;
1.1090 +
1.1091 + if (pred_map[node] != INVALID) {
1.1092 + Node source = _graph.source(pred_map[node]);
1.1093 + flip_map[node] = flip_map[node] != flip_map[source];
1.1094 + }
1.1095 +
1.1096 + Arc first = node_data[order_map[node]].first;
1.1097 + Arc prev = first;
1.1098 +
1.1099 + Arc arc = flip_map[node] ?
1.1100 + arc_lists[prev].prev : arc_lists[prev].next;
1.1101 +
1.1102 + _embedding[prev] = arc;
1.1103 +
1.1104 + while (arc != first) {
1.1105 + Arc next = arc_lists[arc].prev == prev ?
1.1106 + arc_lists[arc].next : arc_lists[arc].prev;
1.1107 + prev = arc; arc = next;
1.1108 + _embedding[prev] = arc;
1.1109 + }
1.1110 + }
1.1111 +
1.1112 +
1.1113 + bool external(const Node& node, int rorder,
1.1114 + ChildLists& child_lists, AncestorMap& ancestor_map,
1.1115 + LowMap& low_map) {
1.1116 + Node child = child_lists[node].first;
1.1117 +
1.1118 + if (child != INVALID) {
1.1119 + if (low_map[child] < rorder) return true;
1.1120 + }
1.1121 +
1.1122 + if (ancestor_map[node] < rorder) return true;
1.1123 +
1.1124 + return false;
1.1125 + }
1.1126 +
1.1127 + bool pertinent(const Node& node, const EmbedArc& embed_arc,
1.1128 + const MergeRoots& merge_roots) {
1.1129 + return !merge_roots[node].empty() || embed_arc[node] != INVALID;
1.1130 + }
1.1131 +
1.1132 + int lowPoint(const Node& node, OrderMap& order_map, ChildLists& child_lists,
1.1133 + AncestorMap& ancestor_map, LowMap& low_map) {
1.1134 + int low_point;
1.1135 +
1.1136 + Node child = child_lists[node].first;
1.1137 +
1.1138 + if (child != INVALID) {
1.1139 + low_point = low_map[child];
1.1140 + } else {
1.1141 + low_point = order_map[node];
1.1142 + }
1.1143 +
1.1144 + if (low_point > ancestor_map[node]) {
1.1145 + low_point = ancestor_map[node];
1.1146 + }
1.1147 +
1.1148 + return low_point;
1.1149 + }
1.1150 +
1.1151 + int findComponentRoot(Node root, Node node, ChildLists& child_lists,
1.1152 + OrderMap& order_map, OrderList& order_list) {
1.1153 +
1.1154 + int order = order_map[root];
1.1155 + int norder = order_map[node];
1.1156 +
1.1157 + Node child = child_lists[root].first;
1.1158 + while (child != INVALID) {
1.1159 + int corder = order_map[child];
1.1160 + if (corder > order && corder < norder) {
1.1161 + order = corder;
1.1162 + }
1.1163 + child = child_lists[child].next;
1.1164 + }
1.1165 + return order + order_list.size();
1.1166 + }
1.1167 +
1.1168 + Node findPertinent(Node node, OrderMap& order_map, NodeData& node_data,
1.1169 + EmbedArc& embed_arc, MergeRoots& merge_roots) {
1.1170 + Node wnode =_graph.target(node_data[order_map[node]].first);
1.1171 + while (!pertinent(wnode, embed_arc, merge_roots)) {
1.1172 + wnode = _graph.target(node_data[order_map[wnode]].first);
1.1173 + }
1.1174 + return wnode;
1.1175 + }
1.1176 +
1.1177 +
1.1178 + Node findExternal(Node node, int rorder, OrderMap& order_map,
1.1179 + ChildLists& child_lists, AncestorMap& ancestor_map,
1.1180 + LowMap& low_map, NodeData& node_data) {
1.1181 + Node wnode =_graph.target(node_data[order_map[node]].first);
1.1182 + while (!external(wnode, rorder, child_lists, ancestor_map, low_map)) {
1.1183 + wnode = _graph.target(node_data[order_map[wnode]].first);
1.1184 + }
1.1185 + return wnode;
1.1186 + }
1.1187 +
1.1188 + void markCommonPath(Node node, int rorder, Node& wnode, Node& znode,
1.1189 + OrderList& order_list, OrderMap& order_map,
1.1190 + NodeData& node_data, ArcLists& arc_lists,
1.1191 + EmbedArc& embed_arc, MergeRoots& merge_roots,
1.1192 + ChildLists& child_lists, AncestorMap& ancestor_map,
1.1193 + LowMap& low_map) {
1.1194 +
1.1195 + Node cnode = node;
1.1196 + Node pred = INVALID;
1.1197 +
1.1198 + while (true) {
1.1199 +
1.1200 + bool pert = pertinent(cnode, embed_arc, merge_roots);
1.1201 + bool ext = external(cnode, rorder, child_lists, ancestor_map, low_map);
1.1202 +
1.1203 + if (pert && ext) {
1.1204 + if (!merge_roots[cnode].empty()) {
1.1205 + int cn = merge_roots[cnode].back();
1.1206 +
1.1207 + if (low_map[order_list[cn - order_list.size()]] < rorder) {
1.1208 + Arc arc = node_data[cn].first;
1.1209 + _kuratowski.set(arc, true);
1.1210 +
1.1211 + pred = cnode;
1.1212 + cnode = _graph.target(arc);
1.1213 +
1.1214 + continue;
1.1215 + }
1.1216 + }
1.1217 + wnode = znode = cnode;
1.1218 + return;
1.1219 +
1.1220 + } else if (pert) {
1.1221 + wnode = cnode;
1.1222 +
1.1223 + while (!external(cnode, rorder, child_lists, ancestor_map, low_map)) {
1.1224 + Arc arc = node_data[order_map[cnode]].first;
1.1225 +
1.1226 + if (_graph.target(arc) == pred) {
1.1227 + arc = arc_lists[arc].next;
1.1228 + }
1.1229 + _kuratowski.set(arc, true);
1.1230 +
1.1231 + Node next = _graph.target(arc);
1.1232 + pred = cnode; cnode = next;
1.1233 + }
1.1234 +
1.1235 + znode = cnode;
1.1236 + return;
1.1237 +
1.1238 + } else if (ext) {
1.1239 + znode = cnode;
1.1240 +
1.1241 + while (!pertinent(cnode, embed_arc, merge_roots)) {
1.1242 + Arc arc = node_data[order_map[cnode]].first;
1.1243 +
1.1244 + if (_graph.target(arc) == pred) {
1.1245 + arc = arc_lists[arc].next;
1.1246 + }
1.1247 + _kuratowski.set(arc, true);
1.1248 +
1.1249 + Node next = _graph.target(arc);
1.1250 + pred = cnode; cnode = next;
1.1251 + }
1.1252 +
1.1253 + wnode = cnode;
1.1254 + return;
1.1255 +
1.1256 + } else {
1.1257 + Arc arc = node_data[order_map[cnode]].first;
1.1258 +
1.1259 + if (_graph.target(arc) == pred) {
1.1260 + arc = arc_lists[arc].next;
1.1261 + }
1.1262 + _kuratowski.set(arc, true);
1.1263 +
1.1264 + Node next = _graph.target(arc);
1.1265 + pred = cnode; cnode = next;
1.1266 + }
1.1267 +
1.1268 + }
1.1269 +
1.1270 + }
1.1271 +
1.1272 + void orientComponent(Node root, int rn, OrderMap& order_map,
1.1273 + PredMap& pred_map, NodeData& node_data,
1.1274 + ArcLists& arc_lists, FlipMap& flip_map,
1.1275 + TypeMap& type_map) {
1.1276 + node_data[order_map[root]].first = node_data[rn].first;
1.1277 + type_map[root] = 1;
1.1278 +
1.1279 + std::vector<Node> st, qu;
1.1280 +
1.1281 + st.push_back(root);
1.1282 + while (!st.empty()) {
1.1283 + Node node = st.back();
1.1284 + st.pop_back();
1.1285 + qu.push_back(node);
1.1286 +
1.1287 + Arc arc = node_data[order_map[node]].first;
1.1288 +
1.1289 + if (type_map[_graph.target(arc)] == 0) {
1.1290 + st.push_back(_graph.target(arc));
1.1291 + type_map[_graph.target(arc)] = 1;
1.1292 + }
1.1293 +
1.1294 + Arc last = arc, pred = arc;
1.1295 + arc = arc_lists[arc].next;
1.1296 + while (arc != last) {
1.1297 +
1.1298 + if (type_map[_graph.target(arc)] == 0) {
1.1299 + st.push_back(_graph.target(arc));
1.1300 + type_map[_graph.target(arc)] = 1;
1.1301 + }
1.1302 +
1.1303 + Arc next = arc_lists[arc].next != pred ?
1.1304 + arc_lists[arc].next : arc_lists[arc].prev;
1.1305 + pred = arc; arc = next;
1.1306 + }
1.1307 +
1.1308 + }
1.1309 +
1.1310 + type_map[root] = 2;
1.1311 + flip_map[root] = false;
1.1312 +
1.1313 + for (int i = 1; i < int(qu.size()); ++i) {
1.1314 +
1.1315 + Node node = qu[i];
1.1316 +
1.1317 + while (type_map[node] != 2) {
1.1318 + st.push_back(node);
1.1319 + type_map[node] = 2;
1.1320 + node = _graph.source(pred_map[node]);
1.1321 + }
1.1322 +
1.1323 + bool flip = flip_map[node];
1.1324 +
1.1325 + while (!st.empty()) {
1.1326 + node = st.back();
1.1327 + st.pop_back();
1.1328 +
1.1329 + flip_map[node] = flip != flip_map[node];
1.1330 + flip = flip_map[node];
1.1331 +
1.1332 + if (flip) {
1.1333 + Arc arc = node_data[order_map[node]].first;
1.1334 + std::swap(arc_lists[arc].prev, arc_lists[arc].next);
1.1335 + arc = arc_lists[arc].prev;
1.1336 + std::swap(arc_lists[arc].prev, arc_lists[arc].next);
1.1337 + node_data[order_map[node]].first = arc;
1.1338 + }
1.1339 + }
1.1340 + }
1.1341 +
1.1342 + for (int i = 0; i < int(qu.size()); ++i) {
1.1343 +
1.1344 + Arc arc = node_data[order_map[qu[i]]].first;
1.1345 + Arc last = arc, pred = arc;
1.1346 +
1.1347 + arc = arc_lists[arc].next;
1.1348 + while (arc != last) {
1.1349 +
1.1350 + if (arc_lists[arc].next == pred) {
1.1351 + std::swap(arc_lists[arc].next, arc_lists[arc].prev);
1.1352 + }
1.1353 + pred = arc; arc = arc_lists[arc].next;
1.1354 + }
1.1355 +
1.1356 + }
1.1357 + }
1.1358 +
1.1359 + void setFaceFlags(Node root, Node wnode, Node ynode, Node xnode,
1.1360 + OrderMap& order_map, NodeData& node_data,
1.1361 + TypeMap& type_map) {
1.1362 + Node node = _graph.target(node_data[order_map[root]].first);
1.1363 +
1.1364 + while (node != ynode) {
1.1365 + type_map[node] = HIGHY;
1.1366 + node = _graph.target(node_data[order_map[node]].first);
1.1367 + }
1.1368 +
1.1369 + while (node != wnode) {
1.1370 + type_map[node] = LOWY;
1.1371 + node = _graph.target(node_data[order_map[node]].first);
1.1372 + }
1.1373 +
1.1374 + node = _graph.target(node_data[order_map[wnode]].first);
1.1375 +
1.1376 + while (node != xnode) {
1.1377 + type_map[node] = LOWX;
1.1378 + node = _graph.target(node_data[order_map[node]].first);
1.1379 + }
1.1380 + type_map[node] = LOWX;
1.1381 +
1.1382 + node = _graph.target(node_data[order_map[xnode]].first);
1.1383 + while (node != root) {
1.1384 + type_map[node] = HIGHX;
1.1385 + node = _graph.target(node_data[order_map[node]].first);
1.1386 + }
1.1387 +
1.1388 + type_map[wnode] = PERTINENT;
1.1389 + type_map[root] = ROOT;
1.1390 + }
1.1391 +
1.1392 + void findInternalPath(std::vector<Arc>& ipath,
1.1393 + Node wnode, Node root, TypeMap& type_map,
1.1394 + OrderMap& order_map, NodeData& node_data,
1.1395 + ArcLists& arc_lists) {
1.1396 + std::vector<Arc> st;
1.1397 +
1.1398 + Node node = wnode;
1.1399 +
1.1400 + while (node != root) {
1.1401 + Arc arc = arc_lists[node_data[order_map[node]].first].next;
1.1402 + st.push_back(arc);
1.1403 + node = _graph.target(arc);
1.1404 + }
1.1405 +
1.1406 + while (true) {
1.1407 + Arc arc = st.back();
1.1408 + if (type_map[_graph.target(arc)] == LOWX ||
1.1409 + type_map[_graph.target(arc)] == HIGHX) {
1.1410 + break;
1.1411 + }
1.1412 + if (type_map[_graph.target(arc)] == 2) {
1.1413 + type_map[_graph.target(arc)] = 3;
1.1414 +
1.1415 + arc = arc_lists[_graph.oppositeArc(arc)].next;
1.1416 + st.push_back(arc);
1.1417 + } else {
1.1418 + st.pop_back();
1.1419 + arc = arc_lists[arc].next;
1.1420 +
1.1421 + while (_graph.oppositeArc(arc) == st.back()) {
1.1422 + arc = st.back();
1.1423 + st.pop_back();
1.1424 + arc = arc_lists[arc].next;
1.1425 + }
1.1426 + st.push_back(arc);
1.1427 + }
1.1428 + }
1.1429 +
1.1430 + for (int i = 0; i < int(st.size()); ++i) {
1.1431 + if (type_map[_graph.target(st[i])] != LOWY &&
1.1432 + type_map[_graph.target(st[i])] != HIGHY) {
1.1433 + for (; i < int(st.size()); ++i) {
1.1434 + ipath.push_back(st[i]);
1.1435 + }
1.1436 + }
1.1437 + }
1.1438 + }
1.1439 +
1.1440 + void setInternalFlags(std::vector<Arc>& ipath, TypeMap& type_map) {
1.1441 + for (int i = 1; i < int(ipath.size()); ++i) {
1.1442 + type_map[_graph.source(ipath[i])] = INTERNAL;
1.1443 + }
1.1444 + }
1.1445 +
1.1446 + void findPilePath(std::vector<Arc>& ppath,
1.1447 + Node root, TypeMap& type_map, OrderMap& order_map,
1.1448 + NodeData& node_data, ArcLists& arc_lists) {
1.1449 + std::vector<Arc> st;
1.1450 +
1.1451 + st.push_back(_graph.oppositeArc(node_data[order_map[root]].first));
1.1452 + st.push_back(node_data[order_map[root]].first);
1.1453 +
1.1454 + while (st.size() > 1) {
1.1455 + Arc arc = st.back();
1.1456 + if (type_map[_graph.target(arc)] == INTERNAL) {
1.1457 + break;
1.1458 + }
1.1459 + if (type_map[_graph.target(arc)] == 3) {
1.1460 + type_map[_graph.target(arc)] = 4;
1.1461 +
1.1462 + arc = arc_lists[_graph.oppositeArc(arc)].next;
1.1463 + st.push_back(arc);
1.1464 + } else {
1.1465 + st.pop_back();
1.1466 + arc = arc_lists[arc].next;
1.1467 +
1.1468 + while (!st.empty() && _graph.oppositeArc(arc) == st.back()) {
1.1469 + arc = st.back();
1.1470 + st.pop_back();
1.1471 + arc = arc_lists[arc].next;
1.1472 + }
1.1473 + st.push_back(arc);
1.1474 + }
1.1475 + }
1.1476 +
1.1477 + for (int i = 1; i < int(st.size()); ++i) {
1.1478 + ppath.push_back(st[i]);
1.1479 + }
1.1480 + }
1.1481 +
1.1482 +
1.1483 + int markExternalPath(Node node, OrderMap& order_map,
1.1484 + ChildLists& child_lists, PredMap& pred_map,
1.1485 + AncestorMap& ancestor_map, LowMap& low_map) {
1.1486 + int lp = lowPoint(node, order_map, child_lists,
1.1487 + ancestor_map, low_map);
1.1488 +
1.1489 + if (ancestor_map[node] != lp) {
1.1490 + node = child_lists[node].first;
1.1491 + _kuratowski[pred_map[node]] = true;
1.1492 +
1.1493 + while (ancestor_map[node] != lp) {
1.1494 + for (OutArcIt e(_graph, node); e != INVALID; ++e) {
1.1495 + Node tnode = _graph.target(e);
1.1496 + if (order_map[tnode] > order_map[node] && low_map[tnode] == lp) {
1.1497 + node = tnode;
1.1498 + _kuratowski[e] = true;
1.1499 + break;
1.1500 + }
1.1501 + }
1.1502 + }
1.1503 + }
1.1504 +
1.1505 + for (OutArcIt e(_graph, node); e != INVALID; ++e) {
1.1506 + if (order_map[_graph.target(e)] == lp) {
1.1507 + _kuratowski[e] = true;
1.1508 + break;
1.1509 + }
1.1510 + }
1.1511 +
1.1512 + return lp;
1.1513 + }
1.1514 +
1.1515 + void markPertinentPath(Node node, OrderMap& order_map,
1.1516 + NodeData& node_data, ArcLists& arc_lists,
1.1517 + EmbedArc& embed_arc, MergeRoots& merge_roots) {
1.1518 + while (embed_arc[node] == INVALID) {
1.1519 + int n = merge_roots[node].front();
1.1520 + Arc arc = node_data[n].first;
1.1521 +
1.1522 + _kuratowski.set(arc, true);
1.1523 +
1.1524 + Node pred = node;
1.1525 + node = _graph.target(arc);
1.1526 + while (!pertinent(node, embed_arc, merge_roots)) {
1.1527 + arc = node_data[order_map[node]].first;
1.1528 + if (_graph.target(arc) == pred) {
1.1529 + arc = arc_lists[arc].next;
1.1530 + }
1.1531 + _kuratowski.set(arc, true);
1.1532 + pred = node;
1.1533 + node = _graph.target(arc);
1.1534 + }
1.1535 + }
1.1536 + _kuratowski.set(embed_arc[node], true);
1.1537 + }
1.1538 +
1.1539 + void markPredPath(Node node, Node snode, PredMap& pred_map) {
1.1540 + while (node != snode) {
1.1541 + _kuratowski.set(pred_map[node], true);
1.1542 + node = _graph.source(pred_map[node]);
1.1543 + }
1.1544 + }
1.1545 +
1.1546 + void markFacePath(Node ynode, Node xnode,
1.1547 + OrderMap& order_map, NodeData& node_data) {
1.1548 + Arc arc = node_data[order_map[ynode]].first;
1.1549 + Node node = _graph.target(arc);
1.1550 + _kuratowski.set(arc, true);
1.1551 +
1.1552 + while (node != xnode) {
1.1553 + arc = node_data[order_map[node]].first;
1.1554 + _kuratowski.set(arc, true);
1.1555 + node = _graph.target(arc);
1.1556 + }
1.1557 + }
1.1558 +
1.1559 + void markInternalPath(std::vector<Arc>& path) {
1.1560 + for (int i = 0; i < int(path.size()); ++i) {
1.1561 + _kuratowski.set(path[i], true);
1.1562 + }
1.1563 + }
1.1564 +
1.1565 + void markPilePath(std::vector<Arc>& path) {
1.1566 + for (int i = 0; i < int(path.size()); ++i) {
1.1567 + _kuratowski.set(path[i], true);
1.1568 + }
1.1569 + }
1.1570 +
1.1571 + void isolateKuratowski(Arc arc, NodeData& node_data,
1.1572 + ArcLists& arc_lists, FlipMap& flip_map,
1.1573 + OrderMap& order_map, OrderList& order_list,
1.1574 + PredMap& pred_map, ChildLists& child_lists,
1.1575 + AncestorMap& ancestor_map, LowMap& low_map,
1.1576 + EmbedArc& embed_arc, MergeRoots& merge_roots) {
1.1577 +
1.1578 + Node root = _graph.source(arc);
1.1579 + Node enode = _graph.target(arc);
1.1580 +
1.1581 + int rorder = order_map[root];
1.1582 +
1.1583 + TypeMap type_map(_graph, 0);
1.1584 +
1.1585 + int rn = findComponentRoot(root, enode, child_lists,
1.1586 + order_map, order_list);
1.1587 +
1.1588 + Node xnode = order_list[node_data[rn].next];
1.1589 + Node ynode = order_list[node_data[rn].prev];
1.1590 +
1.1591 + // Minor-A
1.1592 + {
1.1593 + while (!merge_roots[xnode].empty() || !merge_roots[ynode].empty()) {
1.1594 +
1.1595 + if (!merge_roots[xnode].empty()) {
1.1596 + root = xnode;
1.1597 + rn = merge_roots[xnode].front();
1.1598 + } else {
1.1599 + root = ynode;
1.1600 + rn = merge_roots[ynode].front();
1.1601 + }
1.1602 +
1.1603 + xnode = order_list[node_data[rn].next];
1.1604 + ynode = order_list[node_data[rn].prev];
1.1605 + }
1.1606 +
1.1607 + if (root != _graph.source(arc)) {
1.1608 + orientComponent(root, rn, order_map, pred_map,
1.1609 + node_data, arc_lists, flip_map, type_map);
1.1610 + markFacePath(root, root, order_map, node_data);
1.1611 + int xlp = markExternalPath(xnode, order_map, child_lists,
1.1612 + pred_map, ancestor_map, low_map);
1.1613 + int ylp = markExternalPath(ynode, order_map, child_lists,
1.1614 + pred_map, ancestor_map, low_map);
1.1615 + markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
1.1616 + Node lwnode = findPertinent(ynode, order_map, node_data,
1.1617 + embed_arc, merge_roots);
1.1618 +
1.1619 + markPertinentPath(lwnode, order_map, node_data, arc_lists,
1.1620 + embed_arc, merge_roots);
1.1621 +
1.1622 + return;
1.1623 + }
1.1624 + }
1.1625 +
1.1626 + orientComponent(root, rn, order_map, pred_map,
1.1627 + node_data, arc_lists, flip_map, type_map);
1.1628 +
1.1629 + Node wnode = findPertinent(ynode, order_map, node_data,
1.1630 + embed_arc, merge_roots);
1.1631 + setFaceFlags(root, wnode, ynode, xnode, order_map, node_data, type_map);
1.1632 +
1.1633 +
1.1634 + //Minor-B
1.1635 + if (!merge_roots[wnode].empty()) {
1.1636 + int cn = merge_roots[wnode].back();
1.1637 + Node rep = order_list[cn - order_list.size()];
1.1638 + if (low_map[rep] < rorder) {
1.1639 + markFacePath(root, root, order_map, node_data);
1.1640 + int xlp = markExternalPath(xnode, order_map, child_lists,
1.1641 + pred_map, ancestor_map, low_map);
1.1642 + int ylp = markExternalPath(ynode, order_map, child_lists,
1.1643 + pred_map, ancestor_map, low_map);
1.1644 +
1.1645 + Node lwnode, lznode;
1.1646 + markCommonPath(wnode, rorder, lwnode, lznode, order_list,
1.1647 + order_map, node_data, arc_lists, embed_arc,
1.1648 + merge_roots, child_lists, ancestor_map, low_map);
1.1649 +
1.1650 + markPertinentPath(lwnode, order_map, node_data, arc_lists,
1.1651 + embed_arc, merge_roots);
1.1652 + int zlp = markExternalPath(lznode, order_map, child_lists,
1.1653 + pred_map, ancestor_map, low_map);
1.1654 +
1.1655 + int minlp = xlp < ylp ? xlp : ylp;
1.1656 + if (zlp < minlp) minlp = zlp;
1.1657 +
1.1658 + int maxlp = xlp > ylp ? xlp : ylp;
1.1659 + if (zlp > maxlp) maxlp = zlp;
1.1660 +
1.1661 + markPredPath(order_list[maxlp], order_list[minlp], pred_map);
1.1662 +
1.1663 + return;
1.1664 + }
1.1665 + }
1.1666 +
1.1667 + Node pxnode, pynode;
1.1668 + std::vector<Arc> ipath;
1.1669 + findInternalPath(ipath, wnode, root, type_map, order_map,
1.1670 + node_data, arc_lists);
1.1671 + setInternalFlags(ipath, type_map);
1.1672 + pynode = _graph.source(ipath.front());
1.1673 + pxnode = _graph.target(ipath.back());
1.1674 +
1.1675 + wnode = findPertinent(pynode, order_map, node_data,
1.1676 + embed_arc, merge_roots);
1.1677 +
1.1678 + // Minor-C
1.1679 + {
1.1680 + if (type_map[_graph.source(ipath.front())] == HIGHY) {
1.1681 + if (type_map[_graph.target(ipath.back())] == HIGHX) {
1.1682 + markFacePath(xnode, pxnode, order_map, node_data);
1.1683 + }
1.1684 + markFacePath(root, xnode, order_map, node_data);
1.1685 + markPertinentPath(wnode, order_map, node_data, arc_lists,
1.1686 + embed_arc, merge_roots);
1.1687 + markInternalPath(ipath);
1.1688 + int xlp = markExternalPath(xnode, order_map, child_lists,
1.1689 + pred_map, ancestor_map, low_map);
1.1690 + int ylp = markExternalPath(ynode, order_map, child_lists,
1.1691 + pred_map, ancestor_map, low_map);
1.1692 + markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
1.1693 + return;
1.1694 + }
1.1695 +
1.1696 + if (type_map[_graph.target(ipath.back())] == HIGHX) {
1.1697 + markFacePath(ynode, root, order_map, node_data);
1.1698 + markPertinentPath(wnode, order_map, node_data, arc_lists,
1.1699 + embed_arc, merge_roots);
1.1700 + markInternalPath(ipath);
1.1701 + int xlp = markExternalPath(xnode, order_map, child_lists,
1.1702 + pred_map, ancestor_map, low_map);
1.1703 + int ylp = markExternalPath(ynode, order_map, child_lists,
1.1704 + pred_map, ancestor_map, low_map);
1.1705 + markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
1.1706 + return;
1.1707 + }
1.1708 + }
1.1709 +
1.1710 + std::vector<Arc> ppath;
1.1711 + findPilePath(ppath, root, type_map, order_map, node_data, arc_lists);
1.1712 +
1.1713 + // Minor-D
1.1714 + if (!ppath.empty()) {
1.1715 + markFacePath(ynode, xnode, order_map, node_data);
1.1716 + markPertinentPath(wnode, order_map, node_data, arc_lists,
1.1717 + embed_arc, merge_roots);
1.1718 + markPilePath(ppath);
1.1719 + markInternalPath(ipath);
1.1720 + int xlp = markExternalPath(xnode, order_map, child_lists,
1.1721 + pred_map, ancestor_map, low_map);
1.1722 + int ylp = markExternalPath(ynode, order_map, child_lists,
1.1723 + pred_map, ancestor_map, low_map);
1.1724 + markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
1.1725 + return;
1.1726 + }
1.1727 +
1.1728 + // Minor-E*
1.1729 + {
1.1730 +
1.1731 + if (!external(wnode, rorder, child_lists, ancestor_map, low_map)) {
1.1732 + Node znode = findExternal(pynode, rorder, order_map,
1.1733 + child_lists, ancestor_map,
1.1734 + low_map, node_data);
1.1735 +
1.1736 + if (type_map[znode] == LOWY) {
1.1737 + markFacePath(root, xnode, order_map, node_data);
1.1738 + markPertinentPath(wnode, order_map, node_data, arc_lists,
1.1739 + embed_arc, merge_roots);
1.1740 + markInternalPath(ipath);
1.1741 + int xlp = markExternalPath(xnode, order_map, child_lists,
1.1742 + pred_map, ancestor_map, low_map);
1.1743 + int zlp = markExternalPath(znode, order_map, child_lists,
1.1744 + pred_map, ancestor_map, low_map);
1.1745 + markPredPath(root, order_list[xlp < zlp ? xlp : zlp], pred_map);
1.1746 + } else {
1.1747 + markFacePath(ynode, root, order_map, node_data);
1.1748 + markPertinentPath(wnode, order_map, node_data, arc_lists,
1.1749 + embed_arc, merge_roots);
1.1750 + markInternalPath(ipath);
1.1751 + int ylp = markExternalPath(ynode, order_map, child_lists,
1.1752 + pred_map, ancestor_map, low_map);
1.1753 + int zlp = markExternalPath(znode, order_map, child_lists,
1.1754 + pred_map, ancestor_map, low_map);
1.1755 + markPredPath(root, order_list[ylp < zlp ? ylp : zlp], pred_map);
1.1756 + }
1.1757 + return;
1.1758 + }
1.1759 +
1.1760 + int xlp = markExternalPath(xnode, order_map, child_lists,
1.1761 + pred_map, ancestor_map, low_map);
1.1762 + int ylp = markExternalPath(ynode, order_map, child_lists,
1.1763 + pred_map, ancestor_map, low_map);
1.1764 + int wlp = markExternalPath(wnode, order_map, child_lists,
1.1765 + pred_map, ancestor_map, low_map);
1.1766 +
1.1767 + if (wlp > xlp && wlp > ylp) {
1.1768 + markFacePath(root, root, order_map, node_data);
1.1769 + markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
1.1770 + return;
1.1771 + }
1.1772 +
1.1773 + markInternalPath(ipath);
1.1774 + markPertinentPath(wnode, order_map, node_data, arc_lists,
1.1775 + embed_arc, merge_roots);
1.1776 +
1.1777 + if (xlp > ylp && xlp > wlp) {
1.1778 + markFacePath(root, pynode, order_map, node_data);
1.1779 + markFacePath(wnode, xnode, order_map, node_data);
1.1780 + markPredPath(root, order_list[ylp < wlp ? ylp : wlp], pred_map);
1.1781 + return;
1.1782 + }
1.1783 +
1.1784 + if (ylp > xlp && ylp > wlp) {
1.1785 + markFacePath(pxnode, root, order_map, node_data);
1.1786 + markFacePath(ynode, wnode, order_map, node_data);
1.1787 + markPredPath(root, order_list[xlp < wlp ? xlp : wlp], pred_map);
1.1788 + return;
1.1789 + }
1.1790 +
1.1791 + if (pynode != ynode) {
1.1792 + markFacePath(pxnode, wnode, order_map, node_data);
1.1793 +
1.1794 + int minlp = xlp < ylp ? xlp : ylp;
1.1795 + if (wlp < minlp) minlp = wlp;
1.1796 +
1.1797 + int maxlp = xlp > ylp ? xlp : ylp;
1.1798 + if (wlp > maxlp) maxlp = wlp;
1.1799 +
1.1800 + markPredPath(order_list[maxlp], order_list[minlp], pred_map);
1.1801 + return;
1.1802 + }
1.1803 +
1.1804 + if (pxnode != xnode) {
1.1805 + markFacePath(wnode, pynode, order_map, node_data);
1.1806 +
1.1807 + int minlp = xlp < ylp ? xlp : ylp;
1.1808 + if (wlp < minlp) minlp = wlp;
1.1809 +
1.1810 + int maxlp = xlp > ylp ? xlp : ylp;
1.1811 + if (wlp > maxlp) maxlp = wlp;
1.1812 +
1.1813 + markPredPath(order_list[maxlp], order_list[minlp], pred_map);
1.1814 + return;
1.1815 + }
1.1816 +
1.1817 + markFacePath(root, root, order_map, node_data);
1.1818 + int minlp = xlp < ylp ? xlp : ylp;
1.1819 + if (wlp < minlp) minlp = wlp;
1.1820 + markPredPath(root, order_list[minlp], pred_map);
1.1821 + return;
1.1822 + }
1.1823 +
1.1824 + }
1.1825 +
1.1826 + };
1.1827 +
1.1828 + namespace _planarity_bits {
1.1829 +
1.1830 + template <typename Graph, typename EmbeddingMap>
1.1831 + void makeConnected(Graph& graph, EmbeddingMap& embedding) {
1.1832 + DfsVisitor<Graph> null_visitor;
1.1833 + DfsVisit<Graph, DfsVisitor<Graph> > dfs(graph, null_visitor);
1.1834 + dfs.init();
1.1835 +
1.1836 + typename Graph::Node u = INVALID;
1.1837 + for (typename Graph::NodeIt n(graph); n != INVALID; ++n) {
1.1838 + if (!dfs.reached(n)) {
1.1839 + dfs.addSource(n);
1.1840 + dfs.start();
1.1841 + if (u == INVALID) {
1.1842 + u = n;
1.1843 + } else {
1.1844 + typename Graph::Node v = n;
1.1845 +
1.1846 + typename Graph::Arc ue = typename Graph::OutArcIt(graph, u);
1.1847 + typename Graph::Arc ve = typename Graph::OutArcIt(graph, v);
1.1848 +
1.1849 + typename Graph::Arc e = graph.direct(graph.addEdge(u, v), true);
1.1850 +
1.1851 + if (ue != INVALID) {
1.1852 + embedding[e] = embedding[ue];
1.1853 + embedding[ue] = e;
1.1854 + } else {
1.1855 + embedding[e] = e;
1.1856 + }
1.1857 +
1.1858 + if (ve != INVALID) {
1.1859 + embedding[graph.oppositeArc(e)] = embedding[ve];
1.1860 + embedding[ve] = graph.oppositeArc(e);
1.1861 + } else {
1.1862 + embedding[graph.oppositeArc(e)] = graph.oppositeArc(e);
1.1863 + }
1.1864 + }
1.1865 + }
1.1866 + }
1.1867 + }
1.1868 +
1.1869 + template <typename Graph, typename EmbeddingMap>
1.1870 + void makeBiNodeConnected(Graph& graph, EmbeddingMap& embedding) {
1.1871 + typename Graph::template ArcMap<bool> processed(graph);
1.1872 +
1.1873 + std::vector<typename Graph::Arc> arcs;
1.1874 + for (typename Graph::ArcIt e(graph); e != INVALID; ++e) {
1.1875 + arcs.push_back(e);
1.1876 + }
1.1877 +
1.1878 + IterableBoolMap<Graph, typename Graph::Node> visited(graph, false);
1.1879 +
1.1880 + for (int i = 0; i < int(arcs.size()); ++i) {
1.1881 + typename Graph::Arc pp = arcs[i];
1.1882 + if (processed[pp]) continue;
1.1883 +
1.1884 + typename Graph::Arc e = embedding[graph.oppositeArc(pp)];
1.1885 + processed[e] = true;
1.1886 + visited.set(graph.source(e), true);
1.1887 +
1.1888 + typename Graph::Arc p = e, l = e;
1.1889 + e = embedding[graph.oppositeArc(e)];
1.1890 +
1.1891 + while (e != l) {
1.1892 + processed[e] = true;
1.1893 +
1.1894 + if (visited[graph.source(e)]) {
1.1895 +
1.1896 + typename Graph::Arc n =
1.1897 + graph.direct(graph.addEdge(graph.source(p),
1.1898 + graph.target(e)), true);
1.1899 + embedding[n] = p;
1.1900 + embedding[graph.oppositeArc(pp)] = n;
1.1901 +
1.1902 + embedding[graph.oppositeArc(n)] =
1.1903 + embedding[graph.oppositeArc(e)];
1.1904 + embedding[graph.oppositeArc(e)] =
1.1905 + graph.oppositeArc(n);
1.1906 +
1.1907 + p = n;
1.1908 + e = embedding[graph.oppositeArc(n)];
1.1909 + } else {
1.1910 + visited.set(graph.source(e), true);
1.1911 + pp = p;
1.1912 + p = e;
1.1913 + e = embedding[graph.oppositeArc(e)];
1.1914 + }
1.1915 + }
1.1916 + visited.setAll(false);
1.1917 + }
1.1918 + }
1.1919 +
1.1920 +
1.1921 + template <typename Graph, typename EmbeddingMap>
1.1922 + void makeMaxPlanar(Graph& graph, EmbeddingMap& embedding) {
1.1923 +
1.1924 + typename Graph::template NodeMap<int> degree(graph);
1.1925 +
1.1926 + for (typename Graph::NodeIt n(graph); n != INVALID; ++n) {
1.1927 + degree[n] = countIncEdges(graph, n);
1.1928 + }
1.1929 +
1.1930 + typename Graph::template ArcMap<bool> processed(graph);
1.1931 + IterableBoolMap<Graph, typename Graph::Node> visited(graph, false);
1.1932 +
1.1933 + std::vector<typename Graph::Arc> arcs;
1.1934 + for (typename Graph::ArcIt e(graph); e != INVALID; ++e) {
1.1935 + arcs.push_back(e);
1.1936 + }
1.1937 +
1.1938 + for (int i = 0; i < int(arcs.size()); ++i) {
1.1939 + typename Graph::Arc e = arcs[i];
1.1940 +
1.1941 + if (processed[e]) continue;
1.1942 + processed[e] = true;
1.1943 +
1.1944 + typename Graph::Arc mine = e;
1.1945 + int mind = degree[graph.source(e)];
1.1946 +
1.1947 + int face_size = 1;
1.1948 +
1.1949 + typename Graph::Arc l = e;
1.1950 + e = embedding[graph.oppositeArc(e)];
1.1951 + while (l != e) {
1.1952 + processed[e] = true;
1.1953 +
1.1954 + ++face_size;
1.1955 +
1.1956 + if (degree[graph.source(e)] < mind) {
1.1957 + mine = e;
1.1958 + mind = degree[graph.source(e)];
1.1959 + }
1.1960 +
1.1961 + e = embedding[graph.oppositeArc(e)];
1.1962 + }
1.1963 +
1.1964 + if (face_size < 4) {
1.1965 + continue;
1.1966 + }
1.1967 +
1.1968 + typename Graph::Node s = graph.source(mine);
1.1969 + for (typename Graph::OutArcIt e(graph, s); e != INVALID; ++e) {
1.1970 + visited.set(graph.target(e), true);
1.1971 + }
1.1972 +
1.1973 + typename Graph::Arc oppe = INVALID;
1.1974 +
1.1975 + e = embedding[graph.oppositeArc(mine)];
1.1976 + e = embedding[graph.oppositeArc(e)];
1.1977 + while (graph.target(e) != s) {
1.1978 + if (visited[graph.source(e)]) {
1.1979 + oppe = e;
1.1980 + break;
1.1981 + }
1.1982 + e = embedding[graph.oppositeArc(e)];
1.1983 + }
1.1984 + visited.setAll(false);
1.1985 +
1.1986 + if (oppe == INVALID) {
1.1987 +
1.1988 + e = embedding[graph.oppositeArc(mine)];
1.1989 + typename Graph::Arc pn = mine, p = e;
1.1990 +
1.1991 + e = embedding[graph.oppositeArc(e)];
1.1992 + while (graph.target(e) != s) {
1.1993 + typename Graph::Arc n =
1.1994 + graph.direct(graph.addEdge(s, graph.source(e)), true);
1.1995 +
1.1996 + embedding[n] = pn;
1.1997 + embedding[graph.oppositeArc(n)] = e;
1.1998 + embedding[graph.oppositeArc(p)] = graph.oppositeArc(n);
1.1999 +
1.2000 + pn = n;
1.2001 +
1.2002 + p = e;
1.2003 + e = embedding[graph.oppositeArc(e)];
1.2004 + }
1.2005 +
1.2006 + embedding[graph.oppositeArc(e)] = pn;
1.2007 +
1.2008 + } else {
1.2009 +
1.2010 + mine = embedding[graph.oppositeArc(mine)];
1.2011 + s = graph.source(mine);
1.2012 + oppe = embedding[graph.oppositeArc(oppe)];
1.2013 + typename Graph::Node t = graph.source(oppe);
1.2014 +
1.2015 + typename Graph::Arc ce = graph.direct(graph.addEdge(s, t), true);
1.2016 + embedding[ce] = mine;
1.2017 + embedding[graph.oppositeArc(ce)] = oppe;
1.2018 +
1.2019 + typename Graph::Arc pn = ce, p = oppe;
1.2020 + e = embedding[graph.oppositeArc(oppe)];
1.2021 + while (graph.target(e) != s) {
1.2022 + typename Graph::Arc n =
1.2023 + graph.direct(graph.addEdge(s, graph.source(e)), true);
1.2024 +
1.2025 + embedding[n] = pn;
1.2026 + embedding[graph.oppositeArc(n)] = e;
1.2027 + embedding[graph.oppositeArc(p)] = graph.oppositeArc(n);
1.2028 +
1.2029 + pn = n;
1.2030 +
1.2031 + p = e;
1.2032 + e = embedding[graph.oppositeArc(e)];
1.2033 +
1.2034 + }
1.2035 + embedding[graph.oppositeArc(e)] = pn;
1.2036 +
1.2037 + pn = graph.oppositeArc(ce), p = mine;
1.2038 + e = embedding[graph.oppositeArc(mine)];
1.2039 + while (graph.target(e) != t) {
1.2040 + typename Graph::Arc n =
1.2041 + graph.direct(graph.addEdge(t, graph.source(e)), true);
1.2042 +
1.2043 + embedding[n] = pn;
1.2044 + embedding[graph.oppositeArc(n)] = e;
1.2045 + embedding[graph.oppositeArc(p)] = graph.oppositeArc(n);
1.2046 +
1.2047 + pn = n;
1.2048 +
1.2049 + p = e;
1.2050 + e = embedding[graph.oppositeArc(e)];
1.2051 +
1.2052 + }
1.2053 + embedding[graph.oppositeArc(e)] = pn;
1.2054 + }
1.2055 + }
1.2056 + }
1.2057 +
1.2058 + }
1.2059 +
1.2060 + /// \ingroup planar
1.2061 + ///
1.2062 + /// \brief Schnyder's planar drawing algorithm
1.2063 + ///
1.2064 + /// The planar drawing algorithm calculates positions for the nodes
1.2065 + /// in the plane which coordinates satisfy that if the arcs are
1.2066 + /// represented with straight lines then they will not intersect
1.2067 + /// each other.
1.2068 + ///
1.2069 + /// Scnyder's algorithm embeds the graph on \c (n-2,n-2) size grid,
1.2070 + /// i.e. each node will be located in the \c [0,n-2]x[0,n-2] square.
1.2071 + /// The time complexity of the algorithm is O(n).
1.2072 + template <typename Graph>
1.2073 + class PlanarDrawing {
1.2074 + public:
1.2075 +
1.2076 + TEMPLATE_GRAPH_TYPEDEFS(Graph);
1.2077 +
1.2078 + /// \brief The point type for store coordinates
1.2079 + typedef dim2::Point<int> Point;
1.2080 + /// \brief The map type for store coordinates
1.2081 + typedef typename Graph::template NodeMap<Point> PointMap;
1.2082 +
1.2083 +
1.2084 + /// \brief Constructor
1.2085 + ///
1.2086 + /// Constructor
1.2087 + /// \pre The graph should be simple, i.e. loop and parallel arc free.
1.2088 + PlanarDrawing(const Graph& graph)
1.2089 + : _graph(graph), _point_map(graph) {}
1.2090 +
1.2091 + private:
1.2092 +
1.2093 + template <typename AuxGraph, typename AuxEmbeddingMap>
1.2094 + void drawing(const AuxGraph& graph,
1.2095 + const AuxEmbeddingMap& next,
1.2096 + PointMap& point_map) {
1.2097 + TEMPLATE_GRAPH_TYPEDEFS(AuxGraph);
1.2098 +
1.2099 + typename AuxGraph::template ArcMap<Arc> prev(graph);
1.2100 +
1.2101 + for (NodeIt n(graph); n != INVALID; ++n) {
1.2102 + Arc e = OutArcIt(graph, n);
1.2103 +
1.2104 + Arc p = e, l = e;
1.2105 +
1.2106 + e = next[e];
1.2107 + while (e != l) {
1.2108 + prev[e] = p;
1.2109 + p = e;
1.2110 + e = next[e];
1.2111 + }
1.2112 + prev[e] = p;
1.2113 + }
1.2114 +
1.2115 + Node anode, bnode, cnode;
1.2116 +
1.2117 + {
1.2118 + Arc e = ArcIt(graph);
1.2119 + anode = graph.source(e);
1.2120 + bnode = graph.target(e);
1.2121 + cnode = graph.target(next[graph.oppositeArc(e)]);
1.2122 + }
1.2123 +
1.2124 + IterableBoolMap<AuxGraph, Node> proper(graph, false);
1.2125 + typename AuxGraph::template NodeMap<int> conn(graph, -1);
1.2126 +
1.2127 + conn[anode] = conn[bnode] = -2;
1.2128 + {
1.2129 + for (OutArcIt e(graph, anode); e != INVALID; ++e) {
1.2130 + Node m = graph.target(e);
1.2131 + if (conn[m] == -1) {
1.2132 + conn[m] = 1;
1.2133 + }
1.2134 + }
1.2135 + conn[cnode] = 2;
1.2136 +
1.2137 + for (OutArcIt e(graph, bnode); e != INVALID; ++e) {
1.2138 + Node m = graph.target(e);
1.2139 + if (conn[m] == -1) {
1.2140 + conn[m] = 1;
1.2141 + } else if (conn[m] != -2) {
1.2142 + conn[m] += 1;
1.2143 + Arc pe = graph.oppositeArc(e);
1.2144 + if (conn[graph.target(next[pe])] == -2) {
1.2145 + conn[m] -= 1;
1.2146 + }
1.2147 + if (conn[graph.target(prev[pe])] == -2) {
1.2148 + conn[m] -= 1;
1.2149 + }
1.2150 +
1.2151 + proper.set(m, conn[m] == 1);
1.2152 + }
1.2153 + }
1.2154 + }
1.2155 +
1.2156 +
1.2157 + typename AuxGraph::template ArcMap<int> angle(graph, -1);
1.2158 +
1.2159 + while (proper.trueNum() != 0) {
1.2160 + Node n = typename IterableBoolMap<AuxGraph, Node>::TrueIt(proper);
1.2161 + proper.set(n, false);
1.2162 + conn[n] = -2;
1.2163 +
1.2164 + for (OutArcIt e(graph, n); e != INVALID; ++e) {
1.2165 + Node m = graph.target(e);
1.2166 + if (conn[m] == -1) {
1.2167 + conn[m] = 1;
1.2168 + } else if (conn[m] != -2) {
1.2169 + conn[m] += 1;
1.2170 + Arc pe = graph.oppositeArc(e);
1.2171 + if (conn[graph.target(next[pe])] == -2) {
1.2172 + conn[m] -= 1;
1.2173 + }
1.2174 + if (conn[graph.target(prev[pe])] == -2) {
1.2175 + conn[m] -= 1;
1.2176 + }
1.2177 +
1.2178 + proper.set(m, conn[m] == 1);
1.2179 + }
1.2180 + }
1.2181 +
1.2182 + {
1.2183 + Arc e = OutArcIt(graph, n);
1.2184 + Arc p = e, l = e;
1.2185 +
1.2186 + e = next[e];
1.2187 + while (e != l) {
1.2188 +
1.2189 + if (conn[graph.target(e)] == -2 && conn[graph.target(p)] == -2) {
1.2190 + Arc f = e;
1.2191 + angle[f] = 0;
1.2192 + f = next[graph.oppositeArc(f)];
1.2193 + angle[f] = 1;
1.2194 + f = next[graph.oppositeArc(f)];
1.2195 + angle[f] = 2;
1.2196 + }
1.2197 +
1.2198 + p = e;
1.2199 + e = next[e];
1.2200 + }
1.2201 +
1.2202 + if (conn[graph.target(e)] == -2 && conn[graph.target(p)] == -2) {
1.2203 + Arc f = e;
1.2204 + angle[f] = 0;
1.2205 + f = next[graph.oppositeArc(f)];
1.2206 + angle[f] = 1;
1.2207 + f = next[graph.oppositeArc(f)];
1.2208 + angle[f] = 2;
1.2209 + }
1.2210 + }
1.2211 + }
1.2212 +
1.2213 + typename AuxGraph::template NodeMap<Node> apred(graph, INVALID);
1.2214 + typename AuxGraph::template NodeMap<Node> bpred(graph, INVALID);
1.2215 + typename AuxGraph::template NodeMap<Node> cpred(graph, INVALID);
1.2216 +
1.2217 + typename AuxGraph::template NodeMap<int> apredid(graph, -1);
1.2218 + typename AuxGraph::template NodeMap<int> bpredid(graph, -1);
1.2219 + typename AuxGraph::template NodeMap<int> cpredid(graph, -1);
1.2220 +
1.2221 + for (ArcIt e(graph); e != INVALID; ++e) {
1.2222 + if (angle[e] == angle[next[e]]) {
1.2223 + switch (angle[e]) {
1.2224 + case 2:
1.2225 + apred[graph.target(e)] = graph.source(e);
1.2226 + apredid[graph.target(e)] = graph.id(graph.source(e));
1.2227 + break;
1.2228 + case 1:
1.2229 + bpred[graph.target(e)] = graph.source(e);
1.2230 + bpredid[graph.target(e)] = graph.id(graph.source(e));
1.2231 + break;
1.2232 + case 0:
1.2233 + cpred[graph.target(e)] = graph.source(e);
1.2234 + cpredid[graph.target(e)] = graph.id(graph.source(e));
1.2235 + break;
1.2236 + }
1.2237 + }
1.2238 + }
1.2239 +
1.2240 + cpred[anode] = INVALID;
1.2241 + cpred[bnode] = INVALID;
1.2242 +
1.2243 + std::vector<Node> aorder, border, corder;
1.2244 +
1.2245 + {
1.2246 + typename AuxGraph::template NodeMap<bool> processed(graph, false);
1.2247 + std::vector<Node> st;
1.2248 + for (NodeIt n(graph); n != INVALID; ++n) {
1.2249 + if (!processed[n] && n != bnode && n != cnode) {
1.2250 + st.push_back(n);
1.2251 + processed[n] = true;
1.2252 + Node m = apred[n];
1.2253 + while (m != INVALID && !processed[m]) {
1.2254 + st.push_back(m);
1.2255 + processed[m] = true;
1.2256 + m = apred[m];
1.2257 + }
1.2258 + while (!st.empty()) {
1.2259 + aorder.push_back(st.back());
1.2260 + st.pop_back();
1.2261 + }
1.2262 + }
1.2263 + }
1.2264 + }
1.2265 +
1.2266 + {
1.2267 + typename AuxGraph::template NodeMap<bool> processed(graph, false);
1.2268 + std::vector<Node> st;
1.2269 + for (NodeIt n(graph); n != INVALID; ++n) {
1.2270 + if (!processed[n] && n != cnode && n != anode) {
1.2271 + st.push_back(n);
1.2272 + processed[n] = true;
1.2273 + Node m = bpred[n];
1.2274 + while (m != INVALID && !processed[m]) {
1.2275 + st.push_back(m);
1.2276 + processed[m] = true;
1.2277 + m = bpred[m];
1.2278 + }
1.2279 + while (!st.empty()) {
1.2280 + border.push_back(st.back());
1.2281 + st.pop_back();
1.2282 + }
1.2283 + }
1.2284 + }
1.2285 + }
1.2286 +
1.2287 + {
1.2288 + typename AuxGraph::template NodeMap<bool> processed(graph, false);
1.2289 + std::vector<Node> st;
1.2290 + for (NodeIt n(graph); n != INVALID; ++n) {
1.2291 + if (!processed[n] && n != anode && n != bnode) {
1.2292 + st.push_back(n);
1.2293 + processed[n] = true;
1.2294 + Node m = cpred[n];
1.2295 + while (m != INVALID && !processed[m]) {
1.2296 + st.push_back(m);
1.2297 + processed[m] = true;
1.2298 + m = cpred[m];
1.2299 + }
1.2300 + while (!st.empty()) {
1.2301 + corder.push_back(st.back());
1.2302 + st.pop_back();
1.2303 + }
1.2304 + }
1.2305 + }
1.2306 + }
1.2307 +
1.2308 + typename AuxGraph::template NodeMap<int> atree(graph, 0);
1.2309 + for (int i = aorder.size() - 1; i >= 0; --i) {
1.2310 + Node n = aorder[i];
1.2311 + atree[n] = 1;
1.2312 + for (OutArcIt e(graph, n); e != INVALID; ++e) {
1.2313 + if (apred[graph.target(e)] == n) {
1.2314 + atree[n] += atree[graph.target(e)];
1.2315 + }
1.2316 + }
1.2317 + }
1.2318 +
1.2319 + typename AuxGraph::template NodeMap<int> btree(graph, 0);
1.2320 + for (int i = border.size() - 1; i >= 0; --i) {
1.2321 + Node n = border[i];
1.2322 + btree[n] = 1;
1.2323 + for (OutArcIt e(graph, n); e != INVALID; ++e) {
1.2324 + if (bpred[graph.target(e)] == n) {
1.2325 + btree[n] += btree[graph.target(e)];
1.2326 + }
1.2327 + }
1.2328 + }
1.2329 +
1.2330 + typename AuxGraph::template NodeMap<int> apath(graph, 0);
1.2331 + apath[bnode] = apath[cnode] = 1;
1.2332 + typename AuxGraph::template NodeMap<int> apath_btree(graph, 0);
1.2333 + apath_btree[bnode] = btree[bnode];
1.2334 + for (int i = 1; i < int(aorder.size()); ++i) {
1.2335 + Node n = aorder[i];
1.2336 + apath[n] = apath[apred[n]] + 1;
1.2337 + apath_btree[n] = btree[n] + apath_btree[apred[n]];
1.2338 + }
1.2339 +
1.2340 + typename AuxGraph::template NodeMap<int> bpath_atree(graph, 0);
1.2341 + bpath_atree[anode] = atree[anode];
1.2342 + for (int i = 1; i < int(border.size()); ++i) {
1.2343 + Node n = border[i];
1.2344 + bpath_atree[n] = atree[n] + bpath_atree[bpred[n]];
1.2345 + }
1.2346 +
1.2347 + typename AuxGraph::template NodeMap<int> cpath(graph, 0);
1.2348 + cpath[anode] = cpath[bnode] = 1;
1.2349 + typename AuxGraph::template NodeMap<int> cpath_atree(graph, 0);
1.2350 + cpath_atree[anode] = atree[anode];
1.2351 + typename AuxGraph::template NodeMap<int> cpath_btree(graph, 0);
1.2352 + cpath_btree[bnode] = btree[bnode];
1.2353 + for (int i = 1; i < int(corder.size()); ++i) {
1.2354 + Node n = corder[i];
1.2355 + cpath[n] = cpath[cpred[n]] + 1;
1.2356 + cpath_atree[n] = atree[n] + cpath_atree[cpred[n]];
1.2357 + cpath_btree[n] = btree[n] + cpath_btree[cpred[n]];
1.2358 + }
1.2359 +
1.2360 + typename AuxGraph::template NodeMap<int> third(graph);
1.2361 + for (NodeIt n(graph); n != INVALID; ++n) {
1.2362 + point_map[n].x =
1.2363 + bpath_atree[n] + cpath_atree[n] - atree[n] - cpath[n] + 1;
1.2364 + point_map[n].y =
1.2365 + cpath_btree[n] + apath_btree[n] - btree[n] - apath[n] + 1;
1.2366 + }
1.2367 +
1.2368 + }
1.2369 +
1.2370 + public:
1.2371 +
1.2372 + /// \brief Calculates the node positions
1.2373 + ///
1.2374 + /// This function calculates the node positions.
1.2375 + /// \return %True if the graph is planar.
1.2376 + bool run() {
1.2377 + PlanarEmbedding<Graph> pe(_graph);
1.2378 + if (!pe.run()) return false;
1.2379 +
1.2380 + run(pe);
1.2381 + return true;
1.2382 + }
1.2383 +
1.2384 + /// \brief Calculates the node positions according to a
1.2385 + /// combinatorical embedding
1.2386 + ///
1.2387 + /// This function calculates the node locations. The \c embedding
1.2388 + /// parameter should contain a valid combinatorical embedding, i.e.
1.2389 + /// a valid cyclic order of the arcs.
1.2390 + template <typename EmbeddingMap>
1.2391 + void run(const EmbeddingMap& embedding) {
1.2392 + typedef SmartEdgeSet<Graph> AuxGraph;
1.2393 +
1.2394 + if (3 * countNodes(_graph) - 6 == countEdges(_graph)) {
1.2395 + drawing(_graph, embedding, _point_map);
1.2396 + return;
1.2397 + }
1.2398 +
1.2399 + AuxGraph aux_graph(_graph);
1.2400 + typename AuxGraph::template ArcMap<typename AuxGraph::Arc>
1.2401 + aux_embedding(aux_graph);
1.2402 +
1.2403 + {
1.2404 +
1.2405 + typename Graph::template EdgeMap<typename AuxGraph::Edge>
1.2406 + ref(_graph);
1.2407 +
1.2408 + for (EdgeIt e(_graph); e != INVALID; ++e) {
1.2409 + ref[e] = aux_graph.addEdge(_graph.u(e), _graph.v(e));
1.2410 + }
1.2411 +
1.2412 + for (EdgeIt e(_graph); e != INVALID; ++e) {
1.2413 + Arc ee = embedding[_graph.direct(e, true)];
1.2414 + aux_embedding[aux_graph.direct(ref[e], true)] =
1.2415 + aux_graph.direct(ref[ee], _graph.direction(ee));
1.2416 + ee = embedding[_graph.direct(e, false)];
1.2417 + aux_embedding[aux_graph.direct(ref[e], false)] =
1.2418 + aux_graph.direct(ref[ee], _graph.direction(ee));
1.2419 + }
1.2420 + }
1.2421 + _planarity_bits::makeConnected(aux_graph, aux_embedding);
1.2422 + _planarity_bits::makeBiNodeConnected(aux_graph, aux_embedding);
1.2423 + _planarity_bits::makeMaxPlanar(aux_graph, aux_embedding);
1.2424 + drawing(aux_graph, aux_embedding, _point_map);
1.2425 + }
1.2426 +
1.2427 + /// \brief The coordinate of the given node
1.2428 + ///
1.2429 + /// The coordinate of the given node.
1.2430 + Point operator[](const Node& node) const {
1.2431 + return _point_map[node];
1.2432 + }
1.2433 +
1.2434 + /// \brief Returns the grid embedding in a \e NodeMap.
1.2435 + ///
1.2436 + /// Returns the grid embedding in a \e NodeMap of \c dim2::Point<int> .
1.2437 + const PointMap& coords() const {
1.2438 + return _point_map;
1.2439 + }
1.2440 +
1.2441 + private:
1.2442 +
1.2443 + const Graph& _graph;
1.2444 + PointMap _point_map;
1.2445 +
1.2446 + };
1.2447 +
1.2448 + namespace _planarity_bits {
1.2449 +
1.2450 + template <typename ColorMap>
1.2451 + class KempeFilter {
1.2452 + public:
1.2453 + typedef typename ColorMap::Key Key;
1.2454 + typedef bool Value;
1.2455 +
1.2456 + KempeFilter(const ColorMap& color_map,
1.2457 + const typename ColorMap::Value& first,
1.2458 + const typename ColorMap::Value& second)
1.2459 + : _color_map(color_map), _first(first), _second(second) {}
1.2460 +
1.2461 + Value operator[](const Key& key) const {
1.2462 + return _color_map[key] == _first || _color_map[key] == _second;
1.2463 + }
1.2464 +
1.2465 + private:
1.2466 + const ColorMap& _color_map;
1.2467 + typename ColorMap::Value _first, _second;
1.2468 + };
1.2469 + }
1.2470 +
1.2471 + /// \ingroup planar
1.2472 + ///
1.2473 + /// \brief Coloring planar graphs
1.2474 + ///
1.2475 + /// The graph coloring problem is the coloring of the graph nodes
1.2476 + /// that there are not adjacent nodes with the same color. The
1.2477 + /// planar graphs can be always colored with four colors, it is
1.2478 + /// proved by Appel and Haken and their proofs provide a quadratic
1.2479 + /// time algorithm for four coloring, but it could not be used to
1.2480 + /// implement efficient algorithm. The five and six coloring can be
1.2481 + /// made in linear time, but in this class the five coloring has
1.2482 + /// quadratic worst case time complexity. The two coloring (if
1.2483 + /// possible) is solvable with a graph search algorithm and it is
1.2484 + /// implemented in \ref bipartitePartitions() function in LEMON. To
1.2485 + /// decide whether the planar graph is three colorable is
1.2486 + /// NP-complete.
1.2487 + ///
1.2488 + /// This class contains member functions for calculate colorings
1.2489 + /// with five and six colors. The six coloring algorithm is a simple
1.2490 + /// greedy coloring on the backward minimum outgoing order of nodes.
1.2491 + /// This order can be computed as in each phase the node with least
1.2492 + /// outgoing arcs to unprocessed nodes is chosen. This order
1.2493 + /// guarantees that when a node is chosen for coloring it has at
1.2494 + /// most five already colored adjacents. The five coloring algorithm
1.2495 + /// use the same method, but if the greedy approach fails to color
1.2496 + /// with five colors, i.e. the node has five already different
1.2497 + /// colored neighbours, it swaps the colors in one of the connected
1.2498 + /// two colored sets with the Kempe recoloring method.
1.2499 + template <typename Graph>
1.2500 + class PlanarColoring {
1.2501 + public:
1.2502 +
1.2503 + TEMPLATE_GRAPH_TYPEDEFS(Graph);
1.2504 +
1.2505 + /// \brief The map type for store color indexes
1.2506 + typedef typename Graph::template NodeMap<int> IndexMap;
1.2507 + /// \brief The map type for store colors
1.2508 + typedef ComposeMap<Palette, IndexMap> ColorMap;
1.2509 +
1.2510 + /// \brief Constructor
1.2511 + ///
1.2512 + /// Constructor
1.2513 + /// \pre The graph should be simple, i.e. loop and parallel arc free.
1.2514 + PlanarColoring(const Graph& graph)
1.2515 + : _graph(graph), _color_map(graph), _palette(0) {
1.2516 + _palette.add(Color(1,0,0));
1.2517 + _palette.add(Color(0,1,0));
1.2518 + _palette.add(Color(0,0,1));
1.2519 + _palette.add(Color(1,1,0));
1.2520 + _palette.add(Color(1,0,1));
1.2521 + _palette.add(Color(0,1,1));
1.2522 + }
1.2523 +
1.2524 + /// \brief Returns the \e NodeMap of color indexes
1.2525 + ///
1.2526 + /// Returns the \e NodeMap of color indexes. The values are in the
1.2527 + /// range \c [0..4] or \c [0..5] according to the coloring method.
1.2528 + IndexMap colorIndexMap() const {
1.2529 + return _color_map;
1.2530 + }
1.2531 +
1.2532 + /// \brief Returns the \e NodeMap of colors
1.2533 + ///
1.2534 + /// Returns the \e NodeMap of colors. The values are five or six
1.2535 + /// distinct \ref lemon::Color "colors".
1.2536 + ColorMap colorMap() const {
1.2537 + return composeMap(_palette, _color_map);
1.2538 + }
1.2539 +
1.2540 + /// \brief Returns the color index of the node
1.2541 + ///
1.2542 + /// Returns the color index of the node. The values are in the
1.2543 + /// range \c [0..4] or \c [0..5] according to the coloring method.
1.2544 + int colorIndex(const Node& node) const {
1.2545 + return _color_map[node];
1.2546 + }
1.2547 +
1.2548 + /// \brief Returns the color of the node
1.2549 + ///
1.2550 + /// Returns the color of the node. The values are five or six
1.2551 + /// distinct \ref lemon::Color "colors".
1.2552 + Color color(const Node& node) const {
1.2553 + return _palette[_color_map[node]];
1.2554 + }
1.2555 +
1.2556 +
1.2557 + /// \brief Calculates a coloring with at most six colors
1.2558 + ///
1.2559 + /// This function calculates a coloring with at most six colors. The time
1.2560 + /// complexity of this variant is linear in the size of the graph.
1.2561 + /// \return %True when the algorithm could color the graph with six color.
1.2562 + /// If the algorithm fails, then the graph could not be planar.
1.2563 + /// \note This function can return true if the graph is not
1.2564 + /// planar but it can be colored with 6 colors.
1.2565 + bool runSixColoring() {
1.2566 +
1.2567 + typename Graph::template NodeMap<int> heap_index(_graph, -1);
1.2568 + BucketHeap<typename Graph::template NodeMap<int> > heap(heap_index);
1.2569 +
1.2570 + for (NodeIt n(_graph); n != INVALID; ++n) {
1.2571 + _color_map[n] = -2;
1.2572 + heap.push(n, countOutArcs(_graph, n));
1.2573 + }
1.2574 +
1.2575 + std::vector<Node> order;
1.2576 +
1.2577 + while (!heap.empty()) {
1.2578 + Node n = heap.top();
1.2579 + heap.pop();
1.2580 + _color_map[n] = -1;
1.2581 + order.push_back(n);
1.2582 + for (OutArcIt e(_graph, n); e != INVALID; ++e) {
1.2583 + Node t = _graph.runningNode(e);
1.2584 + if (_color_map[t] == -2) {
1.2585 + heap.decrease(t, heap[t] - 1);
1.2586 + }
1.2587 + }
1.2588 + }
1.2589 +
1.2590 + for (int i = order.size() - 1; i >= 0; --i) {
1.2591 + std::vector<bool> forbidden(6, false);
1.2592 + for (OutArcIt e(_graph, order[i]); e != INVALID; ++e) {
1.2593 + Node t = _graph.runningNode(e);
1.2594 + if (_color_map[t] != -1) {
1.2595 + forbidden[_color_map[t]] = true;
1.2596 + }
1.2597 + }
1.2598 + for (int k = 0; k < 6; ++k) {
1.2599 + if (!forbidden[k]) {
1.2600 + _color_map[order[i]] = k;
1.2601 + break;
1.2602 + }
1.2603 + }
1.2604 + if (_color_map[order[i]] == -1) {
1.2605 + return false;
1.2606 + }
1.2607 + }
1.2608 + return true;
1.2609 + }
1.2610 +
1.2611 + private:
1.2612 +
1.2613 + bool recolor(const Node& u, const Node& v) {
1.2614 + int ucolor = _color_map[u];
1.2615 + int vcolor = _color_map[v];
1.2616 + typedef _planarity_bits::KempeFilter<IndexMap> KempeFilter;
1.2617 + KempeFilter filter(_color_map, ucolor, vcolor);
1.2618 +
1.2619 + typedef FilterNodes<const Graph, const KempeFilter> KempeGraph;
1.2620 + KempeGraph kempe_graph(_graph, filter);
1.2621 +
1.2622 + std::vector<Node> comp;
1.2623 + Bfs<KempeGraph> bfs(kempe_graph);
1.2624 + bfs.init();
1.2625 + bfs.addSource(u);
1.2626 + while (!bfs.emptyQueue()) {
1.2627 + Node n = bfs.nextNode();
1.2628 + if (n == v) return false;
1.2629 + comp.push_back(n);
1.2630 + bfs.processNextNode();
1.2631 + }
1.2632 +
1.2633 + int scolor = ucolor + vcolor;
1.2634 + for (int i = 0; i < static_cast<int>(comp.size()); ++i) {
1.2635 + _color_map[comp[i]] = scolor - _color_map[comp[i]];
1.2636 + }
1.2637 +
1.2638 + return true;
1.2639 + }
1.2640 +
1.2641 + template <typename EmbeddingMap>
1.2642 + void kempeRecoloring(const Node& node, const EmbeddingMap& embedding) {
1.2643 + std::vector<Node> nodes;
1.2644 + nodes.reserve(4);
1.2645 +
1.2646 + for (Arc e = OutArcIt(_graph, node); e != INVALID; e = embedding[e]) {
1.2647 + Node t = _graph.target(e);
1.2648 + if (_color_map[t] != -1) {
1.2649 + nodes.push_back(t);
1.2650 + if (nodes.size() == 4) break;
1.2651 + }
1.2652 + }
1.2653 +
1.2654 + int color = _color_map[nodes[0]];
1.2655 + if (recolor(nodes[0], nodes[2])) {
1.2656 + _color_map[node] = color;
1.2657 + } else {
1.2658 + color = _color_map[nodes[1]];
1.2659 + recolor(nodes[1], nodes[3]);
1.2660 + _color_map[node] = color;
1.2661 + }
1.2662 + }
1.2663 +
1.2664 + public:
1.2665 +
1.2666 + /// \brief Calculates a coloring with at most five colors
1.2667 + ///
1.2668 + /// This function calculates a coloring with at most five
1.2669 + /// colors. The worst case time complexity of this variant is
1.2670 + /// quadratic in the size of the graph.
1.2671 + template <typename EmbeddingMap>
1.2672 + void runFiveColoring(const EmbeddingMap& embedding) {
1.2673 +
1.2674 + typename Graph::template NodeMap<int> heap_index(_graph, -1);
1.2675 + BucketHeap<typename Graph::template NodeMap<int> > heap(heap_index);
1.2676 +
1.2677 + for (NodeIt n(_graph); n != INVALID; ++n) {
1.2678 + _color_map[n] = -2;
1.2679 + heap.push(n, countOutArcs(_graph, n));
1.2680 + }
1.2681 +
1.2682 + std::vector<Node> order;
1.2683 +
1.2684 + while (!heap.empty()) {
1.2685 + Node n = heap.top();
1.2686 + heap.pop();
1.2687 + _color_map[n] = -1;
1.2688 + order.push_back(n);
1.2689 + for (OutArcIt e(_graph, n); e != INVALID; ++e) {
1.2690 + Node t = _graph.runningNode(e);
1.2691 + if (_color_map[t] == -2) {
1.2692 + heap.decrease(t, heap[t] - 1);
1.2693 + }
1.2694 + }
1.2695 + }
1.2696 +
1.2697 + for (int i = order.size() - 1; i >= 0; --i) {
1.2698 + std::vector<bool> forbidden(5, false);
1.2699 + for (OutArcIt e(_graph, order[i]); e != INVALID; ++e) {
1.2700 + Node t = _graph.runningNode(e);
1.2701 + if (_color_map[t] != -1) {
1.2702 + forbidden[_color_map[t]] = true;
1.2703 + }
1.2704 + }
1.2705 + for (int k = 0; k < 5; ++k) {
1.2706 + if (!forbidden[k]) {
1.2707 + _color_map[order[i]] = k;
1.2708 + break;
1.2709 + }
1.2710 + }
1.2711 + if (_color_map[order[i]] == -1) {
1.2712 + kempeRecoloring(order[i], embedding);
1.2713 + }
1.2714 + }
1.2715 + }
1.2716 +
1.2717 + /// \brief Calculates a coloring with at most five colors
1.2718 + ///
1.2719 + /// This function calculates a coloring with at most five
1.2720 + /// colors. The worst case time complexity of this variant is
1.2721 + /// quadratic in the size of the graph.
1.2722 + /// \return %True when the graph is planar.
1.2723 + bool runFiveColoring() {
1.2724 + PlanarEmbedding<Graph> pe(_graph);
1.2725 + if (!pe.run()) return false;
1.2726 +
1.2727 + runFiveColoring(pe.embeddingMap());
1.2728 + return true;
1.2729 + }
1.2730 +
1.2731 + private:
1.2732 +
1.2733 + const Graph& _graph;
1.2734 + IndexMap _color_map;
1.2735 + Palette _palette;
1.2736 + };
1.2737 +
1.2738 +}
1.2739 +
1.2740 +#endif