| 1 |
1 |
/* -*- C++ -*-
|
| 2 |
2 |
*
|
| 3 |
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
| 4 |
4 |
*
|
| 5 |
5 |
* Copyright (C) 2003-2008
|
| 6 |
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
| 7 |
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
| 8 |
8 |
*
|
| 9 |
9 |
* Permission to use, modify and distribute this software is granted
|
| 10 |
10 |
* provided that this copyright notice appears in all copies. For
|
| 11 |
11 |
* precise terms see the accompanying LICENSE file.
|
| 12 |
12 |
*
|
| 13 |
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
| 14 |
14 |
* express or implied, and with no claim as to its suitability for any
|
| 15 |
15 |
* purpose.
|
| 16 |
16 |
*
|
| 17 |
17 |
*/
|
| 18 |
18 |
|
| 19 |
19 |
#ifndef LEMON_GRAPH_UTILS_H
|
| 20 |
20 |
#define LEMON_GRAPH_UTILS_H
|
| 21 |
21 |
|
| 22 |
22 |
#include <iterator>
|
| 23 |
23 |
#include <vector>
|
| 24 |
24 |
#include <map>
|
| 25 |
25 |
#include <cmath>
|
| 26 |
26 |
#include <algorithm>
|
| 27 |
27 |
|
| 28 |
28 |
#include <lemon/bits/invalid.h>
|
| 29 |
29 |
#include <lemon/bits/utility.h>
|
| 30 |
30 |
#include <lemon/maps.h>
|
| 31 |
31 |
#include <lemon/bits/traits.h>
|
| 32 |
32 |
|
| 33 |
33 |
#include <lemon/bits/alteration_notifier.h>
|
| 34 |
34 |
#include <lemon/bits/default_map.h>
|
| 35 |
35 |
|
| 36 |
36 |
///\ingroup gutils
|
| 37 |
37 |
///\file
|
| 38 |
38 |
///\brief Graph utilities.
|
| 39 |
39 |
|
| 40 |
40 |
namespace lemon {
|
| 41 |
41 |
|
| 42 |
42 |
/// \addtogroup gutils
|
| 43 |
43 |
/// @{
|
| 44 |
44 |
|
|
45 |
namespace _graph_utils_bits {
|
|
46 |
template <typename Graph>
|
|
47 |
struct Node { typedef typename Graph::Node type; };
|
|
48 |
|
|
49 |
template <typename Graph>
|
|
50 |
struct NodeIt { typedef typename Graph::NodeIt type; };
|
|
51 |
|
|
52 |
template <typename Graph>
|
|
53 |
struct Arc { typedef typename Graph::Arc type; };
|
|
54 |
|
|
55 |
template <typename Graph>
|
|
56 |
struct ArcIt { typedef typename Graph::ArcIt type; };
|
|
57 |
|
|
58 |
template <typename Graph>
|
|
59 |
struct Edge { typedef typename Graph::Edge type; };
|
|
60 |
|
|
61 |
template <typename Graph>
|
|
62 |
struct EdgeIt { typedef typename Graph::EdgeIt type; };
|
|
63 |
|
|
64 |
template <typename Graph>
|
|
65 |
struct OutArcIt { typedef typename Graph::OutArcIt type; };
|
|
66 |
|
|
67 |
template <typename Graph>
|
|
68 |
struct InArcIt { typedef typename Graph::InArcIt type; };
|
|
69 |
|
|
70 |
template <typename Graph>
|
|
71 |
struct IncEdgeIt { typedef typename Graph::IncEdgeIt type; };
|
|
72 |
|
|
73 |
template <typename Graph>
|
|
74 |
struct BoolNodeMap {
|
|
75 |
typedef typename Graph::template NodeMap<bool> type;
|
|
76 |
};
|
|
77 |
|
|
78 |
template <typename Graph>
|
|
79 |
struct IntNodeMap {
|
|
80 |
typedef typename Graph::template NodeMap<int> type;
|
|
81 |
};
|
|
82 |
|
|
83 |
template <typename Graph>
|
|
84 |
struct DoubleNodeMap {
|
|
85 |
typedef typename Graph::template NodeMap<double> type;
|
|
86 |
};
|
|
87 |
|
|
88 |
template <typename Graph>
|
|
89 |
struct BoolArcMap {
|
|
90 |
typedef typename Graph::template ArcMap<bool> type;
|
|
91 |
};
|
|
92 |
|
|
93 |
template <typename Graph>
|
|
94 |
struct IntArcMap {
|
|
95 |
typedef typename Graph::template ArcMap<int> type;
|
|
96 |
};
|
|
97 |
|
|
98 |
template <typename Graph>
|
|
99 |
struct DoubleArcMap {
|
|
100 |
typedef typename Graph::template ArcMap<double> type;
|
|
101 |
};
|
|
102 |
|
|
103 |
template <typename Graph>
|
|
104 |
struct BoolEdgeMap {
|
|
105 |
typedef typename Graph::template EdgeMap<bool> type;
|
|
106 |
};
|
|
107 |
|
|
108 |
template <typename Graph>
|
|
109 |
struct IntEdgeMap {
|
|
110 |
typedef typename Graph::template EdgeMap<int> type;
|
|
111 |
};
|
|
112 |
|
|
113 |
template <typename Graph>
|
|
114 |
struct DoubleEdgeMap {
|
|
115 |
typedef typename Graph::template EdgeMap<double> type;
|
|
116 |
};
|
|
117 |
|
|
118 |
|
|
119 |
}
|
|
120 |
|
| 45 |
121 |
///Creates convenience typedefs for the digraph types and iterators
|
| 46 |
122 |
|
| 47 |
123 |
///This \c \#define creates convenience typedefs for the following types
|
| 48 |
124 |
///of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
|
| 49 |
125 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap,
|
| 50 |
126 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap.
|
| 51 |
127 |
#define DIGRAPH_TYPEDEFS(Digraph) \
|
| 52 |
|
typedef Digraph::Node Node; \
|
| 53 |
|
typedef Digraph::NodeIt NodeIt; \
|
| 54 |
|
typedef Digraph::Arc Arc; \
|
| 55 |
|
typedef Digraph::ArcIt ArcIt; \
|
| 56 |
|
typedef Digraph::InArcIt InArcIt; \
|
| 57 |
|
typedef Digraph::OutArcIt OutArcIt
|
|
128 |
typedef typename ::lemon::_graph_utils_bits:: \
|
|
129 |
Node<Digraph>::type Node; \
|
|
130 |
typedef typename ::lemon::_graph_utils_bits:: \
|
|
131 |
NodeIt<Digraph>::type NodeIt; \
|
|
132 |
typedef typename ::lemon::_graph_utils_bits:: \
|
|
133 |
Arc<Digraph>::type Arc; \
|
|
134 |
typedef typename ::lemon::_graph_utils_bits:: \
|
|
135 |
ArcIt<Digraph>::type ArcIt; \
|
|
136 |
typedef typename ::lemon::_graph_utils_bits:: \
|
|
137 |
OutArcIt<Digraph>::type OutArcIt; \
|
|
138 |
typedef typename ::lemon::_graph_utils_bits:: \
|
|
139 |
InArcIt<Digraph>::type InArcIt; \
|
|
140 |
typedef typename ::lemon::_graph_utils_bits:: \
|
|
141 |
BoolNodeMap<Digraph>::type BoolNodeMap; \
|
|
142 |
typedef typename ::lemon::_graph_utils_bits:: \
|
|
143 |
IntNodeMap<Digraph>::type IntNodeMap; \
|
|
144 |
typedef typename ::lemon::_graph_utils_bits:: \
|
|
145 |
DoubleNodeMap<Digraph>::type DoubleNodeMap; \
|
|
146 |
typedef typename ::lemon::_graph_utils_bits:: \
|
|
147 |
BoolArcMap<Digraph>::type BoolArcMap; \
|
|
148 |
typedef typename ::lemon::_graph_utils_bits:: \
|
|
149 |
IntArcMap<Digraph>::type IntArcMap; \
|
|
150 |
typedef typename ::lemon::_graph_utils_bits:: \
|
|
151 |
DoubleArcMap<Digraph>::type DoubleArcMap
|
|
152 |
|
| 58 |
153 |
|
| 59 |
154 |
///Creates convenience typedefs for the graph types and iterators
|
| 60 |
155 |
|
| 61 |
156 |
///This \c \#define creates the same convenience typedefs as defined
|
| 62 |
157 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates
|
| 63 |
158 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap,
|
| 64 |
159 |
///\c DoubleEdgeMap.
|
| 65 |
160 |
#define GRAPH_TYPEDEFS(Graph) \
|
| 66 |
161 |
DIGRAPH_TYPEDEFS(Graph); \
|
| 67 |
|
typedef Graph::Edge Edge; \
|
| 68 |
|
typedef Graph::EdgeIt EdgeIt; \
|
| 69 |
|
typedef Graph::IncEdgeIt IncEdgeIt
|
|
162 |
typedef typename ::lemon::_graph_utils_bits:: \
|
|
163 |
Edge<Graph>::type Edge; \
|
|
164 |
typedef typename ::lemon::_graph_utils_bits:: \
|
|
165 |
EdgeIt<Graph>::type EdgeIt; \
|
|
166 |
typedef typename ::lemon::_graph_utils_bits:: \
|
|
167 |
IncEdgeIt<Graph>::type IncEdgeIt \
|
|
168 |
typedef typename ::lemon::_graph_utils_bits:: \
|
|
169 |
BoolEdgeMap<Graph>::type BoolEdgeMap; \
|
|
170 |
typedef typename ::lemon::_graph_utils_bits:: \
|
|
171 |
IntEdgeMap<Graph>::type IntEdgeMap; \
|
|
172 |
typedef typename ::lemon::_graph_utils_bits:: \
|
|
173 |
DoubleEdgeMap<Graph>::type DoubleEdgeMap
|
|
174 |
|
| 70 |
175 |
|
| 71 |
176 |
/// \brief Function to count the items in the graph.
|
| 72 |
177 |
///
|
| 73 |
178 |
/// This function counts the items (nodes, arcs etc) in the graph.
|
| 74 |
179 |
/// The complexity of the function is O(n) because
|
| 75 |
180 |
/// it iterates on all of the items.
|
| 76 |
181 |
template <typename Graph, typename Item>
|
| 77 |
182 |
inline int countItems(const Graph& g) {
|
| 78 |
183 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
|
| 79 |
184 |
int num = 0;
|
| 80 |
185 |
for (ItemIt it(g); it != INVALID; ++it) {
|
| 81 |
186 |
++num;
|
| 82 |
187 |
}
|
| 83 |
188 |
return num;
|
| 84 |
189 |
}
|
| 85 |
190 |
|
| 86 |
191 |
// Node counting:
|
| 87 |
192 |
|
| 88 |
193 |
namespace _graph_utils_bits {
|
| 89 |
194 |
|
| 90 |
195 |
template <typename Graph, typename Enable = void>
|
| 91 |
196 |
struct CountNodesSelector {
|
| 92 |
197 |
static int count(const Graph &g) {
|
| 93 |
198 |
return countItems<Graph, typename Graph::Node>(g);
|
| 94 |
199 |
}
|
| 95 |
200 |
};
|
| 96 |
201 |
|
| 97 |
202 |
template <typename Graph>
|
| 98 |
203 |
struct CountNodesSelector<
|
| 99 |
204 |
Graph, typename
|
| 100 |
205 |
enable_if<typename Graph::NodeNumTag, void>::type>
|
| 101 |
206 |
{
|
| 102 |
207 |
static int count(const Graph &g) {
|
| 103 |
208 |
return g.nodeNum();
|
| 104 |
209 |
}
|
| 105 |
210 |
};
|
| 106 |
211 |
}
|
| 107 |
212 |
|
| 108 |
213 |
/// \brief Function to count the nodes in the graph.
|
| 109 |
214 |
///
|
| 110 |
215 |
/// This function counts the nodes in the graph.
|
| 111 |
216 |
/// The complexity of the function is O(n) but for some
|
| 112 |
217 |
/// graph structures it is specialized to run in O(1).
|
| 113 |
218 |
///
|
| 114 |
219 |
/// If the graph contains a \e nodeNum() member function and a
|
| 115 |
220 |
/// \e NodeNumTag tag then this function calls directly the member
|
| 116 |
221 |
/// function to query the cardinality of the node set.
|
| 117 |
222 |
template <typename Graph>
|
| 118 |
223 |
inline int countNodes(const Graph& g) {
|
| 119 |
224 |
return _graph_utils_bits::CountNodesSelector<Graph>::count(g);
|
| 120 |
225 |
}
|
| 121 |
226 |
|
| 122 |
227 |
// Arc counting:
|
| 123 |
228 |
|
| 124 |
229 |
namespace _graph_utils_bits {
|
| 125 |
230 |
|
| 126 |
231 |
template <typename Graph, typename Enable = void>
|
| 127 |
232 |
struct CountArcsSelector {
|
| 128 |
233 |
static int count(const Graph &g) {
|
| 129 |
234 |
return countItems<Graph, typename Graph::Arc>(g);
|
| 130 |
235 |
}
|
| 131 |
236 |
};
|
| 132 |
237 |
|
| 133 |
238 |
template <typename Graph>
|
| 134 |
239 |
struct CountArcsSelector<
|
| 135 |
240 |
Graph,
|
| 136 |
241 |
typename enable_if<typename Graph::ArcNumTag, void>::type>
|
| 137 |
242 |
{
|
| 138 |
243 |
static int count(const Graph &g) {
|
| 139 |
244 |
return g.arcNum();
|
| 140 |
245 |
}
|
| 141 |
246 |
};
|
| 142 |
247 |
}
|
| 143 |
248 |
|
| 144 |
249 |
/// \brief Function to count the arcs in the graph.
|
| 145 |
250 |
///
|
| 146 |
251 |
/// This function counts the arcs in the graph.
|
| 147 |
252 |
/// The complexity of the function is O(e) but for some
|
| 148 |
253 |
/// graph structures it is specialized to run in O(1).
|
| 149 |
254 |
///
|
| 150 |
255 |
/// If the graph contains a \e arcNum() member function and a
|
| 151 |
256 |
/// \e EdgeNumTag tag then this function calls directly the member
|
| 152 |
257 |
/// function to query the cardinality of the arc set.
|
| 153 |
258 |
template <typename Graph>
|
| 154 |
259 |
inline int countArcs(const Graph& g) {
|
| 155 |
260 |
return _graph_utils_bits::CountArcsSelector<Graph>::count(g);
|
| 156 |
261 |
}
|
| 157 |
262 |
|
| 158 |
263 |
// Edge counting:
|
| 159 |
264 |
namespace _graph_utils_bits {
|
| 160 |
265 |
|
| 161 |
266 |
template <typename Graph, typename Enable = void>
|
| 162 |
267 |
struct CountEdgesSelector {
|
| 163 |
268 |
static int count(const Graph &g) {
|
| 164 |
269 |
return countItems<Graph, typename Graph::Edge>(g);
|
| 165 |
270 |
}
|
| 166 |
271 |
};
|
| 167 |
272 |
|
| 168 |
273 |
template <typename Graph>
|
| 169 |
274 |
struct CountEdgesSelector<
|
| 170 |
275 |
Graph,
|
| 171 |
276 |
typename enable_if<typename Graph::EdgeNumTag, void>::type>
|
| 172 |
277 |
{
|
| 173 |
278 |
static int count(const Graph &g) {
|
| 174 |
279 |
return g.edgeNum();
|
| 175 |
280 |
}
|
| 176 |
281 |
};
|
| 177 |
282 |
}
|
| 178 |
283 |
|
| 179 |
284 |
/// \brief Function to count the edges in the graph.
|
| 180 |
285 |
///
|
| 181 |
286 |
/// This function counts the edges in the graph.
|
| 182 |
287 |
/// The complexity of the function is O(m) but for some
|
| 183 |
288 |
/// graph structures it is specialized to run in O(1).
|
| 184 |
289 |
///
|
| 185 |
290 |
/// If the graph contains a \e edgeNum() member function and a
|
| 186 |
291 |
/// \e EdgeNumTag tag then this function calls directly the member
|
| 187 |
292 |
/// function to query the cardinality of the edge set.
|
| 188 |
293 |
template <typename Graph>
|
| 189 |
294 |
inline int countEdges(const Graph& g) {
|
| 190 |
295 |
return _graph_utils_bits::CountEdgesSelector<Graph>::count(g);
|
| 191 |
296 |
|
| 192 |
297 |
}
|
| 193 |
298 |
|
| 194 |
299 |
|
| 195 |
300 |
template <typename Graph, typename DegIt>
|
| 196 |
301 |
inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
|
| 197 |
302 |
int num = 0;
|
| 198 |
303 |
for (DegIt it(_g, _n); it != INVALID; ++it) {
|
| 199 |
304 |
++num;
|
| 200 |
305 |
}
|
| 201 |
306 |
return num;
|
| 202 |
307 |
}
|
| 203 |
308 |
|
| 204 |
309 |
/// \brief Function to count the number of the out-arcs from node \c n.
|
| 205 |
310 |
///
|
| 206 |
311 |
/// This function counts the number of the out-arcs from node \c n
|
| 207 |
312 |
/// in the graph.
|
| 208 |
313 |
template <typename Graph>
|
| 209 |
314 |
inline int countOutArcs(const Graph& _g, const typename Graph::Node& _n) {
|
| 210 |
315 |
return countNodeDegree<Graph, typename Graph::OutArcIt>(_g, _n);
|
| 211 |
316 |
}
|
| 212 |
317 |
|
| 213 |
318 |
/// \brief Function to count the number of the in-arcs to node \c n.
|
| 214 |
319 |
///
|
| 215 |
320 |
/// This function counts the number of the in-arcs to node \c n
|
| 216 |
321 |
/// in the graph.
|
| 217 |
322 |
template <typename Graph>
|
| 218 |
323 |
inline int countInArcs(const Graph& _g, const typename Graph::Node& _n) {
|
| 219 |
324 |
return countNodeDegree<Graph, typename Graph::InArcIt>(_g, _n);
|
| 220 |
325 |
}
|
| 221 |
326 |
|
| 222 |
327 |
/// \brief Function to count the number of the inc-edges to node \c n.
|
| 223 |
328 |
///
|
| 224 |
329 |
/// This function counts the number of the inc-edges to node \c n
|
| 225 |
330 |
/// in the graph.
|
| 226 |
331 |
template <typename Graph>
|
| 227 |
332 |
inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) {
|
| 228 |
333 |
return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n);
|
| 229 |
334 |
}
|
| 230 |
335 |
|
| 231 |
336 |
namespace _graph_utils_bits {
|
| 232 |
337 |
|
| 233 |
338 |
template <typename Graph, typename Enable = void>
|
| 234 |
339 |
struct FindArcSelector {
|
| 235 |
340 |
typedef typename Graph::Node Node;
|
| 236 |
341 |
typedef typename Graph::Arc Arc;
|
| 237 |
342 |
static Arc find(const Graph &g, Node u, Node v, Arc e) {
|
| 238 |
343 |
if (e == INVALID) {
|
| 239 |
344 |
g.firstOut(e, u);
|
| 240 |
345 |
} else {
|
| 241 |
346 |
g.nextOut(e);
|
| 242 |
347 |
}
|
| 243 |
348 |
while (e != INVALID && g.target(e) != v) {
|
| 244 |
349 |
g.nextOut(e);
|
| 245 |
350 |
}
|
| 246 |
351 |
return e;
|
| 247 |
352 |
}
|
| 248 |
353 |
};
|
| 249 |
354 |
|
| 250 |
355 |
template <typename Graph>
|
| 251 |
356 |
struct FindArcSelector<
|
| 252 |
357 |
Graph,
|
| 253 |
358 |
typename enable_if<typename Graph::FindEdgeTag, void>::type>
|
| 254 |
359 |
{
|
| 255 |
360 |
typedef typename Graph::Node Node;
|
| 256 |
361 |
typedef typename Graph::Arc Arc;
|
| 257 |
362 |
static Arc find(const Graph &g, Node u, Node v, Arc prev) {
|
| 258 |
363 |
return g.findArc(u, v, prev);
|
| 259 |
364 |
}
|
| 260 |
365 |
};
|
| 261 |
366 |
}
|
| 262 |
367 |
|
| 263 |
368 |
/// \brief Finds an arc between two nodes of a graph.
|
| 264 |
369 |
///
|
| 265 |
370 |
/// Finds an arc from node \c u to node \c v in graph \c g.
|
| 266 |
371 |
///
|
| 267 |
372 |
/// If \c prev is \ref INVALID (this is the default value), then
|
| 268 |
373 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for
|
| 269 |
374 |
/// the next arc from \c u to \c v after \c prev.
|
| 270 |
375 |
/// \return The found arc or \ref INVALID if there is no such an arc.
|
| 271 |
376 |
///
|
| 272 |
377 |
/// Thus you can iterate through each arc from \c u to \c v as it follows.
|
| 273 |
378 |
///\code
|
| 274 |
379 |
/// for(Arc e=findArc(g,u,v);e!=INVALID;e=findArc(g,u,v,e)) {
|
| 275 |
380 |
/// ...
|
| 276 |
381 |
/// }
|
| 277 |
382 |
///\endcode
|
| 278 |
383 |
///
|
| 279 |
384 |
///\sa ArcLookUp
|
| 280 |
385 |
///\sa AllArcLookUp
|
| 281 |
386 |
///\sa DynArcLookUp
|
| 282 |
387 |
///\sa ConArcIt
|
| 283 |
388 |
template <typename Graph>
|
| 284 |
389 |
inline typename Graph::Arc
|
| 285 |
390 |
findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v,
|
| 286 |
391 |
typename Graph::Arc prev = INVALID) {
|
| 287 |
392 |
return _graph_utils_bits::FindArcSelector<Graph>::find(g, u, v, prev);
|
| 288 |
393 |
}
|
| 289 |
394 |
|
| 290 |
395 |
/// \brief Iterator for iterating on arcs connected the same nodes.
|
| 291 |
396 |
///
|
| 292 |
397 |
/// Iterator for iterating on arcs connected the same nodes. It is
|
| 293 |
398 |
/// higher level interface for the findArc() function. You can
|
| 294 |
399 |
/// use it the following way:
|
| 295 |
400 |
///\code
|
| 296 |
401 |
/// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
|
| 297 |
402 |
/// ...
|
| 298 |
403 |
/// }
|
| 299 |
404 |
///\endcode
|
| 300 |
405 |
///
|
| 301 |
406 |
///\sa findArc()
|
| 302 |
407 |
///\sa ArcLookUp
|
| 303 |
408 |
///\sa AllArcLookUp
|
| 304 |
409 |
///\sa DynArcLookUp
|
| 305 |
410 |
///
|
| 306 |
411 |
/// \author Balazs Dezso
|
| 307 |
412 |
template <typename _Graph>
|
| 308 |
413 |
class ConArcIt : public _Graph::Arc {
|
| 309 |
414 |
public:
|
| 310 |
415 |
|
| 311 |
416 |
typedef _Graph Graph;
|
| 312 |
417 |
typedef typename Graph::Arc Parent;
|
| 313 |
418 |
|
| 314 |
419 |
typedef typename Graph::Arc Arc;
|
| 315 |
420 |
typedef typename Graph::Node Node;
|
| 316 |
421 |
|
| 317 |
422 |
/// \brief Constructor.
|
| 318 |
423 |
///
|
| 319 |
424 |
/// Construct a new ConArcIt iterating on the arcs which
|
| 320 |
425 |
/// connects the \c u and \c v node.
|
| 321 |
426 |
ConArcIt(const Graph& g, Node u, Node v) : _graph(g) {
|
| 322 |
427 |
Parent::operator=(findArc(_graph, u, v));
|
| 323 |
428 |
}
|
| 324 |
429 |
|
| 325 |
430 |
/// \brief Constructor.
|
| 326 |
431 |
///
|
| 327 |
432 |
/// Construct a new ConArcIt which continues the iterating from
|
| 328 |
433 |
/// the \c e arc.
|
| 329 |
434 |
ConArcIt(const Graph& g, Arc a) : Parent(a), _graph(g) {}
|
| 330 |
435 |
|
| 331 |
436 |
/// \brief Increment operator.
|
| 332 |
437 |
///
|
| 333 |
438 |
/// It increments the iterator and gives back the next arc.
|
| 334 |
439 |
ConArcIt& operator++() {
|
| 335 |
440 |
Parent::operator=(findArc(_graph, _graph.source(*this),
|
| 336 |
441 |
_graph.target(*this), *this));
|
| 337 |
442 |
return *this;
|
| 338 |
443 |
}
|
| 339 |
444 |
private:
|
| 340 |
445 |
const Graph& _graph;
|
| 341 |
446 |
};
|
| 342 |
447 |
|
| 343 |
448 |
namespace _graph_utils_bits {
|
| 344 |
449 |
|
| 345 |
450 |
template <typename Graph, typename Enable = void>
|
| 346 |
451 |
struct FindEdgeSelector {
|
| 347 |
452 |
typedef typename Graph::Node Node;
|
| 348 |
453 |
typedef typename Graph::Edge Edge;
|
| 349 |
454 |
static Edge find(const Graph &g, Node u, Node v, Edge e) {
|
| 350 |
455 |
bool b;
|
| 351 |
456 |
if (u != v) {
|
| 352 |
457 |
if (e == INVALID) {
|
| 353 |
458 |
g.firstInc(e, b, u);
|
| 354 |
459 |
} else {
|
| 355 |
460 |
b = g.source(e) == u;
|
| 356 |
461 |
g.nextInc(e, b);
|
| 357 |
462 |
}
|
| 358 |
463 |
while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) {
|
| 359 |
464 |
g.nextInc(e, b);
|
| 360 |
465 |
}
|
| 361 |
466 |
} else {
|
| 362 |
467 |
if (e == INVALID) {
|
| 363 |
468 |
g.firstInc(e, b, u);
|
| 364 |
469 |
} else {
|
| 365 |
470 |
b = true;
|
| 366 |
471 |
g.nextInc(e, b);
|
| 367 |
472 |
}
|
| 368 |
473 |
while (e != INVALID && (!b || g.target(e) != v)) {
|
| 369 |
474 |
g.nextInc(e, b);
|
| 370 |
475 |
}
|
| 371 |
476 |
}
|
| 372 |
477 |
return e;
|
| 373 |
478 |
}
|
| 374 |
479 |
};
|
| 375 |
480 |
|
| 376 |
481 |
template <typename Graph>
|
| 377 |
482 |
struct FindEdgeSelector<
|
| 378 |
483 |
Graph,
|
| 379 |
484 |
typename enable_if<typename Graph::FindEdgeTag, void>::type>
|
| 380 |
485 |
{
|
| 381 |
486 |
typedef typename Graph::Node Node;
|
| 382 |
487 |
typedef typename Graph::Edge Edge;
|
| 383 |
488 |
static Edge find(const Graph &g, Node u, Node v, Edge prev) {
|
| 384 |
489 |
return g.findEdge(u, v, prev);
|
| 385 |
490 |
}
|
| 386 |
491 |
};
|
| 387 |
492 |
}
|
| 388 |
493 |
|
| 389 |
494 |
/// \brief Finds an edge between two nodes of a graph.
|
| 390 |
495 |
///
|
| 391 |
496 |
/// Finds an edge from node \c u to node \c v in graph \c g.
|
| 392 |
497 |
/// If the node \c u and node \c v is equal then each loop edge
|
| 393 |
498 |
/// will be enumerated once.
|
| 394 |
499 |
///
|
| 395 |
500 |
/// If \c prev is \ref INVALID (this is the default value), then
|
| 396 |
501 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for
|
| 397 |
502 |
/// the next arc from \c u to \c v after \c prev.
|
| 398 |
503 |
/// \return The found arc or \ref INVALID if there is no such an arc.
|
| 399 |
504 |
///
|
| 400 |
505 |
/// Thus you can iterate through each arc from \c u to \c v as it follows.
|
| 401 |
506 |
///\code
|
| 402 |
507 |
/// for(Edge e = findEdge(g,u,v); e != INVALID;
|
| 403 |
508 |
/// e = findEdge(g,u,v,e)) {
|
| 404 |
509 |
/// ...
|
| 405 |
510 |
/// }
|
| 406 |
511 |
///\endcode
|
| 407 |
512 |
///
|
| 408 |
513 |
///\sa ConArcIt
|
| 409 |
514 |
|
| 410 |
515 |
template <typename Graph>
|
| 411 |
516 |
inline typename Graph::Edge
|
| 412 |
517 |
findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v,
|
| 413 |
518 |
typename Graph::Edge p = INVALID) {
|
| 414 |
519 |
return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, p);
|
| 415 |
520 |
}
|
| 416 |
521 |
|
| 417 |
522 |
/// \brief Iterator for iterating on edges connected the same nodes.
|
| 418 |
523 |
///
|
| 419 |
524 |
/// Iterator for iterating on edges connected the same nodes. It is
|
| 420 |
525 |
/// higher level interface for the findEdge() function. You can
|
| 421 |
526 |
/// use it the following way:
|
| 422 |
527 |
///\code
|
| 423 |
528 |
/// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) {
|
| 424 |
529 |
/// ...
|
| 425 |
530 |
/// }
|
| 426 |
531 |
///\endcode
|
| 427 |
532 |
///
|
| 428 |
533 |
///\sa findEdge()
|
| 429 |
534 |
///
|
| 430 |
535 |
/// \author Balazs Dezso
|
| 431 |
536 |
template <typename _Graph>
|
| 432 |
537 |
class ConEdgeIt : public _Graph::Edge {
|
| 433 |
538 |
public:
|
| 434 |
539 |
|
| 435 |
540 |
typedef _Graph Graph;
|
| 436 |
541 |
typedef typename Graph::Edge Parent;
|
| 437 |
542 |
|
| 438 |
543 |
typedef typename Graph::Edge Edge;
|
| 439 |
544 |
typedef typename Graph::Node Node;
|
| 440 |
545 |
|
| 441 |
546 |
/// \brief Constructor.
|
| 442 |
547 |
///
|
| 443 |
548 |
/// Construct a new ConEdgeIt iterating on the edges which
|
| 444 |
549 |
/// connects the \c u and \c v node.
|
| 445 |
550 |
ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g) {
|
| 446 |
551 |
Parent::operator=(findEdge(_graph, u, v));
|
| 447 |
552 |
}
|
| 448 |
553 |
|
| 449 |
554 |
/// \brief Constructor.
|
| 450 |
555 |
///
|
| 451 |
556 |
/// Construct a new ConEdgeIt which continues the iterating from
|
| 452 |
557 |
/// the \c e edge.
|
| 453 |
558 |
ConEdgeIt(const Graph& g, Edge e) : Parent(e), _graph(g) {}
|
| 454 |
559 |
|
| 455 |
560 |
/// \brief Increment operator.
|
| 456 |
561 |
///
|
| 457 |
562 |
/// It increments the iterator and gives back the next edge.
|
| 458 |
563 |
ConEdgeIt& operator++() {
|
| 459 |
564 |
Parent::operator=(findEdge(_graph, _graph.source(*this),
|
| 460 |
565 |
_graph.target(*this), *this));
|
| 461 |
566 |
return *this;
|
| 462 |
567 |
}
|
| 463 |
568 |
private:
|
| 464 |
569 |
const Graph& _graph;
|
| 465 |
570 |
};
|
| 466 |
571 |
|
| 467 |
572 |
namespace _graph_utils_bits {
|
| 468 |
573 |
|
| 469 |
574 |
template <typename Digraph, typename Item, typename RefMap>
|
| 470 |
575 |
class MapCopyBase {
|
| 471 |
576 |
public:
|
| 472 |
577 |
virtual void copy(const Digraph& from, const RefMap& refMap) = 0;
|
| 473 |
578 |
|
| 474 |
579 |
virtual ~MapCopyBase() {}
|
| 475 |
580 |
};
|
| 476 |
581 |
|
| 477 |
582 |
template <typename Digraph, typename Item, typename RefMap,
|
| 478 |
583 |
typename ToMap, typename FromMap>
|
| 479 |
584 |
class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 480 |
585 |
public:
|
| 481 |
586 |
|
| 482 |
587 |
MapCopy(ToMap& tmap, const FromMap& map)
|
| 483 |
588 |
: _tmap(tmap), _map(map) {}
|
| 484 |
589 |
|
| 485 |
590 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
| 486 |
591 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
|
| 487 |
592 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
| 488 |
593 |
_tmap.set(refMap[it], _map[it]);
|
| 489 |
594 |
}
|
| 490 |
595 |
}
|
| 491 |
596 |
|
| 492 |
597 |
private:
|
| 493 |
598 |
ToMap& _tmap;
|
| 494 |
599 |
const FromMap& _map;
|
| 495 |
600 |
};
|
| 496 |
601 |
|
| 497 |
602 |
template <typename Digraph, typename Item, typename RefMap, typename It>
|
| 498 |
603 |
class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 499 |
604 |
public:
|
| 500 |
605 |
|
| 501 |
606 |
ItemCopy(It& it, const Item& item) : _it(it), _item(item) {}
|
| 502 |
607 |
|
| 503 |
608 |
virtual void copy(const Digraph&, const RefMap& refMap) {
|
| 504 |
609 |
_it = refMap[_item];
|
| 505 |
610 |
}
|
| 506 |
611 |
|
| 507 |
612 |
private:
|
| 508 |
613 |
It& _it;
|
| 509 |
614 |
Item _item;
|
| 510 |
615 |
};
|
| 511 |
616 |
|
| 512 |
617 |
template <typename Digraph, typename Item, typename RefMap, typename Ref>
|
| 513 |
618 |
class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 514 |
619 |
public:
|
| 515 |
620 |
|
| 516 |
621 |
RefCopy(Ref& map) : _map(map) {}
|
| 517 |
622 |
|
| 518 |
623 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
| 519 |
624 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
|
| 520 |
625 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
| 521 |
626 |
_map.set(it, refMap[it]);
|
| 522 |
627 |
}
|
| 523 |
628 |
}
|
| 524 |
629 |
|
| 525 |
630 |
private:
|
| 526 |
631 |
Ref& _map;
|
| 527 |
632 |
};
|
| 528 |
633 |
|
| 529 |
634 |
template <typename Digraph, typename Item, typename RefMap,
|
| 530 |
635 |
typename CrossRef>
|
| 531 |
636 |
class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 532 |
637 |
public:
|
| 533 |
638 |
|
| 534 |
639 |
CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
|
| 535 |
640 |
|
| 536 |
641 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
| 537 |
642 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt;
|
| 538 |
643 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
| 539 |
644 |
_cmap.set(refMap[it], it);
|
| 540 |
645 |
}
|
| 541 |
646 |
}
|
| 542 |
647 |
|
| 543 |
648 |
private:
|
| 544 |
649 |
CrossRef& _cmap;
|
| 545 |
650 |
};
|
| 546 |
651 |
|
| 547 |
652 |
template <typename Digraph, typename Enable = void>
|
| 548 |
653 |
struct DigraphCopySelector {
|
| 549 |
654 |
template <typename From, typename NodeRefMap, typename ArcRefMap>
|
| 550 |
655 |
static void copy(Digraph &to, const From& from,
|
| 551 |
656 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
|
| 552 |
657 |
for (typename From::NodeIt it(from); it != INVALID; ++it) {
|
| 553 |
658 |
nodeRefMap[it] = to.addNode();
|
| 554 |
659 |
}
|
| 555 |
660 |
for (typename From::ArcIt it(from); it != INVALID; ++it) {
|
| 556 |
661 |
arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
|
| 557 |
662 |
nodeRefMap[from.target(it)]);
|
| 558 |
663 |
}
|
| 559 |
664 |
}
|
| 560 |
665 |
};
|
| 561 |
666 |
|
| 562 |
667 |
template <typename Digraph>
|
| 563 |
668 |
struct DigraphCopySelector<
|
| 564 |
669 |
Digraph,
|
| 565 |
670 |
typename enable_if<typename Digraph::BuildTag, void>::type>
|
| 566 |
671 |
{
|
| 567 |
672 |
template <typename From, typename NodeRefMap, typename ArcRefMap>
|
| 568 |
673 |
static void copy(Digraph &to, const From& from,
|
| 569 |
674 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
|
| 570 |
675 |
to.build(from, nodeRefMap, arcRefMap);
|
| 571 |
676 |
}
|
| 572 |
677 |
};
|
| 573 |
678 |
|
| 574 |
679 |
template <typename Graph, typename Enable = void>
|
| 575 |
680 |
struct GraphCopySelector {
|
| 576 |
681 |
template <typename From, typename NodeRefMap, typename EdgeRefMap>
|
| 577 |
682 |
static void copy(Graph &to, const From& from,
|
| 578 |
683 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
|
| 579 |
684 |
for (typename From::NodeIt it(from); it != INVALID; ++it) {
|
| 580 |
685 |
nodeRefMap[it] = to.addNode();
|
| 581 |
686 |
}
|
| 582 |
687 |
for (typename From::EdgeIt it(from); it != INVALID; ++it) {
|
| 583 |
688 |
edgeRefMap[it] = to.addArc(nodeRefMap[from.source(it)],
|
| 584 |
689 |
nodeRefMap[from.target(it)]);
|
| 585 |
690 |
}
|
| 586 |
691 |
}
|
| 587 |
692 |
};
|
| 588 |
693 |
|
| 589 |
694 |
template <typename Graph>
|
| 590 |
695 |
struct GraphCopySelector<
|
| 591 |
696 |
Graph,
|
| 592 |
697 |
typename enable_if<typename Graph::BuildTag, void>::type>
|
| 593 |
698 |
{
|
| 594 |
699 |
template <typename From, typename NodeRefMap, typename EdgeRefMap>
|
| 595 |
700 |
static void copy(Graph &to, const From& from,
|
| 596 |
701 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
|
| 597 |
702 |
to.build(from, nodeRefMap, edgeRefMap);
|
| 598 |
703 |
}
|
| 599 |
704 |
};
|
| 600 |
705 |
|
| 601 |
706 |
}
|
| 602 |
707 |
|
| 603 |
708 |
/// \brief Class to copy a digraph.
|
| 604 |
709 |
///
|
| 605 |
710 |
/// Class to copy a digraph to another digraph (duplicate a digraph). The
|
| 606 |
711 |
/// simplest way of using it is through the \c copyDigraph() function.
|
| 607 |
712 |
///
|
| 608 |
713 |
/// This class not just make a copy of a graph, but it can create
|
| 609 |
714 |
/// references and cross references between the nodes and arcs of
|
| 610 |
715 |
/// the two graphs, it can copy maps for use with the newly created
|
| 611 |
716 |
/// graph and copy nodes and arcs.
|
| 612 |
717 |
///
|
| 613 |
718 |
/// To make a copy from a graph, first an instance of DigraphCopy
|
| 614 |
719 |
/// should be created, then the data belongs to the graph should
|
| 615 |
720 |
/// assigned to copy. In the end, the \c run() member should be
|
| 616 |
721 |
/// called.
|
| 617 |
722 |
///
|
| 618 |
723 |
/// The next code copies a graph with several data:
|
| 619 |
724 |
///\code
|
| 620 |
725 |
/// DigraphCopy<NewGraph, OrigGraph> dc(new_graph, orig_graph);
|
| 621 |
726 |
/// // create a reference for the nodes
|
| 622 |
727 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
|
| 623 |
728 |
/// dc.nodeRef(nr);
|
| 624 |
729 |
/// // create a cross reference (inverse) for the arcs
|
| 625 |
730 |
/// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph);
|
| 626 |
731 |
/// dc.arcCrossRef(acr);
|
| 627 |
732 |
/// // copy an arc map
|
| 628 |
733 |
/// OrigGraph::ArcMap<double> oamap(orig_graph);
|
| 629 |
734 |
/// NewGraph::ArcMap<double> namap(new_graph);
|
| 630 |
735 |
/// dc.arcMap(namap, oamap);
|
| 631 |
736 |
/// // copy a node
|
| 632 |
737 |
/// OrigGraph::Node on;
|
| 633 |
738 |
/// NewGraph::Node nn;
|
| 634 |
739 |
/// dc.node(nn, on);
|
| 635 |
740 |
/// // Executions of copy
|
| 636 |
741 |
/// dc.run();
|
| 637 |
742 |
///\endcode
|
| 638 |
743 |
template <typename To, typename From>
|
| 639 |
744 |
class DigraphCopy {
|
| 640 |
745 |
private:
|
| 641 |
746 |
|
| 642 |
747 |
typedef typename From::Node Node;
|
| 643 |
748 |
typedef typename From::NodeIt NodeIt;
|
| 644 |
749 |
typedef typename From::Arc Arc;
|
| 645 |
750 |
typedef typename From::ArcIt ArcIt;
|
| 646 |
751 |
|
| 647 |
752 |
typedef typename To::Node TNode;
|
| 648 |
753 |
typedef typename To::Arc TArc;
|
| 649 |
754 |
|
| 650 |
755 |
typedef typename From::template NodeMap<TNode> NodeRefMap;
|
| 651 |
756 |
typedef typename From::template ArcMap<TArc> ArcRefMap;
|
| 652 |
757 |
|
| 653 |
758 |
|
| 654 |
759 |
public:
|
| 655 |
760 |
|
| 656 |
761 |
|
| 657 |
762 |
/// \brief Constructor for the DigraphCopy.
|
| 658 |
763 |
///
|
| 659 |
764 |
/// It copies the content of the \c _from digraph into the
|
| 660 |
765 |
/// \c _to digraph.
|
| 661 |
766 |
DigraphCopy(To& to, const From& from)
|
| 662 |
767 |
: _from(from), _to(to) {}
|
| 663 |
768 |
|
| 664 |
769 |
/// \brief Destructor of the DigraphCopy
|
| 665 |
770 |
///
|
| 666 |
771 |
/// Destructor of the DigraphCopy
|
| 667 |
772 |
~DigraphCopy() {
|
| 668 |
773 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
| 669 |
774 |
delete _node_maps[i];
|
| 670 |
775 |
}
|
| 671 |
776 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
| 672 |
777 |
delete _arc_maps[i];
|
| 673 |
778 |
}
|
| 674 |
779 |
|
| 675 |
780 |
}
|
| 676 |
781 |
|
| 677 |
782 |
/// \brief Copies the node references into the given map.
|
| 678 |
783 |
///
|
| 679 |
784 |
/// Copies the node references into the given map. The parameter
|
| 680 |
785 |
/// should be a map, which key type is the Node type of the source
|
| 681 |
786 |
/// graph, while the value type is the Node type of the
|
| 682 |
787 |
/// destination graph.
|
| 683 |
788 |
template <typename NodeRef>
|
| 684 |
789 |
DigraphCopy& nodeRef(NodeRef& map) {
|
| 685 |
790 |
_node_maps.push_back(new _graph_utils_bits::RefCopy<From, Node,
|
| 686 |
791 |
NodeRefMap, NodeRef>(map));
|
| 687 |
792 |
return *this;
|
| 688 |
793 |
}
|
| 689 |
794 |
|
| 690 |
795 |
/// \brief Copies the node cross references into the given map.
|
| 691 |
796 |
///
|
| 692 |
797 |
/// Copies the node cross references (reverse references) into
|
| 693 |
798 |
/// the given map. The parameter should be a map, which key type
|
| 694 |
799 |
/// is the Node type of the destination graph, while the value type is
|
| 695 |
800 |
/// the Node type of the source graph.
|
| 696 |
801 |
template <typename NodeCrossRef>
|
| 697 |
802 |
DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
|
| 698 |
803 |
_node_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Node,
|
| 699 |
804 |
NodeRefMap, NodeCrossRef>(map));
|
| 700 |
805 |
return *this;
|
| 701 |
806 |
}
|
| 702 |
807 |
|
| 703 |
808 |
/// \brief Make copy of the given map.
|
| 704 |
809 |
///
|
| 705 |
810 |
/// Makes copy of the given map for the newly created digraph.
|
| 706 |
811 |
/// The new map's key type is the destination graph's node type,
|
| 707 |
812 |
/// and the copied map's key type is the source graph's node type.
|
| 708 |
813 |
template <typename ToMap, typename FromMap>
|
| 709 |
814 |
DigraphCopy& nodeMap(ToMap& tmap, const FromMap& map) {
|
| 710 |
815 |
_node_maps.push_back(new _graph_utils_bits::MapCopy<From, Node,
|
| 711 |
816 |
NodeRefMap, ToMap, FromMap>(tmap, map));
|
| 712 |
817 |
return *this;
|
| 713 |
818 |
}
|
| 714 |
819 |
|
| 715 |
820 |
/// \brief Make a copy of the given node.
|
| 716 |
821 |
///
|
| 717 |
822 |
/// Make a copy of the given node.
|
| 718 |
823 |
DigraphCopy& node(TNode& tnode, const Node& snode) {
|
| 719 |
824 |
_node_maps.push_back(new _graph_utils_bits::ItemCopy<From, Node,
|
| 720 |
825 |
NodeRefMap, TNode>(tnode, snode));
|
| 721 |
826 |
return *this;
|
| 722 |
827 |
}
|
| 723 |
828 |
|
| 724 |
829 |
/// \brief Copies the arc references into the given map.
|
| 725 |
830 |
///
|
| 726 |
831 |
/// Copies the arc references into the given map.
|
| 727 |
832 |
template <typename ArcRef>
|
| 728 |
833 |
DigraphCopy& arcRef(ArcRef& map) {
|
| 729 |
834 |
_arc_maps.push_back(new _graph_utils_bits::RefCopy<From, Arc,
|
| 730 |
835 |
ArcRefMap, ArcRef>(map));
|
| 731 |
836 |
return *this;
|
| 732 |
837 |
}
|
| 733 |
838 |
|
| 734 |
839 |
/// \brief Copies the arc cross references into the given map.
|
| 735 |
840 |
///
|
| 736 |
841 |
/// Copies the arc cross references (reverse references) into
|
| 737 |
842 |
/// the given map.
|
| 738 |
843 |
template <typename ArcCrossRef>
|
| 739 |
844 |
DigraphCopy& arcCrossRef(ArcCrossRef& map) {
|
| 740 |
845 |
_arc_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Arc,
|
| 741 |
846 |
ArcRefMap, ArcCrossRef>(map));
|
| 742 |
847 |
return *this;
|
| 743 |
848 |
}
|
| 744 |
849 |
|
| 745 |
850 |
/// \brief Make copy of the given map.
|
| 746 |
851 |
///
|
| 747 |
852 |
/// Makes copy of the given map for the newly created digraph.
|
| 748 |
853 |
/// The new map's key type is the to digraph's arc type,
|
| 749 |
854 |
/// and the copied map's key type is the from digraph's arc
|
| 750 |
855 |
/// type.
|
| 751 |
856 |
template <typename ToMap, typename FromMap>
|
| 752 |
857 |
DigraphCopy& arcMap(ToMap& tmap, const FromMap& map) {
|
| 753 |
858 |
_arc_maps.push_back(new _graph_utils_bits::MapCopy<From, Arc,
|
| 754 |
859 |
ArcRefMap, ToMap, FromMap>(tmap, map));
|
| 755 |
860 |
return *this;
|
| 756 |
861 |
}
|
| 757 |
862 |
|
| 758 |
863 |
/// \brief Make a copy of the given arc.
|
| 759 |
864 |
///
|
| 760 |
865 |
/// Make a copy of the given arc.
|
| 761 |
866 |
DigraphCopy& arc(TArc& tarc, const Arc& sarc) {
|
| 762 |
867 |
_arc_maps.push_back(new _graph_utils_bits::ItemCopy<From, Arc,
|
| 763 |
868 |
ArcRefMap, TArc>(tarc, sarc));
|
| 764 |
869 |
return *this;
|
| 765 |
870 |
}
|
| 766 |
871 |
|
| 767 |
872 |
/// \brief Executes the copies.
|
| 768 |
873 |
///
|
| 769 |
874 |
/// Executes the copies.
|
| 770 |
875 |
void run() {
|
| 771 |
876 |
NodeRefMap nodeRefMap(_from);
|
| 772 |
877 |
ArcRefMap arcRefMap(_from);
|
| 773 |
878 |
_graph_utils_bits::DigraphCopySelector<To>::
|
| 774 |
879 |
copy(_to, _from, nodeRefMap, arcRefMap);
|
| 775 |
880 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
| 776 |
881 |
_node_maps[i]->copy(_from, nodeRefMap);
|
| 777 |
882 |
}
|
| 778 |
883 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
| 779 |
884 |
_arc_maps[i]->copy(_from, arcRefMap);
|
| 780 |
885 |
}
|
| 781 |
886 |
}
|
| 782 |
887 |
|
| 783 |
888 |
protected:
|
| 784 |
889 |
|
| 785 |
890 |
|
| 786 |
891 |
const From& _from;
|
| 787 |
892 |
To& _to;
|
| 788 |
893 |
|
| 789 |
894 |
std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* >
|
| 790 |
895 |
_node_maps;
|
| 791 |
896 |
|
| 792 |
897 |
std::vector<_graph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* >
|
| 793 |
898 |
_arc_maps;
|
| 794 |
899 |
|
| 795 |
900 |
};
|
| 796 |
901 |
|
| 797 |
902 |
/// \brief Copy a digraph to another digraph.
|
| 798 |
903 |
///
|
| 799 |
904 |
/// Copy a digraph to another digraph. The complete usage of the
|
| 800 |
905 |
/// function is detailed in the DigraphCopy class, but a short
|
| 801 |
906 |
/// example shows a basic work:
|
| 802 |
907 |
///\code
|
| 803 |
908 |
/// copyDigraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run();
|
| 804 |
909 |
///\endcode
|
| 805 |
910 |
///
|
| 806 |
911 |
/// After the copy the \c nr map will contain the mapping from the
|
| 807 |
912 |
/// nodes of the \c from digraph to the nodes of the \c to digraph and
|
| 808 |
913 |
/// \c ecr will contain the mapping from the arcs of the \c to digraph
|
| 809 |
914 |
/// to the arcs of the \c from digraph.
|
| 810 |
915 |
///
|
| 811 |
916 |
/// \see DigraphCopy
|
| 812 |
917 |
template <typename To, typename From>
|
| 813 |
918 |
DigraphCopy<To, From> copyDigraph(To& to, const From& from) {
|
| 814 |
919 |
return DigraphCopy<To, From>(to, from);
|
| 815 |
920 |
}
|
| 816 |
921 |
|
| 817 |
922 |
/// \brief Class to copy a graph.
|
| 818 |
923 |
///
|
| 819 |
924 |
/// Class to copy a graph to another graph (duplicate a graph). The
|
| 820 |
925 |
/// simplest way of using it is through the \c copyGraph() function.
|
| 821 |
926 |
///
|
| 822 |
927 |
/// This class not just make a copy of a graph, but it can create
|
| 823 |
928 |
/// references and cross references between the nodes, edges and arcs of
|
| 824 |
929 |
/// the two graphs, it can copy maps for use with the newly created
|
| 825 |
930 |
/// graph and copy nodes, edges and arcs.
|
| 826 |
931 |
///
|
| 827 |
932 |
/// To make a copy from a graph, first an instance of GraphCopy
|
| 828 |
933 |
/// should be created, then the data belongs to the graph should
|
| 829 |
934 |
/// assigned to copy. In the end, the \c run() member should be
|
| 830 |
935 |
/// called.
|
| 831 |
936 |
///
|
| 832 |
937 |
/// The next code copies a graph with several data:
|
| 833 |
938 |
///\code
|
| 834 |
939 |
/// GraphCopy<NewGraph, OrigGraph> dc(new_graph, orig_graph);
|
| 835 |
940 |
/// // create a reference for the nodes
|
| 836 |
941 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph);
|
| 837 |
942 |
/// dc.nodeRef(nr);
|
| ... |
... |
@@ -1291,1425 +1396,1425 @@
|
| 1291 |
1396 |
/// It gives back the value associated with the key.
|
| 1292 |
1397 |
typename MapTraits<Map>::ConstReturnValue
|
| 1293 |
1398 |
operator[](const Key& key) const {
|
| 1294 |
1399 |
return Map::operator[](key);
|
| 1295 |
1400 |
}
|
| 1296 |
1401 |
|
| 1297 |
1402 |
/// \brief Gives back the item by its value.
|
| 1298 |
1403 |
///
|
| 1299 |
1404 |
/// Gives back the item by its value.
|
| 1300 |
1405 |
Key operator()(const Value& key) const {
|
| 1301 |
1406 |
typename Container::const_iterator it = _inv_map.find(key);
|
| 1302 |
1407 |
return it != _inv_map.end() ? it->second : INVALID;
|
| 1303 |
1408 |
}
|
| 1304 |
1409 |
|
| 1305 |
1410 |
protected:
|
| 1306 |
1411 |
|
| 1307 |
1412 |
/// \brief Erase the key from the map.
|
| 1308 |
1413 |
///
|
| 1309 |
1414 |
/// Erase the key to the map. It is called by the
|
| 1310 |
1415 |
/// \c AlterationNotifier.
|
| 1311 |
1416 |
virtual void erase(const Key& key) {
|
| 1312 |
1417 |
Value val = Map::operator[](key);
|
| 1313 |
1418 |
typename Container::iterator it = _inv_map.find(val);
|
| 1314 |
1419 |
if (it != _inv_map.end() && it->second == key) {
|
| 1315 |
1420 |
_inv_map.erase(it);
|
| 1316 |
1421 |
}
|
| 1317 |
1422 |
Map::erase(key);
|
| 1318 |
1423 |
}
|
| 1319 |
1424 |
|
| 1320 |
1425 |
/// \brief Erase more keys from the map.
|
| 1321 |
1426 |
///
|
| 1322 |
1427 |
/// Erase more keys from the map. It is called by the
|
| 1323 |
1428 |
/// \c AlterationNotifier.
|
| 1324 |
1429 |
virtual void erase(const std::vector<Key>& keys) {
|
| 1325 |
1430 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 1326 |
1431 |
Value val = Map::operator[](keys[i]);
|
| 1327 |
1432 |
typename Container::iterator it = _inv_map.find(val);
|
| 1328 |
1433 |
if (it != _inv_map.end() && it->second == keys[i]) {
|
| 1329 |
1434 |
_inv_map.erase(it);
|
| 1330 |
1435 |
}
|
| 1331 |
1436 |
}
|
| 1332 |
1437 |
Map::erase(keys);
|
| 1333 |
1438 |
}
|
| 1334 |
1439 |
|
| 1335 |
1440 |
/// \brief Clear the keys from the map and inverse map.
|
| 1336 |
1441 |
///
|
| 1337 |
1442 |
/// Clear the keys from the map and inverse map. It is called by the
|
| 1338 |
1443 |
/// \c AlterationNotifier.
|
| 1339 |
1444 |
virtual void clear() {
|
| 1340 |
1445 |
_inv_map.clear();
|
| 1341 |
1446 |
Map::clear();
|
| 1342 |
1447 |
}
|
| 1343 |
1448 |
|
| 1344 |
1449 |
public:
|
| 1345 |
1450 |
|
| 1346 |
1451 |
/// \brief The inverse map type.
|
| 1347 |
1452 |
///
|
| 1348 |
1453 |
/// The inverse of this map. The subscript operator of the map
|
| 1349 |
1454 |
/// gives back always the item what was last assigned to the value.
|
| 1350 |
1455 |
class InverseMap {
|
| 1351 |
1456 |
public:
|
| 1352 |
1457 |
/// \brief Constructor of the InverseMap.
|
| 1353 |
1458 |
///
|
| 1354 |
1459 |
/// Constructor of the InverseMap.
|
| 1355 |
1460 |
explicit InverseMap(const InvertableMap& inverted)
|
| 1356 |
1461 |
: _inverted(inverted) {}
|
| 1357 |
1462 |
|
| 1358 |
1463 |
/// The value type of the InverseMap.
|
| 1359 |
1464 |
typedef typename InvertableMap::Key Value;
|
| 1360 |
1465 |
/// The key type of the InverseMap.
|
| 1361 |
1466 |
typedef typename InvertableMap::Value Key;
|
| 1362 |
1467 |
|
| 1363 |
1468 |
/// \brief Subscript operator.
|
| 1364 |
1469 |
///
|
| 1365 |
1470 |
/// Subscript operator. It gives back always the item
|
| 1366 |
1471 |
/// what was last assigned to the value.
|
| 1367 |
1472 |
Value operator[](const Key& key) const {
|
| 1368 |
1473 |
return _inverted(key);
|
| 1369 |
1474 |
}
|
| 1370 |
1475 |
|
| 1371 |
1476 |
private:
|
| 1372 |
1477 |
const InvertableMap& _inverted;
|
| 1373 |
1478 |
};
|
| 1374 |
1479 |
|
| 1375 |
1480 |
/// \brief It gives back the just readable inverse map.
|
| 1376 |
1481 |
///
|
| 1377 |
1482 |
/// It gives back the just readable inverse map.
|
| 1378 |
1483 |
InverseMap inverse() const {
|
| 1379 |
1484 |
return InverseMap(*this);
|
| 1380 |
1485 |
}
|
| 1381 |
1486 |
|
| 1382 |
1487 |
|
| 1383 |
1488 |
|
| 1384 |
1489 |
};
|
| 1385 |
1490 |
|
| 1386 |
1491 |
/// \brief Provides a mutable, continuous and unique descriptor for each
|
| 1387 |
1492 |
/// item in the graph.
|
| 1388 |
1493 |
///
|
| 1389 |
1494 |
/// The DescriptorMap class provides a unique and continuous (but mutable)
|
| 1390 |
1495 |
/// descriptor (id) for each item of the same type (e.g. node) in the
|
| 1391 |
1496 |
/// graph. This id is <ul><li>\b unique: different items (nodes) get
|
| 1392 |
1497 |
/// different ids <li>\b continuous: the range of the ids is the set of
|
| 1393 |
1498 |
/// integers between 0 and \c n-1, where \c n is the number of the items of
|
| 1394 |
1499 |
/// this type (e.g. nodes) (so the id of a node can change if you delete an
|
| 1395 |
1500 |
/// other node, i.e. this id is mutable). </ul> This map can be inverted
|
| 1396 |
1501 |
/// with its member class \c InverseMap, or with the \c operator() member.
|
| 1397 |
1502 |
///
|
| 1398 |
1503 |
/// \param _Graph The graph class the \c DescriptorMap belongs to.
|
| 1399 |
1504 |
/// \param _Item The Item is the Key of the Map. It may be Node, Arc or
|
| 1400 |
1505 |
/// Edge.
|
| 1401 |
1506 |
template <typename _Graph, typename _Item>
|
| 1402 |
1507 |
class DescriptorMap : protected DefaultMap<_Graph, _Item, int> {
|
| 1403 |
1508 |
|
| 1404 |
1509 |
typedef _Item Item;
|
| 1405 |
1510 |
typedef DefaultMap<_Graph, _Item, int> Map;
|
| 1406 |
1511 |
|
| 1407 |
1512 |
public:
|
| 1408 |
1513 |
/// The graph class of DescriptorMap.
|
| 1409 |
1514 |
typedef _Graph Graph;
|
| 1410 |
1515 |
|
| 1411 |
1516 |
/// The key type of DescriptorMap (Node, Arc, Edge).
|
| 1412 |
1517 |
typedef typename Map::Key Key;
|
| 1413 |
1518 |
/// The value type of DescriptorMap.
|
| 1414 |
1519 |
typedef typename Map::Value Value;
|
| 1415 |
1520 |
|
| 1416 |
1521 |
/// \brief Constructor.
|
| 1417 |
1522 |
///
|
| 1418 |
1523 |
/// Constructor for descriptor map.
|
| 1419 |
1524 |
explicit DescriptorMap(const Graph& _graph) : Map(_graph) {
|
| 1420 |
1525 |
Item it;
|
| 1421 |
1526 |
const typename Map::Notifier* nf = Map::notifier();
|
| 1422 |
1527 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 1423 |
1528 |
Map::set(it, _inv_map.size());
|
| 1424 |
1529 |
_inv_map.push_back(it);
|
| 1425 |
1530 |
}
|
| 1426 |
1531 |
}
|
| 1427 |
1532 |
|
| 1428 |
1533 |
protected:
|
| 1429 |
1534 |
|
| 1430 |
1535 |
/// \brief Add a new key to the map.
|
| 1431 |
1536 |
///
|
| 1432 |
1537 |
/// Add a new key to the map. It is called by the
|
| 1433 |
1538 |
/// \c AlterationNotifier.
|
| 1434 |
1539 |
virtual void add(const Item& item) {
|
| 1435 |
1540 |
Map::add(item);
|
| 1436 |
1541 |
Map::set(item, _inv_map.size());
|
| 1437 |
1542 |
_inv_map.push_back(item);
|
| 1438 |
1543 |
}
|
| 1439 |
1544 |
|
| 1440 |
1545 |
/// \brief Add more new keys to the map.
|
| 1441 |
1546 |
///
|
| 1442 |
1547 |
/// Add more new keys to the map. It is called by the
|
| 1443 |
1548 |
/// \c AlterationNotifier.
|
| 1444 |
1549 |
virtual void add(const std::vector<Item>& items) {
|
| 1445 |
1550 |
Map::add(items);
|
| 1446 |
1551 |
for (int i = 0; i < int(items.size()); ++i) {
|
| 1447 |
1552 |
Map::set(items[i], _inv_map.size());
|
| 1448 |
1553 |
_inv_map.push_back(items[i]);
|
| 1449 |
1554 |
}
|
| 1450 |
1555 |
}
|
| 1451 |
1556 |
|
| 1452 |
1557 |
/// \brief Erase the key from the map.
|
| 1453 |
1558 |
///
|
| 1454 |
1559 |
/// Erase the key from the map. It is called by the
|
| 1455 |
1560 |
/// \c AlterationNotifier.
|
| 1456 |
1561 |
virtual void erase(const Item& item) {
|
| 1457 |
1562 |
Map::set(_inv_map.back(), Map::operator[](item));
|
| 1458 |
1563 |
_inv_map[Map::operator[](item)] = _inv_map.back();
|
| 1459 |
1564 |
_inv_map.pop_back();
|
| 1460 |
1565 |
Map::erase(item);
|
| 1461 |
1566 |
}
|
| 1462 |
1567 |
|
| 1463 |
1568 |
/// \brief Erase more keys from the map.
|
| 1464 |
1569 |
///
|
| 1465 |
1570 |
/// Erase more keys from the map. It is called by the
|
| 1466 |
1571 |
/// \c AlterationNotifier.
|
| 1467 |
1572 |
virtual void erase(const std::vector<Item>& items) {
|
| 1468 |
1573 |
for (int i = 0; i < int(items.size()); ++i) {
|
| 1469 |
1574 |
Map::set(_inv_map.back(), Map::operator[](items[i]));
|
| 1470 |
1575 |
_inv_map[Map::operator[](items[i])] = _inv_map.back();
|
| 1471 |
1576 |
_inv_map.pop_back();
|
| 1472 |
1577 |
}
|
| 1473 |
1578 |
Map::erase(items);
|
| 1474 |
1579 |
}
|
| 1475 |
1580 |
|
| 1476 |
1581 |
/// \brief Build the unique map.
|
| 1477 |
1582 |
///
|
| 1478 |
1583 |
/// Build the unique map. It is called by the
|
| 1479 |
1584 |
/// \c AlterationNotifier.
|
| 1480 |
1585 |
virtual void build() {
|
| 1481 |
1586 |
Map::build();
|
| 1482 |
1587 |
Item it;
|
| 1483 |
1588 |
const typename Map::Notifier* nf = Map::notifier();
|
| 1484 |
1589 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 1485 |
1590 |
Map::set(it, _inv_map.size());
|
| 1486 |
1591 |
_inv_map.push_back(it);
|
| 1487 |
1592 |
}
|
| 1488 |
1593 |
}
|
| 1489 |
1594 |
|
| 1490 |
1595 |
/// \brief Clear the keys from the map.
|
| 1491 |
1596 |
///
|
| 1492 |
1597 |
/// Clear the keys from the map. It is called by the
|
| 1493 |
1598 |
/// \c AlterationNotifier.
|
| 1494 |
1599 |
virtual void clear() {
|
| 1495 |
1600 |
_inv_map.clear();
|
| 1496 |
1601 |
Map::clear();
|
| 1497 |
1602 |
}
|
| 1498 |
1603 |
|
| 1499 |
1604 |
public:
|
| 1500 |
1605 |
|
| 1501 |
1606 |
/// \brief Returns the maximal value plus one.
|
| 1502 |
1607 |
///
|
| 1503 |
1608 |
/// Returns the maximal value plus one in the map.
|
| 1504 |
1609 |
unsigned int size() const {
|
| 1505 |
1610 |
return _inv_map.size();
|
| 1506 |
1611 |
}
|
| 1507 |
1612 |
|
| 1508 |
1613 |
/// \brief Swaps the position of the two items in the map.
|
| 1509 |
1614 |
///
|
| 1510 |
1615 |
/// Swaps the position of the two items in the map.
|
| 1511 |
1616 |
void swap(const Item& p, const Item& q) {
|
| 1512 |
1617 |
int pi = Map::operator[](p);
|
| 1513 |
1618 |
int qi = Map::operator[](q);
|
| 1514 |
1619 |
Map::set(p, qi);
|
| 1515 |
1620 |
_inv_map[qi] = p;
|
| 1516 |
1621 |
Map::set(q, pi);
|
| 1517 |
1622 |
_inv_map[pi] = q;
|
| 1518 |
1623 |
}
|
| 1519 |
1624 |
|
| 1520 |
1625 |
/// \brief Gives back the \e descriptor of the item.
|
| 1521 |
1626 |
///
|
| 1522 |
1627 |
/// Gives back the mutable and unique \e descriptor of the map.
|
| 1523 |
1628 |
int operator[](const Item& item) const {
|
| 1524 |
1629 |
return Map::operator[](item);
|
| 1525 |
1630 |
}
|
| 1526 |
1631 |
|
| 1527 |
1632 |
/// \brief Gives back the item by its descriptor.
|
| 1528 |
1633 |
///
|
| 1529 |
1634 |
/// Gives back th item by its descriptor.
|
| 1530 |
1635 |
Item operator()(int id) const {
|
| 1531 |
1636 |
return _inv_map[id];
|
| 1532 |
1637 |
}
|
| 1533 |
1638 |
|
| 1534 |
1639 |
private:
|
| 1535 |
1640 |
|
| 1536 |
1641 |
typedef std::vector<Item> Container;
|
| 1537 |
1642 |
Container _inv_map;
|
| 1538 |
1643 |
|
| 1539 |
1644 |
public:
|
| 1540 |
1645 |
/// \brief The inverse map type of DescriptorMap.
|
| 1541 |
1646 |
///
|
| 1542 |
1647 |
/// The inverse map type of DescriptorMap.
|
| 1543 |
1648 |
class InverseMap {
|
| 1544 |
1649 |
public:
|
| 1545 |
1650 |
/// \brief Constructor of the InverseMap.
|
| 1546 |
1651 |
///
|
| 1547 |
1652 |
/// Constructor of the InverseMap.
|
| 1548 |
1653 |
explicit InverseMap(const DescriptorMap& inverted)
|
| 1549 |
1654 |
: _inverted(inverted) {}
|
| 1550 |
1655 |
|
| 1551 |
1656 |
|
| 1552 |
1657 |
/// The value type of the InverseMap.
|
| 1553 |
1658 |
typedef typename DescriptorMap::Key Value;
|
| 1554 |
1659 |
/// The key type of the InverseMap.
|
| 1555 |
1660 |
typedef typename DescriptorMap::Value Key;
|
| 1556 |
1661 |
|
| 1557 |
1662 |
/// \brief Subscript operator.
|
| 1558 |
1663 |
///
|
| 1559 |
1664 |
/// Subscript operator. It gives back the item
|
| 1560 |
1665 |
/// that the descriptor belongs to currently.
|
| 1561 |
1666 |
Value operator[](const Key& key) const {
|
| 1562 |
1667 |
return _inverted(key);
|
| 1563 |
1668 |
}
|
| 1564 |
1669 |
|
| 1565 |
1670 |
/// \brief Size of the map.
|
| 1566 |
1671 |
///
|
| 1567 |
1672 |
/// Returns the size of the map.
|
| 1568 |
1673 |
unsigned int size() const {
|
| 1569 |
1674 |
return _inverted.size();
|
| 1570 |
1675 |
}
|
| 1571 |
1676 |
|
| 1572 |
1677 |
private:
|
| 1573 |
1678 |
const DescriptorMap& _inverted;
|
| 1574 |
1679 |
};
|
| 1575 |
1680 |
|
| 1576 |
1681 |
/// \brief Gives back the inverse of the map.
|
| 1577 |
1682 |
///
|
| 1578 |
1683 |
/// Gives back the inverse of the map.
|
| 1579 |
1684 |
const InverseMap inverse() const {
|
| 1580 |
1685 |
return InverseMap(*this);
|
| 1581 |
1686 |
}
|
| 1582 |
1687 |
};
|
| 1583 |
1688 |
|
| 1584 |
1689 |
/// \brief Returns the source of the given arc.
|
| 1585 |
1690 |
///
|
| 1586 |
1691 |
/// The SourceMap gives back the source Node of the given arc.
|
| 1587 |
1692 |
/// \see TargetMap
|
| 1588 |
1693 |
/// \author Balazs Dezso
|
| 1589 |
1694 |
template <typename Digraph>
|
| 1590 |
1695 |
class SourceMap {
|
| 1591 |
1696 |
public:
|
| 1592 |
1697 |
|
| 1593 |
1698 |
typedef typename Digraph::Node Value;
|
| 1594 |
1699 |
typedef typename Digraph::Arc Key;
|
| 1595 |
1700 |
|
| 1596 |
1701 |
/// \brief Constructor
|
| 1597 |
1702 |
///
|
| 1598 |
1703 |
/// Constructor
|
| 1599 |
1704 |
/// \param _digraph The digraph that the map belongs to.
|
| 1600 |
1705 |
explicit SourceMap(const Digraph& digraph) : _digraph(digraph) {}
|
| 1601 |
1706 |
|
| 1602 |
1707 |
/// \brief The subscript operator.
|
| 1603 |
1708 |
///
|
| 1604 |
1709 |
/// The subscript operator.
|
| 1605 |
1710 |
/// \param arc The arc
|
| 1606 |
1711 |
/// \return The source of the arc
|
| 1607 |
1712 |
Value operator[](const Key& arc) const {
|
| 1608 |
1713 |
return _digraph.source(arc);
|
| 1609 |
1714 |
}
|
| 1610 |
1715 |
|
| 1611 |
1716 |
private:
|
| 1612 |
1717 |
const Digraph& _digraph;
|
| 1613 |
1718 |
};
|
| 1614 |
1719 |
|
| 1615 |
1720 |
/// \brief Returns a \ref SourceMap class.
|
| 1616 |
1721 |
///
|
| 1617 |
1722 |
/// This function just returns an \ref SourceMap class.
|
| 1618 |
1723 |
/// \relates SourceMap
|
| 1619 |
1724 |
template <typename Digraph>
|
| 1620 |
1725 |
inline SourceMap<Digraph> sourceMap(const Digraph& digraph) {
|
| 1621 |
1726 |
return SourceMap<Digraph>(digraph);
|
| 1622 |
1727 |
}
|
| 1623 |
1728 |
|
| 1624 |
1729 |
/// \brief Returns the target of the given arc.
|
| 1625 |
1730 |
///
|
| 1626 |
1731 |
/// The TargetMap gives back the target Node of the given arc.
|
| 1627 |
1732 |
/// \see SourceMap
|
| 1628 |
1733 |
/// \author Balazs Dezso
|
| 1629 |
1734 |
template <typename Digraph>
|
| 1630 |
1735 |
class TargetMap {
|
| 1631 |
1736 |
public:
|
| 1632 |
1737 |
|
| 1633 |
1738 |
typedef typename Digraph::Node Value;
|
| 1634 |
1739 |
typedef typename Digraph::Arc Key;
|
| 1635 |
1740 |
|
| 1636 |
1741 |
/// \brief Constructor
|
| 1637 |
1742 |
///
|
| 1638 |
1743 |
/// Constructor
|
| 1639 |
1744 |
/// \param _digraph The digraph that the map belongs to.
|
| 1640 |
1745 |
explicit TargetMap(const Digraph& digraph) : _digraph(digraph) {}
|
| 1641 |
1746 |
|
| 1642 |
1747 |
/// \brief The subscript operator.
|
| 1643 |
1748 |
///
|
| 1644 |
1749 |
/// The subscript operator.
|
| 1645 |
1750 |
/// \param e The arc
|
| 1646 |
1751 |
/// \return The target of the arc
|
| 1647 |
1752 |
Value operator[](const Key& e) const {
|
| 1648 |
1753 |
return _digraph.target(e);
|
| 1649 |
1754 |
}
|
| 1650 |
1755 |
|
| 1651 |
1756 |
private:
|
| 1652 |
1757 |
const Digraph& _digraph;
|
| 1653 |
1758 |
};
|
| 1654 |
1759 |
|
| 1655 |
1760 |
/// \brief Returns a \ref TargetMap class.
|
| 1656 |
1761 |
///
|
| 1657 |
1762 |
/// This function just returns a \ref TargetMap class.
|
| 1658 |
1763 |
/// \relates TargetMap
|
| 1659 |
1764 |
template <typename Digraph>
|
| 1660 |
1765 |
inline TargetMap<Digraph> targetMap(const Digraph& digraph) {
|
| 1661 |
1766 |
return TargetMap<Digraph>(digraph);
|
| 1662 |
1767 |
}
|
| 1663 |
1768 |
|
| 1664 |
1769 |
/// \brief Returns the "forward" directed arc view of an edge.
|
| 1665 |
1770 |
///
|
| 1666 |
1771 |
/// Returns the "forward" directed arc view of an edge.
|
| 1667 |
1772 |
/// \see BackwardMap
|
| 1668 |
1773 |
/// \author Balazs Dezso
|
| 1669 |
1774 |
template <typename Graph>
|
| 1670 |
1775 |
class ForwardMap {
|
| 1671 |
1776 |
public:
|
| 1672 |
1777 |
|
| 1673 |
1778 |
typedef typename Graph::Arc Value;
|
| 1674 |
1779 |
typedef typename Graph::Edge Key;
|
| 1675 |
1780 |
|
| 1676 |
1781 |
/// \brief Constructor
|
| 1677 |
1782 |
///
|
| 1678 |
1783 |
/// Constructor
|
| 1679 |
1784 |
/// \param _graph The graph that the map belongs to.
|
| 1680 |
1785 |
explicit ForwardMap(const Graph& graph) : _graph(graph) {}
|
| 1681 |
1786 |
|
| 1682 |
1787 |
/// \brief The subscript operator.
|
| 1683 |
1788 |
///
|
| 1684 |
1789 |
/// The subscript operator.
|
| 1685 |
1790 |
/// \param key An edge
|
| 1686 |
1791 |
/// \return The "forward" directed arc view of edge
|
| 1687 |
1792 |
Value operator[](const Key& key) const {
|
| 1688 |
1793 |
return _graph.direct(key, true);
|
| 1689 |
1794 |
}
|
| 1690 |
1795 |
|
| 1691 |
1796 |
private:
|
| 1692 |
1797 |
const Graph& _graph;
|
| 1693 |
1798 |
};
|
| 1694 |
1799 |
|
| 1695 |
1800 |
/// \brief Returns a \ref ForwardMap class.
|
| 1696 |
1801 |
///
|
| 1697 |
1802 |
/// This function just returns an \ref ForwardMap class.
|
| 1698 |
1803 |
/// \relates ForwardMap
|
| 1699 |
1804 |
template <typename Graph>
|
| 1700 |
1805 |
inline ForwardMap<Graph> forwardMap(const Graph& graph) {
|
| 1701 |
1806 |
return ForwardMap<Graph>(graph);
|
| 1702 |
1807 |
}
|
| 1703 |
1808 |
|
| 1704 |
1809 |
/// \brief Returns the "backward" directed arc view of an edge.
|
| 1705 |
1810 |
///
|
| 1706 |
1811 |
/// Returns the "backward" directed arc view of an edge.
|
| 1707 |
1812 |
/// \see ForwardMap
|
| 1708 |
1813 |
/// \author Balazs Dezso
|
| 1709 |
1814 |
template <typename Graph>
|
| 1710 |
1815 |
class BackwardMap {
|
| 1711 |
1816 |
public:
|
| 1712 |
1817 |
|
| 1713 |
1818 |
typedef typename Graph::Arc Value;
|
| 1714 |
1819 |
typedef typename Graph::Edge Key;
|
| 1715 |
1820 |
|
| 1716 |
1821 |
/// \brief Constructor
|
| 1717 |
1822 |
///
|
| 1718 |
1823 |
/// Constructor
|
| 1719 |
1824 |
/// \param _graph The graph that the map belongs to.
|
| 1720 |
1825 |
explicit BackwardMap(const Graph& graph) : _graph(graph) {}
|
| 1721 |
1826 |
|
| 1722 |
1827 |
/// \brief The subscript operator.
|
| 1723 |
1828 |
///
|
| 1724 |
1829 |
/// The subscript operator.
|
| 1725 |
1830 |
/// \param key An edge
|
| 1726 |
1831 |
/// \return The "backward" directed arc view of edge
|
| 1727 |
1832 |
Value operator[](const Key& key) const {
|
| 1728 |
1833 |
return _graph.direct(key, false);
|
| 1729 |
1834 |
}
|
| 1730 |
1835 |
|
| 1731 |
1836 |
private:
|
| 1732 |
1837 |
const Graph& _graph;
|
| 1733 |
1838 |
};
|
| 1734 |
1839 |
|
| 1735 |
1840 |
/// \brief Returns a \ref BackwardMap class
|
| 1736 |
1841 |
|
| 1737 |
1842 |
/// This function just returns a \ref BackwardMap class.
|
| 1738 |
1843 |
/// \relates BackwardMap
|
| 1739 |
1844 |
template <typename Graph>
|
| 1740 |
1845 |
inline BackwardMap<Graph> backwardMap(const Graph& graph) {
|
| 1741 |
1846 |
return BackwardMap<Graph>(graph);
|
| 1742 |
1847 |
}
|
| 1743 |
1848 |
|
| 1744 |
1849 |
/// \brief Potential difference map
|
| 1745 |
1850 |
///
|
| 1746 |
1851 |
/// If there is an potential map on the nodes then we
|
| 1747 |
1852 |
/// can get an arc map as we get the substraction of the
|
| 1748 |
1853 |
/// values of the target and source.
|
| 1749 |
1854 |
template <typename Digraph, typename NodeMap>
|
| 1750 |
1855 |
class PotentialDifferenceMap {
|
| 1751 |
1856 |
public:
|
| 1752 |
1857 |
typedef typename Digraph::Arc Key;
|
| 1753 |
1858 |
typedef typename NodeMap::Value Value;
|
| 1754 |
1859 |
|
| 1755 |
1860 |
/// \brief Constructor
|
| 1756 |
1861 |
///
|
| 1757 |
1862 |
/// Contructor of the map
|
| 1758 |
1863 |
explicit PotentialDifferenceMap(const Digraph& digraph,
|
| 1759 |
1864 |
const NodeMap& potential)
|
| 1760 |
1865 |
: _digraph(digraph), _potential(potential) {}
|
| 1761 |
1866 |
|
| 1762 |
1867 |
/// \brief Const subscription operator
|
| 1763 |
1868 |
///
|
| 1764 |
1869 |
/// Const subscription operator
|
| 1765 |
1870 |
Value operator[](const Key& arc) const {
|
| 1766 |
1871 |
return _potential[_digraph.target(arc)] -
|
| 1767 |
1872 |
_potential[_digraph.source(arc)];
|
| 1768 |
1873 |
}
|
| 1769 |
1874 |
|
| 1770 |
1875 |
private:
|
| 1771 |
1876 |
const Digraph& _digraph;
|
| 1772 |
1877 |
const NodeMap& _potential;
|
| 1773 |
1878 |
};
|
| 1774 |
1879 |
|
| 1775 |
1880 |
/// \brief Returns a PotentialDifferenceMap.
|
| 1776 |
1881 |
///
|
| 1777 |
1882 |
/// This function just returns a PotentialDifferenceMap.
|
| 1778 |
1883 |
/// \relates PotentialDifferenceMap
|
| 1779 |
1884 |
template <typename Digraph, typename NodeMap>
|
| 1780 |
1885 |
PotentialDifferenceMap<Digraph, NodeMap>
|
| 1781 |
1886 |
potentialDifferenceMap(const Digraph& digraph, const NodeMap& potential) {
|
| 1782 |
1887 |
return PotentialDifferenceMap<Digraph, NodeMap>(digraph, potential);
|
| 1783 |
1888 |
}
|
| 1784 |
1889 |
|
| 1785 |
1890 |
/// \brief Map of the node in-degrees.
|
| 1786 |
1891 |
///
|
| 1787 |
1892 |
/// This map returns the in-degree of a node. Once it is constructed,
|
| 1788 |
1893 |
/// the degrees are stored in a standard NodeMap, so each query is done
|
| 1789 |
1894 |
/// in constant time. On the other hand, the values are updated automatically
|
| 1790 |
1895 |
/// whenever the digraph changes.
|
| 1791 |
1896 |
///
|
| 1792 |
1897 |
/// \warning Besides addNode() and addArc(), a digraph structure may provide
|
| 1793 |
1898 |
/// alternative ways to modify the digraph. The correct behavior of InDegMap
|
| 1794 |
1899 |
/// is not guarantied if these additional features are used. For example
|
| 1795 |
1900 |
/// the functions \ref ListDigraph::changeSource() "changeSource()",
|
| 1796 |
1901 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and
|
| 1797 |
1902 |
/// \ref ListDigraph::reverseArc() "reverseArc()"
|
| 1798 |
1903 |
/// of \ref ListDigraph will \e not update the degree values correctly.
|
| 1799 |
1904 |
///
|
| 1800 |
1905 |
/// \sa OutDegMap
|
| 1801 |
1906 |
|
| 1802 |
1907 |
template <typename _Digraph>
|
| 1803 |
1908 |
class InDegMap
|
| 1804 |
1909 |
: protected ItemSetTraits<_Digraph, typename _Digraph::Arc>
|
| 1805 |
1910 |
::ItemNotifier::ObserverBase {
|
| 1806 |
1911 |
|
| 1807 |
1912 |
public:
|
| 1808 |
1913 |
|
| 1809 |
1914 |
typedef _Digraph Digraph;
|
| 1810 |
1915 |
typedef int Value;
|
| 1811 |
1916 |
typedef typename Digraph::Node Key;
|
| 1812 |
1917 |
|
| 1813 |
1918 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
|
| 1814 |
1919 |
::ItemNotifier::ObserverBase Parent;
|
| 1815 |
1920 |
|
| 1816 |
1921 |
private:
|
| 1817 |
1922 |
|
| 1818 |
1923 |
class AutoNodeMap : public DefaultMap<Digraph, Key, int> {
|
| 1819 |
1924 |
public:
|
| 1820 |
1925 |
|
| 1821 |
1926 |
typedef DefaultMap<Digraph, Key, int> Parent;
|
| 1822 |
1927 |
|
| 1823 |
1928 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
|
| 1824 |
1929 |
|
| 1825 |
1930 |
virtual void add(const Key& key) {
|
| 1826 |
1931 |
Parent::add(key);
|
| 1827 |
1932 |
Parent::set(key, 0);
|
| 1828 |
1933 |
}
|
| 1829 |
1934 |
|
| 1830 |
1935 |
virtual void add(const std::vector<Key>& keys) {
|
| 1831 |
1936 |
Parent::add(keys);
|
| 1832 |
1937 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 1833 |
1938 |
Parent::set(keys[i], 0);
|
| 1834 |
1939 |
}
|
| 1835 |
1940 |
}
|
| 1836 |
1941 |
|
| 1837 |
1942 |
virtual void build() {
|
| 1838 |
1943 |
Parent::build();
|
| 1839 |
1944 |
Key it;
|
| 1840 |
1945 |
typename Parent::Notifier* nf = Parent::notifier();
|
| 1841 |
1946 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 1842 |
1947 |
Parent::set(it, 0);
|
| 1843 |
1948 |
}
|
| 1844 |
1949 |
}
|
| 1845 |
1950 |
};
|
| 1846 |
1951 |
|
| 1847 |
1952 |
public:
|
| 1848 |
1953 |
|
| 1849 |
1954 |
/// \brief Constructor.
|
| 1850 |
1955 |
///
|
| 1851 |
1956 |
/// Constructor for creating in-degree map.
|
| 1852 |
1957 |
explicit InDegMap(const Digraph& digraph)
|
| 1853 |
1958 |
: _digraph(digraph), _deg(digraph) {
|
| 1854 |
1959 |
Parent::attach(_digraph.notifier(typename Digraph::Arc()));
|
| 1855 |
1960 |
|
| 1856 |
1961 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 1857 |
1962 |
_deg[it] = countInArcs(_digraph, it);
|
| 1858 |
1963 |
}
|
| 1859 |
1964 |
}
|
| 1860 |
1965 |
|
| 1861 |
1966 |
/// Gives back the in-degree of a Node.
|
| 1862 |
1967 |
int operator[](const Key& key) const {
|
| 1863 |
1968 |
return _deg[key];
|
| 1864 |
1969 |
}
|
| 1865 |
1970 |
|
| 1866 |
1971 |
protected:
|
| 1867 |
1972 |
|
| 1868 |
1973 |
typedef typename Digraph::Arc Arc;
|
| 1869 |
1974 |
|
| 1870 |
1975 |
virtual void add(const Arc& arc) {
|
| 1871 |
1976 |
++_deg[_digraph.target(arc)];
|
| 1872 |
1977 |
}
|
| 1873 |
1978 |
|
| 1874 |
1979 |
virtual void add(const std::vector<Arc>& arcs) {
|
| 1875 |
1980 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 1876 |
1981 |
++_deg[_digraph.target(arcs[i])];
|
| 1877 |
1982 |
}
|
| 1878 |
1983 |
}
|
| 1879 |
1984 |
|
| 1880 |
1985 |
virtual void erase(const Arc& arc) {
|
| 1881 |
1986 |
--_deg[_digraph.target(arc)];
|
| 1882 |
1987 |
}
|
| 1883 |
1988 |
|
| 1884 |
1989 |
virtual void erase(const std::vector<Arc>& arcs) {
|
| 1885 |
1990 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 1886 |
1991 |
--_deg[_digraph.target(arcs[i])];
|
| 1887 |
1992 |
}
|
| 1888 |
1993 |
}
|
| 1889 |
1994 |
|
| 1890 |
1995 |
virtual void build() {
|
| 1891 |
1996 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 1892 |
1997 |
_deg[it] = countInArcs(_digraph, it);
|
| 1893 |
1998 |
}
|
| 1894 |
1999 |
}
|
| 1895 |
2000 |
|
| 1896 |
2001 |
virtual void clear() {
|
| 1897 |
2002 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 1898 |
2003 |
_deg[it] = 0;
|
| 1899 |
2004 |
}
|
| 1900 |
2005 |
}
|
| 1901 |
2006 |
private:
|
| 1902 |
2007 |
|
| 1903 |
2008 |
const Digraph& _digraph;
|
| 1904 |
2009 |
AutoNodeMap _deg;
|
| 1905 |
2010 |
};
|
| 1906 |
2011 |
|
| 1907 |
2012 |
/// \brief Map of the node out-degrees.
|
| 1908 |
2013 |
///
|
| 1909 |
2014 |
/// This map returns the out-degree of a node. Once it is constructed,
|
| 1910 |
2015 |
/// the degrees are stored in a standard NodeMap, so each query is done
|
| 1911 |
2016 |
/// in constant time. On the other hand, the values are updated automatically
|
| 1912 |
2017 |
/// whenever the digraph changes.
|
| 1913 |
2018 |
///
|
| 1914 |
2019 |
/// \warning Besides addNode() and addArc(), a digraph structure may provide
|
| 1915 |
2020 |
/// alternative ways to modify the digraph. The correct behavior of OutDegMap
|
| 1916 |
2021 |
/// is not guarantied if these additional features are used. For example
|
| 1917 |
2022 |
/// the functions \ref ListDigraph::changeSource() "changeSource()",
|
| 1918 |
2023 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and
|
| 1919 |
2024 |
/// \ref ListDigraph::reverseArc() "reverseArc()"
|
| 1920 |
2025 |
/// of \ref ListDigraph will \e not update the degree values correctly.
|
| 1921 |
2026 |
///
|
| 1922 |
2027 |
/// \sa InDegMap
|
| 1923 |
2028 |
|
| 1924 |
2029 |
template <typename _Digraph>
|
| 1925 |
2030 |
class OutDegMap
|
| 1926 |
2031 |
: protected ItemSetTraits<_Digraph, typename _Digraph::Arc>
|
| 1927 |
2032 |
::ItemNotifier::ObserverBase {
|
| 1928 |
2033 |
|
| 1929 |
2034 |
public:
|
| 1930 |
2035 |
|
| 1931 |
2036 |
typedef _Digraph Digraph;
|
| 1932 |
2037 |
typedef int Value;
|
| 1933 |
2038 |
typedef typename Digraph::Node Key;
|
| 1934 |
2039 |
|
| 1935 |
2040 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc>
|
| 1936 |
2041 |
::ItemNotifier::ObserverBase Parent;
|
| 1937 |
2042 |
|
| 1938 |
2043 |
private:
|
| 1939 |
2044 |
|
| 1940 |
2045 |
class AutoNodeMap : public DefaultMap<Digraph, Key, int> {
|
| 1941 |
2046 |
public:
|
| 1942 |
2047 |
|
| 1943 |
2048 |
typedef DefaultMap<Digraph, Key, int> Parent;
|
| 1944 |
2049 |
|
| 1945 |
2050 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {}
|
| 1946 |
2051 |
|
| 1947 |
2052 |
virtual void add(const Key& key) {
|
| 1948 |
2053 |
Parent::add(key);
|
| 1949 |
2054 |
Parent::set(key, 0);
|
| 1950 |
2055 |
}
|
| 1951 |
2056 |
virtual void add(const std::vector<Key>& keys) {
|
| 1952 |
2057 |
Parent::add(keys);
|
| 1953 |
2058 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 1954 |
2059 |
Parent::set(keys[i], 0);
|
| 1955 |
2060 |
}
|
| 1956 |
2061 |
}
|
| 1957 |
2062 |
virtual void build() {
|
| 1958 |
2063 |
Parent::build();
|
| 1959 |
2064 |
Key it;
|
| 1960 |
2065 |
typename Parent::Notifier* nf = Parent::notifier();
|
| 1961 |
2066 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 1962 |
2067 |
Parent::set(it, 0);
|
| 1963 |
2068 |
}
|
| 1964 |
2069 |
}
|
| 1965 |
2070 |
};
|
| 1966 |
2071 |
|
| 1967 |
2072 |
public:
|
| 1968 |
2073 |
|
| 1969 |
2074 |
/// \brief Constructor.
|
| 1970 |
2075 |
///
|
| 1971 |
2076 |
/// Constructor for creating out-degree map.
|
| 1972 |
2077 |
explicit OutDegMap(const Digraph& digraph)
|
| 1973 |
2078 |
: _digraph(digraph), _deg(digraph) {
|
| 1974 |
2079 |
Parent::attach(_digraph.notifier(typename Digraph::Arc()));
|
| 1975 |
2080 |
|
| 1976 |
2081 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 1977 |
2082 |
_deg[it] = countOutArcs(_digraph, it);
|
| 1978 |
2083 |
}
|
| 1979 |
2084 |
}
|
| 1980 |
2085 |
|
| 1981 |
2086 |
/// Gives back the out-degree of a Node.
|
| 1982 |
2087 |
int operator[](const Key& key) const {
|
| 1983 |
2088 |
return _deg[key];
|
| 1984 |
2089 |
}
|
| 1985 |
2090 |
|
| 1986 |
2091 |
protected:
|
| 1987 |
2092 |
|
| 1988 |
2093 |
typedef typename Digraph::Arc Arc;
|
| 1989 |
2094 |
|
| 1990 |
2095 |
virtual void add(const Arc& arc) {
|
| 1991 |
2096 |
++_deg[_digraph.source(arc)];
|
| 1992 |
2097 |
}
|
| 1993 |
2098 |
|
| 1994 |
2099 |
virtual void add(const std::vector<Arc>& arcs) {
|
| 1995 |
2100 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 1996 |
2101 |
++_deg[_digraph.source(arcs[i])];
|
| 1997 |
2102 |
}
|
| 1998 |
2103 |
}
|
| 1999 |
2104 |
|
| 2000 |
2105 |
virtual void erase(const Arc& arc) {
|
| 2001 |
2106 |
--_deg[_digraph.source(arc)];
|
| 2002 |
2107 |
}
|
| 2003 |
2108 |
|
| 2004 |
2109 |
virtual void erase(const std::vector<Arc>& arcs) {
|
| 2005 |
2110 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 2006 |
2111 |
--_deg[_digraph.source(arcs[i])];
|
| 2007 |
2112 |
}
|
| 2008 |
2113 |
}
|
| 2009 |
2114 |
|
| 2010 |
2115 |
virtual void build() {
|
| 2011 |
2116 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 2012 |
2117 |
_deg[it] = countOutArcs(_digraph, it);
|
| 2013 |
2118 |
}
|
| 2014 |
2119 |
}
|
| 2015 |
2120 |
|
| 2016 |
2121 |
virtual void clear() {
|
| 2017 |
2122 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
|
| 2018 |
2123 |
_deg[it] = 0;
|
| 2019 |
2124 |
}
|
| 2020 |
2125 |
}
|
| 2021 |
2126 |
private:
|
| 2022 |
2127 |
|
| 2023 |
2128 |
const Digraph& _digraph;
|
| 2024 |
2129 |
AutoNodeMap _deg;
|
| 2025 |
2130 |
};
|
| 2026 |
2131 |
|
| 2027 |
2132 |
|
| 2028 |
2133 |
///Dynamic arc look up between given endpoints.
|
| 2029 |
2134 |
|
| 2030 |
2135 |
///\ingroup gutils
|
| 2031 |
2136 |
///Using this class, you can find an arc in a digraph from a given
|
| 2032 |
2137 |
///source to a given target in amortized time <em>O(log d)</em>,
|
| 2033 |
2138 |
///where <em>d</em> is the out-degree of the source node.
|
| 2034 |
2139 |
///
|
| 2035 |
2140 |
///It is possible to find \e all parallel arcs between two nodes with
|
| 2036 |
2141 |
///the \c findFirst() and \c findNext() members.
|
| 2037 |
2142 |
///
|
| 2038 |
2143 |
///See the \ref ArcLookUp and \ref AllArcLookUp classes if your
|
| 2039 |
2144 |
///digraph is not changed so frequently.
|
| 2040 |
2145 |
///
|
| 2041 |
2146 |
///This class uses a self-adjusting binary search tree, Sleator's
|
| 2042 |
2147 |
///and Tarjan's Splay tree for guarantee the logarithmic amortized
|
| 2043 |
2148 |
///time bound for arc lookups. This class also guarantees the
|
| 2044 |
2149 |
///optimal time bound in a constant factor for any distribution of
|
| 2045 |
2150 |
///queries.
|
| 2046 |
2151 |
///
|
| 2047 |
2152 |
///\param G The type of the underlying digraph.
|
| 2048 |
2153 |
///
|
| 2049 |
2154 |
///\sa ArcLookUp
|
| 2050 |
2155 |
///\sa AllArcLookUp
|
| 2051 |
2156 |
template<class G>
|
| 2052 |
2157 |
class DynArcLookUp
|
| 2053 |
2158 |
: protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase
|
| 2054 |
2159 |
{
|
| 2055 |
2160 |
public:
|
| 2056 |
2161 |
typedef typename ItemSetTraits<G, typename G::Arc>
|
| 2057 |
2162 |
::ItemNotifier::ObserverBase Parent;
|
| 2058 |
2163 |
|
| 2059 |
|
DIGRAPH_TYPEDEFS(typename G);
|
|
2164 |
DIGRAPH_TYPEDEFS(G);
|
| 2060 |
2165 |
typedef G Digraph;
|
| 2061 |
2166 |
|
| 2062 |
2167 |
protected:
|
| 2063 |
2168 |
|
| 2064 |
2169 |
class AutoNodeMap : public DefaultMap<G, Node, Arc> {
|
| 2065 |
2170 |
public:
|
| 2066 |
2171 |
|
| 2067 |
2172 |
typedef DefaultMap<G, Node, Arc> Parent;
|
| 2068 |
2173 |
|
| 2069 |
2174 |
AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {}
|
| 2070 |
2175 |
|
| 2071 |
2176 |
virtual void add(const Node& node) {
|
| 2072 |
2177 |
Parent::add(node);
|
| 2073 |
2178 |
Parent::set(node, INVALID);
|
| 2074 |
2179 |
}
|
| 2075 |
2180 |
|
| 2076 |
2181 |
virtual void add(const std::vector<Node>& nodes) {
|
| 2077 |
2182 |
Parent::add(nodes);
|
| 2078 |
2183 |
for (int i = 0; i < int(nodes.size()); ++i) {
|
| 2079 |
2184 |
Parent::set(nodes[i], INVALID);
|
| 2080 |
2185 |
}
|
| 2081 |
2186 |
}
|
| 2082 |
2187 |
|
| 2083 |
2188 |
virtual void build() {
|
| 2084 |
2189 |
Parent::build();
|
| 2085 |
2190 |
Node it;
|
| 2086 |
2191 |
typename Parent::Notifier* nf = Parent::notifier();
|
| 2087 |
2192 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 2088 |
2193 |
Parent::set(it, INVALID);
|
| 2089 |
2194 |
}
|
| 2090 |
2195 |
}
|
| 2091 |
2196 |
};
|
| 2092 |
2197 |
|
| 2093 |
2198 |
const Digraph &_g;
|
| 2094 |
2199 |
AutoNodeMap _head;
|
| 2095 |
2200 |
typename Digraph::template ArcMap<Arc> _parent;
|
| 2096 |
2201 |
typename Digraph::template ArcMap<Arc> _left;
|
| 2097 |
2202 |
typename Digraph::template ArcMap<Arc> _right;
|
| 2098 |
2203 |
|
| 2099 |
2204 |
class ArcLess {
|
| 2100 |
2205 |
const Digraph &g;
|
| 2101 |
2206 |
public:
|
| 2102 |
2207 |
ArcLess(const Digraph &_g) : g(_g) {}
|
| 2103 |
2208 |
bool operator()(Arc a,Arc b) const
|
| 2104 |
2209 |
{
|
| 2105 |
2210 |
return g.target(a)<g.target(b);
|
| 2106 |
2211 |
}
|
| 2107 |
2212 |
};
|
| 2108 |
2213 |
|
| 2109 |
2214 |
public:
|
| 2110 |
2215 |
|
| 2111 |
2216 |
///Constructor
|
| 2112 |
2217 |
|
| 2113 |
2218 |
///Constructor.
|
| 2114 |
2219 |
///
|
| 2115 |
2220 |
///It builds up the search database.
|
| 2116 |
2221 |
DynArcLookUp(const Digraph &g)
|
| 2117 |
2222 |
: _g(g),_head(g),_parent(g),_left(g),_right(g)
|
| 2118 |
2223 |
{
|
| 2119 |
2224 |
Parent::attach(_g.notifier(typename Digraph::Arc()));
|
| 2120 |
2225 |
refresh();
|
| 2121 |
2226 |
}
|
| 2122 |
2227 |
|
| 2123 |
2228 |
protected:
|
| 2124 |
2229 |
|
| 2125 |
2230 |
virtual void add(const Arc& arc) {
|
| 2126 |
2231 |
insert(arc);
|
| 2127 |
2232 |
}
|
| 2128 |
2233 |
|
| 2129 |
2234 |
virtual void add(const std::vector<Arc>& arcs) {
|
| 2130 |
2235 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 2131 |
2236 |
insert(arcs[i]);
|
| 2132 |
2237 |
}
|
| 2133 |
2238 |
}
|
| 2134 |
2239 |
|
| 2135 |
2240 |
virtual void erase(const Arc& arc) {
|
| 2136 |
2241 |
remove(arc);
|
| 2137 |
2242 |
}
|
| 2138 |
2243 |
|
| 2139 |
2244 |
virtual void erase(const std::vector<Arc>& arcs) {
|
| 2140 |
2245 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 2141 |
2246 |
remove(arcs[i]);
|
| 2142 |
2247 |
}
|
| 2143 |
2248 |
}
|
| 2144 |
2249 |
|
| 2145 |
2250 |
virtual void build() {
|
| 2146 |
2251 |
refresh();
|
| 2147 |
2252 |
}
|
| 2148 |
2253 |
|
| 2149 |
2254 |
virtual void clear() {
|
| 2150 |
2255 |
for(NodeIt n(_g);n!=INVALID;++n) {
|
| 2151 |
2256 |
_head.set(n, INVALID);
|
| 2152 |
2257 |
}
|
| 2153 |
2258 |
}
|
| 2154 |
2259 |
|
| 2155 |
2260 |
void insert(Arc arc) {
|
| 2156 |
2261 |
Node s = _g.source(arc);
|
| 2157 |
2262 |
Node t = _g.target(arc);
|
| 2158 |
2263 |
_left.set(arc, INVALID);
|
| 2159 |
2264 |
_right.set(arc, INVALID);
|
| 2160 |
2265 |
|
| 2161 |
2266 |
Arc e = _head[s];
|
| 2162 |
2267 |
if (e == INVALID) {
|
| 2163 |
2268 |
_head.set(s, arc);
|
| 2164 |
2269 |
_parent.set(arc, INVALID);
|
| 2165 |
2270 |
return;
|
| 2166 |
2271 |
}
|
| 2167 |
2272 |
while (true) {
|
| 2168 |
2273 |
if (t < _g.target(e)) {
|
| 2169 |
2274 |
if (_left[e] == INVALID) {
|
| 2170 |
2275 |
_left.set(e, arc);
|
| 2171 |
2276 |
_parent.set(arc, e);
|
| 2172 |
2277 |
splay(arc);
|
| 2173 |
2278 |
return;
|
| 2174 |
2279 |
} else {
|
| 2175 |
2280 |
e = _left[e];
|
| 2176 |
2281 |
}
|
| 2177 |
2282 |
} else {
|
| 2178 |
2283 |
if (_right[e] == INVALID) {
|
| 2179 |
2284 |
_right.set(e, arc);
|
| 2180 |
2285 |
_parent.set(arc, e);
|
| 2181 |
2286 |
splay(arc);
|
| 2182 |
2287 |
return;
|
| 2183 |
2288 |
} else {
|
| 2184 |
2289 |
e = _right[e];
|
| 2185 |
2290 |
}
|
| 2186 |
2291 |
}
|
| 2187 |
2292 |
}
|
| 2188 |
2293 |
}
|
| 2189 |
2294 |
|
| 2190 |
2295 |
void remove(Arc arc) {
|
| 2191 |
2296 |
if (_left[arc] == INVALID) {
|
| 2192 |
2297 |
if (_right[arc] != INVALID) {
|
| 2193 |
2298 |
_parent.set(_right[arc], _parent[arc]);
|
| 2194 |
2299 |
}
|
| 2195 |
2300 |
if (_parent[arc] != INVALID) {
|
| 2196 |
2301 |
if (_left[_parent[arc]] == arc) {
|
| 2197 |
2302 |
_left.set(_parent[arc], _right[arc]);
|
| 2198 |
2303 |
} else {
|
| 2199 |
2304 |
_right.set(_parent[arc], _right[arc]);
|
| 2200 |
2305 |
}
|
| 2201 |
2306 |
} else {
|
| 2202 |
2307 |
_head.set(_g.source(arc), _right[arc]);
|
| 2203 |
2308 |
}
|
| 2204 |
2309 |
} else if (_right[arc] == INVALID) {
|
| 2205 |
2310 |
_parent.set(_left[arc], _parent[arc]);
|
| 2206 |
2311 |
if (_parent[arc] != INVALID) {
|
| 2207 |
2312 |
if (_left[_parent[arc]] == arc) {
|
| 2208 |
2313 |
_left.set(_parent[arc], _left[arc]);
|
| 2209 |
2314 |
} else {
|
| 2210 |
2315 |
_right.set(_parent[arc], _left[arc]);
|
| 2211 |
2316 |
}
|
| 2212 |
2317 |
} else {
|
| 2213 |
2318 |
_head.set(_g.source(arc), _left[arc]);
|
| 2214 |
2319 |
}
|
| 2215 |
2320 |
} else {
|
| 2216 |
2321 |
Arc e = _left[arc];
|
| 2217 |
2322 |
if (_right[e] != INVALID) {
|
| 2218 |
2323 |
e = _right[e];
|
| 2219 |
2324 |
while (_right[e] != INVALID) {
|
| 2220 |
2325 |
e = _right[e];
|
| 2221 |
2326 |
}
|
| 2222 |
2327 |
Arc s = _parent[e];
|
| 2223 |
2328 |
_right.set(_parent[e], _left[e]);
|
| 2224 |
2329 |
if (_left[e] != INVALID) {
|
| 2225 |
2330 |
_parent.set(_left[e], _parent[e]);
|
| 2226 |
2331 |
}
|
| 2227 |
2332 |
|
| 2228 |
2333 |
_left.set(e, _left[arc]);
|
| 2229 |
2334 |
_parent.set(_left[arc], e);
|
| 2230 |
2335 |
_right.set(e, _right[arc]);
|
| 2231 |
2336 |
_parent.set(_right[arc], e);
|
| 2232 |
2337 |
|
| 2233 |
2338 |
_parent.set(e, _parent[arc]);
|
| 2234 |
2339 |
if (_parent[arc] != INVALID) {
|
| 2235 |
2340 |
if (_left[_parent[arc]] == arc) {
|
| 2236 |
2341 |
_left.set(_parent[arc], e);
|
| 2237 |
2342 |
} else {
|
| 2238 |
2343 |
_right.set(_parent[arc], e);
|
| 2239 |
2344 |
}
|
| 2240 |
2345 |
}
|
| 2241 |
2346 |
splay(s);
|
| 2242 |
2347 |
} else {
|
| 2243 |
2348 |
_right.set(e, _right[arc]);
|
| 2244 |
2349 |
_parent.set(_right[arc], e);
|
| 2245 |
2350 |
|
| 2246 |
2351 |
if (_parent[arc] != INVALID) {
|
| 2247 |
2352 |
if (_left[_parent[arc]] == arc) {
|
| 2248 |
2353 |
_left.set(_parent[arc], e);
|
| 2249 |
2354 |
} else {
|
| 2250 |
2355 |
_right.set(_parent[arc], e);
|
| 2251 |
2356 |
}
|
| 2252 |
2357 |
} else {
|
| 2253 |
2358 |
_head.set(_g.source(arc), e);
|
| 2254 |
2359 |
}
|
| 2255 |
2360 |
}
|
| 2256 |
2361 |
}
|
| 2257 |
2362 |
}
|
| 2258 |
2363 |
|
| 2259 |
2364 |
Arc refreshRec(std::vector<Arc> &v,int a,int b)
|
| 2260 |
2365 |
{
|
| 2261 |
2366 |
int m=(a+b)/2;
|
| 2262 |
2367 |
Arc me=v[m];
|
| 2263 |
2368 |
if (a < m) {
|
| 2264 |
2369 |
Arc left = refreshRec(v,a,m-1);
|
| 2265 |
2370 |
_left.set(me, left);
|
| 2266 |
2371 |
_parent.set(left, me);
|
| 2267 |
2372 |
} else {
|
| 2268 |
2373 |
_left.set(me, INVALID);
|
| 2269 |
2374 |
}
|
| 2270 |
2375 |
if (m < b) {
|
| 2271 |
2376 |
Arc right = refreshRec(v,m+1,b);
|
| 2272 |
2377 |
_right.set(me, right);
|
| 2273 |
2378 |
_parent.set(right, me);
|
| 2274 |
2379 |
} else {
|
| 2275 |
2380 |
_right.set(me, INVALID);
|
| 2276 |
2381 |
}
|
| 2277 |
2382 |
return me;
|
| 2278 |
2383 |
}
|
| 2279 |
2384 |
|
| 2280 |
2385 |
void refresh() {
|
| 2281 |
2386 |
for(NodeIt n(_g);n!=INVALID;++n) {
|
| 2282 |
2387 |
std::vector<Arc> v;
|
| 2283 |
2388 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
|
| 2284 |
2389 |
if(v.size()) {
|
| 2285 |
2390 |
std::sort(v.begin(),v.end(),ArcLess(_g));
|
| 2286 |
2391 |
Arc head = refreshRec(v,0,v.size()-1);
|
| 2287 |
2392 |
_head.set(n, head);
|
| 2288 |
2393 |
_parent.set(head, INVALID);
|
| 2289 |
2394 |
}
|
| 2290 |
2395 |
else _head.set(n, INVALID);
|
| 2291 |
2396 |
}
|
| 2292 |
2397 |
}
|
| 2293 |
2398 |
|
| 2294 |
2399 |
void zig(Arc v) {
|
| 2295 |
2400 |
Arc w = _parent[v];
|
| 2296 |
2401 |
_parent.set(v, _parent[w]);
|
| 2297 |
2402 |
_parent.set(w, v);
|
| 2298 |
2403 |
_left.set(w, _right[v]);
|
| 2299 |
2404 |
_right.set(v, w);
|
| 2300 |
2405 |
if (_parent[v] != INVALID) {
|
| 2301 |
2406 |
if (_right[_parent[v]] == w) {
|
| 2302 |
2407 |
_right.set(_parent[v], v);
|
| 2303 |
2408 |
} else {
|
| 2304 |
2409 |
_left.set(_parent[v], v);
|
| 2305 |
2410 |
}
|
| 2306 |
2411 |
}
|
| 2307 |
2412 |
if (_left[w] != INVALID){
|
| 2308 |
2413 |
_parent.set(_left[w], w);
|
| 2309 |
2414 |
}
|
| 2310 |
2415 |
}
|
| 2311 |
2416 |
|
| 2312 |
2417 |
void zag(Arc v) {
|
| 2313 |
2418 |
Arc w = _parent[v];
|
| 2314 |
2419 |
_parent.set(v, _parent[w]);
|
| 2315 |
2420 |
_parent.set(w, v);
|
| 2316 |
2421 |
_right.set(w, _left[v]);
|
| 2317 |
2422 |
_left.set(v, w);
|
| 2318 |
2423 |
if (_parent[v] != INVALID){
|
| 2319 |
2424 |
if (_left[_parent[v]] == w) {
|
| 2320 |
2425 |
_left.set(_parent[v], v);
|
| 2321 |
2426 |
} else {
|
| 2322 |
2427 |
_right.set(_parent[v], v);
|
| 2323 |
2428 |
}
|
| 2324 |
2429 |
}
|
| 2325 |
2430 |
if (_right[w] != INVALID){
|
| 2326 |
2431 |
_parent.set(_right[w], w);
|
| 2327 |
2432 |
}
|
| 2328 |
2433 |
}
|
| 2329 |
2434 |
|
| 2330 |
2435 |
void splay(Arc v) {
|
| 2331 |
2436 |
while (_parent[v] != INVALID) {
|
| 2332 |
2437 |
if (v == _left[_parent[v]]) {
|
| 2333 |
2438 |
if (_parent[_parent[v]] == INVALID) {
|
| 2334 |
2439 |
zig(v);
|
| 2335 |
2440 |
} else {
|
| 2336 |
2441 |
if (_parent[v] == _left[_parent[_parent[v]]]) {
|
| 2337 |
2442 |
zig(_parent[v]);
|
| 2338 |
2443 |
zig(v);
|
| 2339 |
2444 |
} else {
|
| 2340 |
2445 |
zig(v);
|
| 2341 |
2446 |
zag(v);
|
| 2342 |
2447 |
}
|
| 2343 |
2448 |
}
|
| 2344 |
2449 |
} else {
|
| 2345 |
2450 |
if (_parent[_parent[v]] == INVALID) {
|
| 2346 |
2451 |
zag(v);
|
| 2347 |
2452 |
} else {
|
| 2348 |
2453 |
if (_parent[v] == _left[_parent[_parent[v]]]) {
|
| 2349 |
2454 |
zag(v);
|
| 2350 |
2455 |
zig(v);
|
| 2351 |
2456 |
} else {
|
| 2352 |
2457 |
zag(_parent[v]);
|
| 2353 |
2458 |
zag(v);
|
| 2354 |
2459 |
}
|
| 2355 |
2460 |
}
|
| 2356 |
2461 |
}
|
| 2357 |
2462 |
}
|
| 2358 |
2463 |
_head[_g.source(v)] = v;
|
| 2359 |
2464 |
}
|
| 2360 |
2465 |
|
| 2361 |
2466 |
|
| 2362 |
2467 |
public:
|
| 2363 |
2468 |
|
| 2364 |
2469 |
///Find an arc between two nodes.
|
| 2365 |
2470 |
|
| 2366 |
2471 |
///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where
|
| 2367 |
2472 |
/// <em>d</em> is the number of outgoing arcs of \c s.
|
| 2368 |
2473 |
///\param s The source node
|
| 2369 |
2474 |
///\param t The target node
|
| 2370 |
2475 |
///\return An arc from \c s to \c t if there exists,
|
| 2371 |
2476 |
///\ref INVALID otherwise.
|
| 2372 |
2477 |
Arc operator()(Node s, Node t) const
|
| 2373 |
2478 |
{
|
| 2374 |
2479 |
Arc a = _head[s];
|
| 2375 |
2480 |
while (true) {
|
| 2376 |
2481 |
if (_g.target(a) == t) {
|
| 2377 |
2482 |
const_cast<DynArcLookUp&>(*this).splay(a);
|
| 2378 |
2483 |
return a;
|
| 2379 |
2484 |
} else if (t < _g.target(a)) {
|
| 2380 |
2485 |
if (_left[a] == INVALID) {
|
| 2381 |
2486 |
const_cast<DynArcLookUp&>(*this).splay(a);
|
| 2382 |
2487 |
return INVALID;
|
| 2383 |
2488 |
} else {
|
| 2384 |
2489 |
a = _left[a];
|
| 2385 |
2490 |
}
|
| 2386 |
2491 |
} else {
|
| 2387 |
2492 |
if (_right[a] == INVALID) {
|
| 2388 |
2493 |
const_cast<DynArcLookUp&>(*this).splay(a);
|
| 2389 |
2494 |
return INVALID;
|
| 2390 |
2495 |
} else {
|
| 2391 |
2496 |
a = _right[a];
|
| 2392 |
2497 |
}
|
| 2393 |
2498 |
}
|
| 2394 |
2499 |
}
|
| 2395 |
2500 |
}
|
| 2396 |
2501 |
|
| 2397 |
2502 |
///Find the first arc between two nodes.
|
| 2398 |
2503 |
|
| 2399 |
2504 |
///Find the first arc between two nodes in time
|
| 2400 |
2505 |
/// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of
|
| 2401 |
2506 |
/// outgoing arcs of \c s.
|
| 2402 |
2507 |
///\param s The source node
|
| 2403 |
2508 |
///\param t The target node
|
| 2404 |
2509 |
///\return An arc from \c s to \c t if there exists, \ref INVALID
|
| 2405 |
2510 |
/// otherwise.
|
| 2406 |
2511 |
Arc findFirst(Node s, Node t) const
|
| 2407 |
2512 |
{
|
| 2408 |
2513 |
Arc a = _head[s];
|
| 2409 |
2514 |
Arc r = INVALID;
|
| 2410 |
2515 |
while (true) {
|
| 2411 |
2516 |
if (_g.target(a) < t) {
|
| 2412 |
2517 |
if (_right[a] == INVALID) {
|
| 2413 |
2518 |
const_cast<DynArcLookUp&>(*this).splay(a);
|
| 2414 |
2519 |
return r;
|
| 2415 |
2520 |
} else {
|
| 2416 |
2521 |
a = _right[a];
|
| 2417 |
2522 |
}
|
| 2418 |
2523 |
} else {
|
| 2419 |
2524 |
if (_g.target(a) == t) {
|
| 2420 |
2525 |
r = a;
|
| 2421 |
2526 |
}
|
| 2422 |
2527 |
if (_left[a] == INVALID) {
|
| 2423 |
2528 |
const_cast<DynArcLookUp&>(*this).splay(a);
|
| 2424 |
2529 |
return r;
|
| 2425 |
2530 |
} else {
|
| 2426 |
2531 |
a = _left[a];
|
| 2427 |
2532 |
}
|
| 2428 |
2533 |
}
|
| 2429 |
2534 |
}
|
| 2430 |
2535 |
}
|
| 2431 |
2536 |
|
| 2432 |
2537 |
///Find the next arc between two nodes.
|
| 2433 |
2538 |
|
| 2434 |
2539 |
///Find the next arc between two nodes in time
|
| 2435 |
2540 |
/// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of
|
| 2436 |
2541 |
/// outgoing arcs of \c s.
|
| 2437 |
2542 |
///\param s The source node
|
| 2438 |
2543 |
///\param t The target node
|
| 2439 |
2544 |
///\return An arc from \c s to \c t if there exists, \ref INVALID
|
| 2440 |
2545 |
/// otherwise.
|
| 2441 |
2546 |
|
| 2442 |
2547 |
///\note If \c e is not the result of the previous \c findFirst()
|
| 2443 |
2548 |
///operation then the amorized time bound can not be guaranteed.
|
| 2444 |
2549 |
#ifdef DOXYGEN
|
| 2445 |
2550 |
Arc findNext(Node s, Node t, Arc a) const
|
| 2446 |
2551 |
#else
|
| 2447 |
2552 |
Arc findNext(Node, Node t, Arc a) const
|
| 2448 |
2553 |
#endif
|
| 2449 |
2554 |
{
|
| 2450 |
2555 |
if (_right[a] != INVALID) {
|
| 2451 |
2556 |
a = _right[a];
|
| 2452 |
2557 |
while (_left[a] != INVALID) {
|
| 2453 |
2558 |
a = _left[a];
|
| 2454 |
2559 |
}
|
| 2455 |
2560 |
const_cast<DynArcLookUp&>(*this).splay(a);
|
| 2456 |
2561 |
} else {
|
| 2457 |
2562 |
while (_parent[a] != INVALID && _right[_parent[a]] == a) {
|
| 2458 |
2563 |
a = _parent[a];
|
| 2459 |
2564 |
}
|
| 2460 |
2565 |
if (_parent[a] == INVALID) {
|
| 2461 |
2566 |
return INVALID;
|
| 2462 |
2567 |
} else {
|
| 2463 |
2568 |
a = _parent[a];
|
| 2464 |
2569 |
const_cast<DynArcLookUp&>(*this).splay(a);
|
| 2465 |
2570 |
}
|
| 2466 |
2571 |
}
|
| 2467 |
2572 |
if (_g.target(a) == t) return a;
|
| 2468 |
2573 |
else return INVALID;
|
| 2469 |
2574 |
}
|
| 2470 |
2575 |
|
| 2471 |
2576 |
};
|
| 2472 |
2577 |
|
| 2473 |
2578 |
///Fast arc look up between given endpoints.
|
| 2474 |
2579 |
|
| 2475 |
2580 |
///\ingroup gutils
|
| 2476 |
2581 |
///Using this class, you can find an arc in a digraph from a given
|
| 2477 |
2582 |
///source to a given target in time <em>O(log d)</em>,
|
| 2478 |
2583 |
///where <em>d</em> is the out-degree of the source node.
|
| 2479 |
2584 |
///
|
| 2480 |
2585 |
///It is not possible to find \e all parallel arcs between two nodes.
|
| 2481 |
2586 |
///Use \ref AllArcLookUp for this purpose.
|
| 2482 |
2587 |
///
|
| 2483 |
2588 |
///\warning This class is static, so you should refresh() (or at least
|
| 2484 |
2589 |
///refresh(Node)) this data structure
|
| 2485 |
2590 |
///whenever the digraph changes. This is a time consuming (superlinearly
|
| 2486 |
2591 |
///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs).
|
| 2487 |
2592 |
///
|
| 2488 |
2593 |
///\param G The type of the underlying digraph.
|
| 2489 |
2594 |
///
|
| 2490 |
2595 |
///\sa DynArcLookUp
|
| 2491 |
2596 |
///\sa AllArcLookUp
|
| 2492 |
2597 |
template<class G>
|
| 2493 |
2598 |
class ArcLookUp
|
| 2494 |
2599 |
{
|
| 2495 |
2600 |
public:
|
| 2496 |
|
DIGRAPH_TYPEDEFS(typename G);
|
|
2601 |
DIGRAPH_TYPEDEFS(G);
|
| 2497 |
2602 |
typedef G Digraph;
|
| 2498 |
2603 |
|
| 2499 |
2604 |
protected:
|
| 2500 |
2605 |
const Digraph &_g;
|
| 2501 |
2606 |
typename Digraph::template NodeMap<Arc> _head;
|
| 2502 |
2607 |
typename Digraph::template ArcMap<Arc> _left;
|
| 2503 |
2608 |
typename Digraph::template ArcMap<Arc> _right;
|
| 2504 |
2609 |
|
| 2505 |
2610 |
class ArcLess {
|
| 2506 |
2611 |
const Digraph &g;
|
| 2507 |
2612 |
public:
|
| 2508 |
2613 |
ArcLess(const Digraph &_g) : g(_g) {}
|
| 2509 |
2614 |
bool operator()(Arc a,Arc b) const
|
| 2510 |
2615 |
{
|
| 2511 |
2616 |
return g.target(a)<g.target(b);
|
| 2512 |
2617 |
}
|
| 2513 |
2618 |
};
|
| 2514 |
2619 |
|
| 2515 |
2620 |
public:
|
| 2516 |
2621 |
|
| 2517 |
2622 |
///Constructor
|
| 2518 |
2623 |
|
| 2519 |
2624 |
///Constructor.
|
| 2520 |
2625 |
///
|
| 2521 |
2626 |
///It builds up the search database, which remains valid until the digraph
|
| 2522 |
2627 |
///changes.
|
| 2523 |
2628 |
ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
|
| 2524 |
2629 |
|
| 2525 |
2630 |
private:
|
| 2526 |
2631 |
Arc refreshRec(std::vector<Arc> &v,int a,int b)
|
| 2527 |
2632 |
{
|
| 2528 |
2633 |
int m=(a+b)/2;
|
| 2529 |
2634 |
Arc me=v[m];
|
| 2530 |
2635 |
_left[me] = a<m?refreshRec(v,a,m-1):INVALID;
|
| 2531 |
2636 |
_right[me] = m<b?refreshRec(v,m+1,b):INVALID;
|
| 2532 |
2637 |
return me;
|
| 2533 |
2638 |
}
|
| 2534 |
2639 |
public:
|
| 2535 |
2640 |
///Refresh the data structure at a node.
|
| 2536 |
2641 |
|
| 2537 |
2642 |
///Build up the search database of node \c n.
|
| 2538 |
2643 |
///
|
| 2539 |
2644 |
///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
|
| 2540 |
2645 |
///the number of the outgoing arcs of \c n.
|
| 2541 |
2646 |
void refresh(Node n)
|
| 2542 |
2647 |
{
|
| 2543 |
2648 |
std::vector<Arc> v;
|
| 2544 |
2649 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
|
| 2545 |
2650 |
if(v.size()) {
|
| 2546 |
2651 |
std::sort(v.begin(),v.end(),ArcLess(_g));
|
| 2547 |
2652 |
_head[n]=refreshRec(v,0,v.size()-1);
|
| 2548 |
2653 |
}
|
| 2549 |
2654 |
else _head[n]=INVALID;
|
| 2550 |
2655 |
}
|
| 2551 |
2656 |
///Refresh the full data structure.
|
| 2552 |
2657 |
|
| 2553 |
2658 |
///Build up the full search database. In fact, it simply calls
|
| 2554 |
2659 |
///\ref refresh(Node) "refresh(n)" for each node \c n.
|
| 2555 |
2660 |
///
|
| 2556 |
2661 |
///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
|
| 2557 |
2662 |
///the number of the arcs of \c n and <em>D</em> is the maximum
|
| 2558 |
2663 |
///out-degree of the digraph.
|
| 2559 |
2664 |
|
| 2560 |
2665 |
void refresh()
|
| 2561 |
2666 |
{
|
| 2562 |
2667 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(n);
|
| 2563 |
2668 |
}
|
| 2564 |
2669 |
|
| 2565 |
2670 |
///Find an arc between two nodes.
|
| 2566 |
2671 |
|
| 2567 |
2672 |
///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where
|
| 2568 |
2673 |
/// <em>d</em> is the number of outgoing arcs of \c s.
|
| 2569 |
2674 |
///\param s The source node
|
| 2570 |
2675 |
///\param t The target node
|
| 2571 |
2676 |
///\return An arc from \c s to \c t if there exists,
|
| 2572 |
2677 |
///\ref INVALID otherwise.
|
| 2573 |
2678 |
///
|
| 2574 |
2679 |
///\warning If you change the digraph, refresh() must be called before using
|
| 2575 |
2680 |
///this operator. If you change the outgoing arcs of
|
| 2576 |
2681 |
///a single node \c n, then
|
| 2577 |
2682 |
///\ref refresh(Node) "refresh(n)" is enough.
|
| 2578 |
2683 |
///
|
| 2579 |
2684 |
Arc operator()(Node s, Node t) const
|
| 2580 |
2685 |
{
|
| 2581 |
2686 |
Arc e;
|
| 2582 |
2687 |
for(e=_head[s];
|
| 2583 |
2688 |
e!=INVALID&&_g.target(e)!=t;
|
| 2584 |
2689 |
e = t < _g.target(e)?_left[e]:_right[e]) ;
|
| 2585 |
2690 |
return e;
|
| 2586 |
2691 |
}
|
| 2587 |
2692 |
|
| 2588 |
2693 |
};
|
| 2589 |
2694 |
|
| 2590 |
2695 |
///Fast look up of all arcs between given endpoints.
|
| 2591 |
2696 |
|
| 2592 |
2697 |
///\ingroup gutils
|
| 2593 |
2698 |
///This class is the same as \ref ArcLookUp, with the addition
|
| 2594 |
2699 |
///that it makes it possible to find all arcs between given endpoints.
|
| 2595 |
2700 |
///
|
| 2596 |
2701 |
///\warning This class is static, so you should refresh() (or at least
|
| 2597 |
2702 |
///refresh(Node)) this data structure
|
| 2598 |
2703 |
///whenever the digraph changes. This is a time consuming (superlinearly
|
| 2599 |
2704 |
///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs).
|
| 2600 |
2705 |
///
|
| 2601 |
2706 |
///\param G The type of the underlying digraph.
|
| 2602 |
2707 |
///
|
| 2603 |
2708 |
///\sa DynArcLookUp
|
| 2604 |
2709 |
///\sa ArcLookUp
|
| 2605 |
2710 |
template<class G>
|
| 2606 |
2711 |
class AllArcLookUp : public ArcLookUp<G>
|
| 2607 |
2712 |
{
|
| 2608 |
2713 |
using ArcLookUp<G>::_g;
|
| 2609 |
2714 |
using ArcLookUp<G>::_right;
|
| 2610 |
2715 |
using ArcLookUp<G>::_left;
|
| 2611 |
2716 |
using ArcLookUp<G>::_head;
|
| 2612 |
2717 |
|
| 2613 |
|
DIGRAPH_TYPEDEFS(typename G);
|
|
2718 |
DIGRAPH_TYPEDEFS(G);
|
| 2614 |
2719 |
typedef G Digraph;
|
| 2615 |
2720 |
|
| 2616 |
2721 |
typename Digraph::template ArcMap<Arc> _next;
|
| 2617 |
2722 |
|
| 2618 |
2723 |
Arc refreshNext(Arc head,Arc next=INVALID)
|
| 2619 |
2724 |
{
|
| 2620 |
2725 |
if(head==INVALID) return next;
|
| 2621 |
2726 |
else {
|
| 2622 |
2727 |
next=refreshNext(_right[head],next);
|
| 2623 |
2728 |
// _next[head]=next;
|
| 2624 |
2729 |
_next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
|
| 2625 |
2730 |
? next : INVALID;
|
| 2626 |
2731 |
return refreshNext(_left[head],head);
|
| 2627 |
2732 |
}
|
| 2628 |
2733 |
}
|
| 2629 |
2734 |
|
| 2630 |
2735 |
void refreshNext()
|
| 2631 |
2736 |
{
|
| 2632 |
2737 |
for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
|
| 2633 |
2738 |
}
|
| 2634 |
2739 |
|
| 2635 |
2740 |
public:
|
| 2636 |
2741 |
///Constructor
|
| 2637 |
2742 |
|
| 2638 |
2743 |
///Constructor.
|
| 2639 |
2744 |
///
|
| 2640 |
2745 |
///It builds up the search database, which remains valid until the digraph
|
| 2641 |
2746 |
///changes.
|
| 2642 |
2747 |
AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();}
|
| 2643 |
2748 |
|
| 2644 |
2749 |
///Refresh the data structure at a node.
|
| 2645 |
2750 |
|
| 2646 |
2751 |
///Build up the search database of node \c n.
|
| 2647 |
2752 |
///
|
| 2648 |
2753 |
///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
|
| 2649 |
2754 |
///the number of the outgoing arcs of \c n.
|
| 2650 |
2755 |
|
| 2651 |
2756 |
void refresh(Node n)
|
| 2652 |
2757 |
{
|
| 2653 |
2758 |
ArcLookUp<G>::refresh(n);
|
| 2654 |
2759 |
refreshNext(_head[n]);
|
| 2655 |
2760 |
}
|
| 2656 |
2761 |
|
| 2657 |
2762 |
///Refresh the full data structure.
|
| 2658 |
2763 |
|
| 2659 |
2764 |
///Build up the full search database. In fact, it simply calls
|
| 2660 |
2765 |
///\ref refresh(Node) "refresh(n)" for each node \c n.
|
| 2661 |
2766 |
///
|
| 2662 |
2767 |
///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is
|
| 2663 |
2768 |
///the number of the arcs of \c n and <em>D</em> is the maximum
|
| 2664 |
2769 |
///out-degree of the digraph.
|
| 2665 |
2770 |
|
| 2666 |
2771 |
void refresh()
|
| 2667 |
2772 |
{
|
| 2668 |
2773 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]);
|
| 2669 |
2774 |
}
|
| 2670 |
2775 |
|
| 2671 |
2776 |
///Find an arc between two nodes.
|
| 2672 |
2777 |
|
| 2673 |
2778 |
///Find an arc between two nodes.
|
| 2674 |
2779 |
///\param s The source node
|
| 2675 |
2780 |
///\param t The target node
|
| 2676 |
2781 |
///\param prev The previous arc between \c s and \c t. It it is INVALID or
|
| 2677 |
2782 |
///not given, the operator finds the first appropriate arc.
|
| 2678 |
2783 |
///\return An arc from \c s to \c t after \c prev or
|
| 2679 |
2784 |
///\ref INVALID if there is no more.
|
| 2680 |
2785 |
///
|
| 2681 |
2786 |
///For example, you can count the number of arcs from \c u to \c v in the
|
| 2682 |
2787 |
///following way.
|
| 2683 |
2788 |
///\code
|
| 2684 |
2789 |
///AllArcLookUp<ListDigraph> ae(g);
|
| 2685 |
2790 |
///...
|
| 2686 |
2791 |
///int n=0;
|
| 2687 |
2792 |
///for(Arc e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++;
|
| 2688 |
2793 |
///\endcode
|
| 2689 |
2794 |
///
|
| 2690 |
2795 |
///Finding the first arc take <em>O(</em>log<em>d)</em> time, where
|
| 2691 |
2796 |
/// <em>d</em> is the number of outgoing arcs of \c s. Then, the
|
| 2692 |
2797 |
///consecutive arcs are found in constant time.
|
| 2693 |
2798 |
///
|
| 2694 |
2799 |
///\warning If you change the digraph, refresh() must be called before using
|
| 2695 |
2800 |
///this operator. If you change the outgoing arcs of
|
| 2696 |
2801 |
///a single node \c n, then
|
| 2697 |
2802 |
///\ref refresh(Node) "refresh(n)" is enough.
|
| 2698 |
2803 |
///
|
| 2699 |
2804 |
#ifdef DOXYGEN
|
| 2700 |
2805 |
Arc operator()(Node s, Node t, Arc prev=INVALID) const {}
|
| 2701 |
2806 |
#else
|
| 2702 |
2807 |
using ArcLookUp<G>::operator() ;
|
| 2703 |
2808 |
Arc operator()(Node s, Node t, Arc prev) const
|
| 2704 |
2809 |
{
|
| 2705 |
2810 |
return prev==INVALID?(*this)(s,t):_next[prev];
|
| 2706 |
2811 |
}
|
| 2707 |
2812 |
#endif
|
| 2708 |
2813 |
|
| 2709 |
2814 |
};
|
| 2710 |
2815 |
|
| 2711 |
2816 |
/// @}
|
| 2712 |
2817 |
|
| 2713 |
2818 |
} //END OF NAMESPACE LEMON
|
| 2714 |
2819 |
|
| 2715 |
2820 |
#endif
|