alpar@906
|
1 |
/* -*- C++ -*-
|
ladanyi@1435
|
2 |
* lemon/graph_adaptor.h - Part of LEMON, a generic C++ optimization library
|
alpar@906
|
3 |
*
|
alpar@1875
|
4 |
* Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@1359
|
5 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@906
|
6 |
*
|
alpar@906
|
7 |
* Permission to use, modify and distribute this software is granted
|
alpar@906
|
8 |
* provided that this copyright notice appears in all copies. For
|
alpar@906
|
9 |
* precise terms see the accompanying LICENSE file.
|
alpar@906
|
10 |
*
|
alpar@906
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@906
|
12 |
* express or implied, and with no claim as to its suitability for any
|
alpar@906
|
13 |
* purpose.
|
alpar@906
|
14 |
*
|
alpar@906
|
15 |
*/
|
alpar@906
|
16 |
|
alpar@1401
|
17 |
#ifndef LEMON_GRAPH_ADAPTOR_H
|
alpar@1401
|
18 |
#define LEMON_GRAPH_ADAPTOR_H
|
marci@556
|
19 |
|
alpar@1401
|
20 |
///\ingroup graph_adaptors
|
marci@556
|
21 |
///\file
|
alpar@1401
|
22 |
///\brief Several graph adaptors.
|
marci@556
|
23 |
///
|
alpar@1401
|
24 |
///This file contains several useful graph adaptor functions.
|
marci@556
|
25 |
///
|
marci@556
|
26 |
///\author Marton Makai
|
marci@556
|
27 |
|
alpar@921
|
28 |
#include <lemon/invalid.h>
|
alpar@921
|
29 |
#include <lemon/maps.h>
|
deba@1472
|
30 |
#include <lemon/bits/erasable_graph_extender.h>
|
deba@1472
|
31 |
#include <lemon/bits/clearable_graph_extender.h>
|
deba@1472
|
32 |
#include <lemon/bits/extendable_graph_extender.h>
|
deba@1307
|
33 |
#include <lemon/bits/iterable_graph_extender.h>
|
deba@1472
|
34 |
#include <lemon/bits/alteration_notifier.h>
|
deba@1472
|
35 |
#include <lemon/bits/default_map.h>
|
deba@1791
|
36 |
#include <lemon/bits/graph_extender.h>
|
alpar@774
|
37 |
#include <iostream>
|
marci@556
|
38 |
|
alpar@921
|
39 |
namespace lemon {
|
marci@556
|
40 |
|
klao@1951
|
41 |
///\brief Base type for the Graph Adaptors
|
klao@1951
|
42 |
///\ingroup graph_adaptors
|
klao@1951
|
43 |
///
|
klao@1951
|
44 |
///Base type for the Graph Adaptors
|
klao@1951
|
45 |
///
|
klao@1951
|
46 |
///\warning Graph adaptors are in even
|
klao@1951
|
47 |
///more experimental state than the other
|
klao@1951
|
48 |
///parts of the lib. Use them at you own risk.
|
klao@1951
|
49 |
///
|
klao@1951
|
50 |
///This is the base type for most of LEMON graph adaptors.
|
klao@1951
|
51 |
///This class implements a trivial graph adaptor i.e. it only wraps the
|
klao@1951
|
52 |
///functions and types of the graph. The purpose of this class is to
|
klao@1951
|
53 |
///make easier implementing graph adaptors. E.g. if an adaptor is
|
klao@1951
|
54 |
///considered which differs from the wrapped graph only in some of its
|
klao@1951
|
55 |
///functions or types, then it can be derived from GraphAdaptor,
|
klao@1951
|
56 |
///and only the
|
klao@1951
|
57 |
///differences should be implemented.
|
klao@1951
|
58 |
///
|
klao@1951
|
59 |
///author Marton Makai
|
marci@970
|
60 |
template<typename _Graph>
|
alpar@1401
|
61 |
class GraphAdaptorBase {
|
marci@970
|
62 |
public:
|
marci@970
|
63 |
typedef _Graph Graph;
|
marci@970
|
64 |
typedef Graph ParentGraph;
|
marci@970
|
65 |
|
marci@556
|
66 |
protected:
|
marci@556
|
67 |
Graph* graph;
|
alpar@1401
|
68 |
GraphAdaptorBase() : graph(0) { }
|
marci@556
|
69 |
void setGraph(Graph& _graph) { graph=&_graph; }
|
marci@556
|
70 |
|
marci@556
|
71 |
public:
|
alpar@1401
|
72 |
GraphAdaptorBase(Graph& _graph) : graph(&_graph) { }
|
marci@556
|
73 |
|
alpar@774
|
74 |
typedef typename Graph::Node Node;
|
alpar@774
|
75 |
typedef typename Graph::Edge Edge;
|
marci@556
|
76 |
|
marci@970
|
77 |
void first(Node& i) const { graph->first(i); }
|
marci@970
|
78 |
void first(Edge& i) const { graph->first(i); }
|
marci@970
|
79 |
void firstIn(Edge& i, const Node& n) const { graph->firstIn(i, n); }
|
marci@970
|
80 |
void firstOut(Edge& i, const Node& n ) const { graph->firstOut(i, n); }
|
marci@556
|
81 |
|
marci@970
|
82 |
void next(Node& i) const { graph->next(i); }
|
marci@970
|
83 |
void next(Edge& i) const { graph->next(i); }
|
marci@970
|
84 |
void nextIn(Edge& i) const { graph->nextIn(i); }
|
marci@970
|
85 |
void nextOut(Edge& i) const { graph->nextOut(i); }
|
marci@970
|
86 |
|
alpar@986
|
87 |
Node source(const Edge& e) const { return graph->source(e); }
|
alpar@986
|
88 |
Node target(const Edge& e) const { return graph->target(e); }
|
marci@556
|
89 |
|
deba@1697
|
90 |
typedef NodeNumTagIndicator<Graph> NodeNumTag;
|
marci@556
|
91 |
int nodeNum() const { return graph->nodeNum(); }
|
deba@1697
|
92 |
|
deba@1697
|
93 |
typedef EdgeNumTagIndicator<Graph> EdgeNumTag;
|
marci@556
|
94 |
int edgeNum() const { return graph->edgeNum(); }
|
deba@1697
|
95 |
|
deba@1697
|
96 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag;
|
deba@1697
|
97 |
Edge findEdge(const Node& source, const Node& target,
|
deba@1697
|
98 |
const Edge& prev = INVALID) {
|
deba@1697
|
99 |
return graph->findEdge(source, target, prev);
|
deba@1697
|
100 |
}
|
marci@556
|
101 |
|
deba@1697
|
102 |
Node addNode() const {
|
deba@1697
|
103 |
return Node(graph->addNode());
|
deba@1697
|
104 |
}
|
deba@1697
|
105 |
|
alpar@986
|
106 |
Edge addEdge(const Node& source, const Node& target) const {
|
deba@1697
|
107 |
return Edge(graph->addEdge(source, target));
|
deba@1697
|
108 |
}
|
marci@556
|
109 |
|
marci@556
|
110 |
void erase(const Node& i) const { graph->erase(i); }
|
marci@556
|
111 |
void erase(const Edge& i) const { graph->erase(i); }
|
marci@556
|
112 |
|
marci@556
|
113 |
void clear() const { graph->clear(); }
|
marci@556
|
114 |
|
marci@739
|
115 |
int id(const Node& v) const { return graph->id(v); }
|
marci@739
|
116 |
int id(const Edge& e) const { return graph->id(e); }
|
marci@650
|
117 |
|
deba@1627
|
118 |
Edge oppositeNode(const Edge& e) const {
|
deba@1627
|
119 |
return Edge(graph->opposite(e));
|
deba@1627
|
120 |
}
|
marci@650
|
121 |
|
marci@970
|
122 |
template <typename _Value>
|
marci@970
|
123 |
class NodeMap : public _Graph::template NodeMap<_Value> {
|
marci@970
|
124 |
public:
|
marci@970
|
125 |
typedef typename _Graph::template NodeMap<_Value> Parent;
|
deba@1755
|
126 |
explicit NodeMap(const GraphAdaptorBase<_Graph>& gw)
|
deba@1755
|
127 |
: Parent(*gw.graph) { }
|
alpar@1401
|
128 |
NodeMap(const GraphAdaptorBase<_Graph>& gw, const _Value& value)
|
deba@1755
|
129 |
: Parent(*gw.graph, value) { }
|
marci@970
|
130 |
};
|
marci@556
|
131 |
|
marci@970
|
132 |
template <typename _Value>
|
marci@970
|
133 |
class EdgeMap : public _Graph::template EdgeMap<_Value> {
|
marci@970
|
134 |
public:
|
marci@970
|
135 |
typedef typename _Graph::template EdgeMap<_Value> Parent;
|
deba@1755
|
136 |
explicit EdgeMap(const GraphAdaptorBase<_Graph>& gw)
|
deba@1755
|
137 |
: Parent(*gw.graph) { }
|
alpar@1401
|
138 |
EdgeMap(const GraphAdaptorBase<_Graph>& gw, const _Value& value)
|
deba@1755
|
139 |
: Parent(*gw.graph, value) { }
|
marci@970
|
140 |
};
|
deba@877
|
141 |
|
marci@556
|
142 |
};
|
marci@556
|
143 |
|
marci@970
|
144 |
template <typename _Graph>
|
alpar@1401
|
145 |
class GraphAdaptor :
|
alpar@1401
|
146 |
public IterableGraphExtender<GraphAdaptorBase<_Graph> > {
|
marci@970
|
147 |
public:
|
marci@970
|
148 |
typedef _Graph Graph;
|
alpar@1401
|
149 |
typedef IterableGraphExtender<GraphAdaptorBase<_Graph> > Parent;
|
marci@970
|
150 |
protected:
|
alpar@1401
|
151 |
GraphAdaptor() : Parent() { }
|
marci@569
|
152 |
|
marci@970
|
153 |
public:
|
deba@1755
|
154 |
explicit GraphAdaptor(Graph& _graph) { setGraph(_graph); }
|
marci@970
|
155 |
};
|
marci@569
|
156 |
|
marci@997
|
157 |
template <typename _Graph>
|
alpar@1401
|
158 |
class RevGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
|
marci@997
|
159 |
public:
|
marci@997
|
160 |
typedef _Graph Graph;
|
alpar@1401
|
161 |
typedef GraphAdaptorBase<_Graph> Parent;
|
marci@997
|
162 |
protected:
|
alpar@1401
|
163 |
RevGraphAdaptorBase() : Parent() { }
|
marci@997
|
164 |
public:
|
marci@997
|
165 |
typedef typename Parent::Node Node;
|
marci@997
|
166 |
typedef typename Parent::Edge Edge;
|
marci@997
|
167 |
|
marci@997
|
168 |
void firstIn(Edge& i, const Node& n) const { Parent::firstOut(i, n); }
|
marci@997
|
169 |
void firstOut(Edge& i, const Node& n ) const { Parent::firstIn(i, n); }
|
marci@997
|
170 |
|
marci@997
|
171 |
void nextIn(Edge& i) const { Parent::nextOut(i); }
|
marci@997
|
172 |
void nextOut(Edge& i) const { Parent::nextIn(i); }
|
marci@997
|
173 |
|
marci@997
|
174 |
Node source(const Edge& e) const { return Parent::target(e); }
|
marci@997
|
175 |
Node target(const Edge& e) const { return Parent::source(e); }
|
marci@997
|
176 |
};
|
marci@997
|
177 |
|
marci@997
|
178 |
|
alpar@1949
|
179 |
///\brief A graph adaptor which reverses the orientation of the edges.
|
alpar@1949
|
180 |
///\ingroup graph_adaptors
|
alpar@1949
|
181 |
///
|
alpar@1949
|
182 |
///\warning Graph adaptors are in even more experimental
|
alpar@1949
|
183 |
///state than the other
|
alpar@879
|
184 |
///parts of the lib. Use them at you own risk.
|
alpar@879
|
185 |
///
|
alpar@1949
|
186 |
/// If \c g is defined as
|
alpar@1946
|
187 |
///\code
|
marci@923
|
188 |
/// ListGraph g;
|
alpar@1946
|
189 |
///\endcode
|
alpar@1949
|
190 |
/// then
|
alpar@1946
|
191 |
///\code
|
alpar@1401
|
192 |
/// RevGraphAdaptor<ListGraph> gw(g);
|
alpar@1946
|
193 |
///\endcode
|
alpar@1949
|
194 |
///implements the graph obtained from \c g by
|
alpar@1949
|
195 |
/// reversing the orientation of its edges.
|
alpar@1946
|
196 |
|
marci@997
|
197 |
template<typename _Graph>
|
alpar@1401
|
198 |
class RevGraphAdaptor :
|
alpar@1401
|
199 |
public IterableGraphExtender<RevGraphAdaptorBase<_Graph> > {
|
marci@650
|
200 |
public:
|
marci@997
|
201 |
typedef _Graph Graph;
|
marci@997
|
202 |
typedef IterableGraphExtender<
|
alpar@1401
|
203 |
RevGraphAdaptorBase<_Graph> > Parent;
|
marci@556
|
204 |
protected:
|
alpar@1401
|
205 |
RevGraphAdaptor() { }
|
marci@556
|
206 |
public:
|
deba@1755
|
207 |
explicit RevGraphAdaptor(_Graph& _graph) { setGraph(_graph); }
|
marci@997
|
208 |
};
|
marci@556
|
209 |
|
marci@992
|
210 |
|
deba@1681
|
211 |
template <typename _Graph, typename NodeFilterMap,
|
deba@1681
|
212 |
typename EdgeFilterMap, bool checked = true>
|
alpar@1401
|
213 |
class SubGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
|
marci@992
|
214 |
public:
|
marci@992
|
215 |
typedef _Graph Graph;
|
alpar@1401
|
216 |
typedef GraphAdaptorBase<_Graph> Parent;
|
marci@992
|
217 |
protected:
|
marci@992
|
218 |
NodeFilterMap* node_filter_map;
|
marci@992
|
219 |
EdgeFilterMap* edge_filter_map;
|
alpar@1401
|
220 |
SubGraphAdaptorBase() : Parent(),
|
marci@992
|
221 |
node_filter_map(0), edge_filter_map(0) { }
|
marci@775
|
222 |
|
marci@992
|
223 |
void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
|
marci@992
|
224 |
node_filter_map=&_node_filter_map;
|
marci@992
|
225 |
}
|
marci@992
|
226 |
void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
|
marci@992
|
227 |
edge_filter_map=&_edge_filter_map;
|
marci@992
|
228 |
}
|
marci@992
|
229 |
|
marci@992
|
230 |
public:
|
marci@992
|
231 |
|
marci@992
|
232 |
typedef typename Parent::Node Node;
|
marci@992
|
233 |
typedef typename Parent::Edge Edge;
|
marci@992
|
234 |
|
marci@992
|
235 |
void first(Node& i) const {
|
marci@992
|
236 |
Parent::first(i);
|
marci@992
|
237 |
while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
|
marci@992
|
238 |
}
|
deba@1681
|
239 |
|
deba@1681
|
240 |
void first(Edge& i) const {
|
deba@1681
|
241 |
Parent::first(i);
|
deba@1681
|
242 |
while (i!=INVALID && (!(*edge_filter_map)[i]
|
deba@1681
|
243 |
|| !(*node_filter_map)[Parent::source(i)]
|
deba@1681
|
244 |
|| !(*node_filter_map)[Parent::target(i)])) Parent::next(i);
|
deba@1681
|
245 |
}
|
deba@1681
|
246 |
|
deba@1681
|
247 |
void firstIn(Edge& i, const Node& n) const {
|
deba@1681
|
248 |
Parent::firstIn(i, n);
|
deba@1681
|
249 |
while (i!=INVALID && (!(*edge_filter_map)[i]
|
deba@1681
|
250 |
|| !(*node_filter_map)[Parent::source(i)])) Parent::nextIn(i);
|
deba@1681
|
251 |
}
|
deba@1681
|
252 |
|
deba@1681
|
253 |
void firstOut(Edge& i, const Node& n) const {
|
deba@1681
|
254 |
Parent::firstOut(i, n);
|
deba@1681
|
255 |
while (i!=INVALID && (!(*edge_filter_map)[i]
|
deba@1681
|
256 |
|| !(*node_filter_map)[Parent::target(i)])) Parent::nextOut(i);
|
deba@1681
|
257 |
}
|
deba@1681
|
258 |
|
deba@1681
|
259 |
void next(Node& i) const {
|
deba@1681
|
260 |
Parent::next(i);
|
deba@1681
|
261 |
while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
|
deba@1681
|
262 |
}
|
deba@1681
|
263 |
|
deba@1681
|
264 |
void next(Edge& i) const {
|
deba@1681
|
265 |
Parent::next(i);
|
deba@1681
|
266 |
while (i!=INVALID && (!(*edge_filter_map)[i]
|
deba@1681
|
267 |
|| !(*node_filter_map)[Parent::source(i)]
|
deba@1681
|
268 |
|| !(*node_filter_map)[Parent::target(i)])) Parent::next(i);
|
deba@1681
|
269 |
}
|
deba@1681
|
270 |
|
deba@1681
|
271 |
void nextIn(Edge& i) const {
|
deba@1681
|
272 |
Parent::nextIn(i);
|
deba@1681
|
273 |
while (i!=INVALID && (!(*edge_filter_map)[i]
|
deba@1681
|
274 |
|| !(*node_filter_map)[Parent::source(i)])) Parent::nextIn(i);
|
deba@1681
|
275 |
}
|
deba@1681
|
276 |
|
deba@1681
|
277 |
void nextOut(Edge& i) const {
|
deba@1681
|
278 |
Parent::nextOut(i);
|
deba@1681
|
279 |
while (i!=INVALID && (!(*edge_filter_map)[i]
|
deba@1681
|
280 |
|| !(*node_filter_map)[Parent::target(i)])) Parent::nextOut(i);
|
deba@1681
|
281 |
}
|
deba@1681
|
282 |
|
klao@1951
|
283 |
///\e
|
alpar@1949
|
284 |
|
klao@1951
|
285 |
/// This function hides \c n in the graph, i.e. the iteration
|
klao@1951
|
286 |
/// jumps over it. This is done by simply setting the value of \c n
|
klao@1951
|
287 |
/// to be false in the corresponding node-map.
|
deba@1681
|
288 |
void hide(const Node& n) const { node_filter_map->set(n, false); }
|
deba@1681
|
289 |
|
klao@1951
|
290 |
///\e
|
alpar@1949
|
291 |
|
klao@1951
|
292 |
/// This function hides \c e in the graph, i.e. the iteration
|
klao@1951
|
293 |
/// jumps over it. This is done by simply setting the value of \c e
|
klao@1951
|
294 |
/// to be false in the corresponding edge-map.
|
deba@1681
|
295 |
void hide(const Edge& e) const { edge_filter_map->set(e, false); }
|
deba@1681
|
296 |
|
klao@1951
|
297 |
///\e
|
alpar@1949
|
298 |
|
klao@1951
|
299 |
/// The value of \c n is set to be true in the node-map which stores
|
klao@1951
|
300 |
/// hide information. If \c n was hidden previuosly, then it is shown
|
klao@1951
|
301 |
/// again
|
deba@1681
|
302 |
void unHide(const Node& n) const { node_filter_map->set(n, true); }
|
deba@1681
|
303 |
|
klao@1951
|
304 |
///\e
|
alpar@1949
|
305 |
|
klao@1951
|
306 |
/// The value of \c e is set to be true in the edge-map which stores
|
klao@1951
|
307 |
/// hide information. If \c e was hidden previuosly, then it is shown
|
klao@1951
|
308 |
/// again
|
deba@1681
|
309 |
void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
|
deba@1681
|
310 |
|
klao@1951
|
311 |
/// Returns true if \c n is hidden.
|
alpar@1949
|
312 |
|
klao@1951
|
313 |
///\e
|
klao@1951
|
314 |
///
|
deba@1681
|
315 |
bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
|
deba@1681
|
316 |
|
klao@1951
|
317 |
/// Returns true if \c n is hidden.
|
alpar@1949
|
318 |
|
klao@1951
|
319 |
///\e
|
klao@1951
|
320 |
///
|
deba@1681
|
321 |
bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
|
deba@1681
|
322 |
|
deba@1697
|
323 |
typedef False NodeNumTag;
|
deba@1697
|
324 |
typedef False EdgeNumTag;
|
deba@1681
|
325 |
};
|
deba@1681
|
326 |
|
deba@1681
|
327 |
template <typename _Graph, typename NodeFilterMap, typename EdgeFilterMap>
|
deba@1681
|
328 |
class SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap, false>
|
deba@1681
|
329 |
: public GraphAdaptorBase<_Graph> {
|
deba@1681
|
330 |
public:
|
deba@1681
|
331 |
typedef _Graph Graph;
|
deba@1681
|
332 |
typedef GraphAdaptorBase<_Graph> Parent;
|
deba@1681
|
333 |
protected:
|
deba@1681
|
334 |
NodeFilterMap* node_filter_map;
|
deba@1681
|
335 |
EdgeFilterMap* edge_filter_map;
|
deba@1681
|
336 |
SubGraphAdaptorBase() : Parent(),
|
deba@1681
|
337 |
node_filter_map(0), edge_filter_map(0) { }
|
deba@1681
|
338 |
|
deba@1681
|
339 |
void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
|
deba@1681
|
340 |
node_filter_map=&_node_filter_map;
|
deba@1681
|
341 |
}
|
deba@1681
|
342 |
void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
|
deba@1681
|
343 |
edge_filter_map=&_edge_filter_map;
|
deba@1681
|
344 |
}
|
deba@1681
|
345 |
|
deba@1681
|
346 |
public:
|
deba@1681
|
347 |
|
deba@1681
|
348 |
typedef typename Parent::Node Node;
|
deba@1681
|
349 |
typedef typename Parent::Edge Edge;
|
deba@1681
|
350 |
|
deba@1681
|
351 |
void first(Node& i) const {
|
deba@1681
|
352 |
Parent::first(i);
|
deba@1681
|
353 |
while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
|
deba@1681
|
354 |
}
|
deba@1681
|
355 |
|
marci@992
|
356 |
void first(Edge& i) const {
|
marci@992
|
357 |
Parent::first(i);
|
marci@992
|
358 |
while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i);
|
marci@992
|
359 |
}
|
deba@1681
|
360 |
|
marci@992
|
361 |
void firstIn(Edge& i, const Node& n) const {
|
marci@992
|
362 |
Parent::firstIn(i, n);
|
marci@992
|
363 |
while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i);
|
marci@992
|
364 |
}
|
deba@1681
|
365 |
|
marci@992
|
366 |
void firstOut(Edge& i, const Node& n) const {
|
marci@992
|
367 |
Parent::firstOut(i, n);
|
marci@992
|
368 |
while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i);
|
marci@992
|
369 |
}
|
marci@992
|
370 |
|
marci@992
|
371 |
void next(Node& i) const {
|
marci@992
|
372 |
Parent::next(i);
|
marci@992
|
373 |
while (i!=INVALID && !(*node_filter_map)[i]) Parent::next(i);
|
marci@992
|
374 |
}
|
marci@992
|
375 |
void next(Edge& i) const {
|
marci@992
|
376 |
Parent::next(i);
|
marci@992
|
377 |
while (i!=INVALID && !(*edge_filter_map)[i]) Parent::next(i);
|
marci@992
|
378 |
}
|
marci@992
|
379 |
void nextIn(Edge& i) const {
|
marci@992
|
380 |
Parent::nextIn(i);
|
marci@992
|
381 |
while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextIn(i);
|
marci@992
|
382 |
}
|
deba@1681
|
383 |
|
marci@992
|
384 |
void nextOut(Edge& i) const {
|
marci@992
|
385 |
Parent::nextOut(i);
|
marci@992
|
386 |
while (i!=INVALID && !(*edge_filter_map)[i]) Parent::nextOut(i);
|
marci@992
|
387 |
}
|
marci@992
|
388 |
|
klao@1951
|
389 |
///\e
|
alpar@1949
|
390 |
|
klao@1951
|
391 |
/// This function hides \c n in the graph, i.e. the iteration
|
klao@1951
|
392 |
/// jumps over it. This is done by simply setting the value of \c n
|
klao@1951
|
393 |
/// to be false in the corresponding node-map.
|
marci@992
|
394 |
void hide(const Node& n) const { node_filter_map->set(n, false); }
|
marci@992
|
395 |
|
klao@1951
|
396 |
///\e
|
alpar@1949
|
397 |
|
klao@1951
|
398 |
/// This function hides \c e in the graph, i.e. the iteration
|
klao@1951
|
399 |
/// jumps over it. This is done by simply setting the value of \c e
|
klao@1951
|
400 |
/// to be false in the corresponding edge-map.
|
marci@992
|
401 |
void hide(const Edge& e) const { edge_filter_map->set(e, false); }
|
marci@992
|
402 |
|
klao@1951
|
403 |
///\e
|
alpar@1949
|
404 |
|
klao@1951
|
405 |
/// The value of \c n is set to be true in the node-map which stores
|
klao@1951
|
406 |
/// hide information. If \c n was hidden previuosly, then it is shown
|
klao@1951
|
407 |
/// again
|
marci@992
|
408 |
void unHide(const Node& n) const { node_filter_map->set(n, true); }
|
marci@992
|
409 |
|
klao@1951
|
410 |
///\e
|
alpar@1949
|
411 |
|
klao@1951
|
412 |
/// The value of \c e is set to be true in the edge-map which stores
|
klao@1951
|
413 |
/// hide information. If \c e was hidden previuosly, then it is shown
|
klao@1951
|
414 |
/// again
|
marci@992
|
415 |
void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
|
marci@992
|
416 |
|
klao@1951
|
417 |
/// Returns true if \c n is hidden.
|
alpar@1949
|
418 |
|
klao@1951
|
419 |
///\e
|
klao@1951
|
420 |
///
|
marci@992
|
421 |
bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
|
marci@992
|
422 |
|
klao@1951
|
423 |
/// Returns true if \c n is hidden.
|
alpar@1949
|
424 |
|
klao@1951
|
425 |
///\e
|
klao@1951
|
426 |
///
|
marci@992
|
427 |
bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
|
marci@992
|
428 |
|
deba@1697
|
429 |
typedef False NodeNumTag;
|
deba@1697
|
430 |
typedef False EdgeNumTag;
|
marci@992
|
431 |
};
|
marci@775
|
432 |
|
klao@1951
|
433 |
/// \brief A graph adaptor for hiding nodes and edges from a graph.
|
klao@1951
|
434 |
/// \ingroup graph_adaptors
|
klao@1951
|
435 |
///
|
klao@1951
|
436 |
/// \warning Graph adaptors are in even more experimental state than the
|
klao@1951
|
437 |
/// other parts of the lib. Use them at you own risk.
|
klao@1951
|
438 |
///
|
klao@1951
|
439 |
/// SubGraphAdaptor shows the graph with filtered node-set and
|
klao@1951
|
440 |
/// edge-set. If the \c checked parameter is true then it filters the edgeset
|
klao@1951
|
441 |
/// to do not get invalid edges without source or target.
|
klao@1952
|
442 |
/// Let \f$ G=(V, A) \f$ be a directed graph
|
klao@1951
|
443 |
/// and suppose that the graph instance \c g of type ListGraph
|
klao@1952
|
444 |
/// implements \f$ G \f$.
|
klao@1952
|
445 |
/// Let moreover \f$ b_V \f$ and \f$ b_A \f$ be bool-valued functions resp.
|
klao@1951
|
446 |
/// on the node-set and edge-set.
|
klao@1951
|
447 |
/// SubGraphAdaptor<...>::NodeIt iterates
|
klao@1952
|
448 |
/// on the node-set \f$ \{v\in V : b_V(v)=true\} \f$ and
|
klao@1951
|
449 |
/// SubGraphAdaptor<...>::EdgeIt iterates
|
klao@1952
|
450 |
/// on the edge-set \f$ \{e\in A : b_A(e)=true\} \f$. Similarly,
|
klao@1951
|
451 |
/// SubGraphAdaptor<...>::OutEdgeIt and
|
klao@1951
|
452 |
/// SubGraphAdaptor<...>::InEdgeIt iterates
|
klao@1951
|
453 |
/// only on edges leaving and entering a specific node which have true value.
|
klao@1951
|
454 |
///
|
klao@1951
|
455 |
/// If the \c checked template parameter is false then we have to note that
|
klao@1951
|
456 |
/// the node-iterator cares only the filter on the node-set, and the
|
klao@1951
|
457 |
/// edge-iterator cares only the filter on the edge-set.
|
klao@1951
|
458 |
/// This way the edge-map
|
klao@1951
|
459 |
/// should filter all edges which's source or target is filtered by the
|
klao@1951
|
460 |
/// node-filter.
|
klao@1951
|
461 |
/// \code
|
klao@1951
|
462 |
/// typedef ListGraph Graph;
|
klao@1951
|
463 |
/// Graph g;
|
klao@1951
|
464 |
/// typedef Graph::Node Node;
|
klao@1951
|
465 |
/// typedef Graph::Edge Edge;
|
klao@1951
|
466 |
/// Node u=g.addNode(); //node of id 0
|
klao@1951
|
467 |
/// Node v=g.addNode(); //node of id 1
|
klao@1951
|
468 |
/// Node e=g.addEdge(u, v); //edge of id 0
|
klao@1951
|
469 |
/// Node f=g.addEdge(v, u); //edge of id 1
|
klao@1951
|
470 |
/// Graph::NodeMap<bool> nm(g, true);
|
klao@1951
|
471 |
/// nm.set(u, false);
|
klao@1951
|
472 |
/// Graph::EdgeMap<bool> em(g, true);
|
klao@1951
|
473 |
/// em.set(e, false);
|
klao@1951
|
474 |
/// typedef SubGraphAdaptor<Graph, Graph::NodeMap<bool>, Graph::EdgeMap<bool> > SubGW;
|
klao@1951
|
475 |
/// SubGW gw(g, nm, em);
|
klao@1951
|
476 |
/// for (SubGW::NodeIt n(gw); n!=INVALID; ++n) std::cout << g.id(n) << std::endl;
|
klao@1951
|
477 |
/// std::cout << ":-)" << std::endl;
|
klao@1951
|
478 |
/// for (SubGW::EdgeIt e(gw); e!=INVALID; ++e) std::cout << g.id(e) << std::endl;
|
klao@1951
|
479 |
/// \endcode
|
klao@1951
|
480 |
/// The output of the above code is the following.
|
klao@1951
|
481 |
/// \code
|
klao@1951
|
482 |
/// 1
|
klao@1951
|
483 |
/// :-)
|
klao@1951
|
484 |
/// 1
|
klao@1951
|
485 |
/// \endcode
|
klao@1951
|
486 |
/// Note that \c n is of type \c SubGW::NodeIt, but it can be converted to
|
klao@1951
|
487 |
/// \c Graph::Node that is why \c g.id(n) can be applied.
|
klao@1951
|
488 |
///
|
klao@1951
|
489 |
/// For other examples see also the documentation of NodeSubGraphAdaptor and
|
klao@1951
|
490 |
/// EdgeSubGraphAdaptor.
|
klao@1951
|
491 |
///
|
klao@1951
|
492 |
/// \author Marton Makai
|
marci@1242
|
493 |
|
marci@992
|
494 |
template<typename _Graph, typename NodeFilterMap,
|
deba@1681
|
495 |
typename EdgeFilterMap, bool checked = true>
|
alpar@1401
|
496 |
class SubGraphAdaptor :
|
marci@992
|
497 |
public IterableGraphExtender<
|
deba@1681
|
498 |
SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap, checked> > {
|
marci@650
|
499 |
public:
|
marci@992
|
500 |
typedef _Graph Graph;
|
marci@992
|
501 |
typedef IterableGraphExtender<
|
alpar@1401
|
502 |
SubGraphAdaptorBase<_Graph, NodeFilterMap, EdgeFilterMap> > Parent;
|
marci@556
|
503 |
protected:
|
alpar@1401
|
504 |
SubGraphAdaptor() { }
|
marci@992
|
505 |
public:
|
alpar@1401
|
506 |
SubGraphAdaptor(_Graph& _graph, NodeFilterMap& _node_filter_map,
|
marci@992
|
507 |
EdgeFilterMap& _edge_filter_map) {
|
marci@992
|
508 |
setGraph(_graph);
|
marci@992
|
509 |
setNodeFilterMap(_node_filter_map);
|
marci@992
|
510 |
setEdgeFilterMap(_edge_filter_map);
|
marci@992
|
511 |
}
|
marci@992
|
512 |
};
|
marci@556
|
513 |
|
marci@556
|
514 |
|
marci@569
|
515 |
|
klao@1951
|
516 |
///\brief An adaptor for hiding nodes from a graph.
|
klao@1951
|
517 |
///\ingroup graph_adaptors
|
klao@1951
|
518 |
///
|
klao@1951
|
519 |
///\warning Graph adaptors are in even more experimental state
|
klao@1951
|
520 |
///than the other
|
klao@1951
|
521 |
///parts of the lib. Use them at you own risk.
|
klao@1951
|
522 |
///
|
klao@1951
|
523 |
///An adaptor for hiding nodes from a graph.
|
klao@1951
|
524 |
///This adaptor specializes SubGraphAdaptor in the way that only
|
klao@1951
|
525 |
///the node-set
|
klao@1951
|
526 |
///can be filtered. In usual case the checked parameter is true, we get the
|
klao@1951
|
527 |
///induced subgraph. But if the checked parameter is false then we can only
|
klao@1951
|
528 |
///filter only isolated nodes.
|
klao@1951
|
529 |
///\author Marton Makai
|
deba@1681
|
530 |
template<typename Graph, typename NodeFilterMap, bool checked = true>
|
alpar@1401
|
531 |
class NodeSubGraphAdaptor :
|
alpar@1401
|
532 |
public SubGraphAdaptor<Graph, NodeFilterMap,
|
deba@1681
|
533 |
ConstMap<typename Graph::Edge,bool>, checked> {
|
marci@933
|
534 |
public:
|
alpar@1401
|
535 |
typedef SubGraphAdaptor<Graph, NodeFilterMap,
|
marci@933
|
536 |
ConstMap<typename Graph::Edge,bool> > Parent;
|
marci@933
|
537 |
protected:
|
marci@933
|
538 |
ConstMap<typename Graph::Edge, bool> const_true_map;
|
marci@933
|
539 |
public:
|
alpar@1401
|
540 |
NodeSubGraphAdaptor(Graph& _graph, NodeFilterMap& _node_filter_map) :
|
marci@933
|
541 |
Parent(), const_true_map(true) {
|
marci@933
|
542 |
Parent::setGraph(_graph);
|
marci@933
|
543 |
Parent::setNodeFilterMap(_node_filter_map);
|
marci@933
|
544 |
Parent::setEdgeFilterMap(const_true_map);
|
marci@933
|
545 |
}
|
marci@933
|
546 |
};
|
marci@933
|
547 |
|
marci@933
|
548 |
|
klao@1951
|
549 |
///\brief An adaptor for hiding edges from a graph.
|
klao@1951
|
550 |
///
|
klao@1951
|
551 |
///\warning Graph adaptors are in even more experimental state
|
klao@1951
|
552 |
///than the other parts of the lib. Use them at you own risk.
|
klao@1951
|
553 |
///
|
klao@1951
|
554 |
///An adaptor for hiding edges from a graph.
|
klao@1951
|
555 |
///This adaptor specializes SubGraphAdaptor in the way that
|
klao@1951
|
556 |
///only the edge-set
|
klao@1951
|
557 |
///can be filtered. The usefulness of this adaptor is demonstrated in the
|
klao@1951
|
558 |
///problem of searching a maximum number of edge-disjoint shortest paths
|
klao@1951
|
559 |
///between
|
klao@1951
|
560 |
///two nodes \c s and \c t. Shortest here means being shortest w.r.t.
|
klao@1951
|
561 |
///non-negative edge-lengths. Note that
|
klao@1951
|
562 |
///the comprehension of the presented solution
|
klao@1951
|
563 |
///need's some elementary knowledge from combinatorial optimization.
|
klao@1951
|
564 |
///
|
klao@1951
|
565 |
///If a single shortest path is to be
|
klao@1951
|
566 |
///searched between \c s and \c t, then this can be done easily by
|
klao@1951
|
567 |
///applying the Dijkstra algorithm. What happens, if a maximum number of
|
klao@1951
|
568 |
///edge-disjoint shortest paths is to be computed. It can be proved that an
|
klao@1951
|
569 |
///edge can be in a shortest path if and only
|
klao@1951
|
570 |
///if it is tight with respect to
|
klao@1951
|
571 |
///the potential function computed by Dijkstra.
|
klao@1951
|
572 |
///Moreover, any path containing
|
klao@1951
|
573 |
///only such edges is a shortest one.
|
klao@1951
|
574 |
///Thus we have to compute a maximum number
|
klao@1951
|
575 |
///of edge-disjoint paths between \c s and \c t in
|
klao@1951
|
576 |
///the graph which has edge-set
|
klao@1951
|
577 |
///all the tight edges. The computation will be demonstrated
|
klao@1951
|
578 |
///on the following
|
klao@1951
|
579 |
///graph, which is read from the dimacs file \c sub_graph_adaptor_demo.dim.
|
klao@1951
|
580 |
///The full source code is available in \ref sub_graph_adaptor_demo.cc.
|
klao@1951
|
581 |
///If you are interested in more demo programs, you can use
|
klao@1951
|
582 |
///\ref dim_to_dot.cc to generate .dot files from dimacs files.
|
klao@1951
|
583 |
///The .dot file of the following figure was generated by
|
klao@1951
|
584 |
///the demo program \ref dim_to_dot.cc.
|
klao@1951
|
585 |
///
|
klao@1951
|
586 |
///\dot
|
klao@1951
|
587 |
///digraph lemon_dot_example {
|
klao@1951
|
588 |
///node [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
|
klao@1951
|
589 |
///n0 [ label="0 (s)" ];
|
klao@1951
|
590 |
///n1 [ label="1" ];
|
klao@1951
|
591 |
///n2 [ label="2" ];
|
klao@1951
|
592 |
///n3 [ label="3" ];
|
klao@1951
|
593 |
///n4 [ label="4" ];
|
klao@1951
|
594 |
///n5 [ label="5" ];
|
klao@1951
|
595 |
///n6 [ label="6 (t)" ];
|
klao@1951
|
596 |
///edge [ shape=ellipse, fontname=Helvetica, fontsize=10 ];
|
klao@1951
|
597 |
///n5 -> n6 [ label="9, length:4" ];
|
klao@1951
|
598 |
///n4 -> n6 [ label="8, length:2" ];
|
klao@1951
|
599 |
///n3 -> n5 [ label="7, length:1" ];
|
klao@1951
|
600 |
///n2 -> n5 [ label="6, length:3" ];
|
klao@1951
|
601 |
///n2 -> n6 [ label="5, length:5" ];
|
klao@1951
|
602 |
///n2 -> n4 [ label="4, length:2" ];
|
klao@1951
|
603 |
///n1 -> n4 [ label="3, length:3" ];
|
klao@1951
|
604 |
///n0 -> n3 [ label="2, length:1" ];
|
klao@1951
|
605 |
///n0 -> n2 [ label="1, length:2" ];
|
klao@1951
|
606 |
///n0 -> n1 [ label="0, length:3" ];
|
klao@1951
|
607 |
///}
|
klao@1951
|
608 |
///\enddot
|
klao@1951
|
609 |
///
|
klao@1951
|
610 |
///\code
|
klao@1951
|
611 |
///Graph g;
|
klao@1951
|
612 |
///Node s, t;
|
klao@1951
|
613 |
///LengthMap length(g);
|
klao@1951
|
614 |
///
|
klao@1951
|
615 |
///readDimacs(std::cin, g, length, s, t);
|
klao@1951
|
616 |
///
|
klao@1951
|
617 |
///cout << "edges with lengths (of form id, source--length->target): " << endl;
|
klao@1951
|
618 |
///for(EdgeIt e(g); e!=INVALID; ++e)
|
klao@1951
|
619 |
/// cout << g.id(e) << ", " << g.id(g.source(e)) << "--"
|
klao@1951
|
620 |
/// << length[e] << "->" << g.id(g.target(e)) << endl;
|
klao@1951
|
621 |
///
|
klao@1951
|
622 |
///cout << "s: " << g.id(s) << " t: " << g.id(t) << endl;
|
klao@1951
|
623 |
///\endcode
|
klao@1951
|
624 |
///Next, the potential function is computed with Dijkstra.
|
klao@1951
|
625 |
///\code
|
klao@1951
|
626 |
///typedef Dijkstra<Graph, LengthMap> Dijkstra;
|
klao@1951
|
627 |
///Dijkstra dijkstra(g, length);
|
klao@1951
|
628 |
///dijkstra.run(s);
|
klao@1951
|
629 |
///\endcode
|
klao@1951
|
630 |
///Next, we consrtruct a map which filters the edge-set to the tight edges.
|
klao@1951
|
631 |
///\code
|
klao@1951
|
632 |
///typedef TightEdgeFilterMap<Graph, const Dijkstra::DistMap, LengthMap>
|
klao@1951
|
633 |
/// TightEdgeFilter;
|
klao@1951
|
634 |
///TightEdgeFilter tight_edge_filter(g, dijkstra.distMap(), length);
|
klao@1951
|
635 |
///
|
klao@1951
|
636 |
///typedef EdgeSubGraphAdaptor<Graph, TightEdgeFilter> SubGW;
|
klao@1951
|
637 |
///SubGW gw(g, tight_edge_filter);
|
klao@1951
|
638 |
///\endcode
|
klao@1951
|
639 |
///Then, the maximum nimber of edge-disjoint \c s-\c t paths are computed
|
klao@1951
|
640 |
///with a max flow algorithm Preflow.
|
klao@1951
|
641 |
///\code
|
klao@1951
|
642 |
///ConstMap<Edge, int> const_1_map(1);
|
klao@1951
|
643 |
///Graph::EdgeMap<int> flow(g, 0);
|
klao@1951
|
644 |
///
|
klao@1951
|
645 |
///Preflow<SubGW, int, ConstMap<Edge, int>, Graph::EdgeMap<int> >
|
klao@1951
|
646 |
/// preflow(gw, s, t, const_1_map, flow);
|
klao@1951
|
647 |
///preflow.run();
|
klao@1951
|
648 |
///\endcode
|
klao@1951
|
649 |
///Last, the output is:
|
klao@1951
|
650 |
///\code
|
klao@1951
|
651 |
///cout << "maximum number of edge-disjoint shortest path: "
|
klao@1951
|
652 |
/// << preflow.flowValue() << endl;
|
klao@1951
|
653 |
///cout << "edges of the maximum number of edge-disjoint shortest s-t paths: "
|
klao@1951
|
654 |
/// << endl;
|
klao@1951
|
655 |
///for(EdgeIt e(g); e!=INVALID; ++e)
|
klao@1951
|
656 |
/// if (flow[e])
|
klao@1951
|
657 |
/// cout << " " << g.id(g.source(e)) << "--"
|
klao@1951
|
658 |
/// << length[e] << "->" << g.id(g.target(e)) << endl;
|
klao@1951
|
659 |
///\endcode
|
klao@1951
|
660 |
///The program has the following (expected :-)) output:
|
klao@1951
|
661 |
///\code
|
klao@1951
|
662 |
///edges with lengths (of form id, source--length->target):
|
klao@1951
|
663 |
/// 9, 5--4->6
|
klao@1951
|
664 |
/// 8, 4--2->6
|
klao@1951
|
665 |
/// 7, 3--1->5
|
klao@1951
|
666 |
/// 6, 2--3->5
|
klao@1951
|
667 |
/// 5, 2--5->6
|
klao@1951
|
668 |
/// 4, 2--2->4
|
klao@1951
|
669 |
/// 3, 1--3->4
|
klao@1951
|
670 |
/// 2, 0--1->3
|
klao@1951
|
671 |
/// 1, 0--2->2
|
klao@1951
|
672 |
/// 0, 0--3->1
|
klao@1951
|
673 |
///s: 0 t: 6
|
klao@1951
|
674 |
///maximum number of edge-disjoint shortest path: 2
|
klao@1951
|
675 |
///edges of the maximum number of edge-disjoint shortest s-t paths:
|
klao@1951
|
676 |
/// 9, 5--4->6
|
klao@1951
|
677 |
/// 8, 4--2->6
|
klao@1951
|
678 |
/// 7, 3--1->5
|
klao@1951
|
679 |
/// 4, 2--2->4
|
klao@1951
|
680 |
/// 2, 0--1->3
|
klao@1951
|
681 |
/// 1, 0--2->2
|
klao@1951
|
682 |
///\endcode
|
klao@1951
|
683 |
///
|
klao@1951
|
684 |
///\author Marton Makai
|
marci@932
|
685 |
template<typename Graph, typename EdgeFilterMap>
|
alpar@1401
|
686 |
class EdgeSubGraphAdaptor :
|
alpar@1401
|
687 |
public SubGraphAdaptor<Graph, ConstMap<typename Graph::Node,bool>,
|
deba@1681
|
688 |
EdgeFilterMap, false> {
|
marci@932
|
689 |
public:
|
alpar@1401
|
690 |
typedef SubGraphAdaptor<Graph, ConstMap<typename Graph::Node,bool>,
|
deba@1685
|
691 |
EdgeFilterMap, false> Parent;
|
marci@932
|
692 |
protected:
|
marci@932
|
693 |
ConstMap<typename Graph::Node, bool> const_true_map;
|
marci@932
|
694 |
public:
|
alpar@1401
|
695 |
EdgeSubGraphAdaptor(Graph& _graph, EdgeFilterMap& _edge_filter_map) :
|
marci@932
|
696 |
Parent(), const_true_map(true) {
|
marci@932
|
697 |
Parent::setGraph(_graph);
|
marci@932
|
698 |
Parent::setNodeFilterMap(const_true_map);
|
marci@932
|
699 |
Parent::setEdgeFilterMap(_edge_filter_map);
|
marci@932
|
700 |
}
|
marci@932
|
701 |
};
|
marci@932
|
702 |
|
marci@1383
|
703 |
template <typename _Graph>
|
klao@1909
|
704 |
class UGraphAdaptorBase :
|
klao@1909
|
705 |
public UGraphExtender<GraphAdaptorBase<_Graph> > {
|
marci@1383
|
706 |
public:
|
marci@1383
|
707 |
typedef _Graph Graph;
|
klao@1909
|
708 |
typedef UGraphExtender<GraphAdaptorBase<_Graph> > Parent;
|
marci@1383
|
709 |
protected:
|
klao@1909
|
710 |
UGraphAdaptorBase() : Parent() { }
|
marci@1383
|
711 |
public:
|
klao@1909
|
712 |
typedef typename Parent::UEdge UEdge;
|
marci@1383
|
713 |
typedef typename Parent::Edge Edge;
|
marci@1383
|
714 |
|
marci@1383
|
715 |
template <typename T>
|
marci@1383
|
716 |
class EdgeMap {
|
marci@1383
|
717 |
protected:
|
klao@1909
|
718 |
const UGraphAdaptorBase<_Graph>* g;
|
marci@1383
|
719 |
template <typename TT> friend class EdgeMap;
|
marci@1383
|
720 |
typename _Graph::template EdgeMap<T> forward_map, backward_map;
|
marci@1383
|
721 |
public:
|
marci@1383
|
722 |
typedef T Value;
|
marci@1383
|
723 |
typedef Edge Key;
|
marci@1383
|
724 |
|
klao@1909
|
725 |
EdgeMap(const UGraphAdaptorBase<_Graph>& _g) : g(&_g),
|
marci@1383
|
726 |
forward_map(*(g->graph)), backward_map(*(g->graph)) { }
|
marci@569
|
727 |
|
klao@1909
|
728 |
EdgeMap(const UGraphAdaptorBase<_Graph>& _g, T a) : g(&_g),
|
marci@1383
|
729 |
forward_map(*(g->graph), a), backward_map(*(g->graph), a) { }
|
marci@1383
|
730 |
|
marci@1383
|
731 |
void set(Edge e, T a) {
|
deba@1627
|
732 |
if (g->direction(e))
|
marci@1383
|
733 |
forward_map.set(e, a);
|
marci@1383
|
734 |
else
|
marci@1383
|
735 |
backward_map.set(e, a);
|
marci@1383
|
736 |
}
|
marci@556
|
737 |
|
marci@1383
|
738 |
T operator[](Edge e) const {
|
deba@1627
|
739 |
if (g->direction(e))
|
marci@1383
|
740 |
return forward_map[e];
|
marci@1383
|
741 |
else
|
marci@1383
|
742 |
return backward_map[e];
|
marci@556
|
743 |
}
|
marci@556
|
744 |
};
|
marci@1383
|
745 |
|
marci@1383
|
746 |
template <typename T>
|
klao@1909
|
747 |
class UEdgeMap {
|
klao@1909
|
748 |
template <typename TT> friend class UEdgeMap;
|
marci@1383
|
749 |
typename _Graph::template EdgeMap<T> map;
|
marci@1383
|
750 |
public:
|
marci@1383
|
751 |
typedef T Value;
|
klao@1909
|
752 |
typedef UEdge Key;
|
marci@1383
|
753 |
|
klao@1909
|
754 |
UEdgeMap(const UGraphAdaptorBase<_Graph>& g) :
|
marci@1383
|
755 |
map(*(g.graph)) { }
|
marci@556
|
756 |
|
klao@1909
|
757 |
UEdgeMap(const UGraphAdaptorBase<_Graph>& g, T a) :
|
marci@1383
|
758 |
map(*(g.graph), a) { }
|
marci@1383
|
759 |
|
klao@1909
|
760 |
void set(UEdge e, T a) {
|
marci@1383
|
761 |
map.set(e, a);
|
marci@1383
|
762 |
}
|
marci@556
|
763 |
|
klao@1909
|
764 |
T operator[](UEdge e) const {
|
marci@1383
|
765 |
return map[e];
|
marci@1383
|
766 |
}
|
marci@1383
|
767 |
};
|
marci@1383
|
768 |
|
marci@1383
|
769 |
};
|
marci@1383
|
770 |
|
klao@1951
|
771 |
///\brief An undirected graph is made from a directed graph by an adaptor
|
klao@1951
|
772 |
///\ingroup graph_adaptors
|
klao@1951
|
773 |
///
|
klao@1951
|
774 |
/// Undocumented, untested!!!
|
klao@1951
|
775 |
/// If somebody knows nice demo application, let's polulate it.
|
klao@1951
|
776 |
///
|
klao@1951
|
777 |
/// \author Marton Makai
|
marci@1383
|
778 |
template<typename _Graph>
|
klao@1909
|
779 |
class UGraphAdaptor :
|
klao@1909
|
780 |
public IterableUGraphExtender<
|
klao@1909
|
781 |
UGraphAdaptorBase<_Graph> > {
|
marci@1383
|
782 |
public:
|
marci@1383
|
783 |
typedef _Graph Graph;
|
klao@1909
|
784 |
typedef IterableUGraphExtender<
|
klao@1909
|
785 |
UGraphAdaptorBase<_Graph> > Parent;
|
marci@1383
|
786 |
protected:
|
klao@1909
|
787 |
UGraphAdaptor() { }
|
marci@1383
|
788 |
public:
|
klao@1909
|
789 |
UGraphAdaptor(_Graph& _graph) {
|
marci@1383
|
790 |
setGraph(_graph);
|
marci@556
|
791 |
}
|
marci@556
|
792 |
};
|
marci@556
|
793 |
|
marci@992
|
794 |
|
marci@992
|
795 |
template <typename _Graph,
|
marci@992
|
796 |
typename ForwardFilterMap, typename BackwardFilterMap>
|
alpar@1401
|
797 |
class SubBidirGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
|
marci@992
|
798 |
public:
|
marci@992
|
799 |
typedef _Graph Graph;
|
alpar@1401
|
800 |
typedef GraphAdaptorBase<_Graph> Parent;
|
marci@992
|
801 |
protected:
|
marci@992
|
802 |
ForwardFilterMap* forward_filter;
|
marci@992
|
803 |
BackwardFilterMap* backward_filter;
|
alpar@1401
|
804 |
SubBidirGraphAdaptorBase() : Parent(),
|
marci@992
|
805 |
forward_filter(0), backward_filter(0) { }
|
marci@992
|
806 |
|
marci@992
|
807 |
void setForwardFilterMap(ForwardFilterMap& _forward_filter) {
|
marci@992
|
808 |
forward_filter=&_forward_filter;
|
marci@992
|
809 |
}
|
marci@992
|
810 |
void setBackwardFilterMap(BackwardFilterMap& _backward_filter) {
|
marci@992
|
811 |
backward_filter=&_backward_filter;
|
marci@992
|
812 |
}
|
marci@992
|
813 |
|
marci@992
|
814 |
public:
|
alpar@1401
|
815 |
// SubGraphAdaptorBase(Graph& _graph,
|
marci@992
|
816 |
// NodeFilterMap& _node_filter_map,
|
marci@992
|
817 |
// EdgeFilterMap& _edge_filter_map) :
|
marci@992
|
818 |
// Parent(&_graph),
|
marci@992
|
819 |
// node_filter_map(&node_filter_map),
|
marci@992
|
820 |
// edge_filter_map(&edge_filter_map) { }
|
marci@992
|
821 |
|
marci@992
|
822 |
typedef typename Parent::Node Node;
|
marci@992
|
823 |
typedef typename _Graph::Edge GraphEdge;
|
marci@992
|
824 |
template <typename T> class EdgeMap;
|
alpar@1949
|
825 |
// SubBidirGraphAdaptorBase<..., ..., ...>::Edge is inherited from
|
alpar@1949
|
826 |
// _Graph::Edge. It contains an extra bool flag which is true
|
alpar@1949
|
827 |
// if and only if the
|
alpar@1949
|
828 |
// edge is the backward version of the original edge.
|
marci@992
|
829 |
class Edge : public _Graph::Edge {
|
alpar@1401
|
830 |
friend class SubBidirGraphAdaptorBase<
|
marci@992
|
831 |
Graph, ForwardFilterMap, BackwardFilterMap>;
|
marci@992
|
832 |
template<typename T> friend class EdgeMap;
|
marci@992
|
833 |
protected:
|
marci@992
|
834 |
bool backward; //true, iff backward
|
marci@992
|
835 |
public:
|
marci@992
|
836 |
Edge() { }
|
alpar@1949
|
837 |
// \todo =false is needed, or causes problems?
|
alpar@1949
|
838 |
// If \c _backward is false, then we get an edge corresponding to the
|
alpar@1949
|
839 |
// original one, otherwise its oppositely directed pair is obtained.
|
marci@992
|
840 |
Edge(const typename _Graph::Edge& e, bool _backward/*=false*/) :
|
marci@992
|
841 |
_Graph::Edge(e), backward(_backward) { }
|
marci@992
|
842 |
Edge(Invalid i) : _Graph::Edge(i), backward(true) { }
|
marci@992
|
843 |
bool operator==(const Edge& v) const {
|
marci@992
|
844 |
return (this->backward==v.backward &&
|
marci@992
|
845 |
static_cast<typename _Graph::Edge>(*this)==
|
marci@992
|
846 |
static_cast<typename _Graph::Edge>(v));
|
marci@992
|
847 |
}
|
marci@992
|
848 |
bool operator!=(const Edge& v) const {
|
marci@992
|
849 |
return (this->backward!=v.backward ||
|
marci@992
|
850 |
static_cast<typename _Graph::Edge>(*this)!=
|
marci@992
|
851 |
static_cast<typename _Graph::Edge>(v));
|
marci@992
|
852 |
}
|
marci@992
|
853 |
};
|
marci@992
|
854 |
|
marci@992
|
855 |
void first(Node& i) const {
|
marci@992
|
856 |
Parent::first(i);
|
marci@992
|
857 |
}
|
marci@992
|
858 |
|
marci@992
|
859 |
void first(Edge& i) const {
|
marci@992
|
860 |
Parent::first(i);
|
marci@992
|
861 |
i.backward=false;
|
marci@992
|
862 |
while (*static_cast<GraphEdge*>(&i)!=INVALID &&
|
marci@992
|
863 |
!(*forward_filter)[i]) Parent::next(i);
|
marci@992
|
864 |
if (*static_cast<GraphEdge*>(&i)==INVALID) {
|
marci@992
|
865 |
Parent::first(i);
|
marci@992
|
866 |
i.backward=true;
|
marci@992
|
867 |
while (*static_cast<GraphEdge*>(&i)!=INVALID &&
|
marci@992
|
868 |
!(*backward_filter)[i]) Parent::next(i);
|
marci@992
|
869 |
}
|
marci@992
|
870 |
}
|
marci@992
|
871 |
|
marci@992
|
872 |
void firstIn(Edge& i, const Node& n) const {
|
marci@992
|
873 |
Parent::firstIn(i, n);
|
marci@992
|
874 |
i.backward=false;
|
marci@992
|
875 |
while (*static_cast<GraphEdge*>(&i)!=INVALID &&
|
marci@1269
|
876 |
!(*forward_filter)[i]) Parent::nextIn(i);
|
marci@992
|
877 |
if (*static_cast<GraphEdge*>(&i)==INVALID) {
|
marci@992
|
878 |
Parent::firstOut(i, n);
|
marci@992
|
879 |
i.backward=true;
|
marci@992
|
880 |
while (*static_cast<GraphEdge*>(&i)!=INVALID &&
|
marci@992
|
881 |
!(*backward_filter)[i]) Parent::nextOut(i);
|
marci@992
|
882 |
}
|
marci@992
|
883 |
}
|
marci@992
|
884 |
|
marci@992
|
885 |
void firstOut(Edge& i, const Node& n) const {
|
marci@992
|
886 |
Parent::firstOut(i, n);
|
marci@992
|
887 |
i.backward=false;
|
marci@992
|
888 |
while (*static_cast<GraphEdge*>(&i)!=INVALID &&
|
marci@992
|
889 |
!(*forward_filter)[i]) Parent::nextOut(i);
|
marci@992
|
890 |
if (*static_cast<GraphEdge*>(&i)==INVALID) {
|
marci@992
|
891 |
Parent::firstIn(i, n);
|
marci@992
|
892 |
i.backward=true;
|
marci@992
|
893 |
while (*static_cast<GraphEdge*>(&i)!=INVALID &&
|
marci@992
|
894 |
!(*backward_filter)[i]) Parent::nextIn(i);
|
marci@992
|
895 |
}
|
marci@992
|
896 |
}
|
marci@992
|
897 |
|
marci@992
|
898 |
void next(Node& i) const {
|
marci@992
|
899 |
Parent::next(i);
|
marci@992
|
900 |
}
|
marci@992
|
901 |
|
marci@992
|
902 |
void next(Edge& i) const {
|
marci@992
|
903 |
if (!(i.backward)) {
|
marci@992
|
904 |
Parent::next(i);
|
marci@992
|
905 |
while (*static_cast<GraphEdge*>(&i)!=INVALID &&
|
marci@992
|
906 |
!(*forward_filter)[i]) Parent::next(i);
|
marci@992
|
907 |
if (*static_cast<GraphEdge*>(&i)==INVALID) {
|
marci@992
|
908 |
Parent::first(i);
|
marci@992
|
909 |
i.backward=true;
|
marci@992
|
910 |
while (*static_cast<GraphEdge*>(&i)!=INVALID &&
|
marci@992
|
911 |
!(*backward_filter)[i]) Parent::next(i);
|
marci@992
|
912 |
}
|
marci@992
|
913 |
} else {
|
marci@992
|
914 |
Parent::next(i);
|
marci@992
|
915 |
while (*static_cast<GraphEdge*>(&i)!=INVALID &&
|
marci@992
|
916 |
!(*backward_filter)[i]) Parent::next(i);
|
marci@992
|
917 |
}
|
marci@992
|
918 |
}
|
marci@992
|
919 |
|
marci@992
|
920 |
void nextIn(Edge& i) const {
|
marci@992
|
921 |
if (!(i.backward)) {
|
marci@992
|
922 |
Node n=Parent::target(i);
|
marci@992
|
923 |
Parent::nextIn(i);
|
marci@992
|
924 |
while (*static_cast<GraphEdge*>(&i)!=INVALID &&
|
marci@992
|
925 |
!(*forward_filter)[i]) Parent::nextIn(i);
|
marci@992
|
926 |
if (*static_cast<GraphEdge*>(&i)==INVALID) {
|
marci@992
|
927 |
Parent::firstOut(i, n);
|
marci@992
|
928 |
i.backward=true;
|
marci@992
|
929 |
while (*static_cast<GraphEdge*>(&i)!=INVALID &&
|
marci@992
|
930 |
!(*backward_filter)[i]) Parent::nextOut(i);
|
marci@992
|
931 |
}
|
marci@992
|
932 |
} else {
|
marci@992
|
933 |
Parent::nextOut(i);
|
marci@992
|
934 |
while (*static_cast<GraphEdge*>(&i)!=INVALID &&
|
marci@992
|
935 |
!(*backward_filter)[i]) Parent::nextOut(i);
|
marci@992
|
936 |
}
|
marci@992
|
937 |
}
|
marci@992
|
938 |
|
marci@992
|
939 |
void nextOut(Edge& i) const {
|
marci@992
|
940 |
if (!(i.backward)) {
|
marci@992
|
941 |
Node n=Parent::source(i);
|
marci@992
|
942 |
Parent::nextOut(i);
|
marci@992
|
943 |
while (*static_cast<GraphEdge*>(&i)!=INVALID &&
|
marci@992
|
944 |
!(*forward_filter)[i]) Parent::nextOut(i);
|
marci@992
|
945 |
if (*static_cast<GraphEdge*>(&i)==INVALID) {
|
marci@992
|
946 |
Parent::firstIn(i, n);
|
marci@992
|
947 |
i.backward=true;
|
marci@992
|
948 |
while (*static_cast<GraphEdge*>(&i)!=INVALID &&
|
marci@992
|
949 |
!(*backward_filter)[i]) Parent::nextIn(i);
|
marci@992
|
950 |
}
|
marci@992
|
951 |
} else {
|
marci@992
|
952 |
Parent::nextIn(i);
|
marci@992
|
953 |
while (*static_cast<GraphEdge*>(&i)!=INVALID &&
|
marci@992
|
954 |
!(*backward_filter)[i]) Parent::nextIn(i);
|
marci@992
|
955 |
}
|
marci@992
|
956 |
}
|
marci@992
|
957 |
|
marci@992
|
958 |
Node source(Edge e) const {
|
marci@992
|
959 |
return ((!e.backward) ? this->graph->source(e) : this->graph->target(e)); }
|
marci@992
|
960 |
Node target(Edge e) const {
|
marci@992
|
961 |
return ((!e.backward) ? this->graph->target(e) : this->graph->source(e)); }
|
marci@992
|
962 |
|
klao@1951
|
963 |
/// Gives back the opposite edge.
|
alpar@1949
|
964 |
|
klao@1951
|
965 |
///\e
|
klao@1951
|
966 |
///
|
marci@992
|
967 |
Edge opposite(const Edge& e) const {
|
marci@992
|
968 |
Edge f=e;
|
marci@992
|
969 |
f.backward=!f.backward;
|
marci@992
|
970 |
return f;
|
marci@992
|
971 |
}
|
marci@992
|
972 |
|
klao@1951
|
973 |
///\e
|
alpar@1949
|
974 |
|
klao@1951
|
975 |
/// \warning This is a linear time operation and works only if
|
klao@1951
|
976 |
/// \c Graph::EdgeIt is defined.
|
klao@1951
|
977 |
/// \todo hmm
|
marci@992
|
978 |
int edgeNum() const {
|
marci@992
|
979 |
int i=0;
|
marci@992
|
980 |
Edge e;
|
marci@992
|
981 |
for (first(e); e!=INVALID; next(e)) ++i;
|
marci@992
|
982 |
return i;
|
marci@992
|
983 |
}
|
marci@992
|
984 |
|
marci@992
|
985 |
bool forward(const Edge& e) const { return !e.backward; }
|
marci@992
|
986 |
bool backward(const Edge& e) const { return e.backward; }
|
marci@992
|
987 |
|
klao@1951
|
988 |
///\e
|
alpar@1949
|
989 |
|
klao@1951
|
990 |
/// \c SubBidirGraphAdaptorBase<..., ..., ...>::EdgeMap contains two
|
klao@1951
|
991 |
/// _Graph::EdgeMap one for the forward edges and
|
klao@1951
|
992 |
/// one for the backward edges.
|
marci@992
|
993 |
template <typename T>
|
marci@992
|
994 |
class EdgeMap {
|
marci@992
|
995 |
template <typename TT> friend class EdgeMap;
|
marci@992
|
996 |
typename _Graph::template EdgeMap<T> forward_map, backward_map;
|
marci@992
|
997 |
public:
|
marci@992
|
998 |
typedef T Value;
|
marci@992
|
999 |
typedef Edge Key;
|
marci@992
|
1000 |
|
alpar@1401
|
1001 |
EdgeMap(const SubBidirGraphAdaptorBase<_Graph,
|
marci@992
|
1002 |
ForwardFilterMap, BackwardFilterMap>& g) :
|
marci@992
|
1003 |
forward_map(*(g.graph)), backward_map(*(g.graph)) { }
|
marci@992
|
1004 |
|
alpar@1401
|
1005 |
EdgeMap(const SubBidirGraphAdaptorBase<_Graph,
|
marci@992
|
1006 |
ForwardFilterMap, BackwardFilterMap>& g, T a) :
|
marci@992
|
1007 |
forward_map(*(g.graph), a), backward_map(*(g.graph), a) { }
|
marci@992
|
1008 |
|
marci@992
|
1009 |
void set(Edge e, T a) {
|
marci@992
|
1010 |
if (!e.backward)
|
marci@992
|
1011 |
forward_map.set(e, a);
|
marci@992
|
1012 |
else
|
marci@992
|
1013 |
backward_map.set(e, a);
|
marci@992
|
1014 |
}
|
marci@992
|
1015 |
|
marci@992
|
1016 |
// typename _Graph::template EdgeMap<T>::ConstReference
|
marci@992
|
1017 |
// operator[](Edge e) const {
|
marci@992
|
1018 |
// if (!e.backward)
|
marci@992
|
1019 |
// return forward_map[e];
|
marci@992
|
1020 |
// else
|
marci@992
|
1021 |
// return backward_map[e];
|
marci@992
|
1022 |
// }
|
marci@992
|
1023 |
|
marci@992
|
1024 |
// typename _Graph::template EdgeMap<T>::Reference
|
marci@1016
|
1025 |
T operator[](Edge e) const {
|
marci@992
|
1026 |
if (!e.backward)
|
marci@992
|
1027 |
return forward_map[e];
|
marci@992
|
1028 |
else
|
marci@992
|
1029 |
return backward_map[e];
|
marci@992
|
1030 |
}
|
marci@992
|
1031 |
|
marci@992
|
1032 |
void update() {
|
marci@992
|
1033 |
forward_map.update();
|
marci@992
|
1034 |
backward_map.update();
|
marci@992
|
1035 |
}
|
marci@992
|
1036 |
};
|
marci@992
|
1037 |
|
marci@992
|
1038 |
};
|
marci@569
|
1039 |
|
marci@650
|
1040 |
|
klao@1951
|
1041 |
///\brief An adaptor for composing a subgraph of a
|
klao@1951
|
1042 |
/// bidirected graph made from a directed one.
|
klao@1951
|
1043 |
///\ingroup graph_adaptors
|
klao@1951
|
1044 |
///
|
klao@1951
|
1045 |
/// An adaptor for composing a subgraph of a
|
klao@1951
|
1046 |
/// bidirected graph made from a directed one.
|
klao@1951
|
1047 |
///
|
klao@1951
|
1048 |
///\warning Graph adaptors are in even more experimental state
|
klao@1951
|
1049 |
///than the other
|
klao@1951
|
1050 |
///parts of the lib. Use them at you own risk.
|
klao@1951
|
1051 |
///
|
klao@1952
|
1052 |
/// Let \f$ G=(V, A) \f$ be a directed graph and for each directed edge
|
klao@1952
|
1053 |
///\f$ e\in A \f$, let \f$ \bar e \f$ denote the edge obtained by
|
klao@1951
|
1054 |
/// reversing its orientation. We are given moreover two bool valued
|
klao@1951
|
1055 |
/// maps on the edge-set,
|
klao@1952
|
1056 |
///\f$ forward\_filter \f$, and \f$ backward\_filter \f$.
|
klao@1951
|
1057 |
/// SubBidirGraphAdaptor implements the graph structure with node-set
|
klao@1952
|
1058 |
///\f$ V \f$ and edge-set
|
klao@1952
|
1059 |
///\f$ \{e : e\in A \mbox{ and } forward\_filter(e) \mbox{ is true}\}+\{\bar e : e\in A \mbox{ and } backward\_filter(e) \mbox{ is true}\} \f$.
|
klao@1951
|
1060 |
/// The purpose of writing + instead of union is because parallel
|
klao@1951
|
1061 |
/// edges can arise. (Similarly, antiparallel edges also can arise).
|
klao@1951
|
1062 |
/// In other words, a subgraph of the bidirected graph obtained, which
|
klao@1951
|
1063 |
/// is given by orienting the edges of the original graph in both directions.
|
klao@1951
|
1064 |
/// As the oppositely directed edges are logically different,
|
klao@1951
|
1065 |
/// the maps are able to attach different values for them.
|
klao@1951
|
1066 |
///
|
klao@1951
|
1067 |
/// An example for such a construction is \c RevGraphAdaptor where the
|
klao@1951
|
1068 |
/// forward_filter is everywhere false and the backward_filter is
|
klao@1951
|
1069 |
/// everywhere true. We note that for sake of efficiency,
|
klao@1951
|
1070 |
/// \c RevGraphAdaptor is implemented in a different way.
|
klao@1951
|
1071 |
/// But BidirGraphAdaptor is obtained from
|
klao@1951
|
1072 |
/// SubBidirGraphAdaptor by considering everywhere true
|
klao@1951
|
1073 |
/// valued maps both for forward_filter and backward_filter.
|
klao@1951
|
1074 |
///
|
klao@1951
|
1075 |
/// The most important application of SubBidirGraphAdaptor
|
klao@1951
|
1076 |
/// is ResGraphAdaptor, which stands for the residual graph in directed
|
klao@1951
|
1077 |
/// flow and circulation problems.
|
klao@1951
|
1078 |
/// As adaptors usually, the SubBidirGraphAdaptor implements the
|
klao@1951
|
1079 |
/// above mentioned graph structure without its physical storage,
|
klao@1951
|
1080 |
/// that is the whole stuff is stored in constant memory.
|
marci@992
|
1081 |
template<typename _Graph,
|
marci@650
|
1082 |
typename ForwardFilterMap, typename BackwardFilterMap>
|
alpar@1401
|
1083 |
class SubBidirGraphAdaptor :
|
marci@992
|
1084 |
public IterableGraphExtender<
|
alpar@1401
|
1085 |
SubBidirGraphAdaptorBase<_Graph, ForwardFilterMap, BackwardFilterMap> > {
|
marci@650
|
1086 |
public:
|
marci@992
|
1087 |
typedef _Graph Graph;
|
marci@992
|
1088 |
typedef IterableGraphExtender<
|
alpar@1401
|
1089 |
SubBidirGraphAdaptorBase<
|
marci@992
|
1090 |
_Graph, ForwardFilterMap, BackwardFilterMap> > Parent;
|
marci@569
|
1091 |
protected:
|
alpar@1401
|
1092 |
SubBidirGraphAdaptor() { }
|
marci@992
|
1093 |
public:
|
alpar@1401
|
1094 |
SubBidirGraphAdaptor(_Graph& _graph, ForwardFilterMap& _forward_filter,
|
marci@992
|
1095 |
BackwardFilterMap& _backward_filter) {
|
marci@992
|
1096 |
setGraph(_graph);
|
marci@992
|
1097 |
setForwardFilterMap(_forward_filter);
|
marci@992
|
1098 |
setBackwardFilterMap(_backward_filter);
|
marci@992
|
1099 |
}
|
marci@992
|
1100 |
};
|
marci@650
|
1101 |
|
marci@569
|
1102 |
|
marci@650
|
1103 |
|
klao@1951
|
1104 |
///\brief An adaptor for composing bidirected graph from a directed one.
|
klao@1951
|
1105 |
///\ingroup graph_adaptors
|
klao@1951
|
1106 |
///
|
klao@1951
|
1107 |
///\warning Graph adaptors are in even more experimental state
|
klao@1951
|
1108 |
///than the other
|
klao@1951
|
1109 |
///parts of the lib. Use them at you own risk.
|
klao@1951
|
1110 |
///
|
klao@1951
|
1111 |
/// An adaptor for composing bidirected graph from a directed one.
|
klao@1951
|
1112 |
/// A bidirected graph is composed over the directed one without physical
|
klao@1951
|
1113 |
/// storage. As the oppositely directed edges are logically different ones
|
klao@1951
|
1114 |
/// the maps are able to attach different values for them.
|
marci@650
|
1115 |
template<typename Graph>
|
alpar@1401
|
1116 |
class BidirGraphAdaptor :
|
alpar@1401
|
1117 |
public SubBidirGraphAdaptor<
|
marci@650
|
1118 |
Graph,
|
marci@650
|
1119 |
ConstMap<typename Graph::Edge, bool>,
|
marci@650
|
1120 |
ConstMap<typename Graph::Edge, bool> > {
|
marci@650
|
1121 |
public:
|
alpar@1401
|
1122 |
typedef SubBidirGraphAdaptor<
|
marci@650
|
1123 |
Graph,
|
marci@650
|
1124 |
ConstMap<typename Graph::Edge, bool>,
|
marci@650
|
1125 |
ConstMap<typename Graph::Edge, bool> > Parent;
|
marci@650
|
1126 |
protected:
|
marci@650
|
1127 |
ConstMap<typename Graph::Edge, bool> cm;
|
marci@650
|
1128 |
|
alpar@1401
|
1129 |
BidirGraphAdaptor() : Parent(), cm(true) {
|
marci@655
|
1130 |
Parent::setForwardFilterMap(cm);
|
marci@655
|
1131 |
Parent::setBackwardFilterMap(cm);
|
marci@655
|
1132 |
}
|
marci@650
|
1133 |
public:
|
alpar@1401
|
1134 |
BidirGraphAdaptor(Graph& _graph) : Parent(), cm(true) {
|
marci@650
|
1135 |
Parent::setGraph(_graph);
|
marci@650
|
1136 |
Parent::setForwardFilterMap(cm);
|
marci@650
|
1137 |
Parent::setBackwardFilterMap(cm);
|
marci@650
|
1138 |
}
|
marci@738
|
1139 |
|
marci@738
|
1140 |
int edgeNum() const {
|
marci@738
|
1141 |
return 2*this->graph->edgeNum();
|
marci@738
|
1142 |
}
|
marci@650
|
1143 |
};
|
marci@650
|
1144 |
|
marci@650
|
1145 |
|
marci@650
|
1146 |
template<typename Graph, typename Number,
|
marci@650
|
1147 |
typename CapacityMap, typename FlowMap>
|
marci@658
|
1148 |
class ResForwardFilter {
|
marci@658
|
1149 |
// const Graph* graph;
|
marci@650
|
1150 |
const CapacityMap* capacity;
|
marci@650
|
1151 |
const FlowMap* flow;
|
marci@650
|
1152 |
public:
|
marci@658
|
1153 |
ResForwardFilter(/*const Graph& _graph, */
|
marci@658
|
1154 |
const CapacityMap& _capacity, const FlowMap& _flow) :
|
marci@658
|
1155 |
/*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
|
marci@658
|
1156 |
ResForwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
|
marci@656
|
1157 |
void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
|
marci@656
|
1158 |
void setFlow(const FlowMap& _flow) { flow=&_flow; }
|
marci@650
|
1159 |
bool operator[](const typename Graph::Edge& e) const {
|
marci@738
|
1160 |
return (Number((*flow)[e]) < Number((*capacity)[e]));
|
marci@650
|
1161 |
}
|
marci@650
|
1162 |
};
|
marci@650
|
1163 |
|
marci@650
|
1164 |
template<typename Graph, typename Number,
|
marci@650
|
1165 |
typename CapacityMap, typename FlowMap>
|
marci@658
|
1166 |
class ResBackwardFilter {
|
marci@650
|
1167 |
const CapacityMap* capacity;
|
marci@650
|
1168 |
const FlowMap* flow;
|
marci@650
|
1169 |
public:
|
marci@658
|
1170 |
ResBackwardFilter(/*const Graph& _graph,*/
|
marci@658
|
1171 |
const CapacityMap& _capacity, const FlowMap& _flow) :
|
marci@658
|
1172 |
/*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
|
marci@658
|
1173 |
ResBackwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
|
marci@656
|
1174 |
void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
|
marci@656
|
1175 |
void setFlow(const FlowMap& _flow) { flow=&_flow; }
|
marci@650
|
1176 |
bool operator[](const typename Graph::Edge& e) const {
|
marci@738
|
1177 |
return (Number(0) < Number((*flow)[e]));
|
marci@650
|
1178 |
}
|
marci@650
|
1179 |
};
|
marci@650
|
1180 |
|
marci@653
|
1181 |
|
klao@1951
|
1182 |
///\brief An adaptor for composing the residual
|
klao@1951
|
1183 |
///graph for directed flow and circulation problems.
|
klao@1951
|
1184 |
///\ingroup graph_adaptors
|
klao@1951
|
1185 |
///
|
klao@1951
|
1186 |
///An adaptor for composing the residual graph for
|
klao@1951
|
1187 |
///directed flow and circulation problems.
|
klao@1952
|
1188 |
///Let \f$ G=(V, A) \f$ be a directed graph and let \f$ F \f$ be a
|
klao@1951
|
1189 |
///number type. Let moreover
|
klao@1952
|
1190 |
///\f$ f,c:A\to F \f$, be functions on the edge-set.
|
klao@1952
|
1191 |
///In the appications of ResGraphAdaptor, \f$ f \f$ usually stands for a flow
|
klao@1952
|
1192 |
///and \f$ c \f$ for a capacity function.
|
klao@1951
|
1193 |
///Suppose that a graph instange \c g of type
|
klao@1952
|
1194 |
///\c ListGraph implements \f$ G \f$.
|
klao@1951
|
1195 |
///\code
|
klao@1951
|
1196 |
/// ListGraph g;
|
klao@1951
|
1197 |
///\endcode
|
klao@1951
|
1198 |
///Then RevGraphAdaptor implements the graph structure with node-set
|
klao@1952
|
1199 |
///\f$ V \f$ and edge-set \f$ A_{forward}\cup A_{backward} \f$, where
|
klao@1952
|
1200 |
///\f$ A_{forward}=\{uv : uv\in A, f(uv)<c(uv)\} \f$ and
|
klao@1952
|
1201 |
///\f$ A_{backward}=\{vu : uv\in A, f(uv)>0\} \f$,
|
klao@1951
|
1202 |
///i.e. the so called residual graph.
|
klao@1952
|
1203 |
///When we take the union \f$ A_{forward}\cup A_{backward} \f$,
|
klao@1951
|
1204 |
///multilicities are counted, i.e. if an edge is in both
|
klao@1952
|
1205 |
///\f$ A_{forward} \f$ and \f$ A_{backward} \f$, then in the adaptor it
|
klao@1951
|
1206 |
///appears twice.
|
klao@1951
|
1207 |
///The following code shows how
|
klao@1951
|
1208 |
///such an instance can be constructed.
|
klao@1951
|
1209 |
///\code
|
klao@1951
|
1210 |
///typedef ListGraph Graph;
|
klao@1951
|
1211 |
///Graph::EdgeMap<int> f(g);
|
klao@1951
|
1212 |
///Graph::EdgeMap<int> c(g);
|
klao@1951
|
1213 |
///ResGraphAdaptor<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > gw(g);
|
klao@1951
|
1214 |
///\endcode
|
klao@1951
|
1215 |
///\author Marton Makai
|
klao@1951
|
1216 |
///
|
marci@650
|
1217 |
template<typename Graph, typename Number,
|
marci@650
|
1218 |
typename CapacityMap, typename FlowMap>
|
alpar@1401
|
1219 |
class ResGraphAdaptor :
|
alpar@1401
|
1220 |
public SubBidirGraphAdaptor<
|
marci@650
|
1221 |
Graph,
|
marci@658
|
1222 |
ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,
|
marci@658
|
1223 |
ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > {
|
marci@650
|
1224 |
public:
|
alpar@1401
|
1225 |
typedef SubBidirGraphAdaptor<
|
marci@650
|
1226 |
Graph,
|
marci@658
|
1227 |
ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,
|
marci@658
|
1228 |
ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > Parent;
|
marci@650
|
1229 |
protected:
|
marci@650
|
1230 |
const CapacityMap* capacity;
|
marci@650
|
1231 |
FlowMap* flow;
|
marci@658
|
1232 |
ResForwardFilter<Graph, Number, CapacityMap, FlowMap> forward_filter;
|
marci@658
|
1233 |
ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> backward_filter;
|
alpar@1401
|
1234 |
ResGraphAdaptor() : Parent(),
|
marci@658
|
1235 |
capacity(0), flow(0) { }
|
marci@658
|
1236 |
void setCapacityMap(const CapacityMap& _capacity) {
|
marci@658
|
1237 |
capacity=&_capacity;
|
marci@658
|
1238 |
forward_filter.setCapacity(_capacity);
|
marci@658
|
1239 |
backward_filter.setCapacity(_capacity);
|
marci@658
|
1240 |
}
|
marci@658
|
1241 |
void setFlowMap(FlowMap& _flow) {
|
marci@658
|
1242 |
flow=&_flow;
|
marci@658
|
1243 |
forward_filter.setFlow(_flow);
|
marci@658
|
1244 |
backward_filter.setFlow(_flow);
|
marci@658
|
1245 |
}
|
marci@650
|
1246 |
public:
|
alpar@1401
|
1247 |
ResGraphAdaptor(Graph& _graph, const CapacityMap& _capacity,
|
marci@650
|
1248 |
FlowMap& _flow) :
|
marci@650
|
1249 |
Parent(), capacity(&_capacity), flow(&_flow),
|
marci@658
|
1250 |
forward_filter(/*_graph,*/ _capacity, _flow),
|
marci@658
|
1251 |
backward_filter(/*_graph,*/ _capacity, _flow) {
|
marci@650
|
1252 |
Parent::setGraph(_graph);
|
marci@650
|
1253 |
Parent::setForwardFilterMap(forward_filter);
|
marci@650
|
1254 |
Parent::setBackwardFilterMap(backward_filter);
|
marci@650
|
1255 |
}
|
marci@650
|
1256 |
|
marci@660
|
1257 |
typedef typename Parent::Edge Edge;
|
marci@660
|
1258 |
|
marci@660
|
1259 |
void augment(const Edge& e, Number a) const {
|
marci@650
|
1260 |
if (Parent::forward(e))
|
marci@650
|
1261 |
flow->set(e, (*flow)[e]+a);
|
marci@650
|
1262 |
else
|
marci@650
|
1263 |
flow->set(e, (*flow)[e]-a);
|
marci@650
|
1264 |
}
|
marci@650
|
1265 |
|
klao@1951
|
1266 |
/// \brief Residual capacity map.
|
klao@1951
|
1267 |
///
|
klao@1951
|
1268 |
/// In generic residual graphs the residual capacity can be obtained
|
klao@1951
|
1269 |
/// as a map.
|
marci@660
|
1270 |
class ResCap {
|
marci@660
|
1271 |
protected:
|
alpar@1401
|
1272 |
const ResGraphAdaptor<Graph, Number, CapacityMap, FlowMap>* res_graph;
|
marci@660
|
1273 |
public:
|
alpar@987
|
1274 |
typedef Number Value;
|
alpar@987
|
1275 |
typedef Edge Key;
|
alpar@1401
|
1276 |
ResCap(const ResGraphAdaptor<Graph, Number, CapacityMap, FlowMap>&
|
marci@888
|
1277 |
_res_graph) : res_graph(&_res_graph) { }
|
marci@660
|
1278 |
Number operator[](const Edge& e) const {
|
marci@660
|
1279 |
if (res_graph->forward(e))
|
marci@660
|
1280 |
return (*(res_graph->capacity))[e]-(*(res_graph->flow))[e];
|
marci@660
|
1281 |
else
|
marci@660
|
1282 |
return (*(res_graph->flow))[e];
|
marci@660
|
1283 |
}
|
marci@660
|
1284 |
};
|
marci@660
|
1285 |
|
alpar@1401
|
1286 |
// KEEP_MAPS(Parent, ResGraphAdaptor);
|
marci@650
|
1287 |
};
|
marci@650
|
1288 |
|
marci@650
|
1289 |
|
marci@998
|
1290 |
|
marci@998
|
1291 |
template <typename _Graph, typename FirstOutEdgesMap>
|
alpar@1401
|
1292 |
class ErasingFirstGraphAdaptorBase : public GraphAdaptorBase<_Graph> {
|
marci@998
|
1293 |
public:
|
marci@998
|
1294 |
typedef _Graph Graph;
|
alpar@1401
|
1295 |
typedef GraphAdaptorBase<_Graph> Parent;
|
marci@998
|
1296 |
protected:
|
marci@998
|
1297 |
FirstOutEdgesMap* first_out_edges;
|
alpar@1401
|
1298 |
ErasingFirstGraphAdaptorBase() : Parent(),
|
marci@998
|
1299 |
first_out_edges(0) { }
|
marci@998
|
1300 |
|
marci@998
|
1301 |
void setFirstOutEdgesMap(FirstOutEdgesMap& _first_out_edges) {
|
marci@998
|
1302 |
first_out_edges=&_first_out_edges;
|
marci@998
|
1303 |
}
|
marci@998
|
1304 |
|
marci@998
|
1305 |
public:
|
marci@998
|
1306 |
|
marci@998
|
1307 |
typedef typename Parent::Node Node;
|
marci@998
|
1308 |
typedef typename Parent::Edge Edge;
|
marci@998
|
1309 |
|
marci@998
|
1310 |
void firstOut(Edge& i, const Node& n) const {
|
marci@998
|
1311 |
i=(*first_out_edges)[n];
|
marci@998
|
1312 |
}
|
marci@998
|
1313 |
|
marci@998
|
1314 |
void erase(const Edge& e) const {
|
marci@998
|
1315 |
Node n=source(e);
|
marci@998
|
1316 |
Edge f=e;
|
marci@998
|
1317 |
Parent::nextOut(f);
|
marci@998
|
1318 |
first_out_edges->set(n, f);
|
marci@998
|
1319 |
}
|
marci@998
|
1320 |
};
|
marci@998
|
1321 |
|
marci@998
|
1322 |
|
klao@1951
|
1323 |
///\brief For blocking flows.
|
klao@1951
|
1324 |
///\ingroup graph_adaptors
|
klao@1951
|
1325 |
///
|
klao@1951
|
1326 |
///\warning Graph adaptors are in even more
|
klao@1951
|
1327 |
///experimental state than the other
|
klao@1951
|
1328 |
///parts of the lib. Use them at you own risk.
|
klao@1951
|
1329 |
///
|
klao@1951
|
1330 |
///This graph adaptor is used for on-the-fly
|
klao@1951
|
1331 |
///Dinits blocking flow computations.
|
klao@1951
|
1332 |
///For each node, an out-edge is stored which is used when the
|
klao@1951
|
1333 |
///\code
|
klao@1951
|
1334 |
///OutEdgeIt& first(OutEdgeIt&, const Node&)
|
klao@1951
|
1335 |
///\endcode
|
klao@1951
|
1336 |
///is called.
|
klao@1951
|
1337 |
///
|
klao@1951
|
1338 |
///\author Marton Makai
|
klao@1951
|
1339 |
///
|
marci@998
|
1340 |
template <typename _Graph, typename FirstOutEdgesMap>
|
alpar@1401
|
1341 |
class ErasingFirstGraphAdaptor :
|
marci@998
|
1342 |
public IterableGraphExtender<
|
alpar@1401
|
1343 |
ErasingFirstGraphAdaptorBase<_Graph, FirstOutEdgesMap> > {
|
marci@650
|
1344 |
public:
|
marci@998
|
1345 |
typedef _Graph Graph;
|
marci@998
|
1346 |
typedef IterableGraphExtender<
|
alpar@1401
|
1347 |
ErasingFirstGraphAdaptorBase<_Graph, FirstOutEdgesMap> > Parent;
|
alpar@1401
|
1348 |
ErasingFirstGraphAdaptor(Graph& _graph,
|
marci@998
|
1349 |
FirstOutEdgesMap& _first_out_edges) {
|
marci@998
|
1350 |
setGraph(_graph);
|
marci@998
|
1351 |
setFirstOutEdgesMap(_first_out_edges);
|
marci@998
|
1352 |
}
|
marci@1019
|
1353 |
|
marci@998
|
1354 |
};
|
marci@556
|
1355 |
|
deba@1472
|
1356 |
template <typename _Graph>
|
deba@1697
|
1357 |
class SplitGraphAdaptorBase
|
deba@1697
|
1358 |
: public GraphAdaptorBase<_Graph> {
|
deba@1697
|
1359 |
public:
|
deba@1697
|
1360 |
typedef GraphAdaptorBase<_Graph> Parent;
|
deba@1697
|
1361 |
|
deba@1697
|
1362 |
class Node;
|
deba@1697
|
1363 |
class Edge;
|
deba@1697
|
1364 |
template <typename T> class NodeMap;
|
deba@1697
|
1365 |
template <typename T> class EdgeMap;
|
deba@1697
|
1366 |
|
deba@1697
|
1367 |
|
deba@1697
|
1368 |
class Node : public Parent::Node {
|
deba@1697
|
1369 |
friend class SplitGraphAdaptorBase;
|
deba@1697
|
1370 |
template <typename T> friend class NodeMap;
|
deba@1697
|
1371 |
typedef typename Parent::Node NodeParent;
|
deba@1697
|
1372 |
private:
|
deba@1697
|
1373 |
|
deba@1697
|
1374 |
bool entry;
|
deba@1697
|
1375 |
Node(typename Parent::Node _node, bool _entry)
|
deba@1697
|
1376 |
: Parent::Node(_node), entry(_entry) {}
|
deba@1697
|
1377 |
|
deba@1697
|
1378 |
public:
|
deba@1697
|
1379 |
Node() {}
|
deba@1697
|
1380 |
Node(Invalid) : NodeParent(INVALID), entry(true) {}
|
deba@1697
|
1381 |
|
deba@1697
|
1382 |
bool operator==(const Node& node) const {
|
deba@1697
|
1383 |
return NodeParent::operator==(node) && entry == node.entry;
|
deba@1697
|
1384 |
}
|
deba@1697
|
1385 |
|
deba@1697
|
1386 |
bool operator!=(const Node& node) const {
|
deba@1697
|
1387 |
return !(*this == node);
|
deba@1697
|
1388 |
}
|
deba@1697
|
1389 |
|
deba@1697
|
1390 |
bool operator<(const Node& node) const {
|
deba@1697
|
1391 |
return NodeParent::operator<(node) ||
|
deba@1697
|
1392 |
(NodeParent::operator==(node) && entry < node.entry);
|
deba@1697
|
1393 |
}
|
deba@1697
|
1394 |
};
|
deba@1697
|
1395 |
|
klao@1951
|
1396 |
/// \todo May we want VARIANT/union type
|
deba@1697
|
1397 |
class Edge : public Parent::Edge {
|
deba@1697
|
1398 |
friend class SplitGraphAdaptorBase;
|
deba@1697
|
1399 |
template <typename T> friend class EdgeMap;
|
deba@1697
|
1400 |
private:
|
deba@1697
|
1401 |
typedef typename Parent::Edge EdgeParent;
|
deba@1697
|
1402 |
typedef typename Parent::Node NodeParent;
|
deba@1697
|
1403 |
NodeParent bind;
|
deba@1697
|
1404 |
|
deba@1697
|
1405 |
Edge(const EdgeParent& edge, const NodeParent& node)
|
deba@1697
|
1406 |
: EdgeParent(edge), bind(node) {}
|
deba@1697
|
1407 |
public:
|
deba@1697
|
1408 |
Edge() {}
|
deba@1697
|
1409 |
Edge(Invalid) : EdgeParent(INVALID), bind(INVALID) {}
|
deba@1697
|
1410 |
|
deba@1697
|
1411 |
bool operator==(const Edge& edge) const {
|
deba@1697
|
1412 |
return EdgeParent::operator==(edge) && bind == edge.bind;
|
deba@1697
|
1413 |
}
|
deba@1697
|
1414 |
|
deba@1697
|
1415 |
bool operator!=(const Edge& edge) const {
|
deba@1697
|
1416 |
return !(*this == edge);
|
deba@1697
|
1417 |
}
|
deba@1697
|
1418 |
|
deba@1697
|
1419 |
bool operator<(const Edge& edge) const {
|
deba@1697
|
1420 |
return EdgeParent::operator<(edge) ||
|
deba@1697
|
1421 |
(EdgeParent::operator==(edge) && bind < edge.bind);
|
deba@1697
|
1422 |
}
|
deba@1697
|
1423 |
};
|
deba@1697
|
1424 |
|
deba@1697
|
1425 |
void first(Node& node) const {
|
deba@1697
|
1426 |
Parent::first(node);
|
deba@1697
|
1427 |
node.entry = true;
|
deba@1697
|
1428 |
}
|
deba@1697
|
1429 |
|
deba@1697
|
1430 |
void next(Node& node) const {
|
deba@1697
|
1431 |
if (node.entry) {
|
deba@1697
|
1432 |
node.entry = false;
|
deba@1697
|
1433 |
} else {
|
deba@1697
|
1434 |
node.entry = true;
|
deba@1697
|
1435 |
Parent::next(node);
|
deba@1697
|
1436 |
}
|
deba@1697
|
1437 |
}
|
deba@1697
|
1438 |
|
deba@1697
|
1439 |
void first(Edge& edge) const {
|
deba@1697
|
1440 |
Parent::first(edge);
|
deba@1697
|
1441 |
if ((typename Parent::Edge&)edge == INVALID) {
|
deba@1697
|
1442 |
Parent::first(edge.bind);
|
deba@1697
|
1443 |
} else {
|
deba@1697
|
1444 |
edge.bind = INVALID;
|
deba@1697
|
1445 |
}
|
deba@1697
|
1446 |
}
|
deba@1697
|
1447 |
|
deba@1697
|
1448 |
void next(Edge& edge) const {
|
deba@1697
|
1449 |
if ((typename Parent::Edge&)edge != INVALID) {
|
deba@1697
|
1450 |
Parent::next(edge);
|
deba@1697
|
1451 |
if ((typename Parent::Edge&)edge == INVALID) {
|
deba@1697
|
1452 |
Parent::first(edge.bind);
|
deba@1697
|
1453 |
}
|
deba@1697
|
1454 |
} else {
|
deba@1697
|
1455 |
Parent::next(edge.bind);
|
deba@1697
|
1456 |
}
|
deba@1697
|
1457 |
}
|
deba@1697
|
1458 |
|
deba@1697
|
1459 |
void firstIn(Edge& edge, const Node& node) const {
|
deba@1697
|
1460 |
if (node.entry) {
|
deba@1697
|
1461 |
Parent::firstIn(edge, node);
|
deba@1697
|
1462 |
edge.bind = INVALID;
|
deba@1697
|
1463 |
} else {
|
deba@1697
|
1464 |
(typename Parent::Edge&)edge = INVALID;
|
deba@1697
|
1465 |
edge.bind = node;
|
deba@1697
|
1466 |
}
|
deba@1697
|
1467 |
}
|
deba@1697
|
1468 |
|
deba@1697
|
1469 |
void nextIn(Edge& edge) const {
|
deba@1697
|
1470 |
if ((typename Parent::Edge&)edge != INVALID) {
|
deba@1697
|
1471 |
Parent::nextIn(edge);
|
deba@1697
|
1472 |
} else {
|
deba@1697
|
1473 |
edge.bind = INVALID;
|
deba@1697
|
1474 |
}
|
deba@1697
|
1475 |
}
|
deba@1697
|
1476 |
|
deba@1697
|
1477 |
void firstOut(Edge& edge, const Node& node) const {
|
deba@1697
|
1478 |
if (!node.entry) {
|
deba@1697
|
1479 |
Parent::firstOut(edge, node);
|
deba@1697
|
1480 |
edge.bind = INVALID;
|
deba@1697
|
1481 |
} else {
|
deba@1697
|
1482 |
(typename Parent::Edge&)edge = INVALID;
|
deba@1697
|
1483 |
edge.bind = node;
|
deba@1697
|
1484 |
}
|
deba@1697
|
1485 |
}
|
deba@1697
|
1486 |
|
deba@1697
|
1487 |
void nextOut(Edge& edge) const {
|
deba@1697
|
1488 |
if ((typename Parent::Edge&)edge != INVALID) {
|
deba@1697
|
1489 |
Parent::nextOut(edge);
|
deba@1697
|
1490 |
} else {
|
deba@1697
|
1491 |
edge.bind = INVALID;
|
deba@1697
|
1492 |
}
|
deba@1697
|
1493 |
}
|
deba@1697
|
1494 |
|
deba@1697
|
1495 |
Node source(const Edge& edge) const {
|
deba@1697
|
1496 |
if ((typename Parent::Edge&)edge != INVALID) {
|
deba@1697
|
1497 |
return Node(Parent::source(edge), false);
|
deba@1697
|
1498 |
} else {
|
deba@1697
|
1499 |
return Node(edge.bind, true);
|
deba@1697
|
1500 |
}
|
deba@1697
|
1501 |
}
|
deba@1697
|
1502 |
|
deba@1697
|
1503 |
Node target(const Edge& edge) const {
|
deba@1697
|
1504 |
if ((typename Parent::Edge&)edge != INVALID) {
|
deba@1697
|
1505 |
return Node(Parent::target(edge), true);
|
deba@1697
|
1506 |
} else {
|
deba@1697
|
1507 |
return Node(edge.bind, false);
|
deba@1697
|
1508 |
}
|
deba@1697
|
1509 |
}
|
deba@1697
|
1510 |
|
deba@1697
|
1511 |
static bool entryNode(const Node& node) {
|
deba@1697
|
1512 |
return node.entry;
|
deba@1697
|
1513 |
}
|
deba@1697
|
1514 |
|
deba@1697
|
1515 |
static bool exitNode(const Node& node) {
|
deba@1697
|
1516 |
return !node.entry;
|
deba@1697
|
1517 |
}
|
deba@1697
|
1518 |
|
deba@1697
|
1519 |
static Node getEntry(const typename Parent::Node& node) {
|
deba@1697
|
1520 |
return Node(node, true);
|
deba@1697
|
1521 |
}
|
deba@1697
|
1522 |
|
deba@1697
|
1523 |
static Node getExit(const typename Parent::Node& node) {
|
deba@1697
|
1524 |
return Node(node, false);
|
deba@1697
|
1525 |
}
|
deba@1697
|
1526 |
|
deba@1697
|
1527 |
static bool originalEdge(const Edge& edge) {
|
deba@1697
|
1528 |
return (typename Parent::Edge&)edge != INVALID;
|
deba@1697
|
1529 |
}
|
deba@1697
|
1530 |
|
deba@1697
|
1531 |
static bool bindingEdge(const Edge& edge) {
|
deba@1697
|
1532 |
return edge.bind != INVALID;
|
deba@1697
|
1533 |
}
|
deba@1697
|
1534 |
|
deba@1697
|
1535 |
static Node getBindedNode(const Edge& edge) {
|
deba@1697
|
1536 |
return edge.bind;
|
deba@1697
|
1537 |
}
|
deba@1697
|
1538 |
|
deba@1697
|
1539 |
int nodeNum() const {
|
deba@1697
|
1540 |
return Parent::nodeNum() * 2;
|
deba@1697
|
1541 |
}
|
deba@1697
|
1542 |
|
deba@1697
|
1543 |
typedef CompileTimeAnd<typename Parent::NodeNumTag,
|
deba@1697
|
1544 |
typename Parent::EdgeNumTag> EdgeNumTag;
|
deba@1697
|
1545 |
|
deba@1697
|
1546 |
int edgeNum() const {
|
deba@1697
|
1547 |
return Parent::edgeNum() + Parent::nodeNum();
|
deba@1697
|
1548 |
}
|
deba@1697
|
1549 |
|
deba@1697
|
1550 |
Edge findEdge(const Node& source, const Node& target,
|
deba@1697
|
1551 |
const Edge& prev = INVALID) const {
|
deba@1697
|
1552 |
if (exitNode(source) && entryNode(target)) {
|
deba@1697
|
1553 |
return Parent::findEdge(source, target, prev);
|
deba@1697
|
1554 |
} else {
|
deba@1697
|
1555 |
if (prev == INVALID && entryNode(source) && exitNode(target) &&
|
deba@1697
|
1556 |
(typename Parent::Node&)source == (typename Parent::Node&)target) {
|
deba@1697
|
1557 |
return Edge(INVALID, source);
|
deba@1697
|
1558 |
} else {
|
deba@1697
|
1559 |
return INVALID;
|
deba@1697
|
1560 |
}
|
deba@1697
|
1561 |
}
|
deba@1697
|
1562 |
}
|
deba@1697
|
1563 |
|
deba@1697
|
1564 |
template <typename T>
|
deba@1697
|
1565 |
class NodeMap : public MapBase<Node, T> {
|
deba@1697
|
1566 |
typedef typename Parent::template NodeMap<T> NodeImpl;
|
deba@1697
|
1567 |
public:
|
deba@1697
|
1568 |
NodeMap(const SplitGraphAdaptorBase& _graph)
|
deba@1697
|
1569 |
: entry(_graph), exit(_graph) {}
|
deba@1697
|
1570 |
NodeMap(const SplitGraphAdaptorBase& _graph, const T& t)
|
deba@1697
|
1571 |
: entry(_graph, t), exit(_graph, t) {}
|
deba@1697
|
1572 |
|
deba@1697
|
1573 |
void set(const Node& key, const T& val) {
|
deba@1697
|
1574 |
if (key.entry) { entry.set(key, val); }
|
deba@1697
|
1575 |
else {exit.set(key, val); }
|
deba@1697
|
1576 |
}
|
deba@1697
|
1577 |
|
deba@1725
|
1578 |
typename MapTraits<NodeImpl>::ReturnValue
|
deba@1697
|
1579 |
operator[](const Node& key) {
|
deba@1697
|
1580 |
if (key.entry) { return entry[key]; }
|
deba@1697
|
1581 |
else { return exit[key]; }
|
deba@1697
|
1582 |
}
|
deba@1697
|
1583 |
|
deba@1725
|
1584 |
typename MapTraits<NodeImpl>::ConstReturnValue
|
deba@1725
|
1585 |
operator[](const Node& key) const {
|
deba@1697
|
1586 |
if (key.entry) { return entry[key]; }
|
deba@1697
|
1587 |
else { return exit[key]; }
|
deba@1697
|
1588 |
}
|
deba@1697
|
1589 |
|
deba@1697
|
1590 |
private:
|
deba@1697
|
1591 |
NodeImpl entry, exit;
|
deba@1697
|
1592 |
};
|
deba@1697
|
1593 |
|
deba@1697
|
1594 |
template <typename T>
|
deba@1697
|
1595 |
class EdgeMap : public MapBase<Edge, T> {
|
deba@1697
|
1596 |
typedef typename Parent::template NodeMap<T> NodeImpl;
|
deba@1697
|
1597 |
typedef typename Parent::template EdgeMap<T> EdgeImpl;
|
deba@1697
|
1598 |
public:
|
deba@1697
|
1599 |
EdgeMap(const SplitGraphAdaptorBase& _graph)
|
deba@1697
|
1600 |
: bind(_graph), orig(_graph) {}
|
deba@1697
|
1601 |
EdgeMap(const SplitGraphAdaptorBase& _graph, const T& t)
|
deba@1697
|
1602 |
: bind(_graph, t), orig(_graph, t) {}
|
deba@1697
|
1603 |
|
deba@1697
|
1604 |
void set(const Edge& key, const T& val) {
|
deba@1697
|
1605 |
if ((typename Parent::Edge&)key != INVALID) { orig.set(key, val); }
|
deba@1697
|
1606 |
else {bind.set(key.bind, val); }
|
deba@1697
|
1607 |
}
|
deba@1697
|
1608 |
|
deba@1725
|
1609 |
typename MapTraits<EdgeImpl>::ReturnValue
|
deba@1697
|
1610 |
operator[](const Edge& key) {
|
deba@1697
|
1611 |
if ((typename Parent::Edge&)key != INVALID) { return orig[key]; }
|
deba@1697
|
1612 |
else {return bind[key.bind]; }
|
deba@1697
|
1613 |
}
|
deba@1697
|
1614 |
|
deba@1725
|
1615 |
typename MapTraits<EdgeImpl>::ConstReturnValue
|
deba@1725
|
1616 |
operator[](const Edge& key) const {
|
deba@1697
|
1617 |
if ((typename Parent::Edge&)key != INVALID) { return orig[key]; }
|
deba@1697
|
1618 |
else {return bind[key.bind]; }
|
deba@1697
|
1619 |
}
|
deba@1697
|
1620 |
|
deba@1697
|
1621 |
private:
|
deba@1697
|
1622 |
typename Parent::template NodeMap<T> bind;
|
deba@1697
|
1623 |
typename Parent::template EdgeMap<T> orig;
|
deba@1697
|
1624 |
};
|
deba@1697
|
1625 |
|
deba@1697
|
1626 |
template <typename EntryMap, typename ExitMap>
|
deba@1697
|
1627 |
class CombinedNodeMap : public MapBase<Node, typename EntryMap::Value> {
|
deba@1697
|
1628 |
public:
|
deba@1697
|
1629 |
typedef MapBase<Node, typename EntryMap::Value> Parent;
|
deba@1697
|
1630 |
|
deba@1697
|
1631 |
typedef typename Parent::Key Key;
|
deba@1697
|
1632 |
typedef typename Parent::Value Value;
|
deba@1697
|
1633 |
|
deba@1697
|
1634 |
CombinedNodeMap(EntryMap& _entryMap, ExitMap& _exitMap)
|
deba@1697
|
1635 |
: entryMap(_entryMap), exitMap(_exitMap) {}
|
deba@1697
|
1636 |
|
deba@1697
|
1637 |
Value& operator[](const Key& key) {
|
deba@1697
|
1638 |
if (key.entry) {
|
deba@1697
|
1639 |
return entryMap[key];
|
deba@1697
|
1640 |
} else {
|
deba@1697
|
1641 |
return exitMap[key];
|
deba@1697
|
1642 |
}
|
deba@1697
|
1643 |
}
|
deba@1697
|
1644 |
|
deba@1697
|
1645 |
Value operator[](const Key& key) const {
|
deba@1697
|
1646 |
if (key.entry) {
|
deba@1697
|
1647 |
return entryMap[key];
|
deba@1697
|
1648 |
} else {
|
deba@1697
|
1649 |
return exitMap[key];
|
deba@1697
|
1650 |
}
|
deba@1697
|
1651 |
}
|
deba@1697
|
1652 |
|
deba@1697
|
1653 |
void set(const Key& key, const Value& value) {
|
deba@1697
|
1654 |
if (key.entry) {
|
deba@1697
|
1655 |
entryMap.set(key, value);
|
deba@1697
|
1656 |
} else {
|
deba@1697
|
1657 |
exitMap.set(key, value);
|
deba@1697
|
1658 |
}
|
deba@1697
|
1659 |
}
|
deba@1697
|
1660 |
|
deba@1697
|
1661 |
private:
|
deba@1697
|
1662 |
|
deba@1697
|
1663 |
EntryMap& entryMap;
|
deba@1697
|
1664 |
ExitMap& exitMap;
|
deba@1697
|
1665 |
|
deba@1697
|
1666 |
};
|
deba@1697
|
1667 |
|
deba@1697
|
1668 |
template <typename EdgeMap, typename NodeMap>
|
deba@1697
|
1669 |
class CombinedEdgeMap : public MapBase<Edge, typename EdgeMap::Value> {
|
deba@1697
|
1670 |
public:
|
deba@1697
|
1671 |
typedef MapBase<Edge, typename EdgeMap::Value> Parent;
|
deba@1697
|
1672 |
|
deba@1697
|
1673 |
typedef typename Parent::Key Key;
|
deba@1697
|
1674 |
typedef typename Parent::Value Value;
|
deba@1697
|
1675 |
|
deba@1697
|
1676 |
CombinedEdgeMap(EdgeMap& _edgeMap, NodeMap& _nodeMap)
|
deba@1697
|
1677 |
: edgeMap(_edgeMap), nodeMap(_nodeMap) {}
|
deba@1697
|
1678 |
|
deba@1697
|
1679 |
void set(const Edge& edge, const Value& val) {
|
deba@1697
|
1680 |
if (SplitGraphAdaptorBase::originalEdge(edge)) {
|
deba@1697
|
1681 |
edgeMap.set(edge, val);
|
deba@1697
|
1682 |
} else {
|
deba@1697
|
1683 |
nodeMap.set(SplitGraphAdaptorBase::bindedNode(edge), val);
|
deba@1697
|
1684 |
}
|
deba@1697
|
1685 |
}
|
deba@1697
|
1686 |
|
deba@1697
|
1687 |
Value operator[](const Key& edge) const {
|
deba@1697
|
1688 |
if (SplitGraphAdaptorBase::originalEdge(edge)) {
|
deba@1697
|
1689 |
return edgeMap[edge];
|
deba@1697
|
1690 |
} else {
|
deba@1697
|
1691 |
return nodeMap[SplitGraphAdaptorBase::bindedNode(edge)];
|
deba@1697
|
1692 |
}
|
deba@1697
|
1693 |
}
|
deba@1697
|
1694 |
|
deba@1697
|
1695 |
Value& operator[](const Key& edge) {
|
deba@1697
|
1696 |
if (SplitGraphAdaptorBase::originalEdge(edge)) {
|
deba@1697
|
1697 |
return edgeMap[edge];
|
deba@1697
|
1698 |
} else {
|
deba@1697
|
1699 |
return nodeMap[SplitGraphAdaptorBase::bindedNode(edge)];
|
deba@1697
|
1700 |
}
|
deba@1697
|
1701 |
}
|
deba@1697
|
1702 |
|
deba@1697
|
1703 |
private:
|
deba@1697
|
1704 |
EdgeMap& edgeMap;
|
deba@1697
|
1705 |
NodeMap& nodeMap;
|
deba@1697
|
1706 |
};
|
deba@1697
|
1707 |
|
deba@1697
|
1708 |
};
|
deba@1697
|
1709 |
|
deba@1697
|
1710 |
template <typename _Graph>
|
deba@1697
|
1711 |
class SplitGraphAdaptor
|
deba@1697
|
1712 |
: public IterableGraphExtender<SplitGraphAdaptorBase<_Graph> > {
|
deba@1697
|
1713 |
public:
|
deba@1697
|
1714 |
typedef IterableGraphExtender<SplitGraphAdaptorBase<_Graph> > Parent;
|
deba@1697
|
1715 |
|
deba@1697
|
1716 |
SplitGraphAdaptor(_Graph& graph) {
|
deba@1697
|
1717 |
Parent::setGraph(graph);
|
deba@1697
|
1718 |
}
|
deba@1697
|
1719 |
|
deba@1697
|
1720 |
|
deba@1697
|
1721 |
};
|
deba@1697
|
1722 |
|
alpar@921
|
1723 |
} //namespace lemon
|
marci@556
|
1724 |
|
alpar@1401
|
1725 |
#endif //LEMON_GRAPH_ADAPTOR_H
|
marci@556
|
1726 |
|