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