marci@556
|
1 |
// -*- c++ -*-
|
marci@556
|
2 |
#ifndef HUGO_GRAPH_WRAPPER_H
|
marci@556
|
3 |
#define HUGO_GRAPH_WRAPPER_H
|
marci@556
|
4 |
|
marci@556
|
5 |
///\ingroup gwrappers
|
marci@556
|
6 |
///\file
|
marci@556
|
7 |
///\brief Several graph wrappers.
|
marci@556
|
8 |
///
|
marci@556
|
9 |
///This file contains several useful graph wrapper functions.
|
marci@556
|
10 |
///
|
marci@556
|
11 |
///\author Marton Makai
|
marci@556
|
12 |
|
marci@556
|
13 |
#include <hugo/invalid.h>
|
marci@650
|
14 |
#include <hugo/maps.h>
|
alpar@774
|
15 |
#include <iostream>
|
marci@556
|
16 |
|
marci@556
|
17 |
namespace hugo {
|
marci@556
|
18 |
|
marci@556
|
19 |
// Graph wrappers
|
marci@556
|
20 |
|
marci@556
|
21 |
/// \addtogroup gwrappers
|
marci@556
|
22 |
/// A main parts of HUGOlib are the different graph structures,
|
marci@556
|
23 |
/// generic graph algorithms, graph concepts which couple these, and
|
marci@556
|
24 |
/// graph wrappers. While the previous ones are more or less clear, the
|
marci@556
|
25 |
/// latter notion needs further explanation.
|
marci@556
|
26 |
/// Graph wrappers are graph classes which serve for considering graph
|
marci@556
|
27 |
/// structures in different ways. A short example makes the notion much
|
marci@556
|
28 |
/// clearer.
|
marci@556
|
29 |
/// Suppose that we have an instance \c g of a directed graph
|
marci@556
|
30 |
/// type say \c ListGraph and an algorithm
|
marci@556
|
31 |
/// \code template<typename Graph> int algorithm(const Graph&); \endcode
|
marci@556
|
32 |
/// is needed to run on the reversely oriented graph.
|
marci@556
|
33 |
/// It may be expensive (in time or in memory usage) to copy
|
marci@556
|
34 |
/// \c g with the reverse orientation.
|
marci@556
|
35 |
/// Thus, a wrapper class
|
marci@556
|
36 |
/// \code template<typename Graph> class RevGraphWrapper; \endcode is used.
|
marci@556
|
37 |
/// The code looks as follows
|
marci@556
|
38 |
/// \code
|
marci@556
|
39 |
/// ListGraph g;
|
marci@556
|
40 |
/// RevGraphWrapper<ListGraph> rgw(g);
|
marci@556
|
41 |
/// int result=algorithm(rgw);
|
marci@556
|
42 |
/// \endcode
|
marci@556
|
43 |
/// After running the algorithm, the original graph \c g
|
marci@556
|
44 |
/// remains untouched. Thus the graph wrapper used above is to consider the
|
marci@556
|
45 |
/// original graph with reverse orientation.
|
marci@556
|
46 |
/// This techniques gives rise to an elegant code, and
|
marci@556
|
47 |
/// based on stable graph wrappers, complex algorithms can be
|
marci@556
|
48 |
/// implemented easily.
|
marci@556
|
49 |
/// In flow, circulation and bipartite matching problems, the residual
|
marci@556
|
50 |
/// graph is of particular importance. Combining a wrapper implementing
|
marci@556
|
51 |
/// this, shortest path algorithms and minimum mean cycle algorithms,
|
marci@556
|
52 |
/// a range of weighted and cardinality optimization algorithms can be
|
marci@556
|
53 |
/// obtained. For lack of space, for other examples,
|
marci@556
|
54 |
/// the interested user is referred to the detailed documentation of graph
|
marci@556
|
55 |
/// wrappers.
|
marci@556
|
56 |
/// The behavior of graph wrappers can be very different. Some of them keep
|
marci@556
|
57 |
/// capabilities of the original graph while in other cases this would be
|
marci@556
|
58 |
/// meaningless. This means that the concepts that they are a model of depend
|
marci@556
|
59 |
/// on the graph wrapper, and the wrapped graph(s).
|
marci@556
|
60 |
/// If an edge of \c rgw is deleted, this is carried out by
|
marci@556
|
61 |
/// deleting the corresponding edge of \c g. But for a residual
|
marci@556
|
62 |
/// graph, this operation has no sense.
|
marci@556
|
63 |
/// Let we stand one more example here to simplify your work.
|
marci@556
|
64 |
/// wrapper class
|
marci@556
|
65 |
/// \code template<typename Graph> class RevGraphWrapper; \endcode
|
marci@556
|
66 |
/// has constructor
|
marci@556
|
67 |
/// <tt> RevGraphWrapper(Graph& _g)</tt>.
|
marci@556
|
68 |
/// This means that in a situation,
|
marci@556
|
69 |
/// when a <tt> const ListGraph& </tt> reference to a graph is given,
|
marci@556
|
70 |
/// then it have to be instantiated with <tt>Graph=const ListGraph</tt>.
|
marci@556
|
71 |
/// \code
|
marci@556
|
72 |
/// int algorithm1(const ListGraph& g) {
|
marci@556
|
73 |
/// RevGraphWrapper<const ListGraph> rgw(g);
|
marci@556
|
74 |
/// return algorithm2(rgw);
|
marci@556
|
75 |
/// }
|
marci@556
|
76 |
/// \endcode
|
marci@556
|
77 |
|
marci@556
|
78 |
/// \addtogroup gwrappers
|
marci@556
|
79 |
/// @{
|
marci@556
|
80 |
|
marci@556
|
81 |
///Base type for the Graph Wrappers
|
marci@556
|
82 |
|
alpar@879
|
83 |
///\warning Graph wrappers are in even more experimental state than the other
|
alpar@879
|
84 |
///parts of the lib. Use them at you own risk.
|
alpar@879
|
85 |
///
|
marci@556
|
86 |
///This is the base type for the Graph Wrappers.
|
marci@556
|
87 |
///\todo Some more docs...
|
marci@556
|
88 |
///
|
marci@612
|
89 |
///\author Marton Makai
|
marci@556
|
90 |
template<typename Graph>
|
marci@556
|
91 |
class GraphWrapper {
|
marci@556
|
92 |
protected:
|
marci@556
|
93 |
Graph* graph;
|
marci@556
|
94 |
GraphWrapper() : graph(0) { }
|
marci@556
|
95 |
void setGraph(Graph& _graph) { graph=&_graph; }
|
marci@556
|
96 |
|
marci@556
|
97 |
public:
|
marci@556
|
98 |
typedef Graph BaseGraph;
|
marci@556
|
99 |
typedef Graph ParentGraph;
|
marci@556
|
100 |
|
marci@556
|
101 |
GraphWrapper(Graph& _graph) : graph(&_graph) { }
|
alpar@774
|
102 |
GraphWrapper(const GraphWrapper<Graph>& gw) : graph(gw.graph) { }
|
marci@556
|
103 |
|
alpar@774
|
104 |
typedef typename Graph::Node Node;
|
alpar@774
|
105 |
class NodeIt : public Node {
|
alpar@774
|
106 |
const GraphWrapper<Graph>* gw;
|
marci@556
|
107 |
friend class GraphWrapper<Graph>;
|
marci@556
|
108 |
public:
|
marci@556
|
109 |
NodeIt() { }
|
alpar@774
|
110 |
NodeIt(Invalid i) : Node(i) { }
|
alpar@774
|
111 |
NodeIt(const GraphWrapper<Graph>& _gw) :
|
alpar@774
|
112 |
Node(typename Graph::NodeIt(*(_gw.graph))), gw(&_gw) { }
|
alpar@774
|
113 |
NodeIt(const GraphWrapper<Graph>& _gw, const Node& n) :
|
alpar@774
|
114 |
Node(n), gw(&_gw) { }
|
alpar@774
|
115 |
NodeIt& operator++() {
|
alpar@774
|
116 |
*(static_cast<Node*>(this))=
|
alpar@774
|
117 |
++(typename Graph::NodeIt(*(gw->graph), *this));
|
alpar@774
|
118 |
return *this;
|
alpar@774
|
119 |
}
|
marci@556
|
120 |
};
|
alpar@774
|
121 |
typedef typename Graph::Edge Edge;
|
alpar@774
|
122 |
class OutEdgeIt : public Edge {
|
alpar@774
|
123 |
const GraphWrapper<Graph>* gw;
|
marci@556
|
124 |
friend class GraphWrapper<Graph>;
|
alpar@774
|
125 |
public:
|
alpar@774
|
126 |
OutEdgeIt() { }
|
alpar@774
|
127 |
OutEdgeIt(Invalid i) : Edge(i) { }
|
alpar@774
|
128 |
OutEdgeIt(const GraphWrapper<Graph>& _gw, const Node& n) :
|
alpar@774
|
129 |
Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
|
alpar@774
|
130 |
OutEdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) :
|
alpar@774
|
131 |
Edge(e), gw(&_gw) { }
|
alpar@774
|
132 |
OutEdgeIt& operator++() {
|
alpar@774
|
133 |
*(static_cast<Edge*>(this))=
|
alpar@774
|
134 |
++(typename Graph::OutEdgeIt(*(gw->graph), *this));
|
alpar@774
|
135 |
return *this;
|
alpar@774
|
136 |
}
|
marci@556
|
137 |
};
|
alpar@774
|
138 |
class InEdgeIt : public Edge {
|
alpar@774
|
139 |
const GraphWrapper<Graph>* gw;
|
marci@556
|
140 |
friend class GraphWrapper<Graph>;
|
alpar@774
|
141 |
public:
|
marci@556
|
142 |
InEdgeIt() { }
|
alpar@774
|
143 |
InEdgeIt(Invalid i) : Edge(i) { }
|
alpar@774
|
144 |
InEdgeIt(const GraphWrapper<Graph>& _gw, const Node& n) :
|
alpar@774
|
145 |
Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
|
alpar@774
|
146 |
InEdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) :
|
alpar@774
|
147 |
Edge(e), gw(&_gw) { }
|
alpar@774
|
148 |
InEdgeIt& operator++() {
|
alpar@774
|
149 |
*(static_cast<Edge*>(this))=
|
alpar@774
|
150 |
++(typename Graph::InEdgeIt(*(gw->graph), *this));
|
alpar@774
|
151 |
return *this;
|
alpar@774
|
152 |
}
|
marci@556
|
153 |
};
|
alpar@774
|
154 |
class EdgeIt : public Edge {
|
alpar@774
|
155 |
const GraphWrapper<Graph>* gw;
|
marci@556
|
156 |
friend class GraphWrapper<Graph>;
|
alpar@774
|
157 |
public:
|
marci@556
|
158 |
EdgeIt() { }
|
alpar@774
|
159 |
EdgeIt(Invalid i) : Edge(i) { }
|
alpar@774
|
160 |
EdgeIt(const GraphWrapper<Graph>& _gw) :
|
alpar@774
|
161 |
Edge(typename Graph::EdgeIt(*(_gw.graph))), gw(&_gw) { }
|
alpar@774
|
162 |
EdgeIt(const GraphWrapper<Graph>& _gw, const Edge& e) :
|
marci@777
|
163 |
Edge(e), gw(&_gw) { }
|
alpar@774
|
164 |
EdgeIt& operator++() {
|
alpar@774
|
165 |
*(static_cast<Edge*>(this))=
|
alpar@774
|
166 |
++(typename Graph::EdgeIt(*(gw->graph), *this));
|
alpar@774
|
167 |
return *this;
|
alpar@774
|
168 |
}
|
marci@556
|
169 |
};
|
marci@556
|
170 |
|
marci@556
|
171 |
NodeIt& first(NodeIt& i) const {
|
marci@556
|
172 |
i=NodeIt(*this); return i;
|
marci@556
|
173 |
}
|
marci@556
|
174 |
OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
|
marci@556
|
175 |
i=OutEdgeIt(*this, p); return i;
|
marci@556
|
176 |
}
|
marci@556
|
177 |
InEdgeIt& first(InEdgeIt& i, const Node& p) const {
|
marci@556
|
178 |
i=InEdgeIt(*this, p); return i;
|
marci@556
|
179 |
}
|
marci@556
|
180 |
EdgeIt& first(EdgeIt& i) const {
|
marci@556
|
181 |
i=EdgeIt(*this); return i;
|
marci@556
|
182 |
}
|
marci@556
|
183 |
|
marci@556
|
184 |
Node tail(const Edge& e) const {
|
marci@556
|
185 |
return Node(graph->tail(static_cast<typename Graph::Edge>(e))); }
|
marci@556
|
186 |
Node head(const Edge& e) const {
|
marci@556
|
187 |
return Node(graph->head(static_cast<typename Graph::Edge>(e))); }
|
marci@556
|
188 |
|
marci@556
|
189 |
int nodeNum() const { return graph->nodeNum(); }
|
marci@556
|
190 |
int edgeNum() const { return graph->edgeNum(); }
|
marci@556
|
191 |
|
marci@556
|
192 |
Node addNode() const { return Node(graph->addNode()); }
|
marci@556
|
193 |
Edge addEdge(const Node& tail, const Node& head) const {
|
marci@556
|
194 |
return Edge(graph->addEdge(tail, head)); }
|
marci@556
|
195 |
|
marci@556
|
196 |
void erase(const Node& i) const { graph->erase(i); }
|
marci@556
|
197 |
void erase(const Edge& i) const { graph->erase(i); }
|
marci@556
|
198 |
|
marci@556
|
199 |
void clear() const { graph->clear(); }
|
marci@556
|
200 |
|
alpar@736
|
201 |
bool forward(const Edge& e) const { return graph->forward(e); }
|
alpar@736
|
202 |
bool backward(const Edge& e) const { return graph->backward(e); }
|
marci@739
|
203 |
|
marci@739
|
204 |
int id(const Node& v) const { return graph->id(v); }
|
marci@739
|
205 |
int id(const Edge& e) const { return graph->id(e); }
|
marci@650
|
206 |
|
marci@738
|
207 |
Edge opposite(const Edge& e) const { return Edge(graph->opposite(e)); }
|
marci@650
|
208 |
|
marci@556
|
209 |
|
deba@877
|
210 |
IMPORT_NODE_MAP(Graph, *(gw.graph), GraphWrapper, gw);
|
deba@877
|
211 |
IMPORT_EDGE_MAP(Graph, *(gw.graph), GraphWrapper, gw);
|
deba@877
|
212 |
|
deba@877
|
213 |
|
marci@556
|
214 |
};
|
marci@556
|
215 |
|
marci@569
|
216 |
|
marci@569
|
217 |
|
marci@556
|
218 |
/// A graph wrapper which reverses the orientation of the edges.
|
marci@556
|
219 |
|
alpar@879
|
220 |
///\warning Graph wrappers are in even more experimental state than the other
|
alpar@879
|
221 |
///parts of the lib. Use them at you own risk.
|
alpar@879
|
222 |
///
|
marci@556
|
223 |
/// A graph wrapper which reverses the orientation of the edges.
|
marci@612
|
224 |
/// Thus \c Graph have to be a directed graph type.
|
marci@556
|
225 |
///
|
marci@556
|
226 |
///\author Marton Makai
|
marci@556
|
227 |
template<typename Graph>
|
marci@556
|
228 |
class RevGraphWrapper : public GraphWrapper<Graph> {
|
marci@650
|
229 |
public:
|
marci@650
|
230 |
typedef GraphWrapper<Graph> Parent;
|
marci@556
|
231 |
protected:
|
marci@612
|
232 |
RevGraphWrapper() : GraphWrapper<Graph>() { }
|
marci@556
|
233 |
public:
|
marci@556
|
234 |
RevGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }
|
alpar@774
|
235 |
RevGraphWrapper(const RevGraphWrapper<Graph>& gw) : Parent(gw) { }
|
marci@556
|
236 |
|
marci@556
|
237 |
typedef typename GraphWrapper<Graph>::Node Node;
|
marci@556
|
238 |
typedef typename GraphWrapper<Graph>::Edge Edge;
|
marci@792
|
239 |
//remark: OutEdgeIt and InEdgeIt cannot be typedef-ed to each other
|
marci@792
|
240 |
//because this does not work is some of them are not defined in the
|
marci@792
|
241 |
//original graph. The problem with this is that typedef-ed stuff
|
marci@792
|
242 |
//are instantiated in c++.
|
alpar@774
|
243 |
class OutEdgeIt : public Edge {
|
alpar@774
|
244 |
const RevGraphWrapper<Graph>* gw;
|
marci@556
|
245 |
friend class GraphWrapper<Graph>;
|
alpar@774
|
246 |
public:
|
marci@556
|
247 |
OutEdgeIt() { }
|
alpar@774
|
248 |
OutEdgeIt(Invalid i) : Edge(i) { }
|
alpar@774
|
249 |
OutEdgeIt(const RevGraphWrapper<Graph>& _gw, const Node& n) :
|
alpar@774
|
250 |
Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
|
alpar@774
|
251 |
OutEdgeIt(const RevGraphWrapper<Graph>& _gw, const Edge& e) :
|
alpar@774
|
252 |
Edge(e), gw(&_gw) { }
|
alpar@774
|
253 |
OutEdgeIt& operator++() {
|
alpar@774
|
254 |
*(static_cast<Edge*>(this))=
|
alpar@774
|
255 |
++(typename Graph::InEdgeIt(*(gw->graph), *this));
|
alpar@774
|
256 |
return *this;
|
alpar@774
|
257 |
}
|
marci@556
|
258 |
};
|
alpar@774
|
259 |
class InEdgeIt : public Edge {
|
alpar@774
|
260 |
const RevGraphWrapper<Graph>* gw;
|
marci@556
|
261 |
friend class GraphWrapper<Graph>;
|
alpar@774
|
262 |
public:
|
marci@556
|
263 |
InEdgeIt() { }
|
alpar@774
|
264 |
InEdgeIt(Invalid i) : Edge(i) { }
|
alpar@774
|
265 |
InEdgeIt(const RevGraphWrapper<Graph>& _gw, const Node& n) :
|
alpar@774
|
266 |
Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) { }
|
alpar@774
|
267 |
InEdgeIt(const RevGraphWrapper<Graph>& _gw, const Edge& e) :
|
alpar@774
|
268 |
Edge(e), gw(&_gw) { }
|
alpar@774
|
269 |
InEdgeIt& operator++() {
|
alpar@774
|
270 |
*(static_cast<Edge*>(this))=
|
alpar@774
|
271 |
++(typename Graph::OutEdgeIt(*(gw->graph), *this));
|
alpar@774
|
272 |
return *this;
|
alpar@774
|
273 |
}
|
marci@556
|
274 |
};
|
marci@556
|
275 |
|
marci@556
|
276 |
using GraphWrapper<Graph>::first;
|
marci@556
|
277 |
OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
|
marci@556
|
278 |
i=OutEdgeIt(*this, p); return i;
|
marci@556
|
279 |
}
|
marci@556
|
280 |
InEdgeIt& first(InEdgeIt& i, const Node& p) const {
|
marci@556
|
281 |
i=InEdgeIt(*this, p); return i;
|
marci@556
|
282 |
}
|
marci@556
|
283 |
|
marci@556
|
284 |
Node tail(const Edge& e) const {
|
marci@556
|
285 |
return GraphWrapper<Graph>::head(e); }
|
marci@556
|
286 |
Node head(const Edge& e) const {
|
marci@556
|
287 |
return GraphWrapper<Graph>::tail(e); }
|
marci@556
|
288 |
|
deba@877
|
289 |
KEEP_MAPS(Parent, RevGraphWrapper);
|
deba@877
|
290 |
|
marci@556
|
291 |
};
|
marci@556
|
292 |
|
marci@775
|
293 |
|
marci@775
|
294 |
|
marci@612
|
295 |
/// A graph wrapper for hiding nodes and edges from a graph.
|
marci@556
|
296 |
|
alpar@879
|
297 |
///\warning Graph wrappers are in even more experimental state than the other
|
alpar@879
|
298 |
///parts of the lib. Use them at you own risk.
|
alpar@879
|
299 |
///
|
marci@556
|
300 |
/// This wrapper shows a graph with filtered node-set and
|
marci@838
|
301 |
/// edge-set. Given a bool-valued map on the node-set and one on
|
marci@838
|
302 |
/// the edge-set of the graphs, the iterators shows only the objects
|
marci@838
|
303 |
/// having true value.
|
marci@838
|
304 |
/// The quick brown fox iterators jump over
|
marci@838
|
305 |
/// the lazy dog nodes or edges if their values for are false in the
|
marci@838
|
306 |
/// corresponding bool maps.
|
marci@556
|
307 |
///
|
marci@556
|
308 |
///\author Marton Makai
|
marci@556
|
309 |
template<typename Graph, typename NodeFilterMap,
|
marci@556
|
310 |
typename EdgeFilterMap>
|
marci@556
|
311 |
class SubGraphWrapper : public GraphWrapper<Graph> {
|
marci@650
|
312 |
public:
|
marci@650
|
313 |
typedef GraphWrapper<Graph> Parent;
|
marci@556
|
314 |
protected:
|
marci@556
|
315 |
NodeFilterMap* node_filter_map;
|
marci@556
|
316 |
EdgeFilterMap* edge_filter_map;
|
marci@556
|
317 |
|
marci@612
|
318 |
SubGraphWrapper() : GraphWrapper<Graph>(),
|
marci@556
|
319 |
node_filter_map(0), edge_filter_map(0) { }
|
marci@556
|
320 |
void setNodeFilterMap(NodeFilterMap& _node_filter_map) {
|
marci@556
|
321 |
node_filter_map=&_node_filter_map;
|
marci@556
|
322 |
}
|
marci@556
|
323 |
void setEdgeFilterMap(EdgeFilterMap& _edge_filter_map) {
|
marci@556
|
324 |
edge_filter_map=&_edge_filter_map;
|
marci@556
|
325 |
}
|
marci@556
|
326 |
|
marci@556
|
327 |
public:
|
marci@556
|
328 |
SubGraphWrapper(Graph& _graph, NodeFilterMap& _node_filter_map,
|
marci@556
|
329 |
EdgeFilterMap& _edge_filter_map) :
|
marci@556
|
330 |
GraphWrapper<Graph>(_graph), node_filter_map(&_node_filter_map),
|
marci@556
|
331 |
edge_filter_map(&_edge_filter_map) { }
|
marci@556
|
332 |
|
marci@556
|
333 |
typedef typename GraphWrapper<Graph>::Node Node;
|
marci@775
|
334 |
class NodeIt : public Node {
|
marci@775
|
335 |
const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
|
marci@556
|
336 |
friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
|
marci@775
|
337 |
public:
|
marci@556
|
338 |
NodeIt() { }
|
marci@775
|
339 |
NodeIt(Invalid i) : Node(i) { }
|
marci@775
|
340 |
NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw) :
|
marci@854
|
341 |
Node(typename Graph::NodeIt(*(_gw.graph))), gw(&_gw) {
|
marci@854
|
342 |
while (*static_cast<Node*>(this)!=INVALID &&
|
marci@861
|
343 |
!(*(gw->node_filter_map))[*this])
|
marci@854
|
344 |
*(static_cast<Node*>(this))=
|
marci@854
|
345 |
++(typename Graph::NodeIt(*(gw->graph), *this));
|
marci@854
|
346 |
}
|
marci@775
|
347 |
NodeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw,
|
marci@775
|
348 |
const Node& n) :
|
marci@775
|
349 |
Node(n), gw(&_gw) { }
|
marci@775
|
350 |
NodeIt& operator++() {
|
marci@775
|
351 |
*(static_cast<Node*>(this))=
|
marci@775
|
352 |
++(typename Graph::NodeIt(*(gw->graph), *this));
|
marci@775
|
353 |
while (*static_cast<Node*>(this)!=INVALID &&
|
marci@775
|
354 |
!(*(gw->node_filter_map))[*this])
|
marci@775
|
355 |
*(static_cast<Node*>(this))=
|
marci@775
|
356 |
++(typename Graph::NodeIt(*(gw->graph), *this));
|
marci@775
|
357 |
return *this;
|
marci@556
|
358 |
}
|
marci@556
|
359 |
};
|
marci@556
|
360 |
typedef typename GraphWrapper<Graph>::Edge Edge;
|
marci@775
|
361 |
class OutEdgeIt : public Edge {
|
marci@775
|
362 |
const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
|
marci@556
|
363 |
friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
|
marci@556
|
364 |
public:
|
marci@556
|
365 |
OutEdgeIt() { }
|
marci@775
|
366 |
OutEdgeIt(Invalid i) : Edge(i) { }
|
marci@775
|
367 |
OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, const Node& n) :
|
marci@854
|
368 |
Edge(typename Graph::OutEdgeIt(*(_gw.graph), n)), gw(&_gw) {
|
marci@854
|
369 |
while (*static_cast<Edge*>(this)!=INVALID &&
|
marci@854
|
370 |
!(*(gw->edge_filter_map))[*this])
|
marci@854
|
371 |
*(static_cast<Edge*>(this))=
|
marci@854
|
372 |
++(typename Graph::OutEdgeIt(*(gw->graph), *this));
|
marci@854
|
373 |
}
|
marci@775
|
374 |
OutEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw,
|
marci@775
|
375 |
const Edge& e) :
|
marci@775
|
376 |
Edge(e), gw(&_gw) { }
|
marci@775
|
377 |
OutEdgeIt& operator++() {
|
marci@775
|
378 |
*(static_cast<Edge*>(this))=
|
marci@775
|
379 |
++(typename Graph::OutEdgeIt(*(gw->graph), *this));
|
marci@775
|
380 |
while (*static_cast<Edge*>(this)!=INVALID &&
|
marci@775
|
381 |
!(*(gw->edge_filter_map))[*this])
|
marci@775
|
382 |
*(static_cast<Edge*>(this))=
|
marci@775
|
383 |
++(typename Graph::OutEdgeIt(*(gw->graph), *this));
|
marci@775
|
384 |
return *this;
|
marci@556
|
385 |
}
|
marci@556
|
386 |
};
|
marci@775
|
387 |
class InEdgeIt : public Edge {
|
marci@775
|
388 |
const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
|
marci@556
|
389 |
friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
|
marci@556
|
390 |
public:
|
marci@556
|
391 |
InEdgeIt() { }
|
marci@775
|
392 |
// InEdgeIt(const InEdgeIt& e) : Edge(e), gw(e.gw) { }
|
marci@775
|
393 |
InEdgeIt(Invalid i) : Edge(i) { }
|
marci@775
|
394 |
InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw, const Node& n) :
|
marci@854
|
395 |
Edge(typename Graph::InEdgeIt(*(_gw.graph), n)), gw(&_gw) {
|
marci@854
|
396 |
while (*static_cast<Edge*>(this)!=INVALID &&
|
marci@854
|
397 |
!(*(gw->edge_filter_map))[*this])
|
marci@854
|
398 |
*(static_cast<Edge*>(this))=
|
marci@854
|
399 |
++(typename Graph::InEdgeIt(*(gw->graph), *this));
|
marci@854
|
400 |
}
|
marci@775
|
401 |
InEdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw,
|
marci@775
|
402 |
const Edge& e) :
|
marci@775
|
403 |
Edge(e), gw(&_gw) { }
|
marci@775
|
404 |
InEdgeIt& operator++() {
|
marci@775
|
405 |
*(static_cast<Edge*>(this))=
|
marci@775
|
406 |
++(typename Graph::InEdgeIt(*(gw->graph), *this));
|
marci@775
|
407 |
while (*static_cast<Edge*>(this)!=INVALID &&
|
marci@775
|
408 |
!(*(gw->edge_filter_map))[*this])
|
marci@775
|
409 |
*(static_cast<Edge*>(this))=
|
marci@775
|
410 |
++(typename Graph::InEdgeIt(*(gw->graph), *this));
|
marci@775
|
411 |
return *this;
|
marci@556
|
412 |
}
|
marci@556
|
413 |
};
|
marci@775
|
414 |
class EdgeIt : public Edge {
|
marci@775
|
415 |
const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>* gw;
|
marci@556
|
416 |
friend class SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>;
|
marci@556
|
417 |
public:
|
marci@556
|
418 |
EdgeIt() { }
|
marci@775
|
419 |
EdgeIt(Invalid i) : Edge(i) { }
|
marci@775
|
420 |
EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw) :
|
marci@854
|
421 |
Edge(typename Graph::EdgeIt(*(_gw.graph))), gw(&_gw) {
|
marci@854
|
422 |
while (*static_cast<Edge*>(this)!=INVALID &&
|
marci@854
|
423 |
!(*(gw->edge_filter_map))[*this])
|
marci@854
|
424 |
*(static_cast<Edge*>(this))=
|
marci@854
|
425 |
++(typename Graph::EdgeIt(*(gw->graph), *this));
|
marci@854
|
426 |
}
|
marci@775
|
427 |
EdgeIt(const SubGraphWrapper<Graph, NodeFilterMap, EdgeFilterMap>& _gw,
|
marci@775
|
428 |
const Edge& e) :
|
marci@775
|
429 |
Edge(e), gw(&_gw) { }
|
marci@775
|
430 |
EdgeIt& operator++() {
|
marci@775
|
431 |
*(static_cast<Edge*>(this))=
|
marci@775
|
432 |
++(typename Graph::EdgeIt(*(gw->graph), *this));
|
marci@775
|
433 |
while (*static_cast<Edge*>(this)!=INVALID &&
|
marci@775
|
434 |
!(*(gw->edge_filter_map))[*this])
|
marci@775
|
435 |
*(static_cast<Edge*>(this))=
|
marci@775
|
436 |
++(typename Graph::EdgeIt(*(gw->graph), *this));
|
marci@775
|
437 |
return *this;
|
marci@556
|
438 |
}
|
marci@556
|
439 |
};
|
marci@556
|
440 |
|
marci@556
|
441 |
NodeIt& first(NodeIt& i) const {
|
marci@556
|
442 |
i=NodeIt(*this); return i;
|
marci@556
|
443 |
}
|
marci@556
|
444 |
OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
|
marci@556
|
445 |
i=OutEdgeIt(*this, p); return i;
|
marci@556
|
446 |
}
|
marci@556
|
447 |
InEdgeIt& first(InEdgeIt& i, const Node& p) const {
|
marci@556
|
448 |
i=InEdgeIt(*this, p); return i;
|
marci@556
|
449 |
}
|
marci@556
|
450 |
EdgeIt& first(EdgeIt& i) const {
|
marci@556
|
451 |
i=EdgeIt(*this); return i;
|
marci@556
|
452 |
}
|
marci@556
|
453 |
|
marci@561
|
454 |
/// This function hides \c n in the graph, i.e. the iteration
|
marci@561
|
455 |
/// jumps over it. This is done by simply setting the value of \c n
|
marci@561
|
456 |
/// to be false in the corresponding node-map.
|
marci@556
|
457 |
void hide(const Node& n) const { node_filter_map->set(n, false); }
|
marci@561
|
458 |
|
marci@561
|
459 |
/// This function hides \c e in the graph, i.e. the iteration
|
marci@561
|
460 |
/// jumps over it. This is done by simply setting the value of \c e
|
marci@561
|
461 |
/// to be false in the corresponding edge-map.
|
marci@556
|
462 |
void hide(const Edge& e) const { edge_filter_map->set(e, false); }
|
marci@556
|
463 |
|
marci@561
|
464 |
/// The value of \c n is set to be true in the node-map which stores
|
marci@561
|
465 |
/// hide information. If \c n was hidden previuosly, then it is shown
|
marci@561
|
466 |
/// again
|
marci@561
|
467 |
void unHide(const Node& n) const { node_filter_map->set(n, true); }
|
marci@561
|
468 |
|
marci@561
|
469 |
/// The value of \c e is set to be true in the edge-map which stores
|
marci@561
|
470 |
/// hide information. If \c e was hidden previuosly, then it is shown
|
marci@561
|
471 |
/// again
|
marci@556
|
472 |
void unHide(const Edge& e) const { edge_filter_map->set(e, true); }
|
marci@556
|
473 |
|
marci@561
|
474 |
/// Returns true if \c n is hidden.
|
marci@561
|
475 |
bool hidden(const Node& n) const { return !(*node_filter_map)[n]; }
|
marci@561
|
476 |
|
marci@561
|
477 |
/// Returns true if \c n is hidden.
|
marci@561
|
478 |
bool hidden(const Edge& e) const { return !(*edge_filter_map)[e]; }
|
marci@593
|
479 |
|
marci@792
|
480 |
/// \warning This is a linear time operation and works only if
|
marci@792
|
481 |
/// \c Graph::NodeIt is defined.
|
marci@593
|
482 |
int nodeNum() const {
|
marci@593
|
483 |
int i=0;
|
marci@792
|
484 |
for (NodeIt n(*this); n!=INVALID; ++n) ++i;
|
marci@593
|
485 |
return i;
|
marci@593
|
486 |
}
|
marci@593
|
487 |
|
marci@792
|
488 |
/// \warning This is a linear time operation and works only if
|
marci@792
|
489 |
/// \c Graph::EdgeIt is defined.
|
marci@593
|
490 |
int edgeNum() const {
|
marci@593
|
491 |
int i=0;
|
marci@792
|
492 |
for (EdgeIt e(*this); e!=INVALID; ++e) ++i;
|
marci@593
|
493 |
return i;
|
marci@593
|
494 |
}
|
marci@593
|
495 |
|
deba@877
|
496 |
KEEP_MAPS(Parent, SubGraphWrapper);
|
marci@556
|
497 |
};
|
marci@556
|
498 |
|
marci@569
|
499 |
|
marci@569
|
500 |
|
marci@556
|
501 |
template<typename Graph>
|
marci@556
|
502 |
class UndirGraphWrapper : public GraphWrapper<Graph> {
|
marci@650
|
503 |
public:
|
marci@650
|
504 |
typedef GraphWrapper<Graph> Parent;
|
marci@556
|
505 |
protected:
|
marci@556
|
506 |
UndirGraphWrapper() : GraphWrapper<Graph>() { }
|
marci@556
|
507 |
|
marci@556
|
508 |
public:
|
marci@556
|
509 |
typedef typename GraphWrapper<Graph>::Node Node;
|
marci@556
|
510 |
typedef typename GraphWrapper<Graph>::NodeIt NodeIt;
|
marci@556
|
511 |
typedef typename GraphWrapper<Graph>::Edge Edge;
|
marci@556
|
512 |
typedef typename GraphWrapper<Graph>::EdgeIt EdgeIt;
|
marci@556
|
513 |
|
marci@556
|
514 |
UndirGraphWrapper(Graph& _graph) : GraphWrapper<Graph>(_graph) { }
|
marci@556
|
515 |
|
marci@556
|
516 |
class OutEdgeIt {
|
marci@556
|
517 |
friend class UndirGraphWrapper<Graph>;
|
marci@556
|
518 |
bool out_or_in; //true iff out
|
marci@556
|
519 |
typename Graph::OutEdgeIt out;
|
marci@556
|
520 |
typename Graph::InEdgeIt in;
|
marci@556
|
521 |
public:
|
marci@556
|
522 |
OutEdgeIt() { }
|
marci@556
|
523 |
OutEdgeIt(const Invalid& i) : Edge(i) { }
|
marci@556
|
524 |
OutEdgeIt(const UndirGraphWrapper<Graph>& _G, const Node& _n) {
|
marci@556
|
525 |
out_or_in=true; _G.graph->first(out, _n);
|
marci@556
|
526 |
if (!(_G.graph->valid(out))) { out_or_in=false; _G.graph->first(in, _n); }
|
marci@556
|
527 |
}
|
marci@556
|
528 |
operator Edge() const {
|
marci@556
|
529 |
if (out_or_in) return Edge(out); else return Edge(in);
|
marci@556
|
530 |
}
|
marci@556
|
531 |
};
|
marci@556
|
532 |
|
marci@556
|
533 |
typedef OutEdgeIt InEdgeIt;
|
marci@556
|
534 |
|
marci@556
|
535 |
using GraphWrapper<Graph>::first;
|
marci@556
|
536 |
OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
|
marci@556
|
537 |
i=OutEdgeIt(*this, p); return i;
|
marci@556
|
538 |
}
|
marci@556
|
539 |
|
marci@556
|
540 |
using GraphWrapper<Graph>::next;
|
alpar@878
|
541 |
|
marci@556
|
542 |
OutEdgeIt& next(OutEdgeIt& e) const {
|
marci@556
|
543 |
if (e.out_or_in) {
|
marci@556
|
544 |
typename Graph::Node n=this->graph->tail(e.out);
|
marci@556
|
545 |
this->graph->next(e.out);
|
marci@556
|
546 |
if (!this->graph->valid(e.out)) {
|
marci@556
|
547 |
e.out_or_in=false; this->graph->first(e.in, n); }
|
marci@556
|
548 |
} else {
|
marci@556
|
549 |
this->graph->next(e.in);
|
marci@556
|
550 |
}
|
marci@556
|
551 |
return e;
|
marci@556
|
552 |
}
|
marci@556
|
553 |
|
marci@556
|
554 |
Node aNode(const OutEdgeIt& e) const {
|
marci@556
|
555 |
if (e.out_or_in) return this->graph->tail(e); else
|
marci@556
|
556 |
return this->graph->head(e); }
|
marci@556
|
557 |
Node bNode(const OutEdgeIt& e) const {
|
marci@556
|
558 |
if (e.out_or_in) return this->graph->head(e); else
|
marci@556
|
559 |
return this->graph->tail(e); }
|
deba@877
|
560 |
|
deba@877
|
561 |
KEEP_MAPS(Parent, UndirGraphWrapper);
|
deba@877
|
562 |
|
marci@556
|
563 |
};
|
marci@556
|
564 |
|
marci@612
|
565 |
/// \brief An undirected graph template.
|
marci@612
|
566 |
///
|
alpar@879
|
567 |
///\warning Graph wrappers are in even more experimental state than the other
|
alpar@879
|
568 |
///parts of the lib. Use them at you own risk.
|
alpar@879
|
569 |
///
|
marci@612
|
570 |
/// An undirected graph template.
|
marci@612
|
571 |
/// This class works as an undirected graph and a directed graph of
|
marci@612
|
572 |
/// class \c Graph is used for the physical storage.
|
marci@612
|
573 |
/// \ingroup graphs
|
marci@556
|
574 |
template<typename Graph>
|
marci@556
|
575 |
class UndirGraph : public UndirGraphWrapper<Graph> {
|
marci@556
|
576 |
typedef UndirGraphWrapper<Graph> Parent;
|
marci@556
|
577 |
protected:
|
marci@556
|
578 |
Graph gr;
|
marci@556
|
579 |
public:
|
marci@556
|
580 |
UndirGraph() : UndirGraphWrapper<Graph>() {
|
marci@556
|
581 |
Parent::setGraph(gr);
|
marci@556
|
582 |
}
|
deba@877
|
583 |
|
deba@877
|
584 |
KEEP_MAPS(Parent, UndirGraph);
|
marci@556
|
585 |
};
|
marci@556
|
586 |
|
marci@569
|
587 |
|
marci@650
|
588 |
|
marci@650
|
589 |
///\brief A wrapper for composing a subgraph of a
|
marci@792
|
590 |
/// bidirected graph made from a directed one.
|
marci@612
|
591 |
///
|
alpar@879
|
592 |
///\warning Graph wrappers are in even more experimental state than the other
|
alpar@879
|
593 |
///parts of the lib. Use them at you own risk.
|
alpar@879
|
594 |
///
|
marci@792
|
595 |
/// Suppose that for a directed graph $G=(V, A)$,
|
marci@792
|
596 |
/// two predicates on the edge-set, $forward_filter$, and $backward_filter$
|
marci@792
|
597 |
/// is given, and we are dealing with the directed graph
|
marci@792
|
598 |
/// a $G'=(V, \{uv : uv\in A \mbox{ and } forward_filter(uv) \mbox{ is true}\}+\{vu : uv\in A \mbox{ and } backward_filter(uv) \mbox{ is true}\})$.
|
marci@792
|
599 |
/// The purpose of writing + instead of union is because parallel
|
marci@792
|
600 |
/// edges can arose.
|
marci@792
|
601 |
/// In other words, a subgraph of the bidirected graph obtained, which
|
marci@792
|
602 |
/// is given by orienting the edges of the original graph in both directions.
|
marci@792
|
603 |
/// An example for such a construction is the \c RevGraphWrapper where the
|
marci@792
|
604 |
/// forward_filter is everywhere false and the backward_filter is
|
marci@792
|
605 |
/// everywhere true. We note that for sake of efficiency,
|
marci@792
|
606 |
/// \c RevGraphWrapper is implemented in a different way.
|
marci@792
|
607 |
/// But BidirGraphWrapper is obtained from
|
marci@792
|
608 |
/// SubBidirGraphWrapper by considering everywhere true
|
marci@792
|
609 |
/// predicates both forward_filter and backward_filter.
|
marci@792
|
610 |
/// Finally, one of the most important applications of SubBidirGraphWrapper
|
marci@792
|
611 |
/// is ResGraphWrapper, which stands for the residual graph in directed
|
marci@792
|
612 |
/// flow and circulation problems.
|
marci@792
|
613 |
/// As wrappers usually, the SubBidirGraphWrapper implements the
|
marci@792
|
614 |
/// above mentioned graph structure without its physical storage,
|
marci@792
|
615 |
/// that is the whole stuff eats constant memory.
|
marci@792
|
616 |
/// As the oppositely directed edges are logical different,
|
marci@792
|
617 |
/// the maps are able to attach different values for them.
|
marci@650
|
618 |
template<typename Graph,
|
marci@650
|
619 |
typename ForwardFilterMap, typename BackwardFilterMap>
|
marci@650
|
620 |
class SubBidirGraphWrapper : public GraphWrapper<Graph> {
|
marci@650
|
621 |
public:
|
marci@650
|
622 |
typedef GraphWrapper<Graph> Parent;
|
marci@569
|
623 |
protected:
|
marci@650
|
624 |
ForwardFilterMap* forward_filter;
|
marci@650
|
625 |
BackwardFilterMap* backward_filter;
|
marci@650
|
626 |
|
marci@792
|
627 |
SubBidirGraphWrapper() : GraphWrapper<Graph>() { }
|
marci@650
|
628 |
void setForwardFilterMap(ForwardFilterMap& _forward_filter) {
|
marci@650
|
629 |
forward_filter=&_forward_filter;
|
marci@650
|
630 |
}
|
marci@650
|
631 |
void setBackwardFilterMap(BackwardFilterMap& _backward_filter) {
|
marci@650
|
632 |
backward_filter=&_backward_filter;
|
marci@650
|
633 |
}
|
marci@569
|
634 |
|
marci@569
|
635 |
public:
|
marci@569
|
636 |
|
marci@650
|
637 |
SubBidirGraphWrapper(Graph& _graph, ForwardFilterMap& _forward_filter,
|
marci@650
|
638 |
BackwardFilterMap& _backward_filter) :
|
marci@650
|
639 |
GraphWrapper<Graph>(_graph),
|
marci@650
|
640 |
forward_filter(&_forward_filter), backward_filter(&_backward_filter) { }
|
alpar@774
|
641 |
SubBidirGraphWrapper(const SubBidirGraphWrapper<Graph,
|
alpar@774
|
642 |
ForwardFilterMap, BackwardFilterMap>& gw) :
|
alpar@774
|
643 |
Parent(gw),
|
alpar@774
|
644 |
forward_filter(gw.forward_filter),
|
alpar@774
|
645 |
backward_filter(gw.backward_filter) { }
|
marci@569
|
646 |
|
marci@569
|
647 |
class Edge;
|
marci@569
|
648 |
class OutEdgeIt;
|
marci@569
|
649 |
friend class Edge;
|
marci@569
|
650 |
friend class OutEdgeIt;
|
marci@569
|
651 |
|
marci@621
|
652 |
template<typename T> class EdgeMap;
|
marci@621
|
653 |
|
marci@569
|
654 |
typedef typename GraphWrapper<Graph>::Node Node;
|
marci@621
|
655 |
|
alpar@774
|
656 |
typedef typename Graph::Edge GraphEdge;
|
marci@792
|
657 |
/// SubBidirGraphWrapper<..., ..., ...>::Edge is inherited from
|
marci@792
|
658 |
/// Graph::Edge. It contains an extra bool flag which shows if the
|
marci@792
|
659 |
/// edge is the backward version of the original edge.
|
marci@569
|
660 |
class Edge : public Graph::Edge {
|
marci@650
|
661 |
friend class SubBidirGraphWrapper<Graph,
|
marci@650
|
662 |
ForwardFilterMap, BackwardFilterMap>;
|
marci@621
|
663 |
template<typename T> friend class EdgeMap;
|
marci@569
|
664 |
protected:
|
marci@569
|
665 |
bool backward; //true, iff backward
|
marci@569
|
666 |
public:
|
marci@569
|
667 |
Edge() { }
|
marci@792
|
668 |
/// \todo =false is needed, or causes problems?
|
marci@792
|
669 |
/// If \c _backward is false, then we get an edge corresponding to the
|
marci@792
|
670 |
/// original one, otherwise its oppositely directed pair is obtained.
|
alpar@774
|
671 |
Edge(const typename Graph::Edge& e, bool _backward/*=false*/) :
|
alpar@774
|
672 |
Graph::Edge(e), backward(_backward) { }
|
alpar@774
|
673 |
Edge(Invalid i) : Graph::Edge(i), backward(true) { }
|
alpar@774
|
674 |
bool operator==(const Edge& v) const {
|
alpar@774
|
675 |
return (this->backward==v.backward &&
|
alpar@774
|
676 |
static_cast<typename Graph::Edge>(*this)==
|
marci@569
|
677 |
static_cast<typename Graph::Edge>(v));
|
marci@569
|
678 |
}
|
alpar@774
|
679 |
bool operator!=(const Edge& v) const {
|
alpar@774
|
680 |
return (this->backward!=v.backward ||
|
alpar@774
|
681 |
static_cast<typename Graph::Edge>(*this)!=
|
marci@569
|
682 |
static_cast<typename Graph::Edge>(v));
|
alpar@774
|
683 |
}
|
marci@569
|
684 |
};
|
marci@569
|
685 |
|
alpar@774
|
686 |
class OutEdgeIt : public Edge {
|
marci@650
|
687 |
friend class SubBidirGraphWrapper<Graph,
|
marci@650
|
688 |
ForwardFilterMap, BackwardFilterMap>;
|
marci@569
|
689 |
protected:
|
alpar@774
|
690 |
const SubBidirGraphWrapper<Graph,
|
alpar@774
|
691 |
ForwardFilterMap, BackwardFilterMap>* gw;
|
marci@569
|
692 |
public:
|
marci@569
|
693 |
OutEdgeIt() { }
|
alpar@774
|
694 |
OutEdgeIt(Invalid i) : Edge(i) { }
|
marci@650
|
695 |
OutEdgeIt(const SubBidirGraphWrapper<Graph,
|
alpar@774
|
696 |
ForwardFilterMap, BackwardFilterMap>& _gw, const Node& n) :
|
alpar@774
|
697 |
Edge(typename Graph::OutEdgeIt(*(_gw.graph), n), false), gw(&_gw) {
|
alpar@774
|
698 |
while (*static_cast<GraphEdge*>(this)!=INVALID &&
|
alpar@774
|
699 |
!(*(gw->forward_filter))[*this])
|
alpar@774
|
700 |
*(static_cast<GraphEdge*>(this))=
|
alpar@774
|
701 |
++(typename Graph::OutEdgeIt(*(gw->graph), *this));
|
marci@775
|
702 |
if (*static_cast<GraphEdge*>(this)==INVALID) {
|
alpar@774
|
703 |
*static_cast<Edge*>(this)=
|
alpar@774
|
704 |
Edge(typename Graph::InEdgeIt(*(_gw.graph), n), true);
|
marci@775
|
705 |
while (*static_cast<GraphEdge*>(this)!=INVALID &&
|
marci@775
|
706 |
!(*(gw->backward_filter))[*this])
|
marci@775
|
707 |
*(static_cast<GraphEdge*>(this))=
|
marci@775
|
708 |
++(typename Graph::InEdgeIt(*(gw->graph), *this));
|
marci@775
|
709 |
}
|
alpar@774
|
710 |
}
|
alpar@774
|
711 |
OutEdgeIt(const SubBidirGraphWrapper<Graph,
|
alpar@774
|
712 |
ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) :
|
alpar@774
|
713 |
Edge(e), gw(&_gw) { }
|
alpar@774
|
714 |
OutEdgeIt& operator++() {
|
alpar@774
|
715 |
if (!this->backward) {
|
alpar@774
|
716 |
Node n=gw->tail(*this);
|
alpar@774
|
717 |
*(static_cast<GraphEdge*>(this))=
|
alpar@774
|
718 |
++(typename Graph::OutEdgeIt(*(gw->graph), *this));
|
alpar@774
|
719 |
while (*static_cast<GraphEdge*>(this)!=INVALID &&
|
alpar@774
|
720 |
!(*(gw->forward_filter))[*this])
|
alpar@774
|
721 |
*(static_cast<GraphEdge*>(this))=
|
alpar@774
|
722 |
++(typename Graph::OutEdgeIt(*(gw->graph), *this));
|
marci@775
|
723 |
if (*static_cast<GraphEdge*>(this)==INVALID) {
|
alpar@774
|
724 |
*static_cast<Edge*>(this)=
|
alpar@774
|
725 |
Edge(typename Graph::InEdgeIt(*(gw->graph), n), true);
|
marci@775
|
726 |
while (*static_cast<GraphEdge*>(this)!=INVALID &&
|
marci@775
|
727 |
!(*(gw->backward_filter))[*this])
|
marci@775
|
728 |
*(static_cast<GraphEdge*>(this))=
|
marci@775
|
729 |
++(typename Graph::InEdgeIt(*(gw->graph), *this));
|
marci@775
|
730 |
}
|
alpar@774
|
731 |
} else {
|
alpar@774
|
732 |
*(static_cast<GraphEdge*>(this))=
|
alpar@774
|
733 |
++(typename Graph::InEdgeIt(*(gw->graph), *this));
|
alpar@774
|
734 |
while (*static_cast<GraphEdge*>(this)!=INVALID &&
|
alpar@774
|
735 |
!(*(gw->backward_filter))[*this])
|
alpar@774
|
736 |
*(static_cast<GraphEdge*>(this))=
|
alpar@774
|
737 |
++(typename Graph::InEdgeIt(*(gw->graph), *this));
|
marci@569
|
738 |
}
|
alpar@774
|
739 |
return *this;
|
marci@569
|
740 |
}
|
marci@569
|
741 |
};
|
marci@569
|
742 |
|
alpar@774
|
743 |
class InEdgeIt : public Edge {
|
marci@650
|
744 |
friend class SubBidirGraphWrapper<Graph,
|
marci@650
|
745 |
ForwardFilterMap, BackwardFilterMap>;
|
marci@569
|
746 |
protected:
|
alpar@774
|
747 |
const SubBidirGraphWrapper<Graph,
|
alpar@774
|
748 |
ForwardFilterMap, BackwardFilterMap>* gw;
|
marci@569
|
749 |
public:
|
marci@569
|
750 |
InEdgeIt() { }
|
alpar@774
|
751 |
InEdgeIt(Invalid i) : Edge(i) { }
|
marci@650
|
752 |
InEdgeIt(const SubBidirGraphWrapper<Graph,
|
alpar@774
|
753 |
ForwardFilterMap, BackwardFilterMap>& _gw, const Node& n) :
|
alpar@774
|
754 |
Edge(typename Graph::InEdgeIt(*(_gw.graph), n), false), gw(&_gw) {
|
alpar@774
|
755 |
while (*static_cast<GraphEdge*>(this)!=INVALID &&
|
alpar@774
|
756 |
!(*(gw->forward_filter))[*this])
|
alpar@774
|
757 |
*(static_cast<GraphEdge*>(this))=
|
alpar@774
|
758 |
++(typename Graph::InEdgeIt(*(gw->graph), *this));
|
marci@775
|
759 |
if (*static_cast<GraphEdge*>(this)==INVALID) {
|
alpar@774
|
760 |
*static_cast<Edge*>(this)=
|
alpar@774
|
761 |
Edge(typename Graph::OutEdgeIt(*(_gw.graph), n), true);
|
marci@775
|
762 |
while (*static_cast<GraphEdge*>(this)!=INVALID &&
|
marci@775
|
763 |
!(*(gw->backward_filter))[*this])
|
marci@775
|
764 |
*(static_cast<GraphEdge*>(this))=
|
marci@775
|
765 |
++(typename Graph::OutEdgeIt(*(gw->graph), *this));
|
marci@775
|
766 |
}
|
alpar@774
|
767 |
}
|
alpar@774
|
768 |
InEdgeIt(const SubBidirGraphWrapper<Graph,
|
alpar@774
|
769 |
ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) :
|
alpar@774
|
770 |
Edge(e), gw(&_gw) { }
|
alpar@774
|
771 |
InEdgeIt& operator++() {
|
alpar@774
|
772 |
if (!this->backward) {
|
marci@775
|
773 |
Node n=gw->tail(*this);
|
alpar@774
|
774 |
*(static_cast<GraphEdge*>(this))=
|
alpar@774
|
775 |
++(typename Graph::InEdgeIt(*(gw->graph), *this));
|
alpar@774
|
776 |
while (*static_cast<GraphEdge*>(this)!=INVALID &&
|
alpar@774
|
777 |
!(*(gw->forward_filter))[*this])
|
alpar@774
|
778 |
*(static_cast<GraphEdge*>(this))=
|
alpar@774
|
779 |
++(typename Graph::InEdgeIt(*(gw->graph), *this));
|
marci@775
|
780 |
if (*static_cast<GraphEdge*>(this)==INVALID) {
|
alpar@774
|
781 |
*static_cast<Edge*>(this)=
|
alpar@774
|
782 |
Edge(typename Graph::OutEdgeIt(*(gw->graph), n), true);
|
marci@775
|
783 |
while (*static_cast<GraphEdge*>(this)!=INVALID &&
|
marci@775
|
784 |
!(*(gw->backward_filter))[*this])
|
marci@775
|
785 |
*(static_cast<GraphEdge*>(this))=
|
marci@775
|
786 |
++(typename Graph::OutEdgeIt(*(gw->graph), *this));
|
marci@775
|
787 |
}
|
alpar@774
|
788 |
} else {
|
alpar@774
|
789 |
*(static_cast<GraphEdge*>(this))=
|
alpar@774
|
790 |
++(typename Graph::OutEdgeIt(*(gw->graph), *this));
|
alpar@774
|
791 |
while (*static_cast<GraphEdge*>(this)!=INVALID &&
|
alpar@774
|
792 |
!(*(gw->backward_filter))[*this])
|
alpar@774
|
793 |
*(static_cast<GraphEdge*>(this))=
|
alpar@774
|
794 |
++(typename Graph::OutEdgeIt(*(gw->graph), *this));
|
marci@569
|
795 |
}
|
alpar@774
|
796 |
return *this;
|
marci@569
|
797 |
}
|
marci@569
|
798 |
};
|
marci@569
|
799 |
|
alpar@774
|
800 |
class EdgeIt : public Edge {
|
marci@650
|
801 |
friend class SubBidirGraphWrapper<Graph,
|
marci@650
|
802 |
ForwardFilterMap, BackwardFilterMap>;
|
marci@569
|
803 |
protected:
|
alpar@774
|
804 |
const SubBidirGraphWrapper<Graph,
|
alpar@774
|
805 |
ForwardFilterMap, BackwardFilterMap>* gw;
|
marci@569
|
806 |
public:
|
marci@569
|
807 |
EdgeIt() { }
|
alpar@774
|
808 |
EdgeIt(Invalid i) : Edge(i) { }
|
marci@650
|
809 |
EdgeIt(const SubBidirGraphWrapper<Graph,
|
marci@775
|
810 |
ForwardFilterMap, BackwardFilterMap>& _gw) :
|
marci@775
|
811 |
Edge(typename Graph::OutEdgeIt(*(_gw.graph)), false), gw(&_gw) {
|
alpar@774
|
812 |
while (*static_cast<GraphEdge*>(this)!=INVALID &&
|
alpar@774
|
813 |
!(*(gw->forward_filter))[*this])
|
alpar@774
|
814 |
*(static_cast<GraphEdge*>(this))=
|
alpar@774
|
815 |
++(typename Graph::EdgeIt(*(gw->graph), *this));
|
marci@775
|
816 |
if (*static_cast<GraphEdge*>(this)==INVALID) {
|
alpar@774
|
817 |
*static_cast<Edge*>(this)=
|
alpar@774
|
818 |
Edge(typename Graph::EdgeIt(*(_gw.graph)), true);
|
marci@775
|
819 |
while (*static_cast<GraphEdge*>(this)!=INVALID &&
|
marci@775
|
820 |
!(*(gw->backward_filter))[*this])
|
marci@775
|
821 |
*(static_cast<GraphEdge*>(this))=
|
marci@775
|
822 |
++(typename Graph::EdgeIt(*(gw->graph), *this));
|
marci@775
|
823 |
}
|
alpar@774
|
824 |
}
|
alpar@774
|
825 |
EdgeIt(const SubBidirGraphWrapper<Graph,
|
alpar@774
|
826 |
ForwardFilterMap, BackwardFilterMap>& _gw, const Edge& e) :
|
alpar@774
|
827 |
Edge(e), gw(&_gw) { }
|
alpar@774
|
828 |
EdgeIt& operator++() {
|
alpar@774
|
829 |
if (!this->backward) {
|
alpar@774
|
830 |
*(static_cast<GraphEdge*>(this))=
|
alpar@774
|
831 |
++(typename Graph::EdgeIt(*(gw->graph), *this));
|
alpar@774
|
832 |
while (*static_cast<GraphEdge*>(this)!=INVALID &&
|
alpar@774
|
833 |
!(*(gw->forward_filter))[*this])
|
alpar@774
|
834 |
*(static_cast<GraphEdge*>(this))=
|
alpar@774
|
835 |
++(typename Graph::EdgeIt(*(gw->graph), *this));
|
marci@775
|
836 |
if (*static_cast<GraphEdge*>(this)==INVALID) {
|
alpar@774
|
837 |
*static_cast<Edge*>(this)=
|
alpar@774
|
838 |
Edge(typename Graph::EdgeIt(*(gw->graph)), true);
|
marci@775
|
839 |
while (*static_cast<GraphEdge*>(this)!=INVALID &&
|
marci@775
|
840 |
!(*(gw->backward_filter))[*this])
|
marci@775
|
841 |
*(static_cast<GraphEdge*>(this))=
|
marci@775
|
842 |
++(typename Graph::EdgeIt(*(gw->graph), *this));
|
marci@775
|
843 |
}
|
alpar@774
|
844 |
} else {
|
alpar@774
|
845 |
*(static_cast<GraphEdge*>(this))=
|
alpar@774
|
846 |
++(typename Graph::EdgeIt(*(gw->graph), *this));
|
alpar@774
|
847 |
while (*static_cast<GraphEdge*>(this)!=INVALID &&
|
alpar@774
|
848 |
!(*(gw->backward_filter))[*this])
|
alpar@774
|
849 |
*(static_cast<GraphEdge*>(this))=
|
alpar@774
|
850 |
++(typename Graph::EdgeIt(*(gw->graph), *this));
|
marci@569
|
851 |
}
|
alpar@774
|
852 |
return *this;
|
marci@569
|
853 |
}
|
marci@569
|
854 |
};
|
marci@569
|
855 |
|
marci@569
|
856 |
using GraphWrapper<Graph>::first;
|
marci@569
|
857 |
OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
|
marci@569
|
858 |
i=OutEdgeIt(*this, p); return i;
|
marci@569
|
859 |
}
|
marci@569
|
860 |
InEdgeIt& first(InEdgeIt& i, const Node& p) const {
|
marci@569
|
861 |
i=InEdgeIt(*this, p); return i;
|
marci@569
|
862 |
}
|
marci@569
|
863 |
EdgeIt& first(EdgeIt& i) const {
|
marci@569
|
864 |
i=EdgeIt(*this); return i;
|
marci@569
|
865 |
}
|
marci@556
|
866 |
|
marci@569
|
867 |
|
marci@569
|
868 |
Node tail(Edge e) const {
|
marci@569
|
869 |
return ((!e.backward) ? this->graph->tail(e) : this->graph->head(e)); }
|
marci@569
|
870 |
Node head(Edge e) const {
|
marci@569
|
871 |
return ((!e.backward) ? this->graph->head(e) : this->graph->tail(e)); }
|
marci@569
|
872 |
|
marci@572
|
873 |
/// Gives back the opposite edge.
|
marci@572
|
874 |
Edge opposite(const Edge& e) const {
|
marci@572
|
875 |
Edge f=e;
|
marci@572
|
876 |
f.backward=!f.backward;
|
marci@572
|
877 |
return f;
|
marci@572
|
878 |
}
|
marci@572
|
879 |
|
marci@792
|
880 |
/// \warning This is a linear time operation and works only if
|
marci@792
|
881 |
/// \c Graph::EdgeIt is defined.
|
marci@792
|
882 |
int edgeNum() const {
|
marci@792
|
883 |
int i=0;
|
marci@792
|
884 |
for (EdgeIt e(*this); e!=INVALID; ++e) ++i;
|
marci@792
|
885 |
return i;
|
marci@792
|
886 |
}
|
marci@569
|
887 |
|
marci@569
|
888 |
bool forward(const Edge& e) const { return !e.backward; }
|
marci@569
|
889 |
bool backward(const Edge& e) const { return e.backward; }
|
marci@569
|
890 |
|
marci@569
|
891 |
|
marci@569
|
892 |
template <typename T>
|
marci@792
|
893 |
/// \c SubBidirGraphWrapper<..., ..., ...>::EdgeMap contains two
|
marci@792
|
894 |
/// Graph::EdgeMap one for the forward edges and
|
marci@792
|
895 |
/// one for the backward edges.
|
marci@569
|
896 |
class EdgeMap {
|
marci@569
|
897 |
typename Graph::template EdgeMap<T> forward_map, backward_map;
|
marci@569
|
898 |
public:
|
marci@623
|
899 |
typedef T ValueType;
|
marci@623
|
900 |
typedef Edge KeyType;
|
marci@650
|
901 |
EdgeMap(const SubBidirGraphWrapper<Graph,
|
alpar@774
|
902 |
ForwardFilterMap, BackwardFilterMap>& g) :
|
alpar@774
|
903 |
forward_map(*(g.graph)), backward_map(*(g.graph)) { }
|
marci@650
|
904 |
EdgeMap(const SubBidirGraphWrapper<Graph,
|
alpar@774
|
905 |
ForwardFilterMap, BackwardFilterMap>& g, T a) :
|
alpar@774
|
906 |
forward_map(*(g.graph), a), backward_map(*(g.graph), a) { }
|
marci@569
|
907 |
void set(Edge e, T a) {
|
marci@569
|
908 |
if (!e.backward)
|
marci@792
|
909 |
forward_map.set(e, a);
|
marci@569
|
910 |
else
|
marci@792
|
911 |
backward_map.set(e, a);
|
marci@569
|
912 |
}
|
marci@569
|
913 |
T operator[](Edge e) const {
|
marci@569
|
914 |
if (!e.backward)
|
marci@792
|
915 |
return forward_map[e];
|
marci@569
|
916 |
else
|
marci@792
|
917 |
return backward_map[e];
|
marci@569
|
918 |
}
|
marci@625
|
919 |
void update() {
|
marci@625
|
920 |
forward_map.update();
|
marci@625
|
921 |
backward_map.update();
|
marci@625
|
922 |
}
|
marci@569
|
923 |
};
|
deba@877
|
924 |
|
deba@877
|
925 |
|
deba@877
|
926 |
KEEP_NODE_MAP(Parent, SubBidirGraphWrapper);
|
deba@877
|
927 |
|
marci@569
|
928 |
};
|
marci@569
|
929 |
|
marci@650
|
930 |
|
marci@650
|
931 |
///\brief A wrapper for composing bidirected graph from a directed one.
|
marci@650
|
932 |
///
|
alpar@879
|
933 |
///\warning Graph wrappers are in even more experimental state than the other
|
alpar@879
|
934 |
///parts of the lib. Use them at you own risk.
|
alpar@879
|
935 |
///
|
marci@650
|
936 |
/// A wrapper for composing bidirected graph from a directed one.
|
marci@650
|
937 |
/// A bidirected graph is composed over the directed one without physical
|
marci@650
|
938 |
/// storage. As the oppositely directed edges are logically different ones
|
marci@650
|
939 |
/// the maps are able to attach different values for them.
|
marci@650
|
940 |
template<typename Graph>
|
marci@650
|
941 |
class BidirGraphWrapper :
|
marci@650
|
942 |
public SubBidirGraphWrapper<
|
marci@650
|
943 |
Graph,
|
marci@650
|
944 |
ConstMap<typename Graph::Edge, bool>,
|
marci@650
|
945 |
ConstMap<typename Graph::Edge, bool> > {
|
marci@650
|
946 |
public:
|
marci@650
|
947 |
typedef SubBidirGraphWrapper<
|
marci@650
|
948 |
Graph,
|
marci@650
|
949 |
ConstMap<typename Graph::Edge, bool>,
|
marci@650
|
950 |
ConstMap<typename Graph::Edge, bool> > Parent;
|
marci@650
|
951 |
protected:
|
marci@650
|
952 |
ConstMap<typename Graph::Edge, bool> cm;
|
marci@650
|
953 |
|
marci@655
|
954 |
BidirGraphWrapper() : Parent(), cm(true) {
|
marci@655
|
955 |
Parent::setForwardFilterMap(cm);
|
marci@655
|
956 |
Parent::setBackwardFilterMap(cm);
|
marci@655
|
957 |
}
|
marci@650
|
958 |
public:
|
marci@650
|
959 |
BidirGraphWrapper(Graph& _graph) : Parent() {
|
marci@650
|
960 |
Parent::setGraph(_graph);
|
marci@650
|
961 |
Parent::setForwardFilterMap(cm);
|
marci@650
|
962 |
Parent::setBackwardFilterMap(cm);
|
marci@650
|
963 |
}
|
marci@738
|
964 |
|
marci@738
|
965 |
int edgeNum() const {
|
marci@738
|
966 |
return 2*this->graph->edgeNum();
|
marci@738
|
967 |
}
|
deba@877
|
968 |
KEEP_MAPS(Parent, BidirGraphWrapper);
|
marci@650
|
969 |
};
|
marci@650
|
970 |
|
marci@650
|
971 |
|
marci@612
|
972 |
/// \brief A bidirected graph template.
|
marci@612
|
973 |
///
|
alpar@879
|
974 |
///\warning Graph wrappers are in even more experimental state than the other
|
alpar@879
|
975 |
///parts of the lib. Use them at you own risk.
|
alpar@879
|
976 |
///
|
marci@612
|
977 |
/// A bidirected graph template.
|
marci@612
|
978 |
/// Such a bidirected graph stores each pair of oppositely directed edges
|
marci@612
|
979 |
/// ones in the memory, i.e. a directed graph of type
|
marci@612
|
980 |
/// \c Graph is used for that.
|
marci@612
|
981 |
/// As the oppositely directed edges are logically different ones
|
marci@612
|
982 |
/// the maps are able to attach different values for them.
|
marci@612
|
983 |
/// \ingroup graphs
|
marci@612
|
984 |
template<typename Graph>
|
marci@612
|
985 |
class BidirGraph : public BidirGraphWrapper<Graph> {
|
marci@650
|
986 |
public:
|
marci@612
|
987 |
typedef UndirGraphWrapper<Graph> Parent;
|
marci@612
|
988 |
protected:
|
marci@612
|
989 |
Graph gr;
|
marci@612
|
990 |
public:
|
marci@612
|
991 |
BidirGraph() : BidirGraphWrapper<Graph>() {
|
marci@612
|
992 |
Parent::setGraph(gr);
|
marci@612
|
993 |
}
|
deba@877
|
994 |
KEEP_MAPS(Parent, BidirGraph);
|
marci@612
|
995 |
};
|
marci@569
|
996 |
|
marci@556
|
997 |
|
marci@650
|
998 |
|
marci@650
|
999 |
template<typename Graph, typename Number,
|
marci@650
|
1000 |
typename CapacityMap, typename FlowMap>
|
marci@658
|
1001 |
class ResForwardFilter {
|
marci@658
|
1002 |
// const Graph* graph;
|
marci@650
|
1003 |
const CapacityMap* capacity;
|
marci@650
|
1004 |
const FlowMap* flow;
|
marci@650
|
1005 |
public:
|
marci@658
|
1006 |
ResForwardFilter(/*const Graph& _graph, */
|
marci@658
|
1007 |
const CapacityMap& _capacity, const FlowMap& _flow) :
|
marci@658
|
1008 |
/*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
|
marci@658
|
1009 |
ResForwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
|
marci@656
|
1010 |
void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
|
marci@656
|
1011 |
void setFlow(const FlowMap& _flow) { flow=&_flow; }
|
marci@650
|
1012 |
bool operator[](const typename Graph::Edge& e) const {
|
marci@738
|
1013 |
return (Number((*flow)[e]) < Number((*capacity)[e]));
|
marci@650
|
1014 |
}
|
marci@650
|
1015 |
};
|
marci@650
|
1016 |
|
marci@650
|
1017 |
template<typename Graph, typename Number,
|
marci@650
|
1018 |
typename CapacityMap, typename FlowMap>
|
marci@658
|
1019 |
class ResBackwardFilter {
|
marci@650
|
1020 |
const CapacityMap* capacity;
|
marci@650
|
1021 |
const FlowMap* flow;
|
marci@650
|
1022 |
public:
|
marci@658
|
1023 |
ResBackwardFilter(/*const Graph& _graph,*/
|
marci@658
|
1024 |
const CapacityMap& _capacity, const FlowMap& _flow) :
|
marci@658
|
1025 |
/*graph(&_graph),*/ capacity(&_capacity), flow(&_flow) { }
|
marci@658
|
1026 |
ResBackwardFilter() : /*graph(0),*/ capacity(0), flow(0) { }
|
marci@656
|
1027 |
void setCapacity(const CapacityMap& _capacity) { capacity=&_capacity; }
|
marci@656
|
1028 |
void setFlow(const FlowMap& _flow) { flow=&_flow; }
|
marci@650
|
1029 |
bool operator[](const typename Graph::Edge& e) const {
|
marci@738
|
1030 |
return (Number(0) < Number((*flow)[e]));
|
marci@650
|
1031 |
}
|
marci@650
|
1032 |
};
|
marci@650
|
1033 |
|
marci@653
|
1034 |
|
marci@653
|
1035 |
/// A wrapper for composing the residual graph for directed flow and circulation problems.
|
marci@650
|
1036 |
|
alpar@879
|
1037 |
///\warning Graph wrappers are in even more experimental state than the other
|
alpar@879
|
1038 |
///parts of the lib. Use them at you own risk.
|
alpar@879
|
1039 |
///
|
marci@653
|
1040 |
/// A wrapper for composing the residual graph for directed flow and circulation problems.
|
marci@650
|
1041 |
template<typename Graph, typename Number,
|
marci@650
|
1042 |
typename CapacityMap, typename FlowMap>
|
marci@653
|
1043 |
class ResGraphWrapper :
|
marci@650
|
1044 |
public SubBidirGraphWrapper<
|
marci@650
|
1045 |
Graph,
|
marci@658
|
1046 |
ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,
|
marci@658
|
1047 |
ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > {
|
marci@650
|
1048 |
public:
|
marci@650
|
1049 |
typedef SubBidirGraphWrapper<
|
marci@650
|
1050 |
Graph,
|
marci@658
|
1051 |
ResForwardFilter<Graph, Number, CapacityMap, FlowMap>,
|
marci@658
|
1052 |
ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> > Parent;
|
marci@650
|
1053 |
protected:
|
marci@650
|
1054 |
const CapacityMap* capacity;
|
marci@650
|
1055 |
FlowMap* flow;
|
marci@658
|
1056 |
ResForwardFilter<Graph, Number, CapacityMap, FlowMap> forward_filter;
|
marci@658
|
1057 |
ResBackwardFilter<Graph, Number, CapacityMap, FlowMap> backward_filter;
|
marci@658
|
1058 |
ResGraphWrapper() : Parent(),
|
marci@658
|
1059 |
capacity(0), flow(0) { }
|
marci@658
|
1060 |
void setCapacityMap(const CapacityMap& _capacity) {
|
marci@658
|
1061 |
capacity=&_capacity;
|
marci@658
|
1062 |
forward_filter.setCapacity(_capacity);
|
marci@658
|
1063 |
backward_filter.setCapacity(_capacity);
|
marci@658
|
1064 |
}
|
marci@658
|
1065 |
void setFlowMap(FlowMap& _flow) {
|
marci@658
|
1066 |
flow=&_flow;
|
marci@658
|
1067 |
forward_filter.setFlow(_flow);
|
marci@658
|
1068 |
backward_filter.setFlow(_flow);
|
marci@658
|
1069 |
}
|
marci@650
|
1070 |
public:
|
marci@653
|
1071 |
ResGraphWrapper(Graph& _graph, const CapacityMap& _capacity,
|
marci@650
|
1072 |
FlowMap& _flow) :
|
marci@650
|
1073 |
Parent(), capacity(&_capacity), flow(&_flow),
|
marci@658
|
1074 |
forward_filter(/*_graph,*/ _capacity, _flow),
|
marci@658
|
1075 |
backward_filter(/*_graph,*/ _capacity, _flow) {
|
marci@650
|
1076 |
Parent::setGraph(_graph);
|
marci@650
|
1077 |
Parent::setForwardFilterMap(forward_filter);
|
marci@650
|
1078 |
Parent::setBackwardFilterMap(backward_filter);
|
marci@650
|
1079 |
}
|
marci@650
|
1080 |
|
marci@660
|
1081 |
typedef typename Parent::Edge Edge;
|
marci@660
|
1082 |
|
marci@660
|
1083 |
void augment(const Edge& e, Number a) const {
|
marci@650
|
1084 |
if (Parent::forward(e))
|
marci@650
|
1085 |
flow->set(e, (*flow)[e]+a);
|
marci@650
|
1086 |
else
|
marci@650
|
1087 |
flow->set(e, (*flow)[e]-a);
|
marci@650
|
1088 |
}
|
marci@650
|
1089 |
|
marci@660
|
1090 |
/// \brief Residual capacity map.
|
marci@660
|
1091 |
///
|
marci@792
|
1092 |
/// In generic residual graphs the residual capacity can be obtained as a map. Not tested.
|
marci@660
|
1093 |
class ResCap {
|
marci@660
|
1094 |
protected:
|
marci@660
|
1095 |
const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>* res_graph;
|
marci@660
|
1096 |
public:
|
marci@660
|
1097 |
typedef Number ValueType;
|
marci@660
|
1098 |
typedef Edge KeyType;
|
marci@660
|
1099 |
ResCap(const ResGraphWrapper<Graph, Number, CapacityMap, FlowMap>& _res_graph) :
|
marci@660
|
1100 |
res_graph(&_res_graph) { }
|
marci@660
|
1101 |
Number operator[](const Edge& e) const {
|
marci@660
|
1102 |
if (res_graph->forward(e))
|
marci@660
|
1103 |
return (*(res_graph->capacity))[e]-(*(res_graph->flow))[e];
|
marci@660
|
1104 |
else
|
marci@660
|
1105 |
return (*(res_graph->flow))[e];
|
marci@660
|
1106 |
}
|
marci@660
|
1107 |
};
|
marci@660
|
1108 |
|
deba@877
|
1109 |
KEEP_MAPS(Parent, ResGraphWrapper);
|
marci@650
|
1110 |
};
|
marci@650
|
1111 |
|
marci@650
|
1112 |
|
marci@612
|
1113 |
/// For blocking flows.
|
marci@556
|
1114 |
|
alpar@879
|
1115 |
///\warning Graph wrappers are in even more experimental state than the other
|
alpar@879
|
1116 |
///parts of the lib. Use them at you own risk.
|
alpar@879
|
1117 |
///
|
marci@792
|
1118 |
/// This graph wrapper is used for on-the-fly
|
marci@792
|
1119 |
/// Dinits blocking flow computations.
|
marci@612
|
1120 |
/// For each node, an out-edge is stored which is used when the
|
marci@612
|
1121 |
/// \code
|
marci@612
|
1122 |
/// OutEdgeIt& first(OutEdgeIt&, const Node&)
|
marci@612
|
1123 |
/// \endcode
|
marci@612
|
1124 |
/// is called.
|
marci@556
|
1125 |
///
|
marci@792
|
1126 |
/// \author Marton Makai
|
marci@556
|
1127 |
template<typename Graph, typename FirstOutEdgesMap>
|
marci@556
|
1128 |
class ErasingFirstGraphWrapper : public GraphWrapper<Graph> {
|
marci@650
|
1129 |
public:
|
marci@650
|
1130 |
typedef GraphWrapper<Graph> Parent;
|
marci@556
|
1131 |
protected:
|
marci@556
|
1132 |
FirstOutEdgesMap* first_out_edges;
|
marci@556
|
1133 |
public:
|
marci@556
|
1134 |
ErasingFirstGraphWrapper(Graph& _graph,
|
marci@556
|
1135 |
FirstOutEdgesMap& _first_out_edges) :
|
marci@556
|
1136 |
GraphWrapper<Graph>(_graph), first_out_edges(&_first_out_edges) { }
|
marci@556
|
1137 |
|
marci@556
|
1138 |
typedef typename GraphWrapper<Graph>::Node Node;
|
marci@556
|
1139 |
typedef typename GraphWrapper<Graph>::Edge Edge;
|
marci@777
|
1140 |
class OutEdgeIt : public Edge {
|
marci@556
|
1141 |
friend class GraphWrapper<Graph>;
|
marci@556
|
1142 |
friend class ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>;
|
marci@777
|
1143 |
const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>* gw;
|
marci@556
|
1144 |
public:
|
marci@556
|
1145 |
OutEdgeIt() { }
|
marci@777
|
1146 |
OutEdgeIt(Invalid i) : Edge(i) { }
|
marci@777
|
1147 |
OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _gw,
|
marci@777
|
1148 |
const Node& n) :
|
marci@777
|
1149 |
Edge((*(_gw.first_out_edges))[n]), gw(&_gw) { }
|
marci@777
|
1150 |
OutEdgeIt(const ErasingFirstGraphWrapper<Graph, FirstOutEdgesMap>& _gw,
|
marci@777
|
1151 |
const Edge& e) :
|
marci@777
|
1152 |
Edge(e), gw(&_gw) { }
|
marci@777
|
1153 |
OutEdgeIt& operator++() {
|
marci@777
|
1154 |
*(static_cast<Edge*>(this))=
|
marci@777
|
1155 |
++(typename Graph::OutEdgeIt(*(gw->graph), *this));
|
marci@777
|
1156 |
return *this;
|
marci@777
|
1157 |
}
|
marci@556
|
1158 |
};
|
marci@556
|
1159 |
|
marci@556
|
1160 |
using GraphWrapper<Graph>::first;
|
marci@556
|
1161 |
OutEdgeIt& first(OutEdgeIt& i, const Node& p) const {
|
marci@556
|
1162 |
i=OutEdgeIt(*this, p); return i;
|
marci@556
|
1163 |
}
|
marci@777
|
1164 |
void erase(const Edge& e) const {
|
marci@777
|
1165 |
Node n=tail(e);
|
deba@844
|
1166 |
typename Graph::OutEdgeIt f(*Parent::graph, n);
|
marci@777
|
1167 |
++f;
|
marci@777
|
1168 |
first_out_edges->set(n, f);
|
marci@556
|
1169 |
}
|
deba@877
|
1170 |
|
deba@877
|
1171 |
KEEP_MAPS(Parent, ErasingFirstGraphWrapper);
|
marci@556
|
1172 |
};
|
marci@556
|
1173 |
|
marci@556
|
1174 |
///@}
|
marci@556
|
1175 |
|
marci@556
|
1176 |
} //namespace hugo
|
marci@556
|
1177 |
|
marci@556
|
1178 |
#endif //HUGO_GRAPH_WRAPPER_H
|
marci@556
|
1179 |
|