deba@861
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
deba@861
|
2 |
*
|
deba@861
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
deba@861
|
4 |
*
|
alpar@1270
|
5 |
* Copyright (C) 2003-2013
|
deba@861
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
deba@861
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
deba@861
|
8 |
*
|
deba@861
|
9 |
* Permission to use, modify and distribute this software is granted
|
deba@861
|
10 |
* provided that this copyright notice appears in all copies. For
|
deba@861
|
11 |
* precise terms see the accompanying LICENSE file.
|
deba@861
|
12 |
*
|
deba@861
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
deba@861
|
14 |
* express or implied, and with no claim as to its suitability for any
|
deba@861
|
15 |
* purpose.
|
deba@861
|
16 |
*
|
deba@861
|
17 |
*/
|
deba@861
|
18 |
|
deba@861
|
19 |
#ifndef LEMON_PLANARITY_H
|
deba@861
|
20 |
#define LEMON_PLANARITY_H
|
deba@861
|
21 |
|
deba@861
|
22 |
/// \ingroup planar
|
deba@861
|
23 |
/// \file
|
deba@861
|
24 |
/// \brief Planarity checking, embedding, drawing and coloring
|
deba@861
|
25 |
|
deba@861
|
26 |
#include <vector>
|
deba@861
|
27 |
#include <list>
|
deba@861
|
28 |
|
deba@861
|
29 |
#include <lemon/dfs.h>
|
deba@861
|
30 |
#include <lemon/bfs.h>
|
deba@861
|
31 |
#include <lemon/radix_sort.h>
|
deba@861
|
32 |
#include <lemon/maps.h>
|
deba@861
|
33 |
#include <lemon/path.h>
|
deba@861
|
34 |
#include <lemon/bucket_heap.h>
|
deba@861
|
35 |
#include <lemon/adaptors.h>
|
deba@861
|
36 |
#include <lemon/edge_set.h>
|
deba@861
|
37 |
#include <lemon/color.h>
|
deba@861
|
38 |
#include <lemon/dim2.h>
|
deba@861
|
39 |
|
deba@861
|
40 |
namespace lemon {
|
deba@861
|
41 |
|
deba@861
|
42 |
namespace _planarity_bits {
|
deba@861
|
43 |
|
deba@861
|
44 |
template <typename Graph>
|
deba@861
|
45 |
struct PlanarityVisitor : DfsVisitor<Graph> {
|
deba@861
|
46 |
|
deba@861
|
47 |
TEMPLATE_GRAPH_TYPEDEFS(Graph);
|
deba@861
|
48 |
|
deba@861
|
49 |
typedef typename Graph::template NodeMap<Arc> PredMap;
|
deba@861
|
50 |
|
deba@861
|
51 |
typedef typename Graph::template EdgeMap<bool> TreeMap;
|
deba@861
|
52 |
|
deba@861
|
53 |
typedef typename Graph::template NodeMap<int> OrderMap;
|
deba@861
|
54 |
typedef std::vector<Node> OrderList;
|
deba@861
|
55 |
|
deba@861
|
56 |
typedef typename Graph::template NodeMap<int> LowMap;
|
deba@861
|
57 |
typedef typename Graph::template NodeMap<int> AncestorMap;
|
deba@861
|
58 |
|
deba@861
|
59 |
PlanarityVisitor(const Graph& graph,
|
deba@861
|
60 |
PredMap& pred_map, TreeMap& tree_map,
|
deba@861
|
61 |
OrderMap& order_map, OrderList& order_list,
|
deba@861
|
62 |
AncestorMap& ancestor_map, LowMap& low_map)
|
deba@861
|
63 |
: _graph(graph), _pred_map(pred_map), _tree_map(tree_map),
|
deba@861
|
64 |
_order_map(order_map), _order_list(order_list),
|
deba@861
|
65 |
_ancestor_map(ancestor_map), _low_map(low_map) {}
|
deba@861
|
66 |
|
deba@861
|
67 |
void reach(const Node& node) {
|
deba@861
|
68 |
_order_map[node] = _order_list.size();
|
deba@861
|
69 |
_low_map[node] = _order_list.size();
|
deba@861
|
70 |
_ancestor_map[node] = _order_list.size();
|
deba@861
|
71 |
_order_list.push_back(node);
|
deba@861
|
72 |
}
|
deba@861
|
73 |
|
deba@861
|
74 |
void discover(const Arc& arc) {
|
deba@861
|
75 |
Node target = _graph.target(arc);
|
deba@861
|
76 |
|
deba@861
|
77 |
_tree_map[arc] = true;
|
deba@861
|
78 |
_pred_map[target] = arc;
|
deba@861
|
79 |
}
|
deba@861
|
80 |
|
deba@861
|
81 |
void examine(const Arc& arc) {
|
deba@861
|
82 |
Node source = _graph.source(arc);
|
deba@861
|
83 |
Node target = _graph.target(arc);
|
deba@861
|
84 |
|
deba@861
|
85 |
if (_order_map[target] < _order_map[source] && !_tree_map[arc]) {
|
deba@861
|
86 |
if (_low_map[source] > _order_map[target]) {
|
deba@861
|
87 |
_low_map[source] = _order_map[target];
|
deba@861
|
88 |
}
|
deba@861
|
89 |
if (_ancestor_map[source] > _order_map[target]) {
|
deba@861
|
90 |
_ancestor_map[source] = _order_map[target];
|
deba@861
|
91 |
}
|
deba@861
|
92 |
}
|
deba@861
|
93 |
}
|
deba@861
|
94 |
|
deba@861
|
95 |
void backtrack(const Arc& arc) {
|
deba@861
|
96 |
Node source = _graph.source(arc);
|
deba@861
|
97 |
Node target = _graph.target(arc);
|
deba@861
|
98 |
|
deba@861
|
99 |
if (_low_map[source] > _low_map[target]) {
|
deba@861
|
100 |
_low_map[source] = _low_map[target];
|
deba@861
|
101 |
}
|
deba@861
|
102 |
}
|
deba@861
|
103 |
|
deba@861
|
104 |
const Graph& _graph;
|
deba@861
|
105 |
PredMap& _pred_map;
|
deba@861
|
106 |
TreeMap& _tree_map;
|
deba@861
|
107 |
OrderMap& _order_map;
|
deba@861
|
108 |
OrderList& _order_list;
|
deba@861
|
109 |
AncestorMap& _ancestor_map;
|
deba@861
|
110 |
LowMap& _low_map;
|
deba@861
|
111 |
};
|
deba@861
|
112 |
|
deba@861
|
113 |
template <typename Graph, bool embedding = true>
|
deba@861
|
114 |
struct NodeDataNode {
|
deba@861
|
115 |
int prev, next;
|
deba@861
|
116 |
int visited;
|
deba@861
|
117 |
typename Graph::Arc first;
|
deba@861
|
118 |
bool inverted;
|
deba@861
|
119 |
};
|
deba@861
|
120 |
|
deba@861
|
121 |
template <typename Graph>
|
deba@861
|
122 |
struct NodeDataNode<Graph, false> {
|
deba@861
|
123 |
int prev, next;
|
deba@861
|
124 |
int visited;
|
deba@861
|
125 |
};
|
deba@861
|
126 |
|
deba@861
|
127 |
template <typename Graph>
|
deba@861
|
128 |
struct ChildListNode {
|
deba@861
|
129 |
typedef typename Graph::Node Node;
|
deba@861
|
130 |
Node first;
|
deba@861
|
131 |
Node prev, next;
|
deba@861
|
132 |
};
|
deba@861
|
133 |
|
deba@861
|
134 |
template <typename Graph>
|
deba@861
|
135 |
struct ArcListNode {
|
deba@861
|
136 |
typename Graph::Arc prev, next;
|
deba@861
|
137 |
};
|
deba@861
|
138 |
|
deba@862
|
139 |
template <typename Graph>
|
deba@862
|
140 |
class PlanarityChecking {
|
deba@862
|
141 |
private:
|
alpar@956
|
142 |
|
deba@862
|
143 |
TEMPLATE_GRAPH_TYPEDEFS(Graph);
|
deba@862
|
144 |
|
deba@862
|
145 |
const Graph& _graph;
|
deba@862
|
146 |
|
deba@862
|
147 |
private:
|
alpar@956
|
148 |
|
deba@862
|
149 |
typedef typename Graph::template NodeMap<Arc> PredMap;
|
alpar@956
|
150 |
|
deba@862
|
151 |
typedef typename Graph::template EdgeMap<bool> TreeMap;
|
alpar@956
|
152 |
|
deba@862
|
153 |
typedef typename Graph::template NodeMap<int> OrderMap;
|
deba@862
|
154 |
typedef std::vector<Node> OrderList;
|
deba@862
|
155 |
|
deba@862
|
156 |
typedef typename Graph::template NodeMap<int> LowMap;
|
deba@862
|
157 |
typedef typename Graph::template NodeMap<int> AncestorMap;
|
deba@862
|
158 |
|
deba@862
|
159 |
typedef _planarity_bits::NodeDataNode<Graph> NodeDataNode;
|
deba@862
|
160 |
typedef std::vector<NodeDataNode> NodeData;
|
deba@862
|
161 |
|
deba@862
|
162 |
typedef _planarity_bits::ChildListNode<Graph> ChildListNode;
|
deba@862
|
163 |
typedef typename Graph::template NodeMap<ChildListNode> ChildLists;
|
deba@862
|
164 |
|
deba@862
|
165 |
typedef typename Graph::template NodeMap<std::list<int> > MergeRoots;
|
deba@862
|
166 |
|
deba@862
|
167 |
typedef typename Graph::template NodeMap<bool> EmbedArc;
|
deba@862
|
168 |
|
deba@862
|
169 |
public:
|
deba@862
|
170 |
|
deba@862
|
171 |
PlanarityChecking(const Graph& graph) : _graph(graph) {}
|
deba@862
|
172 |
|
deba@862
|
173 |
bool run() {
|
deba@862
|
174 |
typedef _planarity_bits::PlanarityVisitor<Graph> Visitor;
|
deba@862
|
175 |
|
deba@862
|
176 |
PredMap pred_map(_graph, INVALID);
|
deba@862
|
177 |
TreeMap tree_map(_graph, false);
|
deba@862
|
178 |
|
deba@862
|
179 |
OrderMap order_map(_graph, -1);
|
deba@862
|
180 |
OrderList order_list;
|
deba@862
|
181 |
|
deba@862
|
182 |
AncestorMap ancestor_map(_graph, -1);
|
deba@862
|
183 |
LowMap low_map(_graph, -1);
|
deba@862
|
184 |
|
deba@862
|
185 |
Visitor visitor(_graph, pred_map, tree_map,
|
deba@862
|
186 |
order_map, order_list, ancestor_map, low_map);
|
deba@862
|
187 |
DfsVisit<Graph, Visitor> visit(_graph, visitor);
|
deba@862
|
188 |
visit.run();
|
deba@862
|
189 |
|
deba@862
|
190 |
ChildLists child_lists(_graph);
|
deba@862
|
191 |
createChildLists(tree_map, order_map, low_map, child_lists);
|
deba@862
|
192 |
|
deba@862
|
193 |
NodeData node_data(2 * order_list.size());
|
deba@862
|
194 |
|
deba@862
|
195 |
EmbedArc embed_arc(_graph, false);
|
deba@862
|
196 |
|
deba@862
|
197 |
MergeRoots merge_roots(_graph);
|
deba@862
|
198 |
|
deba@862
|
199 |
for (int i = order_list.size() - 1; i >= 0; --i) {
|
deba@862
|
200 |
|
deba@862
|
201 |
Node node = order_list[i];
|
deba@862
|
202 |
|
deba@862
|
203 |
Node source = node;
|
deba@862
|
204 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
|
deba@862
|
205 |
Node target = _graph.target(e);
|
deba@862
|
206 |
|
deba@862
|
207 |
if (order_map[source] < order_map[target] && tree_map[e]) {
|
deba@862
|
208 |
initFace(target, node_data, order_map, order_list);
|
deba@862
|
209 |
}
|
deba@862
|
210 |
}
|
deba@862
|
211 |
|
deba@862
|
212 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
|
deba@862
|
213 |
Node target = _graph.target(e);
|
deba@862
|
214 |
|
deba@862
|
215 |
if (order_map[source] < order_map[target] && !tree_map[e]) {
|
deba@862
|
216 |
embed_arc[target] = true;
|
deba@862
|
217 |
walkUp(target, source, i, pred_map, low_map,
|
deba@862
|
218 |
order_map, order_list, node_data, merge_roots);
|
deba@862
|
219 |
}
|
deba@862
|
220 |
}
|
deba@862
|
221 |
|
deba@862
|
222 |
for (typename MergeRoots::Value::iterator it =
|
alpar@956
|
223 |
merge_roots[node].begin();
|
deba@862
|
224 |
it != merge_roots[node].end(); ++it) {
|
deba@862
|
225 |
int rn = *it;
|
deba@862
|
226 |
walkDown(rn, i, node_data, order_list, child_lists,
|
deba@862
|
227 |
ancestor_map, low_map, embed_arc, merge_roots);
|
deba@862
|
228 |
}
|
deba@862
|
229 |
merge_roots[node].clear();
|
deba@862
|
230 |
|
deba@862
|
231 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
|
deba@862
|
232 |
Node target = _graph.target(e);
|
deba@862
|
233 |
|
deba@862
|
234 |
if (order_map[source] < order_map[target] && !tree_map[e]) {
|
deba@862
|
235 |
if (embed_arc[target]) {
|
deba@862
|
236 |
return false;
|
deba@862
|
237 |
}
|
deba@862
|
238 |
}
|
deba@862
|
239 |
}
|
deba@862
|
240 |
}
|
deba@862
|
241 |
|
deba@862
|
242 |
return true;
|
deba@862
|
243 |
}
|
deba@862
|
244 |
|
deba@862
|
245 |
private:
|
deba@862
|
246 |
|
deba@862
|
247 |
void createChildLists(const TreeMap& tree_map, const OrderMap& order_map,
|
deba@862
|
248 |
const LowMap& low_map, ChildLists& child_lists) {
|
deba@862
|
249 |
|
deba@862
|
250 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@862
|
251 |
Node source = n;
|
deba@862
|
252 |
|
deba@862
|
253 |
std::vector<Node> targets;
|
deba@862
|
254 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
deba@862
|
255 |
Node target = _graph.target(e);
|
deba@862
|
256 |
|
deba@862
|
257 |
if (order_map[source] < order_map[target] && tree_map[e]) {
|
deba@862
|
258 |
targets.push_back(target);
|
deba@862
|
259 |
}
|
deba@862
|
260 |
}
|
deba@862
|
261 |
|
deba@862
|
262 |
if (targets.size() == 0) {
|
deba@862
|
263 |
child_lists[source].first = INVALID;
|
deba@862
|
264 |
} else if (targets.size() == 1) {
|
deba@862
|
265 |
child_lists[source].first = targets[0];
|
deba@862
|
266 |
child_lists[targets[0]].prev = INVALID;
|
deba@862
|
267 |
child_lists[targets[0]].next = INVALID;
|
deba@862
|
268 |
} else {
|
deba@862
|
269 |
radixSort(targets.begin(), targets.end(), mapToFunctor(low_map));
|
deba@862
|
270 |
for (int i = 1; i < int(targets.size()); ++i) {
|
deba@862
|
271 |
child_lists[targets[i]].prev = targets[i - 1];
|
deba@862
|
272 |
child_lists[targets[i - 1]].next = targets[i];
|
deba@862
|
273 |
}
|
deba@862
|
274 |
child_lists[targets.back()].next = INVALID;
|
deba@862
|
275 |
child_lists[targets.front()].prev = INVALID;
|
deba@862
|
276 |
child_lists[source].first = targets.front();
|
deba@862
|
277 |
}
|
deba@862
|
278 |
}
|
deba@862
|
279 |
}
|
deba@862
|
280 |
|
deba@862
|
281 |
void walkUp(const Node& node, Node root, int rorder,
|
deba@862
|
282 |
const PredMap& pred_map, const LowMap& low_map,
|
deba@862
|
283 |
const OrderMap& order_map, const OrderList& order_list,
|
deba@862
|
284 |
NodeData& node_data, MergeRoots& merge_roots) {
|
deba@862
|
285 |
|
deba@862
|
286 |
int na, nb;
|
deba@862
|
287 |
bool da, db;
|
deba@862
|
288 |
|
deba@862
|
289 |
na = nb = order_map[node];
|
deba@862
|
290 |
da = true; db = false;
|
deba@862
|
291 |
|
deba@862
|
292 |
while (true) {
|
deba@862
|
293 |
|
deba@862
|
294 |
if (node_data[na].visited == rorder) break;
|
deba@862
|
295 |
if (node_data[nb].visited == rorder) break;
|
deba@862
|
296 |
|
deba@862
|
297 |
node_data[na].visited = rorder;
|
deba@862
|
298 |
node_data[nb].visited = rorder;
|
deba@862
|
299 |
|
deba@862
|
300 |
int rn = -1;
|
deba@862
|
301 |
|
deba@862
|
302 |
if (na >= int(order_list.size())) {
|
deba@862
|
303 |
rn = na;
|
deba@862
|
304 |
} else if (nb >= int(order_list.size())) {
|
deba@862
|
305 |
rn = nb;
|
deba@862
|
306 |
}
|
deba@862
|
307 |
|
deba@862
|
308 |
if (rn == -1) {
|
deba@862
|
309 |
int nn;
|
deba@862
|
310 |
|
deba@862
|
311 |
nn = da ? node_data[na].prev : node_data[na].next;
|
deba@862
|
312 |
da = node_data[nn].prev != na;
|
deba@862
|
313 |
na = nn;
|
deba@862
|
314 |
|
deba@862
|
315 |
nn = db ? node_data[nb].prev : node_data[nb].next;
|
deba@862
|
316 |
db = node_data[nn].prev != nb;
|
deba@862
|
317 |
nb = nn;
|
deba@862
|
318 |
|
deba@862
|
319 |
} else {
|
deba@862
|
320 |
|
deba@862
|
321 |
Node rep = order_list[rn - order_list.size()];
|
deba@862
|
322 |
Node parent = _graph.source(pred_map[rep]);
|
deba@862
|
323 |
|
deba@862
|
324 |
if (low_map[rep] < rorder) {
|
deba@862
|
325 |
merge_roots[parent].push_back(rn);
|
deba@862
|
326 |
} else {
|
deba@862
|
327 |
merge_roots[parent].push_front(rn);
|
deba@862
|
328 |
}
|
deba@862
|
329 |
|
deba@862
|
330 |
if (parent != root) {
|
deba@862
|
331 |
na = nb = order_map[parent];
|
deba@862
|
332 |
da = true; db = false;
|
deba@862
|
333 |
} else {
|
deba@862
|
334 |
break;
|
deba@862
|
335 |
}
|
deba@862
|
336 |
}
|
deba@862
|
337 |
}
|
deba@862
|
338 |
}
|
deba@862
|
339 |
|
deba@862
|
340 |
void walkDown(int rn, int rorder, NodeData& node_data,
|
deba@862
|
341 |
OrderList& order_list, ChildLists& child_lists,
|
deba@862
|
342 |
AncestorMap& ancestor_map, LowMap& low_map,
|
deba@862
|
343 |
EmbedArc& embed_arc, MergeRoots& merge_roots) {
|
deba@862
|
344 |
|
deba@862
|
345 |
std::vector<std::pair<int, bool> > merge_stack;
|
deba@862
|
346 |
|
deba@862
|
347 |
for (int di = 0; di < 2; ++di) {
|
deba@862
|
348 |
bool rd = di == 0;
|
deba@862
|
349 |
int pn = rn;
|
deba@862
|
350 |
int n = rd ? node_data[rn].next : node_data[rn].prev;
|
deba@862
|
351 |
|
deba@862
|
352 |
while (n != rn) {
|
deba@862
|
353 |
|
deba@862
|
354 |
Node node = order_list[n];
|
deba@862
|
355 |
|
deba@862
|
356 |
if (embed_arc[node]) {
|
deba@862
|
357 |
|
deba@862
|
358 |
// Merging components on the critical path
|
deba@862
|
359 |
while (!merge_stack.empty()) {
|
deba@862
|
360 |
|
deba@862
|
361 |
// Component root
|
deba@862
|
362 |
int cn = merge_stack.back().first;
|
deba@862
|
363 |
bool cd = merge_stack.back().second;
|
deba@862
|
364 |
merge_stack.pop_back();
|
deba@862
|
365 |
|
deba@862
|
366 |
// Parent of component
|
deba@862
|
367 |
int dn = merge_stack.back().first;
|
deba@862
|
368 |
bool dd = merge_stack.back().second;
|
deba@862
|
369 |
merge_stack.pop_back();
|
deba@862
|
370 |
|
deba@862
|
371 |
Node parent = order_list[dn];
|
deba@862
|
372 |
|
deba@862
|
373 |
// Erasing from merge_roots
|
deba@862
|
374 |
merge_roots[parent].pop_front();
|
deba@862
|
375 |
|
deba@862
|
376 |
Node child = order_list[cn - order_list.size()];
|
deba@862
|
377 |
|
deba@862
|
378 |
// Erasing from child_lists
|
deba@862
|
379 |
if (child_lists[child].prev != INVALID) {
|
deba@862
|
380 |
child_lists[child_lists[child].prev].next =
|
deba@862
|
381 |
child_lists[child].next;
|
deba@862
|
382 |
} else {
|
deba@862
|
383 |
child_lists[parent].first = child_lists[child].next;
|
deba@862
|
384 |
}
|
deba@862
|
385 |
|
deba@862
|
386 |
if (child_lists[child].next != INVALID) {
|
deba@862
|
387 |
child_lists[child_lists[child].next].prev =
|
deba@862
|
388 |
child_lists[child].prev;
|
deba@862
|
389 |
}
|
deba@862
|
390 |
|
deba@862
|
391 |
// Merging external faces
|
deba@862
|
392 |
{
|
deba@862
|
393 |
int en = cn;
|
deba@862
|
394 |
cn = cd ? node_data[cn].prev : node_data[cn].next;
|
deba@862
|
395 |
cd = node_data[cn].next == en;
|
deba@862
|
396 |
|
deba@862
|
397 |
}
|
deba@862
|
398 |
|
deba@862
|
399 |
if (cd) node_data[cn].next = dn; else node_data[cn].prev = dn;
|
deba@862
|
400 |
if (dd) node_data[dn].prev = cn; else node_data[dn].next = cn;
|
deba@862
|
401 |
|
deba@862
|
402 |
}
|
deba@862
|
403 |
|
deba@862
|
404 |
bool d = pn == node_data[n].prev;
|
deba@862
|
405 |
|
deba@862
|
406 |
if (node_data[n].prev == node_data[n].next &&
|
deba@862
|
407 |
node_data[n].inverted) {
|
deba@862
|
408 |
d = !d;
|
deba@862
|
409 |
}
|
deba@862
|
410 |
|
deba@862
|
411 |
// Embedding arc into external face
|
deba@862
|
412 |
if (rd) node_data[rn].next = n; else node_data[rn].prev = n;
|
deba@862
|
413 |
if (d) node_data[n].prev = rn; else node_data[n].next = rn;
|
deba@862
|
414 |
pn = rn;
|
deba@862
|
415 |
|
deba@862
|
416 |
embed_arc[order_list[n]] = false;
|
deba@862
|
417 |
}
|
deba@862
|
418 |
|
deba@862
|
419 |
if (!merge_roots[node].empty()) {
|
deba@862
|
420 |
|
deba@862
|
421 |
bool d = pn == node_data[n].prev;
|
deba@862
|
422 |
|
deba@862
|
423 |
merge_stack.push_back(std::make_pair(n, d));
|
deba@862
|
424 |
|
deba@862
|
425 |
int rn = merge_roots[node].front();
|
deba@862
|
426 |
|
deba@862
|
427 |
int xn = node_data[rn].next;
|
deba@862
|
428 |
Node xnode = order_list[xn];
|
deba@862
|
429 |
|
deba@862
|
430 |
int yn = node_data[rn].prev;
|
deba@862
|
431 |
Node ynode = order_list[yn];
|
deba@862
|
432 |
|
deba@862
|
433 |
bool rd;
|
alpar@956
|
434 |
if (!external(xnode, rorder, child_lists,
|
deba@862
|
435 |
ancestor_map, low_map)) {
|
deba@862
|
436 |
rd = true;
|
deba@862
|
437 |
} else if (!external(ynode, rorder, child_lists,
|
deba@862
|
438 |
ancestor_map, low_map)) {
|
deba@862
|
439 |
rd = false;
|
deba@862
|
440 |
} else if (pertinent(xnode, embed_arc, merge_roots)) {
|
deba@862
|
441 |
rd = true;
|
deba@862
|
442 |
} else {
|
deba@862
|
443 |
rd = false;
|
deba@862
|
444 |
}
|
deba@862
|
445 |
|
deba@862
|
446 |
merge_stack.push_back(std::make_pair(rn, rd));
|
deba@862
|
447 |
|
deba@862
|
448 |
pn = rn;
|
deba@862
|
449 |
n = rd ? xn : yn;
|
deba@862
|
450 |
|
deba@862
|
451 |
} else if (!external(node, rorder, child_lists,
|
deba@862
|
452 |
ancestor_map, low_map)) {
|
deba@862
|
453 |
int nn = (node_data[n].next != pn ?
|
deba@862
|
454 |
node_data[n].next : node_data[n].prev);
|
deba@862
|
455 |
|
deba@862
|
456 |
bool nd = n == node_data[nn].prev;
|
deba@862
|
457 |
|
deba@862
|
458 |
if (nd) node_data[nn].prev = pn;
|
deba@862
|
459 |
else node_data[nn].next = pn;
|
deba@862
|
460 |
|
deba@862
|
461 |
if (n == node_data[pn].prev) node_data[pn].prev = nn;
|
deba@862
|
462 |
else node_data[pn].next = nn;
|
deba@862
|
463 |
|
deba@862
|
464 |
node_data[nn].inverted =
|
deba@862
|
465 |
(node_data[nn].prev == node_data[nn].next && nd != rd);
|
deba@862
|
466 |
|
deba@862
|
467 |
n = nn;
|
deba@862
|
468 |
}
|
deba@862
|
469 |
else break;
|
deba@862
|
470 |
|
deba@862
|
471 |
}
|
deba@862
|
472 |
|
deba@862
|
473 |
if (!merge_stack.empty() || n == rn) {
|
deba@862
|
474 |
break;
|
deba@862
|
475 |
}
|
deba@862
|
476 |
}
|
deba@862
|
477 |
}
|
deba@862
|
478 |
|
deba@862
|
479 |
void initFace(const Node& node, NodeData& node_data,
|
deba@862
|
480 |
const OrderMap& order_map, const OrderList& order_list) {
|
deba@862
|
481 |
int n = order_map[node];
|
deba@862
|
482 |
int rn = n + order_list.size();
|
deba@862
|
483 |
|
deba@862
|
484 |
node_data[n].next = node_data[n].prev = rn;
|
deba@862
|
485 |
node_data[rn].next = node_data[rn].prev = n;
|
deba@862
|
486 |
|
deba@862
|
487 |
node_data[n].visited = order_list.size();
|
deba@862
|
488 |
node_data[rn].visited = order_list.size();
|
deba@862
|
489 |
|
deba@862
|
490 |
}
|
deba@862
|
491 |
|
deba@862
|
492 |
bool external(const Node& node, int rorder,
|
deba@862
|
493 |
ChildLists& child_lists, AncestorMap& ancestor_map,
|
deba@862
|
494 |
LowMap& low_map) {
|
deba@862
|
495 |
Node child = child_lists[node].first;
|
deba@862
|
496 |
|
deba@862
|
497 |
if (child != INVALID) {
|
deba@862
|
498 |
if (low_map[child] < rorder) return true;
|
deba@862
|
499 |
}
|
deba@862
|
500 |
|
deba@862
|
501 |
if (ancestor_map[node] < rorder) return true;
|
deba@862
|
502 |
|
deba@862
|
503 |
return false;
|
deba@862
|
504 |
}
|
deba@862
|
505 |
|
deba@862
|
506 |
bool pertinent(const Node& node, const EmbedArc& embed_arc,
|
deba@862
|
507 |
const MergeRoots& merge_roots) {
|
deba@862
|
508 |
return !merge_roots[node].empty() || embed_arc[node];
|
deba@862
|
509 |
}
|
deba@862
|
510 |
|
deba@862
|
511 |
};
|
deba@862
|
512 |
|
deba@861
|
513 |
}
|
deba@861
|
514 |
|
deba@861
|
515 |
/// \ingroup planar
|
deba@861
|
516 |
///
|
deba@861
|
517 |
/// \brief Planarity checking of an undirected simple graph
|
deba@861
|
518 |
///
|
deba@862
|
519 |
/// This function implements the Boyer-Myrvold algorithm for
|
kpeter@896
|
520 |
/// planarity checking of an undirected simple graph. It is a simplified
|
deba@861
|
521 |
/// version of the PlanarEmbedding algorithm class because neither
|
kpeter@896
|
522 |
/// the embedding nor the Kuratowski subdivisons are computed.
|
deba@862
|
523 |
template <typename GR>
|
deba@862
|
524 |
bool checkPlanarity(const GR& graph) {
|
deba@862
|
525 |
_planarity_bits::PlanarityChecking<GR> pc(graph);
|
deba@862
|
526 |
return pc.run();
|
deba@862
|
527 |
}
|
deba@861
|
528 |
|
deba@861
|
529 |
/// \ingroup planar
|
deba@861
|
530 |
///
|
deba@861
|
531 |
/// \brief Planar embedding of an undirected simple graph
|
deba@861
|
532 |
///
|
deba@861
|
533 |
/// This class implements the Boyer-Myrvold algorithm for planar
|
kpeter@896
|
534 |
/// embedding of an undirected simple graph. The planar embedding is an
|
deba@861
|
535 |
/// ordering of the outgoing edges of the nodes, which is a possible
|
deba@861
|
536 |
/// configuration to draw the graph in the plane. If there is not
|
kpeter@896
|
537 |
/// such ordering then the graph contains a K<sub>5</sub> (full graph
|
kpeter@896
|
538 |
/// with 5 nodes) or a K<sub>3,3</sub> (complete bipartite graph on
|
kpeter@896
|
539 |
/// 3 Red and 3 Blue nodes) subdivision.
|
deba@861
|
540 |
///
|
deba@861
|
541 |
/// The current implementation calculates either an embedding or a
|
kpeter@896
|
542 |
/// Kuratowski subdivision. The running time of the algorithm is O(n).
|
kpeter@896
|
543 |
///
|
kpeter@896
|
544 |
/// \see PlanarDrawing, checkPlanarity()
|
deba@861
|
545 |
template <typename Graph>
|
deba@861
|
546 |
class PlanarEmbedding {
|
deba@861
|
547 |
private:
|
deba@861
|
548 |
|
deba@861
|
549 |
TEMPLATE_GRAPH_TYPEDEFS(Graph);
|
deba@861
|
550 |
|
deba@861
|
551 |
const Graph& _graph;
|
deba@861
|
552 |
typename Graph::template ArcMap<Arc> _embedding;
|
deba@861
|
553 |
|
deba@861
|
554 |
typename Graph::template EdgeMap<bool> _kuratowski;
|
deba@861
|
555 |
|
deba@861
|
556 |
private:
|
deba@861
|
557 |
|
deba@861
|
558 |
typedef typename Graph::template NodeMap<Arc> PredMap;
|
deba@861
|
559 |
|
deba@861
|
560 |
typedef typename Graph::template EdgeMap<bool> TreeMap;
|
deba@861
|
561 |
|
deba@861
|
562 |
typedef typename Graph::template NodeMap<int> OrderMap;
|
deba@861
|
563 |
typedef std::vector<Node> OrderList;
|
deba@861
|
564 |
|
deba@861
|
565 |
typedef typename Graph::template NodeMap<int> LowMap;
|
deba@861
|
566 |
typedef typename Graph::template NodeMap<int> AncestorMap;
|
deba@861
|
567 |
|
deba@861
|
568 |
typedef _planarity_bits::NodeDataNode<Graph> NodeDataNode;
|
deba@861
|
569 |
typedef std::vector<NodeDataNode> NodeData;
|
deba@861
|
570 |
|
deba@861
|
571 |
typedef _planarity_bits::ChildListNode<Graph> ChildListNode;
|
deba@861
|
572 |
typedef typename Graph::template NodeMap<ChildListNode> ChildLists;
|
deba@861
|
573 |
|
deba@861
|
574 |
typedef typename Graph::template NodeMap<std::list<int> > MergeRoots;
|
deba@861
|
575 |
|
deba@861
|
576 |
typedef typename Graph::template NodeMap<Arc> EmbedArc;
|
deba@861
|
577 |
|
deba@861
|
578 |
typedef _planarity_bits::ArcListNode<Graph> ArcListNode;
|
deba@861
|
579 |
typedef typename Graph::template ArcMap<ArcListNode> ArcLists;
|
deba@861
|
580 |
|
deba@861
|
581 |
typedef typename Graph::template NodeMap<bool> FlipMap;
|
deba@861
|
582 |
|
deba@861
|
583 |
typedef typename Graph::template NodeMap<int> TypeMap;
|
deba@861
|
584 |
|
deba@861
|
585 |
enum IsolatorNodeType {
|
deba@861
|
586 |
HIGHX = 6, LOWX = 7,
|
deba@861
|
587 |
HIGHY = 8, LOWY = 9,
|
deba@861
|
588 |
ROOT = 10, PERTINENT = 11,
|
deba@861
|
589 |
INTERNAL = 12
|
deba@861
|
590 |
};
|
deba@861
|
591 |
|
deba@861
|
592 |
public:
|
deba@861
|
593 |
|
kpeter@896
|
594 |
/// \brief The map type for storing the embedding
|
kpeter@896
|
595 |
///
|
kpeter@896
|
596 |
/// The map type for storing the embedding.
|
kpeter@896
|
597 |
/// \see embeddingMap()
|
deba@861
|
598 |
typedef typename Graph::template ArcMap<Arc> EmbeddingMap;
|
deba@861
|
599 |
|
deba@861
|
600 |
/// \brief Constructor
|
deba@861
|
601 |
///
|
kpeter@896
|
602 |
/// Constructor.
|
kpeter@896
|
603 |
/// \pre The graph must be simple, i.e. it should not
|
kpeter@896
|
604 |
/// contain parallel or loop arcs.
|
deba@861
|
605 |
PlanarEmbedding(const Graph& graph)
|
deba@861
|
606 |
: _graph(graph), _embedding(_graph), _kuratowski(graph, false) {}
|
deba@861
|
607 |
|
kpeter@896
|
608 |
/// \brief Run the algorithm.
|
deba@861
|
609 |
///
|
kpeter@896
|
610 |
/// This function runs the algorithm.
|
kpeter@896
|
611 |
/// \param kuratowski If this parameter is set to \c false, then the
|
deba@861
|
612 |
/// algorithm does not compute a Kuratowski subdivision.
|
kpeter@896
|
613 |
/// \return \c true if the graph is planar.
|
deba@861
|
614 |
bool run(bool kuratowski = true) {
|
deba@861
|
615 |
typedef _planarity_bits::PlanarityVisitor<Graph> Visitor;
|
deba@861
|
616 |
|
deba@861
|
617 |
PredMap pred_map(_graph, INVALID);
|
deba@861
|
618 |
TreeMap tree_map(_graph, false);
|
deba@861
|
619 |
|
deba@861
|
620 |
OrderMap order_map(_graph, -1);
|
deba@861
|
621 |
OrderList order_list;
|
deba@861
|
622 |
|
deba@861
|
623 |
AncestorMap ancestor_map(_graph, -1);
|
deba@861
|
624 |
LowMap low_map(_graph, -1);
|
deba@861
|
625 |
|
deba@861
|
626 |
Visitor visitor(_graph, pred_map, tree_map,
|
deba@861
|
627 |
order_map, order_list, ancestor_map, low_map);
|
deba@861
|
628 |
DfsVisit<Graph, Visitor> visit(_graph, visitor);
|
deba@861
|
629 |
visit.run();
|
deba@861
|
630 |
|
deba@861
|
631 |
ChildLists child_lists(_graph);
|
deba@861
|
632 |
createChildLists(tree_map, order_map, low_map, child_lists);
|
deba@861
|
633 |
|
deba@861
|
634 |
NodeData node_data(2 * order_list.size());
|
deba@861
|
635 |
|
deba@861
|
636 |
EmbedArc embed_arc(_graph, INVALID);
|
deba@861
|
637 |
|
deba@861
|
638 |
MergeRoots merge_roots(_graph);
|
deba@861
|
639 |
|
deba@861
|
640 |
ArcLists arc_lists(_graph);
|
deba@861
|
641 |
|
deba@861
|
642 |
FlipMap flip_map(_graph, false);
|
deba@861
|
643 |
|
deba@861
|
644 |
for (int i = order_list.size() - 1; i >= 0; --i) {
|
deba@861
|
645 |
|
deba@861
|
646 |
Node node = order_list[i];
|
deba@861
|
647 |
|
deba@861
|
648 |
node_data[i].first = INVALID;
|
deba@861
|
649 |
|
deba@861
|
650 |
Node source = node;
|
deba@861
|
651 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
|
deba@861
|
652 |
Node target = _graph.target(e);
|
deba@861
|
653 |
|
deba@861
|
654 |
if (order_map[source] < order_map[target] && tree_map[e]) {
|
deba@861
|
655 |
initFace(target, arc_lists, node_data,
|
deba@861
|
656 |
pred_map, order_map, order_list);
|
deba@861
|
657 |
}
|
deba@861
|
658 |
}
|
deba@861
|
659 |
|
deba@861
|
660 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
|
deba@861
|
661 |
Node target = _graph.target(e);
|
deba@861
|
662 |
|
deba@861
|
663 |
if (order_map[source] < order_map[target] && !tree_map[e]) {
|
deba@861
|
664 |
embed_arc[target] = e;
|
deba@861
|
665 |
walkUp(target, source, i, pred_map, low_map,
|
deba@861
|
666 |
order_map, order_list, node_data, merge_roots);
|
deba@861
|
667 |
}
|
deba@861
|
668 |
}
|
deba@861
|
669 |
|
deba@861
|
670 |
for (typename MergeRoots::Value::iterator it =
|
deba@861
|
671 |
merge_roots[node].begin(); it != merge_roots[node].end(); ++it) {
|
deba@861
|
672 |
int rn = *it;
|
deba@861
|
673 |
walkDown(rn, i, node_data, arc_lists, flip_map, order_list,
|
deba@861
|
674 |
child_lists, ancestor_map, low_map, embed_arc, merge_roots);
|
deba@861
|
675 |
}
|
deba@861
|
676 |
merge_roots[node].clear();
|
deba@861
|
677 |
|
deba@861
|
678 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
|
deba@861
|
679 |
Node target = _graph.target(e);
|
deba@861
|
680 |
|
deba@861
|
681 |
if (order_map[source] < order_map[target] && !tree_map[e]) {
|
deba@861
|
682 |
if (embed_arc[target] != INVALID) {
|
deba@861
|
683 |
if (kuratowski) {
|
deba@861
|
684 |
isolateKuratowski(e, node_data, arc_lists, flip_map,
|
deba@861
|
685 |
order_map, order_list, pred_map, child_lists,
|
deba@861
|
686 |
ancestor_map, low_map,
|
deba@861
|
687 |
embed_arc, merge_roots);
|
deba@861
|
688 |
}
|
deba@861
|
689 |
return false;
|
deba@861
|
690 |
}
|
deba@861
|
691 |
}
|
deba@861
|
692 |
}
|
deba@861
|
693 |
}
|
deba@861
|
694 |
|
deba@861
|
695 |
for (int i = 0; i < int(order_list.size()); ++i) {
|
deba@861
|
696 |
|
deba@861
|
697 |
mergeRemainingFaces(order_list[i], node_data, order_list, order_map,
|
deba@861
|
698 |
child_lists, arc_lists);
|
deba@861
|
699 |
storeEmbedding(order_list[i], node_data, order_map, pred_map,
|
deba@861
|
700 |
arc_lists, flip_map);
|
deba@861
|
701 |
}
|
deba@861
|
702 |
|
deba@861
|
703 |
return true;
|
deba@861
|
704 |
}
|
deba@861
|
705 |
|
kpeter@896
|
706 |
/// \brief Give back the successor of an arc
|
deba@861
|
707 |
///
|
kpeter@896
|
708 |
/// This function gives back the successor of an arc. It makes
|
deba@861
|
709 |
/// possible to query the cyclic order of the outgoing arcs from
|
deba@861
|
710 |
/// a node.
|
deba@861
|
711 |
Arc next(const Arc& arc) const {
|
deba@861
|
712 |
return _embedding[arc];
|
deba@861
|
713 |
}
|
deba@861
|
714 |
|
kpeter@896
|
715 |
/// \brief Give back the calculated embedding map
|
deba@861
|
716 |
///
|
kpeter@896
|
717 |
/// This function gives back the calculated embedding map, which
|
kpeter@896
|
718 |
/// contains the successor of each arc in the cyclic order of the
|
kpeter@896
|
719 |
/// outgoing arcs of its source node.
|
deba@862
|
720 |
const EmbeddingMap& embeddingMap() const {
|
deba@861
|
721 |
return _embedding;
|
deba@861
|
722 |
}
|
deba@861
|
723 |
|
kpeter@896
|
724 |
/// \brief Give back \c true if the given edge is in the Kuratowski
|
kpeter@896
|
725 |
/// subdivision
|
deba@861
|
726 |
///
|
kpeter@896
|
727 |
/// This function gives back \c true if the given edge is in the found
|
kpeter@896
|
728 |
/// Kuratowski subdivision.
|
kpeter@896
|
729 |
/// \pre The \c run() function must be called with \c true parameter
|
kpeter@896
|
730 |
/// before using this function.
|
kpeter@896
|
731 |
bool kuratowski(const Edge& edge) const {
|
deba@861
|
732 |
return _kuratowski[edge];
|
deba@861
|
733 |
}
|
deba@861
|
734 |
|
deba@861
|
735 |
private:
|
deba@861
|
736 |
|
deba@861
|
737 |
void createChildLists(const TreeMap& tree_map, const OrderMap& order_map,
|
deba@861
|
738 |
const LowMap& low_map, ChildLists& child_lists) {
|
deba@861
|
739 |
|
deba@861
|
740 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@861
|
741 |
Node source = n;
|
deba@861
|
742 |
|
deba@861
|
743 |
std::vector<Node> targets;
|
deba@861
|
744 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
deba@861
|
745 |
Node target = _graph.target(e);
|
deba@861
|
746 |
|
deba@861
|
747 |
if (order_map[source] < order_map[target] && tree_map[e]) {
|
deba@861
|
748 |
targets.push_back(target);
|
deba@861
|
749 |
}
|
deba@861
|
750 |
}
|
deba@861
|
751 |
|
deba@861
|
752 |
if (targets.size() == 0) {
|
deba@861
|
753 |
child_lists[source].first = INVALID;
|
deba@861
|
754 |
} else if (targets.size() == 1) {
|
deba@861
|
755 |
child_lists[source].first = targets[0];
|
deba@861
|
756 |
child_lists[targets[0]].prev = INVALID;
|
deba@861
|
757 |
child_lists[targets[0]].next = INVALID;
|
deba@861
|
758 |
} else {
|
deba@861
|
759 |
radixSort(targets.begin(), targets.end(), mapToFunctor(low_map));
|
deba@861
|
760 |
for (int i = 1; i < int(targets.size()); ++i) {
|
deba@861
|
761 |
child_lists[targets[i]].prev = targets[i - 1];
|
deba@861
|
762 |
child_lists[targets[i - 1]].next = targets[i];
|
deba@861
|
763 |
}
|
deba@861
|
764 |
child_lists[targets.back()].next = INVALID;
|
deba@861
|
765 |
child_lists[targets.front()].prev = INVALID;
|
deba@861
|
766 |
child_lists[source].first = targets.front();
|
deba@861
|
767 |
}
|
deba@861
|
768 |
}
|
deba@861
|
769 |
}
|
deba@861
|
770 |
|
deba@861
|
771 |
void walkUp(const Node& node, Node root, int rorder,
|
deba@861
|
772 |
const PredMap& pred_map, const LowMap& low_map,
|
deba@861
|
773 |
const OrderMap& order_map, const OrderList& order_list,
|
deba@861
|
774 |
NodeData& node_data, MergeRoots& merge_roots) {
|
deba@861
|
775 |
|
deba@861
|
776 |
int na, nb;
|
deba@861
|
777 |
bool da, db;
|
deba@861
|
778 |
|
deba@861
|
779 |
na = nb = order_map[node];
|
deba@861
|
780 |
da = true; db = false;
|
deba@861
|
781 |
|
deba@861
|
782 |
while (true) {
|
deba@861
|
783 |
|
deba@861
|
784 |
if (node_data[na].visited == rorder) break;
|
deba@861
|
785 |
if (node_data[nb].visited == rorder) break;
|
deba@861
|
786 |
|
deba@861
|
787 |
node_data[na].visited = rorder;
|
deba@861
|
788 |
node_data[nb].visited = rorder;
|
deba@861
|
789 |
|
deba@861
|
790 |
int rn = -1;
|
deba@861
|
791 |
|
deba@861
|
792 |
if (na >= int(order_list.size())) {
|
deba@861
|
793 |
rn = na;
|
deba@861
|
794 |
} else if (nb >= int(order_list.size())) {
|
deba@861
|
795 |
rn = nb;
|
deba@861
|
796 |
}
|
deba@861
|
797 |
|
deba@861
|
798 |
if (rn == -1) {
|
deba@861
|
799 |
int nn;
|
deba@861
|
800 |
|
deba@861
|
801 |
nn = da ? node_data[na].prev : node_data[na].next;
|
deba@861
|
802 |
da = node_data[nn].prev != na;
|
deba@861
|
803 |
na = nn;
|
deba@861
|
804 |
|
deba@861
|
805 |
nn = db ? node_data[nb].prev : node_data[nb].next;
|
deba@861
|
806 |
db = node_data[nn].prev != nb;
|
deba@861
|
807 |
nb = nn;
|
deba@861
|
808 |
|
deba@861
|
809 |
} else {
|
deba@861
|
810 |
|
deba@861
|
811 |
Node rep = order_list[rn - order_list.size()];
|
deba@861
|
812 |
Node parent = _graph.source(pred_map[rep]);
|
deba@861
|
813 |
|
deba@861
|
814 |
if (low_map[rep] < rorder) {
|
deba@861
|
815 |
merge_roots[parent].push_back(rn);
|
deba@861
|
816 |
} else {
|
deba@861
|
817 |
merge_roots[parent].push_front(rn);
|
deba@861
|
818 |
}
|
deba@861
|
819 |
|
deba@861
|
820 |
if (parent != root) {
|
deba@861
|
821 |
na = nb = order_map[parent];
|
deba@861
|
822 |
da = true; db = false;
|
deba@861
|
823 |
} else {
|
deba@861
|
824 |
break;
|
deba@861
|
825 |
}
|
deba@861
|
826 |
}
|
deba@861
|
827 |
}
|
deba@861
|
828 |
}
|
deba@861
|
829 |
|
deba@861
|
830 |
void walkDown(int rn, int rorder, NodeData& node_data,
|
deba@861
|
831 |
ArcLists& arc_lists, FlipMap& flip_map,
|
deba@861
|
832 |
OrderList& order_list, ChildLists& child_lists,
|
deba@861
|
833 |
AncestorMap& ancestor_map, LowMap& low_map,
|
deba@861
|
834 |
EmbedArc& embed_arc, MergeRoots& merge_roots) {
|
deba@861
|
835 |
|
deba@861
|
836 |
std::vector<std::pair<int, bool> > merge_stack;
|
deba@861
|
837 |
|
deba@861
|
838 |
for (int di = 0; di < 2; ++di) {
|
deba@861
|
839 |
bool rd = di == 0;
|
deba@861
|
840 |
int pn = rn;
|
deba@861
|
841 |
int n = rd ? node_data[rn].next : node_data[rn].prev;
|
deba@861
|
842 |
|
deba@861
|
843 |
while (n != rn) {
|
deba@861
|
844 |
|
deba@861
|
845 |
Node node = order_list[n];
|
deba@861
|
846 |
|
deba@861
|
847 |
if (embed_arc[node] != INVALID) {
|
deba@861
|
848 |
|
deba@861
|
849 |
// Merging components on the critical path
|
deba@861
|
850 |
while (!merge_stack.empty()) {
|
deba@861
|
851 |
|
deba@861
|
852 |
// Component root
|
deba@861
|
853 |
int cn = merge_stack.back().first;
|
deba@861
|
854 |
bool cd = merge_stack.back().second;
|
deba@861
|
855 |
merge_stack.pop_back();
|
deba@861
|
856 |
|
deba@861
|
857 |
// Parent of component
|
deba@861
|
858 |
int dn = merge_stack.back().first;
|
deba@861
|
859 |
bool dd = merge_stack.back().second;
|
deba@861
|
860 |
merge_stack.pop_back();
|
deba@861
|
861 |
|
deba@861
|
862 |
Node parent = order_list[dn];
|
deba@861
|
863 |
|
deba@861
|
864 |
// Erasing from merge_roots
|
deba@861
|
865 |
merge_roots[parent].pop_front();
|
deba@861
|
866 |
|
deba@861
|
867 |
Node child = order_list[cn - order_list.size()];
|
deba@861
|
868 |
|
deba@861
|
869 |
// Erasing from child_lists
|
deba@861
|
870 |
if (child_lists[child].prev != INVALID) {
|
deba@861
|
871 |
child_lists[child_lists[child].prev].next =
|
deba@861
|
872 |
child_lists[child].next;
|
deba@861
|
873 |
} else {
|
deba@861
|
874 |
child_lists[parent].first = child_lists[child].next;
|
deba@861
|
875 |
}
|
deba@861
|
876 |
|
deba@861
|
877 |
if (child_lists[child].next != INVALID) {
|
deba@861
|
878 |
child_lists[child_lists[child].next].prev =
|
deba@861
|
879 |
child_lists[child].prev;
|
deba@861
|
880 |
}
|
deba@861
|
881 |
|
deba@861
|
882 |
// Merging arcs + flipping
|
deba@861
|
883 |
Arc de = node_data[dn].first;
|
deba@861
|
884 |
Arc ce = node_data[cn].first;
|
deba@861
|
885 |
|
deba@861
|
886 |
flip_map[order_list[cn - order_list.size()]] = cd != dd;
|
deba@861
|
887 |
if (cd != dd) {
|
deba@861
|
888 |
std::swap(arc_lists[ce].prev, arc_lists[ce].next);
|
deba@861
|
889 |
ce = arc_lists[ce].prev;
|
deba@861
|
890 |
std::swap(arc_lists[ce].prev, arc_lists[ce].next);
|
deba@861
|
891 |
}
|
deba@861
|
892 |
|
deba@861
|
893 |
{
|
deba@861
|
894 |
Arc dne = arc_lists[de].next;
|
deba@861
|
895 |
Arc cne = arc_lists[ce].next;
|
deba@861
|
896 |
|
deba@861
|
897 |
arc_lists[de].next = cne;
|
deba@861
|
898 |
arc_lists[ce].next = dne;
|
deba@861
|
899 |
|
deba@861
|
900 |
arc_lists[dne].prev = ce;
|
deba@861
|
901 |
arc_lists[cne].prev = de;
|
deba@861
|
902 |
}
|
deba@861
|
903 |
|
deba@861
|
904 |
if (dd) {
|
deba@861
|
905 |
node_data[dn].first = ce;
|
deba@861
|
906 |
}
|
deba@861
|
907 |
|
deba@861
|
908 |
// Merging external faces
|
deba@861
|
909 |
{
|
deba@861
|
910 |
int en = cn;
|
deba@861
|
911 |
cn = cd ? node_data[cn].prev : node_data[cn].next;
|
deba@861
|
912 |
cd = node_data[cn].next == en;
|
deba@861
|
913 |
|
deba@861
|
914 |
if (node_data[cn].prev == node_data[cn].next &&
|
deba@861
|
915 |
node_data[cn].inverted) {
|
deba@861
|
916 |
cd = !cd;
|
deba@861
|
917 |
}
|
deba@861
|
918 |
}
|
deba@861
|
919 |
|
deba@861
|
920 |
if (cd) node_data[cn].next = dn; else node_data[cn].prev = dn;
|
deba@861
|
921 |
if (dd) node_data[dn].prev = cn; else node_data[dn].next = cn;
|
deba@861
|
922 |
|
deba@861
|
923 |
}
|
deba@861
|
924 |
|
deba@861
|
925 |
bool d = pn == node_data[n].prev;
|
deba@861
|
926 |
|
deba@861
|
927 |
if (node_data[n].prev == node_data[n].next &&
|
deba@861
|
928 |
node_data[n].inverted) {
|
deba@861
|
929 |
d = !d;
|
deba@861
|
930 |
}
|
deba@861
|
931 |
|
deba@861
|
932 |
// Add new arc
|
deba@861
|
933 |
{
|
deba@861
|
934 |
Arc arc = embed_arc[node];
|
deba@861
|
935 |
Arc re = node_data[rn].first;
|
deba@861
|
936 |
|
deba@861
|
937 |
arc_lists[arc_lists[re].next].prev = arc;
|
deba@861
|
938 |
arc_lists[arc].next = arc_lists[re].next;
|
deba@861
|
939 |
arc_lists[arc].prev = re;
|
deba@861
|
940 |
arc_lists[re].next = arc;
|
deba@861
|
941 |
|
deba@861
|
942 |
if (!rd) {
|
deba@861
|
943 |
node_data[rn].first = arc;
|
deba@861
|
944 |
}
|
deba@861
|
945 |
|
deba@861
|
946 |
Arc rev = _graph.oppositeArc(arc);
|
deba@861
|
947 |
Arc e = node_data[n].first;
|
deba@861
|
948 |
|
deba@861
|
949 |
arc_lists[arc_lists[e].next].prev = rev;
|
deba@861
|
950 |
arc_lists[rev].next = arc_lists[e].next;
|
deba@861
|
951 |
arc_lists[rev].prev = e;
|
deba@861
|
952 |
arc_lists[e].next = rev;
|
deba@861
|
953 |
|
deba@861
|
954 |
if (d) {
|
deba@861
|
955 |
node_data[n].first = rev;
|
deba@861
|
956 |
}
|
deba@861
|
957 |
|
deba@861
|
958 |
}
|
deba@861
|
959 |
|
deba@861
|
960 |
// Embedding arc into external face
|
deba@861
|
961 |
if (rd) node_data[rn].next = n; else node_data[rn].prev = n;
|
deba@861
|
962 |
if (d) node_data[n].prev = rn; else node_data[n].next = rn;
|
deba@861
|
963 |
pn = rn;
|
deba@861
|
964 |
|
deba@861
|
965 |
embed_arc[order_list[n]] = INVALID;
|
deba@861
|
966 |
}
|
deba@861
|
967 |
|
deba@861
|
968 |
if (!merge_roots[node].empty()) {
|
deba@861
|
969 |
|
deba@861
|
970 |
bool d = pn == node_data[n].prev;
|
deba@861
|
971 |
if (node_data[n].prev == node_data[n].next &&
|
deba@861
|
972 |
node_data[n].inverted) {
|
deba@861
|
973 |
d = !d;
|
deba@861
|
974 |
}
|
deba@861
|
975 |
|
deba@861
|
976 |
merge_stack.push_back(std::make_pair(n, d));
|
deba@861
|
977 |
|
deba@861
|
978 |
int rn = merge_roots[node].front();
|
deba@861
|
979 |
|
deba@861
|
980 |
int xn = node_data[rn].next;
|
deba@861
|
981 |
Node xnode = order_list[xn];
|
deba@861
|
982 |
|
deba@861
|
983 |
int yn = node_data[rn].prev;
|
deba@861
|
984 |
Node ynode = order_list[yn];
|
deba@861
|
985 |
|
deba@861
|
986 |
bool rd;
|
deba@861
|
987 |
if (!external(xnode, rorder, child_lists, ancestor_map, low_map)) {
|
deba@861
|
988 |
rd = true;
|
deba@861
|
989 |
} else if (!external(ynode, rorder, child_lists,
|
deba@861
|
990 |
ancestor_map, low_map)) {
|
deba@861
|
991 |
rd = false;
|
deba@861
|
992 |
} else if (pertinent(xnode, embed_arc, merge_roots)) {
|
deba@861
|
993 |
rd = true;
|
deba@861
|
994 |
} else {
|
deba@861
|
995 |
rd = false;
|
deba@861
|
996 |
}
|
deba@861
|
997 |
|
deba@861
|
998 |
merge_stack.push_back(std::make_pair(rn, rd));
|
deba@861
|
999 |
|
deba@861
|
1000 |
pn = rn;
|
deba@861
|
1001 |
n = rd ? xn : yn;
|
deba@861
|
1002 |
|
deba@861
|
1003 |
} else if (!external(node, rorder, child_lists,
|
deba@861
|
1004 |
ancestor_map, low_map)) {
|
deba@861
|
1005 |
int nn = (node_data[n].next != pn ?
|
deba@861
|
1006 |
node_data[n].next : node_data[n].prev);
|
deba@861
|
1007 |
|
deba@861
|
1008 |
bool nd = n == node_data[nn].prev;
|
deba@861
|
1009 |
|
deba@861
|
1010 |
if (nd) node_data[nn].prev = pn;
|
deba@861
|
1011 |
else node_data[nn].next = pn;
|
deba@861
|
1012 |
|
deba@861
|
1013 |
if (n == node_data[pn].prev) node_data[pn].prev = nn;
|
deba@861
|
1014 |
else node_data[pn].next = nn;
|
deba@861
|
1015 |
|
deba@861
|
1016 |
node_data[nn].inverted =
|
deba@861
|
1017 |
(node_data[nn].prev == node_data[nn].next && nd != rd);
|
deba@861
|
1018 |
|
deba@861
|
1019 |
n = nn;
|
deba@861
|
1020 |
}
|
deba@861
|
1021 |
else break;
|
deba@861
|
1022 |
|
deba@861
|
1023 |
}
|
deba@861
|
1024 |
|
deba@861
|
1025 |
if (!merge_stack.empty() || n == rn) {
|
deba@861
|
1026 |
break;
|
deba@861
|
1027 |
}
|
deba@861
|
1028 |
}
|
deba@861
|
1029 |
}
|
deba@861
|
1030 |
|
deba@861
|
1031 |
void initFace(const Node& node, ArcLists& arc_lists,
|
deba@861
|
1032 |
NodeData& node_data, const PredMap& pred_map,
|
deba@861
|
1033 |
const OrderMap& order_map, const OrderList& order_list) {
|
deba@861
|
1034 |
int n = order_map[node];
|
deba@861
|
1035 |
int rn = n + order_list.size();
|
deba@861
|
1036 |
|
deba@861
|
1037 |
node_data[n].next = node_data[n].prev = rn;
|
deba@861
|
1038 |
node_data[rn].next = node_data[rn].prev = n;
|
deba@861
|
1039 |
|
deba@861
|
1040 |
node_data[n].visited = order_list.size();
|
deba@861
|
1041 |
node_data[rn].visited = order_list.size();
|
deba@861
|
1042 |
|
deba@861
|
1043 |
node_data[n].inverted = false;
|
deba@861
|
1044 |
node_data[rn].inverted = false;
|
deba@861
|
1045 |
|
deba@861
|
1046 |
Arc arc = pred_map[node];
|
deba@861
|
1047 |
Arc rev = _graph.oppositeArc(arc);
|
deba@861
|
1048 |
|
deba@861
|
1049 |
node_data[rn].first = arc;
|
deba@861
|
1050 |
node_data[n].first = rev;
|
deba@861
|
1051 |
|
deba@861
|
1052 |
arc_lists[arc].prev = arc;
|
deba@861
|
1053 |
arc_lists[arc].next = arc;
|
deba@861
|
1054 |
|
deba@861
|
1055 |
arc_lists[rev].prev = rev;
|
deba@861
|
1056 |
arc_lists[rev].next = rev;
|
deba@861
|
1057 |
|
deba@861
|
1058 |
}
|
deba@861
|
1059 |
|
deba@861
|
1060 |
void mergeRemainingFaces(const Node& node, NodeData& node_data,
|
deba@861
|
1061 |
OrderList& order_list, OrderMap& order_map,
|
deba@861
|
1062 |
ChildLists& child_lists, ArcLists& arc_lists) {
|
deba@861
|
1063 |
while (child_lists[node].first != INVALID) {
|
deba@861
|
1064 |
int dd = order_map[node];
|
deba@861
|
1065 |
Node child = child_lists[node].first;
|
deba@861
|
1066 |
int cd = order_map[child] + order_list.size();
|
deba@861
|
1067 |
child_lists[node].first = child_lists[child].next;
|
deba@861
|
1068 |
|
deba@861
|
1069 |
Arc de = node_data[dd].first;
|
deba@861
|
1070 |
Arc ce = node_data[cd].first;
|
deba@861
|
1071 |
|
deba@861
|
1072 |
if (de != INVALID) {
|
deba@861
|
1073 |
Arc dne = arc_lists[de].next;
|
deba@861
|
1074 |
Arc cne = arc_lists[ce].next;
|
deba@861
|
1075 |
|
deba@861
|
1076 |
arc_lists[de].next = cne;
|
deba@861
|
1077 |
arc_lists[ce].next = dne;
|
deba@861
|
1078 |
|
deba@861
|
1079 |
arc_lists[dne].prev = ce;
|
deba@861
|
1080 |
arc_lists[cne].prev = de;
|
deba@861
|
1081 |
}
|
deba@861
|
1082 |
|
deba@861
|
1083 |
node_data[dd].first = ce;
|
deba@861
|
1084 |
|
deba@861
|
1085 |
}
|
deba@861
|
1086 |
}
|
deba@861
|
1087 |
|
deba@861
|
1088 |
void storeEmbedding(const Node& node, NodeData& node_data,
|
deba@861
|
1089 |
OrderMap& order_map, PredMap& pred_map,
|
deba@861
|
1090 |
ArcLists& arc_lists, FlipMap& flip_map) {
|
deba@861
|
1091 |
|
deba@861
|
1092 |
if (node_data[order_map[node]].first == INVALID) return;
|
deba@861
|
1093 |
|
deba@861
|
1094 |
if (pred_map[node] != INVALID) {
|
deba@861
|
1095 |
Node source = _graph.source(pred_map[node]);
|
deba@861
|
1096 |
flip_map[node] = flip_map[node] != flip_map[source];
|
deba@861
|
1097 |
}
|
deba@861
|
1098 |
|
deba@861
|
1099 |
Arc first = node_data[order_map[node]].first;
|
deba@861
|
1100 |
Arc prev = first;
|
deba@861
|
1101 |
|
deba@861
|
1102 |
Arc arc = flip_map[node] ?
|
deba@861
|
1103 |
arc_lists[prev].prev : arc_lists[prev].next;
|
deba@861
|
1104 |
|
deba@861
|
1105 |
_embedding[prev] = arc;
|
deba@861
|
1106 |
|
deba@861
|
1107 |
while (arc != first) {
|
deba@861
|
1108 |
Arc next = arc_lists[arc].prev == prev ?
|
deba@861
|
1109 |
arc_lists[arc].next : arc_lists[arc].prev;
|
deba@861
|
1110 |
prev = arc; arc = next;
|
deba@861
|
1111 |
_embedding[prev] = arc;
|
deba@861
|
1112 |
}
|
deba@861
|
1113 |
}
|
deba@861
|
1114 |
|
deba@861
|
1115 |
|
deba@861
|
1116 |
bool external(const Node& node, int rorder,
|
deba@861
|
1117 |
ChildLists& child_lists, AncestorMap& ancestor_map,
|
deba@861
|
1118 |
LowMap& low_map) {
|
deba@861
|
1119 |
Node child = child_lists[node].first;
|
deba@861
|
1120 |
|
deba@861
|
1121 |
if (child != INVALID) {
|
deba@861
|
1122 |
if (low_map[child] < rorder) return true;
|
deba@861
|
1123 |
}
|
deba@861
|
1124 |
|
deba@861
|
1125 |
if (ancestor_map[node] < rorder) return true;
|
deba@861
|
1126 |
|
deba@861
|
1127 |
return false;
|
deba@861
|
1128 |
}
|
deba@861
|
1129 |
|
deba@861
|
1130 |
bool pertinent(const Node& node, const EmbedArc& embed_arc,
|
deba@861
|
1131 |
const MergeRoots& merge_roots) {
|
deba@861
|
1132 |
return !merge_roots[node].empty() || embed_arc[node] != INVALID;
|
deba@861
|
1133 |
}
|
deba@861
|
1134 |
|
deba@861
|
1135 |
int lowPoint(const Node& node, OrderMap& order_map, ChildLists& child_lists,
|
deba@861
|
1136 |
AncestorMap& ancestor_map, LowMap& low_map) {
|
deba@861
|
1137 |
int low_point;
|
deba@861
|
1138 |
|
deba@861
|
1139 |
Node child = child_lists[node].first;
|
deba@861
|
1140 |
|
deba@861
|
1141 |
if (child != INVALID) {
|
deba@861
|
1142 |
low_point = low_map[child];
|
deba@861
|
1143 |
} else {
|
deba@861
|
1144 |
low_point = order_map[node];
|
deba@861
|
1145 |
}
|
deba@861
|
1146 |
|
deba@861
|
1147 |
if (low_point > ancestor_map[node]) {
|
deba@861
|
1148 |
low_point = ancestor_map[node];
|
deba@861
|
1149 |
}
|
deba@861
|
1150 |
|
deba@861
|
1151 |
return low_point;
|
deba@861
|
1152 |
}
|
deba@861
|
1153 |
|
deba@861
|
1154 |
int findComponentRoot(Node root, Node node, ChildLists& child_lists,
|
deba@861
|
1155 |
OrderMap& order_map, OrderList& order_list) {
|
deba@861
|
1156 |
|
deba@861
|
1157 |
int order = order_map[root];
|
deba@861
|
1158 |
int norder = order_map[node];
|
deba@861
|
1159 |
|
deba@861
|
1160 |
Node child = child_lists[root].first;
|
deba@861
|
1161 |
while (child != INVALID) {
|
deba@861
|
1162 |
int corder = order_map[child];
|
deba@861
|
1163 |
if (corder > order && corder < norder) {
|
deba@861
|
1164 |
order = corder;
|
deba@861
|
1165 |
}
|
deba@861
|
1166 |
child = child_lists[child].next;
|
deba@861
|
1167 |
}
|
deba@861
|
1168 |
return order + order_list.size();
|
deba@861
|
1169 |
}
|
deba@861
|
1170 |
|
deba@861
|
1171 |
Node findPertinent(Node node, OrderMap& order_map, NodeData& node_data,
|
deba@861
|
1172 |
EmbedArc& embed_arc, MergeRoots& merge_roots) {
|
deba@861
|
1173 |
Node wnode =_graph.target(node_data[order_map[node]].first);
|
deba@861
|
1174 |
while (!pertinent(wnode, embed_arc, merge_roots)) {
|
deba@861
|
1175 |
wnode = _graph.target(node_data[order_map[wnode]].first);
|
deba@861
|
1176 |
}
|
deba@861
|
1177 |
return wnode;
|
deba@861
|
1178 |
}
|
deba@861
|
1179 |
|
deba@861
|
1180 |
|
deba@861
|
1181 |
Node findExternal(Node node, int rorder, OrderMap& order_map,
|
deba@861
|
1182 |
ChildLists& child_lists, AncestorMap& ancestor_map,
|
deba@861
|
1183 |
LowMap& low_map, NodeData& node_data) {
|
deba@861
|
1184 |
Node wnode =_graph.target(node_data[order_map[node]].first);
|
deba@861
|
1185 |
while (!external(wnode, rorder, child_lists, ancestor_map, low_map)) {
|
deba@861
|
1186 |
wnode = _graph.target(node_data[order_map[wnode]].first);
|
deba@861
|
1187 |
}
|
deba@861
|
1188 |
return wnode;
|
deba@861
|
1189 |
}
|
deba@861
|
1190 |
|
deba@861
|
1191 |
void markCommonPath(Node node, int rorder, Node& wnode, Node& znode,
|
deba@861
|
1192 |
OrderList& order_list, OrderMap& order_map,
|
deba@861
|
1193 |
NodeData& node_data, ArcLists& arc_lists,
|
deba@861
|
1194 |
EmbedArc& embed_arc, MergeRoots& merge_roots,
|
deba@861
|
1195 |
ChildLists& child_lists, AncestorMap& ancestor_map,
|
deba@861
|
1196 |
LowMap& low_map) {
|
deba@861
|
1197 |
|
deba@861
|
1198 |
Node cnode = node;
|
deba@861
|
1199 |
Node pred = INVALID;
|
deba@861
|
1200 |
|
deba@861
|
1201 |
while (true) {
|
deba@861
|
1202 |
|
deba@861
|
1203 |
bool pert = pertinent(cnode, embed_arc, merge_roots);
|
deba@861
|
1204 |
bool ext = external(cnode, rorder, child_lists, ancestor_map, low_map);
|
deba@861
|
1205 |
|
deba@861
|
1206 |
if (pert && ext) {
|
deba@861
|
1207 |
if (!merge_roots[cnode].empty()) {
|
deba@861
|
1208 |
int cn = merge_roots[cnode].back();
|
deba@861
|
1209 |
|
deba@861
|
1210 |
if (low_map[order_list[cn - order_list.size()]] < rorder) {
|
deba@861
|
1211 |
Arc arc = node_data[cn].first;
|
deba@861
|
1212 |
_kuratowski.set(arc, true);
|
deba@861
|
1213 |
|
deba@861
|
1214 |
pred = cnode;
|
deba@861
|
1215 |
cnode = _graph.target(arc);
|
deba@861
|
1216 |
|
deba@861
|
1217 |
continue;
|
deba@861
|
1218 |
}
|
deba@861
|
1219 |
}
|
deba@861
|
1220 |
wnode = znode = cnode;
|
deba@861
|
1221 |
return;
|
deba@861
|
1222 |
|
deba@861
|
1223 |
} else if (pert) {
|
deba@861
|
1224 |
wnode = cnode;
|
deba@861
|
1225 |
|
deba@861
|
1226 |
while (!external(cnode, rorder, child_lists, ancestor_map, low_map)) {
|
deba@861
|
1227 |
Arc arc = node_data[order_map[cnode]].first;
|
deba@861
|
1228 |
|
deba@861
|
1229 |
if (_graph.target(arc) == pred) {
|
deba@861
|
1230 |
arc = arc_lists[arc].next;
|
deba@861
|
1231 |
}
|
deba@861
|
1232 |
_kuratowski.set(arc, true);
|
deba@861
|
1233 |
|
deba@861
|
1234 |
Node next = _graph.target(arc);
|
deba@861
|
1235 |
pred = cnode; cnode = next;
|
deba@861
|
1236 |
}
|
deba@861
|
1237 |
|
deba@861
|
1238 |
znode = cnode;
|
deba@861
|
1239 |
return;
|
deba@861
|
1240 |
|
deba@861
|
1241 |
} else if (ext) {
|
deba@861
|
1242 |
znode = cnode;
|
deba@861
|
1243 |
|
deba@861
|
1244 |
while (!pertinent(cnode, embed_arc, merge_roots)) {
|
deba@861
|
1245 |
Arc arc = node_data[order_map[cnode]].first;
|
deba@861
|
1246 |
|
deba@861
|
1247 |
if (_graph.target(arc) == pred) {
|
deba@861
|
1248 |
arc = arc_lists[arc].next;
|
deba@861
|
1249 |
}
|
deba@861
|
1250 |
_kuratowski.set(arc, true);
|
deba@861
|
1251 |
|
deba@861
|
1252 |
Node next = _graph.target(arc);
|
deba@861
|
1253 |
pred = cnode; cnode = next;
|
deba@861
|
1254 |
}
|
deba@861
|
1255 |
|
deba@861
|
1256 |
wnode = cnode;
|
deba@861
|
1257 |
return;
|
deba@861
|
1258 |
|
deba@861
|
1259 |
} else {
|
deba@861
|
1260 |
Arc arc = node_data[order_map[cnode]].first;
|
deba@861
|
1261 |
|
deba@861
|
1262 |
if (_graph.target(arc) == pred) {
|
deba@861
|
1263 |
arc = arc_lists[arc].next;
|
deba@861
|
1264 |
}
|
deba@861
|
1265 |
_kuratowski.set(arc, true);
|
deba@861
|
1266 |
|
deba@861
|
1267 |
Node next = _graph.target(arc);
|
deba@861
|
1268 |
pred = cnode; cnode = next;
|
deba@861
|
1269 |
}
|
deba@861
|
1270 |
|
deba@861
|
1271 |
}
|
deba@861
|
1272 |
|
deba@861
|
1273 |
}
|
deba@861
|
1274 |
|
deba@861
|
1275 |
void orientComponent(Node root, int rn, OrderMap& order_map,
|
deba@861
|
1276 |
PredMap& pred_map, NodeData& node_data,
|
deba@861
|
1277 |
ArcLists& arc_lists, FlipMap& flip_map,
|
deba@861
|
1278 |
TypeMap& type_map) {
|
deba@861
|
1279 |
node_data[order_map[root]].first = node_data[rn].first;
|
deba@861
|
1280 |
type_map[root] = 1;
|
deba@861
|
1281 |
|
deba@861
|
1282 |
std::vector<Node> st, qu;
|
deba@861
|
1283 |
|
deba@861
|
1284 |
st.push_back(root);
|
deba@861
|
1285 |
while (!st.empty()) {
|
deba@861
|
1286 |
Node node = st.back();
|
deba@861
|
1287 |
st.pop_back();
|
deba@861
|
1288 |
qu.push_back(node);
|
deba@861
|
1289 |
|
deba@861
|
1290 |
Arc arc = node_data[order_map[node]].first;
|
deba@861
|
1291 |
|
deba@861
|
1292 |
if (type_map[_graph.target(arc)] == 0) {
|
deba@861
|
1293 |
st.push_back(_graph.target(arc));
|
deba@861
|
1294 |
type_map[_graph.target(arc)] = 1;
|
deba@861
|
1295 |
}
|
deba@861
|
1296 |
|
deba@861
|
1297 |
Arc last = arc, pred = arc;
|
deba@861
|
1298 |
arc = arc_lists[arc].next;
|
deba@861
|
1299 |
while (arc != last) {
|
deba@861
|
1300 |
|
deba@861
|
1301 |
if (type_map[_graph.target(arc)] == 0) {
|
deba@861
|
1302 |
st.push_back(_graph.target(arc));
|
deba@861
|
1303 |
type_map[_graph.target(arc)] = 1;
|
deba@861
|
1304 |
}
|
deba@861
|
1305 |
|
deba@861
|
1306 |
Arc next = arc_lists[arc].next != pred ?
|
deba@861
|
1307 |
arc_lists[arc].next : arc_lists[arc].prev;
|
deba@861
|
1308 |
pred = arc; arc = next;
|
deba@861
|
1309 |
}
|
deba@861
|
1310 |
|
deba@861
|
1311 |
}
|
deba@861
|
1312 |
|
deba@861
|
1313 |
type_map[root] = 2;
|
deba@861
|
1314 |
flip_map[root] = false;
|
deba@861
|
1315 |
|
deba@861
|
1316 |
for (int i = 1; i < int(qu.size()); ++i) {
|
deba@861
|
1317 |
|
deba@861
|
1318 |
Node node = qu[i];
|
deba@861
|
1319 |
|
deba@861
|
1320 |
while (type_map[node] != 2) {
|
deba@861
|
1321 |
st.push_back(node);
|
deba@861
|
1322 |
type_map[node] = 2;
|
deba@861
|
1323 |
node = _graph.source(pred_map[node]);
|
deba@861
|
1324 |
}
|
deba@861
|
1325 |
|
deba@861
|
1326 |
bool flip = flip_map[node];
|
deba@861
|
1327 |
|
deba@861
|
1328 |
while (!st.empty()) {
|
deba@861
|
1329 |
node = st.back();
|
deba@861
|
1330 |
st.pop_back();
|
deba@861
|
1331 |
|
deba@861
|
1332 |
flip_map[node] = flip != flip_map[node];
|
deba@861
|
1333 |
flip = flip_map[node];
|
deba@861
|
1334 |
|
deba@861
|
1335 |
if (flip) {
|
deba@861
|
1336 |
Arc arc = node_data[order_map[node]].first;
|
deba@861
|
1337 |
std::swap(arc_lists[arc].prev, arc_lists[arc].next);
|
deba@861
|
1338 |
arc = arc_lists[arc].prev;
|
deba@861
|
1339 |
std::swap(arc_lists[arc].prev, arc_lists[arc].next);
|
deba@861
|
1340 |
node_data[order_map[node]].first = arc;
|
deba@861
|
1341 |
}
|
deba@861
|
1342 |
}
|
deba@861
|
1343 |
}
|
deba@861
|
1344 |
|
deba@861
|
1345 |
for (int i = 0; i < int(qu.size()); ++i) {
|
deba@861
|
1346 |
|
deba@861
|
1347 |
Arc arc = node_data[order_map[qu[i]]].first;
|
deba@861
|
1348 |
Arc last = arc, pred = arc;
|
deba@861
|
1349 |
|
deba@861
|
1350 |
arc = arc_lists[arc].next;
|
deba@861
|
1351 |
while (arc != last) {
|
deba@861
|
1352 |
|
deba@861
|
1353 |
if (arc_lists[arc].next == pred) {
|
deba@861
|
1354 |
std::swap(arc_lists[arc].next, arc_lists[arc].prev);
|
deba@861
|
1355 |
}
|
deba@861
|
1356 |
pred = arc; arc = arc_lists[arc].next;
|
deba@861
|
1357 |
}
|
deba@861
|
1358 |
|
deba@861
|
1359 |
}
|
deba@861
|
1360 |
}
|
deba@861
|
1361 |
|
deba@861
|
1362 |
void setFaceFlags(Node root, Node wnode, Node ynode, Node xnode,
|
deba@861
|
1363 |
OrderMap& order_map, NodeData& node_data,
|
deba@861
|
1364 |
TypeMap& type_map) {
|
deba@861
|
1365 |
Node node = _graph.target(node_data[order_map[root]].first);
|
deba@861
|
1366 |
|
deba@861
|
1367 |
while (node != ynode) {
|
deba@861
|
1368 |
type_map[node] = HIGHY;
|
deba@861
|
1369 |
node = _graph.target(node_data[order_map[node]].first);
|
deba@861
|
1370 |
}
|
deba@861
|
1371 |
|
deba@861
|
1372 |
while (node != wnode) {
|
deba@861
|
1373 |
type_map[node] = LOWY;
|
deba@861
|
1374 |
node = _graph.target(node_data[order_map[node]].first);
|
deba@861
|
1375 |
}
|
deba@861
|
1376 |
|
deba@861
|
1377 |
node = _graph.target(node_data[order_map[wnode]].first);
|
deba@861
|
1378 |
|
deba@861
|
1379 |
while (node != xnode) {
|
deba@861
|
1380 |
type_map[node] = LOWX;
|
deba@861
|
1381 |
node = _graph.target(node_data[order_map[node]].first);
|
deba@861
|
1382 |
}
|
deba@861
|
1383 |
type_map[node] = LOWX;
|
deba@861
|
1384 |
|
deba@861
|
1385 |
node = _graph.target(node_data[order_map[xnode]].first);
|
deba@861
|
1386 |
while (node != root) {
|
deba@861
|
1387 |
type_map[node] = HIGHX;
|
deba@861
|
1388 |
node = _graph.target(node_data[order_map[node]].first);
|
deba@861
|
1389 |
}
|
deba@861
|
1390 |
|
deba@861
|
1391 |
type_map[wnode] = PERTINENT;
|
deba@861
|
1392 |
type_map[root] = ROOT;
|
deba@861
|
1393 |
}
|
deba@861
|
1394 |
|
deba@861
|
1395 |
void findInternalPath(std::vector<Arc>& ipath,
|
deba@861
|
1396 |
Node wnode, Node root, TypeMap& type_map,
|
deba@861
|
1397 |
OrderMap& order_map, NodeData& node_data,
|
deba@861
|
1398 |
ArcLists& arc_lists) {
|
deba@861
|
1399 |
std::vector<Arc> st;
|
deba@861
|
1400 |
|
deba@861
|
1401 |
Node node = wnode;
|
deba@861
|
1402 |
|
deba@861
|
1403 |
while (node != root) {
|
deba@861
|
1404 |
Arc arc = arc_lists[node_data[order_map[node]].first].next;
|
deba@861
|
1405 |
st.push_back(arc);
|
deba@861
|
1406 |
node = _graph.target(arc);
|
deba@861
|
1407 |
}
|
deba@861
|
1408 |
|
deba@861
|
1409 |
while (true) {
|
deba@861
|
1410 |
Arc arc = st.back();
|
deba@861
|
1411 |
if (type_map[_graph.target(arc)] == LOWX ||
|
deba@861
|
1412 |
type_map[_graph.target(arc)] == HIGHX) {
|
deba@861
|
1413 |
break;
|
deba@861
|
1414 |
}
|
deba@861
|
1415 |
if (type_map[_graph.target(arc)] == 2) {
|
deba@861
|
1416 |
type_map[_graph.target(arc)] = 3;
|
deba@861
|
1417 |
|
deba@861
|
1418 |
arc = arc_lists[_graph.oppositeArc(arc)].next;
|
deba@861
|
1419 |
st.push_back(arc);
|
deba@861
|
1420 |
} else {
|
deba@861
|
1421 |
st.pop_back();
|
deba@861
|
1422 |
arc = arc_lists[arc].next;
|
deba@861
|
1423 |
|
deba@861
|
1424 |
while (_graph.oppositeArc(arc) == st.back()) {
|
deba@861
|
1425 |
arc = st.back();
|
deba@861
|
1426 |
st.pop_back();
|
deba@861
|
1427 |
arc = arc_lists[arc].next;
|
deba@861
|
1428 |
}
|
deba@861
|
1429 |
st.push_back(arc);
|
deba@861
|
1430 |
}
|
deba@861
|
1431 |
}
|
deba@861
|
1432 |
|
deba@861
|
1433 |
for (int i = 0; i < int(st.size()); ++i) {
|
deba@861
|
1434 |
if (type_map[_graph.target(st[i])] != LOWY &&
|
deba@861
|
1435 |
type_map[_graph.target(st[i])] != HIGHY) {
|
deba@861
|
1436 |
for (; i < int(st.size()); ++i) {
|
deba@861
|
1437 |
ipath.push_back(st[i]);
|
deba@861
|
1438 |
}
|
deba@861
|
1439 |
}
|
deba@861
|
1440 |
}
|
deba@861
|
1441 |
}
|
deba@861
|
1442 |
|
deba@861
|
1443 |
void setInternalFlags(std::vector<Arc>& ipath, TypeMap& type_map) {
|
deba@861
|
1444 |
for (int i = 1; i < int(ipath.size()); ++i) {
|
deba@861
|
1445 |
type_map[_graph.source(ipath[i])] = INTERNAL;
|
deba@861
|
1446 |
}
|
deba@861
|
1447 |
}
|
deba@861
|
1448 |
|
deba@861
|
1449 |
void findPilePath(std::vector<Arc>& ppath,
|
deba@861
|
1450 |
Node root, TypeMap& type_map, OrderMap& order_map,
|
deba@861
|
1451 |
NodeData& node_data, ArcLists& arc_lists) {
|
deba@861
|
1452 |
std::vector<Arc> st;
|
deba@861
|
1453 |
|
deba@861
|
1454 |
st.push_back(_graph.oppositeArc(node_data[order_map[root]].first));
|
deba@861
|
1455 |
st.push_back(node_data[order_map[root]].first);
|
deba@861
|
1456 |
|
deba@861
|
1457 |
while (st.size() > 1) {
|
deba@861
|
1458 |
Arc arc = st.back();
|
deba@861
|
1459 |
if (type_map[_graph.target(arc)] == INTERNAL) {
|
deba@861
|
1460 |
break;
|
deba@861
|
1461 |
}
|
deba@861
|
1462 |
if (type_map[_graph.target(arc)] == 3) {
|
deba@861
|
1463 |
type_map[_graph.target(arc)] = 4;
|
deba@861
|
1464 |
|
deba@861
|
1465 |
arc = arc_lists[_graph.oppositeArc(arc)].next;
|
deba@861
|
1466 |
st.push_back(arc);
|
deba@861
|
1467 |
} else {
|
deba@861
|
1468 |
st.pop_back();
|
deba@861
|
1469 |
arc = arc_lists[arc].next;
|
deba@861
|
1470 |
|
deba@861
|
1471 |
while (!st.empty() && _graph.oppositeArc(arc) == st.back()) {
|
deba@861
|
1472 |
arc = st.back();
|
deba@861
|
1473 |
st.pop_back();
|
deba@861
|
1474 |
arc = arc_lists[arc].next;
|
deba@861
|
1475 |
}
|
deba@861
|
1476 |
st.push_back(arc);
|
deba@861
|
1477 |
}
|
deba@861
|
1478 |
}
|
deba@861
|
1479 |
|
deba@861
|
1480 |
for (int i = 1; i < int(st.size()); ++i) {
|
deba@861
|
1481 |
ppath.push_back(st[i]);
|
deba@861
|
1482 |
}
|
deba@861
|
1483 |
}
|
deba@861
|
1484 |
|
deba@861
|
1485 |
|
deba@861
|
1486 |
int markExternalPath(Node node, OrderMap& order_map,
|
deba@861
|
1487 |
ChildLists& child_lists, PredMap& pred_map,
|
deba@861
|
1488 |
AncestorMap& ancestor_map, LowMap& low_map) {
|
deba@861
|
1489 |
int lp = lowPoint(node, order_map, child_lists,
|
deba@861
|
1490 |
ancestor_map, low_map);
|
deba@861
|
1491 |
|
deba@861
|
1492 |
if (ancestor_map[node] != lp) {
|
deba@861
|
1493 |
node = child_lists[node].first;
|
deba@861
|
1494 |
_kuratowski[pred_map[node]] = true;
|
deba@861
|
1495 |
|
deba@861
|
1496 |
while (ancestor_map[node] != lp) {
|
deba@861
|
1497 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
|
deba@861
|
1498 |
Node tnode = _graph.target(e);
|
deba@861
|
1499 |
if (order_map[tnode] > order_map[node] && low_map[tnode] == lp) {
|
deba@861
|
1500 |
node = tnode;
|
deba@861
|
1501 |
_kuratowski[e] = true;
|
deba@861
|
1502 |
break;
|
deba@861
|
1503 |
}
|
deba@861
|
1504 |
}
|
deba@861
|
1505 |
}
|
deba@861
|
1506 |
}
|
deba@861
|
1507 |
|
deba@861
|
1508 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
|
deba@861
|
1509 |
if (order_map[_graph.target(e)] == lp) {
|
deba@861
|
1510 |
_kuratowski[e] = true;
|
deba@861
|
1511 |
break;
|
deba@861
|
1512 |
}
|
deba@861
|
1513 |
}
|
deba@861
|
1514 |
|
deba@861
|
1515 |
return lp;
|
deba@861
|
1516 |
}
|
deba@861
|
1517 |
|
deba@861
|
1518 |
void markPertinentPath(Node node, OrderMap& order_map,
|
deba@861
|
1519 |
NodeData& node_data, ArcLists& arc_lists,
|
deba@861
|
1520 |
EmbedArc& embed_arc, MergeRoots& merge_roots) {
|
deba@861
|
1521 |
while (embed_arc[node] == INVALID) {
|
deba@861
|
1522 |
int n = merge_roots[node].front();
|
deba@861
|
1523 |
Arc arc = node_data[n].first;
|
deba@861
|
1524 |
|
deba@861
|
1525 |
_kuratowski.set(arc, true);
|
deba@861
|
1526 |
|
deba@861
|
1527 |
Node pred = node;
|
deba@861
|
1528 |
node = _graph.target(arc);
|
deba@861
|
1529 |
while (!pertinent(node, embed_arc, merge_roots)) {
|
deba@861
|
1530 |
arc = node_data[order_map[node]].first;
|
deba@861
|
1531 |
if (_graph.target(arc) == pred) {
|
deba@861
|
1532 |
arc = arc_lists[arc].next;
|
deba@861
|
1533 |
}
|
deba@861
|
1534 |
_kuratowski.set(arc, true);
|
deba@861
|
1535 |
pred = node;
|
deba@861
|
1536 |
node = _graph.target(arc);
|
deba@861
|
1537 |
}
|
deba@861
|
1538 |
}
|
deba@861
|
1539 |
_kuratowski.set(embed_arc[node], true);
|
deba@861
|
1540 |
}
|
deba@861
|
1541 |
|
deba@861
|
1542 |
void markPredPath(Node node, Node snode, PredMap& pred_map) {
|
deba@861
|
1543 |
while (node != snode) {
|
deba@861
|
1544 |
_kuratowski.set(pred_map[node], true);
|
deba@861
|
1545 |
node = _graph.source(pred_map[node]);
|
deba@861
|
1546 |
}
|
deba@861
|
1547 |
}
|
deba@861
|
1548 |
|
deba@861
|
1549 |
void markFacePath(Node ynode, Node xnode,
|
deba@861
|
1550 |
OrderMap& order_map, NodeData& node_data) {
|
deba@861
|
1551 |
Arc arc = node_data[order_map[ynode]].first;
|
deba@861
|
1552 |
Node node = _graph.target(arc);
|
deba@861
|
1553 |
_kuratowski.set(arc, true);
|
deba@861
|
1554 |
|
deba@861
|
1555 |
while (node != xnode) {
|
deba@861
|
1556 |
arc = node_data[order_map[node]].first;
|
deba@861
|
1557 |
_kuratowski.set(arc, true);
|
deba@861
|
1558 |
node = _graph.target(arc);
|
deba@861
|
1559 |
}
|
deba@861
|
1560 |
}
|
deba@861
|
1561 |
|
deba@861
|
1562 |
void markInternalPath(std::vector<Arc>& path) {
|
deba@861
|
1563 |
for (int i = 0; i < int(path.size()); ++i) {
|
deba@861
|
1564 |
_kuratowski.set(path[i], true);
|
deba@861
|
1565 |
}
|
deba@861
|
1566 |
}
|
deba@861
|
1567 |
|
deba@861
|
1568 |
void markPilePath(std::vector<Arc>& path) {
|
deba@861
|
1569 |
for (int i = 0; i < int(path.size()); ++i) {
|
deba@861
|
1570 |
_kuratowski.set(path[i], true);
|
deba@861
|
1571 |
}
|
deba@861
|
1572 |
}
|
deba@861
|
1573 |
|
deba@861
|
1574 |
void isolateKuratowski(Arc arc, NodeData& node_data,
|
deba@861
|
1575 |
ArcLists& arc_lists, FlipMap& flip_map,
|
deba@861
|
1576 |
OrderMap& order_map, OrderList& order_list,
|
deba@861
|
1577 |
PredMap& pred_map, ChildLists& child_lists,
|
deba@861
|
1578 |
AncestorMap& ancestor_map, LowMap& low_map,
|
deba@861
|
1579 |
EmbedArc& embed_arc, MergeRoots& merge_roots) {
|
deba@861
|
1580 |
|
deba@861
|
1581 |
Node root = _graph.source(arc);
|
deba@861
|
1582 |
Node enode = _graph.target(arc);
|
deba@861
|
1583 |
|
deba@861
|
1584 |
int rorder = order_map[root];
|
deba@861
|
1585 |
|
deba@861
|
1586 |
TypeMap type_map(_graph, 0);
|
deba@861
|
1587 |
|
deba@861
|
1588 |
int rn = findComponentRoot(root, enode, child_lists,
|
deba@861
|
1589 |
order_map, order_list);
|
deba@861
|
1590 |
|
deba@861
|
1591 |
Node xnode = order_list[node_data[rn].next];
|
deba@861
|
1592 |
Node ynode = order_list[node_data[rn].prev];
|
deba@861
|
1593 |
|
deba@861
|
1594 |
// Minor-A
|
deba@861
|
1595 |
{
|
deba@861
|
1596 |
while (!merge_roots[xnode].empty() || !merge_roots[ynode].empty()) {
|
deba@861
|
1597 |
|
deba@861
|
1598 |
if (!merge_roots[xnode].empty()) {
|
deba@861
|
1599 |
root = xnode;
|
deba@861
|
1600 |
rn = merge_roots[xnode].front();
|
deba@861
|
1601 |
} else {
|
deba@861
|
1602 |
root = ynode;
|
deba@861
|
1603 |
rn = merge_roots[ynode].front();
|
deba@861
|
1604 |
}
|
deba@861
|
1605 |
|
deba@861
|
1606 |
xnode = order_list[node_data[rn].next];
|
deba@861
|
1607 |
ynode = order_list[node_data[rn].prev];
|
deba@861
|
1608 |
}
|
deba@861
|
1609 |
|
deba@861
|
1610 |
if (root != _graph.source(arc)) {
|
deba@861
|
1611 |
orientComponent(root, rn, order_map, pred_map,
|
deba@861
|
1612 |
node_data, arc_lists, flip_map, type_map);
|
deba@861
|
1613 |
markFacePath(root, root, order_map, node_data);
|
deba@861
|
1614 |
int xlp = markExternalPath(xnode, order_map, child_lists,
|
deba@861
|
1615 |
pred_map, ancestor_map, low_map);
|
deba@861
|
1616 |
int ylp = markExternalPath(ynode, order_map, child_lists,
|
deba@861
|
1617 |
pred_map, ancestor_map, low_map);
|
deba@861
|
1618 |
markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
|
deba@861
|
1619 |
Node lwnode = findPertinent(ynode, order_map, node_data,
|
deba@861
|
1620 |
embed_arc, merge_roots);
|
deba@861
|
1621 |
|
deba@861
|
1622 |
markPertinentPath(lwnode, order_map, node_data, arc_lists,
|
deba@861
|
1623 |
embed_arc, merge_roots);
|
deba@861
|
1624 |
|
deba@861
|
1625 |
return;
|
deba@861
|
1626 |
}
|
deba@861
|
1627 |
}
|
deba@861
|
1628 |
|
deba@861
|
1629 |
orientComponent(root, rn, order_map, pred_map,
|
deba@861
|
1630 |
node_data, arc_lists, flip_map, type_map);
|
deba@861
|
1631 |
|
deba@861
|
1632 |
Node wnode = findPertinent(ynode, order_map, node_data,
|
deba@861
|
1633 |
embed_arc, merge_roots);
|
deba@861
|
1634 |
setFaceFlags(root, wnode, ynode, xnode, order_map, node_data, type_map);
|
deba@861
|
1635 |
|
deba@861
|
1636 |
|
deba@861
|
1637 |
//Minor-B
|
deba@861
|
1638 |
if (!merge_roots[wnode].empty()) {
|
deba@861
|
1639 |
int cn = merge_roots[wnode].back();
|
deba@861
|
1640 |
Node rep = order_list[cn - order_list.size()];
|
deba@861
|
1641 |
if (low_map[rep] < rorder) {
|
deba@861
|
1642 |
markFacePath(root, root, order_map, node_data);
|
deba@861
|
1643 |
int xlp = markExternalPath(xnode, order_map, child_lists,
|
deba@861
|
1644 |
pred_map, ancestor_map, low_map);
|
deba@861
|
1645 |
int ylp = markExternalPath(ynode, order_map, child_lists,
|
deba@861
|
1646 |
pred_map, ancestor_map, low_map);
|
deba@861
|
1647 |
|
deba@861
|
1648 |
Node lwnode, lznode;
|
deba@861
|
1649 |
markCommonPath(wnode, rorder, lwnode, lznode, order_list,
|
deba@861
|
1650 |
order_map, node_data, arc_lists, embed_arc,
|
deba@861
|
1651 |
merge_roots, child_lists, ancestor_map, low_map);
|
deba@861
|
1652 |
|
deba@861
|
1653 |
markPertinentPath(lwnode, order_map, node_data, arc_lists,
|
deba@861
|
1654 |
embed_arc, merge_roots);
|
deba@861
|
1655 |
int zlp = markExternalPath(lznode, order_map, child_lists,
|
deba@861
|
1656 |
pred_map, ancestor_map, low_map);
|
deba@861
|
1657 |
|
deba@861
|
1658 |
int minlp = xlp < ylp ? xlp : ylp;
|
deba@861
|
1659 |
if (zlp < minlp) minlp = zlp;
|
deba@861
|
1660 |
|
deba@861
|
1661 |
int maxlp = xlp > ylp ? xlp : ylp;
|
deba@861
|
1662 |
if (zlp > maxlp) maxlp = zlp;
|
deba@861
|
1663 |
|
deba@861
|
1664 |
markPredPath(order_list[maxlp], order_list[minlp], pred_map);
|
deba@861
|
1665 |
|
deba@861
|
1666 |
return;
|
deba@861
|
1667 |
}
|
deba@861
|
1668 |
}
|
deba@861
|
1669 |
|
deba@861
|
1670 |
Node pxnode, pynode;
|
deba@861
|
1671 |
std::vector<Arc> ipath;
|
deba@861
|
1672 |
findInternalPath(ipath, wnode, root, type_map, order_map,
|
deba@861
|
1673 |
node_data, arc_lists);
|
deba@861
|
1674 |
setInternalFlags(ipath, type_map);
|
deba@861
|
1675 |
pynode = _graph.source(ipath.front());
|
deba@861
|
1676 |
pxnode = _graph.target(ipath.back());
|
deba@861
|
1677 |
|
deba@861
|
1678 |
wnode = findPertinent(pynode, order_map, node_data,
|
deba@861
|
1679 |
embed_arc, merge_roots);
|
deba@861
|
1680 |
|
deba@861
|
1681 |
// Minor-C
|
deba@861
|
1682 |
{
|
deba@861
|
1683 |
if (type_map[_graph.source(ipath.front())] == HIGHY) {
|
deba@861
|
1684 |
if (type_map[_graph.target(ipath.back())] == HIGHX) {
|
deba@861
|
1685 |
markFacePath(xnode, pxnode, order_map, node_data);
|
deba@861
|
1686 |
}
|
deba@861
|
1687 |
markFacePath(root, xnode, order_map, node_data);
|
deba@861
|
1688 |
markPertinentPath(wnode, order_map, node_data, arc_lists,
|
deba@861
|
1689 |
embed_arc, merge_roots);
|
deba@861
|
1690 |
markInternalPath(ipath);
|
deba@861
|
1691 |
int xlp = markExternalPath(xnode, order_map, child_lists,
|
deba@861
|
1692 |
pred_map, ancestor_map, low_map);
|
deba@861
|
1693 |
int ylp = markExternalPath(ynode, order_map, child_lists,
|
deba@861
|
1694 |
pred_map, ancestor_map, low_map);
|
deba@861
|
1695 |
markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
|
deba@861
|
1696 |
return;
|
deba@861
|
1697 |
}
|
deba@861
|
1698 |
|
deba@861
|
1699 |
if (type_map[_graph.target(ipath.back())] == HIGHX) {
|
deba@861
|
1700 |
markFacePath(ynode, root, order_map, node_data);
|
deba@861
|
1701 |
markPertinentPath(wnode, order_map, node_data, arc_lists,
|
deba@861
|
1702 |
embed_arc, merge_roots);
|
deba@861
|
1703 |
markInternalPath(ipath);
|
deba@861
|
1704 |
int xlp = markExternalPath(xnode, order_map, child_lists,
|
deba@861
|
1705 |
pred_map, ancestor_map, low_map);
|
deba@861
|
1706 |
int ylp = markExternalPath(ynode, order_map, child_lists,
|
deba@861
|
1707 |
pred_map, ancestor_map, low_map);
|
deba@861
|
1708 |
markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
|
deba@861
|
1709 |
return;
|
deba@861
|
1710 |
}
|
deba@861
|
1711 |
}
|
deba@861
|
1712 |
|
deba@861
|
1713 |
std::vector<Arc> ppath;
|
deba@861
|
1714 |
findPilePath(ppath, root, type_map, order_map, node_data, arc_lists);
|
deba@861
|
1715 |
|
deba@861
|
1716 |
// Minor-D
|
deba@861
|
1717 |
if (!ppath.empty()) {
|
deba@861
|
1718 |
markFacePath(ynode, xnode, order_map, node_data);
|
deba@861
|
1719 |
markPertinentPath(wnode, order_map, node_data, arc_lists,
|
deba@861
|
1720 |
embed_arc, merge_roots);
|
deba@861
|
1721 |
markPilePath(ppath);
|
deba@861
|
1722 |
markInternalPath(ipath);
|
deba@861
|
1723 |
int xlp = markExternalPath(xnode, order_map, child_lists,
|
deba@861
|
1724 |
pred_map, ancestor_map, low_map);
|
deba@861
|
1725 |
int ylp = markExternalPath(ynode, order_map, child_lists,
|
deba@861
|
1726 |
pred_map, ancestor_map, low_map);
|
deba@861
|
1727 |
markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
|
deba@861
|
1728 |
return;
|
deba@861
|
1729 |
}
|
deba@861
|
1730 |
|
deba@861
|
1731 |
// Minor-E*
|
deba@861
|
1732 |
{
|
deba@861
|
1733 |
|
deba@861
|
1734 |
if (!external(wnode, rorder, child_lists, ancestor_map, low_map)) {
|
deba@861
|
1735 |
Node znode = findExternal(pynode, rorder, order_map,
|
deba@861
|
1736 |
child_lists, ancestor_map,
|
deba@861
|
1737 |
low_map, node_data);
|
deba@861
|
1738 |
|
deba@861
|
1739 |
if (type_map[znode] == LOWY) {
|
deba@861
|
1740 |
markFacePath(root, xnode, order_map, node_data);
|
deba@861
|
1741 |
markPertinentPath(wnode, order_map, node_data, arc_lists,
|
deba@861
|
1742 |
embed_arc, merge_roots);
|
deba@861
|
1743 |
markInternalPath(ipath);
|
deba@861
|
1744 |
int xlp = markExternalPath(xnode, order_map, child_lists,
|
deba@861
|
1745 |
pred_map, ancestor_map, low_map);
|
deba@861
|
1746 |
int zlp = markExternalPath(znode, order_map, child_lists,
|
deba@861
|
1747 |
pred_map, ancestor_map, low_map);
|
deba@861
|
1748 |
markPredPath(root, order_list[xlp < zlp ? xlp : zlp], pred_map);
|
deba@861
|
1749 |
} else {
|
deba@861
|
1750 |
markFacePath(ynode, root, order_map, node_data);
|
deba@861
|
1751 |
markPertinentPath(wnode, order_map, node_data, arc_lists,
|
deba@861
|
1752 |
embed_arc, merge_roots);
|
deba@861
|
1753 |
markInternalPath(ipath);
|
deba@861
|
1754 |
int ylp = markExternalPath(ynode, order_map, child_lists,
|
deba@861
|
1755 |
pred_map, ancestor_map, low_map);
|
deba@861
|
1756 |
int zlp = markExternalPath(znode, order_map, child_lists,
|
deba@861
|
1757 |
pred_map, ancestor_map, low_map);
|
deba@861
|
1758 |
markPredPath(root, order_list[ylp < zlp ? ylp : zlp], pred_map);
|
deba@861
|
1759 |
}
|
deba@861
|
1760 |
return;
|
deba@861
|
1761 |
}
|
deba@861
|
1762 |
|
deba@861
|
1763 |
int xlp = markExternalPath(xnode, order_map, child_lists,
|
deba@861
|
1764 |
pred_map, ancestor_map, low_map);
|
deba@861
|
1765 |
int ylp = markExternalPath(ynode, order_map, child_lists,
|
deba@861
|
1766 |
pred_map, ancestor_map, low_map);
|
deba@861
|
1767 |
int wlp = markExternalPath(wnode, order_map, child_lists,
|
deba@861
|
1768 |
pred_map, ancestor_map, low_map);
|
deba@861
|
1769 |
|
deba@861
|
1770 |
if (wlp > xlp && wlp > ylp) {
|
deba@861
|
1771 |
markFacePath(root, root, order_map, node_data);
|
deba@861
|
1772 |
markPredPath(root, order_list[xlp < ylp ? xlp : ylp], pred_map);
|
deba@861
|
1773 |
return;
|
deba@861
|
1774 |
}
|
deba@861
|
1775 |
|
deba@861
|
1776 |
markInternalPath(ipath);
|
deba@861
|
1777 |
markPertinentPath(wnode, order_map, node_data, arc_lists,
|
deba@861
|
1778 |
embed_arc, merge_roots);
|
deba@861
|
1779 |
|
deba@861
|
1780 |
if (xlp > ylp && xlp > wlp) {
|
deba@861
|
1781 |
markFacePath(root, pynode, order_map, node_data);
|
deba@861
|
1782 |
markFacePath(wnode, xnode, order_map, node_data);
|
deba@861
|
1783 |
markPredPath(root, order_list[ylp < wlp ? ylp : wlp], pred_map);
|
deba@861
|
1784 |
return;
|
deba@861
|
1785 |
}
|
deba@861
|
1786 |
|
deba@861
|
1787 |
if (ylp > xlp && ylp > wlp) {
|
deba@861
|
1788 |
markFacePath(pxnode, root, order_map, node_data);
|
deba@861
|
1789 |
markFacePath(ynode, wnode, order_map, node_data);
|
deba@861
|
1790 |
markPredPath(root, order_list[xlp < wlp ? xlp : wlp], pred_map);
|
deba@861
|
1791 |
return;
|
deba@861
|
1792 |
}
|
deba@861
|
1793 |
|
deba@861
|
1794 |
if (pynode != ynode) {
|
deba@861
|
1795 |
markFacePath(pxnode, wnode, order_map, node_data);
|
deba@861
|
1796 |
|
deba@861
|
1797 |
int minlp = xlp < ylp ? xlp : ylp;
|
deba@861
|
1798 |
if (wlp < minlp) minlp = wlp;
|
deba@861
|
1799 |
|
deba@861
|
1800 |
int maxlp = xlp > ylp ? xlp : ylp;
|
deba@861
|
1801 |
if (wlp > maxlp) maxlp = wlp;
|
deba@861
|
1802 |
|
deba@861
|
1803 |
markPredPath(order_list[maxlp], order_list[minlp], pred_map);
|
deba@861
|
1804 |
return;
|
deba@861
|
1805 |
}
|
deba@861
|
1806 |
|
deba@861
|
1807 |
if (pxnode != xnode) {
|
deba@861
|
1808 |
markFacePath(wnode, pynode, order_map, node_data);
|
deba@861
|
1809 |
|
deba@861
|
1810 |
int minlp = xlp < ylp ? xlp : ylp;
|
deba@861
|
1811 |
if (wlp < minlp) minlp = wlp;
|
deba@861
|
1812 |
|
deba@861
|
1813 |
int maxlp = xlp > ylp ? xlp : ylp;
|
deba@861
|
1814 |
if (wlp > maxlp) maxlp = wlp;
|
deba@861
|
1815 |
|
deba@861
|
1816 |
markPredPath(order_list[maxlp], order_list[minlp], pred_map);
|
deba@861
|
1817 |
return;
|
deba@861
|
1818 |
}
|
deba@861
|
1819 |
|
deba@861
|
1820 |
markFacePath(root, root, order_map, node_data);
|
deba@861
|
1821 |
int minlp = xlp < ylp ? xlp : ylp;
|
deba@861
|
1822 |
if (wlp < minlp) minlp = wlp;
|
deba@861
|
1823 |
markPredPath(root, order_list[minlp], pred_map);
|
deba@861
|
1824 |
return;
|
deba@861
|
1825 |
}
|
deba@861
|
1826 |
|
deba@861
|
1827 |
}
|
deba@861
|
1828 |
|
deba@861
|
1829 |
};
|
deba@861
|
1830 |
|
deba@861
|
1831 |
namespace _planarity_bits {
|
deba@861
|
1832 |
|
deba@861
|
1833 |
template <typename Graph, typename EmbeddingMap>
|
deba@861
|
1834 |
void makeConnected(Graph& graph, EmbeddingMap& embedding) {
|
deba@861
|
1835 |
DfsVisitor<Graph> null_visitor;
|
deba@861
|
1836 |
DfsVisit<Graph, DfsVisitor<Graph> > dfs(graph, null_visitor);
|
deba@861
|
1837 |
dfs.init();
|
deba@861
|
1838 |
|
deba@861
|
1839 |
typename Graph::Node u = INVALID;
|
deba@861
|
1840 |
for (typename Graph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@861
|
1841 |
if (!dfs.reached(n)) {
|
deba@861
|
1842 |
dfs.addSource(n);
|
deba@861
|
1843 |
dfs.start();
|
deba@861
|
1844 |
if (u == INVALID) {
|
deba@861
|
1845 |
u = n;
|
deba@861
|
1846 |
} else {
|
deba@861
|
1847 |
typename Graph::Node v = n;
|
deba@861
|
1848 |
|
deba@861
|
1849 |
typename Graph::Arc ue = typename Graph::OutArcIt(graph, u);
|
deba@861
|
1850 |
typename Graph::Arc ve = typename Graph::OutArcIt(graph, v);
|
deba@861
|
1851 |
|
deba@861
|
1852 |
typename Graph::Arc e = graph.direct(graph.addEdge(u, v), true);
|
deba@861
|
1853 |
|
deba@861
|
1854 |
if (ue != INVALID) {
|
deba@861
|
1855 |
embedding[e] = embedding[ue];
|
deba@861
|
1856 |
embedding[ue] = e;
|
deba@861
|
1857 |
} else {
|
deba@861
|
1858 |
embedding[e] = e;
|
deba@861
|
1859 |
}
|
deba@861
|
1860 |
|
deba@861
|
1861 |
if (ve != INVALID) {
|
deba@861
|
1862 |
embedding[graph.oppositeArc(e)] = embedding[ve];
|
deba@861
|
1863 |
embedding[ve] = graph.oppositeArc(e);
|
deba@861
|
1864 |
} else {
|
deba@861
|
1865 |
embedding[graph.oppositeArc(e)] = graph.oppositeArc(e);
|
deba@861
|
1866 |
}
|
deba@861
|
1867 |
}
|
deba@861
|
1868 |
}
|
deba@861
|
1869 |
}
|
deba@861
|
1870 |
}
|
deba@861
|
1871 |
|
deba@861
|
1872 |
template <typename Graph, typename EmbeddingMap>
|
deba@861
|
1873 |
void makeBiNodeConnected(Graph& graph, EmbeddingMap& embedding) {
|
deba@861
|
1874 |
typename Graph::template ArcMap<bool> processed(graph);
|
deba@861
|
1875 |
|
deba@861
|
1876 |
std::vector<typename Graph::Arc> arcs;
|
deba@861
|
1877 |
for (typename Graph::ArcIt e(graph); e != INVALID; ++e) {
|
deba@861
|
1878 |
arcs.push_back(e);
|
deba@861
|
1879 |
}
|
deba@861
|
1880 |
|
deba@861
|
1881 |
IterableBoolMap<Graph, typename Graph::Node> visited(graph, false);
|
deba@861
|
1882 |
|
deba@861
|
1883 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
deba@861
|
1884 |
typename Graph::Arc pp = arcs[i];
|
deba@861
|
1885 |
if (processed[pp]) continue;
|
deba@861
|
1886 |
|
deba@861
|
1887 |
typename Graph::Arc e = embedding[graph.oppositeArc(pp)];
|
deba@861
|
1888 |
processed[e] = true;
|
deba@861
|
1889 |
visited.set(graph.source(e), true);
|
deba@861
|
1890 |
|
deba@861
|
1891 |
typename Graph::Arc p = e, l = e;
|
deba@861
|
1892 |
e = embedding[graph.oppositeArc(e)];
|
deba@861
|
1893 |
|
deba@861
|
1894 |
while (e != l) {
|
deba@861
|
1895 |
processed[e] = true;
|
deba@861
|
1896 |
|
deba@861
|
1897 |
if (visited[graph.source(e)]) {
|
deba@861
|
1898 |
|
deba@861
|
1899 |
typename Graph::Arc n =
|
deba@861
|
1900 |
graph.direct(graph.addEdge(graph.source(p),
|
deba@861
|
1901 |
graph.target(e)), true);
|
deba@861
|
1902 |
embedding[n] = p;
|
deba@861
|
1903 |
embedding[graph.oppositeArc(pp)] = n;
|
deba@861
|
1904 |
|
deba@861
|
1905 |
embedding[graph.oppositeArc(n)] =
|
deba@861
|
1906 |
embedding[graph.oppositeArc(e)];
|
deba@861
|
1907 |
embedding[graph.oppositeArc(e)] =
|
deba@861
|
1908 |
graph.oppositeArc(n);
|
deba@861
|
1909 |
|
deba@861
|
1910 |
p = n;
|
deba@861
|
1911 |
e = embedding[graph.oppositeArc(n)];
|
deba@861
|
1912 |
} else {
|
deba@861
|
1913 |
visited.set(graph.source(e), true);
|
deba@861
|
1914 |
pp = p;
|
deba@861
|
1915 |
p = e;
|
deba@861
|
1916 |
e = embedding[graph.oppositeArc(e)];
|
deba@861
|
1917 |
}
|
deba@861
|
1918 |
}
|
deba@861
|
1919 |
visited.setAll(false);
|
deba@861
|
1920 |
}
|
deba@861
|
1921 |
}
|
deba@861
|
1922 |
|
deba@861
|
1923 |
|
deba@861
|
1924 |
template <typename Graph, typename EmbeddingMap>
|
deba@861
|
1925 |
void makeMaxPlanar(Graph& graph, EmbeddingMap& embedding) {
|
deba@861
|
1926 |
|
deba@861
|
1927 |
typename Graph::template NodeMap<int> degree(graph);
|
deba@861
|
1928 |
|
deba@861
|
1929 |
for (typename Graph::NodeIt n(graph); n != INVALID; ++n) {
|
deba@861
|
1930 |
degree[n] = countIncEdges(graph, n);
|
deba@861
|
1931 |
}
|
deba@861
|
1932 |
|
deba@861
|
1933 |
typename Graph::template ArcMap<bool> processed(graph);
|
deba@861
|
1934 |
IterableBoolMap<Graph, typename Graph::Node> visited(graph, false);
|
deba@861
|
1935 |
|
deba@861
|
1936 |
std::vector<typename Graph::Arc> arcs;
|
deba@861
|
1937 |
for (typename Graph::ArcIt e(graph); e != INVALID; ++e) {
|
deba@861
|
1938 |
arcs.push_back(e);
|
deba@861
|
1939 |
}
|
deba@861
|
1940 |
|
deba@861
|
1941 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
deba@861
|
1942 |
typename Graph::Arc e = arcs[i];
|
deba@861
|
1943 |
|
deba@861
|
1944 |
if (processed[e]) continue;
|
deba@861
|
1945 |
processed[e] = true;
|
deba@861
|
1946 |
|
deba@861
|
1947 |
typename Graph::Arc mine = e;
|
deba@861
|
1948 |
int mind = degree[graph.source(e)];
|
deba@861
|
1949 |
|
deba@861
|
1950 |
int face_size = 1;
|
deba@861
|
1951 |
|
deba@861
|
1952 |
typename Graph::Arc l = e;
|
deba@861
|
1953 |
e = embedding[graph.oppositeArc(e)];
|
deba@861
|
1954 |
while (l != e) {
|
deba@861
|
1955 |
processed[e] = true;
|
deba@861
|
1956 |
|
deba@861
|
1957 |
++face_size;
|
deba@861
|
1958 |
|
deba@861
|
1959 |
if (degree[graph.source(e)] < mind) {
|
deba@861
|
1960 |
mine = e;
|
deba@861
|
1961 |
mind = degree[graph.source(e)];
|
deba@861
|
1962 |
}
|
deba@861
|
1963 |
|
deba@861
|
1964 |
e = embedding[graph.oppositeArc(e)];
|
deba@861
|
1965 |
}
|
deba@861
|
1966 |
|
deba@861
|
1967 |
if (face_size < 4) {
|
deba@861
|
1968 |
continue;
|
deba@861
|
1969 |
}
|
deba@861
|
1970 |
|
deba@861
|
1971 |
typename Graph::Node s = graph.source(mine);
|
deba@861
|
1972 |
for (typename Graph::OutArcIt e(graph, s); e != INVALID; ++e) {
|
deba@861
|
1973 |
visited.set(graph.target(e), true);
|
deba@861
|
1974 |
}
|
deba@861
|
1975 |
|
deba@861
|
1976 |
typename Graph::Arc oppe = INVALID;
|
deba@861
|
1977 |
|
deba@861
|
1978 |
e = embedding[graph.oppositeArc(mine)];
|
deba@861
|
1979 |
e = embedding[graph.oppositeArc(e)];
|
deba@861
|
1980 |
while (graph.target(e) != s) {
|
deba@861
|
1981 |
if (visited[graph.source(e)]) {
|
deba@861
|
1982 |
oppe = e;
|
deba@861
|
1983 |
break;
|
deba@861
|
1984 |
}
|
deba@861
|
1985 |
e = embedding[graph.oppositeArc(e)];
|
deba@861
|
1986 |
}
|
deba@861
|
1987 |
visited.setAll(false);
|
deba@861
|
1988 |
|
deba@861
|
1989 |
if (oppe == INVALID) {
|
deba@861
|
1990 |
|
deba@861
|
1991 |
e = embedding[graph.oppositeArc(mine)];
|
deba@861
|
1992 |
typename Graph::Arc pn = mine, p = e;
|
deba@861
|
1993 |
|
deba@861
|
1994 |
e = embedding[graph.oppositeArc(e)];
|
deba@861
|
1995 |
while (graph.target(e) != s) {
|
deba@861
|
1996 |
typename Graph::Arc n =
|
deba@861
|
1997 |
graph.direct(graph.addEdge(s, graph.source(e)), true);
|
deba@861
|
1998 |
|
deba@861
|
1999 |
embedding[n] = pn;
|
deba@861
|
2000 |
embedding[graph.oppositeArc(n)] = e;
|
deba@861
|
2001 |
embedding[graph.oppositeArc(p)] = graph.oppositeArc(n);
|
deba@861
|
2002 |
|
deba@861
|
2003 |
pn = n;
|
deba@861
|
2004 |
|
deba@861
|
2005 |
p = e;
|
deba@861
|
2006 |
e = embedding[graph.oppositeArc(e)];
|
deba@861
|
2007 |
}
|
deba@861
|
2008 |
|
deba@861
|
2009 |
embedding[graph.oppositeArc(e)] = pn;
|
deba@861
|
2010 |
|
deba@861
|
2011 |
} else {
|
deba@861
|
2012 |
|
deba@861
|
2013 |
mine = embedding[graph.oppositeArc(mine)];
|
deba@861
|
2014 |
s = graph.source(mine);
|
deba@861
|
2015 |
oppe = embedding[graph.oppositeArc(oppe)];
|
deba@861
|
2016 |
typename Graph::Node t = graph.source(oppe);
|
deba@861
|
2017 |
|
deba@861
|
2018 |
typename Graph::Arc ce = graph.direct(graph.addEdge(s, t), true);
|
deba@861
|
2019 |
embedding[ce] = mine;
|
deba@861
|
2020 |
embedding[graph.oppositeArc(ce)] = oppe;
|
deba@861
|
2021 |
|
deba@861
|
2022 |
typename Graph::Arc pn = ce, p = oppe;
|
deba@861
|
2023 |
e = embedding[graph.oppositeArc(oppe)];
|
deba@861
|
2024 |
while (graph.target(e) != s) {
|
deba@861
|
2025 |
typename Graph::Arc n =
|
deba@861
|
2026 |
graph.direct(graph.addEdge(s, graph.source(e)), true);
|
deba@861
|
2027 |
|
deba@861
|
2028 |
embedding[n] = pn;
|
deba@861
|
2029 |
embedding[graph.oppositeArc(n)] = e;
|
deba@861
|
2030 |
embedding[graph.oppositeArc(p)] = graph.oppositeArc(n);
|
deba@861
|
2031 |
|
deba@861
|
2032 |
pn = n;
|
deba@861
|
2033 |
|
deba@861
|
2034 |
p = e;
|
deba@861
|
2035 |
e = embedding[graph.oppositeArc(e)];
|
deba@861
|
2036 |
|
deba@861
|
2037 |
}
|
deba@861
|
2038 |
embedding[graph.oppositeArc(e)] = pn;
|
deba@861
|
2039 |
|
deba@861
|
2040 |
pn = graph.oppositeArc(ce), p = mine;
|
deba@861
|
2041 |
e = embedding[graph.oppositeArc(mine)];
|
deba@861
|
2042 |
while (graph.target(e) != t) {
|
deba@861
|
2043 |
typename Graph::Arc n =
|
deba@861
|
2044 |
graph.direct(graph.addEdge(t, graph.source(e)), true);
|
deba@861
|
2045 |
|
deba@861
|
2046 |
embedding[n] = pn;
|
deba@861
|
2047 |
embedding[graph.oppositeArc(n)] = e;
|
deba@861
|
2048 |
embedding[graph.oppositeArc(p)] = graph.oppositeArc(n);
|
deba@861
|
2049 |
|
deba@861
|
2050 |
pn = n;
|
deba@861
|
2051 |
|
deba@861
|
2052 |
p = e;
|
deba@861
|
2053 |
e = embedding[graph.oppositeArc(e)];
|
deba@861
|
2054 |
|
deba@861
|
2055 |
}
|
deba@861
|
2056 |
embedding[graph.oppositeArc(e)] = pn;
|
deba@861
|
2057 |
}
|
deba@861
|
2058 |
}
|
deba@861
|
2059 |
}
|
deba@861
|
2060 |
|
deba@861
|
2061 |
}
|
deba@861
|
2062 |
|
deba@861
|
2063 |
/// \ingroup planar
|
deba@861
|
2064 |
///
|
deba@861
|
2065 |
/// \brief Schnyder's planar drawing algorithm
|
deba@861
|
2066 |
///
|
deba@861
|
2067 |
/// The planar drawing algorithm calculates positions for the nodes
|
kpeter@896
|
2068 |
/// in the plane. These coordinates satisfy that if the edges are
|
kpeter@896
|
2069 |
/// represented with straight lines, then they will not intersect
|
deba@861
|
2070 |
/// each other.
|
deba@861
|
2071 |
///
|
kpeter@896
|
2072 |
/// Scnyder's algorithm embeds the graph on an \c (n-2)x(n-2) size grid,
|
kpeter@896
|
2073 |
/// i.e. each node will be located in the \c [0..n-2]x[0..n-2] square.
|
deba@861
|
2074 |
/// The time complexity of the algorithm is O(n).
|
kpeter@896
|
2075 |
///
|
kpeter@896
|
2076 |
/// \see PlanarEmbedding
|
deba@861
|
2077 |
template <typename Graph>
|
deba@861
|
2078 |
class PlanarDrawing {
|
deba@861
|
2079 |
public:
|
deba@861
|
2080 |
|
deba@861
|
2081 |
TEMPLATE_GRAPH_TYPEDEFS(Graph);
|
deba@861
|
2082 |
|
kpeter@896
|
2083 |
/// \brief The point type for storing coordinates
|
deba@861
|
2084 |
typedef dim2::Point<int> Point;
|
kpeter@896
|
2085 |
/// \brief The map type for storing the coordinates of the nodes
|
deba@861
|
2086 |
typedef typename Graph::template NodeMap<Point> PointMap;
|
deba@861
|
2087 |
|
deba@861
|
2088 |
|
deba@861
|
2089 |
/// \brief Constructor
|
deba@861
|
2090 |
///
|
deba@861
|
2091 |
/// Constructor
|
kpeter@896
|
2092 |
/// \pre The graph must be simple, i.e. it should not
|
kpeter@896
|
2093 |
/// contain parallel or loop arcs.
|
deba@861
|
2094 |
PlanarDrawing(const Graph& graph)
|
deba@861
|
2095 |
: _graph(graph), _point_map(graph) {}
|
deba@861
|
2096 |
|
deba@861
|
2097 |
private:
|
deba@861
|
2098 |
|
deba@861
|
2099 |
template <typename AuxGraph, typename AuxEmbeddingMap>
|
deba@861
|
2100 |
void drawing(const AuxGraph& graph,
|
deba@861
|
2101 |
const AuxEmbeddingMap& next,
|
deba@861
|
2102 |
PointMap& point_map) {
|
deba@861
|
2103 |
TEMPLATE_GRAPH_TYPEDEFS(AuxGraph);
|
deba@861
|
2104 |
|
deba@861
|
2105 |
typename AuxGraph::template ArcMap<Arc> prev(graph);
|
deba@861
|
2106 |
|
deba@861
|
2107 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
deba@861
|
2108 |
Arc e = OutArcIt(graph, n);
|
deba@861
|
2109 |
|
deba@861
|
2110 |
Arc p = e, l = e;
|
deba@861
|
2111 |
|
deba@861
|
2112 |
e = next[e];
|
deba@861
|
2113 |
while (e != l) {
|
deba@861
|
2114 |
prev[e] = p;
|
deba@861
|
2115 |
p = e;
|
deba@861
|
2116 |
e = next[e];
|
deba@861
|
2117 |
}
|
deba@861
|
2118 |
prev[e] = p;
|
deba@861
|
2119 |
}
|
deba@861
|
2120 |
|
deba@861
|
2121 |
Node anode, bnode, cnode;
|
deba@861
|
2122 |
|
deba@861
|
2123 |
{
|
deba@861
|
2124 |
Arc e = ArcIt(graph);
|
deba@861
|
2125 |
anode = graph.source(e);
|
deba@861
|
2126 |
bnode = graph.target(e);
|
deba@861
|
2127 |
cnode = graph.target(next[graph.oppositeArc(e)]);
|
deba@861
|
2128 |
}
|
deba@861
|
2129 |
|
deba@861
|
2130 |
IterableBoolMap<AuxGraph, Node> proper(graph, false);
|
deba@861
|
2131 |
typename AuxGraph::template NodeMap<int> conn(graph, -1);
|
deba@861
|
2132 |
|
deba@861
|
2133 |
conn[anode] = conn[bnode] = -2;
|
deba@861
|
2134 |
{
|
deba@861
|
2135 |
for (OutArcIt e(graph, anode); e != INVALID; ++e) {
|
deba@861
|
2136 |
Node m = graph.target(e);
|
deba@861
|
2137 |
if (conn[m] == -1) {
|
deba@861
|
2138 |
conn[m] = 1;
|
deba@861
|
2139 |
}
|
deba@861
|
2140 |
}
|
deba@861
|
2141 |
conn[cnode] = 2;
|
deba@861
|
2142 |
|
deba@861
|
2143 |
for (OutArcIt e(graph, bnode); e != INVALID; ++e) {
|
deba@861
|
2144 |
Node m = graph.target(e);
|
deba@861
|
2145 |
if (conn[m] == -1) {
|
deba@861
|
2146 |
conn[m] = 1;
|
deba@861
|
2147 |
} else if (conn[m] != -2) {
|
deba@861
|
2148 |
conn[m] += 1;
|
deba@861
|
2149 |
Arc pe = graph.oppositeArc(e);
|
deba@861
|
2150 |
if (conn[graph.target(next[pe])] == -2) {
|
deba@861
|
2151 |
conn[m] -= 1;
|
deba@861
|
2152 |
}
|
deba@861
|
2153 |
if (conn[graph.target(prev[pe])] == -2) {
|
deba@861
|
2154 |
conn[m] -= 1;
|
deba@861
|
2155 |
}
|
deba@861
|
2156 |
|
deba@861
|
2157 |
proper.set(m, conn[m] == 1);
|
deba@861
|
2158 |
}
|
deba@861
|
2159 |
}
|
deba@861
|
2160 |
}
|
deba@861
|
2161 |
|
deba@861
|
2162 |
|
deba@861
|
2163 |
typename AuxGraph::template ArcMap<int> angle(graph, -1);
|
deba@861
|
2164 |
|
deba@861
|
2165 |
while (proper.trueNum() != 0) {
|
deba@861
|
2166 |
Node n = typename IterableBoolMap<AuxGraph, Node>::TrueIt(proper);
|
deba@861
|
2167 |
proper.set(n, false);
|
deba@861
|
2168 |
conn[n] = -2;
|
deba@861
|
2169 |
|
deba@861
|
2170 |
for (OutArcIt e(graph, n); e != INVALID; ++e) {
|
deba@861
|
2171 |
Node m = graph.target(e);
|
deba@861
|
2172 |
if (conn[m] == -1) {
|
deba@861
|
2173 |
conn[m] = 1;
|
deba@861
|
2174 |
} else if (conn[m] != -2) {
|
deba@861
|
2175 |
conn[m] += 1;
|
deba@861
|
2176 |
Arc pe = graph.oppositeArc(e);
|
deba@861
|
2177 |
if (conn[graph.target(next[pe])] == -2) {
|
deba@861
|
2178 |
conn[m] -= 1;
|
deba@861
|
2179 |
}
|
deba@861
|
2180 |
if (conn[graph.target(prev[pe])] == -2) {
|
deba@861
|
2181 |
conn[m] -= 1;
|
deba@861
|
2182 |
}
|
deba@861
|
2183 |
|
deba@861
|
2184 |
proper.set(m, conn[m] == 1);
|
deba@861
|
2185 |
}
|
deba@861
|
2186 |
}
|
deba@861
|
2187 |
|
deba@861
|
2188 |
{
|
deba@861
|
2189 |
Arc e = OutArcIt(graph, n);
|
deba@861
|
2190 |
Arc p = e, l = e;
|
deba@861
|
2191 |
|
deba@861
|
2192 |
e = next[e];
|
deba@861
|
2193 |
while (e != l) {
|
deba@861
|
2194 |
|
deba@861
|
2195 |
if (conn[graph.target(e)] == -2 && conn[graph.target(p)] == -2) {
|
deba@861
|
2196 |
Arc f = e;
|
deba@861
|
2197 |
angle[f] = 0;
|
deba@861
|
2198 |
f = next[graph.oppositeArc(f)];
|
deba@861
|
2199 |
angle[f] = 1;
|
deba@861
|
2200 |
f = next[graph.oppositeArc(f)];
|
deba@861
|
2201 |
angle[f] = 2;
|
deba@861
|
2202 |
}
|
deba@861
|
2203 |
|
deba@861
|
2204 |
p = e;
|
deba@861
|
2205 |
e = next[e];
|
deba@861
|
2206 |
}
|
deba@861
|
2207 |
|
deba@861
|
2208 |
if (conn[graph.target(e)] == -2 && conn[graph.target(p)] == -2) {
|
deba@861
|
2209 |
Arc f = e;
|
deba@861
|
2210 |
angle[f] = 0;
|
deba@861
|
2211 |
f = next[graph.oppositeArc(f)];
|
deba@861
|
2212 |
angle[f] = 1;
|
deba@861
|
2213 |
f = next[graph.oppositeArc(f)];
|
deba@861
|
2214 |
angle[f] = 2;
|
deba@861
|
2215 |
}
|
deba@861
|
2216 |
}
|
deba@861
|
2217 |
}
|
deba@861
|
2218 |
|
deba@861
|
2219 |
typename AuxGraph::template NodeMap<Node> apred(graph, INVALID);
|
deba@861
|
2220 |
typename AuxGraph::template NodeMap<Node> bpred(graph, INVALID);
|
deba@861
|
2221 |
typename AuxGraph::template NodeMap<Node> cpred(graph, INVALID);
|
deba@861
|
2222 |
|
deba@861
|
2223 |
typename AuxGraph::template NodeMap<int> apredid(graph, -1);
|
deba@861
|
2224 |
typename AuxGraph::template NodeMap<int> bpredid(graph, -1);
|
deba@861
|
2225 |
typename AuxGraph::template NodeMap<int> cpredid(graph, -1);
|
deba@861
|
2226 |
|
deba@861
|
2227 |
for (ArcIt e(graph); e != INVALID; ++e) {
|
deba@861
|
2228 |
if (angle[e] == angle[next[e]]) {
|
deba@861
|
2229 |
switch (angle[e]) {
|
deba@861
|
2230 |
case 2:
|
deba@861
|
2231 |
apred[graph.target(e)] = graph.source(e);
|
deba@861
|
2232 |
apredid[graph.target(e)] = graph.id(graph.source(e));
|
deba@861
|
2233 |
break;
|
deba@861
|
2234 |
case 1:
|
deba@861
|
2235 |
bpred[graph.target(e)] = graph.source(e);
|
deba@861
|
2236 |
bpredid[graph.target(e)] = graph.id(graph.source(e));
|
deba@861
|
2237 |
break;
|
deba@861
|
2238 |
case 0:
|
deba@861
|
2239 |
cpred[graph.target(e)] = graph.source(e);
|
deba@861
|
2240 |
cpredid[graph.target(e)] = graph.id(graph.source(e));
|
deba@861
|
2241 |
break;
|
deba@861
|
2242 |
}
|
deba@861
|
2243 |
}
|
deba@861
|
2244 |
}
|
deba@861
|
2245 |
|
deba@861
|
2246 |
cpred[anode] = INVALID;
|
deba@861
|
2247 |
cpred[bnode] = INVALID;
|
deba@861
|
2248 |
|
deba@861
|
2249 |
std::vector<Node> aorder, border, corder;
|
deba@861
|
2250 |
|
deba@861
|
2251 |
{
|
deba@861
|
2252 |
typename AuxGraph::template NodeMap<bool> processed(graph, false);
|
deba@861
|
2253 |
std::vector<Node> st;
|
deba@861
|
2254 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
deba@861
|
2255 |
if (!processed[n] && n != bnode && n != cnode) {
|
deba@861
|
2256 |
st.push_back(n);
|
deba@861
|
2257 |
processed[n] = true;
|
deba@861
|
2258 |
Node m = apred[n];
|
deba@861
|
2259 |
while (m != INVALID && !processed[m]) {
|
deba@861
|
2260 |
st.push_back(m);
|
deba@861
|
2261 |
processed[m] = true;
|
deba@861
|
2262 |
m = apred[m];
|
deba@861
|
2263 |
}
|
deba@861
|
2264 |
while (!st.empty()) {
|
deba@861
|
2265 |
aorder.push_back(st.back());
|
deba@861
|
2266 |
st.pop_back();
|
deba@861
|
2267 |
}
|
deba@861
|
2268 |
}
|
deba@861
|
2269 |
}
|
deba@861
|
2270 |
}
|
deba@861
|
2271 |
|
deba@861
|
2272 |
{
|
deba@861
|
2273 |
typename AuxGraph::template NodeMap<bool> processed(graph, false);
|
deba@861
|
2274 |
std::vector<Node> st;
|
deba@861
|
2275 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
deba@861
|
2276 |
if (!processed[n] && n != cnode && n != anode) {
|
deba@861
|
2277 |
st.push_back(n);
|
deba@861
|
2278 |
processed[n] = true;
|
deba@861
|
2279 |
Node m = bpred[n];
|
deba@861
|
2280 |
while (m != INVALID && !processed[m]) {
|
deba@861
|
2281 |
st.push_back(m);
|
deba@861
|
2282 |
processed[m] = true;
|
deba@861
|
2283 |
m = bpred[m];
|
deba@861
|
2284 |
}
|
deba@861
|
2285 |
while (!st.empty()) {
|
deba@861
|
2286 |
border.push_back(st.back());
|
deba@861
|
2287 |
st.pop_back();
|
deba@861
|
2288 |
}
|
deba@861
|
2289 |
}
|
deba@861
|
2290 |
}
|
deba@861
|
2291 |
}
|
deba@861
|
2292 |
|
deba@861
|
2293 |
{
|
deba@861
|
2294 |
typename AuxGraph::template NodeMap<bool> processed(graph, false);
|
deba@861
|
2295 |
std::vector<Node> st;
|
deba@861
|
2296 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
deba@861
|
2297 |
if (!processed[n] && n != anode && n != bnode) {
|
deba@861
|
2298 |
st.push_back(n);
|
deba@861
|
2299 |
processed[n] = true;
|
deba@861
|
2300 |
Node m = cpred[n];
|
deba@861
|
2301 |
while (m != INVALID && !processed[m]) {
|
deba@861
|
2302 |
st.push_back(m);
|
deba@861
|
2303 |
processed[m] = true;
|
deba@861
|
2304 |
m = cpred[m];
|
deba@861
|
2305 |
}
|
deba@861
|
2306 |
while (!st.empty()) {
|
deba@861
|
2307 |
corder.push_back(st.back());
|
deba@861
|
2308 |
st.pop_back();
|
deba@861
|
2309 |
}
|
deba@861
|
2310 |
}
|
deba@861
|
2311 |
}
|
deba@861
|
2312 |
}
|
deba@861
|
2313 |
|
deba@861
|
2314 |
typename AuxGraph::template NodeMap<int> atree(graph, 0);
|
deba@861
|
2315 |
for (int i = aorder.size() - 1; i >= 0; --i) {
|
deba@861
|
2316 |
Node n = aorder[i];
|
deba@861
|
2317 |
atree[n] = 1;
|
deba@861
|
2318 |
for (OutArcIt e(graph, n); e != INVALID; ++e) {
|
deba@861
|
2319 |
if (apred[graph.target(e)] == n) {
|
deba@861
|
2320 |
atree[n] += atree[graph.target(e)];
|
deba@861
|
2321 |
}
|
deba@861
|
2322 |
}
|
deba@861
|
2323 |
}
|
deba@861
|
2324 |
|
deba@861
|
2325 |
typename AuxGraph::template NodeMap<int> btree(graph, 0);
|
deba@861
|
2326 |
for (int i = border.size() - 1; i >= 0; --i) {
|
deba@861
|
2327 |
Node n = border[i];
|
deba@861
|
2328 |
btree[n] = 1;
|
deba@861
|
2329 |
for (OutArcIt e(graph, n); e != INVALID; ++e) {
|
deba@861
|
2330 |
if (bpred[graph.target(e)] == n) {
|
deba@861
|
2331 |
btree[n] += btree[graph.target(e)];
|
deba@861
|
2332 |
}
|
deba@861
|
2333 |
}
|
deba@861
|
2334 |
}
|
deba@861
|
2335 |
|
deba@861
|
2336 |
typename AuxGraph::template NodeMap<int> apath(graph, 0);
|
deba@861
|
2337 |
apath[bnode] = apath[cnode] = 1;
|
deba@861
|
2338 |
typename AuxGraph::template NodeMap<int> apath_btree(graph, 0);
|
deba@861
|
2339 |
apath_btree[bnode] = btree[bnode];
|
deba@861
|
2340 |
for (int i = 1; i < int(aorder.size()); ++i) {
|
deba@861
|
2341 |
Node n = aorder[i];
|
deba@861
|
2342 |
apath[n] = apath[apred[n]] + 1;
|
deba@861
|
2343 |
apath_btree[n] = btree[n] + apath_btree[apred[n]];
|
deba@861
|
2344 |
}
|
deba@861
|
2345 |
|
deba@861
|
2346 |
typename AuxGraph::template NodeMap<int> bpath_atree(graph, 0);
|
deba@861
|
2347 |
bpath_atree[anode] = atree[anode];
|
deba@861
|
2348 |
for (int i = 1; i < int(border.size()); ++i) {
|
deba@861
|
2349 |
Node n = border[i];
|
deba@861
|
2350 |
bpath_atree[n] = atree[n] + bpath_atree[bpred[n]];
|
deba@861
|
2351 |
}
|
deba@861
|
2352 |
|
deba@861
|
2353 |
typename AuxGraph::template NodeMap<int> cpath(graph, 0);
|
deba@861
|
2354 |
cpath[anode] = cpath[bnode] = 1;
|
deba@861
|
2355 |
typename AuxGraph::template NodeMap<int> cpath_atree(graph, 0);
|
deba@861
|
2356 |
cpath_atree[anode] = atree[anode];
|
deba@861
|
2357 |
typename AuxGraph::template NodeMap<int> cpath_btree(graph, 0);
|
deba@861
|
2358 |
cpath_btree[bnode] = btree[bnode];
|
deba@861
|
2359 |
for (int i = 1; i < int(corder.size()); ++i) {
|
deba@861
|
2360 |
Node n = corder[i];
|
deba@861
|
2361 |
cpath[n] = cpath[cpred[n]] + 1;
|
deba@861
|
2362 |
cpath_atree[n] = atree[n] + cpath_atree[cpred[n]];
|
deba@861
|
2363 |
cpath_btree[n] = btree[n] + cpath_btree[cpred[n]];
|
deba@861
|
2364 |
}
|
deba@861
|
2365 |
|
deba@861
|
2366 |
typename AuxGraph::template NodeMap<int> third(graph);
|
deba@861
|
2367 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
deba@861
|
2368 |
point_map[n].x =
|
deba@861
|
2369 |
bpath_atree[n] + cpath_atree[n] - atree[n] - cpath[n] + 1;
|
deba@861
|
2370 |
point_map[n].y =
|
deba@861
|
2371 |
cpath_btree[n] + apath_btree[n] - btree[n] - apath[n] + 1;
|
deba@861
|
2372 |
}
|
deba@861
|
2373 |
|
deba@861
|
2374 |
}
|
deba@861
|
2375 |
|
deba@861
|
2376 |
public:
|
deba@861
|
2377 |
|
kpeter@896
|
2378 |
/// \brief Calculate the node positions
|
deba@861
|
2379 |
///
|
kpeter@896
|
2380 |
/// This function calculates the node positions on the plane.
|
kpeter@896
|
2381 |
/// \return \c true if the graph is planar.
|
deba@861
|
2382 |
bool run() {
|
deba@861
|
2383 |
PlanarEmbedding<Graph> pe(_graph);
|
deba@861
|
2384 |
if (!pe.run()) return false;
|
deba@861
|
2385 |
|
deba@861
|
2386 |
run(pe);
|
deba@861
|
2387 |
return true;
|
deba@861
|
2388 |
}
|
deba@861
|
2389 |
|
kpeter@896
|
2390 |
/// \brief Calculate the node positions according to a
|
deba@861
|
2391 |
/// combinatorical embedding
|
deba@861
|
2392 |
///
|
kpeter@896
|
2393 |
/// This function calculates the node positions on the plane.
|
kpeter@896
|
2394 |
/// The given \c embedding map should contain a valid combinatorical
|
kpeter@896
|
2395 |
/// embedding, i.e. a valid cyclic order of the arcs.
|
kpeter@896
|
2396 |
/// It can be computed using PlanarEmbedding.
|
deba@861
|
2397 |
template <typename EmbeddingMap>
|
deba@861
|
2398 |
void run(const EmbeddingMap& embedding) {
|
deba@861
|
2399 |
typedef SmartEdgeSet<Graph> AuxGraph;
|
deba@861
|
2400 |
|
deba@861
|
2401 |
if (3 * countNodes(_graph) - 6 == countEdges(_graph)) {
|
deba@861
|
2402 |
drawing(_graph, embedding, _point_map);
|
deba@861
|
2403 |
return;
|
deba@861
|
2404 |
}
|
deba@861
|
2405 |
|
deba@861
|
2406 |
AuxGraph aux_graph(_graph);
|
deba@861
|
2407 |
typename AuxGraph::template ArcMap<typename AuxGraph::Arc>
|
deba@861
|
2408 |
aux_embedding(aux_graph);
|
deba@861
|
2409 |
|
deba@861
|
2410 |
{
|
deba@861
|
2411 |
|
deba@861
|
2412 |
typename Graph::template EdgeMap<typename AuxGraph::Edge>
|
deba@861
|
2413 |
ref(_graph);
|
deba@861
|
2414 |
|
deba@861
|
2415 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
deba@861
|
2416 |
ref[e] = aux_graph.addEdge(_graph.u(e), _graph.v(e));
|
deba@861
|
2417 |
}
|
deba@861
|
2418 |
|
deba@861
|
2419 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
deba@861
|
2420 |
Arc ee = embedding[_graph.direct(e, true)];
|
deba@861
|
2421 |
aux_embedding[aux_graph.direct(ref[e], true)] =
|
deba@861
|
2422 |
aux_graph.direct(ref[ee], _graph.direction(ee));
|
deba@861
|
2423 |
ee = embedding[_graph.direct(e, false)];
|
deba@861
|
2424 |
aux_embedding[aux_graph.direct(ref[e], false)] =
|
deba@861
|
2425 |
aux_graph.direct(ref[ee], _graph.direction(ee));
|
deba@861
|
2426 |
}
|
deba@861
|
2427 |
}
|
deba@861
|
2428 |
_planarity_bits::makeConnected(aux_graph, aux_embedding);
|
deba@861
|
2429 |
_planarity_bits::makeBiNodeConnected(aux_graph, aux_embedding);
|
deba@861
|
2430 |
_planarity_bits::makeMaxPlanar(aux_graph, aux_embedding);
|
deba@861
|
2431 |
drawing(aux_graph, aux_embedding, _point_map);
|
deba@861
|
2432 |
}
|
deba@861
|
2433 |
|
deba@861
|
2434 |
/// \brief The coordinate of the given node
|
deba@861
|
2435 |
///
|
kpeter@896
|
2436 |
/// This function returns the coordinate of the given node.
|
deba@861
|
2437 |
Point operator[](const Node& node) const {
|
deba@861
|
2438 |
return _point_map[node];
|
deba@861
|
2439 |
}
|
deba@861
|
2440 |
|
kpeter@896
|
2441 |
/// \brief Return the grid embedding in a node map
|
deba@861
|
2442 |
///
|
kpeter@896
|
2443 |
/// This function returns the grid embedding in a node map of
|
kpeter@896
|
2444 |
/// \c dim2::Point<int> coordinates.
|
deba@861
|
2445 |
const PointMap& coords() const {
|
deba@861
|
2446 |
return _point_map;
|
deba@861
|
2447 |
}
|
deba@861
|
2448 |
|
deba@861
|
2449 |
private:
|
deba@861
|
2450 |
|
deba@861
|
2451 |
const Graph& _graph;
|
deba@861
|
2452 |
PointMap _point_map;
|
deba@861
|
2453 |
|
deba@861
|
2454 |
};
|
deba@861
|
2455 |
|
deba@861
|
2456 |
namespace _planarity_bits {
|
deba@861
|
2457 |
|
deba@861
|
2458 |
template <typename ColorMap>
|
deba@861
|
2459 |
class KempeFilter {
|
deba@861
|
2460 |
public:
|
deba@861
|
2461 |
typedef typename ColorMap::Key Key;
|
deba@861
|
2462 |
typedef bool Value;
|
deba@861
|
2463 |
|
deba@861
|
2464 |
KempeFilter(const ColorMap& color_map,
|
deba@861
|
2465 |
const typename ColorMap::Value& first,
|
deba@861
|
2466 |
const typename ColorMap::Value& second)
|
deba@861
|
2467 |
: _color_map(color_map), _first(first), _second(second) {}
|
deba@861
|
2468 |
|
deba@861
|
2469 |
Value operator[](const Key& key) const {
|
deba@861
|
2470 |
return _color_map[key] == _first || _color_map[key] == _second;
|
deba@861
|
2471 |
}
|
deba@861
|
2472 |
|
deba@861
|
2473 |
private:
|
deba@861
|
2474 |
const ColorMap& _color_map;
|
deba@861
|
2475 |
typename ColorMap::Value _first, _second;
|
deba@861
|
2476 |
};
|
deba@861
|
2477 |
}
|
deba@861
|
2478 |
|
deba@861
|
2479 |
/// \ingroup planar
|
deba@861
|
2480 |
///
|
deba@861
|
2481 |
/// \brief Coloring planar graphs
|
deba@861
|
2482 |
///
|
deba@861
|
2483 |
/// The graph coloring problem is the coloring of the graph nodes
|
kpeter@896
|
2484 |
/// so that there are no adjacent nodes with the same color. The
|
kpeter@896
|
2485 |
/// planar graphs can always be colored with four colors, which is
|
kpeter@896
|
2486 |
/// proved by Appel and Haken. Their proofs provide a quadratic
|
deba@861
|
2487 |
/// time algorithm for four coloring, but it could not be used to
|
kpeter@896
|
2488 |
/// implement an efficient algorithm. The five and six coloring can be
|
kpeter@896
|
2489 |
/// made in linear time, but in this class, the five coloring has
|
deba@861
|
2490 |
/// quadratic worst case time complexity. The two coloring (if
|
deba@861
|
2491 |
/// possible) is solvable with a graph search algorithm and it is
|
deba@861
|
2492 |
/// implemented in \ref bipartitePartitions() function in LEMON. To
|
kpeter@896
|
2493 |
/// decide whether a planar graph is three colorable is NP-complete.
|
deba@861
|
2494 |
///
|
deba@861
|
2495 |
/// This class contains member functions for calculate colorings
|
deba@861
|
2496 |
/// with five and six colors. The six coloring algorithm is a simple
|
deba@861
|
2497 |
/// greedy coloring on the backward minimum outgoing order of nodes.
|
kpeter@896
|
2498 |
/// This order can be computed by selecting the node with least
|
kpeter@896
|
2499 |
/// outgoing arcs to unprocessed nodes in each phase. This order
|
deba@861
|
2500 |
/// guarantees that when a node is chosen for coloring it has at
|
deba@861
|
2501 |
/// most five already colored adjacents. The five coloring algorithm
|
deba@861
|
2502 |
/// use the same method, but if the greedy approach fails to color
|
deba@861
|
2503 |
/// with five colors, i.e. the node has five already different
|
deba@861
|
2504 |
/// colored neighbours, it swaps the colors in one of the connected
|
deba@861
|
2505 |
/// two colored sets with the Kempe recoloring method.
|
deba@861
|
2506 |
template <typename Graph>
|
deba@861
|
2507 |
class PlanarColoring {
|
deba@861
|
2508 |
public:
|
deba@861
|
2509 |
|
deba@861
|
2510 |
TEMPLATE_GRAPH_TYPEDEFS(Graph);
|
deba@861
|
2511 |
|
kpeter@896
|
2512 |
/// \brief The map type for storing color indices
|
deba@861
|
2513 |
typedef typename Graph::template NodeMap<int> IndexMap;
|
kpeter@896
|
2514 |
/// \brief The map type for storing colors
|
kpeter@896
|
2515 |
///
|
kpeter@896
|
2516 |
/// The map type for storing colors.
|
kpeter@896
|
2517 |
/// \see Palette, Color
|
deba@861
|
2518 |
typedef ComposeMap<Palette, IndexMap> ColorMap;
|
deba@861
|
2519 |
|
deba@861
|
2520 |
/// \brief Constructor
|
deba@861
|
2521 |
///
|
kpeter@896
|
2522 |
/// Constructor.
|
kpeter@896
|
2523 |
/// \pre The graph must be simple, i.e. it should not
|
kpeter@896
|
2524 |
/// contain parallel or loop arcs.
|
deba@861
|
2525 |
PlanarColoring(const Graph& graph)
|
deba@861
|
2526 |
: _graph(graph), _color_map(graph), _palette(0) {
|
deba@861
|
2527 |
_palette.add(Color(1,0,0));
|
deba@861
|
2528 |
_palette.add(Color(0,1,0));
|
deba@861
|
2529 |
_palette.add(Color(0,0,1));
|
deba@861
|
2530 |
_palette.add(Color(1,1,0));
|
deba@861
|
2531 |
_palette.add(Color(1,0,1));
|
deba@861
|
2532 |
_palette.add(Color(0,1,1));
|
deba@861
|
2533 |
}
|
deba@861
|
2534 |
|
kpeter@896
|
2535 |
/// \brief Return the node map of color indices
|
deba@861
|
2536 |
///
|
kpeter@896
|
2537 |
/// This function returns the node map of color indices. The values are
|
kpeter@896
|
2538 |
/// in the range \c [0..4] or \c [0..5] according to the coloring method.
|
deba@861
|
2539 |
IndexMap colorIndexMap() const {
|
deba@861
|
2540 |
return _color_map;
|
deba@861
|
2541 |
}
|
deba@861
|
2542 |
|
kpeter@896
|
2543 |
/// \brief Return the node map of colors
|
deba@861
|
2544 |
///
|
kpeter@896
|
2545 |
/// This function returns the node map of colors. The values are among
|
kpeter@896
|
2546 |
/// five or six distinct \ref lemon::Color "colors".
|
deba@861
|
2547 |
ColorMap colorMap() const {
|
deba@861
|
2548 |
return composeMap(_palette, _color_map);
|
deba@861
|
2549 |
}
|
deba@861
|
2550 |
|
kpeter@896
|
2551 |
/// \brief Return the color index of the node
|
deba@861
|
2552 |
///
|
kpeter@896
|
2553 |
/// This function returns the color index of the given node. The value is
|
kpeter@896
|
2554 |
/// in the range \c [0..4] or \c [0..5] according to the coloring method.
|
deba@861
|
2555 |
int colorIndex(const Node& node) const {
|
deba@861
|
2556 |
return _color_map[node];
|
deba@861
|
2557 |
}
|
deba@861
|
2558 |
|
kpeter@896
|
2559 |
/// \brief Return the color of the node
|
deba@861
|
2560 |
///
|
kpeter@896
|
2561 |
/// This function returns the color of the given node. The value is among
|
kpeter@896
|
2562 |
/// five or six distinct \ref lemon::Color "colors".
|
deba@861
|
2563 |
Color color(const Node& node) const {
|
deba@861
|
2564 |
return _palette[_color_map[node]];
|
deba@861
|
2565 |
}
|
deba@861
|
2566 |
|
deba@861
|
2567 |
|
kpeter@896
|
2568 |
/// \brief Calculate a coloring with at most six colors
|
deba@861
|
2569 |
///
|
deba@861
|
2570 |
/// This function calculates a coloring with at most six colors. The time
|
deba@861
|
2571 |
/// complexity of this variant is linear in the size of the graph.
|
kpeter@896
|
2572 |
/// \return \c true if the algorithm could color the graph with six colors.
|
kpeter@896
|
2573 |
/// If the algorithm fails, then the graph is not planar.
|
kpeter@896
|
2574 |
/// \note This function can return \c true if the graph is not
|
kpeter@896
|
2575 |
/// planar, but it can be colored with at most six colors.
|
deba@861
|
2576 |
bool runSixColoring() {
|
deba@861
|
2577 |
|
deba@861
|
2578 |
typename Graph::template NodeMap<int> heap_index(_graph, -1);
|
deba@861
|
2579 |
BucketHeap<typename Graph::template NodeMap<int> > heap(heap_index);
|
deba@861
|
2580 |
|
deba@861
|
2581 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@861
|
2582 |
_color_map[n] = -2;
|
deba@861
|
2583 |
heap.push(n, countOutArcs(_graph, n));
|
deba@861
|
2584 |
}
|
deba@861
|
2585 |
|
deba@861
|
2586 |
std::vector<Node> order;
|
deba@861
|
2587 |
|
deba@861
|
2588 |
while (!heap.empty()) {
|
deba@861
|
2589 |
Node n = heap.top();
|
deba@861
|
2590 |
heap.pop();
|
deba@861
|
2591 |
_color_map[n] = -1;
|
deba@861
|
2592 |
order.push_back(n);
|
deba@861
|
2593 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
deba@861
|
2594 |
Node t = _graph.runningNode(e);
|
deba@861
|
2595 |
if (_color_map[t] == -2) {
|
deba@861
|
2596 |
heap.decrease(t, heap[t] - 1);
|
deba@861
|
2597 |
}
|
deba@861
|
2598 |
}
|
deba@861
|
2599 |
}
|
deba@861
|
2600 |
|
deba@861
|
2601 |
for (int i = order.size() - 1; i >= 0; --i) {
|
deba@861
|
2602 |
std::vector<bool> forbidden(6, false);
|
deba@861
|
2603 |
for (OutArcIt e(_graph, order[i]); e != INVALID; ++e) {
|
deba@861
|
2604 |
Node t = _graph.runningNode(e);
|
deba@861
|
2605 |
if (_color_map[t] != -1) {
|
deba@861
|
2606 |
forbidden[_color_map[t]] = true;
|
deba@861
|
2607 |
}
|
deba@861
|
2608 |
}
|
deba@861
|
2609 |
for (int k = 0; k < 6; ++k) {
|
deba@861
|
2610 |
if (!forbidden[k]) {
|
deba@861
|
2611 |
_color_map[order[i]] = k;
|
deba@861
|
2612 |
break;
|
deba@861
|
2613 |
}
|
deba@861
|
2614 |
}
|
deba@861
|
2615 |
if (_color_map[order[i]] == -1) {
|
deba@861
|
2616 |
return false;
|
deba@861
|
2617 |
}
|
deba@861
|
2618 |
}
|
deba@861
|
2619 |
return true;
|
deba@861
|
2620 |
}
|
deba@861
|
2621 |
|
deba@861
|
2622 |
private:
|
deba@861
|
2623 |
|
deba@861
|
2624 |
bool recolor(const Node& u, const Node& v) {
|
deba@861
|
2625 |
int ucolor = _color_map[u];
|
deba@861
|
2626 |
int vcolor = _color_map[v];
|
deba@861
|
2627 |
typedef _planarity_bits::KempeFilter<IndexMap> KempeFilter;
|
deba@861
|
2628 |
KempeFilter filter(_color_map, ucolor, vcolor);
|
deba@861
|
2629 |
|
deba@861
|
2630 |
typedef FilterNodes<const Graph, const KempeFilter> KempeGraph;
|
deba@861
|
2631 |
KempeGraph kempe_graph(_graph, filter);
|
deba@861
|
2632 |
|
deba@861
|
2633 |
std::vector<Node> comp;
|
deba@861
|
2634 |
Bfs<KempeGraph> bfs(kempe_graph);
|
deba@861
|
2635 |
bfs.init();
|
deba@861
|
2636 |
bfs.addSource(u);
|
deba@861
|
2637 |
while (!bfs.emptyQueue()) {
|
deba@861
|
2638 |
Node n = bfs.nextNode();
|
deba@861
|
2639 |
if (n == v) return false;
|
deba@861
|
2640 |
comp.push_back(n);
|
deba@861
|
2641 |
bfs.processNextNode();
|
deba@861
|
2642 |
}
|
deba@861
|
2643 |
|
deba@861
|
2644 |
int scolor = ucolor + vcolor;
|
deba@861
|
2645 |
for (int i = 0; i < static_cast<int>(comp.size()); ++i) {
|
deba@861
|
2646 |
_color_map[comp[i]] = scolor - _color_map[comp[i]];
|
deba@861
|
2647 |
}
|
deba@861
|
2648 |
|
deba@861
|
2649 |
return true;
|
deba@861
|
2650 |
}
|
deba@861
|
2651 |
|
deba@861
|
2652 |
template <typename EmbeddingMap>
|
deba@861
|
2653 |
void kempeRecoloring(const Node& node, const EmbeddingMap& embedding) {
|
deba@861
|
2654 |
std::vector<Node> nodes;
|
deba@861
|
2655 |
nodes.reserve(4);
|
deba@861
|
2656 |
|
deba@861
|
2657 |
for (Arc e = OutArcIt(_graph, node); e != INVALID; e = embedding[e]) {
|
deba@861
|
2658 |
Node t = _graph.target(e);
|
deba@861
|
2659 |
if (_color_map[t] != -1) {
|
deba@861
|
2660 |
nodes.push_back(t);
|
deba@861
|
2661 |
if (nodes.size() == 4) break;
|
deba@861
|
2662 |
}
|
deba@861
|
2663 |
}
|
deba@861
|
2664 |
|
deba@861
|
2665 |
int color = _color_map[nodes[0]];
|
deba@861
|
2666 |
if (recolor(nodes[0], nodes[2])) {
|
deba@861
|
2667 |
_color_map[node] = color;
|
deba@861
|
2668 |
} else {
|
deba@861
|
2669 |
color = _color_map[nodes[1]];
|
deba@861
|
2670 |
recolor(nodes[1], nodes[3]);
|
deba@861
|
2671 |
_color_map[node] = color;
|
deba@861
|
2672 |
}
|
deba@861
|
2673 |
}
|
deba@861
|
2674 |
|
deba@861
|
2675 |
public:
|
deba@861
|
2676 |
|
kpeter@896
|
2677 |
/// \brief Calculate a coloring with at most five colors
|
deba@861
|
2678 |
///
|
deba@861
|
2679 |
/// This function calculates a coloring with at most five
|
deba@861
|
2680 |
/// colors. The worst case time complexity of this variant is
|
deba@861
|
2681 |
/// quadratic in the size of the graph.
|
kpeter@896
|
2682 |
/// \param embedding This map should contain a valid combinatorical
|
kpeter@896
|
2683 |
/// embedding, i.e. a valid cyclic order of the arcs.
|
kpeter@896
|
2684 |
/// It can be computed using PlanarEmbedding.
|
deba@861
|
2685 |
template <typename EmbeddingMap>
|
deba@861
|
2686 |
void runFiveColoring(const EmbeddingMap& embedding) {
|
deba@861
|
2687 |
|
deba@861
|
2688 |
typename Graph::template NodeMap<int> heap_index(_graph, -1);
|
deba@861
|
2689 |
BucketHeap<typename Graph::template NodeMap<int> > heap(heap_index);
|
deba@861
|
2690 |
|
deba@861
|
2691 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
deba@861
|
2692 |
_color_map[n] = -2;
|
deba@861
|
2693 |
heap.push(n, countOutArcs(_graph, n));
|
deba@861
|
2694 |
}
|
deba@861
|
2695 |
|
deba@861
|
2696 |
std::vector<Node> order;
|
deba@861
|
2697 |
|
deba@861
|
2698 |
while (!heap.empty()) {
|
deba@861
|
2699 |
Node n = heap.top();
|
deba@861
|
2700 |
heap.pop();
|
deba@861
|
2701 |
_color_map[n] = -1;
|
deba@861
|
2702 |
order.push_back(n);
|
deba@861
|
2703 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
deba@861
|
2704 |
Node t = _graph.runningNode(e);
|
deba@861
|
2705 |
if (_color_map[t] == -2) {
|
deba@861
|
2706 |
heap.decrease(t, heap[t] - 1);
|
deba@861
|
2707 |
}
|
deba@861
|
2708 |
}
|
deba@861
|
2709 |
}
|
deba@861
|
2710 |
|
deba@861
|
2711 |
for (int i = order.size() - 1; i >= 0; --i) {
|
deba@861
|
2712 |
std::vector<bool> forbidden(5, false);
|
deba@861
|
2713 |
for (OutArcIt e(_graph, order[i]); e != INVALID; ++e) {
|
deba@861
|
2714 |
Node t = _graph.runningNode(e);
|
deba@861
|
2715 |
if (_color_map[t] != -1) {
|
deba@861
|
2716 |
forbidden[_color_map[t]] = true;
|
deba@861
|
2717 |
}
|
deba@861
|
2718 |
}
|
deba@861
|
2719 |
for (int k = 0; k < 5; ++k) {
|
deba@861
|
2720 |
if (!forbidden[k]) {
|
deba@861
|
2721 |
_color_map[order[i]] = k;
|
deba@861
|
2722 |
break;
|
deba@861
|
2723 |
}
|
deba@861
|
2724 |
}
|
deba@861
|
2725 |
if (_color_map[order[i]] == -1) {
|
deba@861
|
2726 |
kempeRecoloring(order[i], embedding);
|
deba@861
|
2727 |
}
|
deba@861
|
2728 |
}
|
deba@861
|
2729 |
}
|
deba@861
|
2730 |
|
kpeter@896
|
2731 |
/// \brief Calculate a coloring with at most five colors
|
deba@861
|
2732 |
///
|
deba@861
|
2733 |
/// This function calculates a coloring with at most five
|
deba@861
|
2734 |
/// colors. The worst case time complexity of this variant is
|
deba@861
|
2735 |
/// quadratic in the size of the graph.
|
kpeter@896
|
2736 |
/// \return \c true if the graph is planar.
|
deba@861
|
2737 |
bool runFiveColoring() {
|
deba@861
|
2738 |
PlanarEmbedding<Graph> pe(_graph);
|
deba@861
|
2739 |
if (!pe.run()) return false;
|
deba@861
|
2740 |
|
deba@861
|
2741 |
runFiveColoring(pe.embeddingMap());
|
deba@861
|
2742 |
return true;
|
deba@861
|
2743 |
}
|
deba@861
|
2744 |
|
deba@861
|
2745 |
private:
|
deba@861
|
2746 |
|
deba@861
|
2747 |
const Graph& _graph;
|
deba@861
|
2748 |
IndexMap _color_map;
|
deba@861
|
2749 |
Palette _palette;
|
deba@861
|
2750 |
};
|
deba@861
|
2751 |
|
deba@861
|
2752 |
}
|
deba@861
|
2753 |
|
deba@861
|
2754 |
#endif
|