0
3
5
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_ADAPTORS_H |
|
20 |
#define LEMON_ADAPTORS_H |
|
21 |
|
|
22 |
/// \ingroup graph_adaptors |
|
23 |
/// \file |
|
24 |
/// \brief Several graph adaptors |
|
25 |
/// |
|
26 |
/// This file contains several useful adaptors for digraphs and graphs. |
|
27 |
|
|
28 |
#include <lemon/core.h> |
|
29 |
#include <lemon/maps.h> |
|
30 |
#include <lemon/bits/variant.h> |
|
31 |
|
|
32 |
#include <lemon/bits/graph_adaptor_extender.h> |
|
33 |
#include <lemon/tolerance.h> |
|
34 |
|
|
35 |
#include <algorithm> |
|
36 |
|
|
37 |
namespace lemon { |
|
38 |
|
|
39 |
template<typename _Digraph> |
|
40 |
class DigraphAdaptorBase { |
|
41 |
public: |
|
42 |
typedef _Digraph Digraph; |
|
43 |
typedef DigraphAdaptorBase Adaptor; |
|
44 |
typedef Digraph ParentDigraph; |
|
45 |
|
|
46 |
protected: |
|
47 |
Digraph* _digraph; |
|
48 |
DigraphAdaptorBase() : _digraph(0) { } |
|
49 |
void setDigraph(Digraph& digraph) { _digraph = &digraph; } |
|
50 |
|
|
51 |
public: |
|
52 |
DigraphAdaptorBase(Digraph& digraph) : _digraph(&digraph) { } |
|
53 |
|
|
54 |
typedef typename Digraph::Node Node; |
|
55 |
typedef typename Digraph::Arc Arc; |
|
56 |
|
|
57 |
void first(Node& i) const { _digraph->first(i); } |
|
58 |
void first(Arc& i) const { _digraph->first(i); } |
|
59 |
void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); } |
|
60 |
void firstOut(Arc& i, const Node& n ) const { _digraph->firstOut(i, n); } |
|
61 |
|
|
62 |
void next(Node& i) const { _digraph->next(i); } |
|
63 |
void next(Arc& i) const { _digraph->next(i); } |
|
64 |
void nextIn(Arc& i) const { _digraph->nextIn(i); } |
|
65 |
void nextOut(Arc& i) const { _digraph->nextOut(i); } |
|
66 |
|
|
67 |
Node source(const Arc& a) const { return _digraph->source(a); } |
|
68 |
Node target(const Arc& a) const { return _digraph->target(a); } |
|
69 |
|
|
70 |
typedef NodeNumTagIndicator<Digraph> NodeNumTag; |
|
71 |
int nodeNum() const { return _digraph->nodeNum(); } |
|
72 |
|
|
73 |
typedef EdgeNumTagIndicator<Digraph> EdgeNumTag; |
|
74 |
int arcNum() const { return _digraph->arcNum(); } |
|
75 |
|
|
76 |
typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; |
|
77 |
Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) { |
|
78 |
return _digraph->findArc(u, v, prev); |
|
79 |
} |
|
80 |
|
|
81 |
Node addNode() { return _digraph->addNode(); } |
|
82 |
Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); } |
|
83 |
|
|
84 |
void erase(const Node& n) const { _digraph->erase(n); } |
|
85 |
void erase(const Arc& a) const { _digraph->erase(a); } |
|
86 |
|
|
87 |
void clear() const { _digraph->clear(); } |
|
88 |
|
|
89 |
int id(const Node& n) const { return _digraph->id(n); } |
|
90 |
int id(const Arc& a) const { return _digraph->id(a); } |
|
91 |
|
|
92 |
Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); } |
|
93 |
Arc arcFromId(int ix) const { return _digraph->arcFromId(ix); } |
|
94 |
|
|
95 |
int maxNodeId() const { return _digraph->maxNodeId(); } |
|
96 |
int maxArcId() const { return _digraph->maxArcId(); } |
|
97 |
|
|
98 |
typedef typename ItemSetTraits<Digraph, Node>::ItemNotifier NodeNotifier; |
|
99 |
NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } |
|
100 |
|
|
101 |
typedef typename ItemSetTraits<Digraph, Arc>::ItemNotifier ArcNotifier; |
|
102 |
ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); } |
|
103 |
|
|
104 |
template <typename _Value> |
|
105 |
class NodeMap : public Digraph::template NodeMap<_Value> { |
|
106 |
public: |
|
107 |
|
|
108 |
typedef typename Digraph::template NodeMap<_Value> Parent; |
|
109 |
|
|
110 |
explicit NodeMap(const Adaptor& adaptor) |
|
111 |
: Parent(*adaptor._digraph) {} |
|
112 |
|
|
113 |
NodeMap(const Adaptor& adaptor, const _Value& value) |
|
114 |
: Parent(*adaptor._digraph, value) { } |
|
115 |
|
|
116 |
private: |
|
117 |
NodeMap& operator=(const NodeMap& cmap) { |
|
118 |
return operator=<NodeMap>(cmap); |
|
119 |
} |
|
120 |
|
|
121 |
template <typename CMap> |
|
122 |
NodeMap& operator=(const CMap& cmap) { |
|
123 |
Parent::operator=(cmap); |
|
124 |
return *this; |
|
125 |
} |
|
126 |
|
|
127 |
}; |
|
128 |
|
|
129 |
template <typename _Value> |
|
130 |
class ArcMap : public Digraph::template ArcMap<_Value> { |
|
131 |
public: |
|
132 |
|
|
133 |
typedef typename Digraph::template ArcMap<_Value> Parent; |
|
134 |
|
|
135 |
explicit ArcMap(const Adaptor& adaptor) |
|
136 |
: Parent(*adaptor._digraph) {} |
|
137 |
|
|
138 |
ArcMap(const Adaptor& adaptor, const _Value& value) |
|
139 |
: Parent(*adaptor._digraph, value) {} |
|
140 |
|
|
141 |
private: |
|
142 |
ArcMap& operator=(const ArcMap& cmap) { |
|
143 |
return operator=<ArcMap>(cmap); |
|
144 |
} |
|
145 |
|
|
146 |
template <typename CMap> |
|
147 |
ArcMap& operator=(const CMap& cmap) { |
|
148 |
Parent::operator=(cmap); |
|
149 |
return *this; |
|
150 |
} |
|
151 |
|
|
152 |
}; |
|
153 |
|
|
154 |
}; |
|
155 |
|
|
156 |
template<typename _Graph> |
|
157 |
class GraphAdaptorBase { |
|
158 |
public: |
|
159 |
typedef _Graph Graph; |
|
160 |
typedef Graph ParentGraph; |
|
161 |
|
|
162 |
protected: |
|
163 |
Graph* _graph; |
|
164 |
|
|
165 |
GraphAdaptorBase() : _graph(0) {} |
|
166 |
|
|
167 |
void setGraph(Graph& graph) { _graph = &graph; } |
|
168 |
|
|
169 |
public: |
|
170 |
GraphAdaptorBase(Graph& graph) : _graph(&graph) {} |
|
171 |
|
|
172 |
typedef typename Graph::Node Node; |
|
173 |
typedef typename Graph::Arc Arc; |
|
174 |
typedef typename Graph::Edge Edge; |
|
175 |
|
|
176 |
void first(Node& i) const { _graph->first(i); } |
|
177 |
void first(Arc& i) const { _graph->first(i); } |
|
178 |
void first(Edge& i) const { _graph->first(i); } |
|
179 |
void firstIn(Arc& i, const Node& n) const { _graph->firstIn(i, n); } |
|
180 |
void firstOut(Arc& i, const Node& n ) const { _graph->firstOut(i, n); } |
|
181 |
void firstInc(Edge &i, bool &d, const Node &n) const { |
|
182 |
_graph->firstInc(i, d, n); |
|
183 |
} |
|
184 |
|
|
185 |
void next(Node& i) const { _graph->next(i); } |
|
186 |
void next(Arc& i) const { _graph->next(i); } |
|
187 |
void next(Edge& i) const { _graph->next(i); } |
|
188 |
void nextIn(Arc& i) const { _graph->nextIn(i); } |
|
189 |
void nextOut(Arc& i) const { _graph->nextOut(i); } |
|
190 |
void nextInc(Edge &i, bool &d) const { _graph->nextInc(i, d); } |
|
191 |
|
|
192 |
Node u(const Edge& e) const { return _graph->u(e); } |
|
193 |
Node v(const Edge& e) const { return _graph->v(e); } |
|
194 |
|
|
195 |
Node source(const Arc& a) const { return _graph->source(a); } |
|
196 |
Node target(const Arc& a) const { return _graph->target(a); } |
|
197 |
|
|
198 |
typedef NodeNumTagIndicator<Graph> NodeNumTag; |
|
199 |
int nodeNum() const { return _graph->nodeNum(); } |
|
200 |
|
|
201 |
typedef EdgeNumTagIndicator<Graph> EdgeNumTag; |
|
202 |
int arcNum() const { return _graph->arcNum(); } |
|
203 |
int edgeNum() const { return _graph->edgeNum(); } |
|
204 |
|
|
205 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
|
206 |
Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) { |
|
207 |
return _graph->findArc(u, v, prev); |
|
208 |
} |
|
209 |
Edge findEdge(const Node& u, const Node& v, const Edge& prev = INVALID) { |
|
210 |
return _graph->findEdge(u, v, prev); |
|
211 |
} |
|
212 |
|
|
213 |
Node addNode() { return _graph->addNode(); } |
|
214 |
Edge addEdge(const Node& u, const Node& v) { return _graph->addEdge(u, v); } |
|
215 |
|
|
216 |
void erase(const Node& i) { _graph->erase(i); } |
|
217 |
void erase(const Edge& i) { _graph->erase(i); } |
|
218 |
|
|
219 |
void clear() { _graph->clear(); } |
|
220 |
|
|
221 |
bool direction(const Arc& a) const { return _graph->direction(a); } |
|
222 |
Arc direct(const Edge& e, bool d) const { return _graph->direct(e, d); } |
|
223 |
|
|
224 |
int id(const Node& v) const { return _graph->id(v); } |
|
225 |
int id(const Arc& a) const { return _graph->id(a); } |
|
226 |
int id(const Edge& e) const { return _graph->id(e); } |
|
227 |
|
|
228 |
Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); } |
|
229 |
Arc arcFromId(int ix) const { return _graph->arcFromId(ix); } |
|
230 |
Edge edgeFromId(int ix) const { return _graph->edgeFromId(ix); } |
|
231 |
|
|
232 |
int maxNodeId() const { return _graph->maxNodeId(); } |
|
233 |
int maxArcId() const { return _graph->maxArcId(); } |
|
234 |
int maxEdgeId() const { return _graph->maxEdgeId(); } |
|
235 |
|
|
236 |
typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier; |
|
237 |
NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); } |
|
238 |
|
|
239 |
typedef typename ItemSetTraits<Graph, Arc>::ItemNotifier ArcNotifier; |
|
240 |
ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); } |
|
241 |
|
|
242 |
typedef typename ItemSetTraits<Graph, Edge>::ItemNotifier EdgeNotifier; |
|
243 |
EdgeNotifier& notifier(Edge) const { return _graph->notifier(Edge()); } |
|
244 |
|
|
245 |
template <typename _Value> |
|
246 |
class NodeMap : public Graph::template NodeMap<_Value> { |
|
247 |
public: |
|
248 |
typedef typename Graph::template NodeMap<_Value> Parent; |
|
249 |
explicit NodeMap(const GraphAdaptorBase<Graph>& adapter) |
|
250 |
: Parent(*adapter._graph) {} |
|
251 |
NodeMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value) |
|
252 |
: Parent(*adapter._graph, value) {} |
|
253 |
|
|
254 |
private: |
|
255 |
NodeMap& operator=(const NodeMap& cmap) { |
|
256 |
return operator=<NodeMap>(cmap); |
|
257 |
} |
|
258 |
|
|
259 |
template <typename CMap> |
|
260 |
NodeMap& operator=(const CMap& cmap) { |
|
261 |
Parent::operator=(cmap); |
|
262 |
return *this; |
|
263 |
} |
|
264 |
|
|
265 |
}; |
|
266 |
|
|
267 |
template <typename _Value> |
|
268 |
class ArcMap : public Graph::template ArcMap<_Value> { |
|
269 |
public: |
|
270 |
typedef typename Graph::template ArcMap<_Value> Parent; |
|
271 |
explicit ArcMap(const GraphAdaptorBase<Graph>& adapter) |
|
272 |
: Parent(*adapter._graph) {} |
|
273 |
ArcMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value) |
|
274 |
: Parent(*adapter._graph, value) {} |
|
275 |
|
|
276 |
private: |
|
277 |
ArcMap& operator=(const ArcMap& cmap) { |
|
278 |
return operator=<ArcMap>(cmap); |
|
279 |
} |
|
280 |
|
|
281 |
template <typename CMap> |
|
282 |
ArcMap& operator=(const CMap& cmap) { |
|
283 |
Parent::operator=(cmap); |
|
284 |
return *this; |
|
285 |
} |
|
286 |
}; |
|
287 |
|
|
288 |
template <typename _Value> |
|
289 |
class EdgeMap : public Graph::template EdgeMap<_Value> { |
|
290 |
public: |
|
291 |
typedef typename Graph::template EdgeMap<_Value> Parent; |
|
292 |
explicit EdgeMap(const GraphAdaptorBase<Graph>& adapter) |
|
293 |
: Parent(*adapter._graph) {} |
|
294 |
EdgeMap(const GraphAdaptorBase<Graph>& adapter, const _Value& value) |
|
295 |
: Parent(*adapter._graph, value) {} |
|
296 |
|
|
297 |
private: |
|
298 |
EdgeMap& operator=(const EdgeMap& cmap) { |
|
299 |
return operator=<EdgeMap>(cmap); |
|
300 |
} |
|
301 |
|
|
302 |
template <typename CMap> |
|
303 |
EdgeMap& operator=(const CMap& cmap) { |
|
304 |
Parent::operator=(cmap); |
|
305 |
return *this; |
|
306 |
} |
|
307 |
}; |
|
308 |
|
|
309 |
}; |
|
310 |
|
|
311 |
template <typename _Digraph> |
|
312 |
class ReverseDigraphBase : public DigraphAdaptorBase<_Digraph> { |
|
313 |
public: |
|
314 |
typedef _Digraph Digraph; |
|
315 |
typedef DigraphAdaptorBase<_Digraph> Parent; |
|
316 |
protected: |
|
317 |
ReverseDigraphBase() : Parent() { } |
|
318 |
public: |
|
319 |
typedef typename Parent::Node Node; |
|
320 |
typedef typename Parent::Arc Arc; |
|
321 |
|
|
322 |
void firstIn(Arc& a, const Node& n) const { Parent::firstOut(a, n); } |
|
323 |
void firstOut(Arc& a, const Node& n ) const { Parent::firstIn(a, n); } |
|
324 |
|
|
325 |
void nextIn(Arc& a) const { Parent::nextOut(a); } |
|
326 |
void nextOut(Arc& a) const { Parent::nextIn(a); } |
|
327 |
|
|
328 |
Node source(const Arc& a) const { return Parent::target(a); } |
|
329 |
Node target(const Arc& a) const { return Parent::source(a); } |
|
330 |
|
|
331 |
Arc addArc(const Node& u, const Node& v) { return Parent::addArc(v, u); } |
|
332 |
|
|
333 |
typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; |
|
334 |
Arc findArc(const Node& u, const Node& v, |
|
335 |
const Arc& prev = INVALID) { |
|
336 |
return Parent::findArc(v, u, prev); |
|
337 |
} |
|
338 |
|
|
339 |
}; |
|
340 |
|
|
341 |
/// \ingroup graph_adaptors |
|
342 |
/// |
|
343 |
/// \brief A digraph adaptor which reverses the orientation of the arcs. |
|
344 |
/// |
|
345 |
/// ReverseDigraph reverses the arcs in the adapted digraph. The |
|
346 |
/// SubDigraph is conform to the \ref concepts::Digraph |
|
347 |
/// "Digraph concept". |
|
348 |
/// |
|
349 |
/// \tparam _Digraph It must be conform to the \ref concepts::Digraph |
|
350 |
/// "Digraph concept". The type can be specified to be const. |
|
351 |
template<typename _Digraph> |
|
352 |
class ReverseDigraph : |
|
353 |
public DigraphAdaptorExtender<ReverseDigraphBase<_Digraph> > { |
|
354 |
public: |
|
355 |
typedef _Digraph Digraph; |
|
356 |
typedef DigraphAdaptorExtender< |
|
357 |
ReverseDigraphBase<_Digraph> > Parent; |
|
358 |
protected: |
|
359 |
ReverseDigraph() { } |
|
360 |
public: |
|
361 |
|
|
362 |
/// \brief Constructor |
|
363 |
/// |
|
364 |
/// Creates a reverse digraph adaptor for the given digraph |
|
365 |
explicit ReverseDigraph(Digraph& digraph) { |
|
366 |
Parent::setDigraph(digraph); |
|
367 |
} |
|
368 |
}; |
|
369 |
|
|
370 |
/// \brief Just gives back a reverse digraph adaptor |
|
371 |
/// |
|
372 |
/// Just gives back a reverse digraph adaptor |
|
373 |
template<typename Digraph> |
|
374 |
ReverseDigraph<const Digraph> reverseDigraph(const Digraph& digraph) { |
|
375 |
return ReverseDigraph<const Digraph>(digraph); |
|
376 |
} |
|
377 |
|
|
378 |
template <typename _Digraph, typename _NodeFilterMap, |
|
379 |
typename _ArcFilterMap, bool _checked = true> |
|
380 |
class SubDigraphBase : public DigraphAdaptorBase<_Digraph> { |
|
381 |
public: |
|
382 |
typedef _Digraph Digraph; |
|
383 |
typedef _NodeFilterMap NodeFilterMap; |
|
384 |
typedef _ArcFilterMap ArcFilterMap; |
|
385 |
|
|
386 |
typedef SubDigraphBase Adaptor; |
|
387 |
typedef DigraphAdaptorBase<_Digraph> Parent; |
|
388 |
protected: |
|
389 |
NodeFilterMap* _node_filter; |
|
390 |
ArcFilterMap* _arc_filter; |
|
391 |
SubDigraphBase() |
|
392 |
: Parent(), _node_filter(0), _arc_filter(0) { } |
|
393 |
|
|
394 |
void setNodeFilterMap(NodeFilterMap& node_filter) { |
|
395 |
_node_filter = &node_filter; |
|
396 |
} |
|
397 |
void setArcFilterMap(ArcFilterMap& arc_filter) { |
|
398 |
_arc_filter = &arc_filter; |
|
399 |
} |
|
400 |
|
|
401 |
public: |
|
402 |
|
|
403 |
typedef typename Parent::Node Node; |
|
404 |
typedef typename Parent::Arc Arc; |
|
405 |
|
|
406 |
void first(Node& i) const { |
|
407 |
Parent::first(i); |
|
408 |
while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); |
|
409 |
} |
|
410 |
|
|
411 |
void first(Arc& i) const { |
|
412 |
Parent::first(i); |
|
413 |
while (i != INVALID && (!(*_arc_filter)[i] |
|
414 |
|| !(*_node_filter)[Parent::source(i)] |
|
415 |
|| !(*_node_filter)[Parent::target(i)])) |
|
416 |
Parent::next(i); |
|
417 |
} |
|
418 |
|
|
419 |
void firstIn(Arc& i, const Node& n) const { |
|
420 |
Parent::firstIn(i, n); |
|
421 |
while (i != INVALID && (!(*_arc_filter)[i] |
|
422 |
|| !(*_node_filter)[Parent::source(i)])) |
|
423 |
Parent::nextIn(i); |
|
424 |
} |
|
425 |
|
|
426 |
void firstOut(Arc& i, const Node& n) const { |
|
427 |
Parent::firstOut(i, n); |
|
428 |
while (i != INVALID && (!(*_arc_filter)[i] |
|
429 |
|| !(*_node_filter)[Parent::target(i)])) |
|
430 |
Parent::nextOut(i); |
|
431 |
} |
|
432 |
|
|
433 |
void next(Node& i) const { |
|
434 |
Parent::next(i); |
|
435 |
while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); |
|
436 |
} |
|
437 |
|
|
438 |
void next(Arc& i) const { |
|
439 |
Parent::next(i); |
|
440 |
while (i != INVALID && (!(*_arc_filter)[i] |
|
441 |
|| !(*_node_filter)[Parent::source(i)] |
|
442 |
|| !(*_node_filter)[Parent::target(i)])) |
|
443 |
Parent::next(i); |
|
444 |
} |
|
445 |
|
|
446 |
void nextIn(Arc& i) const { |
|
447 |
Parent::nextIn(i); |
|
448 |
while (i != INVALID && (!(*_arc_filter)[i] |
|
449 |
|| !(*_node_filter)[Parent::source(i)])) |
|
450 |
Parent::nextIn(i); |
|
451 |
} |
|
452 |
|
|
453 |
void nextOut(Arc& i) const { |
|
454 |
Parent::nextOut(i); |
|
455 |
while (i != INVALID && (!(*_arc_filter)[i] |
|
456 |
|| !(*_node_filter)[Parent::target(i)])) |
|
457 |
Parent::nextOut(i); |
|
458 |
} |
|
459 |
|
|
460 |
void hide(const Node& n) const { _node_filter->set(n, false); } |
|
461 |
void hide(const Arc& a) const { _arc_filter->set(a, false); } |
|
462 |
|
|
463 |
void unHide(const Node& n) const { _node_filter->set(n, true); } |
|
464 |
void unHide(const Arc& a) const { _arc_filter->set(a, true); } |
|
465 |
|
|
466 |
bool hidden(const Node& n) const { return !(*_node_filter)[n]; } |
|
467 |
bool hidden(const Arc& a) const { return !(*_arc_filter)[a]; } |
|
468 |
|
|
469 |
typedef False NodeNumTag; |
|
470 |
typedef False EdgeNumTag; |
|
471 |
|
|
472 |
typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; |
|
473 |
Arc findArc(const Node& source, const Node& target, |
|
474 |
const Arc& prev = INVALID) { |
|
475 |
if (!(*_node_filter)[source] || !(*_node_filter)[target]) { |
|
476 |
return INVALID; |
|
477 |
} |
|
478 |
Arc arc = Parent::findArc(source, target, prev); |
|
479 |
while (arc != INVALID && !(*_arc_filter)[arc]) { |
|
480 |
arc = Parent::findArc(source, target, arc); |
|
481 |
} |
|
482 |
return arc; |
|
483 |
} |
|
484 |
|
|
485 |
template <typename _Value> |
|
486 |
class NodeMap : public SubMapExtender<Adaptor, |
|
487 |
typename Parent::template NodeMap<_Value> > { |
|
488 |
public: |
|
489 |
typedef _Value Value; |
|
490 |
typedef SubMapExtender<Adaptor, typename Parent:: |
|
491 |
template NodeMap<Value> > MapParent; |
|
492 |
|
|
493 |
NodeMap(const Adaptor& adaptor) |
|
494 |
: MapParent(adaptor) {} |
|
495 |
NodeMap(const Adaptor& adaptor, const Value& value) |
|
496 |
: MapParent(adaptor, value) {} |
|
497 |
|
|
498 |
private: |
|
499 |
NodeMap& operator=(const NodeMap& cmap) { |
|
500 |
return operator=<NodeMap>(cmap); |
|
501 |
} |
|
502 |
|
|
503 |
template <typename CMap> |
|
504 |
NodeMap& operator=(const CMap& cmap) { |
|
505 |
MapParent::operator=(cmap); |
|
506 |
return *this; |
|
507 |
} |
|
508 |
}; |
|
509 |
|
|
510 |
template <typename _Value> |
|
511 |
class ArcMap : public SubMapExtender<Adaptor, |
|
512 |
typename Parent::template ArcMap<_Value> > { |
|
513 |
public: |
|
514 |
typedef _Value Value; |
|
515 |
typedef SubMapExtender<Adaptor, typename Parent:: |
|
516 |
template ArcMap<Value> > MapParent; |
|
517 |
|
|
518 |
ArcMap(const Adaptor& adaptor) |
|
519 |
: MapParent(adaptor) {} |
|
520 |
ArcMap(const Adaptor& adaptor, const Value& value) |
|
521 |
: MapParent(adaptor, value) {} |
|
522 |
|
|
523 |
private: |
|
524 |
ArcMap& operator=(const ArcMap& cmap) { |
|
525 |
return operator=<ArcMap>(cmap); |
|
526 |
} |
|
527 |
|
|
528 |
template <typename CMap> |
|
529 |
ArcMap& operator=(const CMap& cmap) { |
|
530 |
MapParent::operator=(cmap); |
|
531 |
return *this; |
|
532 |
} |
|
533 |
}; |
|
534 |
|
|
535 |
}; |
|
536 |
|
|
537 |
template <typename _Digraph, typename _NodeFilterMap, typename _ArcFilterMap> |
|
538 |
class SubDigraphBase<_Digraph, _NodeFilterMap, _ArcFilterMap, false> |
|
539 |
: public DigraphAdaptorBase<_Digraph> { |
|
540 |
public: |
|
541 |
typedef _Digraph Digraph; |
|
542 |
typedef _NodeFilterMap NodeFilterMap; |
|
543 |
typedef _ArcFilterMap ArcFilterMap; |
|
544 |
|
|
545 |
typedef SubDigraphBase Adaptor; |
|
546 |
typedef DigraphAdaptorBase<Digraph> Parent; |
|
547 |
protected: |
|
548 |
NodeFilterMap* _node_filter; |
|
549 |
ArcFilterMap* _arc_filter; |
|
550 |
SubDigraphBase() |
|
551 |
: Parent(), _node_filter(0), _arc_filter(0) { } |
|
552 |
|
|
553 |
void setNodeFilterMap(NodeFilterMap& node_filter) { |
|
554 |
_node_filter = &node_filter; |
|
555 |
} |
|
556 |
void setArcFilterMap(ArcFilterMap& arc_filter) { |
|
557 |
_arc_filter = &arc_filter; |
|
558 |
} |
|
559 |
|
|
560 |
public: |
|
561 |
|
|
562 |
typedef typename Parent::Node Node; |
|
563 |
typedef typename Parent::Arc Arc; |
|
564 |
|
|
565 |
void first(Node& i) const { |
|
566 |
Parent::first(i); |
|
567 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
|
568 |
} |
|
569 |
|
|
570 |
void first(Arc& i) const { |
|
571 |
Parent::first(i); |
|
572 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); |
|
573 |
} |
|
574 |
|
|
575 |
void firstIn(Arc& i, const Node& n) const { |
|
576 |
Parent::firstIn(i, n); |
|
577 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); |
|
578 |
} |
|
579 |
|
|
580 |
void firstOut(Arc& i, const Node& n) const { |
|
581 |
Parent::firstOut(i, n); |
|
582 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); |
|
583 |
} |
|
584 |
|
|
585 |
void next(Node& i) const { |
|
586 |
Parent::next(i); |
|
587 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
|
588 |
} |
|
589 |
void next(Arc& i) const { |
|
590 |
Parent::next(i); |
|
591 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); |
|
592 |
} |
|
593 |
void nextIn(Arc& i) const { |
|
594 |
Parent::nextIn(i); |
|
595 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); |
|
596 |
} |
|
597 |
|
|
598 |
void nextOut(Arc& i) const { |
|
599 |
Parent::nextOut(i); |
|
600 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); |
|
601 |
} |
|
602 |
|
|
603 |
void hide(const Node& n) const { _node_filter->set(n, false); } |
|
604 |
void hide(const Arc& e) const { _arc_filter->set(e, false); } |
|
605 |
|
|
606 |
void unHide(const Node& n) const { _node_filter->set(n, true); } |
|
607 |
void unHide(const Arc& e) const { _arc_filter->set(e, true); } |
|
608 |
|
|
609 |
bool hidden(const Node& n) const { return !(*_node_filter)[n]; } |
|
610 |
bool hidden(const Arc& e) const { return !(*_arc_filter)[e]; } |
|
611 |
|
|
612 |
typedef False NodeNumTag; |
|
613 |
typedef False EdgeNumTag; |
|
614 |
|
|
615 |
typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; |
|
616 |
Arc findArc(const Node& source, const Node& target, |
|
617 |
const Arc& prev = INVALID) { |
|
618 |
if (!(*_node_filter)[source] || !(*_node_filter)[target]) { |
|
619 |
return INVALID; |
|
620 |
} |
|
621 |
Arc arc = Parent::findArc(source, target, prev); |
|
622 |
while (arc != INVALID && !(*_arc_filter)[arc]) { |
|
623 |
arc = Parent::findArc(source, target, arc); |
|
624 |
} |
|
625 |
return arc; |
|
626 |
} |
|
627 |
|
|
628 |
template <typename _Value> |
|
629 |
class NodeMap : public SubMapExtender<Adaptor, |
|
630 |
typename Parent::template NodeMap<_Value> > { |
|
631 |
public: |
|
632 |
typedef _Value Value; |
|
633 |
typedef SubMapExtender<Adaptor, typename Parent:: |
|
634 |
template NodeMap<Value> > MapParent; |
|
635 |
|
|
636 |
NodeMap(const Adaptor& adaptor) |
|
637 |
: MapParent(adaptor) {} |
|
638 |
NodeMap(const Adaptor& adaptor, const Value& value) |
|
639 |
: MapParent(adaptor, value) {} |
|
640 |
|
|
641 |
private: |
|
642 |
NodeMap& operator=(const NodeMap& cmap) { |
|
643 |
return operator=<NodeMap>(cmap); |
|
644 |
} |
|
645 |
|
|
646 |
template <typename CMap> |
|
647 |
NodeMap& operator=(const CMap& cmap) { |
|
648 |
MapParent::operator=(cmap); |
|
649 |
return *this; |
|
650 |
} |
|
651 |
}; |
|
652 |
|
|
653 |
template <typename _Value> |
|
654 |
class ArcMap : public SubMapExtender<Adaptor, |
|
655 |
typename Parent::template ArcMap<_Value> > { |
|
656 |
public: |
|
657 |
typedef _Value Value; |
|
658 |
typedef SubMapExtender<Adaptor, typename Parent:: |
|
659 |
template ArcMap<Value> > MapParent; |
|
660 |
|
|
661 |
ArcMap(const Adaptor& adaptor) |
|
662 |
: MapParent(adaptor) {} |
|
663 |
ArcMap(const Adaptor& adaptor, const Value& value) |
|
664 |
: MapParent(adaptor, value) {} |
|
665 |
|
|
666 |
private: |
|
667 |
ArcMap& operator=(const ArcMap& cmap) { |
|
668 |
return operator=<ArcMap>(cmap); |
|
669 |
} |
|
670 |
|
|
671 |
template <typename CMap> |
|
672 |
ArcMap& operator=(const CMap& cmap) { |
|
673 |
MapParent::operator=(cmap); |
|
674 |
return *this; |
|
675 |
} |
|
676 |
}; |
|
677 |
|
|
678 |
}; |
|
679 |
|
|
680 |
/// \ingroup graph_adaptors |
|
681 |
/// |
|
682 |
/// \brief An adaptor for hiding nodes and arcs in a digraph |
|
683 |
/// |
|
684 |
/// SubDigraph hides nodes and arcs in a digraph. A bool node map |
|
685 |
/// and a bool arc map must be specified, which define the filters |
|
686 |
/// for nodes and arcs. Just the nodes and arcs with true value are |
|
687 |
/// shown in the subdigraph. The SubDigraph is conform to the \ref |
|
688 |
/// concepts::Digraph "Digraph concept". If the \c _checked parameter |
|
689 |
/// is true, then the arcs incident to filtered nodes are also |
|
690 |
/// filtered out. |
|
691 |
/// |
|
692 |
/// \tparam _Digraph It must be conform to the \ref |
|
693 |
/// concepts::Digraph "Digraph concept". The type can be specified |
|
694 |
/// to const. |
|
695 |
/// \tparam _NodeFilterMap A bool valued node map of the the adapted digraph. |
|
696 |
/// \tparam _ArcFilterMap A bool valued arc map of the the adapted digraph. |
|
697 |
/// \tparam _checked If the parameter is false then the arc filtering |
|
698 |
/// is not checked with respect to node filter. Otherwise, each arc |
|
699 |
/// is automatically filtered, which is incident to a filtered node. |
|
700 |
/// |
|
701 |
/// \see FilterNodes |
|
702 |
/// \see FilterArcs |
|
703 |
template<typename _Digraph, |
|
704 |
typename _NodeFilterMap = typename _Digraph::template NodeMap<bool>, |
|
705 |
typename _ArcFilterMap = typename _Digraph::template ArcMap<bool>, |
|
706 |
bool _checked = true> |
|
707 |
class SubDigraph |
|
708 |
: public DigraphAdaptorExtender< |
|
709 |
SubDigraphBase<_Digraph, _NodeFilterMap, _ArcFilterMap, _checked> > { |
|
710 |
public: |
|
711 |
typedef _Digraph Digraph; |
|
712 |
typedef _NodeFilterMap NodeFilterMap; |
|
713 |
typedef _ArcFilterMap ArcFilterMap; |
|
714 |
|
|
715 |
typedef DigraphAdaptorExtender< |
|
716 |
SubDigraphBase<Digraph, NodeFilterMap, ArcFilterMap, _checked> > |
|
717 |
Parent; |
|
718 |
|
|
719 |
typedef typename Parent::Node Node; |
|
720 |
typedef typename Parent::Arc Arc; |
|
721 |
|
|
722 |
protected: |
|
723 |
SubDigraph() { } |
|
724 |
public: |
|
725 |
|
|
726 |
/// \brief Constructor |
|
727 |
/// |
|
728 |
/// Creates a subdigraph for the given digraph with |
|
729 |
/// given node and arc map filters. |
|
730 |
SubDigraph(Digraph& digraph, NodeFilterMap& node_filter, |
|
731 |
ArcFilterMap& arc_filter) { |
|
732 |
setDigraph(digraph); |
|
733 |
setNodeFilterMap(node_filter); |
|
734 |
setArcFilterMap(arc_filter); |
|
735 |
} |
|
736 |
|
|
737 |
/// \brief Hides the node of the graph |
|
738 |
/// |
|
739 |
/// This function hides \c n in the digraph, i.e. the iteration |
|
740 |
/// jumps over it. This is done by simply setting the value of \c n |
|
741 |
/// to be false in the corresponding node-map. |
|
742 |
void hide(const Node& n) const { Parent::hide(n); } |
|
743 |
|
|
744 |
/// \brief Hides the arc of the graph |
|
745 |
/// |
|
746 |
/// This function hides \c a in the digraph, i.e. the iteration |
|
747 |
/// jumps over it. This is done by simply setting the value of \c a |
|
748 |
/// to be false in the corresponding arc-map. |
|
749 |
void hide(const Arc& a) const { Parent::hide(a); } |
|
750 |
|
|
751 |
/// \brief Unhides the node of the graph |
|
752 |
/// |
|
753 |
/// The value of \c n is set to be true in the node-map which stores |
|
754 |
/// hide information. If \c n was hidden previuosly, then it is shown |
|
755 |
/// again |
|
756 |
void unHide(const Node& n) const { Parent::unHide(n); } |
|
757 |
|
|
758 |
/// \brief Unhides the arc of the graph |
|
759 |
/// |
|
760 |
/// The value of \c a is set to be true in the arc-map which stores |
|
761 |
/// hide information. If \c a was hidden previuosly, then it is shown |
|
762 |
/// again |
|
763 |
void unHide(const Arc& a) const { Parent::unHide(a); } |
|
764 |
|
|
765 |
/// \brief Returns true if \c n is hidden. |
|
766 |
/// |
|
767 |
/// Returns true if \c n is hidden. |
|
768 |
/// |
|
769 |
bool hidden(const Node& n) const { return Parent::hidden(n); } |
|
770 |
|
|
771 |
/// \brief Returns true if \c a is hidden. |
|
772 |
/// |
|
773 |
/// Returns true if \c a is hidden. |
|
774 |
/// |
|
775 |
bool hidden(const Arc& a) const { return Parent::hidden(a); } |
|
776 |
|
|
777 |
}; |
|
778 |
|
|
779 |
/// \brief Just gives back a subdigraph |
|
780 |
/// |
|
781 |
/// Just gives back a subdigraph |
|
782 |
template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap> |
|
783 |
SubDigraph<const Digraph, NodeFilterMap, ArcFilterMap> |
|
784 |
subDigraph(const Digraph& digraph, NodeFilterMap& nfm, ArcFilterMap& afm) { |
|
785 |
return SubDigraph<const Digraph, NodeFilterMap, ArcFilterMap> |
|
786 |
(digraph, nfm, afm); |
|
787 |
} |
|
788 |
|
|
789 |
template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap> |
|
790 |
SubDigraph<const Digraph, const NodeFilterMap, ArcFilterMap> |
|
791 |
subDigraph(const Digraph& digraph, |
|
792 |
const NodeFilterMap& nfm, ArcFilterMap& afm) { |
|
793 |
return SubDigraph<const Digraph, const NodeFilterMap, ArcFilterMap> |
|
794 |
(digraph, nfm, afm); |
|
795 |
} |
|
796 |
|
|
797 |
template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap> |
|
798 |
SubDigraph<const Digraph, NodeFilterMap, const ArcFilterMap> |
|
799 |
subDigraph(const Digraph& digraph, |
|
800 |
NodeFilterMap& nfm, const ArcFilterMap& afm) { |
|
801 |
return SubDigraph<const Digraph, NodeFilterMap, const ArcFilterMap> |
|
802 |
(digraph, nfm, afm); |
|
803 |
} |
|
804 |
|
|
805 |
template<typename Digraph, typename NodeFilterMap, typename ArcFilterMap> |
|
806 |
SubDigraph<const Digraph, const NodeFilterMap, const ArcFilterMap> |
|
807 |
subDigraph(const Digraph& digraph, |
|
808 |
const NodeFilterMap& nfm, const ArcFilterMap& afm) { |
|
809 |
return SubDigraph<const Digraph, const NodeFilterMap, |
|
810 |
const ArcFilterMap>(digraph, nfm, afm); |
|
811 |
} |
|
812 |
|
|
813 |
|
|
814 |
template <typename _Graph, typename NodeFilterMap, |
|
815 |
typename EdgeFilterMap, bool _checked = true> |
|
816 |
class SubGraphBase : public GraphAdaptorBase<_Graph> { |
|
817 |
public: |
|
818 |
typedef _Graph Graph; |
|
819 |
typedef SubGraphBase Adaptor; |
|
820 |
typedef GraphAdaptorBase<_Graph> Parent; |
|
821 |
protected: |
|
822 |
|
|
823 |
NodeFilterMap* _node_filter_map; |
|
824 |
EdgeFilterMap* _edge_filter_map; |
|
825 |
|
|
826 |
SubGraphBase() |
|
827 |
: Parent(), _node_filter_map(0), _edge_filter_map(0) { } |
|
828 |
|
|
829 |
void setNodeFilterMap(NodeFilterMap& node_filter_map) { |
|
830 |
_node_filter_map=&node_filter_map; |
|
831 |
} |
|
832 |
void setEdgeFilterMap(EdgeFilterMap& edge_filter_map) { |
|
833 |
_edge_filter_map=&edge_filter_map; |
|
834 |
} |
|
835 |
|
|
836 |
public: |
|
837 |
|
|
838 |
typedef typename Parent::Node Node; |
|
839 |
typedef typename Parent::Arc Arc; |
|
840 |
typedef typename Parent::Edge Edge; |
|
841 |
|
|
842 |
void first(Node& i) const { |
|
843 |
Parent::first(i); |
|
844 |
while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i); |
|
845 |
} |
|
846 |
|
|
847 |
void first(Arc& i) const { |
|
848 |
Parent::first(i); |
|
849 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
|
850 |
|| !(*_node_filter_map)[Parent::source(i)] |
|
851 |
|| !(*_node_filter_map)[Parent::target(i)])) |
|
852 |
Parent::next(i); |
|
853 |
} |
|
854 |
|
|
855 |
void first(Edge& i) const { |
|
856 |
Parent::first(i); |
|
857 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
|
858 |
|| !(*_node_filter_map)[Parent::u(i)] |
|
859 |
|| !(*_node_filter_map)[Parent::v(i)])) |
|
860 |
Parent::next(i); |
|
861 |
} |
|
862 |
|
|
863 |
void firstIn(Arc& i, const Node& n) const { |
|
864 |
Parent::firstIn(i, n); |
|
865 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
|
866 |
|| !(*_node_filter_map)[Parent::source(i)])) |
|
867 |
Parent::nextIn(i); |
|
868 |
} |
|
869 |
|
|
870 |
void firstOut(Arc& i, const Node& n) const { |
|
871 |
Parent::firstOut(i, n); |
|
872 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
|
873 |
|| !(*_node_filter_map)[Parent::target(i)])) |
|
874 |
Parent::nextOut(i); |
|
875 |
} |
|
876 |
|
|
877 |
void firstInc(Edge& i, bool& d, const Node& n) const { |
|
878 |
Parent::firstInc(i, d, n); |
|
879 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
|
880 |
|| !(*_node_filter_map)[Parent::u(i)] |
|
881 |
|| !(*_node_filter_map)[Parent::v(i)])) |
|
882 |
Parent::nextInc(i, d); |
|
883 |
} |
|
884 |
|
|
885 |
void next(Node& i) const { |
|
886 |
Parent::next(i); |
|
887 |
while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i); |
|
888 |
} |
|
889 |
|
|
890 |
void next(Arc& i) const { |
|
891 |
Parent::next(i); |
|
892 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
|
893 |
|| !(*_node_filter_map)[Parent::source(i)] |
|
894 |
|| !(*_node_filter_map)[Parent::target(i)])) |
|
895 |
Parent::next(i); |
|
896 |
} |
|
897 |
|
|
898 |
void next(Edge& i) const { |
|
899 |
Parent::next(i); |
|
900 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
|
901 |
|| !(*_node_filter_map)[Parent::u(i)] |
|
902 |
|| !(*_node_filter_map)[Parent::v(i)])) |
|
903 |
Parent::next(i); |
|
904 |
} |
|
905 |
|
|
906 |
void nextIn(Arc& i) const { |
|
907 |
Parent::nextIn(i); |
|
908 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
|
909 |
|| !(*_node_filter_map)[Parent::source(i)])) |
|
910 |
Parent::nextIn(i); |
|
911 |
} |
|
912 |
|
|
913 |
void nextOut(Arc& i) const { |
|
914 |
Parent::nextOut(i); |
|
915 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
|
916 |
|| !(*_node_filter_map)[Parent::target(i)])) |
|
917 |
Parent::nextOut(i); |
|
918 |
} |
|
919 |
|
|
920 |
void nextInc(Edge& i, bool& d) const { |
|
921 |
Parent::nextInc(i, d); |
|
922 |
while (i!=INVALID && (!(*_edge_filter_map)[i] |
|
923 |
|| !(*_node_filter_map)[Parent::u(i)] |
|
924 |
|| !(*_node_filter_map)[Parent::v(i)])) |
|
925 |
Parent::nextInc(i, d); |
|
926 |
} |
|
927 |
|
|
928 |
void hide(const Node& n) const { _node_filter_map->set(n, false); } |
|
929 |
void hide(const Edge& e) const { _edge_filter_map->set(e, false); } |
|
930 |
|
|
931 |
void unHide(const Node& n) const { _node_filter_map->set(n, true); } |
|
932 |
void unHide(const Edge& e) const { _edge_filter_map->set(e, true); } |
|
933 |
|
|
934 |
bool hidden(const Node& n) const { return !(*_node_filter_map)[n]; } |
|
935 |
bool hidden(const Edge& e) const { return !(*_edge_filter_map)[e]; } |
|
936 |
|
|
937 |
typedef False NodeNumTag; |
|
938 |
typedef False EdgeNumTag; |
|
939 |
|
|
940 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
|
941 |
Arc findArc(const Node& u, const Node& v, |
|
942 |
const Arc& prev = INVALID) { |
|
943 |
if (!(*_node_filter_map)[u] || !(*_node_filter_map)[v]) { |
|
944 |
return INVALID; |
|
945 |
} |
|
946 |
Arc arc = Parent::findArc(u, v, prev); |
|
947 |
while (arc != INVALID && !(*_edge_filter_map)[arc]) { |
|
948 |
arc = Parent::findArc(u, v, arc); |
|
949 |
} |
|
950 |
return arc; |
|
951 |
} |
|
952 |
Edge findEdge(const Node& u, const Node& v, |
|
953 |
const Edge& prev = INVALID) { |
|
954 |
if (!(*_node_filter_map)[u] || !(*_node_filter_map)[v]) { |
|
955 |
return INVALID; |
|
956 |
} |
|
957 |
Edge edge = Parent::findEdge(u, v, prev); |
|
958 |
while (edge != INVALID && !(*_edge_filter_map)[edge]) { |
|
959 |
edge = Parent::findEdge(u, v, edge); |
|
960 |
} |
|
961 |
return edge; |
|
962 |
} |
|
963 |
|
|
964 |
template <typename _Value> |
|
965 |
class NodeMap : public SubMapExtender<Adaptor, |
|
966 |
typename Parent::template NodeMap<_Value> > { |
|
967 |
public: |
|
968 |
typedef _Value Value; |
|
969 |
typedef SubMapExtender<Adaptor, typename Parent:: |
|
970 |
template NodeMap<Value> > MapParent; |
|
971 |
|
|
972 |
NodeMap(const Adaptor& adaptor) |
|
973 |
: MapParent(adaptor) {} |
|
974 |
NodeMap(const Adaptor& adaptor, const Value& value) |
|
975 |
: MapParent(adaptor, value) {} |
|
976 |
|
|
977 |
private: |
|
978 |
NodeMap& operator=(const NodeMap& cmap) { |
|
979 |
return operator=<NodeMap>(cmap); |
|
980 |
} |
|
981 |
|
|
982 |
template <typename CMap> |
|
983 |
NodeMap& operator=(const CMap& cmap) { |
|
984 |
MapParent::operator=(cmap); |
|
985 |
return *this; |
|
986 |
} |
|
987 |
}; |
|
988 |
|
|
989 |
template <typename _Value> |
|
990 |
class ArcMap : public SubMapExtender<Adaptor, |
|
991 |
typename Parent::template ArcMap<_Value> > { |
|
992 |
public: |
|
993 |
typedef _Value Value; |
|
994 |
typedef SubMapExtender<Adaptor, typename Parent:: |
|
995 |
template ArcMap<Value> > MapParent; |
|
996 |
|
|
997 |
ArcMap(const Adaptor& adaptor) |
|
998 |
: MapParent(adaptor) {} |
|
999 |
ArcMap(const Adaptor& adaptor, const Value& value) |
|
1000 |
: MapParent(adaptor, value) {} |
|
1001 |
|
|
1002 |
private: |
|
1003 |
ArcMap& operator=(const ArcMap& cmap) { |
|
1004 |
return operator=<ArcMap>(cmap); |
|
1005 |
} |
|
1006 |
|
|
1007 |
template <typename CMap> |
|
1008 |
ArcMap& operator=(const CMap& cmap) { |
|
1009 |
MapParent::operator=(cmap); |
|
1010 |
return *this; |
|
1011 |
} |
|
1012 |
}; |
|
1013 |
|
|
1014 |
template <typename _Value> |
|
1015 |
class EdgeMap : public SubMapExtender<Adaptor, |
|
1016 |
typename Parent::template EdgeMap<_Value> > { |
|
1017 |
public: |
|
1018 |
typedef _Value Value; |
|
1019 |
typedef SubMapExtender<Adaptor, typename Parent:: |
|
1020 |
template EdgeMap<Value> > MapParent; |
|
1021 |
|
|
1022 |
EdgeMap(const Adaptor& adaptor) |
|
1023 |
: MapParent(adaptor) {} |
|
1024 |
|
|
1025 |
EdgeMap(const Adaptor& adaptor, const Value& value) |
|
1026 |
: MapParent(adaptor, value) {} |
|
1027 |
|
|
1028 |
private: |
|
1029 |
EdgeMap& operator=(const EdgeMap& cmap) { |
|
1030 |
return operator=<EdgeMap>(cmap); |
|
1031 |
} |
|
1032 |
|
|
1033 |
template <typename CMap> |
|
1034 |
EdgeMap& operator=(const CMap& cmap) { |
|
1035 |
MapParent::operator=(cmap); |
|
1036 |
return *this; |
|
1037 |
} |
|
1038 |
}; |
|
1039 |
|
|
1040 |
}; |
|
1041 |
|
|
1042 |
template <typename _Graph, typename NodeFilterMap, typename EdgeFilterMap> |
|
1043 |
class SubGraphBase<_Graph, NodeFilterMap, EdgeFilterMap, false> |
|
1044 |
: public GraphAdaptorBase<_Graph> { |
|
1045 |
public: |
|
1046 |
typedef _Graph Graph; |
|
1047 |
typedef SubGraphBase Adaptor; |
|
1048 |
typedef GraphAdaptorBase<_Graph> Parent; |
|
1049 |
protected: |
|
1050 |
NodeFilterMap* _node_filter_map; |
|
1051 |
EdgeFilterMap* _edge_filter_map; |
|
1052 |
SubGraphBase() : Parent(), |
|
1053 |
_node_filter_map(0), _edge_filter_map(0) { } |
|
1054 |
|
|
1055 |
void setNodeFilterMap(NodeFilterMap& node_filter_map) { |
|
1056 |
_node_filter_map=&node_filter_map; |
|
1057 |
} |
|
1058 |
void setEdgeFilterMap(EdgeFilterMap& edge_filter_map) { |
|
1059 |
_edge_filter_map=&edge_filter_map; |
|
1060 |
} |
|
1061 |
|
|
1062 |
public: |
|
1063 |
|
|
1064 |
typedef typename Parent::Node Node; |
|
1065 |
typedef typename Parent::Arc Arc; |
|
1066 |
typedef typename Parent::Edge Edge; |
|
1067 |
|
|
1068 |
void first(Node& i) const { |
|
1069 |
Parent::first(i); |
|
1070 |
while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i); |
|
1071 |
} |
|
1072 |
|
|
1073 |
void first(Arc& i) const { |
|
1074 |
Parent::first(i); |
|
1075 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i); |
|
1076 |
} |
|
1077 |
|
|
1078 |
void first(Edge& i) const { |
|
1079 |
Parent::first(i); |
|
1080 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i); |
|
1081 |
} |
|
1082 |
|
|
1083 |
void firstIn(Arc& i, const Node& n) const { |
|
1084 |
Parent::firstIn(i, n); |
|
1085 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextIn(i); |
|
1086 |
} |
|
1087 |
|
|
1088 |
void firstOut(Arc& i, const Node& n) const { |
|
1089 |
Parent::firstOut(i, n); |
|
1090 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextOut(i); |
|
1091 |
} |
|
1092 |
|
|
1093 |
void firstInc(Edge& i, bool& d, const Node& n) const { |
|
1094 |
Parent::firstInc(i, d, n); |
|
1095 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextInc(i, d); |
|
1096 |
} |
|
1097 |
|
|
1098 |
void next(Node& i) const { |
|
1099 |
Parent::next(i); |
|
1100 |
while (i!=INVALID && !(*_node_filter_map)[i]) Parent::next(i); |
|
1101 |
} |
|
1102 |
void next(Arc& i) const { |
|
1103 |
Parent::next(i); |
|
1104 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i); |
|
1105 |
} |
|
1106 |
void next(Edge& i) const { |
|
1107 |
Parent::next(i); |
|
1108 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::next(i); |
|
1109 |
} |
|
1110 |
void nextIn(Arc& i) const { |
|
1111 |
Parent::nextIn(i); |
|
1112 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextIn(i); |
|
1113 |
} |
|
1114 |
|
|
1115 |
void nextOut(Arc& i) const { |
|
1116 |
Parent::nextOut(i); |
|
1117 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextOut(i); |
|
1118 |
} |
|
1119 |
void nextInc(Edge& i, bool& d) const { |
|
1120 |
Parent::nextInc(i, d); |
|
1121 |
while (i!=INVALID && !(*_edge_filter_map)[i]) Parent::nextInc(i, d); |
|
1122 |
} |
|
1123 |
|
|
1124 |
void hide(const Node& n) const { _node_filter_map->set(n, false); } |
|
1125 |
void hide(const Edge& e) const { _edge_filter_map->set(e, false); } |
|
1126 |
|
|
1127 |
void unHide(const Node& n) const { _node_filter_map->set(n, true); } |
|
1128 |
void unHide(const Edge& e) const { _edge_filter_map->set(e, true); } |
|
1129 |
|
|
1130 |
bool hidden(const Node& n) const { return !(*_node_filter_map)[n]; } |
|
1131 |
bool hidden(const Edge& e) const { return !(*_edge_filter_map)[e]; } |
|
1132 |
|
|
1133 |
typedef False NodeNumTag; |
|
1134 |
typedef False EdgeNumTag; |
|
1135 |
|
|
1136 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
|
1137 |
Arc findArc(const Node& u, const Node& v, |
|
1138 |
const Arc& prev = INVALID) { |
|
1139 |
Arc arc = Parent::findArc(u, v, prev); |
|
1140 |
while (arc != INVALID && !(*_edge_filter_map)[arc]) { |
|
1141 |
arc = Parent::findArc(u, v, arc); |
|
1142 |
} |
|
1143 |
return arc; |
|
1144 |
} |
|
1145 |
Edge findEdge(const Node& u, const Node& v, |
|
1146 |
const Edge& prev = INVALID) { |
|
1147 |
Edge edge = Parent::findEdge(u, v, prev); |
|
1148 |
while (edge != INVALID && !(*_edge_filter_map)[edge]) { |
|
1149 |
edge = Parent::findEdge(u, v, edge); |
|
1150 |
} |
|
1151 |
return edge; |
|
1152 |
} |
|
1153 |
|
|
1154 |
template <typename _Value> |
|
1155 |
class NodeMap : public SubMapExtender<Adaptor, |
|
1156 |
typename Parent::template NodeMap<_Value> > { |
|
1157 |
public: |
|
1158 |
typedef _Value Value; |
|
1159 |
typedef SubMapExtender<Adaptor, typename Parent:: |
|
1160 |
template NodeMap<Value> > MapParent; |
|
1161 |
|
|
1162 |
NodeMap(const Adaptor& adaptor) |
|
1163 |
: MapParent(adaptor) {} |
|
1164 |
NodeMap(const Adaptor& adaptor, const Value& value) |
|
1165 |
: MapParent(adaptor, value) {} |
|
1166 |
|
|
1167 |
private: |
|
1168 |
NodeMap& operator=(const NodeMap& cmap) { |
|
1169 |
return operator=<NodeMap>(cmap); |
|
1170 |
} |
|
1171 |
|
|
1172 |
template <typename CMap> |
|
1173 |
NodeMap& operator=(const CMap& cmap) { |
|
1174 |
MapParent::operator=(cmap); |
|
1175 |
return *this; |
|
1176 |
} |
|
1177 |
}; |
|
1178 |
|
|
1179 |
template <typename _Value> |
|
1180 |
class ArcMap : public SubMapExtender<Adaptor, |
|
1181 |
typename Parent::template ArcMap<_Value> > { |
|
1182 |
public: |
|
1183 |
typedef _Value Value; |
|
1184 |
typedef SubMapExtender<Adaptor, typename Parent:: |
|
1185 |
template ArcMap<Value> > MapParent; |
|
1186 |
|
|
1187 |
ArcMap(const Adaptor& adaptor) |
|
1188 |
: MapParent(adaptor) {} |
|
1189 |
ArcMap(const Adaptor& adaptor, const Value& value) |
|
1190 |
: MapParent(adaptor, value) {} |
|
1191 |
|
|
1192 |
private: |
|
1193 |
ArcMap& operator=(const ArcMap& cmap) { |
|
1194 |
return operator=<ArcMap>(cmap); |
|
1195 |
} |
|
1196 |
|
|
1197 |
template <typename CMap> |
|
1198 |
ArcMap& operator=(const CMap& cmap) { |
|
1199 |
MapParent::operator=(cmap); |
|
1200 |
return *this; |
|
1201 |
} |
|
1202 |
}; |
|
1203 |
|
|
1204 |
template <typename _Value> |
|
1205 |
class EdgeMap : public SubMapExtender<Adaptor, |
|
1206 |
typename Parent::template EdgeMap<_Value> > { |
|
1207 |
public: |
|
1208 |
typedef _Value Value; |
|
1209 |
typedef SubMapExtender<Adaptor, typename Parent:: |
|
1210 |
template EdgeMap<Value> > MapParent; |
|
1211 |
|
|
1212 |
EdgeMap(const Adaptor& adaptor) |
|
1213 |
: MapParent(adaptor) {} |
|
1214 |
|
|
1215 |
EdgeMap(const Adaptor& adaptor, const _Value& value) |
|
1216 |
: MapParent(adaptor, value) {} |
|
1217 |
|
|
1218 |
private: |
|
1219 |
EdgeMap& operator=(const EdgeMap& cmap) { |
|
1220 |
return operator=<EdgeMap>(cmap); |
|
1221 |
} |
|
1222 |
|
|
1223 |
template <typename CMap> |
|
1224 |
EdgeMap& operator=(const CMap& cmap) { |
|
1225 |
MapParent::operator=(cmap); |
|
1226 |
return *this; |
|
1227 |
} |
|
1228 |
}; |
|
1229 |
|
|
1230 |
}; |
|
1231 |
|
|
1232 |
/// \ingroup graph_adaptors |
|
1233 |
/// |
|
1234 |
/// \brief A graph adaptor for hiding nodes and edges in an |
|
1235 |
/// undirected graph. |
|
1236 |
/// |
|
1237 |
/// SubGraph hides nodes and edges in a graph. A bool node map and a |
|
1238 |
/// bool edge map must be specified, which define the filters for |
|
1239 |
/// nodes and edges. Just the nodes and edges with true value are |
|
1240 |
/// shown in the subgraph. The SubGraph is conform to the \ref |
|
1241 |
/// concepts::Graph "Graph concept". If the \c _checked parameter is |
|
1242 |
/// true, then the edges incident to filtered nodes are also |
|
1243 |
/// filtered out. |
|
1244 |
/// |
|
1245 |
/// \tparam _Graph It must be conform to the \ref |
|
1246 |
/// concepts::Graph "Graph concept". The type can be specified |
|
1247 |
/// to const. |
|
1248 |
/// \tparam _NodeFilterMap A bool valued node map of the the adapted graph. |
|
1249 |
/// \tparam _EdgeFilterMap A bool valued edge map of the the adapted graph. |
|
1250 |
/// \tparam _checked If the parameter is false then the edge filtering |
|
1251 |
/// is not checked with respect to node filter. Otherwise, each edge |
|
1252 |
/// is automatically filtered, which is incident to a filtered node. |
|
1253 |
/// |
|
1254 |
/// \see FilterNodes |
|
1255 |
/// \see FilterEdges |
|
1256 |
template<typename _Graph, typename NodeFilterMap, |
|
1257 |
typename EdgeFilterMap, bool _checked = true> |
|
1258 |
class SubGraph |
|
1259 |
: public GraphAdaptorExtender< |
|
1260 |
SubGraphBase<_Graph, NodeFilterMap, EdgeFilterMap, _checked> > { |
|
1261 |
public: |
|
1262 |
typedef _Graph Graph; |
|
1263 |
typedef GraphAdaptorExtender< |
|
1264 |
SubGraphBase<_Graph, NodeFilterMap, EdgeFilterMap> > Parent; |
|
1265 |
|
|
1266 |
typedef typename Parent::Node Node; |
|
1267 |
typedef typename Parent::Edge Edge; |
|
1268 |
|
|
1269 |
protected: |
|
1270 |
SubGraph() { } |
|
1271 |
public: |
|
1272 |
|
|
1273 |
/// \brief Constructor |
|
1274 |
/// |
|
1275 |
/// Creates a subgraph for the given graph with given node and |
|
1276 |
/// edge map filters. |
|
1277 |
SubGraph(Graph& _graph, NodeFilterMap& node_filter_map, |
|
1278 |
EdgeFilterMap& edge_filter_map) { |
|
1279 |
setGraph(_graph); |
|
1280 |
setNodeFilterMap(node_filter_map); |
|
1281 |
setEdgeFilterMap(edge_filter_map); |
|
1282 |
} |
|
1283 |
|
|
1284 |
/// \brief Hides the node of the graph |
|
1285 |
/// |
|
1286 |
/// This function hides \c n in the graph, i.e. the iteration |
|
1287 |
/// jumps over it. This is done by simply setting the value of \c n |
|
1288 |
/// to be false in the corresponding node-map. |
|
1289 |
void hide(const Node& n) const { Parent::hide(n); } |
|
1290 |
|
|
1291 |
/// \brief Hides the edge of the graph |
|
1292 |
/// |
|
1293 |
/// This function hides \c e in the graph, i.e. the iteration |
|
1294 |
/// jumps over it. This is done by simply setting the value of \c e |
|
1295 |
/// to be false in the corresponding edge-map. |
|
1296 |
void hide(const Edge& e) const { Parent::hide(e); } |
|
1297 |
|
|
1298 |
/// \brief Unhides the node of the graph |
|
1299 |
/// |
|
1300 |
/// The value of \c n is set to be true in the node-map which stores |
|
1301 |
/// hide information. If \c n was hidden previuosly, then it is shown |
|
1302 |
/// again |
|
1303 |
void unHide(const Node& n) const { Parent::unHide(n); } |
|
1304 |
|
|
1305 |
/// \brief Unhides the edge of the graph |
|
1306 |
/// |
|
1307 |
/// The value of \c e is set to be true in the edge-map which stores |
|
1308 |
/// hide information. If \c e was hidden previuosly, then it is shown |
|
1309 |
/// again |
|
1310 |
void unHide(const Edge& e) const { Parent::unHide(e); } |
|
1311 |
|
|
1312 |
/// \brief Returns true if \c n is hidden. |
|
1313 |
/// |
|
1314 |
/// Returns true if \c n is hidden. |
|
1315 |
/// |
|
1316 |
bool hidden(const Node& n) const { return Parent::hidden(n); } |
|
1317 |
|
|
1318 |
/// \brief Returns true if \c e is hidden. |
|
1319 |
/// |
|
1320 |
/// Returns true if \c e is hidden. |
|
1321 |
/// |
|
1322 |
bool hidden(const Edge& e) const { return Parent::hidden(e); } |
|
1323 |
}; |
|
1324 |
|
|
1325 |
/// \brief Just gives back a subgraph |
|
1326 |
/// |
|
1327 |
/// Just gives back a subgraph |
|
1328 |
template<typename Graph, typename NodeFilterMap, typename ArcFilterMap> |
|
1329 |
SubGraph<const Graph, NodeFilterMap, ArcFilterMap> |
|
1330 |
subGraph(const Graph& graph, NodeFilterMap& nfm, ArcFilterMap& efm) { |
|
1331 |
return SubGraph<const Graph, NodeFilterMap, ArcFilterMap>(graph, nfm, efm); |
|
1332 |
} |
|
1333 |
|
|
1334 |
template<typename Graph, typename NodeFilterMap, typename ArcFilterMap> |
|
1335 |
SubGraph<const Graph, const NodeFilterMap, ArcFilterMap> |
|
1336 |
subGraph(const Graph& graph, |
|
1337 |
const NodeFilterMap& nfm, ArcFilterMap& efm) { |
|
1338 |
return SubGraph<const Graph, const NodeFilterMap, ArcFilterMap> |
|
1339 |
(graph, nfm, efm); |
|
1340 |
} |
|
1341 |
|
|
1342 |
template<typename Graph, typename NodeFilterMap, typename ArcFilterMap> |
|
1343 |
SubGraph<const Graph, NodeFilterMap, const ArcFilterMap> |
|
1344 |
subGraph(const Graph& graph, |
|
1345 |
NodeFilterMap& nfm, const ArcFilterMap& efm) { |
|
1346 |
return SubGraph<const Graph, NodeFilterMap, const ArcFilterMap> |
|
1347 |
(graph, nfm, efm); |
|
1348 |
} |
|
1349 |
|
|
1350 |
template<typename Graph, typename NodeFilterMap, typename ArcFilterMap> |
|
1351 |
SubGraph<const Graph, const NodeFilterMap, const ArcFilterMap> |
|
1352 |
subGraph(const Graph& graph, |
|
1353 |
const NodeFilterMap& nfm, const ArcFilterMap& efm) { |
|
1354 |
return SubGraph<const Graph, const NodeFilterMap, const ArcFilterMap> |
|
1355 |
(graph, nfm, efm); |
|
1356 |
} |
|
1357 |
|
|
1358 |
/// \ingroup graph_adaptors |
|
1359 |
/// |
|
1360 |
/// \brief An adaptor for hiding nodes from a digraph or a graph. |
|
1361 |
/// |
|
1362 |
/// FilterNodes adaptor hides nodes in a graph or a digraph. A bool |
|
1363 |
/// node map must be specified, which defines the filters for |
|
1364 |
/// nodes. Just the unfiltered nodes and the arcs or edges incident |
|
1365 |
/// to unfiltered nodes are shown in the subdigraph or subgraph. The |
|
1366 |
/// FilterNodes is conform to the \ref concepts::Digraph |
|
1367 |
/// "Digraph concept" or \ref concepts::Graph "Graph concept" depending |
|
1368 |
/// on the \c _Digraph template parameter. If the \c _checked |
|
1369 |
/// parameter is true, then the arc or edges incident to filtered nodes |
|
1370 |
/// are also filtered out. |
|
1371 |
/// |
|
1372 |
/// \tparam _Digraph It must be conform to the \ref |
|
1373 |
/// concepts::Digraph "Digraph concept" or \ref concepts::Graph |
|
1374 |
/// "Graph concept". The type can be specified to be const. |
|
1375 |
/// \tparam _NodeFilterMap A bool valued node map of the the adapted graph. |
|
1376 |
/// \tparam _checked If the parameter is false then the arc or edge |
|
1377 |
/// filtering is not checked with respect to node filter. In this |
|
1378 |
/// case just isolated nodes can be filtered out from the |
|
1379 |
/// graph. |
|
1380 |
#ifdef DOXYGEN |
|
1381 |
template<typename _Digraph, |
|
1382 |
typename _NodeFilterMap = typename _Digraph::template NodeMap<bool>, |
|
1383 |
bool _checked = true> |
|
1384 |
#else |
|
1385 |
template<typename _Digraph, |
|
1386 |
typename _NodeFilterMap = typename _Digraph::template NodeMap<bool>, |
|
1387 |
bool _checked = true, |
|
1388 |
typename Enable = void> |
|
1389 |
#endif |
|
1390 |
class FilterNodes |
|
1391 |
: public SubDigraph<_Digraph, _NodeFilterMap, |
|
1392 |
ConstMap<typename _Digraph::Arc, bool>, _checked> { |
|
1393 |
public: |
|
1394 |
|
|
1395 |
typedef _Digraph Digraph; |
|
1396 |
typedef _NodeFilterMap NodeFilterMap; |
|
1397 |
|
|
1398 |
typedef SubDigraph<Digraph, NodeFilterMap, |
|
1399 |
ConstMap<typename Digraph::Arc, bool>, _checked> |
|
1400 |
Parent; |
|
1401 |
|
|
1402 |
typedef typename Parent::Node Node; |
|
1403 |
|
|
1404 |
protected: |
|
1405 |
ConstMap<typename Digraph::Arc, bool> const_true_map; |
|
1406 |
|
|
1407 |
FilterNodes() : const_true_map(true) { |
|
1408 |
Parent::setArcFilterMap(const_true_map); |
|
1409 |
} |
|
1410 |
|
|
1411 |
public: |
|
1412 |
|
|
1413 |
/// \brief Constructor |
|
1414 |
/// |
|
1415 |
/// Creates an adaptor for the given digraph or graph with |
|
1416 |
/// given node filter map. |
|
1417 |
FilterNodes(Digraph& _digraph, NodeFilterMap& node_filter) : |
|
1418 |
Parent(), const_true_map(true) { |
|
1419 |
Parent::setDigraph(_digraph); |
|
1420 |
Parent::setNodeFilterMap(node_filter); |
|
1421 |
Parent::setArcFilterMap(const_true_map); |
|
1422 |
} |
|
1423 |
|
|
1424 |
/// \brief Hides the node of the graph |
|
1425 |
/// |
|
1426 |
/// This function hides \c n in the digraph or graph, i.e. the iteration |
|
1427 |
/// jumps over it. This is done by simply setting the value of \c n |
|
1428 |
/// to be false in the corresponding node map. |
|
1429 |
void hide(const Node& n) const { Parent::hide(n); } |
|
1430 |
|
|
1431 |
/// \brief Unhides the node of the graph |
|
1432 |
/// |
|
1433 |
/// The value of \c n is set to be true in the node-map which stores |
|
1434 |
/// hide information. If \c n was hidden previuosly, then it is shown |
|
1435 |
/// again |
|
1436 |
void unHide(const Node& n) const { Parent::unHide(n); } |
|
1437 |
|
|
1438 |
/// \brief Returns true if \c n is hidden. |
|
1439 |
/// |
|
1440 |
/// Returns true if \c n is hidden. |
|
1441 |
/// |
|
1442 |
bool hidden(const Node& n) const { return Parent::hidden(n); } |
|
1443 |
|
|
1444 |
}; |
|
1445 |
|
|
1446 |
template<typename _Graph, typename _NodeFilterMap, bool _checked> |
|
1447 |
class FilterNodes<_Graph, _NodeFilterMap, _checked, |
|
1448 |
typename enable_if<UndirectedTagIndicator<_Graph> >::type> |
|
1449 |
: public SubGraph<_Graph, _NodeFilterMap, |
|
1450 |
ConstMap<typename _Graph::Edge, bool>, _checked> { |
|
1451 |
public: |
|
1452 |
typedef _Graph Graph; |
|
1453 |
typedef _NodeFilterMap NodeFilterMap; |
|
1454 |
typedef SubGraph<Graph, NodeFilterMap, |
|
1455 |
ConstMap<typename Graph::Edge, bool> > Parent; |
|
1456 |
|
|
1457 |
typedef typename Parent::Node Node; |
|
1458 |
protected: |
|
1459 |
ConstMap<typename Graph::Edge, bool> const_true_map; |
|
1460 |
|
|
1461 |
FilterNodes() : const_true_map(true) { |
|
1462 |
Parent::setEdgeFilterMap(const_true_map); |
|
1463 |
} |
|
1464 |
|
|
1465 |
public: |
|
1466 |
|
|
1467 |
FilterNodes(Graph& _graph, NodeFilterMap& node_filter_map) : |
|
1468 |
Parent(), const_true_map(true) { |
|
1469 |
Parent::setGraph(_graph); |
|
1470 |
Parent::setNodeFilterMap(node_filter_map); |
|
1471 |
Parent::setEdgeFilterMap(const_true_map); |
|
1472 |
} |
|
1473 |
|
|
1474 |
void hide(const Node& n) const { Parent::hide(n); } |
|
1475 |
void unHide(const Node& n) const { Parent::unHide(n); } |
|
1476 |
bool hidden(const Node& n) const { return Parent::hidden(n); } |
|
1477 |
|
|
1478 |
}; |
|
1479 |
|
|
1480 |
|
|
1481 |
/// \brief Just gives back a FilterNodes adaptor |
|
1482 |
/// |
|
1483 |
/// Just gives back a FilterNodes adaptor |
|
1484 |
template<typename Digraph, typename NodeFilterMap> |
|
1485 |
FilterNodes<const Digraph, NodeFilterMap> |
|
1486 |
filterNodes(const Digraph& digraph, NodeFilterMap& nfm) { |
|
1487 |
return FilterNodes<const Digraph, NodeFilterMap>(digraph, nfm); |
|
1488 |
} |
|
1489 |
|
|
1490 |
template<typename Digraph, typename NodeFilterMap> |
|
1491 |
FilterNodes<const Digraph, const NodeFilterMap> |
|
1492 |
filterNodes(const Digraph& digraph, const NodeFilterMap& nfm) { |
|
1493 |
return FilterNodes<const Digraph, const NodeFilterMap>(digraph, nfm); |
|
1494 |
} |
|
1495 |
|
|
1496 |
/// \ingroup graph_adaptors |
|
1497 |
/// |
|
1498 |
/// \brief An adaptor for hiding arcs from a digraph. |
|
1499 |
/// |
|
1500 |
/// FilterArcs adaptor hides arcs in a digraph. A bool arc map must |
|
1501 |
/// be specified, which defines the filters for arcs. Just the |
|
1502 |
/// unfiltered arcs are shown in the subdigraph. The FilterArcs is |
|
1503 |
/// conform to the \ref concepts::Digraph "Digraph concept". |
|
1504 |
/// |
|
1505 |
/// \tparam _Digraph It must be conform to the \ref concepts::Digraph |
|
1506 |
/// "Digraph concept". The type can be specified to be const. |
|
1507 |
/// \tparam _ArcFilterMap A bool valued arc map of the the adapted |
|
1508 |
/// graph. |
|
1509 |
template<typename _Digraph, typename _ArcFilterMap> |
|
1510 |
class FilterArcs : |
|
1511 |
public SubDigraph<_Digraph, ConstMap<typename _Digraph::Node, bool>, |
|
1512 |
_ArcFilterMap, false> { |
|
1513 |
public: |
|
1514 |
typedef _Digraph Digraph; |
|
1515 |
typedef _ArcFilterMap ArcFilterMap; |
|
1516 |
|
|
1517 |
typedef SubDigraph<Digraph, ConstMap<typename Digraph::Node, bool>, |
|
1518 |
ArcFilterMap, false> Parent; |
|
1519 |
|
|
1520 |
typedef typename Parent::Arc Arc; |
|
1521 |
|
|
1522 |
protected: |
|
1523 |
ConstMap<typename Digraph::Node, bool> const_true_map; |
|
1524 |
|
|
1525 |
FilterArcs() : const_true_map(true) { |
|
1526 |
Parent::setNodeFilterMap(const_true_map); |
|
1527 |
} |
|
1528 |
|
|
1529 |
public: |
|
1530 |
|
|
1531 |
/// \brief Constructor |
|
1532 |
/// |
|
1533 |
/// Creates a FilterArcs adaptor for the given graph with |
|
1534 |
/// given arc map filter. |
|
1535 |
FilterArcs(Digraph& digraph, ArcFilterMap& arc_filter) |
|
1536 |
: Parent(), const_true_map(true) { |
|
1537 |
Parent::setDigraph(digraph); |
|
1538 |
Parent::setNodeFilterMap(const_true_map); |
|
1539 |
Parent::setArcFilterMap(arc_filter); |
|
1540 |
} |
|
1541 |
|
|
1542 |
/// \brief Hides the arc of the graph |
|
1543 |
/// |
|
1544 |
/// This function hides \c a in the graph, i.e. the iteration |
|
1545 |
/// jumps over it. This is done by simply setting the value of \c a |
|
1546 |
/// to be false in the corresponding arc map. |
|
1547 |
void hide(const Arc& a) const { Parent::hide(a); } |
|
1548 |
|
|
1549 |
/// \brief Unhides the arc of the graph |
|
1550 |
/// |
|
1551 |
/// The value of \c a is set to be true in the arc-map which stores |
|
1552 |
/// hide information. If \c a was hidden previuosly, then it is shown |
|
1553 |
/// again |
|
1554 |
void unHide(const Arc& a) const { Parent::unHide(a); } |
|
1555 |
|
|
1556 |
/// \brief Returns true if \c a is hidden. |
|
1557 |
/// |
|
1558 |
/// Returns true if \c a is hidden. |
|
1559 |
/// |
|
1560 |
bool hidden(const Arc& a) const { return Parent::hidden(a); } |
|
1561 |
|
|
1562 |
}; |
|
1563 |
|
|
1564 |
/// \brief Just gives back an FilterArcs adaptor |
|
1565 |
/// |
|
1566 |
/// Just gives back an FilterArcs adaptor |
|
1567 |
template<typename Digraph, typename ArcFilterMap> |
|
1568 |
FilterArcs<const Digraph, ArcFilterMap> |
|
1569 |
filterArcs(const Digraph& digraph, ArcFilterMap& afm) { |
|
1570 |
return FilterArcs<const Digraph, ArcFilterMap>(digraph, afm); |
|
1571 |
} |
|
1572 |
|
|
1573 |
template<typename Digraph, typename ArcFilterMap> |
|
1574 |
FilterArcs<const Digraph, const ArcFilterMap> |
|
1575 |
filterArcs(const Digraph& digraph, const ArcFilterMap& afm) { |
|
1576 |
return FilterArcs<const Digraph, const ArcFilterMap>(digraph, afm); |
|
1577 |
} |
|
1578 |
|
|
1579 |
/// \ingroup graph_adaptors |
|
1580 |
/// |
|
1581 |
/// \brief An adaptor for hiding edges from a graph. |
|
1582 |
/// |
|
1583 |
/// FilterEdges adaptor hides edges in a digraph. A bool edge map must |
|
1584 |
/// be specified, which defines the filters for edges. Just the |
|
1585 |
/// unfiltered edges are shown in the subdigraph. The FilterEdges is |
|
1586 |
/// conform to the \ref concepts::Graph "Graph concept". |
|
1587 |
/// |
|
1588 |
/// \tparam _Graph It must be conform to the \ref concepts::Graph |
|
1589 |
/// "Graph concept". The type can be specified to be const. |
|
1590 |
/// \tparam _EdgeFilterMap A bool valued edge map of the the adapted |
|
1591 |
/// graph. |
|
1592 |
template<typename _Graph, typename _EdgeFilterMap> |
|
1593 |
class FilterEdges : |
|
1594 |
public SubGraph<_Graph, ConstMap<typename _Graph::Node,bool>, |
|
1595 |
_EdgeFilterMap, false> { |
|
1596 |
public: |
|
1597 |
typedef _Graph Graph; |
|
1598 |
typedef _EdgeFilterMap EdgeFilterMap; |
|
1599 |
typedef SubGraph<Graph, ConstMap<typename Graph::Node,bool>, |
|
1600 |
EdgeFilterMap, false> Parent; |
|
1601 |
typedef typename Parent::Edge Edge; |
|
1602 |
protected: |
|
1603 |
ConstMap<typename Graph::Node, bool> const_true_map; |
|
1604 |
|
|
1605 |
FilterEdges() : const_true_map(true) { |
|
1606 |
Parent::setNodeFilterMap(const_true_map); |
|
1607 |
} |
|
1608 |
|
|
1609 |
public: |
|
1610 |
|
|
1611 |
/// \brief Constructor |
|
1612 |
/// |
|
1613 |
/// Creates a FilterEdges adaptor for the given graph with |
|
1614 |
/// given edge map filters. |
|
1615 |
FilterEdges(Graph& _graph, EdgeFilterMap& edge_filter_map) : |
|
1616 |
Parent(), const_true_map(true) { |
|
1617 |
Parent::setGraph(_graph); |
|
1618 |
Parent::setNodeFilterMap(const_true_map); |
|
1619 |
Parent::setEdgeFilterMap(edge_filter_map); |
|
1620 |
} |
|
1621 |
|
|
1622 |
/// \brief Hides the edge of the graph |
|
1623 |
/// |
|
1624 |
/// This function hides \c e in the graph, i.e. the iteration |
|
1625 |
/// jumps over it. This is done by simply setting the value of \c e |
|
1626 |
/// to be false in the corresponding edge-map. |
|
1627 |
void hide(const Edge& e) const { Parent::hide(e); } |
|
1628 |
|
|
1629 |
/// \brief Unhides the edge of the graph |
|
1630 |
/// |
|
1631 |
/// The value of \c e is set to be true in the edge-map which stores |
|
1632 |
/// hide information. If \c e was hidden previuosly, then it is shown |
|
1633 |
/// again |
|
1634 |
void unHide(const Edge& e) const { Parent::unHide(e); } |
|
1635 |
|
|
1636 |
/// \brief Returns true if \c e is hidden. |
|
1637 |
/// |
|
1638 |
/// Returns true if \c e is hidden. |
|
1639 |
/// |
|
1640 |
bool hidden(const Edge& e) const { return Parent::hidden(e); } |
|
1641 |
|
|
1642 |
}; |
|
1643 |
|
|
1644 |
/// \brief Just gives back a FilterEdges adaptor |
|
1645 |
/// |
|
1646 |
/// Just gives back a FilterEdges adaptor |
|
1647 |
template<typename Graph, typename EdgeFilterMap> |
|
1648 |
FilterEdges<const Graph, EdgeFilterMap> |
|
1649 |
filterEdges(const Graph& graph, EdgeFilterMap& efm) { |
|
1650 |
return FilterEdges<const Graph, EdgeFilterMap>(graph, efm); |
|
1651 |
} |
|
1652 |
|
|
1653 |
template<typename Graph, typename EdgeFilterMap> |
|
1654 |
FilterEdges<const Graph, const EdgeFilterMap> |
|
1655 |
filterEdges(const Graph& graph, const EdgeFilterMap& efm) { |
|
1656 |
return FilterEdges<const Graph, const EdgeFilterMap>(graph, efm); |
|
1657 |
} |
|
1658 |
|
|
1659 |
template <typename _Digraph> |
|
1660 |
class UndirectorBase { |
|
1661 |
public: |
|
1662 |
typedef _Digraph Digraph; |
|
1663 |
typedef UndirectorBase Adaptor; |
|
1664 |
|
|
1665 |
typedef True UndirectedTag; |
|
1666 |
|
|
1667 |
typedef typename Digraph::Arc Edge; |
|
1668 |
typedef typename Digraph::Node Node; |
|
1669 |
|
|
1670 |
class Arc : public Edge { |
|
1671 |
friend class UndirectorBase; |
|
1672 |
protected: |
|
1673 |
bool _forward; |
|
1674 |
|
|
1675 |
Arc(const Edge& edge, bool forward) : |
|
1676 |
Edge(edge), _forward(forward) {} |
|
1677 |
|
|
1678 |
public: |
|
1679 |
Arc() {} |
|
1680 |
|
|
1681 |
Arc(Invalid) : Edge(INVALID), _forward(true) {} |
|
1682 |
|
|
1683 |
bool operator==(const Arc &other) const { |
|
1684 |
return _forward == other._forward && |
|
1685 |
static_cast<const Edge&>(*this) == static_cast<const Edge&>(other); |
|
1686 |
} |
|
1687 |
bool operator!=(const Arc &other) const { |
|
1688 |
return _forward != other._forward || |
|
1689 |
static_cast<const Edge&>(*this) != static_cast<const Edge&>(other); |
|
1690 |
} |
|
1691 |
bool operator<(const Arc &other) const { |
|
1692 |
return _forward < other._forward || |
|
1693 |
(_forward == other._forward && |
|
1694 |
static_cast<const Edge&>(*this) < static_cast<const Edge&>(other)); |
|
1695 |
} |
|
1696 |
}; |
|
1697 |
|
|
1698 |
|
|
1699 |
|
|
1700 |
void first(Node& n) const { |
|
1701 |
_digraph->first(n); |
|
1702 |
} |
|
1703 |
|
|
1704 |
void next(Node& n) const { |
|
1705 |
_digraph->next(n); |
|
1706 |
} |
|
1707 |
|
|
1708 |
void first(Arc& a) const { |
|
1709 |
_digraph->first(a); |
|
1710 |
a._forward = true; |
|
1711 |
} |
|
1712 |
|
|
1713 |
void next(Arc& a) const { |
|
1714 |
if (a._forward) { |
|
1715 |
a._forward = false; |
|
1716 |
} else { |
|
1717 |
_digraph->next(a); |
|
1718 |
a._forward = true; |
|
1719 |
} |
|
1720 |
} |
|
1721 |
|
|
1722 |
void first(Edge& e) const { |
|
1723 |
_digraph->first(e); |
|
1724 |
} |
|
1725 |
|
|
1726 |
void next(Edge& e) const { |
|
1727 |
_digraph->next(e); |
|
1728 |
} |
|
1729 |
|
|
1730 |
void firstOut(Arc& a, const Node& n) const { |
|
1731 |
_digraph->firstIn(a, n); |
|
1732 |
if( static_cast<const Edge&>(a) != INVALID ) { |
|
1733 |
a._forward = false; |
|
1734 |
} else { |
|
1735 |
_digraph->firstOut(a, n); |
|
1736 |
a._forward = true; |
|
1737 |
} |
|
1738 |
} |
|
1739 |
void nextOut(Arc &a) const { |
|
1740 |
if (!a._forward) { |
|
1741 |
Node n = _digraph->target(a); |
|
1742 |
_digraph->nextIn(a); |
|
1743 |
if (static_cast<const Edge&>(a) == INVALID ) { |
|
1744 |
_digraph->firstOut(a, n); |
|
1745 |
a._forward = true; |
|
1746 |
} |
|
1747 |
} |
|
1748 |
else { |
|
1749 |
_digraph->nextOut(a); |
|
1750 |
} |
|
1751 |
} |
|
1752 |
|
|
1753 |
void firstIn(Arc &a, const Node &n) const { |
|
1754 |
_digraph->firstOut(a, n); |
|
1755 |
if (static_cast<const Edge&>(a) != INVALID ) { |
|
1756 |
a._forward = false; |
|
1757 |
} else { |
|
1758 |
_digraph->firstIn(a, n); |
|
1759 |
a._forward = true; |
|
1760 |
} |
|
1761 |
} |
|
1762 |
void nextIn(Arc &a) const { |
|
1763 |
if (!a._forward) { |
|
1764 |
Node n = _digraph->source(a); |
|
1765 |
_digraph->nextOut(a); |
|
1766 |
if( static_cast<const Edge&>(a) == INVALID ) { |
|
1767 |
_digraph->firstIn(a, n); |
|
1768 |
a._forward = true; |
|
1769 |
} |
|
1770 |
} |
|
1771 |
else { |
|
1772 |
_digraph->nextIn(a); |
|
1773 |
} |
|
1774 |
} |
|
1775 |
|
|
1776 |
void firstInc(Edge &e, bool &d, const Node &n) const { |
|
1777 |
d = true; |
|
1778 |
_digraph->firstOut(e, n); |
|
1779 |
if (e != INVALID) return; |
|
1780 |
d = false; |
|
1781 |
_digraph->firstIn(e, n); |
|
1782 |
} |
|
1783 |
|
|
1784 |
void nextInc(Edge &e, bool &d) const { |
|
1785 |
if (d) { |
|
1786 |
Node s = _digraph->source(e); |
|
1787 |
_digraph->nextOut(e); |
|
1788 |
if (e != INVALID) return; |
|
1789 |
d = false; |
|
1790 |
_digraph->firstIn(e, s); |
|
1791 |
} else { |
|
1792 |
_digraph->nextIn(e); |
|
1793 |
} |
|
1794 |
} |
|
1795 |
|
|
1796 |
Node u(const Edge& e) const { |
|
1797 |
return _digraph->source(e); |
|
1798 |
} |
|
1799 |
|
|
1800 |
Node v(const Edge& e) const { |
|
1801 |
return _digraph->target(e); |
|
1802 |
} |
|
1803 |
|
|
1804 |
Node source(const Arc &a) const { |
|
1805 |
return a._forward ? _digraph->source(a) : _digraph->target(a); |
|
1806 |
} |
|
1807 |
|
|
1808 |
Node target(const Arc &a) const { |
|
1809 |
return a._forward ? _digraph->target(a) : _digraph->source(a); |
|
1810 |
} |
|
1811 |
|
|
1812 |
static Arc direct(const Edge &e, bool d) { |
|
1813 |
return Arc(e, d); |
|
1814 |
} |
|
1815 |
Arc direct(const Edge &e, const Node& n) const { |
|
1816 |
return Arc(e, _digraph->source(e) == n); |
|
1817 |
} |
|
1818 |
|
|
1819 |
static bool direction(const Arc &a) { return a._forward; } |
|
1820 |
|
|
1821 |
Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); } |
|
1822 |
Arc arcFromId(int ix) const { |
|
1823 |
return direct(_digraph->arcFromId(ix >> 1), bool(ix & 1)); |
|
1824 |
} |
|
1825 |
Edge edgeFromId(int ix) const { return _digraph->arcFromId(ix); } |
|
1826 |
|
|
1827 |
int id(const Node &n) const { return _digraph->id(n); } |
|
1828 |
int id(const Arc &a) const { |
|
1829 |
return (_digraph->id(a) << 1) | (a._forward ? 1 : 0); |
|
1830 |
} |
|
1831 |
int id(const Edge &e) const { return _digraph->id(e); } |
|
1832 |
|
|
1833 |
int maxNodeId() const { return _digraph->maxNodeId(); } |
|
1834 |
int maxArcId() const { return (_digraph->maxArcId() << 1) | 1; } |
|
1835 |
int maxEdgeId() const { return _digraph->maxArcId(); } |
|
1836 |
|
|
1837 |
Node addNode() { return _digraph->addNode(); } |
|
1838 |
Edge addEdge(const Node& u, const Node& v) { |
|
1839 |
return _digraph->addArc(u, v); |
|
1840 |
} |
|
1841 |
|
|
1842 |
void erase(const Node& i) { _digraph->erase(i); } |
|
1843 |
void erase(const Edge& i) { _digraph->erase(i); } |
|
1844 |
|
|
1845 |
void clear() { _digraph->clear(); } |
|
1846 |
|
|
1847 |
typedef NodeNumTagIndicator<Digraph> NodeNumTag; |
|
1848 |
int nodeNum() const { return 2 * _digraph->arcNum(); } |
|
1849 |
typedef EdgeNumTagIndicator<Digraph> EdgeNumTag; |
|
1850 |
int arcNum() const { return 2 * _digraph->arcNum(); } |
|
1851 |
int edgeNum() const { return _digraph->arcNum(); } |
|
1852 |
|
|
1853 |
typedef FindEdgeTagIndicator<Digraph> FindEdgeTag; |
|
1854 |
Arc findArc(Node s, Node t, Arc p = INVALID) const { |
|
1855 |
if (p == INVALID) { |
|
1856 |
Edge arc = _digraph->findArc(s, t); |
|
1857 |
if (arc != INVALID) return direct(arc, true); |
|
1858 |
arc = _digraph->findArc(t, s); |
|
1859 |
if (arc != INVALID) return direct(arc, false); |
|
1860 |
} else if (direction(p)) { |
|
1861 |
Edge arc = _digraph->findArc(s, t, p); |
|
1862 |
if (arc != INVALID) return direct(arc, true); |
|
1863 |
arc = _digraph->findArc(t, s); |
|
1864 |
if (arc != INVALID) return direct(arc, false); |
|
1865 |
} else { |
|
1866 |
Edge arc = _digraph->findArc(t, s, p); |
|
1867 |
if (arc != INVALID) return direct(arc, false); |
|
1868 |
} |
|
1869 |
return INVALID; |
|
1870 |
} |
|
1871 |
|
|
1872 |
Edge findEdge(Node s, Node t, Edge p = INVALID) const { |
|
1873 |
if (s != t) { |
|
1874 |
if (p == INVALID) { |
|
1875 |
Edge arc = _digraph->findArc(s, t); |
|
1876 |
if (arc != INVALID) return arc; |
|
1877 |
arc = _digraph->findArc(t, s); |
|
1878 |
if (arc != INVALID) return arc; |
|
1879 |
} else if (_digraph->s(p) == s) { |
|
1880 |
Edge arc = _digraph->findArc(s, t, p); |
|
1881 |
if (arc != INVALID) return arc; |
|
1882 |
arc = _digraph->findArc(t, s); |
|
1883 |
if (arc != INVALID) return arc; |
|
1884 |
} else { |
|
1885 |
Edge arc = _digraph->findArc(t, s, p); |
|
1886 |
if (arc != INVALID) return arc; |
|
1887 |
} |
|
1888 |
} else { |
|
1889 |
return _digraph->findArc(s, t, p); |
|
1890 |
} |
|
1891 |
return INVALID; |
|
1892 |
} |
|
1893 |
|
|
1894 |
private: |
|
1895 |
|
|
1896 |
template <typename _Value> |
|
1897 |
class ArcMapBase { |
|
1898 |
private: |
|
1899 |
|
|
1900 |
typedef typename Digraph::template ArcMap<_Value> MapImpl; |
|
1901 |
|
|
1902 |
public: |
|
1903 |
|
|
1904 |
typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag; |
|
1905 |
|
|
1906 |
typedef _Value Value; |
|
1907 |
typedef Arc Key; |
|
1908 |
|
|
1909 |
ArcMapBase(const Adaptor& adaptor) : |
|
1910 |
_forward(*adaptor._digraph), _backward(*adaptor._digraph) {} |
|
1911 |
|
|
1912 |
ArcMapBase(const Adaptor& adaptor, const Value& v) |
|
1913 |
: _forward(*adaptor._digraph, v), _backward(*adaptor._digraph, v) {} |
|
1914 |
|
|
1915 |
void set(const Arc& a, const Value& v) { |
|
1916 |
if (direction(a)) { |
|
1917 |
_forward.set(a, v); |
|
1918 |
} else { |
|
1919 |
_backward.set(a, v); |
|
1920 |
} |
|
1921 |
} |
|
1922 |
|
|
1923 |
typename MapTraits<MapImpl>::ConstReturnValue |
|
1924 |
operator[](const Arc& a) const { |
|
1925 |
if (direction(a)) { |
|
1926 |
return _forward[a]; |
|
1927 |
} else { |
|
1928 |
return _backward[a]; |
|
1929 |
} |
|
1930 |
} |
|
1931 |
|
|
1932 |
typename MapTraits<MapImpl>::ReturnValue |
|
1933 |
operator[](const Arc& a) { |
|
1934 |
if (direction(a)) { |
|
1935 |
return _forward[a]; |
|
1936 |
} else { |
|
1937 |
return _backward[a]; |
|
1938 |
} |
|
1939 |
} |
|
1940 |
|
|
1941 |
protected: |
|
1942 |
|
|
1943 |
MapImpl _forward, _backward; |
|
1944 |
|
|
1945 |
}; |
|
1946 |
|
|
1947 |
public: |
|
1948 |
|
|
1949 |
template <typename _Value> |
|
1950 |
class NodeMap : public Digraph::template NodeMap<_Value> { |
|
1951 |
public: |
|
1952 |
|
|
1953 |
typedef _Value Value; |
|
1954 |
typedef typename Digraph::template NodeMap<Value> Parent; |
|
1955 |
|
|
1956 |
explicit NodeMap(const Adaptor& adaptor) |
|
1957 |
: Parent(*adaptor._digraph) {} |
|
1958 |
|
|
1959 |
NodeMap(const Adaptor& adaptor, const _Value& value) |
|
1960 |
: Parent(*adaptor._digraph, value) { } |
|
1961 |
|
|
1962 |
private: |
|
1963 |
NodeMap& operator=(const NodeMap& cmap) { |
|
1964 |
return operator=<NodeMap>(cmap); |
|
1965 |
} |
|
1966 |
|
|
1967 |
template <typename CMap> |
|
1968 |
NodeMap& operator=(const CMap& cmap) { |
|
1969 |
Parent::operator=(cmap); |
|
1970 |
return *this; |
|
1971 |
} |
|
1972 |
|
|
1973 |
}; |
|
1974 |
|
|
1975 |
template <typename _Value> |
|
1976 |
class ArcMap |
|
1977 |
: public SubMapExtender<Adaptor, ArcMapBase<_Value> > |
|
1978 |
{ |
|
1979 |
public: |
|
1980 |
typedef _Value Value; |
|
1981 |
typedef SubMapExtender<Adaptor, ArcMapBase<Value> > Parent; |
|
1982 |
|
|
1983 |
ArcMap(const Adaptor& adaptor) |
|
1984 |
: Parent(adaptor) {} |
|
1985 |
|
|
1986 |
ArcMap(const Adaptor& adaptor, const Value& value) |
|
1987 |
: Parent(adaptor, value) {} |
|
1988 |
|
|
1989 |
private: |
|
1990 |
ArcMap& operator=(const ArcMap& cmap) { |
|
1991 |
return operator=<ArcMap>(cmap); |
|
1992 |
} |
|
1993 |
|
|
1994 |
template <typename CMap> |
|
1995 |
ArcMap& operator=(const CMap& cmap) { |
|
1996 |
Parent::operator=(cmap); |
|
1997 |
return *this; |
|
1998 |
} |
|
1999 |
}; |
|
2000 |
|
|
2001 |
template <typename _Value> |
|
2002 |
class EdgeMap : public Digraph::template ArcMap<_Value> { |
|
2003 |
public: |
|
2004 |
|
|
2005 |
typedef _Value Value; |
|
2006 |
typedef typename Digraph::template ArcMap<Value> Parent; |
|
2007 |
|
|
2008 |
explicit EdgeMap(const Adaptor& adaptor) |
|
2009 |
: Parent(*adaptor._digraph) {} |
|
2010 |
|
|
2011 |
EdgeMap(const Adaptor& adaptor, const Value& value) |
|
2012 |
: Parent(*adaptor._digraph, value) {} |
|
2013 |
|
|
2014 |
private: |
|
2015 |
EdgeMap& operator=(const EdgeMap& cmap) { |
|
2016 |
return operator=<EdgeMap>(cmap); |
|
2017 |
} |
|
2018 |
|
|
2019 |
template <typename CMap> |
|
2020 |
EdgeMap& operator=(const CMap& cmap) { |
|
2021 |
Parent::operator=(cmap); |
|
2022 |
return *this; |
|
2023 |
} |
|
2024 |
|
|
2025 |
}; |
|
2026 |
|
|
2027 |
typedef typename ItemSetTraits<Digraph, Node>::ItemNotifier NodeNotifier; |
|
2028 |
NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } |
|
2029 |
|
|
2030 |
protected: |
|
2031 |
|
|
2032 |
UndirectorBase() : _digraph(0) {} |
|
2033 |
|
|
2034 |
Digraph* _digraph; |
|
2035 |
|
|
2036 |
void setDigraph(Digraph& digraph) { |
|
2037 |
_digraph = &digraph; |
|
2038 |
} |
|
2039 |
|
|
2040 |
}; |
|
2041 |
|
|
2042 |
/// \ingroup graph_adaptors |
|
2043 |
/// |
|
2044 |
/// \brief Undirect the graph |
|
2045 |
/// |
|
2046 |
/// This adaptor makes an undirected graph from a directed |
|
2047 |
/// graph. All arcs of the underlying digraph will be showed in the |
|
2048 |
/// adaptor as an edge. The Orienter adaptor is conform to the \ref |
|
2049 |
/// concepts::Graph "Graph concept". |
|
2050 |
/// |
|
2051 |
/// \tparam _Digraph It must be conform to the \ref |
|
2052 |
/// concepts::Digraph "Digraph concept". The type can be specified |
|
2053 |
/// to const. |
|
2054 |
template<typename _Digraph> |
|
2055 |
class Undirector |
|
2056 |
: public GraphAdaptorExtender<UndirectorBase<_Digraph> > { |
|
2057 |
public: |
|
2058 |
typedef _Digraph Digraph; |
|
2059 |
typedef GraphAdaptorExtender<UndirectorBase<Digraph> > Parent; |
|
2060 |
protected: |
|
2061 |
Undirector() { } |
|
2062 |
public: |
|
2063 |
|
|
2064 |
/// \brief Constructor |
|
2065 |
/// |
|
2066 |
/// Creates a undirected graph from the given digraph |
|
2067 |
Undirector(_Digraph& digraph) { |
|
2068 |
setDigraph(digraph); |
|
2069 |
} |
|
2070 |
|
|
2071 |
/// \brief ArcMap combined from two original ArcMap |
|
2072 |
/// |
|
2073 |
/// This class adapts two original digraph ArcMap to |
|
2074 |
/// get an arc map on the undirected graph. |
|
2075 |
template <typename _ForwardMap, typename _BackwardMap> |
|
2076 |
class CombinedArcMap { |
|
2077 |
public: |
|
2078 |
|
|
2079 |
typedef _ForwardMap ForwardMap; |
|
2080 |
typedef _BackwardMap BackwardMap; |
|
2081 |
|
|
2082 |
typedef typename MapTraits<ForwardMap>::ReferenceMapTag ReferenceMapTag; |
|
2083 |
|
|
2084 |
typedef typename ForwardMap::Value Value; |
|
2085 |
typedef typename Parent::Arc Key; |
|
2086 |
|
|
2087 |
/// \brief Constructor |
|
2088 |
/// |
|
2089 |
/// Constructor |
|
2090 |
CombinedArcMap(ForwardMap& forward, BackwardMap& backward) |
|
2091 |
: _forward(&forward), _backward(&backward) {} |
|
2092 |
|
|
2093 |
|
|
2094 |
/// \brief Sets the value associated with a key. |
|
2095 |
/// |
|
2096 |
/// Sets the value associated with a key. |
|
2097 |
void set(const Key& e, const Value& a) { |
|
2098 |
if (Parent::direction(e)) { |
|
2099 |
_forward->set(e, a); |
|
2100 |
} else { |
|
2101 |
_backward->set(e, a); |
|
2102 |
} |
|
2103 |
} |
|
2104 |
|
|
2105 |
/// \brief Returns the value associated with a key. |
|
2106 |
/// |
|
2107 |
/// Returns the value associated with a key. |
|
2108 |
typename MapTraits<ForwardMap>::ConstReturnValue |
|
2109 |
operator[](const Key& e) const { |
|
2110 |
if (Parent::direction(e)) { |
|
2111 |
return (*_forward)[e]; |
|
2112 |
} else { |
|
2113 |
return (*_backward)[e]; |
|
2114 |
} |
|
2115 |
} |
|
2116 |
|
|
2117 |
/// \brief Returns the value associated with a key. |
|
2118 |
/// |
|
2119 |
/// Returns the value associated with a key. |
|
2120 |
typename MapTraits<ForwardMap>::ReturnValue |
|
2121 |
operator[](const Key& e) { |
|
2122 |
if (Parent::direction(e)) { |
|
2123 |
return (*_forward)[e]; |
|
2124 |
} else { |
|
2125 |
return (*_backward)[e]; |
|
2126 |
} |
|
2127 |
} |
|
2128 |
|
|
2129 |
protected: |
|
2130 |
|
|
2131 |
ForwardMap* _forward; |
|
2132 |
BackwardMap* _backward; |
|
2133 |
|
|
2134 |
}; |
|
2135 |
|
|
2136 |
/// \brief Just gives back a combined arc map |
|
2137 |
/// |
|
2138 |
/// Just gives back a combined arc map |
|
2139 |
template <typename ForwardMap, typename BackwardMap> |
|
2140 |
static CombinedArcMap<ForwardMap, BackwardMap> |
|
2141 |
combinedArcMap(ForwardMap& forward, BackwardMap& backward) { |
|
2142 |
return CombinedArcMap<ForwardMap, BackwardMap>(forward, backward); |
|
2143 |
} |
|
2144 |
|
|
2145 |
template <typename ForwardMap, typename BackwardMap> |
|
2146 |
static CombinedArcMap<const ForwardMap, BackwardMap> |
|
2147 |
combinedArcMap(const ForwardMap& forward, BackwardMap& backward) { |
|
2148 |
return CombinedArcMap<const ForwardMap, |
|
2149 |
BackwardMap>(forward, backward); |
|
2150 |
} |
|
2151 |
|
|
2152 |
template <typename ForwardMap, typename BackwardMap> |
|
2153 |
static CombinedArcMap<ForwardMap, const BackwardMap> |
|
2154 |
combinedArcMap(ForwardMap& forward, const BackwardMap& backward) { |
|
2155 |
return CombinedArcMap<ForwardMap, |
|
2156 |
const BackwardMap>(forward, backward); |
|
2157 |
} |
|
2158 |
|
|
2159 |
template <typename ForwardMap, typename BackwardMap> |
|
2160 |
static CombinedArcMap<const ForwardMap, const BackwardMap> |
|
2161 |
combinedArcMap(const ForwardMap& forward, const BackwardMap& backward) { |
|
2162 |
return CombinedArcMap<const ForwardMap, |
|
2163 |
const BackwardMap>(forward, backward); |
|
2164 |
} |
|
2165 |
|
|
2166 |
}; |
|
2167 |
|
|
2168 |
/// \brief Just gives back an undirected view of the given digraph |
|
2169 |
/// |
|
2170 |
/// Just gives back an undirected view of the given digraph |
|
2171 |
template<typename Digraph> |
|
2172 |
Undirector<const Digraph> |
|
2173 |
undirector(const Digraph& digraph) { |
|
2174 |
return Undirector<const Digraph>(digraph); |
|
2175 |
} |
|
2176 |
|
|
2177 |
template <typename _Graph, typename _DirectionMap> |
|
2178 |
class OrienterBase { |
|
2179 |
public: |
|
2180 |
|
|
2181 |
typedef _Graph Graph; |
|
2182 |
typedef _DirectionMap DirectionMap; |
|
2183 |
|
|
2184 |
typedef typename Graph::Node Node; |
|
2185 |
typedef typename Graph::Edge Arc; |
|
2186 |
|
|
2187 |
void reverseArc(const Arc& arc) { |
|
2188 |
_direction->set(arc, !(*_direction)[arc]); |
|
2189 |
} |
|
2190 |
|
|
2191 |
void first(Node& i) const { _graph->first(i); } |
|
2192 |
void first(Arc& i) const { _graph->first(i); } |
|
2193 |
void firstIn(Arc& i, const Node& n) const { |
|
2194 |
bool d; |
|
2195 |
_graph->firstInc(i, d, n); |
|
2196 |
while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d); |
|
2197 |
} |
|
2198 |
void firstOut(Arc& i, const Node& n ) const { |
|
2199 |
bool d; |
|
2200 |
_graph->firstInc(i, d, n); |
|
2201 |
while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d); |
|
2202 |
} |
|
2203 |
|
|
2204 |
void next(Node& i) const { _graph->next(i); } |
|
2205 |
void next(Arc& i) const { _graph->next(i); } |
|
2206 |
void nextIn(Arc& i) const { |
|
2207 |
bool d = !(*_direction)[i]; |
|
2208 |
_graph->nextInc(i, d); |
|
2209 |
while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d); |
|
2210 |
} |
|
2211 |
void nextOut(Arc& i) const { |
|
2212 |
bool d = (*_direction)[i]; |
|
2213 |
_graph->nextInc(i, d); |
|
2214 |
while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d); |
|
2215 |
} |
|
2216 |
|
|
2217 |
Node source(const Arc& e) const { |
|
2218 |
return (*_direction)[e] ? _graph->u(e) : _graph->v(e); |
|
2219 |
} |
|
2220 |
Node target(const Arc& e) const { |
|
2221 |
return (*_direction)[e] ? _graph->v(e) : _graph->u(e); |
|
2222 |
} |
|
2223 |
|
|
2224 |
typedef NodeNumTagIndicator<Graph> NodeNumTag; |
|
2225 |
int nodeNum() const { return _graph->nodeNum(); } |
|
2226 |
|
|
2227 |
typedef EdgeNumTagIndicator<Graph> EdgeNumTag; |
|
2228 |
int arcNum() const { return _graph->edgeNum(); } |
|
2229 |
|
|
2230 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
|
2231 |
Arc findArc(const Node& u, const Node& v, |
|
2232 |
const Arc& prev = INVALID) { |
|
2233 |
Arc arc = prev; |
|
2234 |
bool d = arc == INVALID ? true : (*_direction)[arc]; |
|
2235 |
if (d) { |
|
2236 |
arc = _graph->findEdge(u, v, arc); |
|
2237 |
while (arc != INVALID && !(*_direction)[arc]) { |
|
2238 |
_graph->findEdge(u, v, arc); |
|
2239 |
} |
|
2240 |
if (arc != INVALID) return arc; |
|
2241 |
} |
|
2242 |
_graph->findEdge(v, u, arc); |
|
2243 |
while (arc != INVALID && (*_direction)[arc]) { |
|
2244 |
_graph->findEdge(u, v, arc); |
|
2245 |
} |
|
2246 |
return arc; |
|
2247 |
} |
|
2248 |
|
|
2249 |
Node addNode() { |
|
2250 |
return Node(_graph->addNode()); |
|
2251 |
} |
|
2252 |
|
|
2253 |
Arc addArc(const Node& u, const Node& v) { |
|
2254 |
Arc arc = _graph->addArc(u, v); |
|
2255 |
_direction->set(arc, _graph->source(arc) == u); |
|
2256 |
return arc; |
|
2257 |
} |
|
2258 |
|
|
2259 |
void erase(const Node& i) { _graph->erase(i); } |
|
2260 |
void erase(const Arc& i) { _graph->erase(i); } |
|
2261 |
|
|
2262 |
void clear() { _graph->clear(); } |
|
2263 |
|
|
2264 |
int id(const Node& v) const { return _graph->id(v); } |
|
2265 |
int id(const Arc& e) const { return _graph->id(e); } |
|
2266 |
|
|
2267 |
Node nodeFromId(int idx) const { return _graph->nodeFromId(idx); } |
|
2268 |
Arc arcFromId(int idx) const { return _graph->edgeFromId(idx); } |
|
2269 |
|
|
2270 |
int maxNodeId() const { return _graph->maxNodeId(); } |
|
2271 |
int maxArcId() const { return _graph->maxEdgeId(); } |
|
2272 |
|
|
2273 |
typedef typename ItemSetTraits<Graph, Node>::ItemNotifier NodeNotifier; |
|
2274 |
NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); } |
|
2275 |
|
|
2276 |
typedef typename ItemSetTraits<Graph, Arc>::ItemNotifier ArcNotifier; |
|
2277 |
ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); } |
|
2278 |
|
|
2279 |
template <typename _Value> |
|
2280 |
class NodeMap : public _Graph::template NodeMap<_Value> { |
|
2281 |
public: |
|
2282 |
|
|
2283 |
typedef typename _Graph::template NodeMap<_Value> Parent; |
|
2284 |
|
|
2285 |
explicit NodeMap(const OrienterBase& adapter) |
|
2286 |
: Parent(*adapter._graph) {} |
|
2287 |
|
|
2288 |
NodeMap(const OrienterBase& adapter, const _Value& value) |
|
2289 |
: Parent(*adapter._graph, value) {} |
|
2290 |
|
|
2291 |
private: |
|
2292 |
NodeMap& operator=(const NodeMap& cmap) { |
|
2293 |
return operator=<NodeMap>(cmap); |
|
2294 |
} |
|
2295 |
|
|
2296 |
template <typename CMap> |
|
2297 |
NodeMap& operator=(const CMap& cmap) { |
|
2298 |
Parent::operator=(cmap); |
|
2299 |
return *this; |
|
2300 |
} |
|
2301 |
|
|
2302 |
}; |
|
2303 |
|
|
2304 |
template <typename _Value> |
|
2305 |
class ArcMap : public _Graph::template EdgeMap<_Value> { |
|
2306 |
public: |
|
2307 |
|
|
2308 |
typedef typename Graph::template EdgeMap<_Value> Parent; |
|
2309 |
|
|
2310 |
explicit ArcMap(const OrienterBase& adapter) |
|
2311 |
: Parent(*adapter._graph) { } |
|
2312 |
|
|
2313 |
ArcMap(const OrienterBase& adapter, const _Value& value) |
|
2314 |
: Parent(*adapter._graph, value) { } |
|
2315 |
|
|
2316 |
private: |
|
2317 |
ArcMap& operator=(const ArcMap& cmap) { |
|
2318 |
return operator=<ArcMap>(cmap); |
|
2319 |
} |
|
2320 |
|
|
2321 |
template <typename CMap> |
|
2322 |
ArcMap& operator=(const CMap& cmap) { |
|
2323 |
Parent::operator=(cmap); |
|
2324 |
return *this; |
|
2325 |
} |
|
2326 |
}; |
|
2327 |
|
|
2328 |
|
|
2329 |
|
|
2330 |
protected: |
|
2331 |
Graph* _graph; |
|
2332 |
DirectionMap* _direction; |
|
2333 |
|
|
2334 |
void setDirectionMap(DirectionMap& direction) { |
|
2335 |
_direction = &direction; |
|
2336 |
} |
|
2337 |
|
|
2338 |
void setGraph(Graph& graph) { |
|
2339 |
_graph = &graph; |
|
2340 |
} |
|
2341 |
|
|
2342 |
}; |
|
2343 |
|
|
2344 |
/// \ingroup graph_adaptors |
|
2345 |
/// |
|
2346 |
/// \brief Orients the edges of the graph to get a digraph |
|
2347 |
/// |
|
2348 |
/// This adaptor orients each edge in the undirected graph. The |
|
2349 |
/// direction of the arcs stored in an edge node map. The arcs can |
|
2350 |
/// be easily reverted by the \c reverseArc() member function in the |
|
2351 |
/// adaptor. The Orienter adaptor is conform to the \ref |
|
2352 |
/// concepts::Digraph "Digraph concept". |
|
2353 |
/// |
|
2354 |
/// \tparam _Graph It must be conform to the \ref concepts::Graph |
|
2355 |
/// "Graph concept". The type can be specified to be const. |
|
2356 |
/// \tparam _DirectionMap A bool valued edge map of the the adapted |
|
2357 |
/// graph. |
|
2358 |
/// |
|
2359 |
/// \sa orienter |
|
2360 |
template<typename _Graph, |
|
2361 |
typename DirectionMap = typename _Graph::template EdgeMap<bool> > |
|
2362 |
class Orienter : |
|
2363 |
public DigraphAdaptorExtender<OrienterBase<_Graph, DirectionMap> > { |
|
2364 |
public: |
|
2365 |
typedef _Graph Graph; |
|
2366 |
typedef DigraphAdaptorExtender< |
|
2367 |
OrienterBase<_Graph, DirectionMap> > Parent; |
|
2368 |
typedef typename Parent::Arc Arc; |
|
2369 |
protected: |
|
2370 |
Orienter() { } |
|
2371 |
public: |
|
2372 |
|
|
2373 |
/// \brief Constructor of the adaptor |
|
2374 |
/// |
|
2375 |
/// Constructor of the adaptor |
|
2376 |
Orienter(Graph& graph, DirectionMap& direction) { |
|
2377 |
setGraph(graph); |
|
2378 |
setDirectionMap(direction); |
|
2379 |
} |
|
2380 |
|
|
2381 |
/// \brief Reverse arc |
|
2382 |
/// |
|
2383 |
/// It reverse the given arc. It simply negate the direction in the map. |
|
2384 |
void reverseArc(const Arc& a) { |
|
2385 |
Parent::reverseArc(a); |
|
2386 |
} |
|
2387 |
}; |
|
2388 |
|
|
2389 |
/// \brief Just gives back a Orienter |
|
2390 |
/// |
|
2391 |
/// Just gives back a Orienter |
|
2392 |
template<typename Graph, typename DirectionMap> |
|
2393 |
Orienter<const Graph, DirectionMap> |
|
2394 |
orienter(const Graph& graph, DirectionMap& dm) { |
|
2395 |
return Orienter<const Graph, DirectionMap>(graph, dm); |
|
2396 |
} |
|
2397 |
|
|
2398 |
template<typename Graph, typename DirectionMap> |
|
2399 |
Orienter<const Graph, const DirectionMap> |
|
2400 |
orienter(const Graph& graph, const DirectionMap& dm) { |
|
2401 |
return Orienter<const Graph, const DirectionMap>(graph, dm); |
|
2402 |
} |
|
2403 |
|
|
2404 |
namespace _adaptor_bits { |
|
2405 |
|
|
2406 |
template<typename _Digraph, |
|
2407 |
typename _CapacityMap = typename _Digraph::template ArcMap<int>, |
|
2408 |
typename _FlowMap = _CapacityMap, |
|
2409 |
typename _Tolerance = Tolerance<typename _CapacityMap::Value> > |
|
2410 |
class ResForwardFilter { |
|
2411 |
public: |
|
2412 |
|
|
2413 |
typedef _Digraph Digraph; |
|
2414 |
typedef _CapacityMap CapacityMap; |
|
2415 |
typedef _FlowMap FlowMap; |
|
2416 |
typedef _Tolerance Tolerance; |
|
2417 |
|
|
2418 |
typedef typename Digraph::Arc Key; |
|
2419 |
typedef bool Value; |
|
2420 |
|
|
2421 |
private: |
|
2422 |
|
|
2423 |
const CapacityMap* _capacity; |
|
2424 |
const FlowMap* _flow; |
|
2425 |
Tolerance _tolerance; |
|
2426 |
public: |
|
2427 |
|
|
2428 |
ResForwardFilter(const CapacityMap& capacity, const FlowMap& flow, |
|
2429 |
const Tolerance& tolerance = Tolerance()) |
|
2430 |
: _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } |
|
2431 |
|
|
2432 |
bool operator[](const typename Digraph::Arc& a) const { |
|
2433 |
return _tolerance.positive((*_capacity)[a] - (*_flow)[a]); |
|
2434 |
} |
|
2435 |
}; |
|
2436 |
|
|
2437 |
template<typename _Digraph, |
|
2438 |
typename _CapacityMap = typename _Digraph::template ArcMap<int>, |
|
2439 |
typename _FlowMap = _CapacityMap, |
|
2440 |
typename _Tolerance = Tolerance<typename _CapacityMap::Value> > |
|
2441 |
class ResBackwardFilter { |
|
2442 |
public: |
|
2443 |
|
|
2444 |
typedef _Digraph Digraph; |
|
2445 |
typedef _CapacityMap CapacityMap; |
|
2446 |
typedef _FlowMap FlowMap; |
|
2447 |
typedef _Tolerance Tolerance; |
|
2448 |
|
|
2449 |
typedef typename Digraph::Arc Key; |
|
2450 |
typedef bool Value; |
|
2451 |
|
|
2452 |
private: |
|
2453 |
|
|
2454 |
const CapacityMap* _capacity; |
|
2455 |
const FlowMap* _flow; |
|
2456 |
Tolerance _tolerance; |
|
2457 |
|
|
2458 |
public: |
|
2459 |
|
|
2460 |
ResBackwardFilter(const CapacityMap& capacity, const FlowMap& flow, |
|
2461 |
const Tolerance& tolerance = Tolerance()) |
|
2462 |
: _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } |
|
2463 |
|
|
2464 |
bool operator[](const typename Digraph::Arc& a) const { |
|
2465 |
return _tolerance.positive((*_flow)[a]); |
|
2466 |
} |
|
2467 |
}; |
|
2468 |
|
|
2469 |
} |
|
2470 |
|
|
2471 |
/// \ingroup graph_adaptors |
|
2472 |
/// |
|
2473 |
/// \brief An adaptor for composing the residual graph for directed |
|
2474 |
/// flow and circulation problems. |
|
2475 |
/// |
|
2476 |
/// An adaptor for composing the residual graph for directed flow and |
|
2477 |
/// circulation problems. Let \f$ G=(V, A) \f$ be a directed graph |
|
2478 |
/// and let \f$ F \f$ be a number type. Let moreover \f$ f,c:A\to F \f$, |
|
2479 |
/// be functions on the arc-set. |
|
2480 |
/// |
|
2481 |
/// Then Residual implements the digraph structure with |
|
2482 |
/// node-set \f$ V \f$ and arc-set \f$ A_{forward}\cup A_{backward} \f$, |
|
2483 |
/// where \f$ A_{forward}=\{uv : uv\in A, f(uv)<c(uv)\} \f$ and |
|
2484 |
/// \f$ A_{backward}=\{vu : uv\in A, f(uv)>0\} \f$, i.e. the so |
|
2485 |
/// called residual graph. When we take the union |
|
2486 |
/// \f$ A_{forward}\cup A_{backward} \f$, multiplicities are counted, |
|
2487 |
/// i.e. if an arc is in both \f$ A_{forward} \f$ and |
|
2488 |
/// \f$ A_{backward} \f$, then in the adaptor it appears in both |
|
2489 |
/// orientation. |
|
2490 |
/// |
|
2491 |
/// \tparam _Digraph It must be conform to the \ref concepts::Digraph |
|
2492 |
/// "Digraph concept". The type is implicitly const. |
|
2493 |
/// \tparam _CapacityMap An arc map of some numeric type, it defines |
|
2494 |
/// the capacities in the flow problem. The map is implicitly const. |
|
2495 |
/// \tparam _FlowMap An arc map of some numeric type, it defines |
|
2496 |
/// the capacities in the flow problem. |
|
2497 |
/// \tparam _Tolerance Handler for inexact computation. |
|
2498 |
template<typename _Digraph, |
|
2499 |
typename _CapacityMap = typename _Digraph::template ArcMap<int>, |
|
2500 |
typename _FlowMap = _CapacityMap, |
|
2501 |
typename _Tolerance = Tolerance<typename _CapacityMap::Value> > |
|
2502 |
class Residual : |
|
2503 |
public FilterArcs< |
|
2504 |
Undirector<const _Digraph>, |
|
2505 |
typename Undirector<const _Digraph>::template CombinedArcMap< |
|
2506 |
_adaptor_bits::ResForwardFilter<const _Digraph, _CapacityMap, |
|
2507 |
_FlowMap, _Tolerance>, |
|
2508 |
_adaptor_bits::ResBackwardFilter<const _Digraph, _CapacityMap, |
|
2509 |
_FlowMap, _Tolerance> > > |
|
2510 |
{ |
|
2511 |
public: |
|
2512 |
|
|
2513 |
typedef _Digraph Digraph; |
|
2514 |
typedef _CapacityMap CapacityMap; |
|
2515 |
typedef _FlowMap FlowMap; |
|
2516 |
typedef _Tolerance Tolerance; |
|
2517 |
|
|
2518 |
typedef typename CapacityMap::Value Value; |
|
2519 |
typedef Residual Adaptor; |
|
2520 |
|
|
2521 |
protected: |
|
2522 |
|
|
2523 |
typedef Undirector<const Digraph> Undirected; |
|
2524 |
|
|
2525 |
typedef _adaptor_bits::ResForwardFilter<const Digraph, CapacityMap, |
|
2526 |
FlowMap, Tolerance> ForwardFilter; |
|
2527 |
|
|
2528 |
typedef _adaptor_bits::ResBackwardFilter<const Digraph, CapacityMap, |
|
2529 |
FlowMap, Tolerance> BackwardFilter; |
|
2530 |
|
|
2531 |
typedef typename Undirected:: |
|
2532 |
template CombinedArcMap<ForwardFilter, BackwardFilter> ArcFilter; |
|
2533 |
|
|
2534 |
typedef FilterArcs<Undirected, ArcFilter> Parent; |
|
2535 |
|
|
2536 |
const CapacityMap* _capacity; |
|
2537 |
FlowMap* _flow; |
|
2538 |
|
|
2539 |
Undirected _graph; |
|
2540 |
ForwardFilter _forward_filter; |
|
2541 |
BackwardFilter _backward_filter; |
|
2542 |
ArcFilter _arc_filter; |
|
2543 |
|
|
2544 |
public: |
|
2545 |
|
|
2546 |
/// \brief Constructor of the residual digraph. |
|
2547 |
/// |
|
2548 |
/// Constructor of the residual graph. The parameters are the digraph, |
|
2549 |
/// the flow map, the capacity map and a tolerance object. |
|
2550 |
Residual(const Digraph& digraph, const CapacityMap& capacity, |
|
2551 |
FlowMap& flow, const Tolerance& tolerance = Tolerance()) |
|
2552 |
: Parent(), _capacity(&capacity), _flow(&flow), _graph(digraph), |
|
2553 |
_forward_filter(capacity, flow, tolerance), |
|
2554 |
_backward_filter(capacity, flow, tolerance), |
|
2555 |
_arc_filter(_forward_filter, _backward_filter) |
|
2556 |
{ |
|
2557 |
Parent::setDigraph(_graph); |
|
2558 |
Parent::setArcFilterMap(_arc_filter); |
|
2559 |
} |
|
2560 |
|
|
2561 |
typedef typename Parent::Arc Arc; |
|
2562 |
|
|
2563 |
/// \brief Gives back the residual capacity of the arc. |
|
2564 |
/// |
|
2565 |
/// Gives back the residual capacity of the arc. |
|
2566 |
Value residualCapacity(const Arc& a) const { |
|
2567 |
if (Undirected::direction(a)) { |
|
2568 |
return (*_capacity)[a] - (*_flow)[a]; |
|
2569 |
} else { |
|
2570 |
return (*_flow)[a]; |
|
2571 |
} |
|
2572 |
} |
|
2573 |
|
|
2574 |
/// \brief Augment on the given arc in the residual graph. |
|
2575 |
/// |
|
2576 |
/// Augment on the given arc in the residual graph. It increase |
|
2577 |
/// or decrease the flow on the original arc depend on the direction |
|
2578 |
/// of the residual arc. |
|
2579 |
void augment(const Arc& a, const Value& v) const { |
|
2580 |
if (Undirected::direction(a)) { |
|
2581 |
_flow->set(a, (*_flow)[a] + v); |
|
2582 |
} else { |
|
2583 |
_flow->set(a, (*_flow)[a] - v); |
|
2584 |
} |
|
2585 |
} |
|
2586 |
|
|
2587 |
/// \brief Returns the direction of the arc. |
|
2588 |
/// |
|
2589 |
/// Returns true when the arc is same oriented as the original arc. |
|
2590 |
static bool forward(const Arc& a) { |
|
2591 |
return Undirected::direction(a); |
|
2592 |
} |
|
2593 |
|
|
2594 |
/// \brief Returns the direction of the arc. |
|
2595 |
/// |
|
2596 |
/// Returns true when the arc is opposite oriented as the original arc. |
|
2597 |
static bool backward(const Arc& a) { |
|
2598 |
return !Undirected::direction(a); |
|
2599 |
} |
|
2600 |
|
|
2601 |
/// \brief Gives back the forward oriented residual arc. |
|
2602 |
/// |
|
2603 |
/// Gives back the forward oriented residual arc. |
|
2604 |
static Arc forward(const typename Digraph::Arc& a) { |
|
2605 |
return Undirected::direct(a, true); |
|
2606 |
} |
|
2607 |
|
|
2608 |
/// \brief Gives back the backward oriented residual arc. |
|
2609 |
/// |
|
2610 |
/// Gives back the backward oriented residual arc. |
|
2611 |
static Arc backward(const typename Digraph::Arc& a) { |
|
2612 |
return Undirected::direct(a, false); |
|
2613 |
} |
|
2614 |
|
|
2615 |
/// \brief Residual capacity map. |
|
2616 |
/// |
|
2617 |
/// In generic residual graph the residual capacity can be obtained |
|
2618 |
/// as a map. |
|
2619 |
class ResidualCapacity { |
|
2620 |
protected: |
|
2621 |
const Adaptor* _adaptor; |
|
2622 |
public: |
|
2623 |
/// The Key type |
|
2624 |
typedef Arc Key; |
|
2625 |
/// The Value type |
|
2626 |
typedef typename _CapacityMap::Value Value; |
|
2627 |
|
|
2628 |
/// Constructor |
|
2629 |
ResidualCapacity(const Adaptor& adaptor) : _adaptor(&adaptor) {} |
|
2630 |
|
|
2631 |
/// \e |
|
2632 |
Value operator[](const Arc& a) const { |
|
2633 |
return _adaptor->residualCapacity(a); |
|
2634 |
} |
|
2635 |
|
|
2636 |
}; |
|
2637 |
|
|
2638 |
}; |
|
2639 |
|
|
2640 |
template <typename _Digraph> |
|
2641 |
class SplitNodesBase { |
|
2642 |
public: |
|
2643 |
|
|
2644 |
typedef _Digraph Digraph; |
|
2645 |
typedef DigraphAdaptorBase<const _Digraph> Parent; |
|
2646 |
typedef SplitNodesBase Adaptor; |
|
2647 |
|
|
2648 |
typedef typename Digraph::Node DigraphNode; |
|
2649 |
typedef typename Digraph::Arc DigraphArc; |
|
2650 |
|
|
2651 |
class Node; |
|
2652 |
class Arc; |
|
2653 |
|
|
2654 |
private: |
|
2655 |
|
|
2656 |
template <typename T> class NodeMapBase; |
|
2657 |
template <typename T> class ArcMapBase; |
|
2658 |
|
|
2659 |
public: |
|
2660 |
|
|
2661 |
class Node : public DigraphNode { |
|
2662 |
friend class SplitNodesBase; |
|
2663 |
template <typename T> friend class NodeMapBase; |
|
2664 |
private: |
|
2665 |
|
|
2666 |
bool _in; |
|
2667 |
Node(DigraphNode node, bool in) |
|
2668 |
: DigraphNode(node), _in(in) {} |
|
2669 |
|
|
2670 |
public: |
|
2671 |
|
|
2672 |
Node() {} |
|
2673 |
Node(Invalid) : DigraphNode(INVALID), _in(true) {} |
|
2674 |
|
|
2675 |
bool operator==(const Node& node) const { |
|
2676 |
return DigraphNode::operator==(node) && _in == node._in; |
|
2677 |
} |
|
2678 |
|
|
2679 |
bool operator!=(const Node& node) const { |
|
2680 |
return !(*this == node); |
|
2681 |
} |
|
2682 |
|
|
2683 |
bool operator<(const Node& node) const { |
|
2684 |
return DigraphNode::operator<(node) || |
|
2685 |
(DigraphNode::operator==(node) && _in < node._in); |
|
2686 |
} |
|
2687 |
}; |
|
2688 |
|
|
2689 |
class Arc { |
|
2690 |
friend class SplitNodesBase; |
|
2691 |
template <typename T> friend class ArcMapBase; |
|
2692 |
private: |
|
2693 |
typedef BiVariant<DigraphArc, DigraphNode> ArcImpl; |
|
2694 |
|
|
2695 |
explicit Arc(const DigraphArc& arc) : _item(arc) {} |
|
2696 |
explicit Arc(const DigraphNode& node) : _item(node) {} |
|
2697 |
|
|
2698 |
ArcImpl _item; |
|
2699 |
|
|
2700 |
public: |
|
2701 |
Arc() {} |
|
2702 |
Arc(Invalid) : _item(DigraphArc(INVALID)) {} |
|
2703 |
|
|
2704 |
bool operator==(const Arc& arc) const { |
|
2705 |
if (_item.firstState()) { |
|
2706 |
if (arc._item.firstState()) { |
|
2707 |
return _item.first() == arc._item.first(); |
|
2708 |
} |
|
2709 |
} else { |
|
2710 |
if (arc._item.secondState()) { |
|
2711 |
return _item.second() == arc._item.second(); |
|
2712 |
} |
|
2713 |
} |
|
2714 |
return false; |
|
2715 |
} |
|
2716 |
|
|
2717 |
bool operator!=(const Arc& arc) const { |
|
2718 |
return !(*this == arc); |
|
2719 |
} |
|
2720 |
|
|
2721 |
bool operator<(const Arc& arc) const { |
|
2722 |
if (_item.firstState()) { |
|
2723 |
if (arc._item.firstState()) { |
|
2724 |
return _item.first() < arc._item.first(); |
|
2725 |
} |
|
2726 |
return false; |
|
2727 |
} else { |
|
2728 |
if (arc._item.secondState()) { |
|
2729 |
return _item.second() < arc._item.second(); |
|
2730 |
} |
|
2731 |
return true; |
|
2732 |
} |
|
2733 |
} |
|
2734 |
|
|
2735 |
operator DigraphArc() const { return _item.first(); } |
|
2736 |
operator DigraphNode() const { return _item.second(); } |
|
2737 |
|
|
2738 |
}; |
|
2739 |
|
|
2740 |
void first(Node& n) const { |
|
2741 |
_digraph->first(n); |
|
2742 |
n._in = true; |
|
2743 |
} |
|
2744 |
|
|
2745 |
void next(Node& n) const { |
|
2746 |
if (n._in) { |
|
2747 |
n._in = false; |
|
2748 |
} else { |
|
2749 |
n._in = true; |
|
2750 |
_digraph->next(n); |
|
2751 |
} |
|
2752 |
} |
|
2753 |
|
|
2754 |
void first(Arc& e) const { |
|
2755 |
e._item.setSecond(); |
|
2756 |
_digraph->first(e._item.second()); |
|
2757 |
if (e._item.second() == INVALID) { |
|
2758 |
e._item.setFirst(); |
|
2759 |
_digraph->first(e._item.first()); |
|
2760 |
} |
|
2761 |
} |
|
2762 |
|
|
2763 |
void next(Arc& e) const { |
|
2764 |
if (e._item.secondState()) { |
|
2765 |
_digraph->next(e._item.second()); |
|
2766 |
if (e._item.second() == INVALID) { |
|
2767 |
e._item.setFirst(); |
|
2768 |
_digraph->first(e._item.first()); |
|
2769 |
} |
|
2770 |
} else { |
|
2771 |
_digraph->next(e._item.first()); |
|
2772 |
} |
|
2773 |
} |
|
2774 |
|
|
2775 |
void firstOut(Arc& e, const Node& n) const { |
|
2776 |
if (n._in) { |
|
2777 |
e._item.setSecond(n); |
|
2778 |
} else { |
|
2779 |
e._item.setFirst(); |
|
2780 |
_digraph->firstOut(e._item.first(), n); |
|
2781 |
} |
|
2782 |
} |
|
2783 |
|
|
2784 |
void nextOut(Arc& e) const { |
|
2785 |
if (!e._item.firstState()) { |
|
2786 |
e._item.setFirst(INVALID); |
|
2787 |
} else { |
|
2788 |
_digraph->nextOut(e._item.first()); |
|
2789 |
} |
|
2790 |
} |
|
2791 |
|
|
2792 |
void firstIn(Arc& e, const Node& n) const { |
|
2793 |
if (!n._in) { |
|
2794 |
e._item.setSecond(n); |
|
2795 |
} else { |
|
2796 |
e._item.setFirst(); |
|
2797 |
_digraph->firstIn(e._item.first(), n); |
|
2798 |
} |
|
2799 |
} |
|
2800 |
|
|
2801 |
void nextIn(Arc& e) const { |
|
2802 |
if (!e._item.firstState()) { |
|
2803 |
e._item.setFirst(INVALID); |
|
2804 |
} else { |
|
2805 |
_digraph->nextIn(e._item.first()); |
|
2806 |
} |
|
2807 |
} |
|
2808 |
|
|
2809 |
Node source(const Arc& e) const { |
|
2810 |
if (e._item.firstState()) { |
|
2811 |
return Node(_digraph->source(e._item.first()), false); |
|
2812 |
} else { |
|
2813 |
return Node(e._item.second(), true); |
|
2814 |
} |
|
2815 |
} |
|
2816 |
|
|
2817 |
Node target(const Arc& e) const { |
|
2818 |
if (e._item.firstState()) { |
|
2819 |
return Node(_digraph->target(e._item.first()), true); |
|
2820 |
} else { |
|
2821 |
return Node(e._item.second(), false); |
|
2822 |
} |
|
2823 |
} |
|
2824 |
|
|
2825 |
int id(const Node& n) const { |
|
2826 |
return (_digraph->id(n) << 1) | (n._in ? 0 : 1); |
|
2827 |
} |
|
2828 |
Node nodeFromId(int ix) const { |
|
2829 |
return Node(_digraph->nodeFromId(ix >> 1), (ix & 1) == 0); |
|
2830 |
} |
|
2831 |
int maxNodeId() const { |
|
2832 |
return 2 * _digraph->maxNodeId() + 1; |
|
2833 |
} |
|
2834 |
|
|
2835 |
int id(const Arc& e) const { |
|
2836 |
if (e._item.firstState()) { |
|
2837 |
return _digraph->id(e._item.first()) << 1; |
|
2838 |
} else { |
|
2839 |
return (_digraph->id(e._item.second()) << 1) | 1; |
|
2840 |
} |
|
2841 |
} |
|
2842 |
Arc arcFromId(int ix) const { |
|
2843 |
if ((ix & 1) == 0) { |
|
2844 |
return Arc(_digraph->arcFromId(ix >> 1)); |
|
2845 |
} else { |
|
2846 |
return Arc(_digraph->nodeFromId(ix >> 1)); |
|
2847 |
} |
|
2848 |
} |
|
2849 |
int maxArcId() const { |
|
2850 |
return std::max(_digraph->maxNodeId() << 1, |
|
2851 |
(_digraph->maxArcId() << 1) | 1); |
|
2852 |
} |
|
2853 |
|
|
2854 |
static bool inNode(const Node& n) { |
|
2855 |
return n._in; |
|
2856 |
} |
|
2857 |
|
|
2858 |
static bool outNode(const Node& n) { |
|
2859 |
return !n._in; |
|
2860 |
} |
|
2861 |
|
|
2862 |
static bool origArc(const Arc& e) { |
|
2863 |
return e._item.firstState(); |
|
2864 |
} |
|
2865 |
|
|
2866 |
static bool bindArc(const Arc& e) { |
|
2867 |
return e._item.secondState(); |
|
2868 |
} |
|
2869 |
|
|
2870 |
static Node inNode(const DigraphNode& n) { |
|
2871 |
return Node(n, true); |
|
2872 |
} |
|
2873 |
|
|
2874 |
static Node outNode(const DigraphNode& n) { |
|
2875 |
return Node(n, false); |
|
2876 |
} |
|
2877 |
|
|
2878 |
static Arc arc(const DigraphNode& n) { |
|
2879 |
return Arc(n); |
|
2880 |
} |
|
2881 |
|
|
2882 |
static Arc arc(const DigraphArc& e) { |
|
2883 |
return Arc(e); |
|
2884 |
} |
|
2885 |
|
|
2886 |
typedef True NodeNumTag; |
|
2887 |
|
|
2888 |
int nodeNum() const { |
|
2889 |
return 2 * countNodes(*_digraph); |
|
2890 |
} |
|
2891 |
|
|
2892 |
typedef True EdgeNumTag; |
|
2893 |
int arcNum() const { |
|
2894 |
return countArcs(*_digraph) + countNodes(*_digraph); |
|
2895 |
} |
|
2896 |
|
|
2897 |
typedef True FindEdgeTag; |
|
2898 |
Arc findArc(const Node& u, const Node& v, |
|
2899 |
const Arc& prev = INVALID) const { |
|
2900 |
if (inNode(u)) { |
|
2901 |
if (outNode(v)) { |
|
2902 |
if (static_cast<const DigraphNode&>(u) == |
|
2903 |
static_cast<const DigraphNode&>(v) && prev == INVALID) { |
|
2904 |
return Arc(u); |
|
2905 |
} |
|
2906 |
} |
|
2907 |
} else { |
|
2908 |
if (inNode(v)) { |
|
2909 |
return Arc(::lemon::findArc(*_digraph, u, v, prev)); |
|
2910 |
} |
|
2911 |
} |
|
2912 |
return INVALID; |
|
2913 |
} |
|
2914 |
|
|
2915 |
private: |
|
2916 |
|
|
2917 |
template <typename _Value> |
|
2918 |
class NodeMapBase |
|
2919 |
: public MapTraits<typename Parent::template NodeMap<_Value> > { |
|
2920 |
typedef typename Parent::template NodeMap<_Value> NodeImpl; |
|
2921 |
public: |
|
2922 |
typedef Node Key; |
|
2923 |
typedef _Value Value; |
|
2924 |
|
|
2925 |
NodeMapBase(const Adaptor& adaptor) |
|
2926 |
: _in_map(*adaptor._digraph), _out_map(*adaptor._digraph) {} |
|
2927 |
NodeMapBase(const Adaptor& adaptor, const Value& value) |
|
2928 |
: _in_map(*adaptor._digraph, value), |
|
2929 |
_out_map(*adaptor._digraph, value) {} |
|
2930 |
|
|
2931 |
void set(const Node& key, const Value& val) { |
|
2932 |
if (Adaptor::inNode(key)) { _in_map.set(key, val); } |
|
2933 |
else {_out_map.set(key, val); } |
|
2934 |
} |
|
2935 |
|
|
2936 |
typename MapTraits<NodeImpl>::ReturnValue |
|
2937 |
operator[](const Node& key) { |
|
2938 |
if (Adaptor::inNode(key)) { return _in_map[key]; } |
|
2939 |
else { return _out_map[key]; } |
|
2940 |
} |
|
2941 |
|
|
2942 |
typename MapTraits<NodeImpl>::ConstReturnValue |
|
2943 |
operator[](const Node& key) const { |
|
2944 |
if (Adaptor::inNode(key)) { return _in_map[key]; } |
|
2945 |
else { return _out_map[key]; } |
|
2946 |
} |
|
2947 |
|
|
2948 |
private: |
|
2949 |
NodeImpl _in_map, _out_map; |
|
2950 |
}; |
|
2951 |
|
|
2952 |
template <typename _Value> |
|
2953 |
class ArcMapBase |
|
2954 |
: public MapTraits<typename Parent::template ArcMap<_Value> > { |
|
2955 |
typedef typename Parent::template ArcMap<_Value> ArcImpl; |
|
2956 |
typedef typename Parent::template NodeMap<_Value> NodeImpl; |
|
2957 |
public: |
|
2958 |
typedef Arc Key; |
|
2959 |
typedef _Value Value; |
|
2960 |
|
|
2961 |
ArcMapBase(const Adaptor& adaptor) |
|
2962 |
: _arc_map(*adaptor._digraph), _node_map(*adaptor._digraph) {} |
|
2963 |
ArcMapBase(const Adaptor& adaptor, const Value& value) |
|
2964 |
: _arc_map(*adaptor._digraph, value), |
|
2965 |
_node_map(*adaptor._digraph, value) {} |
|
2966 |
|
|
2967 |
void set(const Arc& key, const Value& val) { |
|
2968 |
if (Adaptor::origArc(key)) { |
|
2969 |
_arc_map.set(key._item.first(), val); |
|
2970 |
} else { |
|
2971 |
_node_map.set(key._item.second(), val); |
|
2972 |
} |
|
2973 |
} |
|
2974 |
|
|
2975 |
typename MapTraits<ArcImpl>::ReturnValue |
|
2976 |
operator[](const Arc& key) { |
|
2977 |
if (Adaptor::origArc(key)) { |
|
2978 |
return _arc_map[key._item.first()]; |
|
2979 |
} else { |
|
2980 |
return _node_map[key._item.second()]; |
|
2981 |
} |
|
2982 |
} |
|
2983 |
|
|
2984 |
typename MapTraits<ArcImpl>::ConstReturnValue |
|
2985 |
operator[](const Arc& key) const { |
|
2986 |
if (Adaptor::origArc(key)) { |
|
2987 |
return _arc_map[key._item.first()]; |
|
2988 |
} else { |
|
2989 |
return _node_map[key._item.second()]; |
|
2990 |
} |
|
2991 |
} |
|
2992 |
|
|
2993 |
private: |
|
2994 |
ArcImpl _arc_map; |
|
2995 |
NodeImpl _node_map; |
|
2996 |
}; |
|
2997 |
|
|
2998 |
public: |
|
2999 |
|
|
3000 |
template <typename _Value> |
|
3001 |
class NodeMap |
|
3002 |
: public SubMapExtender<Adaptor, NodeMapBase<_Value> > |
|
3003 |
{ |
|
3004 |
public: |
|
3005 |
typedef _Value Value; |
|
3006 |
typedef SubMapExtender<Adaptor, NodeMapBase<Value> > Parent; |
|
3007 |
|
|
3008 |
NodeMap(const Adaptor& adaptor) |
|
3009 |
: Parent(adaptor) {} |
|
3010 |
|
|
3011 |
NodeMap(const Adaptor& adaptor, const Value& value) |
|
3012 |
: Parent(adaptor, value) {} |
|
3013 |
|
|
3014 |
private: |
|
3015 |
NodeMap& operator=(const NodeMap& cmap) { |
|
3016 |
return operator=<NodeMap>(cmap); |
|
3017 |
} |
|
3018 |
|
|
3019 |
template <typename CMap> |
|
3020 |
NodeMap& operator=(const CMap& cmap) { |
|
3021 |
Parent::operator=(cmap); |
|
3022 |
return *this; |
|
3023 |
} |
|
3024 |
}; |
|
3025 |
|
|
3026 |
template <typename _Value> |
|
3027 |
class ArcMap |
|
3028 |
: public SubMapExtender<Adaptor, ArcMapBase<_Value> > |
|
3029 |
{ |
|
3030 |
public: |
|
3031 |
typedef _Value Value; |
|
3032 |
typedef SubMapExtender<Adaptor, ArcMapBase<Value> > Parent; |
|
3033 |
|
|
3034 |
ArcMap(const Adaptor& adaptor) |
|
3035 |
: Parent(adaptor) {} |
|
3036 |
|
|
3037 |
ArcMap(const Adaptor& adaptor, const Value& value) |
|
3038 |
: Parent(adaptor, value) {} |
|
3039 |
|
|
3040 |
private: |
|
3041 |
ArcMap& operator=(const ArcMap& cmap) { |
|
3042 |
return operator=<ArcMap>(cmap); |
|
3043 |
} |
|
3044 |
|
|
3045 |
template <typename CMap> |
|
3046 |
ArcMap& operator=(const CMap& cmap) { |
|
3047 |
Parent::operator=(cmap); |
|
3048 |
return *this; |
|
3049 |
} |
|
3050 |
}; |
|
3051 |
|
|
3052 |
protected: |
|
3053 |
|
|
3054 |
SplitNodesBase() : _digraph(0) {} |
|
3055 |
|
|
3056 |
Digraph* _digraph; |
|
3057 |
|
|
3058 |
void setDigraph(Digraph& digraph) { |
|
3059 |
_digraph = &digraph; |
|
3060 |
} |
|
3061 |
|
|
3062 |
}; |
|
3063 |
|
|
3064 |
/// \ingroup graph_adaptors |
|
3065 |
/// |
|
3066 |
/// \brief Split the nodes of a directed graph |
|
3067 |
/// |
|
3068 |
/// The SplitNodes adaptor splits each node into an in-node and an |
|
3069 |
/// out-node. Formaly, the adaptor replaces each \f$ u \f$ node in |
|
3070 |
/// the digraph with two nodes(namely node \f$ u_{in} \f$ and node |
|
3071 |
/// \f$ u_{out} \f$). If there is a \f$ (v, u) \f$ arc in the |
|
3072 |
/// original digraph the new target of the arc will be \f$ u_{in} \f$ |
|
3073 |
/// and similarly the source of the original \f$ (u, v) \f$ arc |
|
3074 |
/// will be \f$ u_{out} \f$. The adaptor will add for each node in |
|
3075 |
/// the original digraph an additional arc which connects |
|
3076 |
/// \f$ (u_{in}, u_{out}) \f$. |
|
3077 |
/// |
|
3078 |
/// The aim of this class is to run algorithm with node costs if the |
|
3079 |
/// algorithm can use directly just arc costs. In this case we should use |
|
3080 |
/// a \c SplitNodes and set the node cost of the graph to the |
|
3081 |
/// bind arc in the adapted graph. |
|
3082 |
/// |
|
3083 |
/// \tparam _Digraph It must be conform to the \ref concepts::Digraph |
|
3084 |
/// "Digraph concept". The type can be specified to be const. |
|
3085 |
template <typename _Digraph> |
|
3086 |
class SplitNodes |
|
3087 |
: public DigraphAdaptorExtender<SplitNodesBase<_Digraph> > { |
|
3088 |
public: |
|
3089 |
typedef _Digraph Digraph; |
|
3090 |
typedef DigraphAdaptorExtender<SplitNodesBase<Digraph> > Parent; |
|
3091 |
|
|
3092 |
typedef typename Digraph::Node DigraphNode; |
|
3093 |
typedef typename Digraph::Arc DigraphArc; |
|
3094 |
|
|
3095 |
typedef typename Parent::Node Node; |
|
3096 |
typedef typename Parent::Arc Arc; |
|
3097 |
|
|
3098 |
/// \brief Constructor of the adaptor. |
|
3099 |
/// |
|
3100 |
/// Constructor of the adaptor. |
|
3101 |
SplitNodes(Digraph& g) { |
|
3102 |
Parent::setDigraph(g); |
|
3103 |
} |
|
3104 |
|
|
3105 |
/// \brief Returns true when the node is in-node. |
|
3106 |
/// |
|
3107 |
/// Returns true when the node is in-node. |
|
3108 |
static bool inNode(const Node& n) { |
|
3109 |
return Parent::inNode(n); |
|
3110 |
} |
|
3111 |
|
|
3112 |
/// \brief Returns true when the node is out-node. |
|
3113 |
/// |
|
3114 |
/// Returns true when the node is out-node. |
|
3115 |
static bool outNode(const Node& n) { |
|
3116 |
return Parent::outNode(n); |
|
3117 |
} |
|
3118 |
|
|
3119 |
/// \brief Returns true when the arc is arc in the original digraph. |
|
3120 |
/// |
|
3121 |
/// Returns true when the arc is arc in the original digraph. |
|
3122 |
static bool origArc(const Arc& a) { |
|
3123 |
return Parent::origArc(a); |
|
3124 |
} |
|
3125 |
|
|
3126 |
/// \brief Returns true when the arc binds an in-node and an out-node. |
|
3127 |
/// |
|
3128 |
/// Returns true when the arc binds an in-node and an out-node. |
|
3129 |
static bool bindArc(const Arc& a) { |
|
3130 |
return Parent::bindArc(a); |
|
3131 |
} |
|
3132 |
|
|
3133 |
/// \brief Gives back the in-node created from the \c node. |
|
3134 |
/// |
|
3135 |
/// Gives back the in-node created from the \c node. |
|
3136 |
static Node inNode(const DigraphNode& n) { |
|
3137 |
return Parent::inNode(n); |
|
3138 |
} |
|
3139 |
|
|
3140 |
/// \brief Gives back the out-node created from the \c node. |
|
3141 |
/// |
|
3142 |
/// Gives back the out-node created from the \c node. |
|
3143 |
static Node outNode(const DigraphNode& n) { |
|
3144 |
return Parent::outNode(n); |
|
3145 |
} |
|
3146 |
|
|
3147 |
/// \brief Gives back the arc binds the two part of the node. |
|
3148 |
/// |
|
3149 |
/// Gives back the arc binds the two part of the node. |
|
3150 |
static Arc arc(const DigraphNode& n) { |
|
3151 |
return Parent::arc(n); |
|
3152 |
} |
|
3153 |
|
|
3154 |
/// \brief Gives back the arc of the original arc. |
|
3155 |
/// |
|
3156 |
/// Gives back the arc of the original arc. |
|
3157 |
static Arc arc(const DigraphArc& a) { |
|
3158 |
return Parent::arc(a); |
|
3159 |
} |
|
3160 |
|
|
3161 |
/// \brief NodeMap combined from two original NodeMap |
|
3162 |
/// |
|
3163 |
/// This class adapt two of the original digraph NodeMap to |
|
3164 |
/// get a node map on the adapted digraph. |
|
3165 |
template <typename InNodeMap, typename OutNodeMap> |
|
3166 |
class CombinedNodeMap { |
|
3167 |
public: |
|
3168 |
|
|
3169 |
typedef Node Key; |
|
3170 |
typedef typename InNodeMap::Value Value; |
|
3171 |
|
|
3172 |
/// \brief Constructor |
|
3173 |
/// |
|
3174 |
/// Constructor. |
|
3175 |
CombinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) |
|
3176 |
: _in_map(in_map), _out_map(out_map) {} |
|
3177 |
|
|
3178 |
/// \brief The subscript operator. |
|
3179 |
/// |
|
3180 |
/// The subscript operator. |
|
3181 |
Value& operator[](const Key& key) { |
|
3182 |
if (Parent::inNode(key)) { |
|
3183 |
return _in_map[key]; |
|
3184 |
} else { |
|
3185 |
return _out_map[key]; |
|
3186 |
} |
|
3187 |
} |
|
3188 |
|
|
3189 |
/// \brief The const subscript operator. |
|
3190 |
/// |
|
3191 |
/// The const subscript operator. |
|
3192 |
Value operator[](const Key& key) const { |
|
3193 |
if (Parent::inNode(key)) { |
|
3194 |
return _in_map[key]; |
|
3195 |
} else { |
|
3196 |
return _out_map[key]; |
|
3197 |
} |
|
3198 |
} |
|
3199 |
|
|
3200 |
/// \brief The setter function of the map. |
|
3201 |
/// |
|
3202 |
/// The setter function of the map. |
|
3203 |
void set(const Key& key, const Value& value) { |
|
3204 |
if (Parent::inNode(key)) { |
|
3205 |
_in_map.set(key, value); |
|
3206 |
} else { |
|
3207 |
_out_map.set(key, value); |
|
3208 |
} |
|
3209 |
} |
|
3210 |
|
|
3211 |
private: |
|
3212 |
|
|
3213 |
InNodeMap& _in_map; |
|
3214 |
OutNodeMap& _out_map; |
|
3215 |
|
|
3216 |
}; |
|
3217 |
|
|
3218 |
|
|
3219 |
/// \brief Just gives back a combined node map |
|
3220 |
/// |
|
3221 |
/// Just gives back a combined node map |
|
3222 |
template <typename InNodeMap, typename OutNodeMap> |
|
3223 |
static CombinedNodeMap<InNodeMap, OutNodeMap> |
|
3224 |
combinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) { |
|
3225 |
return CombinedNodeMap<InNodeMap, OutNodeMap>(in_map, out_map); |
|
3226 |
} |
|
3227 |
|
|
3228 |
template <typename InNodeMap, typename OutNodeMap> |
|
3229 |
static CombinedNodeMap<const InNodeMap, OutNodeMap> |
|
3230 |
combinedNodeMap(const InNodeMap& in_map, OutNodeMap& out_map) { |
|
3231 |
return CombinedNodeMap<const InNodeMap, OutNodeMap>(in_map, out_map); |
|
3232 |
} |
|
3233 |
|
|
3234 |
template <typename InNodeMap, typename OutNodeMap> |
|
3235 |
static CombinedNodeMap<InNodeMap, const OutNodeMap> |
|
3236 |
combinedNodeMap(InNodeMap& in_map, const OutNodeMap& out_map) { |
|
3237 |
return CombinedNodeMap<InNodeMap, const OutNodeMap>(in_map, out_map); |
|
3238 |
} |
|
3239 |
|
|
3240 |
template <typename InNodeMap, typename OutNodeMap> |
|
3241 |
static CombinedNodeMap<const InNodeMap, const OutNodeMap> |
|
3242 |
combinedNodeMap(const InNodeMap& in_map, const OutNodeMap& out_map) { |
|
3243 |
return CombinedNodeMap<const InNodeMap, |
|
3244 |
const OutNodeMap>(in_map, out_map); |
|
3245 |
} |
|
3246 |
|
|
3247 |
/// \brief ArcMap combined from an original ArcMap and a NodeMap |
|
3248 |
/// |
|
3249 |
/// This class adapt an original ArcMap and a NodeMap to get an |
|
3250 |
/// arc map on the adapted digraph |
|
3251 |
template <typename DigraphArcMap, typename DigraphNodeMap> |
|
3252 |
class CombinedArcMap { |
|
3253 |
public: |
|
3254 |
|
|
3255 |
typedef Arc Key; |
|
3256 |
typedef typename DigraphArcMap::Value Value; |
|
3257 |
|
|
3258 |
/// \brief Constructor |
|
3259 |
/// |
|
3260 |
/// Constructor. |
|
3261 |
CombinedArcMap(DigraphArcMap& arc_map, DigraphNodeMap& node_map) |
|
3262 |
: _arc_map(arc_map), _node_map(node_map) {} |
|
3263 |
|
|
3264 |
/// \brief The subscript operator. |
|
3265 |
/// |
|
3266 |
/// The subscript operator. |
|
3267 |
void set(const Arc& arc, const Value& val) { |
|
3268 |
if (Parent::origArc(arc)) { |
|
3269 |
_arc_map.set(arc, val); |
|
3270 |
} else { |
|
3271 |
_node_map.set(arc, val); |
|
3272 |
} |
|
3273 |
} |
|
3274 |
|
|
3275 |
/// \brief The const subscript operator. |
|
3276 |
/// |
|
3277 |
/// The const subscript operator. |
|
3278 |
Value operator[](const Key& arc) const { |
|
3279 |
if (Parent::origArc(arc)) { |
|
3280 |
return _arc_map[arc]; |
|
3281 |
} else { |
|
3282 |
return _node_map[arc]; |
|
3283 |
} |
|
3284 |
} |
|
3285 |
|
|
3286 |
/// \brief The const subscript operator. |
|
3287 |
/// |
|
3288 |
/// The const subscript operator. |
|
3289 |
Value& operator[](const Key& arc) { |
|
3290 |
if (Parent::origArc(arc)) { |
|
3291 |
return _arc_map[arc]; |
|
3292 |
} else { |
|
3293 |
return _node_map[arc]; |
|
3294 |
} |
|
3295 |
} |
|
3296 |
|
|
3297 |
private: |
|
3298 |
DigraphArcMap& _arc_map; |
|
3299 |
DigraphNodeMap& _node_map; |
|
3300 |
}; |
|
3301 |
|
|
3302 |
/// \brief Just gives back a combined arc map |
|
3303 |
/// |
|
3304 |
/// Just gives back a combined arc map |
|
3305 |
template <typename DigraphArcMap, typename DigraphNodeMap> |
|
3306 |
static CombinedArcMap<DigraphArcMap, DigraphNodeMap> |
|
3307 |
combinedArcMap(DigraphArcMap& arc_map, DigraphNodeMap& node_map) { |
|
3308 |
return CombinedArcMap<DigraphArcMap, DigraphNodeMap>(arc_map, node_map); |
|
3309 |
} |
|
3310 |
|
|
3311 |
template <typename DigraphArcMap, typename DigraphNodeMap> |
|
3312 |
static CombinedArcMap<const DigraphArcMap, DigraphNodeMap> |
|
3313 |
combinedArcMap(const DigraphArcMap& arc_map, DigraphNodeMap& node_map) { |
|
3314 |
return CombinedArcMap<const DigraphArcMap, |
|
3315 |
DigraphNodeMap>(arc_map, node_map); |
|
3316 |
} |
|
3317 |
|
|
3318 |
template <typename DigraphArcMap, typename DigraphNodeMap> |
|
3319 |
static CombinedArcMap<DigraphArcMap, const DigraphNodeMap> |
|
3320 |
combinedArcMap(DigraphArcMap& arc_map, const DigraphNodeMap& node_map) { |
|
3321 |
return CombinedArcMap<DigraphArcMap, |
|
3322 |
const DigraphNodeMap>(arc_map, node_map); |
|
3323 |
} |
|
3324 |
|
|
3325 |
template <typename DigraphArcMap, typename DigraphNodeMap> |
|
3326 |
static CombinedArcMap<const DigraphArcMap, const DigraphNodeMap> |
|
3327 |
combinedArcMap(const DigraphArcMap& arc_map, |
|
3328 |
const DigraphNodeMap& node_map) { |
|
3329 |
return CombinedArcMap<const DigraphArcMap, |
|
3330 |
const DigraphNodeMap>(arc_map, node_map); |
|
3331 |
} |
|
3332 |
|
|
3333 |
}; |
|
3334 |
|
|
3335 |
/// \brief Just gives back a node splitter |
|
3336 |
/// |
|
3337 |
/// Just gives back a node splitter |
|
3338 |
template<typename Digraph> |
|
3339 |
SplitNodes<Digraph> |
|
3340 |
splitNodes(const Digraph& digraph) { |
|
3341 |
return SplitNodes<Digraph>(digraph); |
|
3342 |
} |
|
3343 |
|
|
3344 |
|
|
3345 |
} //namespace lemon |
|
3346 |
|
|
3347 |
#endif //LEMON_ADAPTORS_H |
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H |
|
20 |
#define LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H |
|
21 |
|
|
22 |
#include <lemon/core.h> |
|
23 |
#include <lemon/error.h> |
|
24 |
|
|
25 |
#include <lemon/bits/default_map.h> |
|
26 |
|
|
27 |
namespace lemon { |
|
28 |
|
|
29 |
template <typename _Digraph> |
|
30 |
class DigraphAdaptorExtender : public _Digraph { |
|
31 |
public: |
|
32 |
|
|
33 |
typedef _Digraph Parent; |
|
34 |
typedef _Digraph Digraph; |
|
35 |
typedef DigraphAdaptorExtender Adaptor; |
|
36 |
|
|
37 |
// Base extensions |
|
38 |
|
|
39 |
typedef typename Parent::Node Node; |
|
40 |
typedef typename Parent::Arc Arc; |
|
41 |
|
|
42 |
int maxId(Node) const { |
|
43 |
return Parent::maxNodeId(); |
|
44 |
} |
|
45 |
|
|
46 |
int maxId(Arc) const { |
|
47 |
return Parent::maxArcId(); |
|
48 |
} |
|
49 |
|
|
50 |
Node fromId(int id, Node) const { |
|
51 |
return Parent::nodeFromId(id); |
|
52 |
} |
|
53 |
|
|
54 |
Arc fromId(int id, Arc) const { |
|
55 |
return Parent::arcFromId(id); |
|
56 |
} |
|
57 |
|
|
58 |
Node oppositeNode(const Node &n, const Arc &e) const { |
|
59 |
if (n == Parent::source(e)) |
|
60 |
return Parent::target(e); |
|
61 |
else if(n==Parent::target(e)) |
|
62 |
return Parent::source(e); |
|
63 |
else |
|
64 |
return INVALID; |
|
65 |
} |
|
66 |
|
|
67 |
class NodeIt : public Node { |
|
68 |
const Adaptor* _adaptor; |
|
69 |
public: |
|
70 |
|
|
71 |
NodeIt() {} |
|
72 |
|
|
73 |
NodeIt(Invalid i) : Node(i) { } |
|
74 |
|
|
75 |
explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
|
76 |
_adaptor->first(static_cast<Node&>(*this)); |
|
77 |
} |
|
78 |
|
|
79 |
NodeIt(const Adaptor& adaptor, const Node& node) |
|
80 |
: Node(node), _adaptor(&adaptor) {} |
|
81 |
|
|
82 |
NodeIt& operator++() { |
|
83 |
_adaptor->next(*this); |
|
84 |
return *this; |
|
85 |
} |
|
86 |
|
|
87 |
}; |
|
88 |
|
|
89 |
|
|
90 |
class ArcIt : public Arc { |
|
91 |
const Adaptor* _adaptor; |
|
92 |
public: |
|
93 |
|
|
94 |
ArcIt() { } |
|
95 |
|
|
96 |
ArcIt(Invalid i) : Arc(i) { } |
|
97 |
|
|
98 |
explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
|
99 |
_adaptor->first(static_cast<Arc&>(*this)); |
|
100 |
} |
|
101 |
|
|
102 |
ArcIt(const Adaptor& adaptor, const Arc& e) : |
|
103 |
Arc(e), _adaptor(&adaptor) { } |
|
104 |
|
|
105 |
ArcIt& operator++() { |
|
106 |
_adaptor->next(*this); |
|
107 |
return *this; |
|
108 |
} |
|
109 |
|
|
110 |
}; |
|
111 |
|
|
112 |
|
|
113 |
class OutArcIt : public Arc { |
|
114 |
const Adaptor* _adaptor; |
|
115 |
public: |
|
116 |
|
|
117 |
OutArcIt() { } |
|
118 |
|
|
119 |
OutArcIt(Invalid i) : Arc(i) { } |
|
120 |
|
|
121 |
OutArcIt(const Adaptor& adaptor, const Node& node) |
|
122 |
: _adaptor(&adaptor) { |
|
123 |
_adaptor->firstOut(*this, node); |
|
124 |
} |
|
125 |
|
|
126 |
OutArcIt(const Adaptor& adaptor, const Arc& arc) |
|
127 |
: Arc(arc), _adaptor(&adaptor) {} |
|
128 |
|
|
129 |
OutArcIt& operator++() { |
|
130 |
_adaptor->nextOut(*this); |
|
131 |
return *this; |
|
132 |
} |
|
133 |
|
|
134 |
}; |
|
135 |
|
|
136 |
|
|
137 |
class InArcIt : public Arc { |
|
138 |
const Adaptor* _adaptor; |
|
139 |
public: |
|
140 |
|
|
141 |
InArcIt() { } |
|
142 |
|
|
143 |
InArcIt(Invalid i) : Arc(i) { } |
|
144 |
|
|
145 |
InArcIt(const Adaptor& adaptor, const Node& node) |
|
146 |
: _adaptor(&adaptor) { |
|
147 |
_adaptor->firstIn(*this, node); |
|
148 |
} |
|
149 |
|
|
150 |
InArcIt(const Adaptor& adaptor, const Arc& arc) : |
|
151 |
Arc(arc), _adaptor(&adaptor) {} |
|
152 |
|
|
153 |
InArcIt& operator++() { |
|
154 |
_adaptor->nextIn(*this); |
|
155 |
return *this; |
|
156 |
} |
|
157 |
|
|
158 |
}; |
|
159 |
|
|
160 |
Node baseNode(const OutArcIt &e) const { |
|
161 |
return Parent::source(e); |
|
162 |
} |
|
163 |
Node runningNode(const OutArcIt &e) const { |
|
164 |
return Parent::target(e); |
|
165 |
} |
|
166 |
|
|
167 |
Node baseNode(const InArcIt &e) const { |
|
168 |
return Parent::target(e); |
|
169 |
} |
|
170 |
Node runningNode(const InArcIt &e) const { |
|
171 |
return Parent::source(e); |
|
172 |
} |
|
173 |
|
|
174 |
}; |
|
175 |
|
|
176 |
|
|
177 |
/// \ingroup digraphbits |
|
178 |
/// |
|
179 |
/// \brief Extender for the GraphAdaptors |
|
180 |
template <typename _Graph> |
|
181 |
class GraphAdaptorExtender : public _Graph { |
|
182 |
public: |
|
183 |
|
|
184 |
typedef _Graph Parent; |
|
185 |
typedef _Graph Graph; |
|
186 |
typedef GraphAdaptorExtender Adaptor; |
|
187 |
|
|
188 |
typedef typename Parent::Node Node; |
|
189 |
typedef typename Parent::Arc Arc; |
|
190 |
typedef typename Parent::Edge Edge; |
|
191 |
|
|
192 |
// Graph extension |
|
193 |
|
|
194 |
int maxId(Node) const { |
|
195 |
return Parent::maxNodeId(); |
|
196 |
} |
|
197 |
|
|
198 |
int maxId(Arc) const { |
|
199 |
return Parent::maxArcId(); |
|
200 |
} |
|
201 |
|
|
202 |
int maxId(Edge) const { |
|
203 |
return Parent::maxEdgeId(); |
|
204 |
} |
|
205 |
|
|
206 |
Node fromId(int id, Node) const { |
|
207 |
return Parent::nodeFromId(id); |
|
208 |
} |
|
209 |
|
|
210 |
Arc fromId(int id, Arc) const { |
|
211 |
return Parent::arcFromId(id); |
|
212 |
} |
|
213 |
|
|
214 |
Edge fromId(int id, Edge) const { |
|
215 |
return Parent::edgeFromId(id); |
|
216 |
} |
|
217 |
|
|
218 |
Node oppositeNode(const Node &n, const Edge &e) const { |
|
219 |
if( n == Parent::u(e)) |
|
220 |
return Parent::v(e); |
|
221 |
else if( n == Parent::v(e)) |
|
222 |
return Parent::u(e); |
|
223 |
else |
|
224 |
return INVALID; |
|
225 |
} |
|
226 |
|
|
227 |
Arc oppositeArc(const Arc &a) const { |
|
228 |
return Parent::direct(a, !Parent::direction(a)); |
|
229 |
} |
|
230 |
|
|
231 |
using Parent::direct; |
|
232 |
Arc direct(const Edge &e, const Node &s) const { |
|
233 |
return Parent::direct(e, Parent::u(e) == s); |
|
234 |
} |
|
235 |
|
|
236 |
|
|
237 |
class NodeIt : public Node { |
|
238 |
const Adaptor* _adaptor; |
|
239 |
public: |
|
240 |
|
|
241 |
NodeIt() {} |
|
242 |
|
|
243 |
NodeIt(Invalid i) : Node(i) { } |
|
244 |
|
|
245 |
explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
|
246 |
_adaptor->first(static_cast<Node&>(*this)); |
|
247 |
} |
|
248 |
|
|
249 |
NodeIt(const Adaptor& adaptor, const Node& node) |
|
250 |
: Node(node), _adaptor(&adaptor) {} |
|
251 |
|
|
252 |
NodeIt& operator++() { |
|
253 |
_adaptor->next(*this); |
|
254 |
return *this; |
|
255 |
} |
|
256 |
|
|
257 |
}; |
|
258 |
|
|
259 |
|
|
260 |
class ArcIt : public Arc { |
|
261 |
const Adaptor* _adaptor; |
|
262 |
public: |
|
263 |
|
|
264 |
ArcIt() { } |
|
265 |
|
|
266 |
ArcIt(Invalid i) : Arc(i) { } |
|
267 |
|
|
268 |
explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
|
269 |
_adaptor->first(static_cast<Arc&>(*this)); |
|
270 |
} |
|
271 |
|
|
272 |
ArcIt(const Adaptor& adaptor, const Arc& e) : |
|
273 |
Arc(e), _adaptor(&adaptor) { } |
|
274 |
|
|
275 |
ArcIt& operator++() { |
|
276 |
_adaptor->next(*this); |
|
277 |
return *this; |
|
278 |
} |
|
279 |
|
|
280 |
}; |
|
281 |
|
|
282 |
|
|
283 |
class OutArcIt : public Arc { |
|
284 |
const Adaptor* _adaptor; |
|
285 |
public: |
|
286 |
|
|
287 |
OutArcIt() { } |
|
288 |
|
|
289 |
OutArcIt(Invalid i) : Arc(i) { } |
|
290 |
|
|
291 |
OutArcIt(const Adaptor& adaptor, const Node& node) |
|
292 |
: _adaptor(&adaptor) { |
|
293 |
_adaptor->firstOut(*this, node); |
|
294 |
} |
|
295 |
|
|
296 |
OutArcIt(const Adaptor& adaptor, const Arc& arc) |
|
297 |
: Arc(arc), _adaptor(&adaptor) {} |
|
298 |
|
|
299 |
OutArcIt& operator++() { |
|
300 |
_adaptor->nextOut(*this); |
|
301 |
return *this; |
|
302 |
} |
|
303 |
|
|
304 |
}; |
|
305 |
|
|
306 |
|
|
307 |
class InArcIt : public Arc { |
|
308 |
const Adaptor* _adaptor; |
|
309 |
public: |
|
310 |
|
|
311 |
InArcIt() { } |
|
312 |
|
|
313 |
InArcIt(Invalid i) : Arc(i) { } |
|
314 |
|
|
315 |
InArcIt(const Adaptor& adaptor, const Node& node) |
|
316 |
: _adaptor(&adaptor) { |
|
317 |
_adaptor->firstIn(*this, node); |
|
318 |
} |
|
319 |
|
|
320 |
InArcIt(const Adaptor& adaptor, const Arc& arc) : |
|
321 |
Arc(arc), _adaptor(&adaptor) {} |
|
322 |
|
|
323 |
InArcIt& operator++() { |
|
324 |
_adaptor->nextIn(*this); |
|
325 |
return *this; |
|
326 |
} |
|
327 |
|
|
328 |
}; |
|
329 |
|
|
330 |
class EdgeIt : public Parent::Edge { |
|
331 |
const Adaptor* _adaptor; |
|
332 |
public: |
|
333 |
|
|
334 |
EdgeIt() { } |
|
335 |
|
|
336 |
EdgeIt(Invalid i) : Edge(i) { } |
|
337 |
|
|
338 |
explicit EdgeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
|
339 |
_adaptor->first(static_cast<Edge&>(*this)); |
|
340 |
} |
|
341 |
|
|
342 |
EdgeIt(const Adaptor& adaptor, const Edge& e) : |
|
343 |
Edge(e), _adaptor(&adaptor) { } |
|
344 |
|
|
345 |
EdgeIt& operator++() { |
|
346 |
_adaptor->next(*this); |
|
347 |
return *this; |
|
348 |
} |
|
349 |
|
|
350 |
}; |
|
351 |
|
|
352 |
class IncEdgeIt : public Edge { |
|
353 |
friend class GraphAdaptorExtender; |
|
354 |
const Adaptor* _adaptor; |
|
355 |
bool direction; |
|
356 |
public: |
|
357 |
|
|
358 |
IncEdgeIt() { } |
|
359 |
|
|
360 |
IncEdgeIt(Invalid i) : Edge(i), direction(false) { } |
|
361 |
|
|
362 |
IncEdgeIt(const Adaptor& adaptor, const Node &n) : _adaptor(&adaptor) { |
|
363 |
_adaptor->firstInc(static_cast<Edge&>(*this), direction, n); |
|
364 |
} |
|
365 |
|
|
366 |
IncEdgeIt(const Adaptor& adaptor, const Edge &e, const Node &n) |
|
367 |
: _adaptor(&adaptor), Edge(e) { |
|
368 |
direction = (_adaptor->u(e) == n); |
|
369 |
} |
|
370 |
|
|
371 |
IncEdgeIt& operator++() { |
|
372 |
_adaptor->nextInc(*this, direction); |
|
373 |
return *this; |
|
374 |
} |
|
375 |
}; |
|
376 |
|
|
377 |
Node baseNode(const OutArcIt &a) const { |
|
378 |
return Parent::source(a); |
|
379 |
} |
|
380 |
Node runningNode(const OutArcIt &a) const { |
|
381 |
return Parent::target(a); |
|
382 |
} |
|
383 |
|
|
384 |
Node baseNode(const InArcIt &a) const { |
|
385 |
return Parent::target(a); |
|
386 |
} |
|
387 |
Node runningNode(const InArcIt &a) const { |
|
388 |
return Parent::source(a); |
|
389 |
} |
|
390 |
|
|
391 |
Node baseNode(const IncEdgeIt &e) const { |
|
392 |
return e.direction ? Parent::u(e) : Parent::v(e); |
|
393 |
} |
|
394 |
Node runningNode(const IncEdgeIt &e) const { |
|
395 |
return e.direction ? Parent::v(e) : Parent::u(e); |
|
396 |
} |
|
397 |
|
|
398 |
}; |
|
399 |
|
|
400 |
} |
|
401 |
|
|
402 |
|
|
403 |
#endif |
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_BITS_VARIANT_H |
|
20 |
#define LEMON_BITS_VARIANT_H |
|
21 |
|
|
22 |
#include <lemon/assert.h> |
|
23 |
|
|
24 |
/// \file |
|
25 |
/// \brief Variant types |
|
26 |
|
|
27 |
namespace lemon { |
|
28 |
|
|
29 |
namespace _variant_bits { |
|
30 |
|
|
31 |
template <int left, int right> |
|
32 |
struct CTMax { |
|
33 |
static const int value = left < right ? right : left; |
|
34 |
}; |
|
35 |
|
|
36 |
} |
|
37 |
|
|
38 |
|
|
39 |
/// \brief Simple Variant type for two types |
|
40 |
/// |
|
41 |
/// Simple Variant type for two types. The Variant type is a type |
|
42 |
/// safe union. The C++ has strong limitations for using unions, by |
|
43 |
/// example we can not store type with non default constructor or |
|
44 |
/// destructor in an union. This class always knowns the current |
|
45 |
/// state of the variant and it cares for the proper construction |
|
46 |
/// and destruction. |
|
47 |
template <typename _First, typename _Second> |
|
48 |
class BiVariant { |
|
49 |
public: |
|
50 |
|
|
51 |
/// \brief The \c First type. |
|
52 |
typedef _First First; |
|
53 |
/// \brief The \c Second type. |
|
54 |
typedef _Second Second; |
|
55 |
|
|
56 |
/// \brief Constructor |
|
57 |
/// |
|
58 |
/// This constructor initalizes to the default value of the \c First |
|
59 |
/// type. |
|
60 |
BiVariant() { |
|
61 |
flag = true; |
|
62 |
new(reinterpret_cast<First*>(data)) First(); |
|
63 |
} |
|
64 |
|
|
65 |
/// \brief Constructor |
|
66 |
/// |
|
67 |
/// This constructor initalizes to the given value of the \c First |
|
68 |
/// type. |
|
69 |
BiVariant(const First& f) { |
|
70 |
flag = true; |
|
71 |
new(reinterpret_cast<First*>(data)) First(f); |
|
72 |
} |
|
73 |
|
|
74 |
/// \brief Constructor |
|
75 |
/// |
|
76 |
/// This constructor initalizes to the given value of the \c |
|
77 |
/// Second type. |
|
78 |
BiVariant(const Second& s) { |
|
79 |
flag = false; |
|
80 |
new(reinterpret_cast<Second*>(data)) Second(s); |
|
81 |
} |
|
82 |
|
|
83 |
/// \brief Copy constructor |
|
84 |
/// |
|
85 |
/// Copy constructor |
|
86 |
BiVariant(const BiVariant& bivariant) { |
|
87 |
flag = bivariant.flag; |
|
88 |
if (flag) { |
|
89 |
new(reinterpret_cast<First*>(data)) First(bivariant.first()); |
|
90 |
} else { |
|
91 |
new(reinterpret_cast<Second*>(data)) Second(bivariant.second()); |
|
92 |
} |
|
93 |
} |
|
94 |
|
|
95 |
/// \brief Destrcutor |
|
96 |
/// |
|
97 |
/// Destructor |
|
98 |
~BiVariant() { |
|
99 |
destroy(); |
|
100 |
} |
|
101 |
|
|
102 |
/// \brief Set to the default value of the \c First type. |
|
103 |
/// |
|
104 |
/// This function sets the variant to the default value of the \c |
|
105 |
/// First type. |
|
106 |
BiVariant& setFirst() { |
|
107 |
destroy(); |
|
108 |
flag = true; |
|
109 |
new(reinterpret_cast<First*>(data)) First(); |
|
110 |
return *this; |
|
111 |
} |
|
112 |
|
|
113 |
/// \brief Set to the given value of the \c First type. |
|
114 |
/// |
|
115 |
/// This function sets the variant to the given value of the \c |
|
116 |
/// First type. |
|
117 |
BiVariant& setFirst(const First& f) { |
|
118 |
destroy(); |
|
119 |
flag = true; |
|
120 |
new(reinterpret_cast<First*>(data)) First(f); |
|
121 |
return *this; |
|
122 |
} |
|
123 |
|
|
124 |
/// \brief Set to the default value of the \c Second type. |
|
125 |
/// |
|
126 |
/// This function sets the variant to the default value of the \c |
|
127 |
/// Second type. |
|
128 |
BiVariant& setSecond() { |
|
129 |
destroy(); |
|
130 |
flag = false; |
|
131 |
new(reinterpret_cast<Second*>(data)) Second(); |
|
132 |
return *this; |
|
133 |
} |
|
134 |
|
|
135 |
/// \brief Set to the given value of the \c Second type. |
|
136 |
/// |
|
137 |
/// This function sets the variant to the given value of the \c |
|
138 |
/// Second type. |
|
139 |
BiVariant& setSecond(const Second& s) { |
|
140 |
destroy(); |
|
141 |
flag = false; |
|
142 |
new(reinterpret_cast<Second*>(data)) Second(s); |
|
143 |
return *this; |
|
144 |
} |
|
145 |
|
|
146 |
/// \brief Operator form of the \c setFirst() |
|
147 |
BiVariant& operator=(const First& f) { |
|
148 |
return setFirst(f); |
|
149 |
} |
|
150 |
|
|
151 |
/// \brief Operator form of the \c setSecond() |
|
152 |
BiVariant& operator=(const Second& s) { |
|
153 |
return setSecond(s); |
|
154 |
} |
|
155 |
|
|
156 |
/// \brief Assign operator |
|
157 |
BiVariant& operator=(const BiVariant& bivariant) { |
|
158 |
if (this == &bivariant) return *this; |
|
159 |
destroy(); |
|
160 |
flag = bivariant.flag; |
|
161 |
if (flag) { |
|
162 |
new(reinterpret_cast<First*>(data)) First(bivariant.first()); |
|
163 |
} else { |
|
164 |
new(reinterpret_cast<Second*>(data)) Second(bivariant.second()); |
|
165 |
} |
|
166 |
return *this; |
|
167 |
} |
|
168 |
|
|
169 |
/// \brief Reference to the value |
|
170 |
/// |
|
171 |
/// Reference to the value of the \c First type. |
|
172 |
/// \pre The BiVariant should store value of \c First type. |
|
173 |
First& first() { |
|
174 |
LEMON_DEBUG(flag, "Variant wrong state"); |
|
175 |
return *reinterpret_cast<First*>(data); |
|
176 |
} |
|
177 |
|
|
178 |
/// \brief Const reference to the value |
|
179 |
/// |
|
180 |
/// Const reference to the value of the \c First type. |
|
181 |
/// \pre The BiVariant should store value of \c First type. |
|
182 |
const First& first() const { |
|
183 |
LEMON_DEBUG(flag, "Variant wrong state"); |
|
184 |
return *reinterpret_cast<const First*>(data); |
|
185 |
} |
|
186 |
|
|
187 |
/// \brief Operator form of the \c first() |
|
188 |
operator First&() { return first(); } |
|
189 |
/// \brief Operator form of the const \c first() |
|
190 |
operator const First&() const { return first(); } |
|
191 |
|
|
192 |
/// \brief Reference to the value |
|
193 |
/// |
|
194 |
/// Reference to the value of the \c Second type. |
|
195 |
/// \pre The BiVariant should store value of \c Second type. |
|
196 |
Second& second() { |
|
197 |
LEMON_DEBUG(!flag, "Variant wrong state"); |
|
198 |
return *reinterpret_cast<Second*>(data); |
|
199 |
} |
|
200 |
|
|
201 |
/// \brief Const reference to the value |
|
202 |
/// |
|
203 |
/// Const reference to the value of the \c Second type. |
|
204 |
/// \pre The BiVariant should store value of \c Second type. |
|
205 |
const Second& second() const { |
|
206 |
LEMON_DEBUG(!flag, "Variant wrong state"); |
|
207 |
return *reinterpret_cast<const Second*>(data); |
|
208 |
} |
|
209 |
|
|
210 |
/// \brief Operator form of the \c second() |
|
211 |
operator Second&() { return second(); } |
|
212 |
/// \brief Operator form of the const \c second() |
|
213 |
operator const Second&() const { return second(); } |
|
214 |
|
|
215 |
/// \brief %True when the variant is in the first state |
|
216 |
/// |
|
217 |
/// %True when the variant stores value of the \c First type. |
|
218 |
bool firstState() const { return flag; } |
|
219 |
|
|
220 |
/// \brief %True when the variant is in the second state |
|
221 |
/// |
|
222 |
/// %True when the variant stores value of the \c Second type. |
|
223 |
bool secondState() const { return !flag; } |
|
224 |
|
|
225 |
private: |
|
226 |
|
|
227 |
void destroy() { |
|
228 |
if (flag) { |
|
229 |
reinterpret_cast<First*>(data)->~First(); |
|
230 |
} else { |
|
231 |
reinterpret_cast<Second*>(data)->~Second(); |
|
232 |
} |
|
233 |
} |
|
234 |
|
|
235 |
char data[_variant_bits::CTMax<sizeof(First), sizeof(Second)>::value]; |
|
236 |
bool flag; |
|
237 |
}; |
|
238 |
|
|
239 |
namespace _variant_bits { |
|
240 |
|
|
241 |
template <int _idx, typename _TypeMap> |
|
242 |
struct Memory { |
|
243 |
|
|
244 |
typedef typename _TypeMap::template Map<_idx>::Type Current; |
|
245 |
|
|
246 |
static void destroy(int index, char* place) { |
|
247 |
if (index == _idx) { |
|
248 |
reinterpret_cast<Current*>(place)->~Current(); |
|
249 |
} else { |
|
250 |
Memory<_idx - 1, _TypeMap>::destroy(index, place); |
|
251 |
} |
|
252 |
} |
|
253 |
|
|
254 |
static void copy(int index, char* to, const char* from) { |
|
255 |
if (index == _idx) { |
|
256 |
new (reinterpret_cast<Current*>(to)) |
|
257 |
Current(reinterpret_cast<const Current*>(from)); |
|
258 |
} else { |
|
259 |
Memory<_idx - 1, _TypeMap>::copy(index, to, from); |
|
260 |
} |
|
261 |
} |
|
262 |
|
|
263 |
}; |
|
264 |
|
|
265 |
template <typename _TypeMap> |
|
266 |
struct Memory<-1, _TypeMap> { |
|
267 |
|
|
268 |
static void destroy(int, char*) { |
|
269 |
LEMON_DEBUG(false, "Variant wrong index."); |
|
270 |
} |
|
271 |
|
|
272 |
static void copy(int, char*, const char*) { |
|
273 |
LEMON_DEBUG(false, "Variant wrong index."); |
|
274 |
} |
|
275 |
}; |
|
276 |
|
|
277 |
template <int _idx, typename _TypeMap> |
|
278 |
struct Size { |
|
279 |
static const int value = |
|
280 |
CTMax<sizeof(typename _TypeMap::template Map<_idx>::Type), |
|
281 |
Size<_idx - 1, _TypeMap>::value>::value; |
|
282 |
}; |
|
283 |
|
|
284 |
template <typename _TypeMap> |
|
285 |
struct Size<0, _TypeMap> { |
|
286 |
static const int value = |
|
287 |
sizeof(typename _TypeMap::template Map<0>::Type); |
|
288 |
}; |
|
289 |
|
|
290 |
} |
|
291 |
|
|
292 |
/// \brief Variant type |
|
293 |
/// |
|
294 |
/// Simple Variant type. The Variant type is a type safe union. The |
|
295 |
/// C++ has strong limitations for using unions, for example we |
|
296 |
/// cannot store type with non default constructor or destructor in |
|
297 |
/// a union. This class always knowns the current state of the |
|
298 |
/// variant and it cares for the proper construction and |
|
299 |
/// destruction. |
|
300 |
/// |
|
301 |
/// \param _num The number of the types which can be stored in the |
|
302 |
/// variant type. |
|
303 |
/// \param _TypeMap This class describes the types of the Variant. The |
|
304 |
/// _TypeMap::Map<index>::Type should be a valid type for each index |
|
305 |
/// in the range {0, 1, ..., _num - 1}. The \c VariantTypeMap is helper |
|
306 |
/// class to define such type mappings up to 10 types. |
|
307 |
/// |
|
308 |
/// And the usage of the class: |
|
309 |
///\code |
|
310 |
/// typedef Variant<3, VariantTypeMap<int, std::string, double> > MyVariant; |
|
311 |
/// MyVariant var; |
|
312 |
/// var.set<0>(12); |
|
313 |
/// std::cout << var.get<0>() << std::endl; |
|
314 |
/// var.set<1>("alpha"); |
|
315 |
/// std::cout << var.get<1>() << std::endl; |
|
316 |
/// var.set<2>(0.75); |
|
317 |
/// std::cout << var.get<2>() << std::endl; |
|
318 |
///\endcode |
|
319 |
/// |
|
320 |
/// The result of course: |
|
321 |
///\code |
|
322 |
/// 12 |
|
323 |
/// alpha |
|
324 |
/// 0.75 |
|
325 |
///\endcode |
|
326 |
template <int _num, typename _TypeMap> |
|
327 |
class Variant { |
|
328 |
public: |
|
329 |
|
|
330 |
static const int num = _num; |
|
331 |
|
|
332 |
typedef _TypeMap TypeMap; |
|
333 |
|
|
334 |
/// \brief Constructor |
|
335 |
/// |
|
336 |
/// This constructor initalizes to the default value of the \c type |
|
337 |
/// with 0 index. |
|
338 |
Variant() { |
|
339 |
flag = 0; |
|
340 |
new(reinterpret_cast<typename TypeMap::template Map<0>::Type*>(data)) |
|
341 |
typename TypeMap::template Map<0>::Type(); |
|
342 |
} |
|
343 |
|
|
344 |
|
|
345 |
/// \brief Copy constructor |
|
346 |
/// |
|
347 |
/// Copy constructor |
|
348 |
Variant(const Variant& variant) { |
|
349 |
flag = variant.flag; |
|
350 |
_variant_bits::Memory<num - 1, TypeMap>::copy(flag, data, variant.data); |
|
351 |
} |
|
352 |
|
|
353 |
/// \brief Assign operator |
|
354 |
/// |
|
355 |
/// Assign operator |
|
356 |
Variant& operator=(const Variant& variant) { |
|
357 |
if (this == &variant) return *this; |
|
358 |
_variant_bits::Memory<num - 1, TypeMap>:: |
|
359 |
destroy(flag, data); |
|
360 |
flag = variant.flag; |
|
361 |
_variant_bits::Memory<num - 1, TypeMap>:: |
|
362 |
copy(flag, data, variant.data); |
|
363 |
return *this; |
|
364 |
} |
|
365 |
|
|
366 |
/// \brief Destrcutor |
|
367 |
/// |
|
368 |
/// Destructor |
|
369 |
~Variant() { |
|
370 |
_variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data); |
|
371 |
} |
|
372 |
|
|
373 |
/// \brief Set to the default value of the type with \c _idx index. |
|
374 |
/// |
|
375 |
/// This function sets the variant to the default value of the |
|
376 |
/// type with \c _idx index. |
|
377 |
template <int _idx> |
|
378 |
Variant& set() { |
|
379 |
_variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data); |
|
380 |
flag = _idx; |
|
381 |
new(reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>(data)) |
|
382 |
typename TypeMap::template Map<_idx>::Type(); |
|
383 |
return *this; |
|
384 |
} |
|
385 |
|
|
386 |
/// \brief Set to the given value of the type with \c _idx index. |
|
387 |
/// |
|
388 |
/// This function sets the variant to the given value of the type |
|
389 |
/// with \c _idx index. |
|
390 |
template <int _idx> |
|
391 |
Variant& set(const typename _TypeMap::template Map<_idx>::Type& init) { |
|
392 |
_variant_bits::Memory<num - 1, TypeMap>::destroy(flag, data); |
|
393 |
flag = _idx; |
|
394 |
new(reinterpret_cast<typename TypeMap::template Map<_idx>::Type*>(data)) |
|
395 |
typename TypeMap::template Map<_idx>::Type(init); |
|
396 |
return *this; |
|
397 |
} |
|
398 |
|
|
399 |
/// \brief Gets the current value of the type with \c _idx index. |
|
400 |
/// |
|
401 |
/// Gets the current value of the type with \c _idx index. |
|
402 |
template <int _idx> |
|
403 |
const typename TypeMap::template Map<_idx>::Type& get() const { |
|
404 |
LEMON_DEBUG(_idx == flag, "Variant wrong index"); |
|
405 |
return *reinterpret_cast<const typename TypeMap:: |
|
406 |
template Map<_idx>::Type*>(data); |
|
407 |
} |
|
408 |
|
|
409 |
/// \brief Gets the current value of the type with \c _idx index. |
|
410 |
/// |
|
411 |
/// Gets the current value of the type with \c _idx index. |
|
412 |
template <int _idx> |
|
413 |
typename _TypeMap::template Map<_idx>::Type& get() { |
|
414 |
LEMON_DEBUG(_idx == flag, "Variant wrong index"); |
|
415 |
return *reinterpret_cast<typename TypeMap::template Map<_idx>::Type*> |
|
416 |
(data); |
|
417 |
} |
|
418 |
|
|
419 |
/// \brief Returns the current state of the variant. |
|
420 |
/// |
|
421 |
/// Returns the current state of the variant. |
|
422 |
int state() const { |
|
423 |
return flag; |
|
424 |
} |
|
425 |
|
|
426 |
private: |
|
427 |
|
|
428 |
char data[_variant_bits::Size<num - 1, TypeMap>::value]; |
|
429 |
int flag; |
|
430 |
}; |
|
431 |
|
|
432 |
namespace _variant_bits { |
|
433 |
|
|
434 |
template <int _index, typename _List> |
|
435 |
struct Get { |
|
436 |
typedef typename Get<_index - 1, typename _List::Next>::Type Type; |
|
437 |
}; |
|
438 |
|
|
439 |
template <typename _List> |
|
440 |
struct Get<0, _List> { |
|
441 |
typedef typename _List::Type Type; |
|
442 |
}; |
|
443 |
|
|
444 |
struct List {}; |
|
445 |
|
|
446 |
template <typename _Type, typename _List> |
|
447 |
struct Insert { |
|
448 |
typedef _List Next; |
|
449 |
typedef _Type Type; |
|
450 |
}; |
|
451 |
|
|
452 |
template <int _idx, typename _T0, typename _T1, typename _T2, |
|
453 |
typename _T3, typename _T5, typename _T4, typename _T6, |
|
454 |
typename _T7, typename _T8, typename _T9> |
|
455 |
struct Mapper { |
|
456 |
typedef List L10; |
|
457 |
typedef Insert<_T9, L10> L9; |
|
458 |
typedef Insert<_T8, L9> L8; |
|
459 |
typedef Insert<_T7, L8> L7; |
|
460 |
typedef Insert<_T6, L7> L6; |
|
461 |
typedef Insert<_T5, L6> L5; |
|
462 |
typedef Insert<_T4, L5> L4; |
|
463 |
typedef Insert<_T3, L4> L3; |
|
464 |
typedef Insert<_T2, L3> L2; |
|
465 |
typedef Insert<_T1, L2> L1; |
|
466 |
typedef Insert<_T0, L1> L0; |
|
467 |
typedef typename Get<_idx, L0>::Type Type; |
|
468 |
}; |
|
469 |
|
|
470 |
} |
|
471 |
|
|
472 |
/// \brief Helper class for Variant |
|
473 |
/// |
|
474 |
/// Helper class to define type mappings for Variant. This class |
|
475 |
/// converts the template parameters to be mappable by integer. |
|
476 |
/// \see Variant |
|
477 |
template < |
|
478 |
typename _T0, |
|
479 |
typename _T1 = void, typename _T2 = void, typename _T3 = void, |
|
480 |
typename _T5 = void, typename _T4 = void, typename _T6 = void, |
|
481 |
typename _T7 = void, typename _T8 = void, typename _T9 = void> |
|
482 |
struct VariantTypeMap { |
|
483 |
template <int _idx> |
|
484 |
struct Map { |
|
485 |
typedef typename _variant_bits:: |
|
486 |
Mapper<_idx, _T0, _T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9>::Type |
|
487 |
Type; |
|
488 |
}; |
|
489 |
}; |
|
490 |
|
|
491 |
} |
|
492 |
|
|
493 |
|
|
494 |
#endif |
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_TOPOLOGY_H |
|
20 |
#define LEMON_TOPOLOGY_H |
|
21 |
|
|
22 |
#include <lemon/dfs.h> |
|
23 |
#include <lemon/bfs.h> |
|
24 |
#include <lemon/core.h> |
|
25 |
#include <lemon/maps.h> |
|
26 |
#include <lemon/adaptors.h> |
|
27 |
|
|
28 |
#include <lemon/concepts/digraph.h> |
|
29 |
#include <lemon/concepts/graph.h> |
|
30 |
#include <lemon/concept_check.h> |
|
31 |
|
|
32 |
#include <stack> |
|
33 |
#include <functional> |
|
34 |
|
|
35 |
/// \ingroup connectivity |
|
36 |
/// \file |
|
37 |
/// \brief Connectivity algorithms |
|
38 |
/// |
|
39 |
/// Connectivity algorithms |
|
40 |
|
|
41 |
namespace lemon { |
|
42 |
|
|
43 |
/// \ingroup connectivity |
|
44 |
/// |
|
45 |
/// \brief Check whether the given undirected graph is connected. |
|
46 |
/// |
|
47 |
/// Check whether the given undirected graph is connected. |
|
48 |
/// \param graph The undirected graph. |
|
49 |
/// \return %True when there is path between any two nodes in the graph. |
|
50 |
/// \note By definition, the empty graph is connected. |
|
51 |
template <typename Graph> |
|
52 |
bool connected(const Graph& graph) { |
|
53 |
checkConcept<concepts::Graph, Graph>(); |
|
54 |
typedef typename Graph::NodeIt NodeIt; |
|
55 |
if (NodeIt(graph) == INVALID) return true; |
|
56 |
Dfs<Graph> dfs(graph); |
|
57 |
dfs.run(NodeIt(graph)); |
|
58 |
for (NodeIt it(graph); it != INVALID; ++it) { |
|
59 |
if (!dfs.reached(it)) { |
|
60 |
return false; |
|
61 |
} |
|
62 |
} |
|
63 |
return true; |
|
64 |
} |
|
65 |
|
|
66 |
/// \ingroup connectivity |
|
67 |
/// |
|
68 |
/// \brief Count the number of connected components of an undirected graph |
|
69 |
/// |
|
70 |
/// Count the number of connected components of an undirected graph |
|
71 |
/// |
|
72 |
/// \param graph The graph. It must be undirected. |
|
73 |
/// \return The number of components |
|
74 |
/// \note By definition, the empty graph consists |
|
75 |
/// of zero connected components. |
|
76 |
template <typename Graph> |
|
77 |
int countConnectedComponents(const Graph &graph) { |
|
78 |
checkConcept<concepts::Graph, Graph>(); |
|
79 |
typedef typename Graph::Node Node; |
|
80 |
typedef typename Graph::Arc Arc; |
|
81 |
|
|
82 |
typedef NullMap<Node, Arc> PredMap; |
|
83 |
typedef NullMap<Node, int> DistMap; |
|
84 |
|
|
85 |
int compNum = 0; |
|
86 |
typename Bfs<Graph>:: |
|
87 |
template SetPredMap<PredMap>:: |
|
88 |
template SetDistMap<DistMap>:: |
|
89 |
Create bfs(graph); |
|
90 |
|
|
91 |
PredMap predMap; |
|
92 |
bfs.predMap(predMap); |
|
93 |
|
|
94 |
DistMap distMap; |
|
95 |
bfs.distMap(distMap); |
|
96 |
|
|
97 |
bfs.init(); |
|
98 |
for(typename Graph::NodeIt n(graph); n != INVALID; ++n) { |
|
99 |
if (!bfs.reached(n)) { |
|
100 |
bfs.addSource(n); |
|
101 |
bfs.start(); |
|
102 |
++compNum; |
|
103 |
} |
|
104 |
} |
|
105 |
return compNum; |
|
106 |
} |
|
107 |
|
|
108 |
/// \ingroup connectivity |
|
109 |
/// |
|
110 |
/// \brief Find the connected components of an undirected graph |
|
111 |
/// |
|
112 |
/// Find the connected components of an undirected graph. |
|
113 |
/// |
|
114 |
/// \param graph The graph. It must be undirected. |
|
115 |
/// \retval compMap A writable node map. The values will be set from 0 to |
|
116 |
/// the number of the connected components minus one. Each values of the map |
|
117 |
/// will be set exactly once, the values of a certain component will be |
|
118 |
/// set continuously. |
|
119 |
/// \return The number of components |
|
120 |
/// |
|
121 |
template <class Graph, class NodeMap> |
|
122 |
int connectedComponents(const Graph &graph, NodeMap &compMap) { |
|
123 |
checkConcept<concepts::Graph, Graph>(); |
|
124 |
typedef typename Graph::Node Node; |
|
125 |
typedef typename Graph::Arc Arc; |
|
126 |
checkConcept<concepts::WriteMap<Node, int>, NodeMap>(); |
|
127 |
|
|
128 |
typedef NullMap<Node, Arc> PredMap; |
|
129 |
typedef NullMap<Node, int> DistMap; |
|
130 |
|
|
131 |
int compNum = 0; |
|
132 |
typename Bfs<Graph>:: |
|
133 |
template SetPredMap<PredMap>:: |
|
134 |
template SetDistMap<DistMap>:: |
|
135 |
Create bfs(graph); |
|
136 |
|
|
137 |
PredMap predMap; |
|
138 |
bfs.predMap(predMap); |
|
139 |
|
|
140 |
DistMap distMap; |
|
141 |
bfs.distMap(distMap); |
|
142 |
|
|
143 |
bfs.init(); |
|
144 |
for(typename Graph::NodeIt n(graph); n != INVALID; ++n) { |
|
145 |
if(!bfs.reached(n)) { |
|
146 |
bfs.addSource(n); |
|
147 |
while (!bfs.emptyQueue()) { |
|
148 |
compMap.set(bfs.nextNode(), compNum); |
|
149 |
bfs.processNextNode(); |
|
150 |
} |
|
151 |
++compNum; |
|
152 |
} |
|
153 |
} |
|
154 |
return compNum; |
|
155 |
} |
|
156 |
|
|
157 |
namespace _topology_bits { |
|
158 |
|
|
159 |
template <typename Digraph, typename Iterator > |
|
160 |
struct LeaveOrderVisitor : public DfsVisitor<Digraph> { |
|
161 |
public: |
|
162 |
typedef typename Digraph::Node Node; |
|
163 |
LeaveOrderVisitor(Iterator it) : _it(it) {} |
|
164 |
|
|
165 |
void leave(const Node& node) { |
|
166 |
*(_it++) = node; |
|
167 |
} |
|
168 |
|
|
169 |
private: |
|
170 |
Iterator _it; |
|
171 |
}; |
|
172 |
|
|
173 |
template <typename Digraph, typename Map> |
|
174 |
struct FillMapVisitor : public DfsVisitor<Digraph> { |
|
175 |
public: |
|
176 |
typedef typename Digraph::Node Node; |
|
177 |
typedef typename Map::Value Value; |
|
178 |
|
|
179 |
FillMapVisitor(Map& map, Value& value) |
|
180 |
: _map(map), _value(value) {} |
|
181 |
|
|
182 |
void reach(const Node& node) { |
|
183 |
_map.set(node, _value); |
|
184 |
} |
|
185 |
private: |
|
186 |
Map& _map; |
|
187 |
Value& _value; |
|
188 |
}; |
|
189 |
|
|
190 |
template <typename Digraph, typename ArcMap> |
|
191 |
struct StronglyConnectedCutEdgesVisitor : public DfsVisitor<Digraph> { |
|
192 |
public: |
|
193 |
typedef typename Digraph::Node Node; |
|
194 |
typedef typename Digraph::Arc Arc; |
|
195 |
|
|
196 |
StronglyConnectedCutEdgesVisitor(const Digraph& digraph, |
|
197 |
ArcMap& cutMap, |
|
198 |
int& cutNum) |
|
199 |
: _digraph(digraph), _cutMap(cutMap), _cutNum(cutNum), |
|
200 |
_compMap(digraph), _num(0) { |
|
201 |
} |
|
202 |
|
|
203 |
void stop(const Node&) { |
|
204 |
++_num; |
|
205 |
} |
|
206 |
|
|
207 |
void reach(const Node& node) { |
|
208 |
_compMap.set(node, _num); |
|
209 |
} |
|
210 |
|
|
211 |
void examine(const Arc& arc) { |
|
212 |
if (_compMap[_digraph.source(arc)] != |
|
213 |
_compMap[_digraph.target(arc)]) { |
|
214 |
_cutMap.set(arc, true); |
|
215 |
++_cutNum; |
|
216 |
} |
|
217 |
} |
|
218 |
private: |
|
219 |
const Digraph& _digraph; |
|
220 |
ArcMap& _cutMap; |
|
221 |
int& _cutNum; |
|
222 |
|
|
223 |
typename Digraph::template NodeMap<int> _compMap; |
|
224 |
int _num; |
|
225 |
}; |
|
226 |
|
|
227 |
} |
|
228 |
|
|
229 |
|
|
230 |
/// \ingroup connectivity |
|
231 |
/// |
|
232 |
/// \brief Check whether the given directed graph is strongly connected. |
|
233 |
/// |
|
234 |
/// Check whether the given directed graph is strongly connected. The |
|
235 |
/// graph is strongly connected when any two nodes of the graph are |
|
236 |
/// connected with directed paths in both direction. |
|
237 |
/// \return %False when the graph is not strongly connected. |
|
238 |
/// \see connected |
|
239 |
/// |
|
240 |
/// \note By definition, the empty graph is strongly connected. |
|
241 |
template <typename Digraph> |
|
242 |
bool stronglyConnected(const Digraph& digraph) { |
|
243 |
checkConcept<concepts::Digraph, Digraph>(); |
|
244 |
|
|
245 |
typedef typename Digraph::Node Node; |
|
246 |
typedef typename Digraph::NodeIt NodeIt; |
|
247 |
|
|
248 |
typename Digraph::Node source = NodeIt(digraph); |
|
249 |
if (source == INVALID) return true; |
|
250 |
|
|
251 |
using namespace _topology_bits; |
|
252 |
|
|
253 |
typedef DfsVisitor<Digraph> Visitor; |
|
254 |
Visitor visitor; |
|
255 |
|
|
256 |
DfsVisit<Digraph, Visitor> dfs(digraph, visitor); |
|
257 |
dfs.init(); |
|
258 |
dfs.addSource(source); |
|
259 |
dfs.start(); |
|
260 |
|
|
261 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
|
262 |
if (!dfs.reached(it)) { |
|
263 |
return false; |
|
264 |
} |
|
265 |
} |
|
266 |
|
|
267 |
typedef ReverseDigraph<const Digraph> RDigraph; |
|
268 |
RDigraph rdigraph(digraph); |
|
269 |
|
|
270 |
typedef DfsVisitor<Digraph> RVisitor; |
|
271 |
RVisitor rvisitor; |
|
272 |
|
|
273 |
DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor); |
|
274 |
rdfs.init(); |
|
275 |
rdfs.addSource(source); |
|
276 |
rdfs.start(); |
|
277 |
|
|
278 |
for (NodeIt it(rdigraph); it != INVALID; ++it) { |
|
279 |
if (!rdfs.reached(it)) { |
|
280 |
return false; |
|
281 |
} |
|
282 |
} |
|
283 |
|
|
284 |
return true; |
|
285 |
} |
|
286 |
|
|
287 |
/// \ingroup connectivity |
|
288 |
/// |
|
289 |
/// \brief Count the strongly connected components of a directed graph |
|
290 |
/// |
|
291 |
/// Count the strongly connected components of a directed graph. |
|
292 |
/// The strongly connected components are the classes of an |
|
293 |
/// equivalence relation on the nodes of the graph. Two nodes are in |
|
294 |
/// the same class if they are connected with directed paths in both |
|
295 |
/// direction. |
|
296 |
/// |
|
297 |
/// \param graph The graph. |
|
298 |
/// \return The number of components |
|
299 |
/// \note By definition, the empty graph has zero |
|
300 |
/// strongly connected components. |
|
301 |
template <typename Digraph> |
|
302 |
int countStronglyConnectedComponents(const Digraph& digraph) { |
|
303 |
checkConcept<concepts::Digraph, Digraph>(); |
|
304 |
|
|
305 |
using namespace _topology_bits; |
|
306 |
|
|
307 |
typedef typename Digraph::Node Node; |
|
308 |
typedef typename Digraph::Arc Arc; |
|
309 |
typedef typename Digraph::NodeIt NodeIt; |
|
310 |
typedef typename Digraph::ArcIt ArcIt; |
|
311 |
|
|
312 |
typedef std::vector<Node> Container; |
|
313 |
typedef typename Container::iterator Iterator; |
|
314 |
|
|
315 |
Container nodes(countNodes(digraph)); |
|
316 |
typedef LeaveOrderVisitor<Digraph, Iterator> Visitor; |
|
317 |
Visitor visitor(nodes.begin()); |
|
318 |
|
|
319 |
DfsVisit<Digraph, Visitor> dfs(digraph, visitor); |
|
320 |
dfs.init(); |
|
321 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
|
322 |
if (!dfs.reached(it)) { |
|
323 |
dfs.addSource(it); |
|
324 |
dfs.start(); |
|
325 |
} |
|
326 |
} |
|
327 |
|
|
328 |
typedef typename Container::reverse_iterator RIterator; |
|
329 |
typedef ReverseDigraph<const Digraph> RDigraph; |
|
330 |
|
|
331 |
RDigraph rdigraph(digraph); |
|
332 |
|
|
333 |
typedef DfsVisitor<Digraph> RVisitor; |
|
334 |
RVisitor rvisitor; |
|
335 |
|
|
336 |
DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor); |
|
337 |
|
|
338 |
int compNum = 0; |
|
339 |
|
|
340 |
rdfs.init(); |
|
341 |
for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) { |
|
342 |
if (!rdfs.reached(*it)) { |
|
343 |
rdfs.addSource(*it); |
|
344 |
rdfs.start(); |
|
345 |
++compNum; |
|
346 |
} |
|
347 |
} |
|
348 |
return compNum; |
|
349 |
} |
|
350 |
|
|
351 |
/// \ingroup connectivity |
|
352 |
/// |
|
353 |
/// \brief Find the strongly connected components of a directed graph |
|
354 |
/// |
|
355 |
/// Find the strongly connected components of a directed graph. The |
|
356 |
/// strongly connected components are the classes of an equivalence |
|
357 |
/// relation on the nodes of the graph. Two nodes are in |
|
358 |
/// relationship when there are directed paths between them in both |
|
359 |
/// direction. In addition, the numbering of components will satisfy |
|
360 |
/// that there is no arc going from a higher numbered component to |
|
361 |
/// a lower. |
|
362 |
/// |
|
363 |
/// \param digraph The digraph. |
|
364 |
/// \retval compMap A writable node map. The values will be set from 0 to |
|
365 |
/// the number of the strongly connected components minus one. Each value |
|
366 |
/// of the map will be set exactly once, the values of a certain component |
|
367 |
/// will be set continuously. |
|
368 |
/// \return The number of components |
|
369 |
/// |
|
370 |
template <typename Digraph, typename NodeMap> |
|
371 |
int stronglyConnectedComponents(const Digraph& digraph, NodeMap& compMap) { |
|
372 |
checkConcept<concepts::Digraph, Digraph>(); |
|
373 |
typedef typename Digraph::Node Node; |
|
374 |
typedef typename Digraph::NodeIt NodeIt; |
|
375 |
checkConcept<concepts::WriteMap<Node, int>, NodeMap>(); |
|
376 |
|
|
377 |
using namespace _topology_bits; |
|
378 |
|
|
379 |
typedef std::vector<Node> Container; |
|
380 |
typedef typename Container::iterator Iterator; |
|
381 |
|
|
382 |
Container nodes(countNodes(digraph)); |
|
383 |
typedef LeaveOrderVisitor<Digraph, Iterator> Visitor; |
|
384 |
Visitor visitor(nodes.begin()); |
|
385 |
|
|
386 |
DfsVisit<Digraph, Visitor> dfs(digraph, visitor); |
|
387 |
dfs.init(); |
|
388 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
|
389 |
if (!dfs.reached(it)) { |
|
390 |
dfs.addSource(it); |
|
391 |
dfs.start(); |
|
392 |
} |
|
393 |
} |
|
394 |
|
|
395 |
typedef typename Container::reverse_iterator RIterator; |
|
396 |
typedef ReverseDigraph<const Digraph> RDigraph; |
|
397 |
|
|
398 |
RDigraph rdigraph(digraph); |
|
399 |
|
|
400 |
int compNum = 0; |
|
401 |
|
|
402 |
typedef FillMapVisitor<RDigraph, NodeMap> RVisitor; |
|
403 |
RVisitor rvisitor(compMap, compNum); |
|
404 |
|
|
405 |
DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor); |
|
406 |
|
|
407 |
rdfs.init(); |
|
408 |
for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) { |
|
409 |
if (!rdfs.reached(*it)) { |
|
410 |
rdfs.addSource(*it); |
|
411 |
rdfs.start(); |
|
412 |
++compNum; |
|
413 |
} |
|
414 |
} |
|
415 |
return compNum; |
|
416 |
} |
|
417 |
|
|
418 |
/// \ingroup connectivity |
|
419 |
/// |
|
420 |
/// \brief Find the cut arcs of the strongly connected components. |
|
421 |
/// |
|
422 |
/// Find the cut arcs of the strongly connected components. |
|
423 |
/// The strongly connected components are the classes of an equivalence |
|
424 |
/// relation on the nodes of the graph. Two nodes are in relationship |
|
425 |
/// when there are directed paths between them in both direction. |
|
426 |
/// The strongly connected components are separated by the cut arcs. |
|
427 |
/// |
|
428 |
/// \param graph The graph. |
|
429 |
/// \retval cutMap A writable node map. The values will be set true when the |
|
430 |
/// arc is a cut arc. |
|
431 |
/// |
|
432 |
/// \return The number of cut arcs |
|
433 |
template <typename Digraph, typename ArcMap> |
|
434 |
int stronglyConnectedCutArcs(const Digraph& graph, ArcMap& cutMap) { |
|
435 |
checkConcept<concepts::Digraph, Digraph>(); |
|
436 |
typedef typename Digraph::Node Node; |
|
437 |
typedef typename Digraph::Arc Arc; |
|
438 |
typedef typename Digraph::NodeIt NodeIt; |
|
439 |
checkConcept<concepts::WriteMap<Arc, bool>, ArcMap>(); |
|
440 |
|
|
441 |
using namespace _topology_bits; |
|
442 |
|
|
443 |
typedef std::vector<Node> Container; |
|
444 |
typedef typename Container::iterator Iterator; |
|
445 |
|
|
446 |
Container nodes(countNodes(graph)); |
|
447 |
typedef LeaveOrderVisitor<Digraph, Iterator> Visitor; |
|
448 |
Visitor visitor(nodes.begin()); |
|
449 |
|
|
450 |
DfsVisit<Digraph, Visitor> dfs(graph, visitor); |
|
451 |
dfs.init(); |
|
452 |
for (NodeIt it(graph); it != INVALID; ++it) { |
|
453 |
if (!dfs.reached(it)) { |
|
454 |
dfs.addSource(it); |
|
455 |
dfs.start(); |
|
456 |
} |
|
457 |
} |
|
458 |
|
|
459 |
typedef typename Container::reverse_iterator RIterator; |
|
460 |
typedef ReverseDigraph<const Digraph> RDigraph; |
|
461 |
|
|
462 |
RDigraph rgraph(graph); |
|
463 |
|
|
464 |
int cutNum = 0; |
|
465 |
|
|
466 |
typedef StronglyConnectedCutEdgesVisitor<RDigraph, ArcMap> RVisitor; |
|
467 |
RVisitor rvisitor(rgraph, cutMap, cutNum); |
|
468 |
|
|
469 |
DfsVisit<RDigraph, RVisitor> rdfs(rgraph, rvisitor); |
|
470 |
|
|
471 |
rdfs.init(); |
|
472 |
for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) { |
|
473 |
if (!rdfs.reached(*it)) { |
|
474 |
rdfs.addSource(*it); |
|
475 |
rdfs.start(); |
|
476 |
} |
|
477 |
} |
|
478 |
return cutNum; |
|
479 |
} |
|
480 |
|
|
481 |
namespace _topology_bits { |
|
482 |
|
|
483 |
template <typename Digraph> |
|
484 |
class CountBiNodeConnectedComponentsVisitor : public DfsVisitor<Digraph> { |
|
485 |
public: |
|
486 |
typedef typename Digraph::Node Node; |
|
487 |
typedef typename Digraph::Arc Arc; |
|
488 |
typedef typename Digraph::Edge Edge; |
|
489 |
|
|
490 |
CountBiNodeConnectedComponentsVisitor(const Digraph& graph, int &compNum) |
|
491 |
: _graph(graph), _compNum(compNum), |
|
492 |
_numMap(graph), _retMap(graph), _predMap(graph), _num(0) {} |
|
493 |
|
|
494 |
void start(const Node& node) { |
|
495 |
_predMap.set(node, INVALID); |
|
496 |
} |
|
497 |
|
|
498 |
void reach(const Node& node) { |
|
499 |
_numMap.set(node, _num); |
|
500 |
_retMap.set(node, _num); |
|
501 |
++_num; |
|
502 |
} |
|
503 |
|
|
504 |
void discover(const Arc& edge) { |
|
505 |
_predMap.set(_graph.target(edge), _graph.source(edge)); |
|
506 |
} |
|
507 |
|
|
508 |
void examine(const Arc& edge) { |
|
509 |
if (_graph.source(edge) == _graph.target(edge) && |
|
510 |
_graph.direction(edge)) { |
|
511 |
++_compNum; |
|
512 |
return; |
|
513 |
} |
|
514 |
if (_predMap[_graph.source(edge)] == _graph.target(edge)) { |
|
515 |
return; |
|
516 |
} |
|
517 |
if (_retMap[_graph.source(edge)] > _numMap[_graph.target(edge)]) { |
|
518 |
_retMap.set(_graph.source(edge), _numMap[_graph.target(edge)]); |
|
519 |
} |
|
520 |
} |
|
521 |
|
|
522 |
void backtrack(const Arc& edge) { |
|
523 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
|
524 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
|
525 |
} |
|
526 |
if (_numMap[_graph.source(edge)] <= _retMap[_graph.target(edge)]) { |
|
527 |
++_compNum; |
|
528 |
} |
|
529 |
} |
|
530 |
|
|
531 |
private: |
|
532 |
const Digraph& _graph; |
|
533 |
int& _compNum; |
|
534 |
|
|
535 |
typename Digraph::template NodeMap<int> _numMap; |
|
536 |
typename Digraph::template NodeMap<int> _retMap; |
|
537 |
typename Digraph::template NodeMap<Node> _predMap; |
|
538 |
int _num; |
|
539 |
}; |
|
540 |
|
|
541 |
template <typename Digraph, typename ArcMap> |
|
542 |
class BiNodeConnectedComponentsVisitor : public DfsVisitor<Digraph> { |
|
543 |
public: |
|
544 |
typedef typename Digraph::Node Node; |
|
545 |
typedef typename Digraph::Arc Arc; |
|
546 |
typedef typename Digraph::Edge Edge; |
|
547 |
|
|
548 |
BiNodeConnectedComponentsVisitor(const Digraph& graph, |
|
549 |
ArcMap& compMap, int &compNum) |
|
550 |
: _graph(graph), _compMap(compMap), _compNum(compNum), |
|
551 |
_numMap(graph), _retMap(graph), _predMap(graph), _num(0) {} |
|
552 |
|
|
553 |
void start(const Node& node) { |
|
554 |
_predMap.set(node, INVALID); |
|
555 |
} |
|
556 |
|
|
557 |
void reach(const Node& node) { |
|
558 |
_numMap.set(node, _num); |
|
559 |
_retMap.set(node, _num); |
|
560 |
++_num; |
|
561 |
} |
|
562 |
|
|
563 |
void discover(const Arc& edge) { |
|
564 |
Node target = _graph.target(edge); |
|
565 |
_predMap.set(target, edge); |
|
566 |
_edgeStack.push(edge); |
|
567 |
} |
|
568 |
|
|
569 |
void examine(const Arc& edge) { |
|
570 |
Node source = _graph.source(edge); |
|
571 |
Node target = _graph.target(edge); |
|
572 |
if (source == target && _graph.direction(edge)) { |
|
573 |
_compMap.set(edge, _compNum); |
|
574 |
++_compNum; |
|
575 |
return; |
|
576 |
} |
|
577 |
if (_numMap[target] < _numMap[source]) { |
|
578 |
if (_predMap[source] != _graph.oppositeArc(edge)) { |
|
579 |
_edgeStack.push(edge); |
|
580 |
} |
|
581 |
} |
|
582 |
if (_predMap[source] != INVALID && |
|
583 |
target == _graph.source(_predMap[source])) { |
|
584 |
return; |
|
585 |
} |
|
586 |
if (_retMap[source] > _numMap[target]) { |
|
587 |
_retMap.set(source, _numMap[target]); |
|
588 |
} |
|
589 |
} |
|
590 |
|
|
591 |
void backtrack(const Arc& edge) { |
|
592 |
Node source = _graph.source(edge); |
|
593 |
Node target = _graph.target(edge); |
|
594 |
if (_retMap[source] > _retMap[target]) { |
|
595 |
_retMap.set(source, _retMap[target]); |
|
596 |
} |
|
597 |
if (_numMap[source] <= _retMap[target]) { |
|
598 |
while (_edgeStack.top() != edge) { |
|
599 |
_compMap.set(_edgeStack.top(), _compNum); |
|
600 |
_edgeStack.pop(); |
|
601 |
} |
|
602 |
_compMap.set(edge, _compNum); |
|
603 |
_edgeStack.pop(); |
|
604 |
++_compNum; |
|
605 |
} |
|
606 |
} |
|
607 |
|
|
608 |
private: |
|
609 |
const Digraph& _graph; |
|
610 |
ArcMap& _compMap; |
|
611 |
int& _compNum; |
|
612 |
|
|
613 |
typename Digraph::template NodeMap<int> _numMap; |
|
614 |
typename Digraph::template NodeMap<int> _retMap; |
|
615 |
typename Digraph::template NodeMap<Arc> _predMap; |
|
616 |
std::stack<Edge> _edgeStack; |
|
617 |
int _num; |
|
618 |
}; |
|
619 |
|
|
620 |
|
|
621 |
template <typename Digraph, typename NodeMap> |
|
622 |
class BiNodeConnectedCutNodesVisitor : public DfsVisitor<Digraph> { |
|
623 |
public: |
|
624 |
typedef typename Digraph::Node Node; |
|
625 |
typedef typename Digraph::Arc Arc; |
|
626 |
typedef typename Digraph::Edge Edge; |
|
627 |
|
|
628 |
BiNodeConnectedCutNodesVisitor(const Digraph& graph, NodeMap& cutMap, |
|
629 |
int& cutNum) |
|
630 |
: _graph(graph), _cutMap(cutMap), _cutNum(cutNum), |
|
631 |
_numMap(graph), _retMap(graph), _predMap(graph), _num(0) {} |
|
632 |
|
|
633 |
void start(const Node& node) { |
|
634 |
_predMap.set(node, INVALID); |
|
635 |
rootCut = false; |
|
636 |
} |
|
637 |
|
|
638 |
void reach(const Node& node) { |
|
639 |
_numMap.set(node, _num); |
|
640 |
_retMap.set(node, _num); |
|
641 |
++_num; |
|
642 |
} |
|
643 |
|
|
644 |
void discover(const Arc& edge) { |
|
645 |
_predMap.set(_graph.target(edge), _graph.source(edge)); |
|
646 |
} |
|
647 |
|
|
648 |
void examine(const Arc& edge) { |
|
649 |
if (_graph.source(edge) == _graph.target(edge) && |
|
650 |
_graph.direction(edge)) { |
|
651 |
if (!_cutMap[_graph.source(edge)]) { |
|
652 |
_cutMap.set(_graph.source(edge), true); |
|
653 |
++_cutNum; |
|
654 |
} |
|
655 |
return; |
|
656 |
} |
|
657 |
if (_predMap[_graph.source(edge)] == _graph.target(edge)) return; |
|
658 |
if (_retMap[_graph.source(edge)] > _numMap[_graph.target(edge)]) { |
|
659 |
_retMap.set(_graph.source(edge), _numMap[_graph.target(edge)]); |
|
660 |
} |
|
661 |
} |
|
662 |
|
|
663 |
void backtrack(const Arc& edge) { |
|
664 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
|
665 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
|
666 |
} |
|
667 |
if (_numMap[_graph.source(edge)] <= _retMap[_graph.target(edge)]) { |
|
668 |
if (_predMap[_graph.source(edge)] != INVALID) { |
|
669 |
if (!_cutMap[_graph.source(edge)]) { |
|
670 |
_cutMap.set(_graph.source(edge), true); |
|
671 |
++_cutNum; |
|
672 |
} |
|
673 |
} else if (rootCut) { |
|
674 |
if (!_cutMap[_graph.source(edge)]) { |
|
675 |
_cutMap.set(_graph.source(edge), true); |
|
676 |
++_cutNum; |
|
677 |
} |
|
678 |
} else { |
|
679 |
rootCut = true; |
|
680 |
} |
|
681 |
} |
|
682 |
} |
|
683 |
|
|
684 |
private: |
|
685 |
const Digraph& _graph; |
|
686 |
NodeMap& _cutMap; |
|
687 |
int& _cutNum; |
|
688 |
|
|
689 |
typename Digraph::template NodeMap<int> _numMap; |
|
690 |
typename Digraph::template NodeMap<int> _retMap; |
|
691 |
typename Digraph::template NodeMap<Node> _predMap; |
|
692 |
std::stack<Edge> _edgeStack; |
|
693 |
int _num; |
|
694 |
bool rootCut; |
|
695 |
}; |
|
696 |
|
|
697 |
} |
|
698 |
|
|
699 |
template <typename Graph> |
|
700 |
int countBiNodeConnectedComponents(const Graph& graph); |
|
701 |
|
|
702 |
/// \ingroup connectivity |
|
703 |
/// |
|
704 |
/// \brief Checks the graph is bi-node-connected. |
|
705 |
/// |
|
706 |
/// This function checks that the undirected graph is bi-node-connected |
|
707 |
/// graph. The graph is bi-node-connected if any two undirected edge is |
|
708 |
/// on same circle. |
|
709 |
/// |
|
710 |
/// \param graph The graph. |
|
711 |
/// \return %True when the graph bi-node-connected. |
|
712 |
template <typename Graph> |
|
713 |
bool biNodeConnected(const Graph& graph) { |
|
714 |
return countBiNodeConnectedComponents(graph) <= 1; |
|
715 |
} |
|
716 |
|
|
717 |
/// \ingroup connectivity |
|
718 |
/// |
|
719 |
/// \brief Count the biconnected components. |
|
720 |
/// |
|
721 |
/// This function finds the bi-node-connected components in an undirected |
|
722 |
/// graph. The biconnected components are the classes of an equivalence |
|
723 |
/// relation on the undirected edges. Two undirected edge is in relationship |
|
724 |
/// when they are on same circle. |
|
725 |
/// |
|
726 |
/// \param graph The graph. |
|
727 |
/// \return The number of components. |
|
728 |
template <typename Graph> |
|
729 |
int countBiNodeConnectedComponents(const Graph& graph) { |
|
730 |
checkConcept<concepts::Graph, Graph>(); |
|
731 |
typedef typename Graph::NodeIt NodeIt; |
|
732 |
|
|
733 |
using namespace _topology_bits; |
|
734 |
|
|
735 |
typedef CountBiNodeConnectedComponentsVisitor<Graph> Visitor; |
|
736 |
|
|
737 |
int compNum = 0; |
|
738 |
Visitor visitor(graph, compNum); |
|
739 |
|
|
740 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
|
741 |
dfs.init(); |
|
742 |
|
|
743 |
for (NodeIt it(graph); it != INVALID; ++it) { |
|
744 |
if (!dfs.reached(it)) { |
|
745 |
dfs.addSource(it); |
|
746 |
dfs.start(); |
|
747 |
} |
|
748 |
} |
|
749 |
return compNum; |
|
750 |
} |
|
751 |
|
|
752 |
/// \ingroup connectivity |
|
753 |
/// |
|
754 |
/// \brief Find the bi-node-connected components. |
|
755 |
/// |
|
756 |
/// This function finds the bi-node-connected components in an undirected |
|
757 |
/// graph. The bi-node-connected components are the classes of an equivalence |
|
758 |
/// relation on the undirected edges. Two undirected edge are in relationship |
|
759 |
/// when they are on same circle. |
|
760 |
/// |
|
761 |
/// \param graph The graph. |
|
762 |
/// \retval compMap A writable uedge map. The values will be set from 0 |
|
763 |
/// to the number of the biconnected components minus one. Each values |
|
764 |
/// of the map will be set exactly once, the values of a certain component |
|
765 |
/// will be set continuously. |
|
766 |
/// \return The number of components. |
|
767 |
/// |
|
768 |
template <typename Graph, typename EdgeMap> |
|
769 |
int biNodeConnectedComponents(const Graph& graph, |
|
770 |
EdgeMap& compMap) { |
|
771 |
checkConcept<concepts::Graph, Graph>(); |
|
772 |
typedef typename Graph::NodeIt NodeIt; |
|
773 |
typedef typename Graph::Edge Edge; |
|
774 |
checkConcept<concepts::WriteMap<Edge, int>, EdgeMap>(); |
|
775 |
|
|
776 |
using namespace _topology_bits; |
|
777 |
|
|
778 |
typedef BiNodeConnectedComponentsVisitor<Graph, EdgeMap> Visitor; |
|
779 |
|
|
780 |
int compNum = 0; |
|
781 |
Visitor visitor(graph, compMap, compNum); |
|
782 |
|
|
783 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
|
784 |
dfs.init(); |
|
785 |
|
|
786 |
for (NodeIt it(graph); it != INVALID; ++it) { |
|
787 |
if (!dfs.reached(it)) { |
|
788 |
dfs.addSource(it); |
|
789 |
dfs.start(); |
|
790 |
} |
|
791 |
} |
|
792 |
return compNum; |
|
793 |
} |
|
794 |
|
|
795 |
/// \ingroup connectivity |
|
796 |
/// |
|
797 |
/// \brief Find the bi-node-connected cut nodes. |
|
798 |
/// |
|
799 |
/// This function finds the bi-node-connected cut nodes in an undirected |
|
800 |
/// graph. The bi-node-connected components are the classes of an equivalence |
|
801 |
/// relation on the undirected edges. Two undirected edges are in |
|
802 |
/// relationship when they are on same circle. The biconnected components |
|
803 |
/// are separted by nodes which are the cut nodes of the components. |
|
804 |
/// |
|
805 |
/// \param graph The graph. |
|
806 |
/// \retval cutMap A writable edge map. The values will be set true when |
|
807 |
/// the node separate two or more components. |
|
808 |
/// \return The number of the cut nodes. |
|
809 |
template <typename Graph, typename NodeMap> |
|
810 |
int biNodeConnectedCutNodes(const Graph& graph, NodeMap& cutMap) { |
|
811 |
checkConcept<concepts::Graph, Graph>(); |
|
812 |
typedef typename Graph::Node Node; |
|
813 |
typedef typename Graph::NodeIt NodeIt; |
|
814 |
checkConcept<concepts::WriteMap<Node, bool>, NodeMap>(); |
|
815 |
|
|
816 |
using namespace _topology_bits; |
|
817 |
|
|
818 |
typedef BiNodeConnectedCutNodesVisitor<Graph, NodeMap> Visitor; |
|
819 |
|
|
820 |
int cutNum = 0; |
|
821 |
Visitor visitor(graph, cutMap, cutNum); |
|
822 |
|
|
823 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
|
824 |
dfs.init(); |
|
825 |
|
|
826 |
for (NodeIt it(graph); it != INVALID; ++it) { |
|
827 |
if (!dfs.reached(it)) { |
|
828 |
dfs.addSource(it); |
|
829 |
dfs.start(); |
|
830 |
} |
|
831 |
} |
|
832 |
return cutNum; |
|
833 |
} |
|
834 |
|
|
835 |
namespace _topology_bits { |
|
836 |
|
|
837 |
template <typename Digraph> |
|
838 |
class CountBiEdgeConnectedComponentsVisitor : public DfsVisitor<Digraph> { |
|
839 |
public: |
|
840 |
typedef typename Digraph::Node Node; |
|
841 |
typedef typename Digraph::Arc Arc; |
|
842 |
typedef typename Digraph::Edge Edge; |
|
843 |
|
|
844 |
CountBiEdgeConnectedComponentsVisitor(const Digraph& graph, int &compNum) |
|
845 |
: _graph(graph), _compNum(compNum), |
|
846 |
_numMap(graph), _retMap(graph), _predMap(graph), _num(0) {} |
|
847 |
|
|
848 |
void start(const Node& node) { |
|
849 |
_predMap.set(node, INVALID); |
|
850 |
} |
|
851 |
|
|
852 |
void reach(const Node& node) { |
|
853 |
_numMap.set(node, _num); |
|
854 |
_retMap.set(node, _num); |
|
855 |
++_num; |
|
856 |
} |
|
857 |
|
|
858 |
void leave(const Node& node) { |
|
859 |
if (_numMap[node] <= _retMap[node]) { |
|
860 |
++_compNum; |
|
861 |
} |
|
862 |
} |
|
863 |
|
|
864 |
void discover(const Arc& edge) { |
|
865 |
_predMap.set(_graph.target(edge), edge); |
|
866 |
} |
|
867 |
|
|
868 |
void examine(const Arc& edge) { |
|
869 |
if (_predMap[_graph.source(edge)] == _graph.oppositeArc(edge)) { |
|
870 |
return; |
|
871 |
} |
|
872 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
|
873 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
|
874 |
} |
|
875 |
} |
|
876 |
|
|
877 |
void backtrack(const Arc& edge) { |
|
878 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
|
879 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
|
880 |
} |
|
881 |
} |
|
882 |
|
|
883 |
private: |
|
884 |
const Digraph& _graph; |
|
885 |
int& _compNum; |
|
886 |
|
|
887 |
typename Digraph::template NodeMap<int> _numMap; |
|
888 |
typename Digraph::template NodeMap<int> _retMap; |
|
889 |
typename Digraph::template NodeMap<Arc> _predMap; |
|
890 |
int _num; |
|
891 |
}; |
|
892 |
|
|
893 |
template <typename Digraph, typename NodeMap> |
|
894 |
class BiEdgeConnectedComponentsVisitor : public DfsVisitor<Digraph> { |
|
895 |
public: |
|
896 |
typedef typename Digraph::Node Node; |
|
897 |
typedef typename Digraph::Arc Arc; |
|
898 |
typedef typename Digraph::Edge Edge; |
|
899 |
|
|
900 |
BiEdgeConnectedComponentsVisitor(const Digraph& graph, |
|
901 |
NodeMap& compMap, int &compNum) |
|
902 |
: _graph(graph), _compMap(compMap), _compNum(compNum), |
|
903 |
_numMap(graph), _retMap(graph), _predMap(graph), _num(0) {} |
|
904 |
|
|
905 |
void start(const Node& node) { |
|
906 |
_predMap.set(node, INVALID); |
|
907 |
} |
|
908 |
|
|
909 |
void reach(const Node& node) { |
|
910 |
_numMap.set(node, _num); |
|
911 |
_retMap.set(node, _num); |
|
912 |
_nodeStack.push(node); |
|
913 |
++_num; |
|
914 |
} |
|
915 |
|
|
916 |
void leave(const Node& node) { |
|
917 |
if (_numMap[node] <= _retMap[node]) { |
|
918 |
while (_nodeStack.top() != node) { |
|
919 |
_compMap.set(_nodeStack.top(), _compNum); |
|
920 |
_nodeStack.pop(); |
|
921 |
} |
|
922 |
_compMap.set(node, _compNum); |
|
923 |
_nodeStack.pop(); |
|
924 |
++_compNum; |
|
925 |
} |
|
926 |
} |
|
927 |
|
|
928 |
void discover(const Arc& edge) { |
|
929 |
_predMap.set(_graph.target(edge), edge); |
|
930 |
} |
|
931 |
|
|
932 |
void examine(const Arc& edge) { |
|
933 |
if (_predMap[_graph.source(edge)] == _graph.oppositeArc(edge)) { |
|
934 |
return; |
|
935 |
} |
|
936 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
|
937 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
|
938 |
} |
|
939 |
} |
|
940 |
|
|
941 |
void backtrack(const Arc& edge) { |
|
942 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
|
943 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
|
944 |
} |
|
945 |
} |
|
946 |
|
|
947 |
private: |
|
948 |
const Digraph& _graph; |
|
949 |
NodeMap& _compMap; |
|
950 |
int& _compNum; |
|
951 |
|
|
952 |
typename Digraph::template NodeMap<int> _numMap; |
|
953 |
typename Digraph::template NodeMap<int> _retMap; |
|
954 |
typename Digraph::template NodeMap<Arc> _predMap; |
|
955 |
std::stack<Node> _nodeStack; |
|
956 |
int _num; |
|
957 |
}; |
|
958 |
|
|
959 |
|
|
960 |
template <typename Digraph, typename ArcMap> |
|
961 |
class BiEdgeConnectedCutEdgesVisitor : public DfsVisitor<Digraph> { |
|
962 |
public: |
|
963 |
typedef typename Digraph::Node Node; |
|
964 |
typedef typename Digraph::Arc Arc; |
|
965 |
typedef typename Digraph::Edge Edge; |
|
966 |
|
|
967 |
BiEdgeConnectedCutEdgesVisitor(const Digraph& graph, |
|
968 |
ArcMap& cutMap, int &cutNum) |
|
969 |
: _graph(graph), _cutMap(cutMap), _cutNum(cutNum), |
|
970 |
_numMap(graph), _retMap(graph), _predMap(graph), _num(0) {} |
|
971 |
|
|
972 |
void start(const Node& node) { |
|
973 |
_predMap[node] = INVALID; |
|
974 |
} |
|
975 |
|
|
976 |
void reach(const Node& node) { |
|
977 |
_numMap.set(node, _num); |
|
978 |
_retMap.set(node, _num); |
|
979 |
++_num; |
|
980 |
} |
|
981 |
|
|
982 |
void leave(const Node& node) { |
|
983 |
if (_numMap[node] <= _retMap[node]) { |
|
984 |
if (_predMap[node] != INVALID) { |
|
985 |
_cutMap.set(_predMap[node], true); |
|
986 |
++_cutNum; |
|
987 |
} |
|
988 |
} |
|
989 |
} |
|
990 |
|
|
991 |
void discover(const Arc& edge) { |
|
992 |
_predMap.set(_graph.target(edge), edge); |
|
993 |
} |
|
994 |
|
|
995 |
void examine(const Arc& edge) { |
|
996 |
if (_predMap[_graph.source(edge)] == _graph.oppositeArc(edge)) { |
|
997 |
return; |
|
998 |
} |
|
999 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
|
1000 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
|
1001 |
} |
|
1002 |
} |
|
1003 |
|
|
1004 |
void backtrack(const Arc& edge) { |
|
1005 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
|
1006 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
|
1007 |
} |
|
1008 |
} |
|
1009 |
|
|
1010 |
private: |
|
1011 |
const Digraph& _graph; |
|
1012 |
ArcMap& _cutMap; |
|
1013 |
int& _cutNum; |
|
1014 |
|
|
1015 |
typename Digraph::template NodeMap<int> _numMap; |
|
1016 |
typename Digraph::template NodeMap<int> _retMap; |
|
1017 |
typename Digraph::template NodeMap<Arc> _predMap; |
|
1018 |
int _num; |
|
1019 |
}; |
|
1020 |
} |
|
1021 |
|
|
1022 |
template <typename Graph> |
|
1023 |
int countBiEdgeConnectedComponents(const Graph& graph); |
|
1024 |
|
|
1025 |
/// \ingroup connectivity |
|
1026 |
/// |
|
1027 |
/// \brief Checks that the graph is bi-edge-connected. |
|
1028 |
/// |
|
1029 |
/// This function checks that the graph is bi-edge-connected. The undirected |
|
1030 |
/// graph is bi-edge-connected when any two nodes are connected with two |
|
1031 |
/// edge-disjoint paths. |
|
1032 |
/// |
|
1033 |
/// \param graph The undirected graph. |
|
1034 |
/// \return The number of components. |
|
1035 |
template <typename Graph> |
|
1036 |
bool biEdgeConnected(const Graph& graph) { |
|
1037 |
return countBiEdgeConnectedComponents(graph) <= 1; |
|
1038 |
} |
|
1039 |
|
|
1040 |
/// \ingroup connectivity |
|
1041 |
/// |
|
1042 |
/// \brief Count the bi-edge-connected components. |
|
1043 |
/// |
|
1044 |
/// This function count the bi-edge-connected components in an undirected |
|
1045 |
/// graph. The bi-edge-connected components are the classes of an equivalence |
|
1046 |
/// relation on the nodes. Two nodes are in relationship when they are |
|
1047 |
/// connected with at least two edge-disjoint paths. |
|
1048 |
/// |
|
1049 |
/// \param graph The undirected graph. |
|
1050 |
/// \return The number of components. |
|
1051 |
template <typename Graph> |
|
1052 |
int countBiEdgeConnectedComponents(const Graph& graph) { |
|
1053 |
checkConcept<concepts::Graph, Graph>(); |
|
1054 |
typedef typename Graph::NodeIt NodeIt; |
|
1055 |
|
|
1056 |
using namespace _topology_bits; |
|
1057 |
|
|
1058 |
typedef CountBiEdgeConnectedComponentsVisitor<Graph> Visitor; |
|
1059 |
|
|
1060 |
int compNum = 0; |
|
1061 |
Visitor visitor(graph, compNum); |
|
1062 |
|
|
1063 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
|
1064 |
dfs.init(); |
|
1065 |
|
|
1066 |
for (NodeIt it(graph); it != INVALID; ++it) { |
|
1067 |
if (!dfs.reached(it)) { |
|
1068 |
dfs.addSource(it); |
|
1069 |
dfs.start(); |
|
1070 |
} |
|
1071 |
} |
|
1072 |
return compNum; |
|
1073 |
} |
|
1074 |
|
|
1075 |
/// \ingroup connectivity |
|
1076 |
/// |
|
1077 |
/// \brief Find the bi-edge-connected components. |
|
1078 |
/// |
|
1079 |
/// This function finds the bi-edge-connected components in an undirected |
|
1080 |
/// graph. The bi-edge-connected components are the classes of an equivalence |
|
1081 |
/// relation on the nodes. Two nodes are in relationship when they are |
|
1082 |
/// connected at least two edge-disjoint paths. |
|
1083 |
/// |
|
1084 |
/// \param graph The graph. |
|
1085 |
/// \retval compMap A writable node map. The values will be set from 0 to |
|
1086 |
/// the number of the biconnected components minus one. Each values |
|
1087 |
/// of the map will be set exactly once, the values of a certain component |
|
1088 |
/// will be set continuously. |
|
1089 |
/// \return The number of components. |
|
1090 |
/// |
|
1091 |
template <typename Graph, typename NodeMap> |
|
1092 |
int biEdgeConnectedComponents(const Graph& graph, NodeMap& compMap) { |
|
1093 |
checkConcept<concepts::Graph, Graph>(); |
|
1094 |
typedef typename Graph::NodeIt NodeIt; |
|
1095 |
typedef typename Graph::Node Node; |
|
1096 |
checkConcept<concepts::WriteMap<Node, int>, NodeMap>(); |
|
1097 |
|
|
1098 |
using namespace _topology_bits; |
|
1099 |
|
|
1100 |
typedef BiEdgeConnectedComponentsVisitor<Graph, NodeMap> Visitor; |
|
1101 |
|
|
1102 |
int compNum = 0; |
|
1103 |
Visitor visitor(graph, compMap, compNum); |
|
1104 |
|
|
1105 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
|
1106 |
dfs.init(); |
|
1107 |
|
|
1108 |
for (NodeIt it(graph); it != INVALID; ++it) { |
|
1109 |
if (!dfs.reached(it)) { |
|
1110 |
dfs.addSource(it); |
|
1111 |
dfs.start(); |
|
1112 |
} |
|
1113 |
} |
|
1114 |
return compNum; |
|
1115 |
} |
|
1116 |
|
|
1117 |
/// \ingroup connectivity |
|
1118 |
/// |
|
1119 |
/// \brief Find the bi-edge-connected cut edges. |
|
1120 |
/// |
|
1121 |
/// This function finds the bi-edge-connected components in an undirected |
|
1122 |
/// graph. The bi-edge-connected components are the classes of an equivalence |
|
1123 |
/// relation on the nodes. Two nodes are in relationship when they are |
|
1124 |
/// connected with at least two edge-disjoint paths. The bi-edge-connected |
|
1125 |
/// components are separted by edges which are the cut edges of the |
|
1126 |
/// components. |
|
1127 |
/// |
|
1128 |
/// \param graph The graph. |
|
1129 |
/// \retval cutMap A writable node map. The values will be set true when the |
|
1130 |
/// edge is a cut edge. |
|
1131 |
/// \return The number of cut edges. |
|
1132 |
template <typename Graph, typename EdgeMap> |
|
1133 |
int biEdgeConnectedCutEdges(const Graph& graph, EdgeMap& cutMap) { |
|
1134 |
checkConcept<concepts::Graph, Graph>(); |
|
1135 |
typedef typename Graph::NodeIt NodeIt; |
|
1136 |
typedef typename Graph::Edge Edge; |
|
1137 |
checkConcept<concepts::WriteMap<Edge, bool>, EdgeMap>(); |
|
1138 |
|
|
1139 |
using namespace _topology_bits; |
|
1140 |
|
|
1141 |
typedef BiEdgeConnectedCutEdgesVisitor<Graph, EdgeMap> Visitor; |
|
1142 |
|
|
1143 |
int cutNum = 0; |
|
1144 |
Visitor visitor(graph, cutMap, cutNum); |
|
1145 |
|
|
1146 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
|
1147 |
dfs.init(); |
|
1148 |
|
|
1149 |
for (NodeIt it(graph); it != INVALID; ++it) { |
|
1150 |
if (!dfs.reached(it)) { |
|
1151 |
dfs.addSource(it); |
|
1152 |
dfs.start(); |
|
1153 |
} |
|
1154 |
} |
|
1155 |
return cutNum; |
|
1156 |
} |
|
1157 |
|
|
1158 |
|
|
1159 |
namespace _topology_bits { |
|
1160 |
|
|
1161 |
template <typename Digraph, typename IntNodeMap> |
|
1162 |
class TopologicalSortVisitor : public DfsVisitor<Digraph> { |
|
1163 |
public: |
|
1164 |
typedef typename Digraph::Node Node; |
|
1165 |
typedef typename Digraph::Arc edge; |
|
1166 |
|
|
1167 |
TopologicalSortVisitor(IntNodeMap& order, int num) |
|
1168 |
: _order(order), _num(num) {} |
|
1169 |
|
|
1170 |
void leave(const Node& node) { |
|
1171 |
_order.set(node, --_num); |
|
1172 |
} |
|
1173 |
|
|
1174 |
private: |
|
1175 |
IntNodeMap& _order; |
|
1176 |
int _num; |
|
1177 |
}; |
|
1178 |
|
|
1179 |
} |
|
1180 |
|
|
1181 |
/// \ingroup connectivity |
|
1182 |
/// |
|
1183 |
/// \brief Sort the nodes of a DAG into topolgical order. |
|
1184 |
/// |
|
1185 |
/// Sort the nodes of a DAG into topolgical order. |
|
1186 |
/// |
|
1187 |
/// \param graph The graph. It must be directed and acyclic. |
|
1188 |
/// \retval order A writable node map. The values will be set from 0 to |
|
1189 |
/// the number of the nodes in the graph minus one. Each values of the map |
|
1190 |
/// will be set exactly once, the values will be set descending order. |
|
1191 |
/// |
|
1192 |
/// \see checkedTopologicalSort |
|
1193 |
/// \see dag |
|
1194 |
template <typename Digraph, typename NodeMap> |
|
1195 |
void topologicalSort(const Digraph& graph, NodeMap& order) { |
|
1196 |
using namespace _topology_bits; |
|
1197 |
|
|
1198 |
checkConcept<concepts::Digraph, Digraph>(); |
|
1199 |
checkConcept<concepts::WriteMap<typename Digraph::Node, int>, NodeMap>(); |
|
1200 |
|
|
1201 |
typedef typename Digraph::Node Node; |
|
1202 |
typedef typename Digraph::NodeIt NodeIt; |
|
1203 |
typedef typename Digraph::Arc Arc; |
|
1204 |
|
|
1205 |
TopologicalSortVisitor<Digraph, NodeMap> |
|
1206 |
visitor(order, countNodes(graph)); |
|
1207 |
|
|
1208 |
DfsVisit<Digraph, TopologicalSortVisitor<Digraph, NodeMap> > |
|
1209 |
dfs(graph, visitor); |
|
1210 |
|
|
1211 |
dfs.init(); |
|
1212 |
for (NodeIt it(graph); it != INVALID; ++it) { |
|
1213 |
if (!dfs.reached(it)) { |
|
1214 |
dfs.addSource(it); |
|
1215 |
dfs.start(); |
|
1216 |
} |
|
1217 |
} |
|
1218 |
} |
|
1219 |
|
|
1220 |
/// \ingroup connectivity |
|
1221 |
/// |
|
1222 |
/// \brief Sort the nodes of a DAG into topolgical order. |
|
1223 |
/// |
|
1224 |
/// Sort the nodes of a DAG into topolgical order. It also checks |
|
1225 |
/// that the given graph is DAG. |
|
1226 |
/// |
|
1227 |
/// \param graph The graph. It must be directed and acyclic. |
|
1228 |
/// \retval order A readable - writable node map. The values will be set |
|
1229 |
/// from 0 to the number of the nodes in the graph minus one. Each values |
|
1230 |
/// of the map will be set exactly once, the values will be set descending |
|
1231 |
/// order. |
|
1232 |
/// \return %False when the graph is not DAG. |
|
1233 |
/// |
|
1234 |
/// \see topologicalSort |
|
1235 |
/// \see dag |
|
1236 |
template <typename Digraph, typename NodeMap> |
|
1237 |
bool checkedTopologicalSort(const Digraph& graph, NodeMap& order) { |
|
1238 |
using namespace _topology_bits; |
|
1239 |
|
|
1240 |
checkConcept<concepts::Digraph, Digraph>(); |
|
1241 |
checkConcept<concepts::ReadWriteMap<typename Digraph::Node, int>, |
|
1242 |
NodeMap>(); |
|
1243 |
|
|
1244 |
typedef typename Digraph::Node Node; |
|
1245 |
typedef typename Digraph::NodeIt NodeIt; |
|
1246 |
typedef typename Digraph::Arc Arc; |
|
1247 |
|
|
1248 |
order = constMap<Node, int, -1>(); |
|
1249 |
|
|
1250 |
TopologicalSortVisitor<Digraph, NodeMap> |
|
1251 |
visitor(order, countNodes(graph)); |
|
1252 |
|
|
1253 |
DfsVisit<Digraph, TopologicalSortVisitor<Digraph, NodeMap> > |
|
1254 |
dfs(graph, visitor); |
|
1255 |
|
|
1256 |
dfs.init(); |
|
1257 |
for (NodeIt it(graph); it != INVALID; ++it) { |
|
1258 |
if (!dfs.reached(it)) { |
|
1259 |
dfs.addSource(it); |
|
1260 |
while (!dfs.emptyQueue()) { |
|
1261 |
Arc edge = dfs.nextArc(); |
|
1262 |
Node target = graph.target(edge); |
|
1263 |
if (dfs.reached(target) && order[target] == -1) { |
|
1264 |
return false; |
|
1265 |
} |
|
1266 |
dfs.processNextArc(); |
|
1267 |
} |
|
1268 |
} |
|
1269 |
} |
|
1270 |
return true; |
|
1271 |
} |
|
1272 |
|
|
1273 |
/// \ingroup connectivity |
|
1274 |
/// |
|
1275 |
/// \brief Check that the given directed graph is a DAG. |
|
1276 |
/// |
|
1277 |
/// Check that the given directed graph is a DAG. The DAG is |
|
1278 |
/// an Directed Acyclic Digraph. |
|
1279 |
/// \return %False when the graph is not DAG. |
|
1280 |
/// \see acyclic |
|
1281 |
template <typename Digraph> |
|
1282 |
bool dag(const Digraph& graph) { |
|
1283 |
|
|
1284 |
checkConcept<concepts::Digraph, Digraph>(); |
|
1285 |
|
|
1286 |
typedef typename Digraph::Node Node; |
|
1287 |
typedef typename Digraph::NodeIt NodeIt; |
|
1288 |
typedef typename Digraph::Arc Arc; |
|
1289 |
|
|
1290 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
|
1291 |
|
|
1292 |
typename Dfs<Digraph>::template SetProcessedMap<ProcessedMap>:: |
|
1293 |
Create dfs(graph); |
|
1294 |
|
|
1295 |
ProcessedMap processed(graph); |
|
1296 |
dfs.processedMap(processed); |
|
1297 |
|
|
1298 |
dfs.init(); |
|
1299 |
for (NodeIt it(graph); it != INVALID; ++it) { |
|
1300 |
if (!dfs.reached(it)) { |
|
1301 |
dfs.addSource(it); |
|
1302 |
while (!dfs.emptyQueue()) { |
|
1303 |
Arc edge = dfs.nextArc(); |
|
1304 |
Node target = graph.target(edge); |
|
1305 |
if (dfs.reached(target) && !processed[target]) { |
|
1306 |
return false; |
|
1307 |
} |
|
1308 |
dfs.processNextArc(); |
|
1309 |
} |
|
1310 |
} |
|
1311 |
} |
|
1312 |
return true; |
|
1313 |
} |
|
1314 |
|
|
1315 |
/// \ingroup connectivity |
|
1316 |
/// |
|
1317 |
/// \brief Check that the given undirected graph is acyclic. |
|
1318 |
/// |
|
1319 |
/// Check that the given undirected graph acyclic. |
|
1320 |
/// \param graph The undirected graph. |
|
1321 |
/// \return %True when there is no circle in the graph. |
|
1322 |
/// \see dag |
|
1323 |
template <typename Graph> |
|
1324 |
bool acyclic(const Graph& graph) { |
|
1325 |
checkConcept<concepts::Graph, Graph>(); |
|
1326 |
typedef typename Graph::Node Node; |
|
1327 |
typedef typename Graph::NodeIt NodeIt; |
|
1328 |
typedef typename Graph::Arc Arc; |
|
1329 |
Dfs<Graph> dfs(graph); |
|
1330 |
dfs.init(); |
|
1331 |
for (NodeIt it(graph); it != INVALID; ++it) { |
|
1332 |
if (!dfs.reached(it)) { |
|
1333 |
dfs.addSource(it); |
|
1334 |
while (!dfs.emptyQueue()) { |
|
1335 |
Arc edge = dfs.nextArc(); |
|
1336 |
Node source = graph.source(edge); |
|
1337 |
Node target = graph.target(edge); |
|
1338 |
if (dfs.reached(target) && |
|
1339 |
dfs.predArc(source) != graph.oppositeArc(edge)) { |
|
1340 |
return false; |
|
1341 |
} |
|
1342 |
dfs.processNextArc(); |
|
1343 |
} |
|
1344 |
} |
|
1345 |
} |
|
1346 |
return true; |
|
1347 |
} |
|
1348 |
|
|
1349 |
/// \ingroup connectivity |
|
1350 |
/// |
|
1351 |
/// \brief Check that the given undirected graph is tree. |
|
1352 |
/// |
|
1353 |
/// Check that the given undirected graph is tree. |
|
1354 |
/// \param graph The undirected graph. |
|
1355 |
/// \return %True when the graph is acyclic and connected. |
|
1356 |
template <typename Graph> |
|
1357 |
bool tree(const Graph& graph) { |
|
1358 |
checkConcept<concepts::Graph, Graph>(); |
|
1359 |
typedef typename Graph::Node Node; |
|
1360 |
typedef typename Graph::NodeIt NodeIt; |
|
1361 |
typedef typename Graph::Arc Arc; |
|
1362 |
Dfs<Graph> dfs(graph); |
|
1363 |
dfs.init(); |
|
1364 |
dfs.addSource(NodeIt(graph)); |
|
1365 |
while (!dfs.emptyQueue()) { |
|
1366 |
Arc edge = dfs.nextArc(); |
|
1367 |
Node source = graph.source(edge); |
|
1368 |
Node target = graph.target(edge); |
|
1369 |
if (dfs.reached(target) && |
|
1370 |
dfs.predArc(source) != graph.oppositeArc(edge)) { |
|
1371 |
return false; |
|
1372 |
} |
|
1373 |
dfs.processNextArc(); |
|
1374 |
} |
|
1375 |
for (NodeIt it(graph); it != INVALID; ++it) { |
|
1376 |
if (!dfs.reached(it)) { |
|
1377 |
return false; |
|
1378 |
} |
|
1379 |
} |
|
1380 |
return true; |
|
1381 |
} |
|
1382 |
|
|
1383 |
namespace _topology_bits { |
|
1384 |
|
|
1385 |
template <typename Digraph> |
|
1386 |
class BipartiteVisitor : public BfsVisitor<Digraph> { |
|
1387 |
public: |
|
1388 |
typedef typename Digraph::Arc Arc; |
|
1389 |
typedef typename Digraph::Node Node; |
|
1390 |
|
|
1391 |
BipartiteVisitor(const Digraph& graph, bool& bipartite) |
|
1392 |
: _graph(graph), _part(graph), _bipartite(bipartite) {} |
|
1393 |
|
|
1394 |
void start(const Node& node) { |
|
1395 |
_part[node] = true; |
|
1396 |
} |
|
1397 |
void discover(const Arc& edge) { |
|
1398 |
_part.set(_graph.target(edge), !_part[_graph.source(edge)]); |
|
1399 |
} |
|
1400 |
void examine(const Arc& edge) { |
|
1401 |
_bipartite = _bipartite && |
|
1402 |
_part[_graph.target(edge)] != _part[_graph.source(edge)]; |
|
1403 |
} |
|
1404 |
|
|
1405 |
private: |
|
1406 |
|
|
1407 |
const Digraph& _graph; |
|
1408 |
typename Digraph::template NodeMap<bool> _part; |
|
1409 |
bool& _bipartite; |
|
1410 |
}; |
|
1411 |
|
|
1412 |
template <typename Digraph, typename PartMap> |
|
1413 |
class BipartitePartitionsVisitor : public BfsVisitor<Digraph> { |
|
1414 |
public: |
|
1415 |
typedef typename Digraph::Arc Arc; |
|
1416 |
typedef typename Digraph::Node Node; |
|
1417 |
|
|
1418 |
BipartitePartitionsVisitor(const Digraph& graph, |
|
1419 |
PartMap& part, bool& bipartite) |
|
1420 |
: _graph(graph), _part(part), _bipartite(bipartite) {} |
|
1421 |
|
|
1422 |
void start(const Node& node) { |
|
1423 |
_part.set(node, true); |
|
1424 |
} |
|
1425 |
void discover(const Arc& edge) { |
|
1426 |
_part.set(_graph.target(edge), !_part[_graph.source(edge)]); |
|
1427 |
} |
|
1428 |
void examine(const Arc& edge) { |
|
1429 |
_bipartite = _bipartite && |
|
1430 |
_part[_graph.target(edge)] != _part[_graph.source(edge)]; |
|
1431 |
} |
|
1432 |
|
|
1433 |
private: |
|
1434 |
|
|
1435 |
const Digraph& _graph; |
|
1436 |
PartMap& _part; |
|
1437 |
bool& _bipartite; |
|
1438 |
}; |
|
1439 |
} |
|
1440 |
|
|
1441 |
/// \ingroup connectivity |
|
1442 |
/// |
|
1443 |
/// \brief Check if the given undirected graph is bipartite or not |
|
1444 |
/// |
|
1445 |
/// The function checks if the given undirected \c graph graph is bipartite |
|
1446 |
/// or not. The \ref Bfs algorithm is used to calculate the result. |
|
1447 |
/// \param graph The undirected graph. |
|
1448 |
/// \return %True if \c graph is bipartite, %false otherwise. |
|
1449 |
/// \sa bipartitePartitions |
|
1450 |
template<typename Graph> |
|
1451 |
inline bool bipartite(const Graph &graph){ |
|
1452 |
using namespace _topology_bits; |
|
1453 |
|
|
1454 |
checkConcept<concepts::Graph, Graph>(); |
|
1455 |
|
|
1456 |
typedef typename Graph::NodeIt NodeIt; |
|
1457 |
typedef typename Graph::ArcIt ArcIt; |
|
1458 |
|
|
1459 |
bool bipartite = true; |
|
1460 |
|
|
1461 |
BipartiteVisitor<Graph> |
|
1462 |
visitor(graph, bipartite); |
|
1463 |
BfsVisit<Graph, BipartiteVisitor<Graph> > |
|
1464 |
bfs(graph, visitor); |
|
1465 |
bfs.init(); |
|
1466 |
for(NodeIt it(graph); it != INVALID; ++it) { |
|
1467 |
if(!bfs.reached(it)){ |
|
1468 |
bfs.addSource(it); |
|
1469 |
while (!bfs.emptyQueue()) { |
|
1470 |
bfs.processNextNode(); |
|
1471 |
if (!bipartite) return false; |
|
1472 |
} |
|
1473 |
} |
|
1474 |
} |
|
1475 |
return true; |
|
1476 |
} |
|
1477 |
|
|
1478 |
/// \ingroup connectivity |
|
1479 |
/// |
|
1480 |
/// \brief Check if the given undirected graph is bipartite or not |
|
1481 |
/// |
|
1482 |
/// The function checks if the given undirected graph is bipartite |
|
1483 |
/// or not. The \ref Bfs algorithm is used to calculate the result. |
|
1484 |
/// During the execution, the \c partMap will be set as the two |
|
1485 |
/// partitions of the graph. |
|
1486 |
/// \param graph The undirected graph. |
|
1487 |
/// \retval partMap A writable bool map of nodes. It will be set as the |
|
1488 |
/// two partitions of the graph. |
|
1489 |
/// \return %True if \c graph is bipartite, %false otherwise. |
|
1490 |
template<typename Graph, typename NodeMap> |
|
1491 |
inline bool bipartitePartitions(const Graph &graph, NodeMap &partMap){ |
|
1492 |
using namespace _topology_bits; |
|
1493 |
|
|
1494 |
checkConcept<concepts::Graph, Graph>(); |
|
1495 |
|
|
1496 |
typedef typename Graph::Node Node; |
|
1497 |
typedef typename Graph::NodeIt NodeIt; |
|
1498 |
typedef typename Graph::ArcIt ArcIt; |
|
1499 |
|
|
1500 |
bool bipartite = true; |
|
1501 |
|
|
1502 |
BipartitePartitionsVisitor<Graph, NodeMap> |
|
1503 |
visitor(graph, partMap, bipartite); |
|
1504 |
BfsVisit<Graph, BipartitePartitionsVisitor<Graph, NodeMap> > |
|
1505 |
bfs(graph, visitor); |
|
1506 |
bfs.init(); |
|
1507 |
for(NodeIt it(graph); it != INVALID; ++it) { |
|
1508 |
if(!bfs.reached(it)){ |
|
1509 |
bfs.addSource(it); |
|
1510 |
while (!bfs.emptyQueue()) { |
|
1511 |
bfs.processNextNode(); |
|
1512 |
if (!bipartite) return false; |
|
1513 |
} |
|
1514 |
} |
|
1515 |
} |
|
1516 |
return true; |
|
1517 |
} |
|
1518 |
|
|
1519 |
/// \brief Returns true when there are not loop edges in the graph. |
|
1520 |
/// |
|
1521 |
/// Returns true when there are not loop edges in the graph. |
|
1522 |
template <typename Digraph> |
|
1523 |
bool loopFree(const Digraph& graph) { |
|
1524 |
for (typename Digraph::ArcIt it(graph); it != INVALID; ++it) { |
|
1525 |
if (graph.source(it) == graph.target(it)) return false; |
|
1526 |
} |
|
1527 |
return true; |
|
1528 |
} |
|
1529 |
|
|
1530 |
/// \brief Returns true when there are not parallel edges in the graph. |
|
1531 |
/// |
|
1532 |
/// Returns true when there are not parallel edges in the graph. |
|
1533 |
template <typename Digraph> |
|
1534 |
bool parallelFree(const Digraph& graph) { |
|
1535 |
typename Digraph::template NodeMap<bool> reached(graph, false); |
|
1536 |
for (typename Digraph::NodeIt n(graph); n != INVALID; ++n) { |
|
1537 |
for (typename Digraph::OutArcIt e(graph, n); e != INVALID; ++e) { |
|
1538 |
if (reached[graph.target(e)]) return false; |
|
1539 |
reached.set(graph.target(e), true); |
|
1540 |
} |
|
1541 |
for (typename Digraph::OutArcIt e(graph, n); e != INVALID; ++e) { |
|
1542 |
reached.set(graph.target(e), false); |
|
1543 |
} |
|
1544 |
} |
|
1545 |
return true; |
|
1546 |
} |
|
1547 |
|
|
1548 |
/// \brief Returns true when there are not loop edges and parallel |
|
1549 |
/// edges in the graph. |
|
1550 |
/// |
|
1551 |
/// Returns true when there are not loop edges and parallel edges in |
|
1552 |
/// the graph. |
|
1553 |
template <typename Digraph> |
|
1554 |
bool simpleDigraph(const Digraph& graph) { |
|
1555 |
typename Digraph::template NodeMap<bool> reached(graph, false); |
|
1556 |
for (typename Digraph::NodeIt n(graph); n != INVALID; ++n) { |
|
1557 |
reached.set(n, true); |
|
1558 |
for (typename Digraph::OutArcIt e(graph, n); e != INVALID; ++e) { |
|
1559 |
if (reached[graph.target(e)]) return false; |
|
1560 |
reached.set(graph.target(e), true); |
|
1561 |
} |
|
1562 |
for (typename Digraph::OutArcIt e(graph, n); e != INVALID; ++e) { |
|
1563 |
reached.set(graph.target(e), false); |
|
1564 |
} |
|
1565 |
reached.set(n, false); |
|
1566 |
} |
|
1567 |
return true; |
|
1568 |
} |
|
1569 |
|
|
1570 |
} //namespace lemon |
|
1571 |
|
|
1572 |
#endif //LEMON_TOPOLOGY_H |
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#include<iostream> |
|
20 |
#include<lemon/concept_check.h> |
|
21 |
|
|
22 |
#include<lemon/list_graph.h> |
|
23 |
#include<lemon/smart_graph.h> |
|
24 |
|
|
25 |
#include<lemon/concepts/digraph.h> |
|
26 |
#include<lemon/concepts/graph.h> |
|
27 |
|
|
28 |
#include<lemon/adaptors.h> |
|
29 |
|
|
30 |
#include <limits> |
|
31 |
#include <lemon/bfs.h> |
|
32 |
#include <lemon/path.h> |
|
33 |
|
|
34 |
#include"test/test_tools.h" |
|
35 |
#include"test/graph_test.h" |
|
36 |
|
|
37 |
using namespace lemon; |
|
38 |
|
|
39 |
void checkReverseDigraph() { |
|
40 |
checkConcept<concepts::Digraph, ReverseDigraph<concepts::Digraph> >(); |
|
41 |
|
|
42 |
typedef ListDigraph Digraph; |
|
43 |
typedef ReverseDigraph<Digraph> Adaptor; |
|
44 |
|
|
45 |
Digraph digraph; |
|
46 |
Adaptor adaptor(digraph); |
|
47 |
|
|
48 |
Digraph::Node n1 = digraph.addNode(); |
|
49 |
Digraph::Node n2 = digraph.addNode(); |
|
50 |
Digraph::Node n3 = digraph.addNode(); |
|
51 |
|
|
52 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
|
53 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
|
54 |
Digraph::Arc a3 = digraph.addArc(n2, n3); |
|
55 |
|
|
56 |
checkGraphNodeList(adaptor, 3); |
|
57 |
checkGraphArcList(adaptor, 3); |
|
58 |
checkGraphConArcList(adaptor, 3); |
|
59 |
|
|
60 |
checkGraphOutArcList(adaptor, n1, 0); |
|
61 |
checkGraphOutArcList(adaptor, n2, 1); |
|
62 |
checkGraphOutArcList(adaptor, n3, 2); |
|
63 |
|
|
64 |
checkGraphInArcList(adaptor, n1, 2); |
|
65 |
checkGraphInArcList(adaptor, n2, 1); |
|
66 |
checkGraphInArcList(adaptor, n3, 0); |
|
67 |
|
|
68 |
checkNodeIds(adaptor); |
|
69 |
checkArcIds(adaptor); |
|
70 |
|
|
71 |
checkGraphNodeMap(adaptor); |
|
72 |
checkGraphArcMap(adaptor); |
|
73 |
|
|
74 |
for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) { |
|
75 |
check(adaptor.source(a) == digraph.target(a), "Wrong reverse"); |
|
76 |
check(adaptor.target(a) == digraph.source(a), "Wrong reverse"); |
|
77 |
} |
|
78 |
} |
|
79 |
|
|
80 |
void checkSubDigraph() { |
|
81 |
checkConcept<concepts::Digraph, |
|
82 |
SubDigraph<concepts::Digraph, |
|
83 |
concepts::Digraph::NodeMap<bool>, |
|
84 |
concepts::Digraph::ArcMap<bool> > >(); |
|
85 |
|
|
86 |
typedef ListDigraph Digraph; |
|
87 |
typedef Digraph::NodeMap<bool> NodeFilter; |
|
88 |
typedef Digraph::ArcMap<bool> ArcFilter; |
|
89 |
typedef SubDigraph<Digraph, NodeFilter, ArcFilter> Adaptor; |
|
90 |
|
|
91 |
Digraph digraph; |
|
92 |
NodeFilter node_filter(digraph); |
|
93 |
ArcFilter arc_filter(digraph); |
|
94 |
Adaptor adaptor(digraph, node_filter, arc_filter); |
|
95 |
|
|
96 |
Digraph::Node n1 = digraph.addNode(); |
|
97 |
Digraph::Node n2 = digraph.addNode(); |
|
98 |
Digraph::Node n3 = digraph.addNode(); |
|
99 |
|
|
100 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
|
101 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
|
102 |
Digraph::Arc a3 = digraph.addArc(n2, n3); |
|
103 |
|
|
104 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = true; |
|
105 |
arc_filter[a1] = arc_filter[a2] = arc_filter[a3] = true; |
|
106 |
|
|
107 |
checkGraphNodeList(adaptor, 3); |
|
108 |
checkGraphArcList(adaptor, 3); |
|
109 |
checkGraphConArcList(adaptor, 3); |
|
110 |
|
|
111 |
checkGraphOutArcList(adaptor, n1, 2); |
|
112 |
checkGraphOutArcList(adaptor, n2, 1); |
|
113 |
checkGraphOutArcList(adaptor, n3, 0); |
|
114 |
|
|
115 |
checkGraphInArcList(adaptor, n1, 0); |
|
116 |
checkGraphInArcList(adaptor, n2, 1); |
|
117 |
checkGraphInArcList(adaptor, n3, 2); |
|
118 |
|
|
119 |
checkNodeIds(adaptor); |
|
120 |
checkArcIds(adaptor); |
|
121 |
|
|
122 |
checkGraphNodeMap(adaptor); |
|
123 |
checkGraphArcMap(adaptor); |
|
124 |
|
|
125 |
arc_filter[a2] = false; |
|
126 |
|
|
127 |
checkGraphNodeList(adaptor, 3); |
|
128 |
checkGraphArcList(adaptor, 2); |
|
129 |
checkGraphConArcList(adaptor, 2); |
|
130 |
|
|
131 |
checkGraphOutArcList(adaptor, n1, 1); |
|
132 |
checkGraphOutArcList(adaptor, n2, 1); |
|
133 |
checkGraphOutArcList(adaptor, n3, 0); |
|
134 |
|
|
135 |
checkGraphInArcList(adaptor, n1, 0); |
|
136 |
checkGraphInArcList(adaptor, n2, 1); |
|
137 |
checkGraphInArcList(adaptor, n3, 1); |
|
138 |
|
|
139 |
checkNodeIds(adaptor); |
|
140 |
checkArcIds(adaptor); |
|
141 |
|
|
142 |
checkGraphNodeMap(adaptor); |
|
143 |
checkGraphArcMap(adaptor); |
|
144 |
|
|
145 |
node_filter[n1] = false; |
|
146 |
|
|
147 |
checkGraphNodeList(adaptor, 2); |
|
148 |
checkGraphArcList(adaptor, 1); |
|
149 |
checkGraphConArcList(adaptor, 1); |
|
150 |
|
|
151 |
checkGraphOutArcList(adaptor, n2, 1); |
|
152 |
checkGraphOutArcList(adaptor, n3, 0); |
|
153 |
|
|
154 |
checkGraphInArcList(adaptor, n2, 0); |
|
155 |
checkGraphInArcList(adaptor, n3, 1); |
|
156 |
|
|
157 |
checkNodeIds(adaptor); |
|
158 |
checkArcIds(adaptor); |
|
159 |
|
|
160 |
checkGraphNodeMap(adaptor); |
|
161 |
checkGraphArcMap(adaptor); |
|
162 |
|
|
163 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = false; |
|
164 |
arc_filter[a1] = arc_filter[a2] = arc_filter[a3] = false; |
|
165 |
|
|
166 |
checkGraphNodeList(adaptor, 0); |
|
167 |
checkGraphArcList(adaptor, 0); |
|
168 |
checkGraphConArcList(adaptor, 0); |
|
169 |
|
|
170 |
checkNodeIds(adaptor); |
|
171 |
checkArcIds(adaptor); |
|
172 |
|
|
173 |
checkGraphNodeMap(adaptor); |
|
174 |
checkGraphArcMap(adaptor); |
|
175 |
} |
|
176 |
|
|
177 |
void checkFilterNodes1() { |
|
178 |
checkConcept<concepts::Digraph, |
|
179 |
FilterNodes<concepts::Digraph, |
|
180 |
concepts::Digraph::NodeMap<bool> > >(); |
|
181 |
|
|
182 |
typedef ListDigraph Digraph; |
|
183 |
typedef Digraph::NodeMap<bool> NodeFilter; |
|
184 |
typedef FilterNodes<Digraph, NodeFilter> Adaptor; |
|
185 |
|
|
186 |
Digraph digraph; |
|
187 |
NodeFilter node_filter(digraph); |
|
188 |
Adaptor adaptor(digraph, node_filter); |
|
189 |
|
|
190 |
Digraph::Node n1 = digraph.addNode(); |
|
191 |
Digraph::Node n2 = digraph.addNode(); |
|
192 |
Digraph::Node n3 = digraph.addNode(); |
|
193 |
|
|
194 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
|
195 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
|
196 |
Digraph::Arc a3 = digraph.addArc(n2, n3); |
|
197 |
|
|
198 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = true; |
|
199 |
|
|
200 |
checkGraphNodeList(adaptor, 3); |
|
201 |
checkGraphArcList(adaptor, 3); |
|
202 |
checkGraphConArcList(adaptor, 3); |
|
203 |
|
|
204 |
checkGraphOutArcList(adaptor, n1, 2); |
|
205 |
checkGraphOutArcList(adaptor, n2, 1); |
|
206 |
checkGraphOutArcList(adaptor, n3, 0); |
|
207 |
|
|
208 |
checkGraphInArcList(adaptor, n1, 0); |
|
209 |
checkGraphInArcList(adaptor, n2, 1); |
|
210 |
checkGraphInArcList(adaptor, n3, 2); |
|
211 |
|
|
212 |
checkNodeIds(adaptor); |
|
213 |
checkArcIds(adaptor); |
|
214 |
|
|
215 |
checkGraphNodeMap(adaptor); |
|
216 |
checkGraphArcMap(adaptor); |
|
217 |
|
|
218 |
node_filter[n1] = false; |
|
219 |
|
|
220 |
checkGraphNodeList(adaptor, 2); |
|
221 |
checkGraphArcList(adaptor, 1); |
|
222 |
checkGraphConArcList(adaptor, 1); |
|
223 |
|
|
224 |
checkGraphOutArcList(adaptor, n2, 1); |
|
225 |
checkGraphOutArcList(adaptor, n3, 0); |
|
226 |
|
|
227 |
checkGraphInArcList(adaptor, n2, 0); |
|
228 |
checkGraphInArcList(adaptor, n3, 1); |
|
229 |
|
|
230 |
checkNodeIds(adaptor); |
|
231 |
checkArcIds(adaptor); |
|
232 |
|
|
233 |
checkGraphNodeMap(adaptor); |
|
234 |
checkGraphArcMap(adaptor); |
|
235 |
|
|
236 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = false; |
|
237 |
|
|
238 |
checkGraphNodeList(adaptor, 0); |
|
239 |
checkGraphArcList(adaptor, 0); |
|
240 |
checkGraphConArcList(adaptor, 0); |
|
241 |
|
|
242 |
checkNodeIds(adaptor); |
|
243 |
checkArcIds(adaptor); |
|
244 |
|
|
245 |
checkGraphNodeMap(adaptor); |
|
246 |
checkGraphArcMap(adaptor); |
|
247 |
} |
|
248 |
|
|
249 |
void checkFilterArcs() { |
|
250 |
checkConcept<concepts::Digraph, |
|
251 |
FilterArcs<concepts::Digraph, |
|
252 |
concepts::Digraph::ArcMap<bool> > >(); |
|
253 |
|
|
254 |
typedef ListDigraph Digraph; |
|
255 |
typedef Digraph::ArcMap<bool> ArcFilter; |
|
256 |
typedef FilterArcs<Digraph, ArcFilter> Adaptor; |
|
257 |
|
|
258 |
Digraph digraph; |
|
259 |
ArcFilter arc_filter(digraph); |
|
260 |
Adaptor adaptor(digraph, arc_filter); |
|
261 |
|
|
262 |
Digraph::Node n1 = digraph.addNode(); |
|
263 |
Digraph::Node n2 = digraph.addNode(); |
|
264 |
Digraph::Node n3 = digraph.addNode(); |
|
265 |
|
|
266 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
|
267 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
|
268 |
Digraph::Arc a3 = digraph.addArc(n2, n3); |
|
269 |
|
|
270 |
arc_filter[a1] = arc_filter[a2] = arc_filter[a3] = true; |
|
271 |
|
|
272 |
checkGraphNodeList(adaptor, 3); |
|
273 |
checkGraphArcList(adaptor, 3); |
|
274 |
checkGraphConArcList(adaptor, 3); |
|
275 |
|
|
276 |
checkGraphOutArcList(adaptor, n1, 2); |
|
277 |
checkGraphOutArcList(adaptor, n2, 1); |
|
278 |
checkGraphOutArcList(adaptor, n3, 0); |
|
279 |
|
|
280 |
checkGraphInArcList(adaptor, n1, 0); |
|
281 |
checkGraphInArcList(adaptor, n2, 1); |
|
282 |
checkGraphInArcList(adaptor, n3, 2); |
|
283 |
|
|
284 |
checkNodeIds(adaptor); |
|
285 |
checkArcIds(adaptor); |
|
286 |
|
|
287 |
checkGraphNodeMap(adaptor); |
|
288 |
checkGraphArcMap(adaptor); |
|
289 |
|
|
290 |
arc_filter[a2] = false; |
|
291 |
|
|
292 |
checkGraphNodeList(adaptor, 3); |
|
293 |
checkGraphArcList(adaptor, 2); |
|
294 |
checkGraphConArcList(adaptor, 2); |
|
295 |
|
|
296 |
checkGraphOutArcList(adaptor, n1, 1); |
|
297 |
checkGraphOutArcList(adaptor, n2, 1); |
|
298 |
checkGraphOutArcList(adaptor, n3, 0); |
|
299 |
|
|
300 |
checkGraphInArcList(adaptor, n1, 0); |
|
301 |
checkGraphInArcList(adaptor, n2, 1); |
|
302 |
checkGraphInArcList(adaptor, n3, 1); |
|
303 |
|
|
304 |
checkNodeIds(adaptor); |
|
305 |
checkArcIds(adaptor); |
|
306 |
|
|
307 |
checkGraphNodeMap(adaptor); |
|
308 |
checkGraphArcMap(adaptor); |
|
309 |
|
|
310 |
arc_filter[a1] = arc_filter[a2] = arc_filter[a3] = false; |
|
311 |
|
|
312 |
checkGraphNodeList(adaptor, 3); |
|
313 |
checkGraphArcList(adaptor, 0); |
|
314 |
checkGraphConArcList(adaptor, 0); |
|
315 |
|
|
316 |
checkNodeIds(adaptor); |
|
317 |
checkArcIds(adaptor); |
|
318 |
|
|
319 |
checkGraphNodeMap(adaptor); |
|
320 |
checkGraphArcMap(adaptor); |
|
321 |
} |
|
322 |
|
|
323 |
void checkUndirector() { |
|
324 |
checkConcept<concepts::Graph, Undirector<concepts::Digraph> >(); |
|
325 |
|
|
326 |
typedef ListDigraph Digraph; |
|
327 |
typedef Undirector<Digraph> Adaptor; |
|
328 |
|
|
329 |
Digraph digraph; |
|
330 |
Adaptor adaptor(digraph); |
|
331 |
|
|
332 |
Digraph::Node n1 = digraph.addNode(); |
|
333 |
Digraph::Node n2 = digraph.addNode(); |
|
334 |
Digraph::Node n3 = digraph.addNode(); |
|
335 |
|
|
336 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
|
337 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
|
338 |
Digraph::Arc a3 = digraph.addArc(n2, n3); |
|
339 |
|
|
340 |
checkGraphNodeList(adaptor, 3); |
|
341 |
checkGraphArcList(adaptor, 6); |
|
342 |
checkGraphEdgeList(adaptor, 3); |
|
343 |
checkGraphConArcList(adaptor, 6); |
|
344 |
checkGraphConEdgeList(adaptor, 3); |
|
345 |
|
|
346 |
checkGraphOutArcList(adaptor, n1, 2); |
|
347 |
checkGraphOutArcList(adaptor, n2, 2); |
|
348 |
checkGraphOutArcList(adaptor, n3, 2); |
|
349 |
|
|
350 |
checkGraphInArcList(adaptor, n1, 2); |
|
351 |
checkGraphInArcList(adaptor, n2, 2); |
|
352 |
checkGraphInArcList(adaptor, n3, 2); |
|
353 |
|
|
354 |
checkGraphIncEdgeList(adaptor, n1, 2); |
|
355 |
checkGraphIncEdgeList(adaptor, n2, 2); |
|
356 |
checkGraphIncEdgeList(adaptor, n3, 2); |
|
357 |
|
|
358 |
checkNodeIds(adaptor); |
|
359 |
checkArcIds(adaptor); |
|
360 |
checkEdgeIds(adaptor); |
|
361 |
|
|
362 |
checkGraphNodeMap(adaptor); |
|
363 |
checkGraphArcMap(adaptor); |
|
364 |
checkGraphEdgeMap(adaptor); |
|
365 |
|
|
366 |
for (Adaptor::EdgeIt e(adaptor); e != INVALID; ++e) { |
|
367 |
check(adaptor.u(e) == digraph.source(e), "Wrong undir"); |
|
368 |
check(adaptor.v(e) == digraph.target(e), "Wrong undir"); |
|
369 |
} |
|
370 |
|
|
371 |
} |
|
372 |
|
|
373 |
void checkResidual() { |
|
374 |
checkConcept<concepts::Digraph, |
|
375 |
Residual<concepts::Digraph, |
|
376 |
concepts::Digraph::ArcMap<int>, |
|
377 |
concepts::Digraph::ArcMap<int> > >(); |
|
378 |
|
|
379 |
typedef ListDigraph Digraph; |
|
380 |
typedef Digraph::ArcMap<int> IntArcMap; |
|
381 |
typedef Residual<Digraph, IntArcMap> Adaptor; |
|
382 |
|
|
383 |
Digraph digraph; |
|
384 |
IntArcMap capacity(digraph), flow(digraph); |
|
385 |
Adaptor adaptor(digraph, capacity, flow); |
|
386 |
|
|
387 |
Digraph::Node n1 = digraph.addNode(); |
|
388 |
Digraph::Node n2 = digraph.addNode(); |
|
389 |
Digraph::Node n3 = digraph.addNode(); |
|
390 |
Digraph::Node n4 = digraph.addNode(); |
|
391 |
|
|
392 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
|
393 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
|
394 |
Digraph::Arc a3 = digraph.addArc(n1, n4); |
|
395 |
Digraph::Arc a4 = digraph.addArc(n2, n3); |
|
396 |
Digraph::Arc a5 = digraph.addArc(n2, n4); |
|
397 |
Digraph::Arc a6 = digraph.addArc(n3, n4); |
|
398 |
|
|
399 |
capacity[a1] = 8; |
|
400 |
capacity[a2] = 6; |
|
401 |
capacity[a3] = 4; |
|
402 |
capacity[a4] = 4; |
|
403 |
capacity[a5] = 6; |
|
404 |
capacity[a6] = 10; |
|
405 |
|
|
406 |
for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) { |
|
407 |
flow[a] = 0; |
|
408 |
} |
|
409 |
|
|
410 |
checkGraphNodeList(adaptor, 4); |
|
411 |
checkGraphArcList(adaptor, 6); |
|
412 |
checkGraphConArcList(adaptor, 6); |
|
413 |
|
|
414 |
checkGraphOutArcList(adaptor, n1, 3); |
|
415 |
checkGraphOutArcList(adaptor, n2, 2); |
|
416 |
checkGraphOutArcList(adaptor, n3, 1); |
|
417 |
checkGraphOutArcList(adaptor, n4, 0); |
|
418 |
|
|
419 |
checkGraphInArcList(adaptor, n1, 0); |
|
420 |
checkGraphInArcList(adaptor, n2, 1); |
|
421 |
checkGraphInArcList(adaptor, n3, 2); |
|
422 |
checkGraphInArcList(adaptor, n4, 3); |
|
423 |
|
|
424 |
for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) { |
|
425 |
flow[a] = capacity[a] / 2; |
|
426 |
} |
|
427 |
|
|
428 |
checkGraphNodeList(adaptor, 4); |
|
429 |
checkGraphArcList(adaptor, 12); |
|
430 |
checkGraphConArcList(adaptor, 12); |
|
431 |
|
|
432 |
checkGraphOutArcList(adaptor, n1, 3); |
|
433 |
checkGraphOutArcList(adaptor, n2, 3); |
|
434 |
checkGraphOutArcList(adaptor, n3, 3); |
|
435 |
checkGraphOutArcList(adaptor, n4, 3); |
|
436 |
|
|
437 |
checkGraphInArcList(adaptor, n1, 3); |
|
438 |
checkGraphInArcList(adaptor, n2, 3); |
|
439 |
checkGraphInArcList(adaptor, n3, 3); |
|
440 |
checkGraphInArcList(adaptor, n4, 3); |
|
441 |
|
|
442 |
checkNodeIds(adaptor); |
|
443 |
checkArcIds(adaptor); |
|
444 |
|
|
445 |
checkGraphNodeMap(adaptor); |
|
446 |
checkGraphArcMap(adaptor); |
|
447 |
|
|
448 |
for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) { |
|
449 |
flow[a] = capacity[a]; |
|
450 |
} |
|
451 |
|
|
452 |
checkGraphNodeList(adaptor, 4); |
|
453 |
checkGraphArcList(adaptor, 6); |
|
454 |
checkGraphConArcList(adaptor, 6); |
|
455 |
|
|
456 |
checkGraphOutArcList(adaptor, n1, 0); |
|
457 |
checkGraphOutArcList(adaptor, n2, 1); |
|
458 |
checkGraphOutArcList(adaptor, n3, 2); |
|
459 |
checkGraphOutArcList(adaptor, n4, 3); |
|
460 |
|
|
461 |
checkGraphInArcList(adaptor, n1, 3); |
|
462 |
checkGraphInArcList(adaptor, n2, 2); |
|
463 |
checkGraphInArcList(adaptor, n3, 1); |
|
464 |
checkGraphInArcList(adaptor, n4, 0); |
|
465 |
|
|
466 |
for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) { |
|
467 |
flow[a] = 0; |
|
468 |
} |
|
469 |
|
|
470 |
int flow_value = 0; |
|
471 |
while (true) { |
|
472 |
|
|
473 |
Bfs<Adaptor> bfs(adaptor); |
|
474 |
bfs.run(n1, n4); |
|
475 |
|
|
476 |
if (!bfs.reached(n4)) break; |
|
477 |
|
|
478 |
Path<Adaptor> p = bfs.path(n4); |
|
479 |
|
|
480 |
int min = std::numeric_limits<int>::max(); |
|
481 |
for (Path<Adaptor>::ArcIt a(p); a != INVALID; ++a) { |
|
482 |
if (adaptor.residualCapacity(a) < min) |
|
483 |
min = adaptor.residualCapacity(a); |
|
484 |
} |
|
485 |
|
|
486 |
for (Path<Adaptor>::ArcIt a(p); a != INVALID; ++a) { |
|
487 |
adaptor.augment(a, min); |
|
488 |
} |
|
489 |
flow_value += min; |
|
490 |
} |
|
491 |
|
|
492 |
check(flow_value == 18, "Wrong flow with res graph adaptor"); |
|
493 |
|
|
494 |
} |
|
495 |
|
|
496 |
void checkSplitNodes() { |
|
497 |
checkConcept<concepts::Digraph, SplitNodes<concepts::Digraph> >(); |
|
498 |
|
|
499 |
typedef ListDigraph Digraph; |
|
500 |
typedef SplitNodes<Digraph> Adaptor; |
|
501 |
|
|
502 |
Digraph digraph; |
|
503 |
Adaptor adaptor(digraph); |
|
504 |
|
|
505 |
Digraph::Node n1 = digraph.addNode(); |
|
506 |
Digraph::Node n2 = digraph.addNode(); |
|
507 |
Digraph::Node n3 = digraph.addNode(); |
|
508 |
|
|
509 |
Digraph::Arc a1 = digraph.addArc(n1, n2); |
|
510 |
Digraph::Arc a2 = digraph.addArc(n1, n3); |
|
511 |
Digraph::Arc a3 = digraph.addArc(n2, n3); |
|
512 |
|
|
513 |
checkGraphNodeList(adaptor, 6); |
|
514 |
checkGraphArcList(adaptor, 6); |
|
515 |
checkGraphConArcList(adaptor, 6); |
|
516 |
|
|
517 |
checkGraphOutArcList(adaptor, adaptor.inNode(n1), 1); |
|
518 |
checkGraphOutArcList(adaptor, adaptor.outNode(n1), 2); |
|
519 |
checkGraphOutArcList(adaptor, adaptor.inNode(n2), 1); |
|
520 |
checkGraphOutArcList(adaptor, adaptor.outNode(n2), 1); |
|
521 |
checkGraphOutArcList(adaptor, adaptor.inNode(n3), 1); |
|
522 |
checkGraphOutArcList(adaptor, adaptor.outNode(n3), 0); |
|
523 |
|
|
524 |
checkGraphInArcList(adaptor, adaptor.inNode(n1), 0); |
|
525 |
checkGraphInArcList(adaptor, adaptor.outNode(n1), 1); |
|
526 |
checkGraphInArcList(adaptor, adaptor.inNode(n2), 1); |
|
527 |
checkGraphInArcList(adaptor, adaptor.outNode(n2), 1); |
|
528 |
checkGraphInArcList(adaptor, adaptor.inNode(n3), 2); |
|
529 |
checkGraphInArcList(adaptor, adaptor.outNode(n3), 1); |
|
530 |
|
|
531 |
checkNodeIds(adaptor); |
|
532 |
checkArcIds(adaptor); |
|
533 |
|
|
534 |
checkGraphNodeMap(adaptor); |
|
535 |
checkGraphArcMap(adaptor); |
|
536 |
|
|
537 |
for (Adaptor::ArcIt a(adaptor); a != INVALID; ++a) { |
|
538 |
if (adaptor.origArc(a)) { |
|
539 |
Digraph::Arc oa = a; |
|
540 |
check(adaptor.source(a) == adaptor.outNode(digraph.source(oa)), |
|
541 |
"Wrong split"); |
|
542 |
check(adaptor.target(a) == adaptor.inNode(digraph.target(oa)), |
|
543 |
"Wrong split"); |
|
544 |
} else { |
|
545 |
Digraph::Node on = a; |
|
546 |
check(adaptor.source(a) == adaptor.inNode(on), "Wrong split"); |
|
547 |
check(adaptor.target(a) == adaptor.outNode(on), "Wrong split"); |
|
548 |
} |
|
549 |
} |
|
550 |
} |
|
551 |
|
|
552 |
void checkSubGraph() { |
|
553 |
checkConcept<concepts::Graph, |
|
554 |
SubGraph<concepts::Graph, |
|
555 |
concepts::Graph::NodeMap<bool>, |
|
556 |
concepts::Graph::EdgeMap<bool> > >(); |
|
557 |
|
|
558 |
typedef ListGraph Graph; |
|
559 |
typedef Graph::NodeMap<bool> NodeFilter; |
|
560 |
typedef Graph::EdgeMap<bool> EdgeFilter; |
|
561 |
typedef SubGraph<Graph, NodeFilter, EdgeFilter> Adaptor; |
|
562 |
|
|
563 |
Graph graph; |
|
564 |
NodeFilter node_filter(graph); |
|
565 |
EdgeFilter edge_filter(graph); |
|
566 |
Adaptor adaptor(graph, node_filter, edge_filter); |
|
567 |
|
|
568 |
Graph::Node n1 = graph.addNode(); |
|
569 |
Graph::Node n2 = graph.addNode(); |
|
570 |
Graph::Node n3 = graph.addNode(); |
|
571 |
Graph::Node n4 = graph.addNode(); |
|
572 |
|
|
573 |
Graph::Edge e1 = graph.addEdge(n1, n2); |
|
574 |
Graph::Edge e2 = graph.addEdge(n1, n3); |
|
575 |
Graph::Edge e3 = graph.addEdge(n2, n3); |
|
576 |
Graph::Edge e4 = graph.addEdge(n3, n4); |
|
577 |
|
|
578 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = node_filter[n4] = true; |
|
579 |
edge_filter[e1] = edge_filter[e2] = edge_filter[e3] = edge_filter[e4] = true; |
|
580 |
|
|
581 |
checkGraphNodeList(adaptor, 4); |
|
582 |
checkGraphArcList(adaptor, 8); |
|
583 |
checkGraphEdgeList(adaptor, 4); |
|
584 |
checkGraphConArcList(adaptor, 8); |
|
585 |
checkGraphConEdgeList(adaptor, 4); |
|
586 |
|
|
587 |
checkGraphOutArcList(adaptor, n1, 2); |
|
588 |
checkGraphOutArcList(adaptor, n2, 2); |
|
589 |
checkGraphOutArcList(adaptor, n3, 3); |
|
590 |
checkGraphOutArcList(adaptor, n4, 1); |
|
591 |
|
|
592 |
checkGraphInArcList(adaptor, n1, 2); |
|
593 |
checkGraphInArcList(adaptor, n2, 2); |
|
594 |
checkGraphInArcList(adaptor, n3, 3); |
|
595 |
checkGraphInArcList(adaptor, n4, 1); |
|
596 |
|
|
597 |
checkGraphIncEdgeList(adaptor, n1, 2); |
|
598 |
checkGraphIncEdgeList(adaptor, n2, 2); |
|
599 |
checkGraphIncEdgeList(adaptor, n3, 3); |
|
600 |
checkGraphIncEdgeList(adaptor, n4, 1); |
|
601 |
|
|
602 |
checkNodeIds(adaptor); |
|
603 |
checkArcIds(adaptor); |
|
604 |
checkEdgeIds(adaptor); |
|
605 |
|
|
606 |
checkGraphNodeMap(adaptor); |
|
607 |
checkGraphArcMap(adaptor); |
|
608 |
checkGraphEdgeMap(adaptor); |
|
609 |
|
|
610 |
edge_filter[e2] = false; |
|
611 |
|
|
612 |
checkGraphNodeList(adaptor, 4); |
|
613 |
checkGraphArcList(adaptor, 6); |
|
614 |
checkGraphEdgeList(adaptor, 3); |
|
615 |
checkGraphConArcList(adaptor, 6); |
|
616 |
checkGraphConEdgeList(adaptor, 3); |
|
617 |
|
|
618 |
checkGraphOutArcList(adaptor, n1, 1); |
|
619 |
checkGraphOutArcList(adaptor, n2, 2); |
|
620 |
checkGraphOutArcList(adaptor, n3, 2); |
|
621 |
checkGraphOutArcList(adaptor, n4, 1); |
|
622 |
|
|
623 |
checkGraphInArcList(adaptor, n1, 1); |
|
624 |
checkGraphInArcList(adaptor, n2, 2); |
|
625 |
checkGraphInArcList(adaptor, n3, 2); |
|
626 |
checkGraphInArcList(adaptor, n4, 1); |
|
627 |
|
|
628 |
checkGraphIncEdgeList(adaptor, n1, 1); |
|
629 |
checkGraphIncEdgeList(adaptor, n2, 2); |
|
630 |
checkGraphIncEdgeList(adaptor, n3, 2); |
|
631 |
checkGraphIncEdgeList(adaptor, n4, 1); |
|
632 |
|
|
633 |
checkNodeIds(adaptor); |
|
634 |
checkArcIds(adaptor); |
|
635 |
checkEdgeIds(adaptor); |
|
636 |
|
|
637 |
checkGraphNodeMap(adaptor); |
|
638 |
checkGraphArcMap(adaptor); |
|
639 |
checkGraphEdgeMap(adaptor); |
|
640 |
|
|
641 |
node_filter[n1] = false; |
|
642 |
|
|
643 |
checkGraphNodeList(adaptor, 3); |
|
644 |
checkGraphArcList(adaptor, 4); |
|
645 |
checkGraphEdgeList(adaptor, 2); |
|
646 |
checkGraphConArcList(adaptor, 4); |
|
647 |
checkGraphConEdgeList(adaptor, 2); |
|
648 |
|
|
649 |
checkGraphOutArcList(adaptor, n2, 1); |
|
650 |
checkGraphOutArcList(adaptor, n3, 2); |
|
651 |
checkGraphOutArcList(adaptor, n4, 1); |
|
652 |
|
|
653 |
checkGraphInArcList(adaptor, n2, 1); |
|
654 |
checkGraphInArcList(adaptor, n3, 2); |
|
655 |
checkGraphInArcList(adaptor, n4, 1); |
|
656 |
|
|
657 |
checkGraphIncEdgeList(adaptor, n2, 1); |
|
658 |
checkGraphIncEdgeList(adaptor, n3, 2); |
|
659 |
checkGraphIncEdgeList(adaptor, n4, 1); |
|
660 |
|
|
661 |
checkNodeIds(adaptor); |
|
662 |
checkArcIds(adaptor); |
|
663 |
checkEdgeIds(adaptor); |
|
664 |
|
|
665 |
checkGraphNodeMap(adaptor); |
|
666 |
checkGraphArcMap(adaptor); |
|
667 |
checkGraphEdgeMap(adaptor); |
|
668 |
|
|
669 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = node_filter[n4] = false; |
|
670 |
edge_filter[e1] = edge_filter[e2] = edge_filter[e3] = edge_filter[e4] = false; |
|
671 |
|
|
672 |
checkGraphNodeList(adaptor, 0); |
|
673 |
checkGraphArcList(adaptor, 0); |
|
674 |
checkGraphEdgeList(adaptor, 0); |
|
675 |
checkGraphConArcList(adaptor, 0); |
|
676 |
checkGraphConEdgeList(adaptor, 0); |
|
677 |
|
|
678 |
checkNodeIds(adaptor); |
|
679 |
checkArcIds(adaptor); |
|
680 |
checkEdgeIds(adaptor); |
|
681 |
|
|
682 |
checkGraphNodeMap(adaptor); |
|
683 |
checkGraphArcMap(adaptor); |
|
684 |
checkGraphEdgeMap(adaptor); |
|
685 |
} |
|
686 |
|
|
687 |
void checkFilterNodes2() { |
|
688 |
checkConcept<concepts::Graph, |
|
689 |
FilterNodes<concepts::Graph, |
|
690 |
concepts::Graph::NodeMap<bool> > >(); |
|
691 |
|
|
692 |
typedef ListGraph Graph; |
|
693 |
typedef Graph::NodeMap<bool> NodeFilter; |
|
694 |
typedef FilterNodes<Graph, NodeFilter> Adaptor; |
|
695 |
|
|
696 |
Graph graph; |
|
697 |
NodeFilter node_filter(graph); |
|
698 |
Adaptor adaptor(graph, node_filter); |
|
699 |
|
|
700 |
Graph::Node n1 = graph.addNode(); |
|
701 |
Graph::Node n2 = graph.addNode(); |
|
702 |
Graph::Node n3 = graph.addNode(); |
|
703 |
Graph::Node n4 = graph.addNode(); |
|
704 |
|
|
705 |
Graph::Edge e1 = graph.addEdge(n1, n2); |
|
706 |
Graph::Edge e2 = graph.addEdge(n1, n3); |
|
707 |
Graph::Edge e3 = graph.addEdge(n2, n3); |
|
708 |
Graph::Edge e4 = graph.addEdge(n3, n4); |
|
709 |
|
|
710 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = node_filter[n4] = true; |
|
711 |
|
|
712 |
checkGraphNodeList(adaptor, 4); |
|
713 |
checkGraphArcList(adaptor, 8); |
|
714 |
checkGraphEdgeList(adaptor, 4); |
|
715 |
checkGraphConArcList(adaptor, 8); |
|
716 |
checkGraphConEdgeList(adaptor, 4); |
|
717 |
|
|
718 |
checkGraphOutArcList(adaptor, n1, 2); |
|
719 |
checkGraphOutArcList(adaptor, n2, 2); |
|
720 |
checkGraphOutArcList(adaptor, n3, 3); |
|
721 |
checkGraphOutArcList(adaptor, n4, 1); |
|
722 |
|
|
723 |
checkGraphInArcList(adaptor, n1, 2); |
|
724 |
checkGraphInArcList(adaptor, n2, 2); |
|
725 |
checkGraphInArcList(adaptor, n3, 3); |
|
726 |
checkGraphInArcList(adaptor, n4, 1); |
|
727 |
|
|
728 |
checkGraphIncEdgeList(adaptor, n1, 2); |
|
729 |
checkGraphIncEdgeList(adaptor, n2, 2); |
|
730 |
checkGraphIncEdgeList(adaptor, n3, 3); |
|
731 |
checkGraphIncEdgeList(adaptor, n4, 1); |
|
732 |
|
|
733 |
checkNodeIds(adaptor); |
|
734 |
checkArcIds(adaptor); |
|
735 |
checkEdgeIds(adaptor); |
|
736 |
|
|
737 |
checkGraphNodeMap(adaptor); |
|
738 |
checkGraphArcMap(adaptor); |
|
739 |
checkGraphEdgeMap(adaptor); |
|
740 |
|
|
741 |
node_filter[n1] = false; |
|
742 |
|
|
743 |
checkGraphNodeList(adaptor, 3); |
|
744 |
checkGraphArcList(adaptor, 4); |
|
745 |
checkGraphEdgeList(adaptor, 2); |
|
746 |
checkGraphConArcList(adaptor, 4); |
|
747 |
checkGraphConEdgeList(adaptor, 2); |
|
748 |
|
|
749 |
checkGraphOutArcList(adaptor, n2, 1); |
|
750 |
checkGraphOutArcList(adaptor, n3, 2); |
|
751 |
checkGraphOutArcList(adaptor, n4, 1); |
|
752 |
|
|
753 |
checkGraphInArcList(adaptor, n2, 1); |
|
754 |
checkGraphInArcList(adaptor, n3, 2); |
|
755 |
checkGraphInArcList(adaptor, n4, 1); |
|
756 |
|
|
757 |
checkGraphIncEdgeList(adaptor, n2, 1); |
|
758 |
checkGraphIncEdgeList(adaptor, n3, 2); |
|
759 |
checkGraphIncEdgeList(adaptor, n4, 1); |
|
760 |
|
|
761 |
checkNodeIds(adaptor); |
|
762 |
checkArcIds(adaptor); |
|
763 |
checkEdgeIds(adaptor); |
|
764 |
|
|
765 |
checkGraphNodeMap(adaptor); |
|
766 |
checkGraphArcMap(adaptor); |
|
767 |
checkGraphEdgeMap(adaptor); |
|
768 |
|
|
769 |
node_filter[n1] = node_filter[n2] = node_filter[n3] = node_filter[n4] = false; |
|
770 |
|
|
771 |
checkGraphNodeList(adaptor, 0); |
|
772 |
checkGraphArcList(adaptor, 0); |
|
773 |
checkGraphEdgeList(adaptor, 0); |
|
774 |
checkGraphConArcList(adaptor, 0); |
|
775 |
checkGraphConEdgeList(adaptor, 0); |
|
776 |
|
|
777 |
checkNodeIds(adaptor); |
|
778 |
checkArcIds(adaptor); |
|
779 |
checkEdgeIds(adaptor); |
|
780 |
|
|
781 |
checkGraphNodeMap(adaptor); |
|
782 |
checkGraphArcMap(adaptor); |
|
783 |
checkGraphEdgeMap(adaptor); |
|
784 |
} |
|
785 |
|
|
786 |
void checkFilterEdges() { |
|
787 |
checkConcept<concepts::Graph, |
|
788 |
FilterEdges<concepts::Graph, |
|
789 |
concepts::Graph::EdgeMap<bool> > >(); |
|
790 |
|
|
791 |
typedef ListGraph Graph; |
|
792 |
typedef Graph::EdgeMap<bool> EdgeFilter; |
|
793 |
typedef FilterEdges<Graph, EdgeFilter> Adaptor; |
|
794 |
|
|
795 |
Graph graph; |
|
796 |
EdgeFilter edge_filter(graph); |
|
797 |
Adaptor adaptor(graph, edge_filter); |
|
798 |
|
|
799 |
Graph::Node n1 = graph.addNode(); |
|
800 |
Graph::Node n2 = graph.addNode(); |
|
801 |
Graph::Node n3 = graph.addNode(); |
|
802 |
Graph::Node n4 = graph.addNode(); |
|
803 |
|
|
804 |
Graph::Edge e1 = graph.addEdge(n1, n2); |
|
805 |
Graph::Edge e2 = graph.addEdge(n1, n3); |
|
806 |
Graph::Edge e3 = graph.addEdge(n2, n3); |
|
807 |
Graph::Edge e4 = graph.addEdge(n3, n4); |
|
808 |
|
|
809 |
edge_filter[e1] = edge_filter[e2] = edge_filter[e3] = edge_filter[e4] = true; |
|
810 |
|
|
811 |
checkGraphNodeList(adaptor, 4); |
|
812 |
checkGraphArcList(adaptor, 8); |
|
813 |
checkGraphEdgeList(adaptor, 4); |
|
814 |
checkGraphConArcList(adaptor, 8); |
|
815 |
checkGraphConEdgeList(adaptor, 4); |
|
816 |
|
|
817 |
checkGraphOutArcList(adaptor, n1, 2); |
|
818 |
checkGraphOutArcList(adaptor, n2, 2); |
|
819 |
checkGraphOutArcList(adaptor, n3, 3); |
|
820 |
checkGraphOutArcList(adaptor, n4, 1); |
|
821 |
|
|
822 |
checkGraphInArcList(adaptor, n1, 2); |
|
823 |
checkGraphInArcList(adaptor, n2, 2); |
|
824 |
checkGraphInArcList(adaptor, n3, 3); |
|
825 |
checkGraphInArcList(adaptor, n4, 1); |
|
826 |
|
|
827 |
checkGraphIncEdgeList(adaptor, n1, 2); |
|
828 |
checkGraphIncEdgeList(adaptor, n2, 2); |
|
829 |
checkGraphIncEdgeList(adaptor, n3, 3); |
|
830 |
checkGraphIncEdgeList(adaptor, n4, 1); |
|
831 |
|
|
832 |
checkNodeIds(adaptor); |
|
833 |
checkArcIds(adaptor); |
|
834 |
checkEdgeIds(adaptor); |
|
835 |
|
|
836 |
checkGraphNodeMap(adaptor); |
|
837 |
checkGraphArcMap(adaptor); |
|
838 |
checkGraphEdgeMap(adaptor); |
|
839 |
|
|
840 |
edge_filter[e2] = false; |
|
841 |
|
|
842 |
checkGraphNodeList(adaptor, 4); |
|
843 |
checkGraphArcList(adaptor, 6); |
|
844 |
checkGraphEdgeList(adaptor, 3); |
|
845 |
checkGraphConArcList(adaptor, 6); |
|
846 |
checkGraphConEdgeList(adaptor, 3); |
|
847 |
|
|
848 |
checkGraphOutArcList(adaptor, n1, 1); |
|
849 |
checkGraphOutArcList(adaptor, n2, 2); |
|
850 |
checkGraphOutArcList(adaptor, n3, 2); |
|
851 |
checkGraphOutArcList(adaptor, n4, 1); |
|
852 |
|
|
853 |
checkGraphInArcList(adaptor, n1, 1); |
|
854 |
checkGraphInArcList(adaptor, n2, 2); |
|
855 |
checkGraphInArcList(adaptor, n3, 2); |
|
856 |
checkGraphInArcList(adaptor, n4, 1); |
|
857 |
|
|
858 |
checkGraphIncEdgeList(adaptor, n1, 1); |
|
859 |
checkGraphIncEdgeList(adaptor, n2, 2); |
|
860 |
checkGraphIncEdgeList(adaptor, n3, 2); |
|
861 |
checkGraphIncEdgeList(adaptor, n4, 1); |
|
862 |
|
|
863 |
checkNodeIds(adaptor); |
|
864 |
checkArcIds(adaptor); |
|
865 |
checkEdgeIds(adaptor); |
|
866 |
|
|
867 |
checkGraphNodeMap(adaptor); |
|
868 |
checkGraphArcMap(adaptor); |
|
869 |
checkGraphEdgeMap(adaptor); |
|
870 |
|
|
871 |
edge_filter[e1] = edge_filter[e2] = edge_filter[e3] = edge_filter[e4] = false; |
|
872 |
|
|
873 |
checkGraphNodeList(adaptor, 4); |
|
874 |
checkGraphArcList(adaptor, 0); |
|
875 |
checkGraphEdgeList(adaptor, 0); |
|
876 |
checkGraphConArcList(adaptor, 0); |
|
877 |
checkGraphConEdgeList(adaptor, 0); |
|
878 |
|
|
879 |
checkNodeIds(adaptor); |
|
880 |
checkArcIds(adaptor); |
|
881 |
checkEdgeIds(adaptor); |
|
882 |
|
|
883 |
checkGraphNodeMap(adaptor); |
|
884 |
checkGraphArcMap(adaptor); |
|
885 |
checkGraphEdgeMap(adaptor); |
|
886 |
} |
|
887 |
|
|
888 |
void checkOrienter() { |
|
889 |
checkConcept<concepts::Digraph, |
|
890 |
Orienter<concepts::Graph, concepts::Graph::EdgeMap<bool> > >(); |
|
891 |
|
|
892 |
typedef ListGraph Graph; |
|
893 |
typedef ListGraph::EdgeMap<bool> DirMap; |
|
894 |
typedef Orienter<Graph> Adaptor; |
|
895 |
|
|
896 |
Graph graph; |
|
897 |
DirMap dir(graph, true); |
|
898 |
Adaptor adaptor(graph, dir); |
|
899 |
|
|
900 |
Graph::Node n1 = graph.addNode(); |
|
901 |
Graph::Node n2 = graph.addNode(); |
|
902 |
Graph::Node n3 = graph.addNode(); |
|
903 |
|
|
904 |
Graph::Edge e1 = graph.addEdge(n1, n2); |
|
905 |
Graph::Edge e2 = graph.addEdge(n1, n3); |
|
906 |
Graph::Edge e3 = graph.addEdge(n2, n3); |
|
907 |
|
|
908 |
checkGraphNodeList(adaptor, 3); |
|
909 |
checkGraphArcList(adaptor, 3); |
|
910 |
checkGraphConArcList(adaptor, 3); |
|
911 |
|
|
912 |
{ |
|
913 |
dir[e1] = true; |
|
914 |
Adaptor::Node u = adaptor.source(e1); |
|
915 |
Adaptor::Node v = adaptor.target(e1); |
|
916 |
|
|
917 |
dir[e1] = false; |
|
918 |
check (u == adaptor.target(e1), "Wrong dir"); |
|
919 |
check (v == adaptor.source(e1), "Wrong dir"); |
|
920 |
|
|
921 |
check ((u == n1 && v == n2) || (u == n2 && v == n1), "Wrong dir"); |
|
922 |
dir[e1] = n1 == u; |
|
923 |
} |
|
924 |
|
|
925 |
{ |
|
926 |
dir[e2] = true; |
|
927 |
Adaptor::Node u = adaptor.source(e2); |
|
928 |
Adaptor::Node v = adaptor.target(e2); |
|
929 |
|
|
930 |
dir[e2] = false; |
|
931 |
check (u == adaptor.target(e2), "Wrong dir"); |
|
932 |
check (v == adaptor.source(e2), "Wrong dir"); |
|
933 |
|
|
934 |
check ((u == n1 && v == n3) || (u == n3 && v == n1), "Wrong dir"); |
|
935 |
dir[e2] = n3 == u; |
|
936 |
} |
|
937 |
|
|
938 |
{ |
|
939 |
dir[e3] = true; |
|
940 |
Adaptor::Node u = adaptor.source(e3); |
|
941 |
Adaptor::Node v = adaptor.target(e3); |
|
942 |
|
|
943 |
dir[e3] = false; |
|
944 |
check (u == adaptor.target(e3), "Wrong dir"); |
|
945 |
check (v == adaptor.source(e3), "Wrong dir"); |
|
946 |
|
|
947 |
check ((u == n2 && v == n3) || (u == n3 && v == n2), "Wrong dir"); |
|
948 |
dir[e3] = n2 == u; |
|
949 |
} |
|
950 |
|
|
951 |
checkGraphOutArcList(adaptor, n1, 1); |
|
952 |
checkGraphOutArcList(adaptor, n2, 1); |
|
953 |
checkGraphOutArcList(adaptor, n3, 1); |
|
954 |
|
|
955 |
checkGraphInArcList(adaptor, n1, 1); |
|
956 |
checkGraphInArcList(adaptor, n2, 1); |
|
957 |
checkGraphInArcList(adaptor, n3, 1); |
|
958 |
|
|
959 |
checkNodeIds(adaptor); |
|
960 |
checkArcIds(adaptor); |
|
961 |
|
|
962 |
checkGraphNodeMap(adaptor); |
|
963 |
checkGraphArcMap(adaptor); |
|
964 |
|
|
965 |
} |
|
966 |
|
|
967 |
|
|
968 |
int main(int, const char **) { |
|
969 |
|
|
970 |
checkReverseDigraph(); |
|
971 |
checkSubDigraph(); |
|
972 |
checkFilterNodes1(); |
|
973 |
checkFilterArcs(); |
|
974 |
checkUndirector(); |
|
975 |
checkResidual(); |
|
976 |
checkSplitNodes(); |
|
977 |
|
|
978 |
checkSubGraph(); |
|
979 |
checkFilterNodes2(); |
|
980 |
checkFilterEdges(); |
|
981 |
checkOrienter(); |
|
982 |
|
|
983 |
return 0; |
|
984 |
} |
... | ... |
@@ -64,2 +64,75 @@ |
64 | 64 |
/** |
65 |
@defgroup graph_adaptors Adaptor Classes for graphs |
|
66 |
@ingroup graphs |
|
67 |
\brief This group contains several adaptor classes for digraphs and graphs |
|
68 |
|
|
69 |
The main parts of LEMON are the different graph structures, generic |
|
70 |
graph algorithms, graph concepts which couple these, and graph |
|
71 |
adaptors. While the previous notions are more or less clear, the |
|
72 |
latter one needs further explanation. Graph adaptors are graph classes |
|
73 |
which serve for considering graph structures in different ways. |
|
74 |
|
|
75 |
A short example makes this much clearer. Suppose that we have an |
|
76 |
instance \c g of a directed graph type say ListDigraph and an algorithm |
|
77 |
\code |
|
78 |
template <typename Digraph> |
|
79 |
int algorithm(const Digraph&); |
|
80 |
\endcode |
|
81 |
is needed to run on the reverse oriented graph. It may be expensive |
|
82 |
(in time or in memory usage) to copy \c g with the reversed |
|
83 |
arcs. In this case, an adaptor class is used, which (according |
|
84 |
to LEMON digraph concepts) works as a digraph. The adaptor uses the |
|
85 |
original digraph structure and digraph operations when methods of the |
|
86 |
reversed oriented graph are called. This means that the adaptor have |
|
87 |
minor memory usage, and do not perform sophisticated algorithmic |
|
88 |
actions. The purpose of it is to give a tool for the cases when a |
|
89 |
graph have to be used in a specific alteration. If this alteration is |
|
90 |
obtained by a usual construction like filtering the arc-set or |
|
91 |
considering a new orientation, then an adaptor is worthwhile to use. |
|
92 |
To come back to the reverse oriented graph, in this situation |
|
93 |
\code |
|
94 |
template<typename Digraph> class ReverseDigraph; |
|
95 |
\endcode |
|
96 |
template class can be used. The code looks as follows |
|
97 |
\code |
|
98 |
ListDigraph g; |
|
99 |
ReverseDigraph<ListGraph> rg(g); |
|
100 |
int result = algorithm(rg); |
|
101 |
\endcode |
|
102 |
After running the algorithm, the original graph \c g is untouched. |
|
103 |
This techniques gives rise to an elegant code, and based on stable |
|
104 |
graph adaptors, complex algorithms can be implemented easily. |
|
105 |
|
|
106 |
In flow, circulation and bipartite matching problems, the residual |
|
107 |
graph is of particular importance. Combining an adaptor implementing |
|
108 |
this, shortest path algorithms and minimum mean cycle algorithms, |
|
109 |
a range of weighted and cardinality optimization algorithms can be |
|
110 |
obtained. For other examples, the interested user is referred to the |
|
111 |
detailed documentation of particular adaptors. |
|
112 |
|
|
113 |
The behavior of graph adaptors can be very different. Some of them keep |
|
114 |
capabilities of the original graph while in other cases this would be |
|
115 |
meaningless. This means that the concepts that they are models of depend |
|
116 |
on the graph adaptor, and the wrapped graph(s). |
|
117 |
If an arc of \c rg is deleted, this is carried out by deleting the |
|
118 |
corresponding arc of \c g, thus the adaptor modifies the original graph. |
|
119 |
|
|
120 |
But for a residual graph, this operation has no sense. |
|
121 |
Let us stand one more example here to simplify your work. |
|
122 |
RevGraphAdaptor has constructor |
|
123 |
\code |
|
124 |
ReverseDigraph(Digraph& digraph); |
|
125 |
\endcode |
|
126 |
This means that in a situation, when a <tt>const ListDigraph&</tt> |
|
127 |
reference to a graph is given, then it have to be instantiated with |
|
128 |
<tt>Digraph=const ListDigraph</tt>. |
|
129 |
\code |
|
130 |
int algorithm1(const ListDigraph& g) { |
|
131 |
RevGraphAdaptor<const ListDigraph> rg(g); |
|
132 |
return algorithm2(rg); |
|
133 |
} |
|
134 |
\endcode |
|
135 |
*/ |
|
136 |
|
|
137 |
/** |
|
65 | 138 |
@defgroup semi_adaptors Semi-Adaptor Classes for Graphs |
... | ... |
@@ -18,2 +18,3 @@ |
18 | 18 |
lemon_HEADERS += \ |
19 |
lemon/adaptors.h \ |
|
19 | 20 |
lemon/arg_parser.h \ |
... | ... |
@@ -62,2 +63,3 @@ |
62 | 63 |
lemon/bits/enable_if.h \ |
64 |
lemon/bits/graph_adaptor_extender.h \ |
|
63 | 65 |
lemon/bits/graph_extender.h \ |
... | ... |
@@ -66,2 +68,3 @@ |
66 | 68 |
lemon/bits/traits.h \ |
69 |
lemon/bits/variant.h \ |
|
67 | 70 |
lemon/bits/vector_map.h |
... | ... |
@@ -18,2 +18,3 @@ |
18 | 18 |
test/error_test \ |
19 |
test/graph_adaptor_test \ |
|
19 | 20 |
test/graph_copy_test \ |
... | ... |
@@ -46,2 +47,3 @@ |
46 | 47 |
test_error_test_SOURCES = test/error_test.cc |
48 |
test_graph_adaptor_test_SOURCES = test/graph_adaptor_test.cc |
|
47 | 49 |
test_graph_copy_test_SOURCES = test/graph_copy_test.cc |
0 comments (0 inline)