deba@861: /* -*- mode: C++; indent-tabs-mode: nil; -*- deba@861: * deba@861: * This file is a part of LEMON, a generic C++ optimization library. deba@861: * alpar@1270: * Copyright (C) 2003-2013 deba@861: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@861: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@861: * deba@861: * Permission to use, modify and distribute this software is granted deba@861: * provided that this copyright notice appears in all copies. For deba@861: * precise terms see the accompanying LICENSE file. deba@861: * deba@861: * This software is provided "AS IS" with no warranty of any kind, deba@861: * express or implied, and with no claim as to its suitability for any deba@861: * purpose. deba@861: * deba@861: */ deba@861: deba@861: #ifndef LEMON_PLANARITY_H deba@861: #define LEMON_PLANARITY_H deba@861: deba@861: /// \ingroup planar deba@861: /// \file deba@861: /// \brief Planarity checking, embedding, drawing and coloring deba@861: deba@861: #include deba@861: #include deba@861: deba@861: #include deba@861: #include deba@861: #include deba@861: #include deba@861: #include deba@861: #include deba@861: #include deba@861: #include deba@861: #include deba@861: #include deba@861: deba@861: namespace lemon { deba@861: deba@861: namespace _planarity_bits { deba@861: deba@861: template deba@861: struct PlanarityVisitor : DfsVisitor { deba@861: deba@861: TEMPLATE_GRAPH_TYPEDEFS(Graph); deba@861: deba@861: typedef typename Graph::template NodeMap PredMap; deba@861: deba@861: typedef typename Graph::template EdgeMap TreeMap; deba@861: deba@861: typedef typename Graph::template NodeMap OrderMap; deba@861: typedef std::vector OrderList; deba@861: deba@861: typedef typename Graph::template NodeMap LowMap; deba@861: typedef typename Graph::template NodeMap AncestorMap; deba@861: deba@861: PlanarityVisitor(const Graph& graph, deba@861: PredMap& pred_map, TreeMap& tree_map, deba@861: OrderMap& order_map, OrderList& order_list, deba@861: AncestorMap& ancestor_map, LowMap& low_map) deba@861: : _graph(graph), _pred_map(pred_map), _tree_map(tree_map), deba@861: _order_map(order_map), _order_list(order_list), deba@861: _ancestor_map(ancestor_map), _low_map(low_map) {} deba@861: deba@861: void reach(const Node& node) { deba@861: _order_map[node] = _order_list.size(); deba@861: _low_map[node] = _order_list.size(); deba@861: _ancestor_map[node] = _order_list.size(); deba@861: _order_list.push_back(node); deba@861: } deba@861: deba@861: void discover(const Arc& arc) { deba@861: Node target = _graph.target(arc); deba@861: deba@861: _tree_map[arc] = true; deba@861: _pred_map[target] = arc; deba@861: } deba@861: deba@861: void examine(const Arc& arc) { deba@861: Node source = _graph.source(arc); deba@861: Node target = _graph.target(arc); deba@861: deba@861: if (_order_map[target] < _order_map[source] && !_tree_map[arc]) { deba@861: if (_low_map[source] > _order_map[target]) { deba@861: _low_map[source] = _order_map[target]; deba@861: } deba@861: if (_ancestor_map[source] > _order_map[target]) { deba@861: _ancestor_map[source] = _order_map[target]; deba@861: } deba@861: } deba@861: } deba@861: deba@861: void backtrack(const Arc& arc) { deba@861: Node source = _graph.source(arc); deba@861: Node target = _graph.target(arc); deba@861: deba@861: if (_low_map[source] > _low_map[target]) { deba@861: _low_map[source] = _low_map[target]; deba@861: } deba@861: } deba@861: deba@861: const Graph& _graph; deba@861: PredMap& _pred_map; deba@861: TreeMap& _tree_map; deba@861: OrderMap& _order_map; deba@861: OrderList& _order_list; deba@861: AncestorMap& _ancestor_map; deba@861: LowMap& _low_map; deba@861: }; deba@861: deba@861: template deba@861: struct NodeDataNode { deba@861: int prev, next; deba@861: int visited; deba@861: typename Graph::Arc first; deba@861: bool inverted; deba@861: }; deba@861: deba@861: template deba@861: struct NodeDataNode { deba@861: int prev, next; deba@861: int visited; deba@861: }; deba@861: deba@861: template deba@861: struct ChildListNode { deba@861: typedef typename Graph::Node Node; deba@861: Node first; deba@861: Node prev, next; deba@861: }; deba@861: deba@861: template deba@861: struct ArcListNode { deba@861: typename Graph::Arc prev, next; deba@861: }; deba@861: deba@862: template deba@862: class PlanarityChecking { deba@862: private: alpar@956: deba@862: TEMPLATE_GRAPH_TYPEDEFS(Graph); deba@862: deba@862: const Graph& _graph; deba@862: deba@862: private: alpar@956: deba@862: typedef typename Graph::template NodeMap PredMap; alpar@956: deba@862: typedef typename Graph::template EdgeMap TreeMap; alpar@956: deba@862: typedef typename Graph::template NodeMap OrderMap; deba@862: typedef std::vector OrderList; deba@862: deba@862: typedef typename Graph::template NodeMap LowMap; deba@862: typedef typename Graph::template NodeMap AncestorMap; deba@862: deba@862: typedef _planarity_bits::NodeDataNode NodeDataNode; deba@862: typedef std::vector NodeData; deba@862: deba@862: typedef _planarity_bits::ChildListNode ChildListNode; deba@862: typedef typename Graph::template NodeMap ChildLists; deba@862: deba@862: typedef typename Graph::template NodeMap > MergeRoots; deba@862: deba@862: typedef typename Graph::template NodeMap EmbedArc; deba@862: deba@862: public: deba@862: deba@862: PlanarityChecking(const Graph& graph) : _graph(graph) {} deba@862: deba@862: bool run() { deba@862: typedef _planarity_bits::PlanarityVisitor Visitor; deba@862: deba@862: PredMap pred_map(_graph, INVALID); deba@862: TreeMap tree_map(_graph, false); deba@862: deba@862: OrderMap order_map(_graph, -1); deba@862: OrderList order_list; deba@862: deba@862: AncestorMap ancestor_map(_graph, -1); deba@862: LowMap low_map(_graph, -1); deba@862: deba@862: Visitor visitor(_graph, pred_map, tree_map, deba@862: order_map, order_list, ancestor_map, low_map); deba@862: DfsVisit visit(_graph, visitor); deba@862: visit.run(); deba@862: deba@862: ChildLists child_lists(_graph); deba@862: createChildLists(tree_map, order_map, low_map, child_lists); deba@862: deba@862: NodeData node_data(2 * order_list.size()); deba@862: deba@862: EmbedArc embed_arc(_graph, false); deba@862: deba@862: MergeRoots merge_roots(_graph); deba@862: deba@862: for (int i = order_list.size() - 1; i >= 0; --i) { deba@862: deba@862: Node node = order_list[i]; deba@862: deba@862: Node source = node; deba@862: for (OutArcIt e(_graph, node); e != INVALID; ++e) { deba@862: Node target = _graph.target(e); deba@862: deba@862: if (order_map[source] < order_map[target] && tree_map[e]) { deba@862: initFace(target, node_data, order_map, order_list); deba@862: } deba@862: } deba@862: deba@862: for (OutArcIt e(_graph, node); e != INVALID; ++e) { deba@862: Node target = _graph.target(e); deba@862: deba@862: if (order_map[source] < order_map[target] && !tree_map[e]) { deba@862: embed_arc[target] = true; deba@862: walkUp(target, source, i, pred_map, low_map, deba@862: order_map, order_list, node_data, merge_roots); deba@862: } deba@862: } deba@862: deba@862: for (typename MergeRoots::Value::iterator it = alpar@956: merge_roots[node].begin(); deba@862: it != merge_roots[node].end(); ++it) { deba@862: int rn = *it; deba@862: walkDown(rn, i, node_data, order_list, child_lists, deba@862: ancestor_map, low_map, embed_arc, merge_roots); deba@862: } deba@862: merge_roots[node].clear(); deba@862: deba@862: for (OutArcIt e(_graph, node); e != INVALID; ++e) { deba@862: Node target = _graph.target(e); deba@862: deba@862: if (order_map[source] < order_map[target] && !tree_map[e]) { deba@862: if (embed_arc[target]) { deba@862: return false; deba@862: } deba@862: } deba@862: } deba@862: } deba@862: deba@862: return true; deba@862: } deba@862: deba@862: private: deba@862: deba@862: void createChildLists(const TreeMap& tree_map, const OrderMap& order_map, deba@862: const LowMap& low_map, ChildLists& child_lists) { deba@862: deba@862: for (NodeIt n(_graph); n != INVALID; ++n) { deba@862: Node source = n; deba@862: deba@862: std::vector targets; deba@862: for (OutArcIt e(_graph, n); e != INVALID; ++e) { deba@862: Node target = _graph.target(e); deba@862: deba@862: if (order_map[source] < order_map[target] && tree_map[e]) { deba@862: targets.push_back(target); deba@862: } deba@862: } deba@862: deba@862: if (targets.size() == 0) { deba@862: child_lists[source].first = INVALID; deba@862: } else if (targets.size() == 1) { deba@862: child_lists[source].first = targets[0]; deba@862: child_lists[targets[0]].prev = INVALID; deba@862: child_lists[targets[0]].next = INVALID; deba@862: } else { deba@862: radixSort(targets.begin(), targets.end(), mapToFunctor(low_map)); deba@862: for (int i = 1; i < int(targets.size()); ++i) { deba@862: child_lists[targets[i]].prev = targets[i - 1]; deba@862: child_lists[targets[i - 1]].next = targets[i]; deba@862: } deba@862: child_lists[targets.back()].next = INVALID; deba@862: child_lists[targets.front()].prev = INVALID; deba@862: child_lists[source].first = targets.front(); deba@862: } deba@862: } deba@862: } deba@862: deba@862: void walkUp(const Node& node, Node root, int rorder, deba@862: const PredMap& pred_map, const LowMap& low_map, deba@862: const OrderMap& order_map, const OrderList& order_list, deba@862: NodeData& node_data, MergeRoots& merge_roots) { deba@862: deba@862: int na, nb; deba@862: bool da, db; deba@862: deba@862: na = nb = order_map[node]; deba@862: da = true; db = false; deba@862: deba@862: while (true) { deba@862: deba@862: if (node_data[na].visited == rorder) break; deba@862: if (node_data[nb].visited == rorder) break; deba@862: deba@862: node_data[na].visited = rorder; deba@862: node_data[nb].visited = rorder; deba@862: deba@862: int rn = -1; deba@862: deba@862: if (na >= int(order_list.size())) { deba@862: rn = na; deba@862: } else if (nb >= int(order_list.size())) { deba@862: rn = nb; deba@862: } deba@862: deba@862: if (rn == -1) { deba@862: int nn; deba@862: deba@862: nn = da ? node_data[na].prev : node_data[na].next; deba@862: da = node_data[nn].prev != na; deba@862: na = nn; deba@862: deba@862: nn = db ? node_data[nb].prev : node_data[nb].next; deba@862: db = node_data[nn].prev != nb; deba@862: nb = nn; deba@862: deba@862: } else { deba@862: deba@862: Node rep = order_list[rn - order_list.size()]; deba@862: Node parent = _graph.source(pred_map[rep]); deba@862: deba@862: if (low_map[rep] < rorder) { deba@862: merge_roots[parent].push_back(rn); deba@862: } else { deba@862: merge_roots[parent].push_front(rn); deba@862: } deba@862: deba@862: if (parent != root) { deba@862: na = nb = order_map[parent]; deba@862: da = true; db = false; deba@862: } else { deba@862: break; deba@862: } deba@862: } deba@862: } deba@862: } deba@862: deba@862: void walkDown(int rn, int rorder, NodeData& node_data, deba@862: OrderList& order_list, ChildLists& child_lists, deba@862: AncestorMap& ancestor_map, LowMap& low_map, deba@862: EmbedArc& embed_arc, MergeRoots& merge_roots) { deba@862: deba@862: std::vector > merge_stack; deba@862: deba@862: for (int di = 0; di < 2; ++di) { deba@862: bool rd = di == 0; deba@862: int pn = rn; deba@862: int n = rd ? node_data[rn].next : node_data[rn].prev; deba@862: deba@862: while (n != rn) { deba@862: deba@862: Node node = order_list[n]; deba@862: deba@862: if (embed_arc[node]) { deba@862: deba@862: // Merging components on the critical path deba@862: while (!merge_stack.empty()) { deba@862: deba@862: // Component root deba@862: int cn = merge_stack.back().first; deba@862: bool cd = merge_stack.back().second; deba@862: merge_stack.pop_back(); deba@862: deba@862: // Parent of component deba@862: int dn = merge_stack.back().first; deba@862: bool dd = merge_stack.back().second; deba@862: merge_stack.pop_back(); deba@862: deba@862: Node parent = order_list[dn]; deba@862: deba@862: // Erasing from merge_roots deba@862: merge_roots[parent].pop_front(); deba@862: deba@862: Node child = order_list[cn - order_list.size()]; deba@862: deba@862: // Erasing from child_lists deba@862: if (child_lists[child].prev != INVALID) { deba@862: child_lists[child_lists[child].prev].next = deba@862: child_lists[child].next; deba@862: } else { deba@862: child_lists[parent].first = child_lists[child].next; deba@862: } deba@862: deba@862: if (child_lists[child].next != INVALID) { deba@862: child_lists[child_lists[child].next].prev = deba@862: child_lists[child].prev; deba@862: } deba@862: deba@862: // Merging external faces deba@862: { deba@862: int en = cn; deba@862: cn = cd ? node_data[cn].prev : node_data[cn].next; deba@862: cd = node_data[cn].next == en; deba@862: deba@862: } deba@862: deba@862: if (cd) node_data[cn].next = dn; else node_data[cn].prev = dn; deba@862: if (dd) node_data[dn].prev = cn; else node_data[dn].next = cn; deba@862: deba@862: } deba@862: deba@862: bool d = pn == node_data[n].prev; deba@862: deba@862: if (node_data[n].prev == node_data[n].next && deba@862: node_data[n].inverted) { deba@862: d = !d; deba@862: } deba@862: deba@862: // Embedding arc into external face deba@862: if (rd) node_data[rn].next = n; else node_data[rn].prev = n; deba@862: if (d) node_data[n].prev = rn; else node_data[n].next = rn; deba@862: pn = rn; deba@862: deba@862: embed_arc[order_list[n]] = false; deba@862: } deba@862: deba@862: if (!merge_roots[node].empty()) { deba@862: deba@862: bool d = pn == node_data[n].prev; deba@862: deba@862: merge_stack.push_back(std::make_pair(n, d)); deba@862: deba@862: int rn = merge_roots[node].front(); deba@862: deba@862: int xn = node_data[rn].next; deba@862: Node xnode = order_list[xn]; deba@862: deba@862: int yn = node_data[rn].prev; deba@862: Node ynode = order_list[yn]; deba@862: deba@862: bool rd; alpar@956: if (!external(xnode, rorder, child_lists, deba@862: ancestor_map, low_map)) { deba@862: rd = true; deba@862: } else if (!external(ynode, rorder, child_lists, deba@862: ancestor_map, low_map)) { deba@862: rd = false; deba@862: } else if (pertinent(xnode, embed_arc, merge_roots)) { deba@862: rd = true; deba@862: } else { deba@862: rd = false; deba@862: } deba@862: deba@862: merge_stack.push_back(std::make_pair(rn, rd)); deba@862: deba@862: pn = rn; deba@862: n = rd ? xn : yn; deba@862: deba@862: } else if (!external(node, rorder, child_lists, deba@862: ancestor_map, low_map)) { deba@862: int nn = (node_data[n].next != pn ? deba@862: node_data[n].next : node_data[n].prev); deba@862: deba@862: bool nd = n == node_data[nn].prev; deba@862: deba@862: if (nd) node_data[nn].prev = pn; deba@862: else node_data[nn].next = pn; deba@862: deba@862: if (n == node_data[pn].prev) node_data[pn].prev = nn; deba@862: else node_data[pn].next = nn; deba@862: deba@862: node_data[nn].inverted = deba@862: (node_data[nn].prev == node_data[nn].next && nd != rd); deba@862: deba@862: n = nn; deba@862: } deba@862: else break; deba@862: deba@862: } deba@862: deba@862: if (!merge_stack.empty() || n == rn) { deba@862: break; deba@862: } deba@862: } deba@862: } deba@862: deba@862: void initFace(const Node& node, NodeData& node_data, deba@862: const OrderMap& order_map, const OrderList& order_list) { deba@862: int n = order_map[node]; deba@862: int rn = n + order_list.size(); deba@862: deba@862: node_data[n].next = node_data[n].prev = rn; deba@862: node_data[rn].next = node_data[rn].prev = n; deba@862: deba@862: node_data[n].visited = order_list.size(); deba@862: node_data[rn].visited = order_list.size(); deba@862: deba@862: } deba@862: deba@862: bool external(const Node& node, int rorder, deba@862: ChildLists& child_lists, AncestorMap& ancestor_map, deba@862: LowMap& low_map) { deba@862: Node child = child_lists[node].first; deba@862: deba@862: if (child != INVALID) { deba@862: if (low_map[child] < rorder) return true; deba@862: } deba@862: deba@862: if (ancestor_map[node] < rorder) return true; deba@862: deba@862: return false; deba@862: } deba@862: deba@862: bool pertinent(const Node& node, const EmbedArc& embed_arc, deba@862: const MergeRoots& merge_roots) { deba@862: return !merge_roots[node].empty() || embed_arc[node]; deba@862: } deba@862: deba@862: }; deba@862: deba@861: } deba@861: deba@861: /// \ingroup planar deba@861: /// deba@861: /// \brief Planarity checking of an undirected simple graph deba@861: /// deba@862: /// This function implements the Boyer-Myrvold algorithm for kpeter@896: /// planarity checking of an undirected simple graph. It is a simplified deba@861: /// version of the PlanarEmbedding algorithm class because neither kpeter@896: /// the embedding nor the Kuratowski subdivisons are computed. deba@862: template deba@862: bool checkPlanarity(const GR& graph) { deba@862: _planarity_bits::PlanarityChecking pc(graph); deba@862: return pc.run(); deba@862: } deba@861: deba@861: /// \ingroup planar deba@861: /// deba@861: /// \brief Planar embedding of an undirected simple graph deba@861: /// deba@861: /// This class implements the Boyer-Myrvold algorithm for planar kpeter@896: /// embedding of an undirected simple graph. The planar embedding is an deba@861: /// ordering of the outgoing edges of the nodes, which is a possible deba@861: /// configuration to draw the graph in the plane. If there is not kpeter@896: /// such ordering then the graph contains a K5 (full graph kpeter@896: /// with 5 nodes) or a K3,3 (complete bipartite graph on kpeter@896: /// 3 Red and 3 Blue nodes) subdivision. deba@861: /// deba@861: /// The current implementation calculates either an embedding or a kpeter@896: /// Kuratowski subdivision. The running time of the algorithm is O(n). kpeter@896: /// kpeter@896: /// \see PlanarDrawing, checkPlanarity() deba@861: template deba@861: class PlanarEmbedding { deba@861: private: deba@861: deba@861: TEMPLATE_GRAPH_TYPEDEFS(Graph); deba@861: deba@861: const Graph& _graph; deba@861: typename Graph::template ArcMap _embedding; deba@861: deba@861: typename Graph::template EdgeMap _kuratowski; deba@861: deba@861: private: deba@861: deba@861: typedef typename Graph::template NodeMap PredMap; deba@861: deba@861: typedef typename Graph::template EdgeMap TreeMap; deba@861: deba@861: typedef typename Graph::template NodeMap OrderMap; deba@861: typedef std::vector OrderList; deba@861: deba@861: typedef typename Graph::template NodeMap LowMap; deba@861: typedef typename Graph::template NodeMap AncestorMap; deba@861: deba@861: typedef _planarity_bits::NodeDataNode NodeDataNode; deba@861: typedef std::vector NodeData; deba@861: deba@861: typedef _planarity_bits::ChildListNode ChildListNode; deba@861: typedef typename Graph::template NodeMap ChildLists; deba@861: deba@861: typedef typename Graph::template NodeMap > MergeRoots; deba@861: deba@861: typedef typename Graph::template NodeMap EmbedArc; deba@861: deba@861: typedef _planarity_bits::ArcListNode ArcListNode; deba@861: typedef typename Graph::template ArcMap ArcLists; deba@861: deba@861: typedef typename Graph::template NodeMap FlipMap; deba@861: deba@861: typedef typename Graph::template NodeMap TypeMap; deba@861: deba@861: enum IsolatorNodeType { deba@861: HIGHX = 6, LOWX = 7, deba@861: HIGHY = 8, LOWY = 9, deba@861: ROOT = 10, PERTINENT = 11, deba@861: INTERNAL = 12 deba@861: }; deba@861: deba@861: public: deba@861: kpeter@896: /// \brief The map type for storing the embedding kpeter@896: /// kpeter@896: /// The map type for storing the embedding. kpeter@896: /// \see embeddingMap() deba@861: typedef typename Graph::template ArcMap EmbeddingMap; deba@861: deba@861: /// \brief Constructor deba@861: /// kpeter@896: /// Constructor. kpeter@896: /// \pre The graph must be simple, i.e. it should not kpeter@896: /// contain parallel or loop arcs. deba@861: PlanarEmbedding(const Graph& graph) deba@861: : _graph(graph), _embedding(_graph), _kuratowski(graph, false) {} deba@861: kpeter@896: /// \brief Run the algorithm. deba@861: /// kpeter@896: /// This function runs the algorithm. kpeter@896: /// \param kuratowski If this parameter is set to \c false, then the deba@861: /// algorithm does not compute a Kuratowski subdivision. kpeter@896: /// \return \c true if the graph is planar. deba@861: bool run(bool kuratowski = true) { deba@861: typedef _planarity_bits::PlanarityVisitor Visitor; deba@861: deba@861: PredMap pred_map(_graph, INVALID); deba@861: TreeMap tree_map(_graph, false); deba@861: deba@861: OrderMap order_map(_graph, -1); deba@861: OrderList order_list; deba@861: deba@861: AncestorMap ancestor_map(_graph, -1); deba@861: LowMap low_map(_graph, -1); deba@861: deba@861: Visitor visitor(_graph, pred_map, tree_map, deba@861: order_map, order_list, ancestor_map, low_map); deba@861: DfsVisit visit(_graph, visitor); deba@861: visit.run(); deba@861: deba@861: ChildLists child_lists(_graph); deba@861: createChildLists(tree_map, order_map, low_map, child_lists); deba@861: deba@861: NodeData node_data(2 * order_list.size()); deba@861: deba@861: EmbedArc embed_arc(_graph, INVALID); deba@861: deba@861: MergeRoots merge_roots(_graph); deba@861: deba@861: ArcLists arc_lists(_graph); deba@861: deba@861: FlipMap flip_map(_graph, false); deba@861: deba@861: for (int i = order_list.size() - 1; i >= 0; --i) { deba@861: deba@861: Node node = order_list[i]; deba@861: deba@861: node_data[i].first = INVALID; deba@861: deba@861: Node source = node; deba@861: for (OutArcIt e(_graph, node); e != INVALID; ++e) { deba@861: Node target = _graph.target(e); deba@861: deba@861: if (order_map[source] < order_map[target] && tree_map[e]) { deba@861: initFace(target, arc_lists, node_data, deba@861: pred_map, order_map, order_list); deba@861: } deba@861: } deba@861: deba@861: for (OutArcIt e(_graph, node); e != INVALID; ++e) { deba@861: Node target = _graph.target(e); deba@861: deba@861: if (order_map[source] < order_map[target] && !tree_map[e]) { deba@861: embed_arc[target] = e; deba@861: walkUp(target, source, i, pred_map, low_map, deba@861: order_map, order_list, node_data, merge_roots); deba@861: } deba@861: } deba@861: deba@861: for (typename MergeRoots::Value::iterator it = deba@861: merge_roots[node].begin(); it != merge_roots[node].end(); ++it) { deba@861: int rn = *it; deba@861: walkDown(rn, i, node_data, arc_lists, flip_map, order_list, deba@861: child_lists, ancestor_map, low_map, embed_arc, merge_roots); deba@861: } deba@861: merge_roots[node].clear(); deba@861: deba@861: for (OutArcIt e(_graph, node); e != INVALID; ++e) { deba@861: Node target = _graph.target(e); deba@861: deba@861: if (order_map[source] < order_map[target] && !tree_map[e]) { deba@861: if (embed_arc[target] != INVALID) { deba@861: if (kuratowski) { deba@861: isolateKuratowski(e, node_data, arc_lists, flip_map, deba@861: order_map, order_list, pred_map, child_lists, deba@861: ancestor_map, low_map, deba@861: embed_arc, merge_roots); deba@861: } deba@861: return false; deba@861: } deba@861: } deba@861: } deba@861: } deba@861: deba@861: for (int i = 0; i < int(order_list.size()); ++i) { deba@861: deba@861: mergeRemainingFaces(order_list[i], node_data, order_list, order_map, deba@861: child_lists, arc_lists); deba@861: storeEmbedding(order_list[i], node_data, order_map, pred_map, deba@861: arc_lists, flip_map); deba@861: } deba@861: deba@861: return true; deba@861: } deba@861: kpeter@896: /// \brief Give back the successor of an arc deba@861: /// kpeter@896: /// This function gives back the successor of an arc. It makes deba@861: /// possible to query the cyclic order of the outgoing arcs from deba@861: /// a node. deba@861: Arc next(const Arc& arc) const { deba@861: return _embedding[arc]; deba@861: } deba@861: kpeter@896: /// \brief Give back the calculated embedding map deba@861: /// kpeter@896: /// This function gives back the calculated embedding map, which kpeter@896: /// contains the successor of each arc in the cyclic order of the kpeter@896: /// outgoing arcs of its source node. deba@862: const EmbeddingMap& embeddingMap() const { deba@861: return _embedding; deba@861: } deba@861: kpeter@896: /// \brief Give back \c true if the given edge is in the Kuratowski kpeter@896: /// subdivision deba@861: /// kpeter@896: /// This function gives back \c true if the given edge is in the found kpeter@896: /// Kuratowski subdivision. kpeter@896: /// \pre The \c run() function must be called with \c true parameter kpeter@896: /// before using this function. kpeter@896: bool kuratowski(const Edge& edge) const { deba@861: return _kuratowski[edge]; deba@861: } deba@861: deba@861: private: deba@861: deba@861: void createChildLists(const TreeMap& tree_map, const OrderMap& order_map, deba@861: const LowMap& low_map, ChildLists& child_lists) { deba@861: deba@861: for (NodeIt n(_graph); n != INVALID; ++n) { deba@861: Node source = n; deba@861: deba@861: std::vector targets; deba@861: for (OutArcIt e(_graph, n); e != INVALID; ++e) { deba@861: Node target = _graph.target(e); deba@861: deba@861: if (order_map[source] < order_map[target] && tree_map[e]) { deba@861: targets.push_back(target); deba@861: } deba@861: } deba@861: deba@861: if (targets.size() == 0) { deba@861: child_lists[source].first = INVALID; deba@861: } else if (targets.size() == 1) { deba@861: child_lists[source].first = targets[0]; deba@861: child_lists[targets[0]].prev = INVALID; deba@861: child_lists[targets[0]].next = INVALID; deba@861: } else { deba@861: radixSort(targets.begin(), targets.end(), mapToFunctor(low_map)); deba@861: for (int i = 1; i < int(targets.size()); ++i) { deba@861: child_lists[targets[i]].prev = targets[i - 1]; deba@861: child_lists[targets[i - 1]].next = targets[i]; deba@861: } deba@861: child_lists[targets.back()].next = INVALID; deba@861: child_lists[targets.front()].prev = INVALID; deba@861: child_lists[source].first = targets.front(); deba@861: } deba@861: } deba@861: } deba@861: deba@861: void walkUp(const Node& node, Node root, int rorder, deba@861: const PredMap& pred_map, const LowMap& low_map, deba@861: const OrderMap& order_map, const OrderList& order_list, deba@861: NodeData& node_data, MergeRoots& merge_roots) { deba@861: deba@861: int na, nb; deba@861: bool da, db; deba@861: deba@861: na = nb = order_map[node]; deba@861: da = true; db = false; deba@861: deba@861: while (true) { deba@861: deba@861: if (node_data[na].visited == rorder) break; deba@861: if (node_data[nb].visited == rorder) break; deba@861: deba@861: node_data[na].visited = rorder; deba@861: node_data[nb].visited = rorder; deba@861: deba@861: int rn = -1; deba@861: deba@861: if (na >= int(order_list.size())) { deba@861: rn = na; deba@861: } else if (nb >= int(order_list.size())) { deba@861: rn = nb; deba@861: } deba@861: deba@861: if (rn == -1) { deba@861: int nn; deba@861: deba@861: nn = da ? node_data[na].prev : node_data[na].next; deba@861: da = node_data[nn].prev != na; deba@861: na = nn; deba@861: deba@861: nn = db ? node_data[nb].prev : node_data[nb].next; deba@861: db = node_data[nn].prev != nb; deba@861: nb = nn; deba@861: deba@861: } else { deba@861: deba@861: Node rep = order_list[rn - order_list.size()]; deba@861: Node parent = _graph.source(pred_map[rep]); deba@861: deba@861: if (low_map[rep] < rorder) { deba@861: merge_roots[parent].push_back(rn); deba@861: } else { deba@861: merge_roots[parent].push_front(rn); deba@861: } deba@861: deba@861: if (parent != root) { deba@861: na = nb = order_map[parent]; deba@861: da = true; db = false; deba@861: } else { deba@861: break; deba@861: } deba@861: } deba@861: } deba@861: } deba@861: deba@861: void walkDown(int rn, int rorder, NodeData& node_data, deba@861: ArcLists& arc_lists, FlipMap& flip_map, deba@861: OrderList& order_list, ChildLists& child_lists, deba@861: AncestorMap& ancestor_map, LowMap& low_map, deba@861: EmbedArc& embed_arc, MergeRoots& merge_roots) { deba@861: deba@861: std::vector > merge_stack; deba@861: deba@861: for (int di = 0; di < 2; ++di) { deba@861: bool rd = di == 0; deba@861: int pn = rn; deba@861: int n = rd ? node_data[rn].next : node_data[rn].prev; deba@861: deba@861: while (n != rn) { deba@861: deba@861: Node node = order_list[n]; deba@861: deba@861: if (embed_arc[node] != INVALID) { deba@861: deba@861: // Merging components on the critical path deba@861: while (!merge_stack.empty()) { deba@861: deba@861: // Component root deba@861: int cn = merge_stack.back().first; deba@861: bool cd = merge_stack.back().second; deba@861: merge_stack.pop_back(); deba@861: deba@861: // Parent of component deba@861: int dn = merge_stack.back().first; deba@861: bool dd = merge_stack.back().second; deba@861: merge_stack.pop_back(); deba@861: deba@861: Node parent = order_list[dn]; deba@861: deba@861: // Erasing from merge_roots deba@861: merge_roots[parent].pop_front(); deba@861: deba@861: Node child = order_list[cn - order_list.size()]; deba@861: deba@861: // Erasing from child_lists deba@861: if (child_lists[child].prev != INVALID) { deba@861: child_lists[child_lists[child].prev].next = deba@861: child_lists[child].next; deba@861: } else { deba@861: child_lists[parent].first = child_lists[child].next; deba@861: } deba@861: deba@861: if (child_lists[child].next != INVALID) { deba@861: child_lists[child_lists[child].next].prev = deba@861: child_lists[child].prev; deba@861: } deba@861: deba@861: // Merging arcs + flipping deba@861: Arc de = node_data[dn].first; deba@861: Arc ce = node_data[cn].first; deba@861: deba@861: flip_map[order_list[cn - order_list.size()]] = cd != dd; deba@861: if (cd != dd) { deba@861: std::swap(arc_lists[ce].prev, arc_lists[ce].next); deba@861: ce = arc_lists[ce].prev; deba@861: std::swap(arc_lists[ce].prev, arc_lists[ce].next); deba@861: } deba@861: deba@861: { deba@861: Arc dne = arc_lists[de].next; deba@861: Arc cne = arc_lists[ce].next; deba@861: deba@861: arc_lists[de].next = cne; deba@861: arc_lists[ce].next = dne; deba@861: deba@861: arc_lists[dne].prev = ce; deba@861: arc_lists[cne].prev = de; deba@861: } deba@861: deba@861: if (dd) { deba@861: node_data[dn].first = ce; deba@861: } deba@861: deba@861: // Merging external faces deba@861: { deba@861: int en = cn; deba@861: cn = cd ? node_data[cn].prev : node_data[cn].next; deba@861: cd = node_data[cn].next == en; deba@861: deba@861: if (node_data[cn].prev == node_data[cn].next && deba@861: node_data[cn].inverted) { deba@861: cd = !cd; deba@861: } deba@861: } deba@861: deba@861: if (cd) node_data[cn].next = dn; else node_data[cn].prev = dn; deba@861: if (dd) node_data[dn].prev = cn; else node_data[dn].next = cn; deba@861: deba@861: } deba@861: deba@861: bool d = pn == node_data[n].prev; deba@861: deba@861: if (node_data[n].prev == node_data[n].next && deba@861: node_data[n].inverted) { deba@861: d = !d; deba@861: } deba@861: deba@861: // Add new arc deba@861: { deba@861: Arc arc = embed_arc[node]; deba@861: Arc re = node_data[rn].first; deba@861: deba@861: arc_lists[arc_lists[re].next].prev = arc; deba@861: arc_lists[arc].next = arc_lists[re].next; deba@861: arc_lists[arc].prev = re; deba@861: arc_lists[re].next = arc; deba@861: deba@861: if (!rd) { deba@861: node_data[rn].first = arc; deba@861: } deba@861: deba@861: Arc rev = _graph.oppositeArc(arc); deba@861: Arc e = node_data[n].first; deba@861: deba@861: arc_lists[arc_lists[e].next].prev = rev; deba@861: arc_lists[rev].next = arc_lists[e].next; deba@861: arc_lists[rev].prev = e; deba@861: arc_lists[e].next = rev; deba@861: deba@861: if (d) { deba@861: node_data[n].first = rev; deba@861: } deba@861: deba@861: } deba@861: deba@861: // Embedding arc into external face deba@861: if (rd) node_data[rn].next = n; else node_data[rn].prev = n; deba@861: if (d) node_data[n].prev = rn; else node_data[n].next = rn; deba@861: pn = rn; deba@861: deba@861: embed_arc[order_list[n]] = INVALID; deba@861: } deba@861: deba@861: if (!merge_roots[node].empty()) { deba@861: deba@861: bool d = pn == node_data[n].prev; deba@861: if (node_data[n].prev == node_data[n].next && deba@861: node_data[n].inverted) { deba@861: d = !d; deba@861: } deba@861: deba@861: merge_stack.push_back(std::make_pair(n, d)); deba@861: deba@861: int rn = merge_roots[node].front(); deba@861: deba@861: int xn = node_data[rn].next; deba@861: Node xnode = order_list[xn]; deba@861: deba@861: int yn = node_data[rn].prev; deba@861: Node ynode = order_list[yn]; deba@861: deba@861: bool rd; deba@861: if (!external(xnode, rorder, child_lists, ancestor_map, low_map)) { deba@861: rd = true; deba@861: } else if (!external(ynode, rorder, child_lists, deba@861: ancestor_map, low_map)) { deba@861: rd = false; deba@861: } else if (pertinent(xnode, embed_arc, merge_roots)) { deba@861: rd = true; deba@861: } else { deba@861: rd = false; deba@861: } deba@861: deba@861: merge_stack.push_back(std::make_pair(rn, rd)); deba@861: deba@861: pn = rn; deba@861: n = rd ? xn : yn; deba@861: deba@861: } else if (!external(node, rorder, child_lists, deba@861: ancestor_map, low_map)) { deba@861: int nn = (node_data[n].next != pn ? deba@861: node_data[n].next : node_data[n].prev); deba@861: deba@861: bool nd = n == node_data[nn].prev; deba@861: deba@861: if (nd) node_data[nn].prev = pn; deba@861: else node_data[nn].next = pn; deba@861: deba@861: if (n == node_data[pn].prev) node_data[pn].prev = nn; deba@861: else node_data[pn].next = nn; deba@861: deba@861: node_data[nn].inverted = deba@861: (node_data[nn].prev == node_data[nn].next && nd != rd); deba@861: deba@861: n = nn; deba@861: } deba@861: else break; deba@861: deba@861: } deba@861: deba@861: if (!merge_stack.empty() || n == rn) { deba@861: break; deba@861: } deba@861: } deba@861: } deba@861: deba@861: void initFace(const Node& node, ArcLists& arc_lists, deba@861: NodeData& node_data, const PredMap& pred_map, deba@861: const OrderMap& order_map, const OrderList& order_list) { deba@861: int n = order_map[node]; deba@861: int rn = n + order_list.size(); deba@861: deba@861: node_data[n].next = node_data[n].prev = rn; deba@861: node_data[rn].next = node_data[rn].prev = n; deba@861: deba@861: node_data[n].visited = order_list.size(); deba@861: node_data[rn].visited = order_list.size(); deba@861: deba@861: node_data[n].inverted = false; deba@861: node_data[rn].inverted = false; deba@861: deba@861: Arc arc = pred_map[node]; deba@861: Arc rev = _graph.oppositeArc(arc); deba@861: deba@861: node_data[rn].first = arc; deba@861: node_data[n].first = rev; deba@861: deba@861: arc_lists[arc].prev = arc; deba@861: arc_lists[arc].next = arc; deba@861: deba@861: arc_lists[rev].prev = rev; deba@861: arc_lists[rev].next = rev; deba@861: deba@861: } deba@861: deba@861: void mergeRemainingFaces(const Node& node, NodeData& node_data, deba@861: OrderList& order_list, OrderMap& order_map, deba@861: ChildLists& child_lists, ArcLists& arc_lists) { deba@861: while (child_lists[node].first != INVALID) { deba@861: int dd = order_map[node]; deba@861: Node child = child_lists[node].first; deba@861: int cd = order_map[child] + order_list.size(); deba@861: child_lists[node].first = child_lists[child].next; deba@861: deba@861: Arc de = node_data[dd].first; deba@861: Arc ce = node_data[cd].first; deba@861: deba@861: if (de != INVALID) { deba@861: Arc dne = arc_lists[de].next; deba@861: Arc cne = arc_lists[ce].next; deba@861: deba@861: arc_lists[de].next = cne; deba@861: arc_lists[ce].next = dne; deba@861: deba@861: arc_lists[dne].prev = ce; deba@861: arc_lists[cne].prev = de; deba@861: } deba@861: deba@861: node_data[dd].first = ce; deba@861: deba@861: } deba@861: } deba@861: deba@861: void storeEmbedding(const Node& node, NodeData& node_data, deba@861: OrderMap& order_map, PredMap& pred_map, deba@861: ArcLists& arc_lists, FlipMap& flip_map) { deba@861: deba@861: if (node_data[order_map[node]].first == INVALID) return; deba@861: deba@861: if (pred_map[node] != INVALID) { deba@861: Node source = _graph.source(pred_map[node]); deba@861: flip_map[node] = flip_map[node] != flip_map[source]; deba@861: } deba@861: deba@861: Arc first = node_data[order_map[node]].first; deba@861: Arc prev = first; deba@861: deba@861: Arc arc = flip_map[node] ? deba@861: arc_lists[prev].prev : arc_lists[prev].next; deba@861: deba@861: _embedding[prev] = arc; deba@861: deba@861: while (arc != first) { deba@861: Arc next = arc_lists[arc].prev == prev ? deba@861: arc_lists[arc].next : arc_lists[arc].prev; deba@861: prev = arc; arc = next; deba@861: _embedding[prev] = arc; deba@861: } deba@861: } deba@861: deba@861: deba@861: bool external(const Node& node, int rorder, deba@861: ChildLists& child_lists, AncestorMap& ancestor_map, deba@861: LowMap& low_map) { deba@861: Node child = child_lists[node].first; deba@861: deba@861: if (child != INVALID) { deba@861: if (low_map[child] < rorder) return true; deba@861: } deba@861: deba@861: if (ancestor_map[node] < rorder) return true; deba@861: deba@861: return false; deba@861: } deba@861: deba@861: bool pertinent(const Node& node, const EmbedArc& embed_arc, deba@861: const MergeRoots& merge_roots) { deba@861: return !merge_roots[node].empty() || embed_arc[node] != INVALID; deba@861: } deba@861: deba@861: int lowPoint(const Node& node, OrderMap& order_map, ChildLists& child_lists, deba@861: AncestorMap& ancestor_map, LowMap& low_map) { deba@861: int low_point; deba@861: deba@861: Node child = child_lists[node].first; deba@861: deba@861: if (child != INVALID) { deba@861: low_point = low_map[child]; deba@861: } else { deba@861: low_point = order_map[node]; deba@861: } deba@861: deba@861: if (low_point > ancestor_map[node]) { deba@861: low_point = ancestor_map[node]; deba@861: } deba@861: deba@861: return low_point; deba@861: } deba@861: deba@861: int findComponentRoot(Node root, Node node, ChildLists& child_lists, deba@861: OrderMap& order_map, OrderList& order_list) { deba@861: deba@861: int order = order_map[root]; deba@861: int norder = order_map[node]; deba@861: deba@861: Node child = child_lists[root].first; deba@861: while (child != INVALID) { deba@861: int corder = order_map[child]; deba@861: if (corder > order && corder < norder) { deba@861: order = corder; deba@861: } deba@861: child = child_lists[child].next; deba@861: } deba@861: return order + order_list.size(); deba@861: } deba@861: deba@861: Node findPertinent(Node node, OrderMap& order_map, NodeData& node_data, deba@861: EmbedArc& embed_arc, MergeRoots& merge_roots) { deba@861: Node wnode =_graph.target(node_data[order_map[node]].first); deba@861: while (!pertinent(wnode, embed_arc, merge_roots)) { deba@861: wnode = _graph.target(node_data[order_map[wnode]].first); deba@861: } deba@861: return wnode; deba@861: } deba@861: deba@861: deba@861: Node findExternal(Node node, int rorder, OrderMap& order_map, deba@861: ChildLists& child_lists, AncestorMap& ancestor_map, deba@861: LowMap& low_map, NodeData& node_data) { deba@861: Node wnode =_graph.target(node_data[order_map[node]].first); deba@861: while (!external(wnode, rorder, child_lists, ancestor_map, low_map)) { deba@861: wnode = _graph.target(node_data[order_map[wnode]].first); deba@861: } deba@861: return wnode; deba@861: } deba@861: deba@861: void markCommonPath(Node node, int rorder, Node& wnode, Node& znode, deba@861: OrderList& order_list, OrderMap& order_map, deba@861: NodeData& node_data, ArcLists& arc_lists, deba@861: EmbedArc& embed_arc, MergeRoots& merge_roots, deba@861: ChildLists& child_lists, AncestorMap& ancestor_map, deba@861: LowMap& low_map) { deba@861: deba@861: Node cnode = node; deba@861: Node pred = INVALID; deba@861: deba@861: while (true) { deba@861: deba@861: bool pert = pertinent(cnode, embed_arc, merge_roots); deba@861: bool ext = external(cnode, rorder, child_lists, ancestor_map, low_map); deba@861: deba@861: if (pert && ext) { deba@861: if (!merge_roots[cnode].empty()) { deba@861: int cn = merge_roots[cnode].back(); deba@861: deba@861: if (low_map[order_list[cn - order_list.size()]] < rorder) { deba@861: Arc arc = node_data[cn].first; deba@861: _kuratowski.set(arc, true); deba@861: deba@861: pred = cnode; deba@861: cnode = _graph.target(arc); deba@861: deba@861: continue; deba@861: } deba@861: } deba@861: wnode = znode = cnode; deba@861: return; deba@861: deba@861: } else if (pert) { deba@861: wnode = cnode; deba@861: deba@861: while (!external(cnode, rorder, child_lists, ancestor_map, low_map)) { deba@861: Arc arc = node_data[order_map[cnode]].first; deba@861: deba@861: if (_graph.target(arc) == pred) { deba@861: arc = arc_lists[arc].next; deba@861: } deba@861: _kuratowski.set(arc, true); deba@861: deba@861: Node next = _graph.target(arc); deba@861: pred = cnode; cnode = next; deba@861: } deba@861: deba@861: znode = cnode; deba@861: return; deba@861: deba@861: } else if (ext) { deba@861: znode = cnode; deba@861: deba@861: while (!pertinent(cnode, embed_arc, merge_roots)) { deba@861: Arc arc = node_data[order_map[cnode]].first; deba@861: deba@861: if (_graph.target(arc) == pred) { deba@861: arc = arc_lists[arc].next; deba@861: } deba@861: _kuratowski.set(arc, true); deba@861: deba@861: Node next = _graph.target(arc); deba@861: pred = cnode; cnode = next; deba@861: } deba@861: deba@861: wnode = cnode; deba@861: return; deba@861: deba@861: } else { deba@861: Arc arc = node_data[order_map[cnode]].first; deba@861: deba@861: if (_graph.target(arc) == pred) { deba@861: arc = arc_lists[arc].next; deba@861: } deba@861: _kuratowski.set(arc, true); deba@861: deba@861: Node next = _graph.target(arc); deba@861: pred = cnode; cnode = next; deba@861: } deba@861: deba@861: } deba@861: deba@861: } deba@861: deba@861: void orientComponent(Node root, int rn, OrderMap& order_map, deba@861: PredMap& pred_map, NodeData& node_data, deba@861: ArcLists& arc_lists, FlipMap& flip_map, deba@861: TypeMap& type_map) { deba@861: node_data[order_map[root]].first = node_data[rn].first; deba@861: type_map[root] = 1; deba@861: deba@861: std::vector st, qu; deba@861: deba@861: st.push_back(root); deba@861: while (!st.empty()) { deba@861: Node node = st.back(); deba@861: st.pop_back(); deba@861: qu.push_back(node); deba@861: deba@861: Arc arc = node_data[order_map[node]].first; deba@861: deba@861: if (type_map[_graph.target(arc)] == 0) { deba@861: st.push_back(_graph.target(arc)); deba@861: type_map[_graph.target(arc)] = 1; deba@861: } deba@861: deba@861: Arc last = arc, pred = arc; deba@861: arc = arc_lists[arc].next; deba@861: while (arc != last) { deba@861: deba@861: if (type_map[_graph.target(arc)] == 0) { deba@861: st.push_back(_graph.target(arc)); deba@861: type_map[_graph.target(arc)] = 1; deba@861: } deba@861: deba@861: Arc next = arc_lists[arc].next != pred ? deba@861: arc_lists[arc].next : arc_lists[arc].prev; deba@861: pred = arc; arc = next; deba@861: } deba@861: deba@861: } deba@861: deba@861: type_map[root] = 2; deba@861: flip_map[root] = false; deba@861: deba@861: for (int i = 1; i < int(qu.size()); ++i) { deba@861: deba@861: Node node = qu[i]; deba@861: deba@861: while (type_map[node] != 2) { deba@861: st.push_back(node); deba@861: type_map[node] = 2; deba@861: node = _graph.source(pred_map[node]); deba@861: } deba@861: deba@861: bool flip = flip_map[node]; deba@861: deba@861: while (!st.empty()) { deba@861: node = st.back(); deba@861: st.pop_back(); deba@861: deba@861: flip_map[node] = flip != flip_map[node]; deba@861: flip = flip_map[node]; deba@861: deba@861: if (flip) { deba@861: Arc arc = node_data[order_map[node]].first; deba@861: std::swap(arc_lists[arc].prev, arc_lists[arc].next); deba@861: arc = arc_lists[arc].prev; deba@861: std::swap(arc_lists[arc].prev, arc_lists[arc].next); deba@861: node_data[order_map[node]].first = arc; deba@861: } deba@861: } deba@861: } deba@861: deba@861: for (int i = 0; i < int(qu.size()); ++i) { deba@861: deba@861: Arc arc = node_data[order_map[qu[i]]].first; deba@861: Arc last = arc, pred = arc; deba@861: deba@861: arc = arc_lists[arc].next; deba@861: while (arc != last) { deba@861: deba@861: if (arc_lists[arc].next == pred) { deba@861: std::swap(arc_lists[arc].next, arc_lists[arc].prev); deba@861: } deba@861: pred = arc; arc = arc_lists[arc].next; deba@861: } deba@861: deba@861: } deba@861: } deba@861: deba@861: void setFaceFlags(Node root, Node wnode, Node ynode, Node xnode, deba@861: OrderMap& order_map, NodeData& node_data, deba@861: TypeMap& type_map) { deba@861: Node node = _graph.target(node_data[order_map[root]].first); deba@861: deba@861: while (node != ynode) { deba@861: type_map[node] = HIGHY; deba@861: node = _graph.target(node_data[order_map[node]].first); deba@861: } deba@861: deba@861: while (node != wnode) { deba@861: type_map[node] = LOWY; deba@861: node = _graph.target(node_data[order_map[node]].first); deba@861: } deba@861: deba@861: node = _graph.target(node_data[order_map[wnode]].first); deba@861: deba@861: while (node != xnode) { deba@861: type_map[node] = LOWX; deba@861: node = _graph.target(node_data[order_map[node]].first); deba@861: } deba@861: type_map[node] = LOWX; deba@861: deba@861: node = _graph.target(node_data[order_map[xnode]].first); deba@861: while (node != root) { deba@861: type_map[node] = HIGHX; deba@861: node = _graph.target(node_data[order_map[node]].first); deba@861: } deba@861: deba@861: type_map[wnode] = PERTINENT; deba@861: type_map[root] = ROOT; deba@861: } deba@861: deba@861: void findInternalPath(std::vector& ipath, deba@861: Node wnode, Node root, TypeMap& type_map, deba@861: OrderMap& order_map, NodeData& node_data, deba@861: ArcLists& arc_lists) { deba@861: std::vector st; deba@861: deba@861: Node node = wnode; deba@861: deba@861: while (node != root) { deba@861: Arc arc = arc_lists[node_data[order_map[node]].first].next; deba@861: st.push_back(arc); deba@861: node = _graph.target(arc); deba@861: } deba@861: deba@861: while (true) { deba@861: Arc arc = st.back(); deba@861: if (type_map[_graph.target(arc)] == LOWX || deba@861: type_map[_graph.target(arc)] == HIGHX) { deba@861: break; deba@861: } deba@861: if (type_map[_graph.target(arc)] == 2) { deba@861: type_map[_graph.target(arc)] = 3; deba@861: deba@861: arc = arc_lists[_graph.oppositeArc(arc)].next; deba@861: st.push_back(arc); deba@861: } else { deba@861: st.pop_back(); deba@861: arc = arc_lists[arc].next; deba@861: deba@861: while (_graph.oppositeArc(arc) == st.back()) { deba@861: arc = st.back(); deba@861: st.pop_back(); deba@861: arc = arc_lists[arc].next; deba@861: } deba@861: st.push_back(arc); deba@861: } deba@861: } deba@861: deba@861: for (int i = 0; i < int(st.size()); ++i) { deba@861: if (type_map[_graph.target(st[i])] != LOWY && deba@861: type_map[_graph.target(st[i])] != HIGHY) { deba@861: for (; i < int(st.size()); ++i) { deba@861: ipath.push_back(st[i]); deba@861: } deba@861: } deba@861: } deba@861: } deba@861: deba@861: void setInternalFlags(std::vector& ipath, TypeMap& type_map) { deba@861: for (int i = 1; i < int(ipath.size()); ++i) { deba@861: type_map[_graph.source(ipath[i])] = INTERNAL; deba@861: } deba@861: } deba@861: deba@861: void findPilePath(std::vector& ppath, deba@861: Node root, TypeMap& type_map, OrderMap& order_map, deba@861: NodeData& node_data, ArcLists& arc_lists) { deba@861: std::vector st; deba@861: deba@861: st.push_back(_graph.oppositeArc(node_data[order_map[root]].first)); deba@861: st.push_back(node_data[order_map[root]].first); deba@861: deba@861: while (st.size() > 1) { deba@861: Arc arc = st.back(); deba@861: if (type_map[_graph.target(arc)] == INTERNAL) { deba@861: break; deba@861: } deba@861: if (type_map[_graph.target(arc)] == 3) { deba@861: type_map[_graph.target(arc)] = 4; deba@861: deba@861: arc = arc_lists[_graph.oppositeArc(arc)].next; deba@861: st.push_back(arc); deba@861: } else { deba@861: st.pop_back(); deba@861: arc = arc_lists[arc].next; deba@861: deba@861: while (!st.empty() && _graph.oppositeArc(arc) == st.back()) { deba@861: arc = st.back(); deba@861: st.pop_back(); deba@861: arc = arc_lists[arc].next; deba@861: } deba@861: st.push_back(arc); deba@861: } deba@861: } deba@861: deba@861: for (int i = 1; i < int(st.size()); ++i) { deba@861: ppath.push_back(st[i]); deba@861: } deba@861: } deba@861: deba@861: deba@861: int markExternalPath(Node node, OrderMap& order_map, deba@861: ChildLists& child_lists, PredMap& pred_map, deba@861: AncestorMap& ancestor_map, LowMap& low_map) { deba@861: int lp = lowPoint(node, order_map, child_lists, deba@861: ancestor_map, low_map); deba@861: deba@861: if (ancestor_map[node] != lp) { deba@861: node = child_lists[node].first; deba@861: _kuratowski[pred_map[node]] = true; deba@861: deba@861: while (ancestor_map[node] != lp) { deba@861: for (OutArcIt e(_graph, node); e != INVALID; ++e) { deba@861: Node tnode = _graph.target(e); deba@861: if (order_map[tnode] > order_map[node] && low_map[tnode] == lp) { deba@861: node = tnode; deba@861: _kuratowski[e] = true; deba@861: break; deba@861: } deba@861: } deba@861: } deba@861: } deba@861: deba@861: for (OutArcIt e(_graph, node); e != INVALID; ++e) { deba@861: if (order_map[_graph.target(e)] == lp) { deba@861: _kuratowski[e] = true; deba@861: break; deba@861: } deba@861: } deba@861: deba@861: return lp; deba@861: } deba@861: deba@861: void markPertinentPath(Node node, OrderMap& order_map, deba@861: NodeData& node_data, ArcLists& arc_lists, deba@861: EmbedArc& embed_arc, MergeRoots& merge_roots) { deba@861: while (embed_arc[node] == INVALID) { deba@861: int n = merge_roots[node].front(); deba@861: Arc arc = node_data[n].first; deba@861: deba@861: _kuratowski.set(arc, true); deba@861: deba@861: Node pred = node; deba@861: node = _graph.target(arc); deba@861: while (!pertinent(node, embed_arc, merge_roots)) { deba@861: arc = node_data[order_map[node]].first; deba@861: if (_graph.target(arc) == pred) { deba@861: arc = arc_lists[arc].next; deba@861: } deba@861: _kuratowski.set(arc, true); deba@861: pred = node; deba@861: node = _graph.target(arc); deba@861: } deba@861: } deba@861: _kuratowski.set(embed_arc[node], true); deba@861: } deba@861: deba@861: void markPredPath(Node node, Node snode, PredMap& pred_map) { deba@861: while (node != snode) { deba@861: _kuratowski.set(pred_map[node], true); deba@861: node = _graph.source(pred_map[node]); deba@861: } deba@861: } deba@861: deba@861: void markFacePath(Node ynode, Node xnode, deba@861: OrderMap& order_map, NodeData& node_data) { deba@861: Arc arc = node_data[order_map[ynode]].first; deba@861: Node node = _graph.target(arc); deba@861: _kuratowski.set(arc, true); deba@861: deba@861: while (node != xnode) { deba@861: arc = node_data[order_map[node]].first; deba@861: _kuratowski.set(arc, true); deba@861: node = _graph.target(arc); deba@861: } deba@861: } deba@861: deba@861: void markInternalPath(std::vector& path) { deba@861: for (int i = 0; i < int(path.size()); ++i) { deba@861: _kuratowski.set(path[i], true); deba@861: } deba@861: } deba@861: deba@861: void markPilePath(std::vector& path) { deba@861: for (int i = 0; i < int(path.size()); ++i) { deba@861: _kuratowski.set(path[i], true); deba@861: } deba@861: } deba@861: deba@861: void isolateKuratowski(Arc arc, NodeData& node_data, deba@861: ArcLists& arc_lists, FlipMap& flip_map, deba@861: OrderMap& order_map, OrderList& order_list, deba@861: PredMap& pred_map, ChildLists& child_lists, deba@861: AncestorMap& ancestor_map, LowMap& low_map, deba@861: EmbedArc& embed_arc, MergeRoots& merge_roots) { deba@861: deba@861: Node root = _graph.source(arc); deba@861: Node enode = _graph.target(arc); deba@861: deba@861: int rorder = order_map[root]; deba@861: deba@861: TypeMap type_map(_graph, 0); deba@861: deba@861: int rn = findComponentRoot(root, enode, child_lists, deba@861: order_map, order_list); deba@861: deba@861: Node xnode = order_list[node_data[rn].next]; deba@861: Node ynode = order_list[node_data[rn].prev]; deba@861: deba@861: // Minor-A deba@861: { deba@861: while (!merge_roots[xnode].empty() || !merge_roots[ynode].empty()) { deba@861: deba@861: if (!merge_roots[xnode].empty()) { deba@861: root = xnode; deba@861: rn = merge_roots[xnode].front(); deba@861: } else { deba@861: root = ynode; deba@861: rn = merge_roots[ynode].front(); deba@861: } deba@861: deba@861: xnode = order_list[node_data[rn].next]; deba@861: ynode = order_list[node_data[rn].prev]; deba@861: } deba@861: deba@861: if (root != _graph.source(arc)) { deba@861: orientComponent(root, rn, order_map, pred_map, deba@861: node_data, arc_lists, flip_map, type_map); deba@861: markFacePath(root, root, order_map, node_data); deba@861: int xlp = markExternalPath(xnode, order_map, child_lists, deba@861: pred_map, ancestor_map, low_map); deba@861: int ylp = markExternalPath(ynode, order_map, child_lists, deba@861: pred_map, ancestor_map, low_map); deba@861: markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map); deba@861: Node lwnode = findPertinent(ynode, order_map, node_data, deba@861: embed_arc, merge_roots); deba@861: deba@861: markPertinentPath(lwnode, order_map, node_data, arc_lists, deba@861: embed_arc, merge_roots); deba@861: deba@861: return; deba@861: } deba@861: } deba@861: deba@861: orientComponent(root, rn, order_map, pred_map, deba@861: node_data, arc_lists, flip_map, type_map); deba@861: deba@861: Node wnode = findPertinent(ynode, order_map, node_data, deba@861: embed_arc, merge_roots); deba@861: setFaceFlags(root, wnode, ynode, xnode, order_map, node_data, type_map); deba@861: deba@861: deba@861: //Minor-B deba@861: if (!merge_roots[wnode].empty()) { deba@861: int cn = merge_roots[wnode].back(); deba@861: Node rep = order_list[cn - order_list.size()]; deba@861: if (low_map[rep] < rorder) { deba@861: markFacePath(root, root, order_map, node_data); deba@861: int xlp = markExternalPath(xnode, order_map, child_lists, deba@861: pred_map, ancestor_map, low_map); deba@861: int ylp = markExternalPath(ynode, order_map, child_lists, deba@861: pred_map, ancestor_map, low_map); deba@861: deba@861: Node lwnode, lznode; deba@861: markCommonPath(wnode, rorder, lwnode, lznode, order_list, deba@861: order_map, node_data, arc_lists, embed_arc, deba@861: merge_roots, child_lists, ancestor_map, low_map); deba@861: deba@861: markPertinentPath(lwnode, order_map, node_data, arc_lists, deba@861: embed_arc, merge_roots); deba@861: int zlp = markExternalPath(lznode, order_map, child_lists, deba@861: pred_map, ancestor_map, low_map); deba@861: deba@861: int minlp = xlp < ylp ? xlp : ylp; deba@861: if (zlp < minlp) minlp = zlp; deba@861: deba@861: int maxlp = xlp > ylp ? xlp : ylp; deba@861: if (zlp > maxlp) maxlp = zlp; deba@861: deba@861: markPredPath(order_list[maxlp], order_list[minlp], pred_map); deba@861: deba@861: return; deba@861: } deba@861: } deba@861: deba@861: Node pxnode, pynode; deba@861: std::vector ipath; deba@861: findInternalPath(ipath, wnode, root, type_map, order_map, deba@861: node_data, arc_lists); deba@861: setInternalFlags(ipath, type_map); deba@861: pynode = _graph.source(ipath.front()); deba@861: pxnode = _graph.target(ipath.back()); deba@861: deba@861: wnode = findPertinent(pynode, order_map, node_data, deba@861: embed_arc, merge_roots); deba@861: deba@861: // Minor-C deba@861: { deba@861: if (type_map[_graph.source(ipath.front())] == HIGHY) { deba@861: if (type_map[_graph.target(ipath.back())] == HIGHX) { deba@861: markFacePath(xnode, pxnode, order_map, node_data); deba@861: } deba@861: markFacePath(root, xnode, order_map, node_data); deba@861: markPertinentPath(wnode, order_map, node_data, arc_lists, deba@861: embed_arc, merge_roots); deba@861: markInternalPath(ipath); deba@861: int xlp = markExternalPath(xnode, order_map, child_lists, deba@861: pred_map, ancestor_map, low_map); deba@861: int ylp = markExternalPath(ynode, order_map, child_lists, deba@861: pred_map, ancestor_map, low_map); deba@861: markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map); deba@861: return; deba@861: } deba@861: deba@861: if (type_map[_graph.target(ipath.back())] == HIGHX) { deba@861: markFacePath(ynode, root, order_map, node_data); deba@861: markPertinentPath(wnode, order_map, node_data, arc_lists, deba@861: embed_arc, merge_roots); deba@861: markInternalPath(ipath); deba@861: int xlp = markExternalPath(xnode, order_map, child_lists, deba@861: pred_map, ancestor_map, low_map); deba@861: int ylp = markExternalPath(ynode, order_map, child_lists, deba@861: pred_map, ancestor_map, low_map); deba@861: markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map); deba@861: return; deba@861: } deba@861: } deba@861: deba@861: std::vector ppath; deba@861: findPilePath(ppath, root, type_map, order_map, node_data, arc_lists); deba@861: deba@861: // Minor-D deba@861: if (!ppath.empty()) { deba@861: markFacePath(ynode, xnode, order_map, node_data); deba@861: markPertinentPath(wnode, order_map, node_data, arc_lists, deba@861: embed_arc, merge_roots); deba@861: markPilePath(ppath); deba@861: markInternalPath(ipath); deba@861: int xlp = markExternalPath(xnode, order_map, child_lists, deba@861: pred_map, ancestor_map, low_map); deba@861: int ylp = markExternalPath(ynode, order_map, child_lists, deba@861: pred_map, ancestor_map, low_map); deba@861: markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map); deba@861: return; deba@861: } deba@861: deba@861: // Minor-E* deba@861: { deba@861: deba@861: if (!external(wnode, rorder, child_lists, ancestor_map, low_map)) { deba@861: Node znode = findExternal(pynode, rorder, order_map, deba@861: child_lists, ancestor_map, deba@861: low_map, node_data); deba@861: deba@861: if (type_map[znode] == LOWY) { deba@861: markFacePath(root, xnode, order_map, node_data); deba@861: markPertinentPath(wnode, order_map, node_data, arc_lists, deba@861: embed_arc, merge_roots); deba@861: markInternalPath(ipath); deba@861: int xlp = markExternalPath(xnode, order_map, child_lists, deba@861: pred_map, ancestor_map, low_map); deba@861: int zlp = markExternalPath(znode, order_map, child_lists, deba@861: pred_map, ancestor_map, low_map); deba@861: markPredPath(root, order_list[xlp < zlp ? xlp : zlp], pred_map); deba@861: } else { deba@861: markFacePath(ynode, root, order_map, node_data); deba@861: markPertinentPath(wnode, order_map, node_data, arc_lists, deba@861: embed_arc, merge_roots); deba@861: markInternalPath(ipath); deba@861: int ylp = markExternalPath(ynode, order_map, child_lists, deba@861: pred_map, ancestor_map, low_map); deba@861: int zlp = markExternalPath(znode, order_map, child_lists, deba@861: pred_map, ancestor_map, low_map); deba@861: markPredPath(root, order_list[ylp < zlp ? ylp : zlp], pred_map); deba@861: } deba@861: return; deba@861: } deba@861: deba@861: int xlp = markExternalPath(xnode, order_map, child_lists, deba@861: pred_map, ancestor_map, low_map); deba@861: int ylp = markExternalPath(ynode, order_map, child_lists, deba@861: pred_map, ancestor_map, low_map); deba@861: int wlp = markExternalPath(wnode, order_map, child_lists, deba@861: pred_map, ancestor_map, low_map); deba@861: deba@861: if (wlp > xlp && wlp > ylp) { deba@861: markFacePath(root, root, order_map, node_data); deba@861: markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map); deba@861: return; deba@861: } deba@861: deba@861: markInternalPath(ipath); deba@861: markPertinentPath(wnode, order_map, node_data, arc_lists, deba@861: embed_arc, merge_roots); deba@861: deba@861: if (xlp > ylp && xlp > wlp) { deba@861: markFacePath(root, pynode, order_map, node_data); deba@861: markFacePath(wnode, xnode, order_map, node_data); deba@861: markPredPath(root, order_list[ylp < wlp ? ylp : wlp], pred_map); deba@861: return; deba@861: } deba@861: deba@861: if (ylp > xlp && ylp > wlp) { deba@861: markFacePath(pxnode, root, order_map, node_data); deba@861: markFacePath(ynode, wnode, order_map, node_data); deba@861: markPredPath(root, order_list[xlp < wlp ? xlp : wlp], pred_map); deba@861: return; deba@861: } deba@861: deba@861: if (pynode != ynode) { deba@861: markFacePath(pxnode, wnode, order_map, node_data); deba@861: deba@861: int minlp = xlp < ylp ? xlp : ylp; deba@861: if (wlp < minlp) minlp = wlp; deba@861: deba@861: int maxlp = xlp > ylp ? xlp : ylp; deba@861: if (wlp > maxlp) maxlp = wlp; deba@861: deba@861: markPredPath(order_list[maxlp], order_list[minlp], pred_map); deba@861: return; deba@861: } deba@861: deba@861: if (pxnode != xnode) { deba@861: markFacePath(wnode, pynode, order_map, node_data); deba@861: deba@861: int minlp = xlp < ylp ? xlp : ylp; deba@861: if (wlp < minlp) minlp = wlp; deba@861: deba@861: int maxlp = xlp > ylp ? xlp : ylp; deba@861: if (wlp > maxlp) maxlp = wlp; deba@861: deba@861: markPredPath(order_list[maxlp], order_list[minlp], pred_map); deba@861: return; deba@861: } deba@861: deba@861: markFacePath(root, root, order_map, node_data); deba@861: int minlp = xlp < ylp ? xlp : ylp; deba@861: if (wlp < minlp) minlp = wlp; deba@861: markPredPath(root, order_list[minlp], pred_map); deba@861: return; deba@861: } deba@861: deba@861: } deba@861: deba@861: }; deba@861: deba@861: namespace _planarity_bits { deba@861: deba@861: template deba@861: void makeConnected(Graph& graph, EmbeddingMap& embedding) { deba@861: DfsVisitor null_visitor; deba@861: DfsVisit > dfs(graph, null_visitor); deba@861: dfs.init(); deba@861: deba@861: typename Graph::Node u = INVALID; deba@861: for (typename Graph::NodeIt n(graph); n != INVALID; ++n) { deba@861: if (!dfs.reached(n)) { deba@861: dfs.addSource(n); deba@861: dfs.start(); deba@861: if (u == INVALID) { deba@861: u = n; deba@861: } else { deba@861: typename Graph::Node v = n; deba@861: deba@861: typename Graph::Arc ue = typename Graph::OutArcIt(graph, u); deba@861: typename Graph::Arc ve = typename Graph::OutArcIt(graph, v); deba@861: deba@861: typename Graph::Arc e = graph.direct(graph.addEdge(u, v), true); deba@861: deba@861: if (ue != INVALID) { deba@861: embedding[e] = embedding[ue]; deba@861: embedding[ue] = e; deba@861: } else { deba@861: embedding[e] = e; deba@861: } deba@861: deba@861: if (ve != INVALID) { deba@861: embedding[graph.oppositeArc(e)] = embedding[ve]; deba@861: embedding[ve] = graph.oppositeArc(e); deba@861: } else { deba@861: embedding[graph.oppositeArc(e)] = graph.oppositeArc(e); deba@861: } deba@861: } deba@861: } deba@861: } deba@861: } deba@861: deba@861: template deba@861: void makeBiNodeConnected(Graph& graph, EmbeddingMap& embedding) { deba@861: typename Graph::template ArcMap processed(graph); deba@861: deba@861: std::vector arcs; deba@861: for (typename Graph::ArcIt e(graph); e != INVALID; ++e) { deba@861: arcs.push_back(e); deba@861: } deba@861: deba@861: IterableBoolMap visited(graph, false); deba@861: deba@861: for (int i = 0; i < int(arcs.size()); ++i) { deba@861: typename Graph::Arc pp = arcs[i]; deba@861: if (processed[pp]) continue; deba@861: deba@861: typename Graph::Arc e = embedding[graph.oppositeArc(pp)]; deba@861: processed[e] = true; deba@861: visited.set(graph.source(e), true); deba@861: deba@861: typename Graph::Arc p = e, l = e; deba@861: e = embedding[graph.oppositeArc(e)]; deba@861: deba@861: while (e != l) { deba@861: processed[e] = true; deba@861: deba@861: if (visited[graph.source(e)]) { deba@861: deba@861: typename Graph::Arc n = deba@861: graph.direct(graph.addEdge(graph.source(p), deba@861: graph.target(e)), true); deba@861: embedding[n] = p; deba@861: embedding[graph.oppositeArc(pp)] = n; deba@861: deba@861: embedding[graph.oppositeArc(n)] = deba@861: embedding[graph.oppositeArc(e)]; deba@861: embedding[graph.oppositeArc(e)] = deba@861: graph.oppositeArc(n); deba@861: deba@861: p = n; deba@861: e = embedding[graph.oppositeArc(n)]; deba@861: } else { deba@861: visited.set(graph.source(e), true); deba@861: pp = p; deba@861: p = e; deba@861: e = embedding[graph.oppositeArc(e)]; deba@861: } deba@861: } deba@861: visited.setAll(false); deba@861: } deba@861: } deba@861: deba@861: deba@861: template deba@861: void makeMaxPlanar(Graph& graph, EmbeddingMap& embedding) { deba@861: deba@861: typename Graph::template NodeMap degree(graph); deba@861: deba@861: for (typename Graph::NodeIt n(graph); n != INVALID; ++n) { deba@861: degree[n] = countIncEdges(graph, n); deba@861: } deba@861: deba@861: typename Graph::template ArcMap processed(graph); deba@861: IterableBoolMap visited(graph, false); deba@861: deba@861: std::vector arcs; deba@861: for (typename Graph::ArcIt e(graph); e != INVALID; ++e) { deba@861: arcs.push_back(e); deba@861: } deba@861: deba@861: for (int i = 0; i < int(arcs.size()); ++i) { deba@861: typename Graph::Arc e = arcs[i]; deba@861: deba@861: if (processed[e]) continue; deba@861: processed[e] = true; deba@861: deba@861: typename Graph::Arc mine = e; deba@861: int mind = degree[graph.source(e)]; deba@861: deba@861: int face_size = 1; deba@861: deba@861: typename Graph::Arc l = e; deba@861: e = embedding[graph.oppositeArc(e)]; deba@861: while (l != e) { deba@861: processed[e] = true; deba@861: deba@861: ++face_size; deba@861: deba@861: if (degree[graph.source(e)] < mind) { deba@861: mine = e; deba@861: mind = degree[graph.source(e)]; deba@861: } deba@861: deba@861: e = embedding[graph.oppositeArc(e)]; deba@861: } deba@861: deba@861: if (face_size < 4) { deba@861: continue; deba@861: } deba@861: deba@861: typename Graph::Node s = graph.source(mine); deba@861: for (typename Graph::OutArcIt e(graph, s); e != INVALID; ++e) { deba@861: visited.set(graph.target(e), true); deba@861: } deba@861: deba@861: typename Graph::Arc oppe = INVALID; deba@861: deba@861: e = embedding[graph.oppositeArc(mine)]; deba@861: e = embedding[graph.oppositeArc(e)]; deba@861: while (graph.target(e) != s) { deba@861: if (visited[graph.source(e)]) { deba@861: oppe = e; deba@861: break; deba@861: } deba@861: e = embedding[graph.oppositeArc(e)]; deba@861: } deba@861: visited.setAll(false); deba@861: deba@861: if (oppe == INVALID) { deba@861: deba@861: e = embedding[graph.oppositeArc(mine)]; deba@861: typename Graph::Arc pn = mine, p = e; deba@861: deba@861: e = embedding[graph.oppositeArc(e)]; deba@861: while (graph.target(e) != s) { deba@861: typename Graph::Arc n = deba@861: graph.direct(graph.addEdge(s, graph.source(e)), true); deba@861: deba@861: embedding[n] = pn; deba@861: embedding[graph.oppositeArc(n)] = e; deba@861: embedding[graph.oppositeArc(p)] = graph.oppositeArc(n); deba@861: deba@861: pn = n; deba@861: deba@861: p = e; deba@861: e = embedding[graph.oppositeArc(e)]; deba@861: } deba@861: deba@861: embedding[graph.oppositeArc(e)] = pn; deba@861: deba@861: } else { deba@861: deba@861: mine = embedding[graph.oppositeArc(mine)]; deba@861: s = graph.source(mine); deba@861: oppe = embedding[graph.oppositeArc(oppe)]; deba@861: typename Graph::Node t = graph.source(oppe); deba@861: deba@861: typename Graph::Arc ce = graph.direct(graph.addEdge(s, t), true); deba@861: embedding[ce] = mine; deba@861: embedding[graph.oppositeArc(ce)] = oppe; deba@861: deba@861: typename Graph::Arc pn = ce, p = oppe; deba@861: e = embedding[graph.oppositeArc(oppe)]; deba@861: while (graph.target(e) != s) { deba@861: typename Graph::Arc n = deba@861: graph.direct(graph.addEdge(s, graph.source(e)), true); deba@861: deba@861: embedding[n] = pn; deba@861: embedding[graph.oppositeArc(n)] = e; deba@861: embedding[graph.oppositeArc(p)] = graph.oppositeArc(n); deba@861: deba@861: pn = n; deba@861: deba@861: p = e; deba@861: e = embedding[graph.oppositeArc(e)]; deba@861: deba@861: } deba@861: embedding[graph.oppositeArc(e)] = pn; deba@861: deba@861: pn = graph.oppositeArc(ce), p = mine; deba@861: e = embedding[graph.oppositeArc(mine)]; deba@861: while (graph.target(e) != t) { deba@861: typename Graph::Arc n = deba@861: graph.direct(graph.addEdge(t, graph.source(e)), true); deba@861: deba@861: embedding[n] = pn; deba@861: embedding[graph.oppositeArc(n)] = e; deba@861: embedding[graph.oppositeArc(p)] = graph.oppositeArc(n); deba@861: deba@861: pn = n; deba@861: deba@861: p = e; deba@861: e = embedding[graph.oppositeArc(e)]; deba@861: deba@861: } deba@861: embedding[graph.oppositeArc(e)] = pn; deba@861: } deba@861: } deba@861: } deba@861: deba@861: } deba@861: deba@861: /// \ingroup planar deba@861: /// deba@861: /// \brief Schnyder's planar drawing algorithm deba@861: /// deba@861: /// The planar drawing algorithm calculates positions for the nodes kpeter@896: /// in the plane. These coordinates satisfy that if the edges are kpeter@896: /// represented with straight lines, then they will not intersect deba@861: /// each other. deba@861: /// kpeter@896: /// Scnyder's algorithm embeds the graph on an \c (n-2)x(n-2) size grid, kpeter@896: /// i.e. each node will be located in the \c [0..n-2]x[0..n-2] square. deba@861: /// The time complexity of the algorithm is O(n). kpeter@896: /// kpeter@896: /// \see PlanarEmbedding deba@861: template deba@861: class PlanarDrawing { deba@861: public: deba@861: deba@861: TEMPLATE_GRAPH_TYPEDEFS(Graph); deba@861: kpeter@896: /// \brief The point type for storing coordinates deba@861: typedef dim2::Point Point; kpeter@896: /// \brief The map type for storing the coordinates of the nodes deba@861: typedef typename Graph::template NodeMap PointMap; deba@861: deba@861: deba@861: /// \brief Constructor deba@861: /// deba@861: /// Constructor kpeter@896: /// \pre The graph must be simple, i.e. it should not kpeter@896: /// contain parallel or loop arcs. deba@861: PlanarDrawing(const Graph& graph) deba@861: : _graph(graph), _point_map(graph) {} deba@861: deba@861: private: deba@861: deba@861: template deba@861: void drawing(const AuxGraph& graph, deba@861: const AuxEmbeddingMap& next, deba@861: PointMap& point_map) { deba@861: TEMPLATE_GRAPH_TYPEDEFS(AuxGraph); deba@861: deba@861: typename AuxGraph::template ArcMap prev(graph); deba@861: deba@861: for (NodeIt n(graph); n != INVALID; ++n) { deba@861: Arc e = OutArcIt(graph, n); deba@861: deba@861: Arc p = e, l = e; deba@861: deba@861: e = next[e]; deba@861: while (e != l) { deba@861: prev[e] = p; deba@861: p = e; deba@861: e = next[e]; deba@861: } deba@861: prev[e] = p; deba@861: } deba@861: deba@861: Node anode, bnode, cnode; deba@861: deba@861: { deba@861: Arc e = ArcIt(graph); deba@861: anode = graph.source(e); deba@861: bnode = graph.target(e); deba@861: cnode = graph.target(next[graph.oppositeArc(e)]); deba@861: } deba@861: deba@861: IterableBoolMap proper(graph, false); deba@861: typename AuxGraph::template NodeMap conn(graph, -1); deba@861: deba@861: conn[anode] = conn[bnode] = -2; deba@861: { deba@861: for (OutArcIt e(graph, anode); e != INVALID; ++e) { deba@861: Node m = graph.target(e); deba@861: if (conn[m] == -1) { deba@861: conn[m] = 1; deba@861: } deba@861: } deba@861: conn[cnode] = 2; deba@861: deba@861: for (OutArcIt e(graph, bnode); e != INVALID; ++e) { deba@861: Node m = graph.target(e); deba@861: if (conn[m] == -1) { deba@861: conn[m] = 1; deba@861: } else if (conn[m] != -2) { deba@861: conn[m] += 1; deba@861: Arc pe = graph.oppositeArc(e); deba@861: if (conn[graph.target(next[pe])] == -2) { deba@861: conn[m] -= 1; deba@861: } deba@861: if (conn[graph.target(prev[pe])] == -2) { deba@861: conn[m] -= 1; deba@861: } deba@861: deba@861: proper.set(m, conn[m] == 1); deba@861: } deba@861: } deba@861: } deba@861: deba@861: deba@861: typename AuxGraph::template ArcMap angle(graph, -1); deba@861: deba@861: while (proper.trueNum() != 0) { deba@861: Node n = typename IterableBoolMap::TrueIt(proper); deba@861: proper.set(n, false); deba@861: conn[n] = -2; deba@861: deba@861: for (OutArcIt e(graph, n); e != INVALID; ++e) { deba@861: Node m = graph.target(e); deba@861: if (conn[m] == -1) { deba@861: conn[m] = 1; deba@861: } else if (conn[m] != -2) { deba@861: conn[m] += 1; deba@861: Arc pe = graph.oppositeArc(e); deba@861: if (conn[graph.target(next[pe])] == -2) { deba@861: conn[m] -= 1; deba@861: } deba@861: if (conn[graph.target(prev[pe])] == -2) { deba@861: conn[m] -= 1; deba@861: } deba@861: deba@861: proper.set(m, conn[m] == 1); deba@861: } deba@861: } deba@861: deba@861: { deba@861: Arc e = OutArcIt(graph, n); deba@861: Arc p = e, l = e; deba@861: deba@861: e = next[e]; deba@861: while (e != l) { deba@861: deba@861: if (conn[graph.target(e)] == -2 && conn[graph.target(p)] == -2) { deba@861: Arc f = e; deba@861: angle[f] = 0; deba@861: f = next[graph.oppositeArc(f)]; deba@861: angle[f] = 1; deba@861: f = next[graph.oppositeArc(f)]; deba@861: angle[f] = 2; deba@861: } deba@861: deba@861: p = e; deba@861: e = next[e]; deba@861: } deba@861: deba@861: if (conn[graph.target(e)] == -2 && conn[graph.target(p)] == -2) { deba@861: Arc f = e; deba@861: angle[f] = 0; deba@861: f = next[graph.oppositeArc(f)]; deba@861: angle[f] = 1; deba@861: f = next[graph.oppositeArc(f)]; deba@861: angle[f] = 2; deba@861: } deba@861: } deba@861: } deba@861: deba@861: typename AuxGraph::template NodeMap apred(graph, INVALID); deba@861: typename AuxGraph::template NodeMap bpred(graph, INVALID); deba@861: typename AuxGraph::template NodeMap cpred(graph, INVALID); deba@861: deba@861: typename AuxGraph::template NodeMap apredid(graph, -1); deba@861: typename AuxGraph::template NodeMap bpredid(graph, -1); deba@861: typename AuxGraph::template NodeMap cpredid(graph, -1); deba@861: deba@861: for (ArcIt e(graph); e != INVALID; ++e) { deba@861: if (angle[e] == angle[next[e]]) { deba@861: switch (angle[e]) { deba@861: case 2: deba@861: apred[graph.target(e)] = graph.source(e); deba@861: apredid[graph.target(e)] = graph.id(graph.source(e)); deba@861: break; deba@861: case 1: deba@861: bpred[graph.target(e)] = graph.source(e); deba@861: bpredid[graph.target(e)] = graph.id(graph.source(e)); deba@861: break; deba@861: case 0: deba@861: cpred[graph.target(e)] = graph.source(e); deba@861: cpredid[graph.target(e)] = graph.id(graph.source(e)); deba@861: break; deba@861: } deba@861: } deba@861: } deba@861: deba@861: cpred[anode] = INVALID; deba@861: cpred[bnode] = INVALID; deba@861: deba@861: std::vector aorder, border, corder; deba@861: deba@861: { deba@861: typename AuxGraph::template NodeMap processed(graph, false); deba@861: std::vector st; deba@861: for (NodeIt n(graph); n != INVALID; ++n) { deba@861: if (!processed[n] && n != bnode && n != cnode) { deba@861: st.push_back(n); deba@861: processed[n] = true; deba@861: Node m = apred[n]; deba@861: while (m != INVALID && !processed[m]) { deba@861: st.push_back(m); deba@861: processed[m] = true; deba@861: m = apred[m]; deba@861: } deba@861: while (!st.empty()) { deba@861: aorder.push_back(st.back()); deba@861: st.pop_back(); deba@861: } deba@861: } deba@861: } deba@861: } deba@861: deba@861: { deba@861: typename AuxGraph::template NodeMap processed(graph, false); deba@861: std::vector st; deba@861: for (NodeIt n(graph); n != INVALID; ++n) { deba@861: if (!processed[n] && n != cnode && n != anode) { deba@861: st.push_back(n); deba@861: processed[n] = true; deba@861: Node m = bpred[n]; deba@861: while (m != INVALID && !processed[m]) { deba@861: st.push_back(m); deba@861: processed[m] = true; deba@861: m = bpred[m]; deba@861: } deba@861: while (!st.empty()) { deba@861: border.push_back(st.back()); deba@861: st.pop_back(); deba@861: } deba@861: } deba@861: } deba@861: } deba@861: deba@861: { deba@861: typename AuxGraph::template NodeMap processed(graph, false); deba@861: std::vector st; deba@861: for (NodeIt n(graph); n != INVALID; ++n) { deba@861: if (!processed[n] && n != anode && n != bnode) { deba@861: st.push_back(n); deba@861: processed[n] = true; deba@861: Node m = cpred[n]; deba@861: while (m != INVALID && !processed[m]) { deba@861: st.push_back(m); deba@861: processed[m] = true; deba@861: m = cpred[m]; deba@861: } deba@861: while (!st.empty()) { deba@861: corder.push_back(st.back()); deba@861: st.pop_back(); deba@861: } deba@861: } deba@861: } deba@861: } deba@861: deba@861: typename AuxGraph::template NodeMap atree(graph, 0); deba@861: for (int i = aorder.size() - 1; i >= 0; --i) { deba@861: Node n = aorder[i]; deba@861: atree[n] = 1; deba@861: for (OutArcIt e(graph, n); e != INVALID; ++e) { deba@861: if (apred[graph.target(e)] == n) { deba@861: atree[n] += atree[graph.target(e)]; deba@861: } deba@861: } deba@861: } deba@861: deba@861: typename AuxGraph::template NodeMap btree(graph, 0); deba@861: for (int i = border.size() - 1; i >= 0; --i) { deba@861: Node n = border[i]; deba@861: btree[n] = 1; deba@861: for (OutArcIt e(graph, n); e != INVALID; ++e) { deba@861: if (bpred[graph.target(e)] == n) { deba@861: btree[n] += btree[graph.target(e)]; deba@861: } deba@861: } deba@861: } deba@861: deba@861: typename AuxGraph::template NodeMap apath(graph, 0); deba@861: apath[bnode] = apath[cnode] = 1; deba@861: typename AuxGraph::template NodeMap apath_btree(graph, 0); deba@861: apath_btree[bnode] = btree[bnode]; deba@861: for (int i = 1; i < int(aorder.size()); ++i) { deba@861: Node n = aorder[i]; deba@861: apath[n] = apath[apred[n]] + 1; deba@861: apath_btree[n] = btree[n] + apath_btree[apred[n]]; deba@861: } deba@861: deba@861: typename AuxGraph::template NodeMap bpath_atree(graph, 0); deba@861: bpath_atree[anode] = atree[anode]; deba@861: for (int i = 1; i < int(border.size()); ++i) { deba@861: Node n = border[i]; deba@861: bpath_atree[n] = atree[n] + bpath_atree[bpred[n]]; deba@861: } deba@861: deba@861: typename AuxGraph::template NodeMap cpath(graph, 0); deba@861: cpath[anode] = cpath[bnode] = 1; deba@861: typename AuxGraph::template NodeMap cpath_atree(graph, 0); deba@861: cpath_atree[anode] = atree[anode]; deba@861: typename AuxGraph::template NodeMap cpath_btree(graph, 0); deba@861: cpath_btree[bnode] = btree[bnode]; deba@861: for (int i = 1; i < int(corder.size()); ++i) { deba@861: Node n = corder[i]; deba@861: cpath[n] = cpath[cpred[n]] + 1; deba@861: cpath_atree[n] = atree[n] + cpath_atree[cpred[n]]; deba@861: cpath_btree[n] = btree[n] + cpath_btree[cpred[n]]; deba@861: } deba@861: deba@861: typename AuxGraph::template NodeMap third(graph); deba@861: for (NodeIt n(graph); n != INVALID; ++n) { deba@861: point_map[n].x = deba@861: bpath_atree[n] + cpath_atree[n] - atree[n] - cpath[n] + 1; deba@861: point_map[n].y = deba@861: cpath_btree[n] + apath_btree[n] - btree[n] - apath[n] + 1; deba@861: } deba@861: deba@861: } deba@861: deba@861: public: deba@861: kpeter@896: /// \brief Calculate the node positions deba@861: /// kpeter@896: /// This function calculates the node positions on the plane. kpeter@896: /// \return \c true if the graph is planar. deba@861: bool run() { deba@861: PlanarEmbedding pe(_graph); deba@861: if (!pe.run()) return false; deba@861: deba@861: run(pe); deba@861: return true; deba@861: } deba@861: kpeter@896: /// \brief Calculate the node positions according to a deba@861: /// combinatorical embedding deba@861: /// kpeter@896: /// This function calculates the node positions on the plane. kpeter@896: /// The given \c embedding map should contain a valid combinatorical kpeter@896: /// embedding, i.e. a valid cyclic order of the arcs. kpeter@896: /// It can be computed using PlanarEmbedding. deba@861: template deba@861: void run(const EmbeddingMap& embedding) { deba@861: typedef SmartEdgeSet AuxGraph; deba@861: deba@861: if (3 * countNodes(_graph) - 6 == countEdges(_graph)) { deba@861: drawing(_graph, embedding, _point_map); deba@861: return; deba@861: } deba@861: deba@861: AuxGraph aux_graph(_graph); deba@861: typename AuxGraph::template ArcMap deba@861: aux_embedding(aux_graph); deba@861: deba@861: { deba@861: deba@861: typename Graph::template EdgeMap deba@861: ref(_graph); deba@861: deba@861: for (EdgeIt e(_graph); e != INVALID; ++e) { deba@861: ref[e] = aux_graph.addEdge(_graph.u(e), _graph.v(e)); deba@861: } deba@861: deba@861: for (EdgeIt e(_graph); e != INVALID; ++e) { deba@861: Arc ee = embedding[_graph.direct(e, true)]; deba@861: aux_embedding[aux_graph.direct(ref[e], true)] = deba@861: aux_graph.direct(ref[ee], _graph.direction(ee)); deba@861: ee = embedding[_graph.direct(e, false)]; deba@861: aux_embedding[aux_graph.direct(ref[e], false)] = deba@861: aux_graph.direct(ref[ee], _graph.direction(ee)); deba@861: } deba@861: } deba@861: _planarity_bits::makeConnected(aux_graph, aux_embedding); deba@861: _planarity_bits::makeBiNodeConnected(aux_graph, aux_embedding); deba@861: _planarity_bits::makeMaxPlanar(aux_graph, aux_embedding); deba@861: drawing(aux_graph, aux_embedding, _point_map); deba@861: } deba@861: deba@861: /// \brief The coordinate of the given node deba@861: /// kpeter@896: /// This function returns the coordinate of the given node. deba@861: Point operator[](const Node& node) const { deba@861: return _point_map[node]; deba@861: } deba@861: kpeter@896: /// \brief Return the grid embedding in a node map deba@861: /// kpeter@896: /// This function returns the grid embedding in a node map of kpeter@896: /// \c dim2::Point coordinates. deba@861: const PointMap& coords() const { deba@861: return _point_map; deba@861: } deba@861: deba@861: private: deba@861: deba@861: const Graph& _graph; deba@861: PointMap _point_map; deba@861: deba@861: }; deba@861: deba@861: namespace _planarity_bits { deba@861: deba@861: template deba@861: class KempeFilter { deba@861: public: deba@861: typedef typename ColorMap::Key Key; deba@861: typedef bool Value; deba@861: deba@861: KempeFilter(const ColorMap& color_map, deba@861: const typename ColorMap::Value& first, deba@861: const typename ColorMap::Value& second) deba@861: : _color_map(color_map), _first(first), _second(second) {} deba@861: deba@861: Value operator[](const Key& key) const { deba@861: return _color_map[key] == _first || _color_map[key] == _second; deba@861: } deba@861: deba@861: private: deba@861: const ColorMap& _color_map; deba@861: typename ColorMap::Value _first, _second; deba@861: }; deba@861: } deba@861: deba@861: /// \ingroup planar deba@861: /// deba@861: /// \brief Coloring planar graphs deba@861: /// deba@861: /// The graph coloring problem is the coloring of the graph nodes kpeter@896: /// so that there are no adjacent nodes with the same color. The kpeter@896: /// planar graphs can always be colored with four colors, which is kpeter@896: /// proved by Appel and Haken. Their proofs provide a quadratic deba@861: /// time algorithm for four coloring, but it could not be used to kpeter@896: /// implement an efficient algorithm. The five and six coloring can be kpeter@896: /// made in linear time, but in this class, the five coloring has deba@861: /// quadratic worst case time complexity. The two coloring (if deba@861: /// possible) is solvable with a graph search algorithm and it is deba@861: /// implemented in \ref bipartitePartitions() function in LEMON. To kpeter@896: /// decide whether a planar graph is three colorable is NP-complete. deba@861: /// deba@861: /// This class contains member functions for calculate colorings deba@861: /// with five and six colors. The six coloring algorithm is a simple deba@861: /// greedy coloring on the backward minimum outgoing order of nodes. kpeter@896: /// This order can be computed by selecting the node with least kpeter@896: /// outgoing arcs to unprocessed nodes in each phase. This order deba@861: /// guarantees that when a node is chosen for coloring it has at deba@861: /// most five already colored adjacents. The five coloring algorithm deba@861: /// use the same method, but if the greedy approach fails to color deba@861: /// with five colors, i.e. the node has five already different deba@861: /// colored neighbours, it swaps the colors in one of the connected deba@861: /// two colored sets with the Kempe recoloring method. deba@861: template deba@861: class PlanarColoring { deba@861: public: deba@861: deba@861: TEMPLATE_GRAPH_TYPEDEFS(Graph); deba@861: kpeter@896: /// \brief The map type for storing color indices deba@861: typedef typename Graph::template NodeMap IndexMap; kpeter@896: /// \brief The map type for storing colors kpeter@896: /// kpeter@896: /// The map type for storing colors. kpeter@896: /// \see Palette, Color deba@861: typedef ComposeMap ColorMap; deba@861: deba@861: /// \brief Constructor deba@861: /// kpeter@896: /// Constructor. kpeter@896: /// \pre The graph must be simple, i.e. it should not kpeter@896: /// contain parallel or loop arcs. deba@861: PlanarColoring(const Graph& graph) deba@861: : _graph(graph), _color_map(graph), _palette(0) { deba@861: _palette.add(Color(1,0,0)); deba@861: _palette.add(Color(0,1,0)); deba@861: _palette.add(Color(0,0,1)); deba@861: _palette.add(Color(1,1,0)); deba@861: _palette.add(Color(1,0,1)); deba@861: _palette.add(Color(0,1,1)); deba@861: } deba@861: kpeter@896: /// \brief Return the node map of color indices deba@861: /// kpeter@896: /// This function returns the node map of color indices. The values are kpeter@896: /// in the range \c [0..4] or \c [0..5] according to the coloring method. deba@861: IndexMap colorIndexMap() const { deba@861: return _color_map; deba@861: } deba@861: kpeter@896: /// \brief Return the node map of colors deba@861: /// kpeter@896: /// This function returns the node map of colors. The values are among kpeter@896: /// five or six distinct \ref lemon::Color "colors". deba@861: ColorMap colorMap() const { deba@861: return composeMap(_palette, _color_map); deba@861: } deba@861: kpeter@896: /// \brief Return the color index of the node deba@861: /// kpeter@896: /// This function returns the color index of the given node. The value is kpeter@896: /// in the range \c [0..4] or \c [0..5] according to the coloring method. deba@861: int colorIndex(const Node& node) const { deba@861: return _color_map[node]; deba@861: } deba@861: kpeter@896: /// \brief Return the color of the node deba@861: /// kpeter@896: /// This function returns the color of the given node. The value is among kpeter@896: /// five or six distinct \ref lemon::Color "colors". deba@861: Color color(const Node& node) const { deba@861: return _palette[_color_map[node]]; deba@861: } deba@861: deba@861: kpeter@896: /// \brief Calculate a coloring with at most six colors deba@861: /// deba@861: /// This function calculates a coloring with at most six colors. The time deba@861: /// complexity of this variant is linear in the size of the graph. kpeter@896: /// \return \c true if the algorithm could color the graph with six colors. kpeter@896: /// If the algorithm fails, then the graph is not planar. kpeter@896: /// \note This function can return \c true if the graph is not kpeter@896: /// planar, but it can be colored with at most six colors. deba@861: bool runSixColoring() { deba@861: deba@861: typename Graph::template NodeMap heap_index(_graph, -1); deba@861: BucketHeap > heap(heap_index); deba@861: deba@861: for (NodeIt n(_graph); n != INVALID; ++n) { deba@861: _color_map[n] = -2; deba@861: heap.push(n, countOutArcs(_graph, n)); deba@861: } deba@861: deba@861: std::vector order; deba@861: deba@861: while (!heap.empty()) { deba@861: Node n = heap.top(); deba@861: heap.pop(); deba@861: _color_map[n] = -1; deba@861: order.push_back(n); deba@861: for (OutArcIt e(_graph, n); e != INVALID; ++e) { deba@861: Node t = _graph.runningNode(e); deba@861: if (_color_map[t] == -2) { deba@861: heap.decrease(t, heap[t] - 1); deba@861: } deba@861: } deba@861: } deba@861: deba@861: for (int i = order.size() - 1; i >= 0; --i) { deba@861: std::vector forbidden(6, false); deba@861: for (OutArcIt e(_graph, order[i]); e != INVALID; ++e) { deba@861: Node t = _graph.runningNode(e); deba@861: if (_color_map[t] != -1) { deba@861: forbidden[_color_map[t]] = true; deba@861: } deba@861: } deba@861: for (int k = 0; k < 6; ++k) { deba@861: if (!forbidden[k]) { deba@861: _color_map[order[i]] = k; deba@861: break; deba@861: } deba@861: } deba@861: if (_color_map[order[i]] == -1) { deba@861: return false; deba@861: } deba@861: } deba@861: return true; deba@861: } deba@861: deba@861: private: deba@861: deba@861: bool recolor(const Node& u, const Node& v) { deba@861: int ucolor = _color_map[u]; deba@861: int vcolor = _color_map[v]; deba@861: typedef _planarity_bits::KempeFilter KempeFilter; deba@861: KempeFilter filter(_color_map, ucolor, vcolor); deba@861: deba@861: typedef FilterNodes KempeGraph; deba@861: KempeGraph kempe_graph(_graph, filter); deba@861: deba@861: std::vector comp; deba@861: Bfs bfs(kempe_graph); deba@861: bfs.init(); deba@861: bfs.addSource(u); deba@861: while (!bfs.emptyQueue()) { deba@861: Node n = bfs.nextNode(); deba@861: if (n == v) return false; deba@861: comp.push_back(n); deba@861: bfs.processNextNode(); deba@861: } deba@861: deba@861: int scolor = ucolor + vcolor; deba@861: for (int i = 0; i < static_cast(comp.size()); ++i) { deba@861: _color_map[comp[i]] = scolor - _color_map[comp[i]]; deba@861: } deba@861: deba@861: return true; deba@861: } deba@861: deba@861: template deba@861: void kempeRecoloring(const Node& node, const EmbeddingMap& embedding) { deba@861: std::vector nodes; deba@861: nodes.reserve(4); deba@861: deba@861: for (Arc e = OutArcIt(_graph, node); e != INVALID; e = embedding[e]) { deba@861: Node t = _graph.target(e); deba@861: if (_color_map[t] != -1) { deba@861: nodes.push_back(t); deba@861: if (nodes.size() == 4) break; deba@861: } deba@861: } deba@861: deba@861: int color = _color_map[nodes[0]]; deba@861: if (recolor(nodes[0], nodes[2])) { deba@861: _color_map[node] = color; deba@861: } else { deba@861: color = _color_map[nodes[1]]; deba@861: recolor(nodes[1], nodes[3]); deba@861: _color_map[node] = color; deba@861: } deba@861: } deba@861: deba@861: public: deba@861: kpeter@896: /// \brief Calculate a coloring with at most five colors deba@861: /// deba@861: /// This function calculates a coloring with at most five deba@861: /// colors. The worst case time complexity of this variant is deba@861: /// quadratic in the size of the graph. kpeter@896: /// \param embedding This map should contain a valid combinatorical kpeter@896: /// embedding, i.e. a valid cyclic order of the arcs. kpeter@896: /// It can be computed using PlanarEmbedding. deba@861: template deba@861: void runFiveColoring(const EmbeddingMap& embedding) { deba@861: deba@861: typename Graph::template NodeMap heap_index(_graph, -1); deba@861: BucketHeap > heap(heap_index); deba@861: deba@861: for (NodeIt n(_graph); n != INVALID; ++n) { deba@861: _color_map[n] = -2; deba@861: heap.push(n, countOutArcs(_graph, n)); deba@861: } deba@861: deba@861: std::vector order; deba@861: deba@861: while (!heap.empty()) { deba@861: Node n = heap.top(); deba@861: heap.pop(); deba@861: _color_map[n] = -1; deba@861: order.push_back(n); deba@861: for (OutArcIt e(_graph, n); e != INVALID; ++e) { deba@861: Node t = _graph.runningNode(e); deba@861: if (_color_map[t] == -2) { deba@861: heap.decrease(t, heap[t] - 1); deba@861: } deba@861: } deba@861: } deba@861: deba@861: for (int i = order.size() - 1; i >= 0; --i) { deba@861: std::vector forbidden(5, false); deba@861: for (OutArcIt e(_graph, order[i]); e != INVALID; ++e) { deba@861: Node t = _graph.runningNode(e); deba@861: if (_color_map[t] != -1) { deba@861: forbidden[_color_map[t]] = true; deba@861: } deba@861: } deba@861: for (int k = 0; k < 5; ++k) { deba@861: if (!forbidden[k]) { deba@861: _color_map[order[i]] = k; deba@861: break; deba@861: } deba@861: } deba@861: if (_color_map[order[i]] == -1) { deba@861: kempeRecoloring(order[i], embedding); deba@861: } deba@861: } deba@861: } deba@861: kpeter@896: /// \brief Calculate a coloring with at most five colors deba@861: /// deba@861: /// This function calculates a coloring with at most five deba@861: /// colors. The worst case time complexity of this variant is deba@861: /// quadratic in the size of the graph. kpeter@896: /// \return \c true if the graph is planar. deba@861: bool runFiveColoring() { deba@861: PlanarEmbedding pe(_graph); deba@861: if (!pe.run()) return false; deba@861: deba@861: runFiveColoring(pe.embeddingMap()); deba@861: return true; deba@861: } deba@861: deba@861: private: deba@861: deba@861: const Graph& _graph; deba@861: IndexMap _color_map; deba@861: Palette _palette; deba@861: }; deba@861: deba@861: } deba@861: deba@861: #endif