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