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