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