1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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_BFS_H |
20 | 20 |
#define LEMON_BFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 | 24 |
///\brief BFS algorithm. |
25 | 25 |
|
26 | 26 |
#include <lemon/list_graph.h> |
27 | 27 |
#include <lemon/bits/path_dump.h> |
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/error.h> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/path.h> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
///Default traits class of Bfs class. |
36 | 36 |
|
37 | 37 |
///Default traits class of Bfs class. |
38 | 38 |
///\tparam GR Digraph type. |
39 | 39 |
template<class GR> |
40 | 40 |
struct BfsDefaultTraits |
41 | 41 |
{ |
42 | 42 |
///The type of the digraph the algorithm runs on. |
43 | 43 |
typedef GR Digraph; |
44 | 44 |
|
45 | 45 |
///\brief The type of the map that stores the predecessor |
46 | 46 |
///arcs of the shortest paths. |
47 | 47 |
/// |
48 | 48 |
///The type of the map that stores the predecessor |
49 | 49 |
///arcs of the shortest paths. |
50 | 50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
52 | 52 |
///Instantiates a \ref PredMap. |
53 | 53 |
|
54 | 54 |
///This function instantiates a \ref PredMap. |
55 | 55 |
///\param g is the digraph, to which we would like to define the |
56 | 56 |
///\ref PredMap. |
57 |
///\todo The digraph alone may be insufficient to initialize |
|
58 | 57 |
static PredMap *createPredMap(const Digraph &g) |
59 | 58 |
{ |
60 | 59 |
return new PredMap(g); |
61 | 60 |
} |
62 | 61 |
|
63 | 62 |
///The type of the map that indicates which nodes are processed. |
64 | 63 |
|
65 | 64 |
///The type of the map that indicates which nodes are processed. |
66 | 65 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
67 |
///By default it is a NullMap. |
|
68 | 66 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
69 | 67 |
///Instantiates a \ref ProcessedMap. |
70 | 68 |
|
71 | 69 |
///This function instantiates a \ref ProcessedMap. |
72 | 70 |
///\param g is the digraph, to which |
73 | 71 |
///we would like to define the \ref ProcessedMap |
74 | 72 |
#ifdef DOXYGEN |
75 | 73 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
76 | 74 |
#else |
77 | 75 |
static ProcessedMap *createProcessedMap(const Digraph &) |
78 | 76 |
#endif |
79 | 77 |
{ |
80 | 78 |
return new ProcessedMap(); |
81 | 79 |
} |
82 | 80 |
|
83 | 81 |
///The type of the map that indicates which nodes are reached. |
84 | 82 |
|
85 | 83 |
///The type of the map that indicates which nodes are reached. |
86 | 84 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
87 | 85 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
88 | 86 |
///Instantiates a \ref ReachedMap. |
89 | 87 |
|
90 | 88 |
///This function instantiates a \ref ReachedMap. |
91 | 89 |
///\param g is the digraph, to which |
92 | 90 |
///we would like to define the \ref ReachedMap. |
93 | 91 |
static ReachedMap *createReachedMap(const Digraph &g) |
94 | 92 |
{ |
95 | 93 |
return new ReachedMap(g); |
96 | 94 |
} |
97 | 95 |
|
98 | 96 |
///The type of the map that stores the distances of the nodes. |
99 | 97 |
|
100 | 98 |
///The type of the map that stores the distances of the nodes. |
101 | 99 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
102 | 100 |
typedef typename Digraph::template NodeMap<int> DistMap; |
103 | 101 |
///Instantiates a \ref DistMap. |
104 | 102 |
|
105 | 103 |
///This function instantiates a \ref DistMap. |
106 | 104 |
///\param g is the digraph, to which we would like to define the |
107 | 105 |
///\ref DistMap. |
108 | 106 |
static DistMap *createDistMap(const Digraph &g) |
109 | 107 |
{ |
110 | 108 |
return new DistMap(g); |
111 | 109 |
} |
112 | 110 |
}; |
113 | 111 |
|
114 | 112 |
///%BFS algorithm class. |
115 | 113 |
|
116 | 114 |
///\ingroup search |
117 | 115 |
///This class provides an efficient implementation of the %BFS algorithm. |
118 | 116 |
/// |
119 | 117 |
///There is also a \ref bfs() "function-type interface" for the BFS |
120 | 118 |
///algorithm, which is convenient in the simplier cases and it can be |
121 | 119 |
///used easier. |
122 | 120 |
/// |
123 | 121 |
///\tparam GR The type of the digraph the algorithm runs on. |
124 | 122 |
///The default value is \ref ListDigraph. The value of GR is not used |
125 | 123 |
///directly by \ref Bfs, it is only passed to \ref BfsDefaultTraits. |
126 | 124 |
///\tparam TR Traits class to set various data types used by the algorithm. |
127 | 125 |
///The default traits class is |
128 | 126 |
///\ref BfsDefaultTraits "BfsDefaultTraits<GR>". |
129 | 127 |
///See \ref BfsDefaultTraits for the documentation of |
130 | 128 |
///a Bfs traits class. |
131 | 129 |
#ifdef DOXYGEN |
132 | 130 |
template <typename GR, |
133 | 131 |
typename TR> |
134 | 132 |
#else |
135 | 133 |
template <typename GR=ListDigraph, |
136 | 134 |
typename TR=BfsDefaultTraits<GR> > |
137 | 135 |
#endif |
138 | 136 |
class Bfs { |
139 | 137 |
public: |
140 | 138 |
///\ref Exception for uninitialized parameters. |
141 | 139 |
|
142 | 140 |
///This error represents problems in the initialization of the |
143 | 141 |
///parameters of the algorithm. |
144 | 142 |
class UninitializedParameter : public lemon::UninitializedParameter { |
145 | 143 |
public: |
146 | 144 |
virtual const char* what() const throw() { |
147 | 145 |
return "lemon::Bfs::UninitializedParameter"; |
148 | 146 |
} |
149 | 147 |
}; |
150 | 148 |
|
151 | 149 |
///The type of the digraph the algorithm runs on. |
152 | 150 |
typedef typename TR::Digraph Digraph; |
153 | 151 |
|
154 | 152 |
///\brief The type of the map that stores the predecessor arcs of the |
155 | 153 |
///shortest paths. |
156 | 154 |
typedef typename TR::PredMap PredMap; |
157 | 155 |
///The type of the map that stores the distances of the nodes. |
158 | 156 |
typedef typename TR::DistMap DistMap; |
159 | 157 |
///The type of the map that indicates which nodes are reached. |
160 | 158 |
typedef typename TR::ReachedMap ReachedMap; |
161 | 159 |
///The type of the map that indicates which nodes are processed. |
162 | 160 |
typedef typename TR::ProcessedMap ProcessedMap; |
163 | 161 |
///The type of the paths. |
164 | 162 |
typedef PredMapPath<Digraph, PredMap> Path; |
165 | 163 |
|
166 | 164 |
///The traits class. |
167 | 165 |
typedef TR Traits; |
168 | 166 |
|
169 | 167 |
private: |
170 | 168 |
|
171 | 169 |
typedef typename Digraph::Node Node; |
172 | 170 |
typedef typename Digraph::NodeIt NodeIt; |
173 | 171 |
typedef typename Digraph::Arc Arc; |
174 | 172 |
typedef typename Digraph::OutArcIt OutArcIt; |
175 | 173 |
|
176 | 174 |
//Pointer to the underlying digraph. |
177 | 175 |
const Digraph *G; |
178 | 176 |
//Pointer to the map of predecessor arcs. |
179 | 177 |
PredMap *_pred; |
180 | 178 |
//Indicates if _pred is locally allocated (true) or not. |
181 | 179 |
bool local_pred; |
182 | 180 |
//Pointer to the map of distances. |
183 | 181 |
DistMap *_dist; |
184 | 182 |
//Indicates if _dist is locally allocated (true) or not. |
185 | 183 |
bool local_dist; |
186 | 184 |
//Pointer to the map of reached status of the nodes. |
187 | 185 |
ReachedMap *_reached; |
188 | 186 |
//Indicates if _reached is locally allocated (true) or not. |
189 | 187 |
bool local_reached; |
190 | 188 |
//Pointer to the map of processed status of the nodes. |
191 | 189 |
ProcessedMap *_processed; |
192 | 190 |
//Indicates if _processed is locally allocated (true) or not. |
193 | 191 |
bool local_processed; |
194 | 192 |
|
195 | 193 |
std::vector<typename Digraph::Node> _queue; |
196 | 194 |
int _queue_head,_queue_tail,_queue_next_dist; |
197 | 195 |
int _curr_dist; |
198 | 196 |
|
199 |
///Creates the maps if necessary. |
|
200 |
///\todo Better memory allocation (instead of new). |
|
197 |
//Creates the maps if necessary. |
|
201 | 198 |
void create_maps() |
202 | 199 |
{ |
203 | 200 |
if(!_pred) { |
204 | 201 |
local_pred = true; |
205 | 202 |
_pred = Traits::createPredMap(*G); |
206 | 203 |
} |
207 | 204 |
if(!_dist) { |
208 | 205 |
local_dist = true; |
209 | 206 |
_dist = Traits::createDistMap(*G); |
210 | 207 |
} |
211 | 208 |
if(!_reached) { |
212 | 209 |
local_reached = true; |
213 | 210 |
_reached = Traits::createReachedMap(*G); |
214 | 211 |
} |
215 | 212 |
if(!_processed) { |
216 | 213 |
local_processed = true; |
217 | 214 |
_processed = Traits::createProcessedMap(*G); |
218 | 215 |
} |
219 | 216 |
} |
220 | 217 |
|
221 | 218 |
protected: |
222 | 219 |
|
223 | 220 |
Bfs() {} |
224 | 221 |
|
225 | 222 |
public: |
226 | 223 |
|
227 | 224 |
typedef Bfs Create; |
228 | 225 |
|
229 | 226 |
///\name Named template parameters |
230 | 227 |
|
231 | 228 |
///@{ |
232 | 229 |
|
233 | 230 |
template <class T> |
234 | 231 |
struct SetPredMapTraits : public Traits { |
235 | 232 |
typedef T PredMap; |
236 | 233 |
static PredMap *createPredMap(const Digraph &) |
237 | 234 |
{ |
238 | 235 |
throw UninitializedParameter(); |
239 | 236 |
} |
240 | 237 |
}; |
241 | 238 |
///\brief \ref named-templ-param "Named parameter" for setting |
242 | 239 |
///\ref PredMap type. |
243 | 240 |
/// |
244 | 241 |
///\ref named-templ-param "Named parameter" for setting |
245 | 242 |
///\ref PredMap type. |
246 | 243 |
template <class T> |
247 | 244 |
struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > { |
248 | 245 |
typedef Bfs< Digraph, SetPredMapTraits<T> > Create; |
249 | 246 |
}; |
250 | 247 |
|
251 | 248 |
template <class T> |
252 | 249 |
struct SetDistMapTraits : public Traits { |
253 | 250 |
typedef T DistMap; |
254 | 251 |
static DistMap *createDistMap(const Digraph &) |
255 | 252 |
{ |
256 | 253 |
throw UninitializedParameter(); |
257 | 254 |
} |
258 | 255 |
}; |
259 | 256 |
///\brief \ref named-templ-param "Named parameter" for setting |
260 | 257 |
///\ref DistMap type. |
261 | 258 |
/// |
262 | 259 |
///\ref named-templ-param "Named parameter" for setting |
263 | 260 |
///\ref DistMap type. |
264 | 261 |
template <class T> |
265 | 262 |
struct SetDistMap : public Bfs< Digraph, SetDistMapTraits<T> > { |
266 | 263 |
typedef Bfs< Digraph, SetDistMapTraits<T> > Create; |
267 | 264 |
}; |
268 | 265 |
|
269 | 266 |
template <class T> |
270 | 267 |
struct SetReachedMapTraits : public Traits { |
271 | 268 |
typedef T ReachedMap; |
272 | 269 |
static ReachedMap *createReachedMap(const Digraph &) |
273 | 270 |
{ |
274 | 271 |
throw UninitializedParameter(); |
275 | 272 |
} |
276 | 273 |
}; |
277 | 274 |
///\brief \ref named-templ-param "Named parameter" for setting |
278 | 275 |
///\ref ReachedMap type. |
279 | 276 |
/// |
280 | 277 |
///\ref named-templ-param "Named parameter" for setting |
281 | 278 |
///\ref ReachedMap type. |
282 | 279 |
template <class T> |
283 | 280 |
struct SetReachedMap : public Bfs< Digraph, SetReachedMapTraits<T> > { |
284 | 281 |
typedef Bfs< Digraph, SetReachedMapTraits<T> > Create; |
285 | 282 |
}; |
286 | 283 |
|
287 | 284 |
template <class T> |
288 | 285 |
struct SetProcessedMapTraits : public Traits { |
289 | 286 |
typedef T ProcessedMap; |
290 | 287 |
static ProcessedMap *createProcessedMap(const Digraph &) |
291 | 288 |
{ |
292 | 289 |
throw UninitializedParameter(); |
293 | 290 |
} |
294 | 291 |
}; |
295 | 292 |
///\brief \ref named-templ-param "Named parameter" for setting |
296 | 293 |
///\ref ProcessedMap type. |
297 | 294 |
/// |
298 | 295 |
///\ref named-templ-param "Named parameter" for setting |
299 | 296 |
///\ref ProcessedMap type. |
300 | 297 |
template <class T> |
301 | 298 |
struct SetProcessedMap : public Bfs< Digraph, SetProcessedMapTraits<T> > { |
302 | 299 |
typedef Bfs< Digraph, SetProcessedMapTraits<T> > Create; |
303 | 300 |
}; |
304 | 301 |
|
305 | 302 |
struct SetStandardProcessedMapTraits : public Traits { |
306 | 303 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
307 | 304 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
308 | 305 |
{ |
309 | 306 |
return new ProcessedMap(g); |
310 | 307 |
} |
311 | 308 |
}; |
312 | 309 |
///\brief \ref named-templ-param "Named parameter" for setting |
313 | 310 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
314 | 311 |
/// |
315 | 312 |
///\ref named-templ-param "Named parameter" for setting |
316 | 313 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
317 | 314 |
///If you don't set it explicitly, it will be automatically allocated. |
318 | 315 |
struct SetStandardProcessedMap : |
319 | 316 |
public Bfs< Digraph, SetStandardProcessedMapTraits > { |
320 | 317 |
typedef Bfs< Digraph, SetStandardProcessedMapTraits > Create; |
321 | 318 |
}; |
322 | 319 |
|
323 | 320 |
///@} |
324 | 321 |
|
325 | 322 |
public: |
326 | 323 |
|
327 | 324 |
///Constructor. |
328 | 325 |
|
329 | 326 |
///Constructor. |
330 | 327 |
///\param g The digraph the algorithm runs on. |
331 | 328 |
Bfs(const Digraph &g) : |
332 | 329 |
G(&g), |
333 | 330 |
_pred(NULL), local_pred(false), |
334 | 331 |
_dist(NULL), local_dist(false), |
335 | 332 |
_reached(NULL), local_reached(false), |
336 | 333 |
_processed(NULL), local_processed(false) |
337 | 334 |
{ } |
338 | 335 |
|
339 | 336 |
///Destructor. |
340 | 337 |
~Bfs() |
341 | 338 |
{ |
342 | 339 |
if(local_pred) delete _pred; |
343 | 340 |
if(local_dist) delete _dist; |
344 | 341 |
if(local_reached) delete _reached; |
345 | 342 |
if(local_processed) delete _processed; |
346 | 343 |
} |
347 | 344 |
|
348 | 345 |
///Sets the map that stores the predecessor arcs. |
349 | 346 |
|
350 | 347 |
///Sets the map that stores the predecessor arcs. |
351 | 348 |
///If you don't use this function before calling \ref run(), |
352 | 349 |
///it will allocate one. The destructor deallocates this |
353 | 350 |
///automatically allocated map, of course. |
354 | 351 |
///\return <tt> (*this) </tt> |
355 | 352 |
Bfs &predMap(PredMap &m) |
356 | 353 |
{ |
357 | 354 |
if(local_pred) { |
358 | 355 |
delete _pred; |
359 | 356 |
local_pred=false; |
360 | 357 |
} |
361 | 358 |
_pred = &m; |
362 | 359 |
return *this; |
363 | 360 |
} |
364 | 361 |
|
365 | 362 |
///Sets the map that indicates which nodes are reached. |
366 | 363 |
|
367 | 364 |
///Sets the map that indicates which nodes are reached. |
368 | 365 |
///If you don't use this function before calling \ref run(), |
369 | 366 |
///it will allocate one. The destructor deallocates this |
370 | 367 |
///automatically allocated map, of course. |
371 | 368 |
///\return <tt> (*this) </tt> |
372 | 369 |
Bfs &reachedMap(ReachedMap &m) |
373 | 370 |
{ |
374 | 371 |
if(local_reached) { |
375 | 372 |
delete _reached; |
376 | 373 |
local_reached=false; |
377 | 374 |
} |
378 | 375 |
_reached = &m; |
379 | 376 |
return *this; |
380 | 377 |
} |
381 | 378 |
|
382 | 379 |
///Sets the map that indicates which nodes are processed. |
383 | 380 |
|
384 | 381 |
///Sets the map that indicates which nodes are processed. |
385 | 382 |
///If you don't use this function before calling \ref run(), |
386 | 383 |
///it will allocate one. The destructor deallocates this |
387 | 384 |
///automatically allocated map, of course. |
388 | 385 |
///\return <tt> (*this) </tt> |
389 | 386 |
Bfs &processedMap(ProcessedMap &m) |
390 | 387 |
{ |
391 | 388 |
if(local_processed) { |
392 | 389 |
delete _processed; |
393 | 390 |
local_processed=false; |
394 | 391 |
} |
395 | 392 |
_processed = &m; |
396 | 393 |
return *this; |
397 | 394 |
} |
398 | 395 |
|
399 | 396 |
///Sets the map that stores the distances of the nodes. |
400 | 397 |
|
401 | 398 |
///Sets the map that stores the distances of the nodes calculated by |
402 | 399 |
///the algorithm. |
403 | 400 |
///If you don't use this function before calling \ref run(), |
404 | 401 |
///it will allocate one. The destructor deallocates this |
405 | 402 |
///automatically allocated map, of course. |
406 | 403 |
///\return <tt> (*this) </tt> |
407 | 404 |
Bfs &distMap(DistMap &m) |
408 | 405 |
{ |
409 | 406 |
if(local_dist) { |
410 | 407 |
delete _dist; |
411 | 408 |
local_dist=false; |
412 | 409 |
} |
413 | 410 |
_dist = &m; |
414 | 411 |
return *this; |
415 | 412 |
} |
416 | 413 |
|
417 | 414 |
public: |
418 | 415 |
|
419 | 416 |
///\name Execution control |
420 | 417 |
///The simplest way to execute the algorithm is to use |
421 | 418 |
///one of the member functions called \ref lemon::Bfs::run() "run()". |
422 | 419 |
///\n |
423 | 420 |
///If you need more control on the execution, first you must call |
424 | 421 |
///\ref lemon::Bfs::init() "init()", then you can add several source |
425 | 422 |
///nodes with \ref lemon::Bfs::addSource() "addSource()". |
426 | 423 |
///Finally \ref lemon::Bfs::start() "start()" will perform the |
427 | 424 |
///actual path computation. |
428 | 425 |
|
429 | 426 |
///@{ |
430 | 427 |
|
431 | 428 |
///Initializes the internal data structures. |
432 | 429 |
|
433 | 430 |
///Initializes the internal data structures. |
434 | 431 |
/// |
435 | 432 |
void init() |
436 | 433 |
{ |
437 | 434 |
create_maps(); |
438 | 435 |
_queue.resize(countNodes(*G)); |
439 | 436 |
_queue_head=_queue_tail=0; |
440 | 437 |
_curr_dist=1; |
441 | 438 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
442 | 439 |
_pred->set(u,INVALID); |
443 | 440 |
_reached->set(u,false); |
444 | 441 |
_processed->set(u,false); |
445 | 442 |
} |
446 | 443 |
} |
447 | 444 |
|
448 | 445 |
///Adds a new source node. |
449 | 446 |
|
450 | 447 |
///Adds a new source node to the set of nodes to be processed. |
451 | 448 |
/// |
452 | 449 |
void addSource(Node s) |
453 | 450 |
{ |
454 | 451 |
if(!(*_reached)[s]) |
455 | 452 |
{ |
456 | 453 |
_reached->set(s,true); |
457 | 454 |
_pred->set(s,INVALID); |
458 | 455 |
_dist->set(s,0); |
459 | 456 |
_queue[_queue_head++]=s; |
460 | 457 |
_queue_next_dist=_queue_head; |
461 | 458 |
} |
462 | 459 |
} |
463 | 460 |
|
464 | 461 |
///Processes the next node. |
465 | 462 |
|
466 | 463 |
///Processes the next node. |
467 | 464 |
/// |
468 | 465 |
///\return The processed node. |
469 | 466 |
/// |
470 | 467 |
///\pre The queue must not be empty. |
471 | 468 |
Node processNextNode() |
472 | 469 |
{ |
473 | 470 |
if(_queue_tail==_queue_next_dist) { |
474 | 471 |
_curr_dist++; |
475 | 472 |
_queue_next_dist=_queue_head; |
476 | 473 |
} |
477 | 474 |
Node n=_queue[_queue_tail++]; |
478 | 475 |
_processed->set(n,true); |
479 | 476 |
Node m; |
480 | 477 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
481 | 478 |
if(!(*_reached)[m=G->target(e)]) { |
482 | 479 |
_queue[_queue_head++]=m; |
483 | 480 |
_reached->set(m,true); |
484 | 481 |
_pred->set(m,e); |
485 | 482 |
_dist->set(m,_curr_dist); |
486 | 483 |
} |
487 | 484 |
return n; |
488 | 485 |
} |
489 | 486 |
|
490 | 487 |
///Processes the next node. |
491 | 488 |
|
492 | 489 |
///Processes the next node and checks if the given target node |
493 | 490 |
///is reached. If the target node is reachable from the processed |
494 | 491 |
///node, then the \c reach parameter will be set to \c true. |
495 | 492 |
/// |
496 | 493 |
///\param target The target node. |
497 | 494 |
///\retval reach Indicates if the target node is reached. |
498 | 495 |
///It should be initially \c false. |
499 | 496 |
/// |
500 | 497 |
///\return The processed node. |
501 | 498 |
/// |
502 | 499 |
///\pre The queue must not be empty. |
503 | 500 |
Node processNextNode(Node target, bool& reach) |
504 | 501 |
{ |
505 | 502 |
if(_queue_tail==_queue_next_dist) { |
506 | 503 |
_curr_dist++; |
507 | 504 |
_queue_next_dist=_queue_head; |
508 | 505 |
} |
509 | 506 |
Node n=_queue[_queue_tail++]; |
510 | 507 |
_processed->set(n,true); |
511 | 508 |
Node m; |
512 | 509 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
513 | 510 |
if(!(*_reached)[m=G->target(e)]) { |
514 | 511 |
_queue[_queue_head++]=m; |
515 | 512 |
_reached->set(m,true); |
516 | 513 |
_pred->set(m,e); |
517 | 514 |
_dist->set(m,_curr_dist); |
518 | 515 |
reach = reach || (target == m); |
519 | 516 |
} |
520 | 517 |
return n; |
521 | 518 |
} |
522 | 519 |
|
523 | 520 |
///Processes the next node. |
524 | 521 |
|
525 | 522 |
///Processes the next node and checks if at least one of reached |
526 | 523 |
///nodes has \c true value in the \c nm node map. If one node |
527 | 524 |
///with \c true value is reachable from the processed node, then the |
528 | 525 |
///\c rnode parameter will be set to the first of such nodes. |
529 | 526 |
/// |
530 | 527 |
///\param nm A \c bool (or convertible) node map that indicates the |
531 | 528 |
///possible targets. |
532 | 529 |
///\retval rnode The reached target node. |
533 | 530 |
///It should be initially \c INVALID. |
534 | 531 |
/// |
535 | 532 |
///\return The processed node. |
536 | 533 |
/// |
537 | 534 |
///\pre The queue must not be empty. |
538 | 535 |
template<class NM> |
539 | 536 |
Node processNextNode(const NM& nm, Node& rnode) |
540 | 537 |
{ |
541 | 538 |
if(_queue_tail==_queue_next_dist) { |
542 | 539 |
_curr_dist++; |
543 | 540 |
_queue_next_dist=_queue_head; |
544 | 541 |
} |
545 | 542 |
Node n=_queue[_queue_tail++]; |
546 | 543 |
_processed->set(n,true); |
547 | 544 |
Node m; |
548 | 545 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
549 | 546 |
if(!(*_reached)[m=G->target(e)]) { |
550 | 547 |
_queue[_queue_head++]=m; |
551 | 548 |
_reached->set(m,true); |
552 | 549 |
_pred->set(m,e); |
553 | 550 |
_dist->set(m,_curr_dist); |
554 | 551 |
if (nm[m] && rnode == INVALID) rnode = m; |
555 | 552 |
} |
556 | 553 |
return n; |
557 | 554 |
} |
558 | 555 |
|
559 | 556 |
///The next node to be processed. |
560 | 557 |
|
561 | 558 |
///Returns the next node to be processed or \c INVALID if the queue |
562 | 559 |
///is empty. |
563 | 560 |
Node nextNode() const |
564 | 561 |
{ |
565 | 562 |
return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID; |
566 | 563 |
} |
567 | 564 |
|
568 | 565 |
///\brief Returns \c false if there are nodes |
569 | 566 |
///to be processed. |
570 | 567 |
/// |
571 | 568 |
///Returns \c false if there are nodes |
572 | 569 |
///to be processed in the queue. |
573 | 570 |
bool emptyQueue() const { return _queue_tail==_queue_head; } |
574 | 571 |
|
575 | 572 |
///Returns the number of the nodes to be processed. |
576 | 573 |
|
577 | 574 |
///Returns the number of the nodes to be processed in the queue. |
578 | 575 |
int queueSize() const { return _queue_head-_queue_tail; } |
579 | 576 |
|
580 | 577 |
///Executes the algorithm. |
581 | 578 |
|
582 | 579 |
///Executes the algorithm. |
583 | 580 |
/// |
584 | 581 |
///This method runs the %BFS algorithm from the root node(s) |
585 | 582 |
///in order to compute the shortest path to each node. |
586 | 583 |
/// |
587 | 584 |
///The algorithm computes |
588 | 585 |
///- the shortest path tree (forest), |
589 | 586 |
///- the distance of each node from the root(s). |
590 | 587 |
/// |
591 | 588 |
///\pre init() must be called and at least one root node should be |
592 | 589 |
///added with addSource() before using this function. |
593 | 590 |
/// |
594 | 591 |
///\note <tt>b.start()</tt> is just a shortcut of the following code. |
595 | 592 |
///\code |
596 | 593 |
/// while ( !b.emptyQueue() ) { |
597 | 594 |
/// b.processNextNode(); |
598 | 595 |
/// } |
599 | 596 |
///\endcode |
600 | 597 |
void start() |
601 | 598 |
{ |
602 | 599 |
while ( !emptyQueue() ) processNextNode(); |
603 | 600 |
} |
604 | 601 |
|
605 | 602 |
///Executes the algorithm until the given target node is reached. |
606 | 603 |
|
607 | 604 |
///Executes the algorithm until the given target node is reached. |
608 | 605 |
/// |
609 | 606 |
///This method runs the %BFS algorithm from the root node(s) |
610 | 607 |
///in order to compute the shortest path to \c t. |
611 | 608 |
/// |
612 | 609 |
///The algorithm computes |
613 | 610 |
///- the shortest path to \c t, |
614 | 611 |
///- the distance of \c t from the root(s). |
615 | 612 |
/// |
616 | 613 |
///\pre init() must be called and at least one root node should be |
617 | 614 |
///added with addSource() before using this function. |
618 | 615 |
/// |
619 | 616 |
///\note <tt>b.start(t)</tt> is just a shortcut of the following code. |
620 | 617 |
///\code |
621 | 618 |
/// bool reach = false; |
622 | 619 |
/// while ( !b.emptyQueue() && !reach ) { |
623 | 620 |
/// b.processNextNode(t, reach); |
624 | 621 |
/// } |
625 | 622 |
///\endcode |
626 | 623 |
void start(Node t) |
627 | 624 |
{ |
628 | 625 |
bool reach = false; |
629 | 626 |
while ( !emptyQueue() && !reach ) processNextNode(t, reach); |
630 | 627 |
} |
631 | 628 |
|
632 | 629 |
///Executes the algorithm until a condition is met. |
633 | 630 |
|
634 | 631 |
///Executes the algorithm until a condition is met. |
635 | 632 |
/// |
636 | 633 |
///This method runs the %BFS algorithm from the root node(s) in |
637 | 634 |
///order to compute the shortest path to a node \c v with |
638 | 635 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
639 | 636 |
/// |
640 | 637 |
///\param nm A \c bool (or convertible) node map. The algorithm |
641 | 638 |
///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. |
642 | 639 |
/// |
643 | 640 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
644 | 641 |
///\c INVALID if no such node was found. |
645 | 642 |
/// |
646 | 643 |
///\pre init() must be called and at least one root node should be |
647 | 644 |
///added with addSource() before using this function. |
648 | 645 |
/// |
649 | 646 |
///\note <tt>b.start(nm)</tt> is just a shortcut of the following code. |
650 | 647 |
///\code |
651 | 648 |
/// Node rnode = INVALID; |
652 | 649 |
/// while ( !b.emptyQueue() && rnode == INVALID ) { |
653 | 650 |
/// b.processNextNode(nm, rnode); |
654 | 651 |
/// } |
655 | 652 |
/// return rnode; |
656 | 653 |
///\endcode |
657 | 654 |
template<class NodeBoolMap> |
658 | 655 |
Node start(const NodeBoolMap &nm) |
659 | 656 |
{ |
660 | 657 |
Node rnode = INVALID; |
661 | 658 |
while ( !emptyQueue() && rnode == INVALID ) { |
662 | 659 |
processNextNode(nm, rnode); |
663 | 660 |
} |
664 | 661 |
return rnode; |
665 | 662 |
} |
666 | 663 |
|
667 | 664 |
///Runs the algorithm from the given source node. |
668 | 665 |
|
669 | 666 |
///This method runs the %BFS algorithm from node \c s |
670 | 667 |
///in order to compute the shortest path to each node. |
671 | 668 |
/// |
672 | 669 |
///The algorithm computes |
673 | 670 |
///- the shortest path tree, |
674 | 671 |
///- the distance of each node from the root. |
675 | 672 |
/// |
676 | 673 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
677 | 674 |
///\code |
678 | 675 |
/// b.init(); |
679 | 676 |
/// b.addSource(s); |
680 | 677 |
/// b.start(); |
681 | 678 |
///\endcode |
682 | 679 |
void run(Node s) { |
683 | 680 |
init(); |
684 | 681 |
addSource(s); |
685 | 682 |
start(); |
686 | 683 |
} |
687 | 684 |
|
688 | 685 |
///Finds the shortest path between \c s and \c t. |
689 | 686 |
|
690 | 687 |
///This method runs the %BFS algorithm from node \c s |
691 | 688 |
///in order to compute the shortest path to node \c t |
692 | 689 |
///(it stops searching when \c t is processed). |
693 | 690 |
/// |
694 | 691 |
///\return \c true if \c t is reachable form \c s. |
695 | 692 |
/// |
696 | 693 |
///\note Apart from the return value, <tt>b.run(s,t)</tt> is just a |
697 | 694 |
///shortcut of the following code. |
698 | 695 |
///\code |
699 | 696 |
/// b.init(); |
700 | 697 |
/// b.addSource(s); |
701 | 698 |
/// b.start(t); |
702 | 699 |
///\endcode |
703 | 700 |
bool run(Node s,Node t) { |
704 | 701 |
init(); |
705 | 702 |
addSource(s); |
706 | 703 |
start(t); |
707 | 704 |
return reached(t); |
708 | 705 |
} |
709 | 706 |
|
710 | 707 |
///Runs the algorithm to visit all nodes in the digraph. |
711 | 708 |
|
712 | 709 |
///This method runs the %BFS algorithm in order to |
713 | 710 |
///compute the shortest path to each node. |
714 | 711 |
/// |
715 | 712 |
///The algorithm computes |
716 | 713 |
///- the shortest path tree (forest), |
717 | 714 |
///- the distance of each node from the root(s). |
718 | 715 |
/// |
719 | 716 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
720 | 717 |
///\code |
721 | 718 |
/// b.init(); |
722 | 719 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
723 | 720 |
/// if (!b.reached(n)) { |
724 | 721 |
/// b.addSource(n); |
725 | 722 |
/// b.start(); |
726 | 723 |
/// } |
727 | 724 |
/// } |
728 | 725 |
///\endcode |
729 | 726 |
void run() { |
730 | 727 |
init(); |
731 | 728 |
for (NodeIt n(*G); n != INVALID; ++n) { |
732 | 729 |
if (!reached(n)) { |
733 | 730 |
addSource(n); |
734 | 731 |
start(); |
735 | 732 |
} |
736 | 733 |
} |
737 | 734 |
} |
738 | 735 |
|
739 | 736 |
///@} |
740 | 737 |
|
741 | 738 |
///\name Query Functions |
742 | 739 |
///The result of the %BFS algorithm can be obtained using these |
743 | 740 |
///functions.\n |
744 | 741 |
///Either \ref lemon::Bfs::run() "run()" or \ref lemon::Bfs::start() |
745 | 742 |
///"start()" must be called before using them. |
746 | 743 |
|
747 | 744 |
///@{ |
748 | 745 |
|
749 | 746 |
///The shortest path to a node. |
750 | 747 |
|
751 | 748 |
///Returns the shortest path to a node. |
752 | 749 |
/// |
753 | 750 |
///\warning \c t should be reachable from the root(s). |
754 | 751 |
/// |
755 | 752 |
///\pre Either \ref run() or \ref start() must be called before |
756 | 753 |
///using this function. |
757 | 754 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
758 | 755 |
|
759 | 756 |
///The distance of a node from the root(s). |
760 | 757 |
|
761 | 758 |
///Returns the distance of a node from the root(s). |
762 | 759 |
/// |
763 | 760 |
///\warning If node \c v is not reachable from the root(s), then |
764 | 761 |
///the return value of this function is undefined. |
765 | 762 |
/// |
766 | 763 |
///\pre Either \ref run() or \ref start() must be called before |
767 | 764 |
///using this function. |
768 | 765 |
int dist(Node v) const { return (*_dist)[v]; } |
769 | 766 |
|
770 | 767 |
///Returns the 'previous arc' of the shortest path tree for a node. |
771 | 768 |
|
772 | 769 |
///This function returns the 'previous arc' of the shortest path |
773 | 770 |
///tree for the node \c v, i.e. it returns the last arc of a |
774 | 771 |
///shortest path from the root(s) to \c v. It is \c INVALID if \c v |
775 | 772 |
///is not reachable from the root(s) or if \c v is a root. |
776 | 773 |
/// |
777 | 774 |
///The shortest path tree used here is equal to the shortest path |
778 | 775 |
///tree used in \ref predNode(). |
779 | 776 |
/// |
780 | 777 |
///\pre Either \ref run() or \ref start() must be called before |
781 | 778 |
///using this function. |
782 | 779 |
Arc predArc(Node v) const { return (*_pred)[v];} |
783 | 780 |
|
784 | 781 |
///Returns the 'previous node' of the shortest path tree for a node. |
785 | 782 |
|
786 | 783 |
///This function returns the 'previous node' of the shortest path |
787 | 784 |
///tree for the node \c v, i.e. it returns the last but one node |
788 | 785 |
///from a shortest path from the root(s) to \c v. It is \c INVALID |
789 | 786 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
790 | 787 |
/// |
791 | 788 |
///The shortest path tree used here is equal to the shortest path |
792 | 789 |
///tree used in \ref predArc(). |
793 | 790 |
/// |
794 | 791 |
///\pre Either \ref run() or \ref start() must be called before |
795 | 792 |
///using this function. |
796 | 793 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
797 | 794 |
G->source((*_pred)[v]); } |
798 | 795 |
|
799 | 796 |
///\brief Returns a const reference to the node map that stores the |
800 | 797 |
/// distances of the nodes. |
801 | 798 |
/// |
802 | 799 |
///Returns a const reference to the node map that stores the distances |
803 | 800 |
///of the nodes calculated by the algorithm. |
804 | 801 |
/// |
805 | 802 |
///\pre Either \ref run() or \ref init() |
806 | 803 |
///must be called before using this function. |
807 | 804 |
const DistMap &distMap() const { return *_dist;} |
808 | 805 |
|
809 | 806 |
///\brief Returns a const reference to the node map that stores the |
810 | 807 |
///predecessor arcs. |
811 | 808 |
/// |
812 | 809 |
///Returns a const reference to the node map that stores the predecessor |
813 | 810 |
///arcs, which form the shortest path tree. |
814 | 811 |
/// |
815 | 812 |
///\pre Either \ref run() or \ref init() |
816 | 813 |
///must be called before using this function. |
817 | 814 |
const PredMap &predMap() const { return *_pred;} |
818 | 815 |
|
819 | 816 |
///Checks if a node is reachable from the root(s). |
820 | 817 |
|
821 | 818 |
///Returns \c true if \c v is reachable from the root(s). |
822 | 819 |
///\pre Either \ref run() or \ref start() |
823 | 820 |
///must be called before using this function. |
824 | 821 |
bool reached(Node v) const { return (*_reached)[v]; } |
825 | 822 |
|
826 | 823 |
///@} |
827 | 824 |
}; |
828 | 825 |
|
829 | 826 |
///Default traits class of bfs() function. |
830 | 827 |
|
831 | 828 |
///Default traits class of bfs() function. |
832 | 829 |
///\tparam GR Digraph type. |
833 | 830 |
template<class GR> |
834 | 831 |
struct BfsWizardDefaultTraits |
835 | 832 |
{ |
836 | 833 |
///The type of the digraph the algorithm runs on. |
837 | 834 |
typedef GR Digraph; |
838 | 835 |
|
839 | 836 |
///\brief The type of the map that stores the predecessor |
840 | 837 |
///arcs of the shortest paths. |
841 | 838 |
/// |
842 | 839 |
///The type of the map that stores the predecessor |
843 | 840 |
///arcs of the shortest paths. |
844 | 841 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
845 | 842 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
846 | 843 |
///Instantiates a \ref PredMap. |
847 | 844 |
|
848 | 845 |
///This function instantiates a \ref PredMap. |
849 | 846 |
///\param g is the digraph, to which we would like to define the |
850 | 847 |
///\ref PredMap. |
851 |
///\todo The digraph alone may be insufficient to initialize |
|
852 | 848 |
static PredMap *createPredMap(const Digraph &g) |
853 | 849 |
{ |
854 | 850 |
return new PredMap(g); |
855 | 851 |
} |
856 | 852 |
|
857 | 853 |
///The type of the map that indicates which nodes are processed. |
858 | 854 |
|
859 | 855 |
///The type of the map that indicates which nodes are processed. |
860 | 856 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
861 | 857 |
///By default it is a NullMap. |
862 | 858 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
863 | 859 |
///Instantiates a \ref ProcessedMap. |
864 | 860 |
|
865 | 861 |
///This function instantiates a \ref ProcessedMap. |
866 | 862 |
///\param g is the digraph, to which |
867 | 863 |
///we would like to define the \ref ProcessedMap. |
868 | 864 |
#ifdef DOXYGEN |
869 | 865 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
870 | 866 |
#else |
871 | 867 |
static ProcessedMap *createProcessedMap(const Digraph &) |
872 | 868 |
#endif |
873 | 869 |
{ |
874 | 870 |
return new ProcessedMap(); |
875 | 871 |
} |
876 | 872 |
|
877 | 873 |
///The type of the map that indicates which nodes are reached. |
878 | 874 |
|
879 | 875 |
///The type of the map that indicates which nodes are reached. |
880 | 876 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
881 | 877 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
882 | 878 |
///Instantiates a \ref ReachedMap. |
883 | 879 |
|
884 | 880 |
///This function instantiates a \ref ReachedMap. |
885 | 881 |
///\param g is the digraph, to which |
886 | 882 |
///we would like to define the \ref ReachedMap. |
887 | 883 |
static ReachedMap *createReachedMap(const Digraph &g) |
888 | 884 |
{ |
889 | 885 |
return new ReachedMap(g); |
890 | 886 |
} |
891 | 887 |
|
892 | 888 |
///The type of the map that stores the distances of the nodes. |
893 | 889 |
|
894 | 890 |
///The type of the map that stores the distances of the nodes. |
895 | 891 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
896 | 892 |
typedef typename Digraph::template NodeMap<int> DistMap; |
897 | 893 |
///Instantiates a \ref DistMap. |
898 | 894 |
|
899 | 895 |
///This function instantiates a \ref DistMap. |
900 | 896 |
///\param g is the digraph, to which we would like to define |
901 | 897 |
///the \ref DistMap |
902 | 898 |
static DistMap *createDistMap(const Digraph &g) |
903 | 899 |
{ |
904 | 900 |
return new DistMap(g); |
905 | 901 |
} |
906 | 902 |
|
907 | 903 |
///The type of the shortest paths. |
908 | 904 |
|
909 | 905 |
///The type of the shortest paths. |
910 | 906 |
///It must meet the \ref concepts::Path "Path" concept. |
911 | 907 |
typedef lemon::Path<Digraph> Path; |
912 | 908 |
}; |
913 | 909 |
|
914 | 910 |
/// Default traits class used by \ref BfsWizard |
915 | 911 |
|
916 | 912 |
/// To make it easier to use Bfs algorithm |
917 | 913 |
/// we have created a wizard class. |
918 | 914 |
/// This \ref BfsWizard class needs default traits, |
919 | 915 |
/// as well as the \ref Bfs class. |
920 | 916 |
/// The \ref BfsWizardBase is a class to be the default traits of the |
921 | 917 |
/// \ref BfsWizard class. |
922 | 918 |
template<class GR> |
923 | 919 |
class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
924 | 920 |
{ |
925 | 921 |
|
926 | 922 |
typedef BfsWizardDefaultTraits<GR> Base; |
927 | 923 |
protected: |
928 | 924 |
//The type of the nodes in the digraph. |
929 | 925 |
typedef typename Base::Digraph::Node Node; |
930 | 926 |
|
931 | 927 |
//Pointer to the digraph the algorithm runs on. |
932 | 928 |
void *_g; |
933 | 929 |
//Pointer to the map of reached nodes. |
934 | 930 |
void *_reached; |
935 | 931 |
//Pointer to the map of processed nodes. |
936 | 932 |
void *_processed; |
937 | 933 |
//Pointer to the map of predecessors arcs. |
938 | 934 |
void *_pred; |
939 | 935 |
//Pointer to the map of distances. |
940 | 936 |
void *_dist; |
941 | 937 |
//Pointer to the shortest path to the target node. |
942 | 938 |
void *_path; |
943 | 939 |
//Pointer to the distance of the target node. |
944 | 940 |
int *_di; |
945 | 941 |
|
946 | 942 |
public: |
947 | 943 |
/// Constructor. |
948 | 944 |
|
949 | 945 |
/// This constructor does not require parameters, therefore it initiates |
950 | 946 |
/// all of the attributes to \c 0. |
951 | 947 |
BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
952 | 948 |
_dist(0), _path(0), _di(0) {} |
953 | 949 |
|
954 | 950 |
/// Constructor. |
955 | 951 |
|
956 | 952 |
/// This constructor requires one parameter, |
957 | 953 |
/// others are initiated to \c 0. |
958 | 954 |
/// \param g The digraph the algorithm runs on. |
959 | 955 |
BfsWizardBase(const GR &g) : |
960 | 956 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
961 | 957 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
962 | 958 |
|
963 | 959 |
}; |
964 | 960 |
|
965 | 961 |
/// Auxiliary class for the function-type interface of BFS algorithm. |
966 | 962 |
|
967 | 963 |
/// This auxiliary class is created to implement the |
968 | 964 |
/// \ref bfs() "function-type interface" of \ref Bfs algorithm. |
969 | 965 |
/// It does not have own \ref run() method, it uses the functions |
970 | 966 |
/// and features of the plain \ref Bfs. |
971 | 967 |
/// |
972 | 968 |
/// This class should only be used through the \ref bfs() function, |
973 | 969 |
/// which makes it easier to use the algorithm. |
974 | 970 |
template<class TR> |
975 | 971 |
class BfsWizard : public TR |
976 | 972 |
{ |
977 | 973 |
typedef TR Base; |
978 | 974 |
|
979 | 975 |
///The type of the digraph the algorithm runs on. |
980 | 976 |
typedef typename TR::Digraph Digraph; |
981 | 977 |
|
982 | 978 |
typedef typename Digraph::Node Node; |
983 | 979 |
typedef typename Digraph::NodeIt NodeIt; |
984 | 980 |
typedef typename Digraph::Arc Arc; |
985 | 981 |
typedef typename Digraph::OutArcIt OutArcIt; |
986 | 982 |
|
987 | 983 |
///\brief The type of the map that stores the predecessor |
988 | 984 |
///arcs of the shortest paths. |
989 | 985 |
typedef typename TR::PredMap PredMap; |
990 | 986 |
///\brief The type of the map that stores the distances of the nodes. |
991 | 987 |
typedef typename TR::DistMap DistMap; |
992 | 988 |
///\brief The type of the map that indicates which nodes are reached. |
993 | 989 |
typedef typename TR::ReachedMap ReachedMap; |
994 | 990 |
///\brief The type of the map that indicates which nodes are processed. |
995 | 991 |
typedef typename TR::ProcessedMap ProcessedMap; |
996 | 992 |
///The type of the shortest paths |
997 | 993 |
typedef typename TR::Path Path; |
998 | 994 |
|
999 | 995 |
public: |
1000 | 996 |
|
1001 | 997 |
/// Constructor. |
1002 | 998 |
BfsWizard() : TR() {} |
1003 | 999 |
|
1004 | 1000 |
/// Constructor that requires parameters. |
1005 | 1001 |
|
1006 | 1002 |
/// Constructor that requires parameters. |
1007 | 1003 |
/// These parameters will be the default values for the traits class. |
1008 | 1004 |
/// \param g The digraph the algorithm runs on. |
1009 | 1005 |
BfsWizard(const Digraph &g) : |
1010 | 1006 |
TR(g) {} |
1011 | 1007 |
|
1012 | 1008 |
///Copy constructor |
1013 | 1009 |
BfsWizard(const TR &b) : TR(b) {} |
1014 | 1010 |
|
1015 | 1011 |
~BfsWizard() {} |
1016 | 1012 |
|
1017 | 1013 |
///Runs BFS algorithm from the given source node. |
1018 | 1014 |
|
1019 | 1015 |
///This method runs BFS algorithm from node \c s |
1020 | 1016 |
///in order to compute the shortest path to each node. |
1021 | 1017 |
void run(Node s) |
1022 | 1018 |
{ |
1023 | 1019 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
1024 | 1020 |
if (Base::_pred) |
1025 | 1021 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1026 | 1022 |
if (Base::_dist) |
1027 | 1023 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1028 | 1024 |
if (Base::_reached) |
1029 | 1025 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
1030 | 1026 |
if (Base::_processed) |
1031 | 1027 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1032 | 1028 |
if (s!=INVALID) |
1033 | 1029 |
alg.run(s); |
1034 | 1030 |
else |
1035 | 1031 |
alg.run(); |
1036 | 1032 |
} |
1037 | 1033 |
|
1038 | 1034 |
///Finds the shortest path between \c s and \c t. |
1039 | 1035 |
|
1040 | 1036 |
///This method runs BFS algorithm from node \c s |
1041 | 1037 |
///in order to compute the shortest path to node \c t |
1042 | 1038 |
///(it stops searching when \c t is processed). |
1043 | 1039 |
/// |
1044 | 1040 |
///\return \c true if \c t is reachable form \c s. |
1045 | 1041 |
bool run(Node s, Node t) |
1046 | 1042 |
{ |
1047 | 1043 |
if (s==INVALID || t==INVALID) throw UninitializedParameter(); |
1048 | 1044 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
1049 | 1045 |
if (Base::_pred) |
1050 | 1046 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1051 | 1047 |
if (Base::_dist) |
1052 | 1048 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1053 | 1049 |
if (Base::_reached) |
1054 | 1050 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
1055 | 1051 |
if (Base::_processed) |
1056 | 1052 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1057 | 1053 |
alg.run(s,t); |
1058 | 1054 |
if (Base::_path) |
1059 | 1055 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
1060 | 1056 |
if (Base::_di) |
1061 | 1057 |
*Base::_di = alg.dist(t); |
1062 | 1058 |
return alg.reached(t); |
1063 | 1059 |
} |
1064 | 1060 |
|
1065 | 1061 |
///Runs BFS algorithm to visit all nodes in the digraph. |
1066 | 1062 |
|
1067 | 1063 |
///This method runs BFS algorithm in order to compute |
1068 | 1064 |
///the shortest path to each node. |
1069 | 1065 |
void run() |
1070 | 1066 |
{ |
1071 | 1067 |
run(INVALID); |
1072 | 1068 |
} |
1073 | 1069 |
|
1074 | 1070 |
template<class T> |
1075 | 1071 |
struct SetPredMapBase : public Base { |
1076 | 1072 |
typedef T PredMap; |
1077 | 1073 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1078 | 1074 |
SetPredMapBase(const TR &b) : TR(b) {} |
1079 | 1075 |
}; |
1080 | 1076 |
///\brief \ref named-func-param "Named parameter" |
1081 | 1077 |
///for setting \ref PredMap object. |
1082 | 1078 |
/// |
1083 | 1079 |
///\ref named-func-param "Named parameter" |
1084 | 1080 |
///for setting \ref PredMap object. |
1085 | 1081 |
template<class T> |
1086 | 1082 |
BfsWizard<SetPredMapBase<T> > predMap(const T &t) |
1087 | 1083 |
{ |
1088 | 1084 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1089 | 1085 |
return BfsWizard<SetPredMapBase<T> >(*this); |
1090 | 1086 |
} |
1091 | 1087 |
|
1092 | 1088 |
template<class T> |
1093 | 1089 |
struct SetReachedMapBase : public Base { |
1094 | 1090 |
typedef T ReachedMap; |
1095 | 1091 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
1096 | 1092 |
SetReachedMapBase(const TR &b) : TR(b) {} |
1097 | 1093 |
}; |
1098 | 1094 |
///\brief \ref named-func-param "Named parameter" |
1099 | 1095 |
///for setting \ref ReachedMap object. |
1100 | 1096 |
/// |
1101 | 1097 |
/// \ref named-func-param "Named parameter" |
1102 | 1098 |
///for setting \ref ReachedMap object. |
1103 | 1099 |
template<class T> |
1104 | 1100 |
BfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
1105 | 1101 |
{ |
1106 | 1102 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1107 | 1103 |
return BfsWizard<SetReachedMapBase<T> >(*this); |
1108 | 1104 |
} |
1109 | 1105 |
|
1110 | 1106 |
template<class T> |
1111 | 1107 |
struct SetDistMapBase : public Base { |
1112 | 1108 |
typedef T DistMap; |
1113 | 1109 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1114 | 1110 |
SetDistMapBase(const TR &b) : TR(b) {} |
1115 | 1111 |
}; |
1116 | 1112 |
///\brief \ref named-func-param "Named parameter" |
1117 | 1113 |
///for setting \ref DistMap object. |
1118 | 1114 |
/// |
1119 | 1115 |
/// \ref named-func-param "Named parameter" |
1120 | 1116 |
///for setting \ref DistMap object. |
1121 | 1117 |
template<class T> |
1122 | 1118 |
BfsWizard<SetDistMapBase<T> > distMap(const T &t) |
1123 | 1119 |
{ |
1124 | 1120 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1125 | 1121 |
return BfsWizard<SetDistMapBase<T> >(*this); |
1126 | 1122 |
} |
1127 | 1123 |
|
1128 | 1124 |
template<class T> |
1129 | 1125 |
struct SetProcessedMapBase : public Base { |
1130 | 1126 |
typedef T ProcessedMap; |
1131 | 1127 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1132 | 1128 |
SetProcessedMapBase(const TR &b) : TR(b) {} |
1133 | 1129 |
}; |
1134 | 1130 |
///\brief \ref named-func-param "Named parameter" |
1135 | 1131 |
///for setting \ref ProcessedMap object. |
1136 | 1132 |
/// |
1137 | 1133 |
/// \ref named-func-param "Named parameter" |
1138 | 1134 |
///for setting \ref ProcessedMap object. |
1139 | 1135 |
template<class T> |
1140 | 1136 |
BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
1141 | 1137 |
{ |
1142 | 1138 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1143 | 1139 |
return BfsWizard<SetProcessedMapBase<T> >(*this); |
1144 | 1140 |
} |
1145 | 1141 |
|
1146 | 1142 |
template<class T> |
1147 | 1143 |
struct SetPathBase : public Base { |
1148 | 1144 |
typedef T Path; |
1149 | 1145 |
SetPathBase(const TR &b) : TR(b) {} |
1150 | 1146 |
}; |
1151 | 1147 |
///\brief \ref named-func-param "Named parameter" |
1152 | 1148 |
///for getting the shortest path to the target node. |
1153 | 1149 |
/// |
1154 | 1150 |
///\ref named-func-param "Named parameter" |
1155 | 1151 |
///for getting the shortest path to the target node. |
1156 | 1152 |
template<class T> |
1157 | 1153 |
BfsWizard<SetPathBase<T> > path(const T &t) |
1158 | 1154 |
{ |
1159 | 1155 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1160 | 1156 |
return BfsWizard<SetPathBase<T> >(*this); |
1161 | 1157 |
} |
1162 | 1158 |
|
1163 | 1159 |
///\brief \ref named-func-param "Named parameter" |
1164 | 1160 |
///for getting the distance of the target node. |
1165 | 1161 |
/// |
1166 | 1162 |
///\ref named-func-param "Named parameter" |
1167 | 1163 |
///for getting the distance of the target node. |
1168 | 1164 |
BfsWizard dist(const int &d) |
1169 | 1165 |
{ |
1170 | 1166 |
Base::_di=const_cast<int*>(&d); |
1171 | 1167 |
return *this; |
1172 | 1168 |
} |
1173 | 1169 |
|
1174 | 1170 |
}; |
1175 | 1171 |
|
1176 | 1172 |
///Function-type interface for BFS algorithm. |
1177 | 1173 |
|
1178 | 1174 |
/// \ingroup search |
1179 | 1175 |
///Function-type interface for BFS algorithm. |
1180 | 1176 |
/// |
1181 | 1177 |
///This function also has several \ref named-func-param "named parameters", |
1182 | 1178 |
///they are declared as the members of class \ref BfsWizard. |
1183 | 1179 |
///The following examples show how to use these parameters. |
1184 | 1180 |
///\code |
1185 | 1181 |
/// // Compute shortest path from node s to each node |
1186 | 1182 |
/// bfs(g).predMap(preds).distMap(dists).run(s); |
1187 | 1183 |
/// |
1188 | 1184 |
/// // Compute shortest path from s to t |
1189 | 1185 |
/// bool reached = bfs(g).path(p).dist(d).run(s,t); |
1190 | 1186 |
///\endcode |
1191 | 1187 |
///\warning Don't forget to put the \ref BfsWizard::run() "run()" |
1192 | 1188 |
///to the end of the parameter list. |
1193 | 1189 |
///\sa BfsWizard |
1194 | 1190 |
///\sa Bfs |
1195 | 1191 |
template<class GR> |
1196 | 1192 |
BfsWizard<BfsWizardBase<GR> > |
1197 | 1193 |
bfs(const GR &digraph) |
1198 | 1194 |
{ |
1199 | 1195 |
return BfsWizard<BfsWizardBase<GR> >(digraph); |
1200 | 1196 |
} |
1201 | 1197 |
|
1202 | 1198 |
#ifdef DOXYGEN |
1203 | 1199 |
/// \brief Visitor class for BFS. |
1204 | 1200 |
/// |
1205 | 1201 |
/// This class defines the interface of the BfsVisit events, and |
1206 | 1202 |
/// it could be the base of a real visitor class. |
1207 | 1203 |
template <typename _Digraph> |
1208 | 1204 |
struct BfsVisitor { |
1209 | 1205 |
typedef _Digraph Digraph; |
1210 | 1206 |
typedef typename Digraph::Arc Arc; |
1211 | 1207 |
typedef typename Digraph::Node Node; |
1212 | 1208 |
/// \brief Called for the source node(s) of the BFS. |
1213 | 1209 |
/// |
1214 | 1210 |
/// This function is called for the source node(s) of the BFS. |
1215 | 1211 |
void start(const Node& node) {} |
1216 | 1212 |
/// \brief Called when a node is reached first time. |
1217 | 1213 |
/// |
1218 | 1214 |
/// This function is called when a node is reached first time. |
1219 | 1215 |
void reach(const Node& node) {} |
1220 | 1216 |
/// \brief Called when a node is processed. |
1221 | 1217 |
/// |
1222 | 1218 |
/// This function is called when a node is processed. |
1223 | 1219 |
void process(const Node& node) {} |
1224 | 1220 |
/// \brief Called when an arc reaches a new node. |
1225 | 1221 |
/// |
1226 | 1222 |
/// This function is called when the BFS finds an arc whose target node |
1227 | 1223 |
/// is not reached yet. |
1228 | 1224 |
void discover(const Arc& arc) {} |
1229 | 1225 |
/// \brief Called when an arc is examined but its target node is |
1230 | 1226 |
/// already discovered. |
1231 | 1227 |
/// |
1232 | 1228 |
/// This function is called when an arc is examined but its target node is |
1233 | 1229 |
/// already discovered. |
1234 | 1230 |
void examine(const Arc& arc) {} |
1235 | 1231 |
}; |
1236 | 1232 |
#else |
1237 | 1233 |
template <typename _Digraph> |
1238 | 1234 |
struct BfsVisitor { |
1239 | 1235 |
typedef _Digraph Digraph; |
1240 | 1236 |
typedef typename Digraph::Arc Arc; |
1241 | 1237 |
typedef typename Digraph::Node Node; |
1242 | 1238 |
void start(const Node&) {} |
1243 | 1239 |
void reach(const Node&) {} |
1244 | 1240 |
void process(const Node&) {} |
1245 | 1241 |
void discover(const Arc&) {} |
1246 | 1242 |
void examine(const Arc&) {} |
1247 | 1243 |
|
1248 | 1244 |
template <typename _Visitor> |
1249 | 1245 |
struct Constraints { |
1250 | 1246 |
void constraints() { |
1251 | 1247 |
Arc arc; |
1252 | 1248 |
Node node; |
1253 | 1249 |
visitor.start(node); |
1254 | 1250 |
visitor.reach(node); |
1255 | 1251 |
visitor.process(node); |
1256 | 1252 |
visitor.discover(arc); |
1257 | 1253 |
visitor.examine(arc); |
1258 | 1254 |
} |
1259 | 1255 |
_Visitor& visitor; |
1260 | 1256 |
}; |
1261 | 1257 |
}; |
1262 | 1258 |
#endif |
1263 | 1259 |
|
1264 | 1260 |
/// \brief Default traits class of BfsVisit class. |
1265 | 1261 |
/// |
1266 | 1262 |
/// Default traits class of BfsVisit class. |
1267 | 1263 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1268 | 1264 |
template<class _Digraph> |
1269 | 1265 |
struct BfsVisitDefaultTraits { |
1270 | 1266 |
|
1271 | 1267 |
/// \brief The type of the digraph the algorithm runs on. |
1272 | 1268 |
typedef _Digraph Digraph; |
1273 | 1269 |
|
1274 | 1270 |
/// \brief The type of the map that indicates which nodes are reached. |
1275 | 1271 |
/// |
1276 | 1272 |
/// The type of the map that indicates which nodes are reached. |
1277 | 1273 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
1278 | 1274 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1279 | 1275 |
|
1280 | 1276 |
/// \brief Instantiates a \ref ReachedMap. |
1281 | 1277 |
/// |
1282 | 1278 |
/// This function instantiates a \ref ReachedMap. |
1283 | 1279 |
/// \param digraph is the digraph, to which |
1284 | 1280 |
/// we would like to define the \ref ReachedMap. |
1285 | 1281 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1286 | 1282 |
return new ReachedMap(digraph); |
1287 | 1283 |
} |
1288 | 1284 |
|
1289 | 1285 |
}; |
1290 | 1286 |
|
1291 | 1287 |
/// \ingroup search |
1292 | 1288 |
/// |
1293 | 1289 |
/// \brief %BFS algorithm class with visitor interface. |
1294 | 1290 |
/// |
1295 | 1291 |
/// This class provides an efficient implementation of the %BFS algorithm |
1296 | 1292 |
/// with visitor interface. |
1297 | 1293 |
/// |
1298 | 1294 |
/// The %BfsVisit class provides an alternative interface to the Bfs |
1299 | 1295 |
/// class. It works with callback mechanism, the BfsVisit object calls |
1300 | 1296 |
/// the member functions of the \c Visitor class on every BFS event. |
1301 | 1297 |
/// |
1302 | 1298 |
/// This interface of the BFS algorithm should be used in special cases |
1303 | 1299 |
/// when extra actions have to be performed in connection with certain |
1304 | 1300 |
/// events of the BFS algorithm. Otherwise consider to use Bfs or bfs() |
1305 | 1301 |
/// instead. |
1306 | 1302 |
/// |
1307 | 1303 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1308 | 1304 |
/// The default value is |
1309 | 1305 |
/// \ref ListDigraph. The value of _Digraph is not used directly by |
1310 | 1306 |
/// \ref BfsVisit, it is only passed to \ref BfsVisitDefaultTraits. |
1311 | 1307 |
/// \tparam _Visitor The Visitor type that is used by the algorithm. |
1312 | 1308 |
/// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty visitor, which |
1313 | 1309 |
/// does not observe the BFS events. If you want to observe the BFS |
1314 | 1310 |
/// events, you should implement your own visitor class. |
1315 | 1311 |
/// \tparam _Traits Traits class to set various data types used by the |
1316 | 1312 |
/// algorithm. The default traits class is |
1317 | 1313 |
/// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>". |
1318 | 1314 |
/// See \ref BfsVisitDefaultTraits for the documentation of |
1319 | 1315 |
/// a BFS visit traits class. |
1320 | 1316 |
#ifdef DOXYGEN |
1321 | 1317 |
template <typename _Digraph, typename _Visitor, typename _Traits> |
1322 | 1318 |
#else |
1323 | 1319 |
template <typename _Digraph = ListDigraph, |
1324 | 1320 |
typename _Visitor = BfsVisitor<_Digraph>, |
1325 | 1321 |
typename _Traits = BfsDefaultTraits<_Digraph> > |
1326 | 1322 |
#endif |
1327 | 1323 |
class BfsVisit { |
1328 | 1324 |
public: |
1329 | 1325 |
|
1330 | 1326 |
/// \brief \ref Exception for uninitialized parameters. |
1331 | 1327 |
/// |
1332 | 1328 |
/// This error represents problems in the initialization |
1333 | 1329 |
/// of the parameters of the algorithm. |
1334 | 1330 |
class UninitializedParameter : public lemon::UninitializedParameter { |
1335 | 1331 |
public: |
1336 | 1332 |
virtual const char* what() const throw() |
1337 | 1333 |
{ |
1338 | 1334 |
return "lemon::BfsVisit::UninitializedParameter"; |
1339 | 1335 |
} |
1340 | 1336 |
}; |
1341 | 1337 |
|
1342 | 1338 |
///The traits class. |
1343 | 1339 |
typedef _Traits Traits; |
1344 | 1340 |
|
1345 | 1341 |
///The type of the digraph the algorithm runs on. |
1346 | 1342 |
typedef typename Traits::Digraph Digraph; |
1347 | 1343 |
|
1348 | 1344 |
///The visitor type used by the algorithm. |
1349 | 1345 |
typedef _Visitor Visitor; |
1350 | 1346 |
|
1351 | 1347 |
///The type of the map that indicates which nodes are reached. |
1352 | 1348 |
typedef typename Traits::ReachedMap ReachedMap; |
1353 | 1349 |
|
1354 | 1350 |
private: |
1355 | 1351 |
|
1356 | 1352 |
typedef typename Digraph::Node Node; |
1357 | 1353 |
typedef typename Digraph::NodeIt NodeIt; |
1358 | 1354 |
typedef typename Digraph::Arc Arc; |
1359 | 1355 |
typedef typename Digraph::OutArcIt OutArcIt; |
1360 | 1356 |
|
1361 | 1357 |
//Pointer to the underlying digraph. |
1362 | 1358 |
const Digraph *_digraph; |
1363 | 1359 |
//Pointer to the visitor object. |
1364 | 1360 |
Visitor *_visitor; |
1365 | 1361 |
//Pointer to the map of reached status of the nodes. |
1366 | 1362 |
ReachedMap *_reached; |
1367 | 1363 |
//Indicates if _reached is locally allocated (true) or not. |
1368 | 1364 |
bool local_reached; |
1369 | 1365 |
|
1370 | 1366 |
std::vector<typename Digraph::Node> _list; |
1371 | 1367 |
int _list_front, _list_back; |
1372 | 1368 |
|
1373 |
///Creates the maps if necessary. |
|
1374 |
///\todo Better memory allocation (instead of new). |
|
1369 |
//Creates the maps if necessary. |
|
1375 | 1370 |
void create_maps() { |
1376 | 1371 |
if(!_reached) { |
1377 | 1372 |
local_reached = true; |
1378 | 1373 |
_reached = Traits::createReachedMap(*_digraph); |
1379 | 1374 |
} |
1380 | 1375 |
} |
1381 | 1376 |
|
1382 | 1377 |
protected: |
1383 | 1378 |
|
1384 | 1379 |
BfsVisit() {} |
1385 | 1380 |
|
1386 | 1381 |
public: |
1387 | 1382 |
|
1388 | 1383 |
typedef BfsVisit Create; |
1389 | 1384 |
|
1390 | 1385 |
/// \name Named template parameters |
1391 | 1386 |
|
1392 | 1387 |
///@{ |
1393 | 1388 |
template <class T> |
1394 | 1389 |
struct SetReachedMapTraits : public Traits { |
1395 | 1390 |
typedef T ReachedMap; |
1396 | 1391 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1397 | 1392 |
throw UninitializedParameter(); |
1398 | 1393 |
} |
1399 | 1394 |
}; |
1400 | 1395 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1401 | 1396 |
/// ReachedMap type. |
1402 | 1397 |
/// |
1403 | 1398 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
1404 | 1399 |
template <class T> |
1405 | 1400 |
struct SetReachedMap : public BfsVisit< Digraph, Visitor, |
1406 | 1401 |
SetReachedMapTraits<T> > { |
1407 | 1402 |
typedef BfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
1408 | 1403 |
}; |
1409 | 1404 |
///@} |
1410 | 1405 |
|
1411 | 1406 |
public: |
1412 | 1407 |
|
1413 | 1408 |
/// \brief Constructor. |
1414 | 1409 |
/// |
1415 | 1410 |
/// Constructor. |
1416 | 1411 |
/// |
1417 | 1412 |
/// \param digraph The digraph the algorithm runs on. |
1418 | 1413 |
/// \param visitor The visitor object of the algorithm. |
1419 | 1414 |
BfsVisit(const Digraph& digraph, Visitor& visitor) |
1420 | 1415 |
: _digraph(&digraph), _visitor(&visitor), |
1421 | 1416 |
_reached(0), local_reached(false) {} |
1422 | 1417 |
|
1423 | 1418 |
/// \brief Destructor. |
1424 | 1419 |
~BfsVisit() { |
1425 | 1420 |
if(local_reached) delete _reached; |
1426 | 1421 |
} |
1427 | 1422 |
|
1428 | 1423 |
/// \brief Sets the map that indicates which nodes are reached. |
1429 | 1424 |
/// |
1430 | 1425 |
/// Sets the map that indicates which nodes are reached. |
1431 | 1426 |
/// If you don't use this function before calling \ref run(), |
1432 | 1427 |
/// it will allocate one. The destructor deallocates this |
1433 | 1428 |
/// automatically allocated map, of course. |
1434 | 1429 |
/// \return <tt> (*this) </tt> |
1435 | 1430 |
BfsVisit &reachedMap(ReachedMap &m) { |
1436 | 1431 |
if(local_reached) { |
1437 | 1432 |
delete _reached; |
1438 | 1433 |
local_reached = false; |
1439 | 1434 |
} |
1440 | 1435 |
_reached = &m; |
1441 | 1436 |
return *this; |
1442 | 1437 |
} |
1443 | 1438 |
|
1444 | 1439 |
public: |
1445 | 1440 |
|
1446 | 1441 |
/// \name Execution control |
1447 | 1442 |
/// The simplest way to execute the algorithm is to use |
1448 | 1443 |
/// one of the member functions called \ref lemon::BfsVisit::run() |
1449 | 1444 |
/// "run()". |
1450 | 1445 |
/// \n |
1451 | 1446 |
/// If you need more control on the execution, first you must call |
1452 | 1447 |
/// \ref lemon::BfsVisit::init() "init()", then you can add several |
1453 | 1448 |
/// source nodes with \ref lemon::BfsVisit::addSource() "addSource()". |
1454 | 1449 |
/// Finally \ref lemon::BfsVisit::start() "start()" will perform the |
1455 | 1450 |
/// actual path computation. |
1456 | 1451 |
|
1457 | 1452 |
/// @{ |
1458 | 1453 |
|
1459 | 1454 |
/// \brief Initializes the internal data structures. |
1460 | 1455 |
/// |
1461 | 1456 |
/// Initializes the internal data structures. |
1462 | 1457 |
void init() { |
1463 | 1458 |
create_maps(); |
1464 | 1459 |
_list.resize(countNodes(*_digraph)); |
1465 | 1460 |
_list_front = _list_back = -1; |
1466 | 1461 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1467 | 1462 |
_reached->set(u, false); |
1468 | 1463 |
} |
1469 | 1464 |
} |
1470 | 1465 |
|
1471 | 1466 |
/// \brief Adds a new source node. |
1472 | 1467 |
/// |
1473 | 1468 |
/// Adds a new source node to the set of nodes to be processed. |
1474 | 1469 |
void addSource(Node s) { |
1475 | 1470 |
if(!(*_reached)[s]) { |
1476 | 1471 |
_reached->set(s,true); |
1477 | 1472 |
_visitor->start(s); |
1478 | 1473 |
_visitor->reach(s); |
1479 | 1474 |
_list[++_list_back] = s; |
1480 | 1475 |
} |
1481 | 1476 |
} |
1482 | 1477 |
|
1483 | 1478 |
/// \brief Processes the next node. |
1484 | 1479 |
/// |
1485 | 1480 |
/// Processes the next node. |
1486 | 1481 |
/// |
1487 | 1482 |
/// \return The processed node. |
1488 | 1483 |
/// |
1489 | 1484 |
/// \pre The queue must not be empty. |
1490 | 1485 |
Node processNextNode() { |
1491 | 1486 |
Node n = _list[++_list_front]; |
1492 | 1487 |
_visitor->process(n); |
1493 | 1488 |
Arc e; |
1494 | 1489 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1495 | 1490 |
Node m = _digraph->target(e); |
1496 | 1491 |
if (!(*_reached)[m]) { |
1497 | 1492 |
_visitor->discover(e); |
1498 | 1493 |
_visitor->reach(m); |
1499 | 1494 |
_reached->set(m, true); |
1500 | 1495 |
_list[++_list_back] = m; |
1501 | 1496 |
} else { |
1502 | 1497 |
_visitor->examine(e); |
1503 | 1498 |
} |
1504 | 1499 |
} |
1505 | 1500 |
return n; |
1506 | 1501 |
} |
1507 | 1502 |
|
1508 | 1503 |
/// \brief Processes the next node. |
1509 | 1504 |
/// |
1510 | 1505 |
/// Processes the next node and checks if the given target node |
1511 | 1506 |
/// is reached. If the target node is reachable from the processed |
1512 | 1507 |
/// node, then the \c reach parameter will be set to \c true. |
1513 | 1508 |
/// |
1514 | 1509 |
/// \param target The target node. |
1515 | 1510 |
/// \retval reach Indicates if the target node is reached. |
1516 | 1511 |
/// It should be initially \c false. |
1517 | 1512 |
/// |
1518 | 1513 |
/// \return The processed node. |
1519 | 1514 |
/// |
1520 | 1515 |
/// \pre The queue must not be empty. |
1521 | 1516 |
Node processNextNode(Node target, bool& reach) { |
1522 | 1517 |
Node n = _list[++_list_front]; |
1523 | 1518 |
_visitor->process(n); |
1524 | 1519 |
Arc e; |
1525 | 1520 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1526 | 1521 |
Node m = _digraph->target(e); |
1527 | 1522 |
if (!(*_reached)[m]) { |
1528 | 1523 |
_visitor->discover(e); |
1529 | 1524 |
_visitor->reach(m); |
1530 | 1525 |
_reached->set(m, true); |
1531 | 1526 |
_list[++_list_back] = m; |
1532 | 1527 |
reach = reach || (target == m); |
1533 | 1528 |
} else { |
1534 | 1529 |
_visitor->examine(e); |
1535 | 1530 |
} |
1536 | 1531 |
} |
1537 | 1532 |
return n; |
1538 | 1533 |
} |
1539 | 1534 |
|
1540 | 1535 |
/// \brief Processes the next node. |
1541 | 1536 |
/// |
1542 | 1537 |
/// Processes the next node and checks if at least one of reached |
1543 | 1538 |
/// nodes has \c true value in the \c nm node map. If one node |
1544 | 1539 |
/// with \c true value is reachable from the processed node, then the |
1545 | 1540 |
/// \c rnode parameter will be set to the first of such nodes. |
1546 | 1541 |
/// |
1547 | 1542 |
/// \param nm A \c bool (or convertible) node map that indicates the |
1548 | 1543 |
/// possible targets. |
1549 | 1544 |
/// \retval rnode The reached target node. |
1550 | 1545 |
/// It should be initially \c INVALID. |
1551 | 1546 |
/// |
1552 | 1547 |
/// \return The processed node. |
1553 | 1548 |
/// |
1554 | 1549 |
/// \pre The queue must not be empty. |
1555 | 1550 |
template <typename NM> |
1556 | 1551 |
Node processNextNode(const NM& nm, Node& rnode) { |
1557 | 1552 |
Node n = _list[++_list_front]; |
1558 | 1553 |
_visitor->process(n); |
1559 | 1554 |
Arc e; |
1560 | 1555 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1561 | 1556 |
Node m = _digraph->target(e); |
1562 | 1557 |
if (!(*_reached)[m]) { |
1563 | 1558 |
_visitor->discover(e); |
1564 | 1559 |
_visitor->reach(m); |
1565 | 1560 |
_reached->set(m, true); |
1566 | 1561 |
_list[++_list_back] = m; |
1567 | 1562 |
if (nm[m] && rnode == INVALID) rnode = m; |
1568 | 1563 |
} else { |
1569 | 1564 |
_visitor->examine(e); |
1570 | 1565 |
} |
1571 | 1566 |
} |
1572 | 1567 |
return n; |
1573 | 1568 |
} |
1574 | 1569 |
|
1575 | 1570 |
/// \brief The next node to be processed. |
1576 | 1571 |
/// |
1577 | 1572 |
/// Returns the next node to be processed or \c INVALID if the queue |
1578 | 1573 |
/// is empty. |
1579 | 1574 |
Node nextNode() const { |
1580 | 1575 |
return _list_front != _list_back ? _list[_list_front + 1] : INVALID; |
1581 | 1576 |
} |
1582 | 1577 |
|
1583 | 1578 |
/// \brief Returns \c false if there are nodes |
1584 | 1579 |
/// to be processed. |
1585 | 1580 |
/// |
1586 | 1581 |
/// Returns \c false if there are nodes |
1587 | 1582 |
/// to be processed in the queue. |
1588 | 1583 |
bool emptyQueue() const { return _list_front == _list_back; } |
1589 | 1584 |
|
1590 | 1585 |
/// \brief Returns the number of the nodes to be processed. |
1591 | 1586 |
/// |
1592 | 1587 |
/// Returns the number of the nodes to be processed in the queue. |
1593 | 1588 |
int queueSize() const { return _list_back - _list_front; } |
1594 | 1589 |
|
1595 | 1590 |
/// \brief Executes the algorithm. |
1596 | 1591 |
/// |
1597 | 1592 |
/// Executes the algorithm. |
1598 | 1593 |
/// |
1599 | 1594 |
/// This method runs the %BFS algorithm from the root node(s) |
1600 | 1595 |
/// in order to compute the shortest path to each node. |
1601 | 1596 |
/// |
1602 | 1597 |
/// The algorithm computes |
1603 | 1598 |
/// - the shortest path tree (forest), |
1604 | 1599 |
/// - the distance of each node from the root(s). |
1605 | 1600 |
/// |
1606 | 1601 |
/// \pre init() must be called and at least one root node should be added |
1607 | 1602 |
/// with addSource() before using this function. |
1608 | 1603 |
/// |
1609 | 1604 |
/// \note <tt>b.start()</tt> is just a shortcut of the following code. |
1610 | 1605 |
/// \code |
1611 | 1606 |
/// while ( !b.emptyQueue() ) { |
1612 | 1607 |
/// b.processNextNode(); |
1613 | 1608 |
/// } |
1614 | 1609 |
/// \endcode |
1615 | 1610 |
void start() { |
1616 | 1611 |
while ( !emptyQueue() ) processNextNode(); |
1617 | 1612 |
} |
1618 | 1613 |
|
1619 | 1614 |
/// \brief Executes the algorithm until the given target node is reached. |
1620 | 1615 |
/// |
1621 | 1616 |
/// Executes the algorithm until the given target node is reached. |
1622 | 1617 |
/// |
1623 | 1618 |
/// This method runs the %BFS algorithm from the root node(s) |
1624 | 1619 |
/// in order to compute the shortest path to \c t. |
1625 | 1620 |
/// |
1626 | 1621 |
/// The algorithm computes |
1627 | 1622 |
/// - the shortest path to \c t, |
1628 | 1623 |
/// - the distance of \c t from the root(s). |
1629 | 1624 |
/// |
1630 | 1625 |
/// \pre init() must be called and at least one root node should be |
1631 | 1626 |
/// added with addSource() before using this function. |
1632 | 1627 |
/// |
1633 | 1628 |
/// \note <tt>b.start(t)</tt> is just a shortcut of the following code. |
1634 | 1629 |
/// \code |
1635 | 1630 |
/// bool reach = false; |
1636 | 1631 |
/// while ( !b.emptyQueue() && !reach ) { |
1637 | 1632 |
/// b.processNextNode(t, reach); |
1638 | 1633 |
/// } |
1639 | 1634 |
/// \endcode |
1640 | 1635 |
void start(Node t) { |
1641 | 1636 |
bool reach = false; |
1642 | 1637 |
while ( !emptyQueue() && !reach ) processNextNode(t, reach); |
1643 | 1638 |
} |
1644 | 1639 |
|
1645 | 1640 |
/// \brief Executes the algorithm until a condition is met. |
1646 | 1641 |
/// |
1647 | 1642 |
/// Executes the algorithm until a condition is met. |
1648 | 1643 |
/// |
1649 | 1644 |
/// This method runs the %BFS algorithm from the root node(s) in |
1650 | 1645 |
/// order to compute the shortest path to a node \c v with |
1651 | 1646 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
1652 | 1647 |
/// |
1653 | 1648 |
/// \param nm must be a bool (or convertible) node map. The |
1654 | 1649 |
/// algorithm will stop when it reaches a node \c v with |
1655 | 1650 |
/// <tt>nm[v]</tt> true. |
1656 | 1651 |
/// |
1657 | 1652 |
/// \return The reached node \c v with <tt>nm[v]</tt> true or |
1658 | 1653 |
/// \c INVALID if no such node was found. |
1659 | 1654 |
/// |
1660 | 1655 |
/// \pre init() must be called and at least one root node should be |
1661 | 1656 |
/// added with addSource() before using this function. |
1662 | 1657 |
/// |
1663 | 1658 |
/// \note <tt>b.start(nm)</tt> is just a shortcut of the following code. |
1664 | 1659 |
/// \code |
1665 | 1660 |
/// Node rnode = INVALID; |
1666 | 1661 |
/// while ( !b.emptyQueue() && rnode == INVALID ) { |
1667 | 1662 |
/// b.processNextNode(nm, rnode); |
1668 | 1663 |
/// } |
1669 | 1664 |
/// return rnode; |
1670 | 1665 |
/// \endcode |
1671 | 1666 |
template <typename NM> |
1672 | 1667 |
Node start(const NM &nm) { |
1673 | 1668 |
Node rnode = INVALID; |
1674 | 1669 |
while ( !emptyQueue() && rnode == INVALID ) { |
1675 | 1670 |
processNextNode(nm, rnode); |
1676 | 1671 |
} |
1677 | 1672 |
return rnode; |
1678 | 1673 |
} |
1679 | 1674 |
|
1680 | 1675 |
/// \brief Runs the algorithm from the given source node. |
1681 | 1676 |
/// |
1682 | 1677 |
/// This method runs the %BFS algorithm from node \c s |
1683 | 1678 |
/// in order to compute the shortest path to each node. |
1684 | 1679 |
/// |
1685 | 1680 |
/// The algorithm computes |
1686 | 1681 |
/// - the shortest path tree, |
1687 | 1682 |
/// - the distance of each node from the root. |
1688 | 1683 |
/// |
1689 | 1684 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
1690 | 1685 |
///\code |
1691 | 1686 |
/// b.init(); |
1692 | 1687 |
/// b.addSource(s); |
1693 | 1688 |
/// b.start(); |
1694 | 1689 |
///\endcode |
1695 | 1690 |
void run(Node s) { |
1696 | 1691 |
init(); |
1697 | 1692 |
addSource(s); |
1698 | 1693 |
start(); |
1699 | 1694 |
} |
1700 | 1695 |
|
1701 | 1696 |
/// \brief Finds the shortest path between \c s and \c t. |
1702 | 1697 |
/// |
1703 | 1698 |
/// This method runs the %BFS algorithm from node \c s |
1704 | 1699 |
/// in order to compute the shortest path to node \c t |
1705 | 1700 |
/// (it stops searching when \c t is processed). |
1706 | 1701 |
/// |
1707 | 1702 |
/// \return \c true if \c t is reachable form \c s. |
1708 | 1703 |
/// |
1709 | 1704 |
/// \note Apart from the return value, <tt>b.run(s,t)</tt> is just a |
1710 | 1705 |
/// shortcut of the following code. |
1711 | 1706 |
///\code |
1712 | 1707 |
/// b.init(); |
1713 | 1708 |
/// b.addSource(s); |
1714 | 1709 |
/// b.start(t); |
1715 | 1710 |
///\endcode |
1716 | 1711 |
bool run(Node s,Node t) { |
1717 | 1712 |
init(); |
1718 | 1713 |
addSource(s); |
1719 | 1714 |
start(t); |
1720 | 1715 |
return reached(t); |
1721 | 1716 |
} |
1722 | 1717 |
|
1723 | 1718 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
1724 | 1719 |
/// |
1725 | 1720 |
/// This method runs the %BFS algorithm in order to |
1726 | 1721 |
/// compute the shortest path to each node. |
1727 | 1722 |
/// |
1728 | 1723 |
/// The algorithm computes |
1729 | 1724 |
/// - the shortest path tree (forest), |
1730 | 1725 |
/// - the distance of each node from the root(s). |
1731 | 1726 |
/// |
1732 | 1727 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
1733 | 1728 |
///\code |
1734 | 1729 |
/// b.init(); |
1735 | 1730 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
1736 | 1731 |
/// if (!b.reached(n)) { |
1737 | 1732 |
/// b.addSource(n); |
1738 | 1733 |
/// b.start(); |
1739 | 1734 |
/// } |
1740 | 1735 |
/// } |
1741 | 1736 |
///\endcode |
1742 | 1737 |
void run() { |
1743 | 1738 |
init(); |
1744 | 1739 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1745 | 1740 |
if (!reached(it)) { |
1746 | 1741 |
addSource(it); |
1747 | 1742 |
start(); |
1748 | 1743 |
} |
1749 | 1744 |
} |
1750 | 1745 |
} |
1751 | 1746 |
|
1752 | 1747 |
///@} |
1753 | 1748 |
|
1754 | 1749 |
/// \name Query Functions |
1755 | 1750 |
/// The result of the %BFS algorithm can be obtained using these |
1756 | 1751 |
/// functions.\n |
1757 | 1752 |
/// Either \ref lemon::BfsVisit::run() "run()" or |
1758 | 1753 |
/// \ref lemon::BfsVisit::start() "start()" must be called before |
1759 | 1754 |
/// using them. |
1760 | 1755 |
///@{ |
1761 | 1756 |
|
1762 | 1757 |
/// \brief Checks if a node is reachable from the root(s). |
1763 | 1758 |
/// |
1764 | 1759 |
/// Returns \c true if \c v is reachable from the root(s). |
1765 | 1760 |
/// \pre Either \ref run() or \ref start() |
1766 | 1761 |
/// must be called before using this function. |
1767 | 1762 |
bool reached(Node v) { return (*_reached)[v]; } |
1768 | 1763 |
|
1769 | 1764 |
///@} |
1770 | 1765 |
|
1771 | 1766 |
}; |
1772 | 1767 |
|
1773 | 1768 |
} //END OF NAMESPACE LEMON |
1774 | 1769 |
|
1775 | 1770 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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_BITS_BASE_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_BASE_EXTENDER_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
#include <lemon/error.h> |
24 | 24 |
|
25 | 25 |
#include <lemon/bits/map_extender.h> |
26 | 26 |
#include <lemon/bits/default_map.h> |
27 | 27 |
|
28 | 28 |
#include <lemon/concept_check.h> |
29 | 29 |
#include <lemon/concepts/maps.h> |
30 | 30 |
|
31 | 31 |
///\ingroup digraphbits |
32 | 32 |
///\file |
33 | 33 |
///\brief Extenders for the digraph types |
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \ingroup digraphbits |
37 | 37 |
/// |
38 | 38 |
/// \brief BaseDigraph to BaseGraph extender |
39 | 39 |
template <typename Base> |
40 | 40 |
class UndirDigraphExtender : public Base { |
41 | 41 |
|
42 | 42 |
public: |
43 | 43 |
|
44 | 44 |
typedef Base Parent; |
45 | 45 |
typedef typename Parent::Arc Edge; |
46 | 46 |
typedef typename Parent::Node Node; |
47 | 47 |
|
48 | 48 |
typedef True UndirectedTag; |
49 | 49 |
|
50 | 50 |
class Arc : public Edge { |
51 | 51 |
friend class UndirDigraphExtender; |
52 | 52 |
|
53 | 53 |
protected: |
54 | 54 |
bool forward; |
55 | 55 |
|
56 | 56 |
Arc(const Edge &ue, bool _forward) : |
57 | 57 |
Edge(ue), forward(_forward) {} |
58 | 58 |
|
59 | 59 |
public: |
60 | 60 |
Arc() {} |
61 | 61 |
|
62 | 62 |
// Invalid arc constructor |
63 | 63 |
Arc(Invalid i) : Edge(i), forward(true) {} |
64 | 64 |
|
65 | 65 |
bool operator==(const Arc &that) const { |
66 | 66 |
return forward==that.forward && Edge(*this)==Edge(that); |
67 | 67 |
} |
68 | 68 |
bool operator!=(const Arc &that) const { |
69 | 69 |
return forward!=that.forward || Edge(*this)!=Edge(that); |
70 | 70 |
} |
71 | 71 |
bool operator<(const Arc &that) const { |
72 | 72 |
return forward<that.forward || |
73 | 73 |
(!(that.forward<forward) && Edge(*this)<Edge(that)); |
74 | 74 |
} |
75 | 75 |
}; |
76 | 76 |
|
77 | 77 |
/// First node of the edge |
78 | 78 |
Node u(const Edge &e) const { |
79 | 79 |
return Parent::source(e); |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
/// Source of the given arc |
83 | 83 |
Node source(const Arc &e) const { |
84 | 84 |
return e.forward ? Parent::source(e) : Parent::target(e); |
85 | 85 |
} |
86 | 86 |
|
87 | 87 |
/// Second node of the edge |
88 | 88 |
Node v(const Edge &e) const { |
89 | 89 |
return Parent::target(e); |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
/// Target of the given arc |
93 | 93 |
Node target(const Arc &e) const { |
94 | 94 |
return e.forward ? Parent::target(e) : Parent::source(e); |
95 | 95 |
} |
96 | 96 |
|
97 | 97 |
/// \brief Directed arc from an edge. |
98 | 98 |
/// |
99 | 99 |
/// Returns a directed arc corresponding to the specified edge. |
100 | 100 |
/// If the given bool is true, the first node of the given edge and |
101 | 101 |
/// the source node of the returned arc are the same. |
102 | 102 |
static Arc direct(const Edge &e, bool d) { |
103 | 103 |
return Arc(e, d); |
104 | 104 |
} |
105 | 105 |
|
106 | 106 |
/// Returns whether the given directed arc has the same orientation |
107 | 107 |
/// as the corresponding edge. |
108 |
/// |
|
109 |
/// \todo reference to the corresponding point of the undirected digraph |
|
110 |
/// concept. "What does the direction of an edge mean?" |
|
111 | 108 |
static bool direction(const Arc &a) { return a.forward; } |
112 | 109 |
|
113 | 110 |
using Parent::first; |
114 | 111 |
using Parent::next; |
115 | 112 |
|
116 | 113 |
void first(Arc &e) const { |
117 | 114 |
Parent::first(e); |
118 | 115 |
e.forward=true; |
119 | 116 |
} |
120 | 117 |
|
121 | 118 |
void next(Arc &e) const { |
122 | 119 |
if( e.forward ) { |
123 | 120 |
e.forward = false; |
124 | 121 |
} |
125 | 122 |
else { |
126 | 123 |
Parent::next(e); |
127 | 124 |
e.forward = true; |
128 | 125 |
} |
129 | 126 |
} |
130 | 127 |
|
131 | 128 |
void firstOut(Arc &e, const Node &n) const { |
132 | 129 |
Parent::firstIn(e,n); |
133 | 130 |
if( Edge(e) != INVALID ) { |
134 | 131 |
e.forward = false; |
135 | 132 |
} |
136 | 133 |
else { |
137 | 134 |
Parent::firstOut(e,n); |
138 | 135 |
e.forward = true; |
139 | 136 |
} |
140 | 137 |
} |
141 | 138 |
void nextOut(Arc &e) const { |
142 | 139 |
if( ! e.forward ) { |
143 | 140 |
Node n = Parent::target(e); |
144 | 141 |
Parent::nextIn(e); |
145 | 142 |
if( Edge(e) == INVALID ) { |
146 | 143 |
Parent::firstOut(e, n); |
147 | 144 |
e.forward = true; |
148 | 145 |
} |
149 | 146 |
} |
150 | 147 |
else { |
151 | 148 |
Parent::nextOut(e); |
152 | 149 |
} |
153 | 150 |
} |
154 | 151 |
|
155 | 152 |
void firstIn(Arc &e, const Node &n) const { |
156 | 153 |
Parent::firstOut(e,n); |
157 | 154 |
if( Edge(e) != INVALID ) { |
158 | 155 |
e.forward = false; |
159 | 156 |
} |
160 | 157 |
else { |
161 | 158 |
Parent::firstIn(e,n); |
162 | 159 |
e.forward = true; |
163 | 160 |
} |
164 | 161 |
} |
165 | 162 |
void nextIn(Arc &e) const { |
166 | 163 |
if( ! e.forward ) { |
167 | 164 |
Node n = Parent::source(e); |
168 | 165 |
Parent::nextOut(e); |
169 | 166 |
if( Edge(e) == INVALID ) { |
170 | 167 |
Parent::firstIn(e, n); |
171 | 168 |
e.forward = true; |
172 | 169 |
} |
173 | 170 |
} |
174 | 171 |
else { |
175 | 172 |
Parent::nextIn(e); |
176 | 173 |
} |
177 | 174 |
} |
178 | 175 |
|
179 | 176 |
void firstInc(Edge &e, bool &d, const Node &n) const { |
180 | 177 |
d = true; |
181 | 178 |
Parent::firstOut(e, n); |
182 | 179 |
if (e != INVALID) return; |
183 | 180 |
d = false; |
184 | 181 |
Parent::firstIn(e, n); |
185 | 182 |
} |
186 | 183 |
|
187 | 184 |
void nextInc(Edge &e, bool &d) const { |
188 | 185 |
if (d) { |
189 | 186 |
Node s = Parent::source(e); |
190 | 187 |
Parent::nextOut(e); |
191 | 188 |
if (e != INVALID) return; |
192 | 189 |
d = false; |
193 | 190 |
Parent::firstIn(e, s); |
194 | 191 |
} else { |
195 | 192 |
Parent::nextIn(e); |
196 | 193 |
} |
197 | 194 |
} |
198 | 195 |
|
199 | 196 |
Node nodeFromId(int ix) const { |
200 | 197 |
return Parent::nodeFromId(ix); |
201 | 198 |
} |
202 | 199 |
|
203 | 200 |
Arc arcFromId(int ix) const { |
204 | 201 |
return direct(Parent::arcFromId(ix >> 1), bool(ix & 1)); |
205 | 202 |
} |
206 | 203 |
|
207 | 204 |
Edge edgeFromId(int ix) const { |
208 | 205 |
return Parent::arcFromId(ix); |
209 | 206 |
} |
210 | 207 |
|
211 | 208 |
int id(const Node &n) const { |
212 | 209 |
return Parent::id(n); |
213 | 210 |
} |
214 | 211 |
|
215 | 212 |
int id(const Edge &e) const { |
216 | 213 |
return Parent::id(e); |
217 | 214 |
} |
218 | 215 |
|
219 | 216 |
int id(const Arc &e) const { |
220 | 217 |
return 2 * Parent::id(e) + int(e.forward); |
221 | 218 |
} |
222 | 219 |
|
223 | 220 |
int maxNodeId() const { |
224 | 221 |
return Parent::maxNodeId(); |
225 | 222 |
} |
226 | 223 |
|
227 | 224 |
int maxArcId() const { |
228 | 225 |
return 2 * Parent::maxArcId() + 1; |
229 | 226 |
} |
230 | 227 |
|
231 | 228 |
int maxEdgeId() const { |
232 | 229 |
return Parent::maxArcId(); |
233 | 230 |
} |
234 | 231 |
|
235 | 232 |
int arcNum() const { |
236 | 233 |
return 2 * Parent::arcNum(); |
237 | 234 |
} |
238 | 235 |
|
239 | 236 |
int edgeNum() const { |
240 | 237 |
return Parent::arcNum(); |
241 | 238 |
} |
242 | 239 |
|
243 | 240 |
Arc findArc(Node s, Node t, Arc p = INVALID) const { |
244 | 241 |
if (p == INVALID) { |
245 | 242 |
Edge arc = Parent::findArc(s, t); |
246 | 243 |
if (arc != INVALID) return direct(arc, true); |
247 | 244 |
arc = Parent::findArc(t, s); |
248 | 245 |
if (arc != INVALID) return direct(arc, false); |
249 | 246 |
} else if (direction(p)) { |
250 | 247 |
Edge arc = Parent::findArc(s, t, p); |
251 | 248 |
if (arc != INVALID) return direct(arc, true); |
252 | 249 |
arc = Parent::findArc(t, s); |
253 | 250 |
if (arc != INVALID) return direct(arc, false); |
254 | 251 |
} else { |
255 | 252 |
Edge arc = Parent::findArc(t, s, p); |
256 | 253 |
if (arc != INVALID) return direct(arc, false); |
257 | 254 |
} |
258 | 255 |
return INVALID; |
259 | 256 |
} |
260 | 257 |
|
261 | 258 |
Edge findEdge(Node s, Node t, Edge p = INVALID) const { |
262 | 259 |
if (s != t) { |
263 | 260 |
if (p == INVALID) { |
264 | 261 |
Edge arc = Parent::findArc(s, t); |
265 | 262 |
if (arc != INVALID) return arc; |
266 | 263 |
arc = Parent::findArc(t, s); |
267 | 264 |
if (arc != INVALID) return arc; |
268 | 265 |
} else if (Parent::s(p) == s) { |
269 | 266 |
Edge arc = Parent::findArc(s, t, p); |
270 | 267 |
if (arc != INVALID) return arc; |
271 | 268 |
arc = Parent::findArc(t, s); |
272 | 269 |
if (arc != INVALID) return arc; |
273 | 270 |
} else { |
274 | 271 |
Edge arc = Parent::findArc(t, s, p); |
275 | 272 |
if (arc != INVALID) return arc; |
276 | 273 |
} |
277 | 274 |
} else { |
278 | 275 |
return Parent::findArc(s, t, p); |
279 | 276 |
} |
280 | 277 |
return INVALID; |
281 | 278 |
} |
282 | 279 |
}; |
283 | 280 |
|
284 | 281 |
template <typename Base> |
285 | 282 |
class BidirBpGraphExtender : public Base { |
286 | 283 |
public: |
287 | 284 |
typedef Base Parent; |
288 | 285 |
typedef BidirBpGraphExtender Digraph; |
289 | 286 |
|
290 | 287 |
typedef typename Parent::Node Node; |
291 | 288 |
typedef typename Parent::Edge Edge; |
292 | 289 |
|
293 | 290 |
|
294 | 291 |
using Parent::first; |
295 | 292 |
using Parent::next; |
296 | 293 |
|
297 | 294 |
using Parent::id; |
298 | 295 |
|
299 | 296 |
class Red : public Node { |
300 | 297 |
friend class BidirBpGraphExtender; |
301 | 298 |
public: |
302 | 299 |
Red() {} |
303 | 300 |
Red(const Node& node) : Node(node) { |
304 | 301 |
LEMON_ASSERT(Parent::red(node) || node == INVALID, |
305 | 302 |
typename Parent::NodeSetError()); |
306 | 303 |
} |
307 | 304 |
Red& operator=(const Node& node) { |
308 | 305 |
LEMON_ASSERT(Parent::red(node) || node == INVALID, |
309 | 306 |
typename Parent::NodeSetError()); |
310 | 307 |
Node::operator=(node); |
311 | 308 |
return *this; |
312 | 309 |
} |
313 | 310 |
Red(Invalid) : Node(INVALID) {} |
314 | 311 |
Red& operator=(Invalid) { |
315 | 312 |
Node::operator=(INVALID); |
316 | 313 |
return *this; |
317 | 314 |
} |
318 | 315 |
}; |
319 | 316 |
|
320 | 317 |
void first(Red& node) const { |
321 | 318 |
Parent::firstRed(static_cast<Node&>(node)); |
322 | 319 |
} |
323 | 320 |
void next(Red& node) const { |
324 | 321 |
Parent::nextRed(static_cast<Node&>(node)); |
325 | 322 |
} |
326 | 323 |
|
327 | 324 |
int id(const Red& node) const { |
328 | 325 |
return Parent::redId(node); |
329 | 326 |
} |
330 | 327 |
|
331 | 328 |
class Blue : public Node { |
332 | 329 |
friend class BidirBpGraphExtender; |
333 | 330 |
public: |
334 | 331 |
Blue() {} |
335 | 332 |
Blue(const Node& node) : Node(node) { |
336 | 333 |
LEMON_ASSERT(Parent::blue(node) || node == INVALID, |
337 | 334 |
typename Parent::NodeSetError()); |
338 | 335 |
} |
339 | 336 |
Blue& operator=(const Node& node) { |
340 | 337 |
LEMON_ASSERT(Parent::blue(node) || node == INVALID, |
341 | 338 |
typename Parent::NodeSetError()); |
342 | 339 |
Node::operator=(node); |
343 | 340 |
return *this; |
344 | 341 |
} |
345 | 342 |
Blue(Invalid) : Node(INVALID) {} |
346 | 343 |
Blue& operator=(Invalid) { |
347 | 344 |
Node::operator=(INVALID); |
348 | 345 |
return *this; |
349 | 346 |
} |
350 | 347 |
}; |
351 | 348 |
|
352 | 349 |
void first(Blue& node) const { |
353 | 350 |
Parent::firstBlue(static_cast<Node&>(node)); |
354 | 351 |
} |
355 | 352 |
void next(Blue& node) const { |
356 | 353 |
Parent::nextBlue(static_cast<Node&>(node)); |
357 | 354 |
} |
358 | 355 |
|
359 | 356 |
int id(const Blue& node) const { |
360 | 357 |
return Parent::redId(node); |
361 | 358 |
} |
362 | 359 |
|
363 | 360 |
Node source(const Edge& arc) const { |
364 | 361 |
return red(arc); |
365 | 362 |
} |
366 | 363 |
Node target(const Edge& arc) const { |
367 | 364 |
return blue(arc); |
368 | 365 |
} |
369 | 366 |
|
370 | 367 |
void firstInc(Edge& arc, bool& dir, const Node& node) const { |
371 | 368 |
if (Parent::red(node)) { |
372 | 369 |
Parent::firstFromRed(arc, node); |
373 | 370 |
dir = true; |
374 | 371 |
} else { |
375 | 372 |
Parent::firstFromBlue(arc, node); |
376 | 373 |
dir = static_cast<Edge&>(arc) == INVALID; |
377 | 374 |
} |
378 | 375 |
} |
379 | 376 |
void nextInc(Edge& arc, bool& dir) const { |
380 | 377 |
if (dir) { |
381 | 378 |
Parent::nextFromRed(arc); |
382 | 379 |
} else { |
383 | 380 |
Parent::nextFromBlue(arc); |
384 | 381 |
if (arc == INVALID) dir = true; |
385 | 382 |
} |
386 | 383 |
} |
387 | 384 |
|
388 | 385 |
class Arc : public Edge { |
389 | 386 |
friend class BidirBpGraphExtender; |
390 | 387 |
protected: |
391 | 388 |
bool forward; |
392 | 389 |
|
393 | 390 |
Arc(const Edge& arc, bool _forward) |
394 | 391 |
: Edge(arc), forward(_forward) {} |
395 | 392 |
|
396 | 393 |
public: |
397 | 394 |
Arc() {} |
398 | 395 |
Arc (Invalid) : Edge(INVALID), forward(true) {} |
399 | 396 |
bool operator==(const Arc& i) const { |
400 | 397 |
return Edge::operator==(i) && forward == i.forward; |
401 | 398 |
} |
402 | 399 |
bool operator!=(const Arc& i) const { |
403 | 400 |
return Edge::operator!=(i) || forward != i.forward; |
404 | 401 |
} |
405 | 402 |
bool operator<(const Arc& i) const { |
406 | 403 |
return Edge::operator<(i) || |
407 | 404 |
(!(i.forward<forward) && Edge(*this)<Edge(i)); |
408 | 405 |
} |
409 | 406 |
}; |
410 | 407 |
|
411 | 408 |
void first(Arc& arc) const { |
412 | 409 |
Parent::first(static_cast<Edge&>(arc)); |
413 | 410 |
arc.forward = true; |
414 | 411 |
} |
415 | 412 |
|
416 | 413 |
void next(Arc& arc) const { |
417 | 414 |
if (!arc.forward) { |
418 | 415 |
Parent::next(static_cast<Edge&>(arc)); |
419 | 416 |
} |
420 | 417 |
arc.forward = !arc.forward; |
421 | 418 |
} |
422 | 419 |
|
423 | 420 |
void firstOut(Arc& arc, const Node& node) const { |
424 | 421 |
if (Parent::red(node)) { |
425 | 422 |
Parent::firstFromRed(arc, node); |
426 | 423 |
arc.forward = true; |
427 | 424 |
} else { |
428 | 425 |
Parent::firstFromBlue(arc, node); |
429 | 426 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
430 | 427 |
} |
431 | 428 |
} |
432 | 429 |
void nextOut(Arc& arc) const { |
433 | 430 |
if (arc.forward) { |
434 | 431 |
Parent::nextFromRed(arc); |
435 | 432 |
} else { |
436 | 433 |
Parent::nextFromBlue(arc); |
437 | 434 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
438 | 435 |
} |
439 | 436 |
} |
440 | 437 |
|
441 | 438 |
void firstIn(Arc& arc, const Node& node) const { |
442 | 439 |
if (Parent::blue(node)) { |
443 | 440 |
Parent::firstFromBlue(arc, node); |
444 | 441 |
arc.forward = true; |
445 | 442 |
} else { |
446 | 443 |
Parent::firstFromRed(arc, node); |
447 | 444 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
448 | 445 |
} |
449 | 446 |
} |
450 | 447 |
void nextIn(Arc& arc) const { |
451 | 448 |
if (arc.forward) { |
452 | 449 |
Parent::nextFromBlue(arc); |
453 | 450 |
} else { |
454 | 451 |
Parent::nextFromRed(arc); |
455 | 452 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
456 | 453 |
} |
457 | 454 |
} |
458 | 455 |
|
459 | 456 |
Node source(const Arc& arc) const { |
460 | 457 |
return arc.forward ? Parent::red(arc) : Parent::blue(arc); |
461 | 458 |
} |
462 | 459 |
Node target(const Arc& arc) const { |
463 | 460 |
return arc.forward ? Parent::blue(arc) : Parent::red(arc); |
464 | 461 |
} |
465 | 462 |
|
466 | 463 |
int id(const Arc& arc) const { |
467 | 464 |
return (Parent::id(static_cast<const Edge&>(arc)) << 1) + |
468 | 465 |
(arc.forward ? 0 : 1); |
469 | 466 |
} |
470 | 467 |
Arc arcFromId(int ix) const { |
471 | 468 |
return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0); |
472 | 469 |
} |
473 | 470 |
int maxArcId() const { |
474 | 471 |
return (Parent::maxEdgeId() << 1) + 1; |
475 | 472 |
} |
476 | 473 |
|
477 | 474 |
bool direction(const Arc& arc) const { |
478 | 475 |
return arc.forward; |
479 | 476 |
} |
480 | 477 |
|
481 | 478 |
Arc direct(const Edge& arc, bool dir) const { |
482 | 479 |
return Arc(arc, dir); |
483 | 480 |
} |
484 | 481 |
|
485 | 482 |
int arcNum() const { |
486 | 483 |
return 2 * Parent::edgeNum(); |
487 | 484 |
} |
488 | 485 |
|
489 | 486 |
int edgeNum() const { |
490 | 487 |
return Parent::edgeNum(); |
491 | 488 |
} |
492 | 489 |
|
493 | 490 |
|
494 | 491 |
}; |
495 | 492 |
} |
496 | 493 |
|
497 | 494 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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_BITS_VECTOR_MAP_H |
20 | 20 |
#define LEMON_BITS_VECTOR_MAP_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <algorithm> |
24 | 24 |
|
25 | 25 |
#include <lemon/core.h> |
26 | 26 |
#include <lemon/bits/alteration_notifier.h> |
27 | 27 |
|
28 | 28 |
#include <lemon/concept_check.h> |
29 | 29 |
#include <lemon/concepts/maps.h> |
30 | 30 |
|
31 | 31 |
///\ingroup graphbits |
32 | 32 |
/// |
33 | 33 |
///\file |
34 | 34 |
///\brief Vector based graph maps. |
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
/// \ingroup graphbits |
38 | 38 |
/// |
39 | 39 |
/// \brief Graph map based on the std::vector storage. |
40 | 40 |
/// |
41 | 41 |
/// The VectorMap template class is graph map structure what |
42 | 42 |
/// automatically updates the map when a key is added to or erased from |
43 | 43 |
/// the map. This map type uses the std::vector to store the values. |
44 | 44 |
/// |
45 |
/// \tparam |
|
45 |
/// \tparam _Graph The graph this map is attached to. |
|
46 | 46 |
/// \tparam _Item The item type of the graph items. |
47 | 47 |
/// \tparam _Value The value type of the map. |
48 |
/// \todo Fix the doc: there is _Graph parameter instead of _Notifier. |
|
49 | 48 |
template <typename _Graph, typename _Item, typename _Value> |
50 | 49 |
class VectorMap |
51 | 50 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { |
52 | 51 |
private: |
53 | 52 |
|
54 | 53 |
/// The container type of the map. |
55 | 54 |
typedef std::vector<_Value> Container; |
56 | 55 |
|
57 | 56 |
public: |
58 | 57 |
|
59 | 58 |
/// The graph type of the map. |
60 | 59 |
typedef _Graph Graph; |
61 | 60 |
/// The item type of the map. |
62 | 61 |
typedef _Item Item; |
63 | 62 |
/// The reference map tag. |
64 | 63 |
typedef True ReferenceMapTag; |
65 | 64 |
|
66 | 65 |
/// The key type of the map. |
67 | 66 |
typedef _Item Key; |
68 | 67 |
/// The value type of the map. |
69 | 68 |
typedef _Value Value; |
70 | 69 |
|
71 | 70 |
/// The notifier type. |
72 | 71 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
73 | 72 |
|
74 | 73 |
/// The map type. |
75 | 74 |
typedef VectorMap Map; |
76 | 75 |
/// The base class of the map. |
77 | 76 |
typedef typename Notifier::ObserverBase Parent; |
78 | 77 |
|
79 | 78 |
/// The reference type of the map; |
80 | 79 |
typedef typename Container::reference Reference; |
81 | 80 |
/// The const reference type of the map; |
82 | 81 |
typedef typename Container::const_reference ConstReference; |
83 | 82 |
|
84 | 83 |
|
85 | 84 |
/// \brief Constructor to attach the new map into the notifier. |
86 | 85 |
/// |
87 | 86 |
/// It constructs a map and attachs it into the notifier. |
88 | 87 |
/// It adds all the items of the graph to the map. |
89 | 88 |
VectorMap(const Graph& graph) { |
90 | 89 |
Parent::attach(graph.notifier(Item())); |
91 | 90 |
container.resize(Parent::notifier()->maxId() + 1); |
92 | 91 |
} |
93 | 92 |
|
94 | 93 |
/// \brief Constructor uses given value to initialize the map. |
95 | 94 |
/// |
96 | 95 |
/// It constructs a map uses a given value to initialize the map. |
97 | 96 |
/// It adds all the items of the graph to the map. |
98 | 97 |
VectorMap(const Graph& graph, const Value& value) { |
99 | 98 |
Parent::attach(graph.notifier(Item())); |
100 | 99 |
container.resize(Parent::notifier()->maxId() + 1, value); |
101 | 100 |
} |
102 | 101 |
|
103 | 102 |
private: |
104 | 103 |
/// \brief Copy constructor |
105 | 104 |
/// |
106 | 105 |
/// Copy constructor. |
107 | 106 |
VectorMap(const VectorMap& _copy) : Parent() { |
108 | 107 |
if (_copy.attached()) { |
109 | 108 |
Parent::attach(*_copy.notifier()); |
110 | 109 |
container = _copy.container; |
111 | 110 |
} |
112 | 111 |
} |
113 | 112 |
|
114 | 113 |
/// \brief Assign operator. |
115 | 114 |
/// |
116 | 115 |
/// This operator assigns for each item in the map the |
117 | 116 |
/// value mapped to the same item in the copied map. |
118 | 117 |
/// The parameter map should be indiced with the same |
119 | 118 |
/// itemset because this assign operator does not change |
120 | 119 |
/// the container of the map. |
121 | 120 |
VectorMap& operator=(const VectorMap& cmap) { |
122 | 121 |
return operator=<VectorMap>(cmap); |
123 | 122 |
} |
124 | 123 |
|
125 | 124 |
|
126 | 125 |
/// \brief Template assign operator. |
127 | 126 |
/// |
128 | 127 |
/// The given parameter should be conform to the ReadMap |
129 | 128 |
/// concecpt and could be indiced by the current item set of |
130 | 129 |
/// the NodeMap. In this case the value for each item |
131 | 130 |
/// is assigned by the value of the given ReadMap. |
132 | 131 |
template <typename CMap> |
133 | 132 |
VectorMap& operator=(const CMap& cmap) { |
134 | 133 |
checkConcept<concepts::ReadMap<Key, _Value>, CMap>(); |
135 | 134 |
const typename Parent::Notifier* nf = Parent::notifier(); |
136 | 135 |
Item it; |
137 | 136 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
138 | 137 |
set(it, cmap[it]); |
139 | 138 |
} |
140 | 139 |
return *this; |
141 | 140 |
} |
142 | 141 |
|
143 | 142 |
public: |
144 | 143 |
|
145 | 144 |
/// \brief The subcript operator. |
146 | 145 |
/// |
147 | 146 |
/// The subscript operator. The map can be subscripted by the |
148 | 147 |
/// actual items of the graph. |
149 | 148 |
Reference operator[](const Key& key) { |
150 | 149 |
return container[Parent::notifier()->id(key)]; |
151 | 150 |
} |
152 | 151 |
|
153 | 152 |
/// \brief The const subcript operator. |
154 | 153 |
/// |
155 | 154 |
/// The const subscript operator. The map can be subscripted by the |
156 | 155 |
/// actual items of the graph. |
157 | 156 |
ConstReference operator[](const Key& key) const { |
158 | 157 |
return container[Parent::notifier()->id(key)]; |
159 | 158 |
} |
160 | 159 |
|
161 | 160 |
|
162 | 161 |
/// \brief The setter function of the map. |
163 | 162 |
/// |
164 | 163 |
/// It the same as operator[](key) = value expression. |
165 | 164 |
void set(const Key& key, const Value& value) { |
166 | 165 |
(*this)[key] = value; |
167 | 166 |
} |
168 | 167 |
|
169 | 168 |
protected: |
170 | 169 |
|
171 | 170 |
/// \brief Adds a new key to the map. |
172 | 171 |
/// |
173 | 172 |
/// It adds a new key to the map. It called by the observer notifier |
174 | 173 |
/// and it overrides the add() member function of the observer base. |
175 | 174 |
virtual void add(const Key& key) { |
176 | 175 |
int id = Parent::notifier()->id(key); |
177 | 176 |
if (id >= int(container.size())) { |
178 | 177 |
container.resize(id + 1); |
179 | 178 |
} |
180 | 179 |
} |
181 | 180 |
|
182 | 181 |
/// \brief Adds more new keys to the map. |
183 | 182 |
/// |
184 | 183 |
/// It adds more new keys to the map. It called by the observer notifier |
185 | 184 |
/// and it overrides the add() member function of the observer base. |
186 | 185 |
virtual void add(const std::vector<Key>& keys) { |
187 | 186 |
int max = container.size() - 1; |
188 | 187 |
for (int i = 0; i < int(keys.size()); ++i) { |
189 | 188 |
int id = Parent::notifier()->id(keys[i]); |
190 | 189 |
if (id >= max) { |
191 | 190 |
max = id; |
192 | 191 |
} |
193 | 192 |
} |
194 | 193 |
container.resize(max + 1); |
195 | 194 |
} |
196 | 195 |
|
197 | 196 |
/// \brief Erase a key from the map. |
198 | 197 |
/// |
199 | 198 |
/// Erase a key from the map. It called by the observer notifier |
200 | 199 |
/// and it overrides the erase() member function of the observer base. |
201 | 200 |
virtual void erase(const Key& key) { |
202 | 201 |
container[Parent::notifier()->id(key)] = Value(); |
203 | 202 |
} |
204 | 203 |
|
205 | 204 |
/// \brief Erase more keys from the map. |
206 | 205 |
/// |
207 | 206 |
/// Erase more keys from the map. It called by the observer notifier |
208 | 207 |
/// and it overrides the erase() member function of the observer base. |
209 | 208 |
virtual void erase(const std::vector<Key>& keys) { |
210 | 209 |
for (int i = 0; i < int(keys.size()); ++i) { |
211 | 210 |
container[Parent::notifier()->id(keys[i])] = Value(); |
212 | 211 |
} |
213 | 212 |
} |
214 | 213 |
|
215 | 214 |
/// \brief Buildes the map. |
216 | 215 |
/// |
217 | 216 |
/// It buildes the map. It called by the observer notifier |
218 | 217 |
/// and it overrides the build() member function of the observer base. |
219 | 218 |
virtual void build() { |
220 | 219 |
int size = Parent::notifier()->maxId() + 1; |
221 | 220 |
container.reserve(size); |
222 | 221 |
container.resize(size); |
223 | 222 |
} |
224 | 223 |
|
225 | 224 |
/// \brief Clear the map. |
226 | 225 |
/// |
227 | 226 |
/// It erase all items from the map. It called by the observer notifier |
228 | 227 |
/// and it overrides the clear() member function of the observer base. |
229 | 228 |
virtual void clear() { |
230 | 229 |
container.clear(); |
231 | 230 |
} |
232 | 231 |
|
233 | 232 |
private: |
234 | 233 |
|
235 | 234 |
Container container; |
236 | 235 |
|
237 | 236 |
}; |
238 | 237 |
|
239 | 238 |
} |
240 | 239 |
|
241 | 240 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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 |
// This file contains a modified version of the concept checking |
|
20 |
// utility from BOOST. |
|
21 |
// See the appropriate copyright notice below. |
|
22 |
|
|
23 |
// (C) Copyright Jeremy Siek 2000. |
|
24 |
// Distributed under the Boost Software License, Version 1.0. (See |
|
25 |
// accompanying file LICENSE_1_0.txt or copy at |
|
26 |
// http://www.boost.org/LICENSE_1_0.txt) |
|
27 |
// |
|
28 |
// Revision History: |
|
29 |
// 05 May 2001: Workarounds for HP aCC from Thomas Matelich. (Jeremy Siek) |
|
30 |
// 02 April 2001: Removed limits header altogether. (Jeremy Siek) |
|
31 |
// 01 April 2001: Modified to use new <boost/limits.hpp> header. (JMaddock) |
|
32 |
// |
|
33 |
|
|
34 |
// See http://www.boost.org/libs/concept_check for documentation. |
|
19 |
// The contents of this file was inspired by the concept checking |
|
20 |
// utility of the BOOST library (http://www.boost.org). |
|
35 | 21 |
|
36 | 22 |
///\file |
37 | 23 |
///\brief Basic utilities for concept checking. |
38 | 24 |
/// |
39 |
///\todo Are we still using BOOST concept checking utility? |
|
40 |
///Is the BOOST copyright notice necessary? |
|
41 | 25 |
|
42 | 26 |
#ifndef LEMON_CONCEPT_CHECK_H |
43 | 27 |
#define LEMON_CONCEPT_CHECK_H |
44 | 28 |
|
45 | 29 |
namespace lemon { |
46 | 30 |
|
47 | 31 |
/* |
48 | 32 |
"inline" is used for ignore_unused_variable_warning() |
49 | 33 |
and function_requires() to make sure there is no |
50 | 34 |
overtarget with g++. |
51 | 35 |
*/ |
52 | 36 |
|
53 | 37 |
template <class T> inline void ignore_unused_variable_warning(const T&) { } |
54 | 38 |
|
55 | 39 |
///\e |
56 | 40 |
template <class Concept> |
57 | 41 |
inline void function_requires() |
58 | 42 |
{ |
59 | 43 |
#if !defined(NDEBUG) |
60 | 44 |
void (Concept::*x)() = & Concept::constraints; |
61 | 45 |
ignore_unused_variable_warning(x); |
62 | 46 |
#endif |
63 | 47 |
} |
64 | 48 |
|
65 | 49 |
///\e |
66 | 50 |
template <typename Concept, typename Type> |
67 | 51 |
inline void checkConcept() { |
68 | 52 |
#if !defined(NDEBUG) |
69 | 53 |
typedef typename Concept::template Constraints<Type> ConceptCheck; |
70 | 54 |
void (ConceptCheck::*x)() = & ConceptCheck::constraints; |
71 | 55 |
ignore_unused_variable_warning(x); |
72 | 56 |
#endif |
73 | 57 |
} |
74 | 58 |
|
75 | 59 |
} // namespace lemon |
76 | 60 |
|
77 | 61 |
#endif // LEMON_CONCEPT_CHECK_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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 |
///\ingroup concept |
20 | 20 |
///\file |
21 | 21 |
///\brief Classes for representing paths in digraphs. |
22 | 22 |
/// |
23 |
///\todo Iterators have obsolete style |
|
24 | 23 |
|
25 | 24 |
#ifndef LEMON_CONCEPT_PATH_H |
26 | 25 |
#define LEMON_CONCEPT_PATH_H |
27 | 26 |
|
28 | 27 |
#include <lemon/core.h> |
29 | 28 |
#include <lemon/concept_check.h> |
30 | 29 |
|
31 | 30 |
namespace lemon { |
32 | 31 |
namespace concepts { |
33 | 32 |
|
34 | 33 |
/// \addtogroup concept |
35 | 34 |
/// @{ |
36 | 35 |
|
37 | 36 |
/// \brief A skeleton structure for representing directed paths in |
38 | 37 |
/// a digraph. |
39 | 38 |
/// |
40 | 39 |
/// A skeleton structure for representing directed paths in a |
41 | 40 |
/// digraph. |
42 | 41 |
/// \tparam _Digraph The digraph type in which the path is. |
43 | 42 |
/// |
44 | 43 |
/// In a sense, the path can be treated as a list of arcs. The |
45 | 44 |
/// lemon path type stores just this list. As a consequence it |
46 | 45 |
/// cannot enumerate the nodes in the path and the zero length |
47 | 46 |
/// paths cannot store the source. |
48 | 47 |
/// |
49 | 48 |
template <typename _Digraph> |
50 | 49 |
class Path { |
51 | 50 |
public: |
52 | 51 |
|
53 | 52 |
/// Type of the underlying digraph. |
54 | 53 |
typedef _Digraph Digraph; |
55 | 54 |
/// Arc type of the underlying digraph. |
56 | 55 |
typedef typename Digraph::Arc Arc; |
57 | 56 |
|
58 | 57 |
class ArcIt; |
59 | 58 |
|
60 | 59 |
/// \brief Default constructor |
61 | 60 |
Path() {} |
62 | 61 |
|
63 | 62 |
/// \brief Template constructor |
64 | 63 |
template <typename CPath> |
65 | 64 |
Path(const CPath& cpath) {} |
66 | 65 |
|
67 | 66 |
/// \brief Template assigment |
68 | 67 |
template <typename CPath> |
69 | 68 |
Path& operator=(const CPath& cpath) { |
70 | 69 |
ignore_unused_variable_warning(cpath); |
71 | 70 |
return *this; |
72 | 71 |
} |
73 | 72 |
|
74 | 73 |
/// Length of the path ie. the number of arcs in the path. |
75 | 74 |
int length() const { return 0;} |
76 | 75 |
|
77 | 76 |
/// Returns whether the path is empty. |
78 | 77 |
bool empty() const { return true;} |
79 | 78 |
|
80 | 79 |
/// Resets the path to an empty path. |
81 | 80 |
void clear() {} |
82 | 81 |
|
83 | 82 |
/// \brief LEMON style iterator for path arcs |
84 | 83 |
/// |
85 | 84 |
/// This class is used to iterate on the arcs of the paths. |
86 | 85 |
class ArcIt { |
87 | 86 |
public: |
88 | 87 |
/// Default constructor |
89 | 88 |
ArcIt() {} |
90 | 89 |
/// Invalid constructor |
91 | 90 |
ArcIt(Invalid) {} |
92 | 91 |
/// Constructor for first arc |
93 | 92 |
ArcIt(const Path &) {} |
94 | 93 |
|
95 | 94 |
/// Conversion to Arc |
96 | 95 |
operator Arc() const { return INVALID; } |
97 | 96 |
|
98 | 97 |
/// Next arc |
99 | 98 |
ArcIt& operator++() {return *this;} |
100 | 99 |
|
101 | 100 |
/// Comparison operator |
102 | 101 |
bool operator==(const ArcIt&) const {return true;} |
103 | 102 |
/// Comparison operator |
104 | 103 |
bool operator!=(const ArcIt&) const {return true;} |
105 | 104 |
/// Comparison operator |
106 | 105 |
bool operator<(const ArcIt&) const {return false;} |
107 | 106 |
|
108 | 107 |
}; |
109 | 108 |
|
110 | 109 |
template <typename _Path> |
111 | 110 |
struct Constraints { |
112 | 111 |
void constraints() { |
113 | 112 |
Path<Digraph> pc; |
114 | 113 |
_Path p, pp(pc); |
115 | 114 |
int l = p.length(); |
116 | 115 |
int e = p.empty(); |
117 | 116 |
p.clear(); |
118 | 117 |
|
119 | 118 |
p = pc; |
120 | 119 |
|
121 | 120 |
typename _Path::ArcIt id, ii(INVALID), i(p); |
122 | 121 |
|
123 | 122 |
++i; |
124 | 123 |
typename Digraph::Arc ed = i; |
125 | 124 |
|
126 | 125 |
e = (i == ii); |
127 | 126 |
e = (i != ii); |
128 | 127 |
e = (i < ii); |
129 | 128 |
|
130 | 129 |
ignore_unused_variable_warning(l); |
131 | 130 |
ignore_unused_variable_warning(pp); |
132 | 131 |
ignore_unused_variable_warning(e); |
133 | 132 |
ignore_unused_variable_warning(id); |
134 | 133 |
ignore_unused_variable_warning(ii); |
135 | 134 |
ignore_unused_variable_warning(ed); |
136 | 135 |
} |
137 | 136 |
}; |
138 | 137 |
|
139 | 138 |
}; |
140 | 139 |
|
141 | 140 |
namespace _path_bits { |
142 | 141 |
|
143 | 142 |
template <typename _Digraph, typename _Path, typename RevPathTag = void> |
144 | 143 |
struct PathDumperConstraints { |
145 | 144 |
void constraints() { |
146 | 145 |
int l = p.length(); |
147 | 146 |
int e = p.empty(); |
148 | 147 |
|
149 | 148 |
typename _Path::ArcIt id, i(p); |
150 | 149 |
|
151 | 150 |
++i; |
152 | 151 |
typename _Digraph::Arc ed = i; |
153 | 152 |
|
154 | 153 |
e = (i == INVALID); |
155 | 154 |
e = (i != INVALID); |
156 | 155 |
|
157 | 156 |
ignore_unused_variable_warning(l); |
158 | 157 |
ignore_unused_variable_warning(e); |
159 | 158 |
ignore_unused_variable_warning(id); |
160 | 159 |
ignore_unused_variable_warning(ed); |
161 | 160 |
} |
162 | 161 |
_Path& p; |
163 | 162 |
}; |
164 | 163 |
|
165 | 164 |
template <typename _Digraph, typename _Path> |
166 | 165 |
struct PathDumperConstraints< |
167 | 166 |
_Digraph, _Path, |
168 | 167 |
typename enable_if<typename _Path::RevPathTag, void>::type |
169 | 168 |
> { |
170 | 169 |
void constraints() { |
171 | 170 |
int l = p.length(); |
172 | 171 |
int e = p.empty(); |
173 | 172 |
|
174 | 173 |
typename _Path::RevArcIt id, i(p); |
175 | 174 |
|
176 | 175 |
++i; |
177 | 176 |
typename _Digraph::Arc ed = i; |
178 | 177 |
|
179 | 178 |
e = (i == INVALID); |
180 | 179 |
e = (i != INVALID); |
181 | 180 |
|
182 | 181 |
ignore_unused_variable_warning(l); |
183 | 182 |
ignore_unused_variable_warning(e); |
184 | 183 |
ignore_unused_variable_warning(id); |
185 | 184 |
ignore_unused_variable_warning(ed); |
186 | 185 |
} |
187 | 186 |
_Path& p; |
188 | 187 |
}; |
189 | 188 |
|
190 | 189 |
} |
191 | 190 |
|
192 | 191 |
|
193 | 192 |
/// \brief A skeleton structure for path dumpers. |
194 | 193 |
/// |
195 | 194 |
/// A skeleton structure for path dumpers. The path dumpers are |
196 | 195 |
/// the generalization of the paths. The path dumpers can |
197 | 196 |
/// enumerate the arcs of the path wheter in forward or in |
198 | 197 |
/// backward order. In most time these classes are not used |
199 | 198 |
/// directly rather it used to assign a dumped class to a real |
200 | 199 |
/// path type. |
201 | 200 |
/// |
202 | 201 |
/// The main purpose of this concept is that the shortest path |
203 | 202 |
/// algorithms can enumerate easily the arcs in reverse order. |
204 | 203 |
/// If we would like to give back a real path from these |
205 | 204 |
/// algorithms then we should create a temporarly path object. In |
206 | 205 |
/// LEMON such algorithms gives back a path dumper what can |
207 | 206 |
/// assigned to a real path and the dumpers can be implemented as |
208 | 207 |
/// an adaptor class to the predecessor map. |
209 | 208 |
|
210 | 209 |
/// \tparam _Digraph The digraph type in which the path is. |
211 | 210 |
/// |
212 | 211 |
/// The paths can be constructed from any path type by a |
213 | 212 |
/// template constructor or a template assignment operator. |
214 | 213 |
/// |
215 | 214 |
template <typename _Digraph> |
216 | 215 |
class PathDumper { |
217 | 216 |
public: |
218 | 217 |
|
219 | 218 |
/// Type of the underlying digraph. |
220 | 219 |
typedef _Digraph Digraph; |
221 | 220 |
/// Arc type of the underlying digraph. |
222 | 221 |
typedef typename Digraph::Arc Arc; |
223 | 222 |
|
224 | 223 |
/// Length of the path ie. the number of arcs in the path. |
225 | 224 |
int length() const { return 0;} |
226 | 225 |
|
227 | 226 |
/// Returns whether the path is empty. |
228 | 227 |
bool empty() const { return true;} |
229 | 228 |
|
230 | 229 |
/// \brief Forward or reverse dumping |
231 | 230 |
/// |
232 | 231 |
/// If the RevPathTag is defined and true then reverse dumping |
233 | 232 |
/// is provided in the path dumper. In this case instead of the |
234 | 233 |
/// ArcIt the RevArcIt iterator should be implemented in the |
235 | 234 |
/// dumper. |
236 | 235 |
typedef False RevPathTag; |
237 | 236 |
|
238 | 237 |
/// \brief LEMON style iterator for path arcs |
239 | 238 |
/// |
240 | 239 |
/// This class is used to iterate on the arcs of the paths. |
241 | 240 |
class ArcIt { |
242 | 241 |
public: |
243 | 242 |
/// Default constructor |
244 | 243 |
ArcIt() {} |
245 | 244 |
/// Invalid constructor |
246 | 245 |
ArcIt(Invalid) {} |
247 | 246 |
/// Constructor for first arc |
248 | 247 |
ArcIt(const PathDumper&) {} |
249 | 248 |
|
250 | 249 |
/// Conversion to Arc |
251 | 250 |
operator Arc() const { return INVALID; } |
252 | 251 |
|
253 | 252 |
/// Next arc |
254 | 253 |
ArcIt& operator++() {return *this;} |
255 | 254 |
|
256 | 255 |
/// Comparison operator |
257 | 256 |
bool operator==(const ArcIt&) const {return true;} |
258 | 257 |
/// Comparison operator |
259 | 258 |
bool operator!=(const ArcIt&) const {return true;} |
260 | 259 |
/// Comparison operator |
261 | 260 |
bool operator<(const ArcIt&) const {return false;} |
262 | 261 |
|
263 | 262 |
}; |
264 | 263 |
|
265 | 264 |
/// \brief LEMON style iterator for path arcs |
266 | 265 |
/// |
267 | 266 |
/// This class is used to iterate on the arcs of the paths in |
268 | 267 |
/// reverse direction. |
269 | 268 |
class RevArcIt { |
270 | 269 |
public: |
271 | 270 |
/// Default constructor |
272 | 271 |
RevArcIt() {} |
273 | 272 |
/// Invalid constructor |
274 | 273 |
RevArcIt(Invalid) {} |
275 | 274 |
/// Constructor for first arc |
276 | 275 |
RevArcIt(const PathDumper &) {} |
277 | 276 |
|
278 | 277 |
/// Conversion to Arc |
279 | 278 |
operator Arc() const { return INVALID; } |
280 | 279 |
|
281 | 280 |
/// Next arc |
282 | 281 |
RevArcIt& operator++() {return *this;} |
283 | 282 |
|
284 | 283 |
/// Comparison operator |
285 | 284 |
bool operator==(const RevArcIt&) const {return true;} |
286 | 285 |
/// Comparison operator |
287 | 286 |
bool operator!=(const RevArcIt&) const {return true;} |
288 | 287 |
/// Comparison operator |
289 | 288 |
bool operator<(const RevArcIt&) const {return false;} |
290 | 289 |
|
291 | 290 |
}; |
292 | 291 |
|
293 | 292 |
template <typename _Path> |
294 | 293 |
struct Constraints { |
295 | 294 |
void constraints() { |
296 | 295 |
function_requires<_path_bits:: |
297 | 296 |
PathDumperConstraints<Digraph, _Path> >(); |
298 | 297 |
} |
299 | 298 |
}; |
300 | 299 |
|
301 | 300 |
}; |
302 | 301 |
|
303 | 302 |
|
304 | 303 |
///@} |
305 | 304 |
} |
306 | 305 |
|
307 | 306 |
} // namespace lemon |
308 | 307 |
|
309 | 308 |
#endif // LEMON_CONCEPT_PATH_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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_CORE_H |
20 | 20 |
#define LEMON_CORE_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <algorithm> |
24 | 24 |
|
25 | 25 |
#include <lemon/bits/enable_if.h> |
26 | 26 |
#include <lemon/bits/traits.h> |
27 | 27 |
|
28 | 28 |
///\file |
29 | 29 |
///\brief LEMON core utilities. |
30 | 30 |
/// |
31 | 31 |
///This header file contains core utilities for LEMON. |
32 | 32 |
///It is automatically included by all graph types, therefore it usually |
33 | 33 |
///do not have to be included directly. |
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
/// \brief Dummy type to make it easier to create invalid iterators. |
38 | 38 |
/// |
39 | 39 |
/// Dummy type to make it easier to create invalid iterators. |
40 | 40 |
/// See \ref INVALID for the usage. |
41 | 41 |
struct Invalid { |
42 | 42 |
public: |
43 | 43 |
bool operator==(Invalid) { return true; } |
44 | 44 |
bool operator!=(Invalid) { return false; } |
45 | 45 |
bool operator< (Invalid) { return false; } |
46 | 46 |
}; |
47 | 47 |
|
48 | 48 |
/// \brief Invalid iterators. |
49 | 49 |
/// |
50 | 50 |
/// \ref Invalid is a global type that converts to each iterator |
51 | 51 |
/// in such a way that the value of the target iterator will be invalid. |
52 | 52 |
#ifdef LEMON_ONLY_TEMPLATES |
53 | 53 |
const Invalid INVALID = Invalid(); |
54 | 54 |
#else |
55 | 55 |
extern const Invalid INVALID; |
56 | 56 |
#endif |
57 | 57 |
|
58 | 58 |
/// \addtogroup gutils |
59 | 59 |
/// @{ |
60 | 60 |
|
61 |
/// |
|
61 |
///Create convenient typedefs for the digraph types and iterators |
|
62 | 62 |
|
63 |
///This \c \#define creates convenience typedefs for the following types |
|
64 |
///of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
|
63 |
///This \c \#define creates convenient type definitions for the following |
|
64 |
///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
|
65 | 65 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
66 | 66 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
67 | 67 |
/// |
68 | 68 |
///\note If the graph type is a dependent type, ie. the graph type depend |
69 | 69 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
70 | 70 |
///macro. |
71 | 71 |
#define DIGRAPH_TYPEDEFS(Digraph) \ |
72 | 72 |
typedef Digraph::Node Node; \ |
73 | 73 |
typedef Digraph::NodeIt NodeIt; \ |
74 | 74 |
typedef Digraph::Arc Arc; \ |
75 | 75 |
typedef Digraph::ArcIt ArcIt; \ |
76 | 76 |
typedef Digraph::InArcIt InArcIt; \ |
77 | 77 |
typedef Digraph::OutArcIt OutArcIt; \ |
78 | 78 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \ |
79 | 79 |
typedef Digraph::NodeMap<int> IntNodeMap; \ |
80 | 80 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \ |
81 | 81 |
typedef Digraph::ArcMap<bool> BoolArcMap; \ |
82 | 82 |
typedef Digraph::ArcMap<int> IntArcMap; \ |
83 |
typedef Digraph::ArcMap<double> DoubleArcMap |
|
83 |
typedef Digraph::ArcMap<double> DoubleArcMap; |
|
84 | 84 |
|
85 |
/// |
|
85 |
///Create convenient typedefs for the digraph types and iterators |
|
86 | 86 |
|
87 | 87 |
///\see DIGRAPH_TYPEDEFS |
88 | 88 |
/// |
89 | 89 |
///\note Use this macro, if the graph type is a dependent type, |
90 | 90 |
///ie. the graph type depend on a template parameter. |
91 | 91 |
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \ |
92 | 92 |
typedef typename Digraph::Node Node; \ |
93 | 93 |
typedef typename Digraph::NodeIt NodeIt; \ |
94 | 94 |
typedef typename Digraph::Arc Arc; \ |
95 | 95 |
typedef typename Digraph::ArcIt ArcIt; \ |
96 | 96 |
typedef typename Digraph::InArcIt InArcIt; \ |
97 | 97 |
typedef typename Digraph::OutArcIt OutArcIt; \ |
98 | 98 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \ |
99 | 99 |
typedef typename Digraph::template NodeMap<int> IntNodeMap; \ |
100 | 100 |
typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \ |
101 | 101 |
typedef typename Digraph::template ArcMap<bool> BoolArcMap; \ |
102 | 102 |
typedef typename Digraph::template ArcMap<int> IntArcMap; \ |
103 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap |
|
103 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap; |
|
104 | 104 |
|
105 |
/// |
|
105 |
///Create convenient typedefs for the graph types and iterators |
|
106 | 106 |
|
107 |
///This \c \#define creates the same |
|
107 |
///This \c \#define creates the same convenient type definitions as defined |
|
108 | 108 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates |
109 | 109 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap, |
110 | 110 |
///\c DoubleEdgeMap. |
111 | 111 |
/// |
112 | 112 |
///\note If the graph type is a dependent type, ie. the graph type depend |
113 |
///on a template parameter, then use \c |
|
113 |
///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS() |
|
114 | 114 |
///macro. |
115 | 115 |
#define GRAPH_TYPEDEFS(Graph) \ |
116 | 116 |
DIGRAPH_TYPEDEFS(Graph); \ |
117 | 117 |
typedef Graph::Edge Edge; \ |
118 | 118 |
typedef Graph::EdgeIt EdgeIt; \ |
119 | 119 |
typedef Graph::IncEdgeIt IncEdgeIt; \ |
120 | 120 |
typedef Graph::EdgeMap<bool> BoolEdgeMap; \ |
121 | 121 |
typedef Graph::EdgeMap<int> IntEdgeMap; \ |
122 |
typedef Graph::EdgeMap<double> DoubleEdgeMap |
|
122 |
typedef Graph::EdgeMap<double> DoubleEdgeMap; |
|
123 | 123 |
|
124 |
/// |
|
124 |
///Create convenient typedefs for the graph types and iterators |
|
125 | 125 |
|
126 | 126 |
///\see GRAPH_TYPEDEFS |
127 | 127 |
/// |
128 | 128 |
///\note Use this macro, if the graph type is a dependent type, |
129 | 129 |
///ie. the graph type depend on a template parameter. |
130 | 130 |
#define TEMPLATE_GRAPH_TYPEDEFS(Graph) \ |
131 | 131 |
TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \ |
132 | 132 |
typedef typename Graph::Edge Edge; \ |
133 | 133 |
typedef typename Graph::EdgeIt EdgeIt; \ |
134 | 134 |
typedef typename Graph::IncEdgeIt IncEdgeIt; \ |
135 | 135 |
typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \ |
136 | 136 |
typedef typename Graph::template EdgeMap<int> IntEdgeMap; \ |
137 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap |
|
137 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap; |
|
138 | 138 |
|
139 |
/// \brief Function to count the items in |
|
139 |
/// \brief Function to count the items in a graph. |
|
140 | 140 |
/// |
141 |
/// This function counts the items (nodes, arcs etc) in the graph. |
|
142 |
/// The complexity of the function is O(n) because |
|
141 |
/// This function counts the items (nodes, arcs etc.) in a graph. |
|
142 |
/// The complexity of the function is linear because |
|
143 | 143 |
/// it iterates on all of the items. |
144 | 144 |
template <typename Graph, typename Item> |
145 | 145 |
inline int countItems(const Graph& g) { |
146 | 146 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
147 | 147 |
int num = 0; |
148 | 148 |
for (ItemIt it(g); it != INVALID; ++it) { |
149 | 149 |
++num; |
150 | 150 |
} |
151 | 151 |
return num; |
152 | 152 |
} |
153 | 153 |
|
154 | 154 |
// Node counting: |
155 | 155 |
|
156 | 156 |
namespace _core_bits { |
157 | 157 |
|
158 | 158 |
template <typename Graph, typename Enable = void> |
159 | 159 |
struct CountNodesSelector { |
160 | 160 |
static int count(const Graph &g) { |
161 | 161 |
return countItems<Graph, typename Graph::Node>(g); |
162 | 162 |
} |
163 | 163 |
}; |
164 | 164 |
|
165 | 165 |
template <typename Graph> |
166 | 166 |
struct CountNodesSelector< |
167 | 167 |
Graph, typename |
168 | 168 |
enable_if<typename Graph::NodeNumTag, void>::type> |
169 | 169 |
{ |
170 | 170 |
static int count(const Graph &g) { |
171 | 171 |
return g.nodeNum(); |
172 | 172 |
} |
173 | 173 |
}; |
174 | 174 |
} |
175 | 175 |
|
176 | 176 |
/// \brief Function to count the nodes in the graph. |
177 | 177 |
/// |
178 | 178 |
/// This function counts the nodes in the graph. |
179 |
/// The complexity of the function is O(n) but for some |
|
180 |
/// graph structures it is specialized to run in O(1). |
|
179 |
/// The complexity of the function is <em>O</em>(<em>n</em>), but for some |
|
180 |
/// graph structures it is specialized to run in <em>O</em>(1). |
|
181 | 181 |
/// |
182 |
/// If the graph contains a \e nodeNum() member function and a |
|
183 |
/// \e NodeNumTag tag then this function calls directly the member |
|
182 |
/// \note If the graph contains a \c nodeNum() member function and a |
|
183 |
/// \c NodeNumTag tag then this function calls directly the member |
|
184 | 184 |
/// function to query the cardinality of the node set. |
185 | 185 |
template <typename Graph> |
186 | 186 |
inline int countNodes(const Graph& g) { |
187 | 187 |
return _core_bits::CountNodesSelector<Graph>::count(g); |
188 | 188 |
} |
189 | 189 |
|
190 | 190 |
// Arc counting: |
191 | 191 |
|
192 | 192 |
namespace _core_bits { |
193 | 193 |
|
194 | 194 |
template <typename Graph, typename Enable = void> |
195 | 195 |
struct CountArcsSelector { |
196 | 196 |
static int count(const Graph &g) { |
197 | 197 |
return countItems<Graph, typename Graph::Arc>(g); |
198 | 198 |
} |
199 | 199 |
}; |
200 | 200 |
|
201 | 201 |
template <typename Graph> |
202 | 202 |
struct CountArcsSelector< |
203 | 203 |
Graph, |
204 | 204 |
typename enable_if<typename Graph::ArcNumTag, void>::type> |
205 | 205 |
{ |
206 | 206 |
static int count(const Graph &g) { |
207 | 207 |
return g.arcNum(); |
208 | 208 |
} |
209 | 209 |
}; |
210 | 210 |
} |
211 | 211 |
|
212 | 212 |
/// \brief Function to count the arcs in the graph. |
213 | 213 |
/// |
214 | 214 |
/// This function counts the arcs in the graph. |
215 |
/// The complexity of the function is O(e) but for some |
|
216 |
/// graph structures it is specialized to run in O(1). |
|
215 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some |
|
216 |
/// graph structures it is specialized to run in <em>O</em>(1). |
|
217 | 217 |
/// |
218 |
/// If the graph contains a \e arcNum() member function and a |
|
219 |
/// \e EdgeNumTag tag then this function calls directly the member |
|
218 |
/// \note If the graph contains a \c arcNum() member function and a |
|
219 |
/// \c ArcNumTag tag then this function calls directly the member |
|
220 | 220 |
/// function to query the cardinality of the arc set. |
221 | 221 |
template <typename Graph> |
222 | 222 |
inline int countArcs(const Graph& g) { |
223 | 223 |
return _core_bits::CountArcsSelector<Graph>::count(g); |
224 | 224 |
} |
225 | 225 |
|
226 | 226 |
// Edge counting: |
227 |
|
|
227 | 228 |
namespace _core_bits { |
228 | 229 |
|
229 | 230 |
template <typename Graph, typename Enable = void> |
230 | 231 |
struct CountEdgesSelector { |
231 | 232 |
static int count(const Graph &g) { |
232 | 233 |
return countItems<Graph, typename Graph::Edge>(g); |
233 | 234 |
} |
234 | 235 |
}; |
235 | 236 |
|
236 | 237 |
template <typename Graph> |
237 | 238 |
struct CountEdgesSelector< |
238 | 239 |
Graph, |
239 | 240 |
typename enable_if<typename Graph::EdgeNumTag, void>::type> |
240 | 241 |
{ |
241 | 242 |
static int count(const Graph &g) { |
242 | 243 |
return g.edgeNum(); |
243 | 244 |
} |
244 | 245 |
}; |
245 | 246 |
} |
246 | 247 |
|
247 | 248 |
/// \brief Function to count the edges in the graph. |
248 | 249 |
/// |
249 | 250 |
/// This function counts the edges in the graph. |
250 |
/// The complexity of the function is O(m) but for some |
|
251 |
/// graph structures it is specialized to run in O(1). |
|
251 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some |
|
252 |
/// graph structures it is specialized to run in <em>O</em>(1). |
|
252 | 253 |
/// |
253 |
/// If the graph contains a \e edgeNum() member function and a |
|
254 |
/// \e EdgeNumTag tag then this function calls directly the member |
|
254 |
/// \note If the graph contains a \c edgeNum() member function and a |
|
255 |
/// \c EdgeNumTag tag then this function calls directly the member |
|
255 | 256 |
/// function to query the cardinality of the edge set. |
256 | 257 |
template <typename Graph> |
257 | 258 |
inline int countEdges(const Graph& g) { |
258 | 259 |
return _core_bits::CountEdgesSelector<Graph>::count(g); |
259 | 260 |
|
260 | 261 |
} |
261 | 262 |
|
262 | 263 |
|
263 | 264 |
template <typename Graph, typename DegIt> |
264 | 265 |
inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) { |
265 | 266 |
int num = 0; |
266 | 267 |
for (DegIt it(_g, _n); it != INVALID; ++it) { |
267 | 268 |
++num; |
268 | 269 |
} |
269 | 270 |
return num; |
270 | 271 |
} |
271 | 272 |
|
272 | 273 |
/// \brief Function to count the number of the out-arcs from node \c n. |
273 | 274 |
/// |
274 | 275 |
/// This function counts the number of the out-arcs from node \c n |
275 |
/// in the graph. |
|
276 |
/// in the graph \c g. |
|
276 | 277 |
template <typename Graph> |
277 |
inline int countOutArcs(const Graph& _g, const typename Graph::Node& _n) { |
|
278 |
return countNodeDegree<Graph, typename Graph::OutArcIt>(_g, _n); |
|
278 |
inline int countOutArcs(const Graph& g, const typename Graph::Node& n) { |
|
279 |
return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n); |
|
279 | 280 |
} |
280 | 281 |
|
281 | 282 |
/// \brief Function to count the number of the in-arcs to node \c n. |
282 | 283 |
/// |
283 | 284 |
/// This function counts the number of the in-arcs to node \c n |
284 |
/// in the graph. |
|
285 |
/// in the graph \c g. |
|
285 | 286 |
template <typename Graph> |
286 |
inline int countInArcs(const Graph& _g, const typename Graph::Node& _n) { |
|
287 |
return countNodeDegree<Graph, typename Graph::InArcIt>(_g, _n); |
|
287 |
inline int countInArcs(const Graph& g, const typename Graph::Node& n) { |
|
288 |
return countNodeDegree<Graph, typename Graph::InArcIt>(g, n); |
|
288 | 289 |
} |
289 | 290 |
|
290 | 291 |
/// \brief Function to count the number of the inc-edges to node \c n. |
291 | 292 |
/// |
292 | 293 |
/// This function counts the number of the inc-edges to node \c n |
293 |
/// in the graph. |
|
294 |
/// in the undirected graph \c g. |
|
294 | 295 |
template <typename Graph> |
295 |
inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) { |
|
296 |
return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n); |
|
296 |
inline int countIncEdges(const Graph& g, const typename Graph::Node& n) { |
|
297 |
return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n); |
|
297 | 298 |
} |
298 | 299 |
|
299 | 300 |
namespace _core_bits { |
300 | 301 |
|
301 | 302 |
template <typename Digraph, typename Item, typename RefMap> |
302 | 303 |
class MapCopyBase { |
303 | 304 |
public: |
304 | 305 |
virtual void copy(const Digraph& from, const RefMap& refMap) = 0; |
305 | 306 |
|
306 | 307 |
virtual ~MapCopyBase() {} |
307 | 308 |
}; |
308 | 309 |
|
309 | 310 |
template <typename Digraph, typename Item, typename RefMap, |
310 |
typename |
|
311 |
typename FromMap, typename ToMap> |
|
311 | 312 |
class MapCopy : public MapCopyBase<Digraph, Item, RefMap> { |
312 | 313 |
public: |
313 | 314 |
|
314 |
MapCopy(ToMap& tmap, const FromMap& map) |
|
315 |
: _tmap(tmap), _map(map) {} |
|
315 |
MapCopy(const FromMap& map, ToMap& tmap) |
|
316 |
: _map(map), _tmap(tmap) {} |
|
316 | 317 |
|
317 | 318 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
318 | 319 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
319 | 320 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
320 | 321 |
_tmap.set(refMap[it], _map[it]); |
321 | 322 |
} |
322 | 323 |
} |
323 | 324 |
|
324 | 325 |
private: |
326 |
const FromMap& _map; |
|
325 | 327 |
ToMap& _tmap; |
326 |
const FromMap& _map; |
|
327 | 328 |
}; |
328 | 329 |
|
329 | 330 |
template <typename Digraph, typename Item, typename RefMap, typename It> |
330 | 331 |
class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> { |
331 | 332 |
public: |
332 | 333 |
|
333 |
ItemCopy( |
|
334 |
ItemCopy(const Item& item, It& it) : _item(item), _it(it) {} |
|
334 | 335 |
|
335 | 336 |
virtual void copy(const Digraph&, const RefMap& refMap) { |
336 | 337 |
_it = refMap[_item]; |
337 | 338 |
} |
338 | 339 |
|
339 | 340 |
private: |
341 |
Item _item; |
|
340 | 342 |
It& _it; |
341 |
Item _item; |
|
342 | 343 |
}; |
343 | 344 |
|
344 | 345 |
template <typename Digraph, typename Item, typename RefMap, typename Ref> |
345 | 346 |
class RefCopy : public MapCopyBase<Digraph, Item, RefMap> { |
346 | 347 |
public: |
347 | 348 |
|
348 | 349 |
RefCopy(Ref& map) : _map(map) {} |
349 | 350 |
|
350 | 351 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
351 | 352 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
352 | 353 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
353 | 354 |
_map.set(it, refMap[it]); |
354 | 355 |
} |
355 | 356 |
} |
356 | 357 |
|
357 | 358 |
private: |
358 | 359 |
Ref& _map; |
359 | 360 |
}; |
360 | 361 |
|
361 | 362 |
template <typename Digraph, typename Item, typename RefMap, |
362 | 363 |
typename CrossRef> |
363 | 364 |
class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> { |
364 | 365 |
public: |
365 | 366 |
|
366 | 367 |
CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {} |
367 | 368 |
|
368 | 369 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
369 | 370 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
370 | 371 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
371 | 372 |
_cmap.set(refMap[it], it); |
372 | 373 |
} |
373 | 374 |
} |
374 | 375 |
|
375 | 376 |
private: |
376 | 377 |
CrossRef& _cmap; |
377 | 378 |
}; |
378 | 379 |
|
379 | 380 |
template <typename Digraph, typename Enable = void> |
380 | 381 |
struct DigraphCopySelector { |
381 | 382 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
382 |
static void copy( |
|
383 |
static void copy(const From& from, Digraph &to, |
|
383 | 384 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
384 | 385 |
for (typename From::NodeIt it(from); it != INVALID; ++it) { |
385 | 386 |
nodeRefMap[it] = to.addNode(); |
386 | 387 |
} |
387 | 388 |
for (typename From::ArcIt it(from); it != INVALID; ++it) { |
388 | 389 |
arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
389 | 390 |
nodeRefMap[from.target(it)]); |
390 | 391 |
} |
391 | 392 |
} |
392 | 393 |
}; |
393 | 394 |
|
394 | 395 |
template <typename Digraph> |
395 | 396 |
struct DigraphCopySelector< |
396 | 397 |
Digraph, |
397 | 398 |
typename enable_if<typename Digraph::BuildTag, void>::type> |
398 | 399 |
{ |
399 | 400 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
400 |
static void copy( |
|
401 |
static void copy(const From& from, Digraph &to, |
|
401 | 402 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
402 | 403 |
to.build(from, nodeRefMap, arcRefMap); |
403 | 404 |
} |
404 | 405 |
}; |
405 | 406 |
|
406 | 407 |
template <typename Graph, typename Enable = void> |
407 | 408 |
struct GraphCopySelector { |
408 | 409 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
409 |
static void copy( |
|
410 |
static void copy(const From& from, Graph &to, |
|
410 | 411 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
411 | 412 |
for (typename From::NodeIt it(from); it != INVALID; ++it) { |
412 | 413 |
nodeRefMap[it] = to.addNode(); |
413 | 414 |
} |
414 | 415 |
for (typename From::EdgeIt it(from); it != INVALID; ++it) { |
415 | 416 |
edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)], |
416 | 417 |
nodeRefMap[from.v(it)]); |
417 | 418 |
} |
418 | 419 |
} |
419 | 420 |
}; |
420 | 421 |
|
421 | 422 |
template <typename Graph> |
422 | 423 |
struct GraphCopySelector< |
423 | 424 |
Graph, |
424 | 425 |
typename enable_if<typename Graph::BuildTag, void>::type> |
425 | 426 |
{ |
426 | 427 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
427 |
static void copy( |
|
428 |
static void copy(const From& from, Graph &to, |
|
428 | 429 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
429 | 430 |
to.build(from, nodeRefMap, edgeRefMap); |
430 | 431 |
} |
431 | 432 |
}; |
432 | 433 |
|
433 | 434 |
} |
434 | 435 |
|
435 | 436 |
/// \brief Class to copy a digraph. |
436 | 437 |
/// |
437 | 438 |
/// Class to copy a digraph to another digraph (duplicate a digraph). The |
438 |
/// simplest way of using it is through the \c |
|
439 |
/// simplest way of using it is through the \c digraphCopy() function. |
|
439 | 440 |
/// |
440 |
/// This class not |
|
441 |
/// This class not only make a copy of a digraph, but it can create |
|
441 | 442 |
/// references and cross references between the nodes and arcs of |
442 |
/// the two graphs, it can copy maps for use with the newly created |
|
443 |
/// graph and copy nodes and arcs. |
|
443 |
/// the two digraphs, and it can copy maps to use with the newly created |
|
444 |
/// digraph. |
|
444 | 445 |
/// |
445 |
/// To make a copy from a graph, first an instance of DigraphCopy |
|
446 |
/// should be created, then the data belongs to the graph should |
|
446 |
/// To make a copy from a digraph, first an instance of DigraphCopy |
|
447 |
/// should be created, then the data belongs to the digraph should |
|
447 | 448 |
/// assigned to copy. In the end, the \c run() member should be |
448 | 449 |
/// called. |
449 | 450 |
/// |
450 |
/// The next code copies a |
|
451 |
/// The next code copies a digraph with several data: |
|
451 | 452 |
///\code |
452 |
/// DigraphCopy<NewGraph, OrigGraph> dc(new_graph, orig_graph); |
|
453 |
/// // create a reference for the nodes |
|
453 |
/// DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph); |
|
454 |
/// // Create references for the nodes |
|
454 | 455 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
455 |
/// dc.nodeRef(nr); |
|
456 |
/// // create a cross reference (inverse) for the arcs |
|
456 |
/// cg.nodeRef(nr); |
|
457 |
/// // Create cross references (inverse) for the arcs |
|
457 | 458 |
/// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph); |
458 |
/// dc.arcCrossRef(acr); |
|
459 |
/// // copy an arc map |
|
459 |
/// cg.arcCrossRef(acr); |
|
460 |
/// // Copy an arc map |
|
460 | 461 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
461 | 462 |
/// NewGraph::ArcMap<double> namap(new_graph); |
462 |
/// dc.arcMap(namap, oamap); |
|
463 |
/// // copy a node |
|
463 |
/// cg.arcMap(oamap, namap); |
|
464 |
/// // Copy a node |
|
464 | 465 |
/// OrigGraph::Node on; |
465 | 466 |
/// NewGraph::Node nn; |
466 |
/// dc.node(nn, on); |
|
467 |
/// // Executions of copy |
|
468 |
/// |
|
467 |
/// cg.node(on, nn); |
|
468 |
/// // Execute copying |
|
469 |
/// cg.run(); |
|
469 | 470 |
///\endcode |
470 |
template <typename |
|
471 |
template <typename From, typename To> |
|
471 | 472 |
class DigraphCopy { |
472 | 473 |
private: |
473 | 474 |
|
474 | 475 |
typedef typename From::Node Node; |
475 | 476 |
typedef typename From::NodeIt NodeIt; |
476 | 477 |
typedef typename From::Arc Arc; |
477 | 478 |
typedef typename From::ArcIt ArcIt; |
478 | 479 |
|
479 | 480 |
typedef typename To::Node TNode; |
480 | 481 |
typedef typename To::Arc TArc; |
481 | 482 |
|
482 | 483 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
483 | 484 |
typedef typename From::template ArcMap<TArc> ArcRefMap; |
484 | 485 |
|
485 |
|
|
486 | 486 |
public: |
487 | 487 |
|
488 |
|
|
489 |
/// \brief Constructor for the DigraphCopy. |
|
488 |
/// \brief Constructor of DigraphCopy. |
|
490 | 489 |
/// |
491 |
/// It copies the content of the \c _from digraph into the |
|
492 |
/// \c _to digraph. |
|
493 |
|
|
490 |
/// Constructor of DigraphCopy for copying the content of the |
|
491 |
/// \c from digraph into the \c to digraph. |
|
492 |
DigraphCopy(const From& from, To& to) |
|
494 | 493 |
: _from(from), _to(to) {} |
495 | 494 |
|
496 |
/// \brief Destructor of |
|
495 |
/// \brief Destructor of DigraphCopy |
|
497 | 496 |
/// |
498 |
/// Destructor of |
|
497 |
/// Destructor of DigraphCopy. |
|
499 | 498 |
~DigraphCopy() { |
500 | 499 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
501 | 500 |
delete _node_maps[i]; |
502 | 501 |
} |
503 | 502 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
504 | 503 |
delete _arc_maps[i]; |
505 | 504 |
} |
506 | 505 |
|
507 | 506 |
} |
508 | 507 |
|
509 |
/// \brief |
|
508 |
/// \brief Copy the node references into the given map. |
|
510 | 509 |
/// |
511 |
/// Copies the node references into the given map. The parameter |
|
512 |
/// should be a map, which key type is the Node type of the source |
|
513 |
/// graph, while the value type is the Node type of the |
|
514 |
/// destination graph. |
|
510 |
/// This function copies the node references into the given map. |
|
511 |
/// The parameter should be a map, whose key type is the Node type of |
|
512 |
/// the source digraph, while the value type is the Node type of the |
|
513 |
/// destination digraph. |
|
515 | 514 |
template <typename NodeRef> |
516 | 515 |
DigraphCopy& nodeRef(NodeRef& map) { |
517 | 516 |
_node_maps.push_back(new _core_bits::RefCopy<From, Node, |
518 | 517 |
NodeRefMap, NodeRef>(map)); |
519 | 518 |
return *this; |
520 | 519 |
} |
521 | 520 |
|
522 |
/// \brief |
|
521 |
/// \brief Copy the node cross references into the given map. |
|
523 | 522 |
/// |
524 |
/// Copies the node cross references (reverse references) into |
|
525 |
/// the given map. The parameter should be a map, which key type |
|
526 |
/// is the Node type of the destination graph, while the value type is |
|
527 |
/// the Node type of the source graph. |
|
523 |
/// This function copies the node cross references (reverse references) |
|
524 |
/// into the given map. The parameter should be a map, whose key type |
|
525 |
/// is the Node type of the destination digraph, while the value type is |
|
526 |
/// the Node type of the source digraph. |
|
528 | 527 |
template <typename NodeCrossRef> |
529 | 528 |
DigraphCopy& nodeCrossRef(NodeCrossRef& map) { |
530 | 529 |
_node_maps.push_back(new _core_bits::CrossRefCopy<From, Node, |
531 | 530 |
NodeRefMap, NodeCrossRef>(map)); |
532 | 531 |
return *this; |
533 | 532 |
} |
534 | 533 |
|
535 |
/// \brief Make copy of the given map. |
|
534 |
/// \brief Make a copy of the given node map. |
|
536 | 535 |
/// |
537 |
/// Makes copy of the given map for the newly created digraph. |
|
538 |
/// The new map's key type is the destination graph's node type, |
|
539 |
/// and the copied map's key type is the source graph's node type. |
|
540 |
template <typename ToMap, typename FromMap> |
|
541 |
|
|
536 |
/// This function makes a copy of the given node map for the newly |
|
537 |
/// created digraph. |
|
538 |
/// The key type of the new map \c tmap should be the Node type of the |
|
539 |
/// destination digraph, and the key type of the original map \c map |
|
540 |
/// should be the Node type of the source digraph. |
|
541 |
template <typename FromMap, typename ToMap> |
|
542 |
DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) { |
|
542 | 543 |
_node_maps.push_back(new _core_bits::MapCopy<From, Node, |
543 |
NodeRefMap, |
|
544 |
NodeRefMap, FromMap, ToMap>(map, tmap)); |
|
544 | 545 |
return *this; |
545 | 546 |
} |
546 | 547 |
|
547 | 548 |
/// \brief Make a copy of the given node. |
548 | 549 |
/// |
549 |
/// Make a copy of the given node. |
|
550 |
DigraphCopy& node(TNode& tnode, const Node& snode) { |
|
550 |
/// This function makes a copy of the given node. |
|
551 |
DigraphCopy& node(const Node& node, TNode& tnode) { |
|
551 | 552 |
_node_maps.push_back(new _core_bits::ItemCopy<From, Node, |
552 |
NodeRefMap, TNode>( |
|
553 |
NodeRefMap, TNode>(node, tnode)); |
|
553 | 554 |
return *this; |
554 | 555 |
} |
555 | 556 |
|
556 |
/// \brief |
|
557 |
/// \brief Copy the arc references into the given map. |
|
557 | 558 |
/// |
558 |
/// |
|
559 |
/// This function copies the arc references into the given map. |
|
560 |
/// The parameter should be a map, whose key type is the Arc type of |
|
561 |
/// the source digraph, while the value type is the Arc type of the |
|
562 |
/// destination digraph. |
|
559 | 563 |
template <typename ArcRef> |
560 | 564 |
DigraphCopy& arcRef(ArcRef& map) { |
561 | 565 |
_arc_maps.push_back(new _core_bits::RefCopy<From, Arc, |
562 | 566 |
ArcRefMap, ArcRef>(map)); |
563 | 567 |
return *this; |
564 | 568 |
} |
565 | 569 |
|
566 |
/// \brief |
|
570 |
/// \brief Copy the arc cross references into the given map. |
|
567 | 571 |
/// |
568 |
/// Copies the arc cross references (reverse references) into |
|
569 |
/// the given map. |
|
572 |
/// This function copies the arc cross references (reverse references) |
|
573 |
/// into the given map. The parameter should be a map, whose key type |
|
574 |
/// is the Arc type of the destination digraph, while the value type is |
|
575 |
/// the Arc type of the source digraph. |
|
570 | 576 |
template <typename ArcCrossRef> |
571 | 577 |
DigraphCopy& arcCrossRef(ArcCrossRef& map) { |
572 | 578 |
_arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc, |
573 | 579 |
ArcRefMap, ArcCrossRef>(map)); |
574 | 580 |
return *this; |
575 | 581 |
} |
576 | 582 |
|
577 |
/// \brief Make copy of the given map. |
|
583 |
/// \brief Make a copy of the given arc map. |
|
578 | 584 |
/// |
579 |
/// Makes copy of the given map for the newly created digraph. |
|
580 |
/// The new map's key type is the to digraph's arc type, |
|
581 |
/// and the copied map's key type is the from digraph's arc |
|
582 |
/// type. |
|
583 |
template <typename ToMap, typename FromMap> |
|
584 |
DigraphCopy& arcMap(ToMap& tmap, const FromMap& map) { |
|
585 |
/// This function makes a copy of the given arc map for the newly |
|
586 |
/// created digraph. |
|
587 |
/// The key type of the new map \c tmap should be the Arc type of the |
|
588 |
/// destination digraph, and the key type of the original map \c map |
|
589 |
/// should be the Arc type of the source digraph. |
|
590 |
template <typename FromMap, typename ToMap> |
|
591 |
DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) { |
|
585 | 592 |
_arc_maps.push_back(new _core_bits::MapCopy<From, Arc, |
586 |
ArcRefMap, |
|
593 |
ArcRefMap, FromMap, ToMap>(map, tmap)); |
|
587 | 594 |
return *this; |
588 | 595 |
} |
589 | 596 |
|
590 | 597 |
/// \brief Make a copy of the given arc. |
591 | 598 |
/// |
592 |
/// Make a copy of the given arc. |
|
593 |
DigraphCopy& arc(TArc& tarc, const Arc& sarc) { |
|
599 |
/// This function makes a copy of the given arc. |
|
600 |
DigraphCopy& arc(const Arc& arc, TArc& tarc) { |
|
594 | 601 |
_arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, |
595 |
ArcRefMap, TArc>( |
|
602 |
ArcRefMap, TArc>(arc, tarc)); |
|
596 | 603 |
return *this; |
597 | 604 |
} |
598 | 605 |
|
599 |
/// \brief |
|
606 |
/// \brief Execute copying. |
|
600 | 607 |
/// |
601 |
/// |
|
608 |
/// This function executes the copying of the digraph along with the |
|
609 |
/// copying of the assigned data. |
|
602 | 610 |
void run() { |
603 | 611 |
NodeRefMap nodeRefMap(_from); |
604 | 612 |
ArcRefMap arcRefMap(_from); |
605 | 613 |
_core_bits::DigraphCopySelector<To>:: |
606 |
copy( |
|
614 |
copy(_from, _to, nodeRefMap, arcRefMap); |
|
607 | 615 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
608 | 616 |
_node_maps[i]->copy(_from, nodeRefMap); |
609 | 617 |
} |
610 | 618 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
611 | 619 |
_arc_maps[i]->copy(_from, arcRefMap); |
612 | 620 |
} |
613 | 621 |
} |
614 | 622 |
|
615 | 623 |
protected: |
616 | 624 |
|
617 |
|
|
618 | 625 |
const From& _from; |
619 | 626 |
To& _to; |
620 | 627 |
|
621 | 628 |
std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* > |
622 |
_node_maps; |
|
629 |
_node_maps; |
|
623 | 630 |
|
624 | 631 |
std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
625 |
_arc_maps; |
|
632 |
_arc_maps; |
|
626 | 633 |
|
627 | 634 |
}; |
628 | 635 |
|
629 | 636 |
/// \brief Copy a digraph to another digraph. |
630 | 637 |
/// |
631 |
/// Copy a digraph to another digraph. The complete usage of the |
|
632 |
/// function is detailed in the DigraphCopy class, but a short |
|
633 |
/// |
|
638 |
/// This function copies a digraph to another digraph. |
|
639 |
/// The complete usage of it is detailed in the DigraphCopy class, but |
|
640 |
/// a short example shows a basic work: |
|
634 | 641 |
///\code |
635 |
/// |
|
642 |
/// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run(); |
|
636 | 643 |
///\endcode |
637 | 644 |
/// |
638 | 645 |
/// After the copy the \c nr map will contain the mapping from the |
639 | 646 |
/// nodes of the \c from digraph to the nodes of the \c to digraph and |
640 |
/// \c |
|
647 |
/// \c acr will contain the mapping from the arcs of the \c to digraph |
|
641 | 648 |
/// to the arcs of the \c from digraph. |
642 | 649 |
/// |
643 | 650 |
/// \see DigraphCopy |
644 |
template <typename To, typename From> |
|
645 |
DigraphCopy<To, From> copyDigraph(To& to, const From& from) { |
|
646 |
|
|
651 |
template <typename From, typename To> |
|
652 |
DigraphCopy<From, To> digraphCopy(const From& from, To& to) { |
|
653 |
return DigraphCopy<From, To>(from, to); |
|
647 | 654 |
} |
648 | 655 |
|
649 | 656 |
/// \brief Class to copy a graph. |
650 | 657 |
/// |
651 | 658 |
/// Class to copy a graph to another graph (duplicate a graph). The |
652 |
/// simplest way of using it is through the \c |
|
659 |
/// simplest way of using it is through the \c graphCopy() function. |
|
653 | 660 |
/// |
654 |
/// This class not |
|
661 |
/// This class not only make a copy of a graph, but it can create |
|
655 | 662 |
/// references and cross references between the nodes, edges and arcs of |
656 |
/// the two graphs, it can copy maps for use with the newly created |
|
657 |
/// graph and copy nodes, edges and arcs. |
|
663 |
/// the two graphs, and it can copy maps for using with the newly created |
|
664 |
/// graph. |
|
658 | 665 |
/// |
659 | 666 |
/// To make a copy from a graph, first an instance of GraphCopy |
660 | 667 |
/// should be created, then the data belongs to the graph should |
661 | 668 |
/// assigned to copy. In the end, the \c run() member should be |
662 | 669 |
/// called. |
663 | 670 |
/// |
664 | 671 |
/// The next code copies a graph with several data: |
665 | 672 |
///\code |
666 |
/// GraphCopy<NewGraph, OrigGraph> dc(new_graph, orig_graph); |
|
667 |
/// // create a reference for the nodes |
|
673 |
/// GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph); |
|
674 |
/// // Create references for the nodes |
|
668 | 675 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
669 |
/// dc.nodeRef(nr); |
|
670 |
/// // create a cross reference (inverse) for the edges |
|
671 |
/// NewGraph::EdgeMap<OrigGraph::Arc> ecr(new_graph); |
|
672 |
/// dc.edgeCrossRef(ecr); |
|
673 |
/// // copy an arc map |
|
674 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
|
675 |
/// NewGraph::ArcMap<double> namap(new_graph); |
|
676 |
/// dc.arcMap(namap, oamap); |
|
677 |
/// |
|
676 |
/// cg.nodeRef(nr); |
|
677 |
/// // Create cross references (inverse) for the edges |
|
678 |
/// NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph); |
|
679 |
/// cg.edgeCrossRef(ecr); |
|
680 |
/// // Copy an edge map |
|
681 |
/// OrigGraph::EdgeMap<double> oemap(orig_graph); |
|
682 |
/// NewGraph::EdgeMap<double> nemap(new_graph); |
|
683 |
/// cg.edgeMap(oemap, nemap); |
|
684 |
/// // Copy a node |
|
678 | 685 |
/// OrigGraph::Node on; |
679 | 686 |
/// NewGraph::Node nn; |
680 |
/// dc.node(nn, on); |
|
681 |
/// // Executions of copy |
|
682 |
/// |
|
687 |
/// cg.node(on, nn); |
|
688 |
/// // Execute copying |
|
689 |
/// cg.run(); |
|
683 | 690 |
///\endcode |
684 |
template <typename |
|
691 |
template <typename From, typename To> |
|
685 | 692 |
class GraphCopy { |
686 | 693 |
private: |
687 | 694 |
|
688 | 695 |
typedef typename From::Node Node; |
689 | 696 |
typedef typename From::NodeIt NodeIt; |
690 | 697 |
typedef typename From::Arc Arc; |
691 | 698 |
typedef typename From::ArcIt ArcIt; |
692 | 699 |
typedef typename From::Edge Edge; |
693 | 700 |
typedef typename From::EdgeIt EdgeIt; |
694 | 701 |
|
695 | 702 |
typedef typename To::Node TNode; |
696 | 703 |
typedef typename To::Arc TArc; |
697 | 704 |
typedef typename To::Edge TEdge; |
698 | 705 |
|
699 | 706 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
700 | 707 |
typedef typename From::template EdgeMap<TEdge> EdgeRefMap; |
701 | 708 |
|
702 | 709 |
struct ArcRefMap { |
703 |
ArcRefMap(const To& to, |
|
710 |
ArcRefMap(const From& from, const To& to, |
|
704 | 711 |
const EdgeRefMap& edge_ref, const NodeRefMap& node_ref) |
705 |
: _to(to), |
|
712 |
: _from(from), _to(to), |
|
706 | 713 |
_edge_ref(edge_ref), _node_ref(node_ref) {} |
707 | 714 |
|
708 | 715 |
typedef typename From::Arc Key; |
709 | 716 |
typedef typename To::Arc Value; |
710 | 717 |
|
711 | 718 |
Value operator[](const Key& key) const { |
712 | 719 |
bool forward = _from.u(key) != _from.v(key) ? |
713 | 720 |
_node_ref[_from.source(key)] == |
714 | 721 |
_to.source(_to.direct(_edge_ref[key], true)) : |
715 | 722 |
_from.direction(key); |
716 | 723 |
return _to.direct(_edge_ref[key], forward); |
717 | 724 |
} |
718 | 725 |
|
726 |
const From& _from; |
|
719 | 727 |
const To& _to; |
720 |
const From& _from; |
|
721 | 728 |
const EdgeRefMap& _edge_ref; |
722 | 729 |
const NodeRefMap& _node_ref; |
723 | 730 |
}; |
724 | 731 |
|
725 |
|
|
726 | 732 |
public: |
727 | 733 |
|
728 |
|
|
729 |
/// \brief Constructor for the GraphCopy. |
|
734 |
/// \brief Constructor of GraphCopy. |
|
730 | 735 |
/// |
731 |
/// It copies the content of the \c _from graph into the |
|
732 |
/// \c _to graph. |
|
733 |
|
|
736 |
/// Constructor of GraphCopy for copying the content of the |
|
737 |
/// \c from graph into the \c to graph. |
|
738 |
GraphCopy(const From& from, To& to) |
|
734 | 739 |
: _from(from), _to(to) {} |
735 | 740 |
|
736 |
/// \brief Destructor of |
|
741 |
/// \brief Destructor of GraphCopy |
|
737 | 742 |
/// |
738 |
/// Destructor of |
|
743 |
/// Destructor of GraphCopy. |
|
739 | 744 |
~GraphCopy() { |
740 | 745 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
741 | 746 |
delete _node_maps[i]; |
742 | 747 |
} |
743 | 748 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
744 | 749 |
delete _arc_maps[i]; |
745 | 750 |
} |
746 | 751 |
for (int i = 0; i < int(_edge_maps.size()); ++i) { |
747 | 752 |
delete _edge_maps[i]; |
748 | 753 |
} |
749 |
|
|
750 | 754 |
} |
751 | 755 |
|
752 |
/// \brief |
|
756 |
/// \brief Copy the node references into the given map. |
|
753 | 757 |
/// |
754 |
/// |
|
758 |
/// This function copies the node references into the given map. |
|
759 |
/// The parameter should be a map, whose key type is the Node type of |
|
760 |
/// the source graph, while the value type is the Node type of the |
|
761 |
/// destination graph. |
|
755 | 762 |
template <typename NodeRef> |
756 | 763 |
GraphCopy& nodeRef(NodeRef& map) { |
757 | 764 |
_node_maps.push_back(new _core_bits::RefCopy<From, Node, |
758 | 765 |
NodeRefMap, NodeRef>(map)); |
759 | 766 |
return *this; |
760 | 767 |
} |
761 | 768 |
|
762 |
/// \brief |
|
769 |
/// \brief Copy the node cross references into the given map. |
|
763 | 770 |
/// |
764 |
/// Copies the node cross references (reverse references) into |
|
765 |
/// the given map. |
|
771 |
/// This function copies the node cross references (reverse references) |
|
772 |
/// into the given map. The parameter should be a map, whose key type |
|
773 |
/// is the Node type of the destination graph, while the value type is |
|
774 |
/// the Node type of the source graph. |
|
766 | 775 |
template <typename NodeCrossRef> |
767 | 776 |
GraphCopy& nodeCrossRef(NodeCrossRef& map) { |
768 | 777 |
_node_maps.push_back(new _core_bits::CrossRefCopy<From, Node, |
769 | 778 |
NodeRefMap, NodeCrossRef>(map)); |
770 | 779 |
return *this; |
771 | 780 |
} |
772 | 781 |
|
773 |
/// \brief Make copy of the given map. |
|
782 |
/// \brief Make a copy of the given node map. |
|
774 | 783 |
/// |
775 |
/// Makes copy of the given map for the newly created graph. |
|
776 |
/// The new map's key type is the to graph's node type, |
|
777 |
/// and the copied map's key type is the from graph's node |
|
778 |
/// type. |
|
779 |
template <typename ToMap, typename FromMap> |
|
780 |
GraphCopy& nodeMap(ToMap& tmap, const FromMap& map) { |
|
784 |
/// This function makes a copy of the given node map for the newly |
|
785 |
/// created graph. |
|
786 |
/// The key type of the new map \c tmap should be the Node type of the |
|
787 |
/// destination graph, and the key type of the original map \c map |
|
788 |
/// should be the Node type of the source graph. |
|
789 |
template <typename FromMap, typename ToMap> |
|
790 |
GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) { |
|
781 | 791 |
_node_maps.push_back(new _core_bits::MapCopy<From, Node, |
782 |
NodeRefMap, |
|
792 |
NodeRefMap, FromMap, ToMap>(map, tmap)); |
|
783 | 793 |
return *this; |
784 | 794 |
} |
785 | 795 |
|
786 | 796 |
/// \brief Make a copy of the given node. |
787 | 797 |
/// |
788 |
/// Make a copy of the given node. |
|
789 |
GraphCopy& node(TNode& tnode, const Node& snode) { |
|
798 |
/// This function makes a copy of the given node. |
|
799 |
GraphCopy& node(const Node& node, TNode& tnode) { |
|
790 | 800 |
_node_maps.push_back(new _core_bits::ItemCopy<From, Node, |
791 |
NodeRefMap, TNode>( |
|
801 |
NodeRefMap, TNode>(node, tnode)); |
|
792 | 802 |
return *this; |
793 | 803 |
} |
794 | 804 |
|
795 |
/// \brief |
|
805 |
/// \brief Copy the arc references into the given map. |
|
796 | 806 |
/// |
797 |
/// |
|
807 |
/// This function copies the arc references into the given map. |
|
808 |
/// The parameter should be a map, whose key type is the Arc type of |
|
809 |
/// the source graph, while the value type is the Arc type of the |
|
810 |
/// destination graph. |
|
798 | 811 |
template <typename ArcRef> |
799 | 812 |
GraphCopy& arcRef(ArcRef& map) { |
800 | 813 |
_arc_maps.push_back(new _core_bits::RefCopy<From, Arc, |
801 | 814 |
ArcRefMap, ArcRef>(map)); |
802 | 815 |
return *this; |
803 | 816 |
} |
804 | 817 |
|
805 |
/// \brief |
|
818 |
/// \brief Copy the arc cross references into the given map. |
|
806 | 819 |
/// |
807 |
/// Copies the arc cross references (reverse references) into |
|
808 |
/// the given map. |
|
820 |
/// This function copies the arc cross references (reverse references) |
|
821 |
/// into the given map. The parameter should be a map, whose key type |
|
822 |
/// is the Arc type of the destination graph, while the value type is |
|
823 |
/// the Arc type of the source graph. |
|
809 | 824 |
template <typename ArcCrossRef> |
810 | 825 |
GraphCopy& arcCrossRef(ArcCrossRef& map) { |
811 | 826 |
_arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc, |
812 | 827 |
ArcRefMap, ArcCrossRef>(map)); |
813 | 828 |
return *this; |
814 | 829 |
} |
815 | 830 |
|
816 |
/// \brief Make copy of the given map. |
|
831 |
/// \brief Make a copy of the given arc map. |
|
817 | 832 |
/// |
818 |
/// Makes copy of the given map for the newly created graph. |
|
819 |
/// The new map's key type is the to graph's arc type, |
|
820 |
/// and the copied map's key type is the from graph's arc |
|
821 |
/// type. |
|
822 |
template <typename ToMap, typename FromMap> |
|
823 |
GraphCopy& arcMap(ToMap& tmap, const FromMap& map) { |
|
833 |
/// This function makes a copy of the given arc map for the newly |
|
834 |
/// created graph. |
|
835 |
/// The key type of the new map \c tmap should be the Arc type of the |
|
836 |
/// destination graph, and the key type of the original map \c map |
|
837 |
/// should be the Arc type of the source graph. |
|
838 |
template <typename FromMap, typename ToMap> |
|
839 |
GraphCopy& arcMap(const FromMap& map, ToMap& tmap) { |
|
824 | 840 |
_arc_maps.push_back(new _core_bits::MapCopy<From, Arc, |
825 |
ArcRefMap, |
|
841 |
ArcRefMap, FromMap, ToMap>(map, tmap)); |
|
826 | 842 |
return *this; |
827 | 843 |
} |
828 | 844 |
|
829 | 845 |
/// \brief Make a copy of the given arc. |
830 | 846 |
/// |
831 |
/// Make a copy of the given arc. |
|
832 |
GraphCopy& arc(TArc& tarc, const Arc& sarc) { |
|
847 |
/// This function makes a copy of the given arc. |
|
848 |
GraphCopy& arc(const Arc& arc, TArc& tarc) { |
|
833 | 849 |
_arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, |
834 |
ArcRefMap, TArc>( |
|
850 |
ArcRefMap, TArc>(arc, tarc)); |
|
835 | 851 |
return *this; |
836 | 852 |
} |
837 | 853 |
|
838 |
/// \brief |
|
854 |
/// \brief Copy the edge references into the given map. |
|
839 | 855 |
/// |
840 |
/// |
|
856 |
/// This function copies the edge references into the given map. |
|
857 |
/// The parameter should be a map, whose key type is the Edge type of |
|
858 |
/// the source graph, while the value type is the Edge type of the |
|
859 |
/// destination graph. |
|
841 | 860 |
template <typename EdgeRef> |
842 | 861 |
GraphCopy& edgeRef(EdgeRef& map) { |
843 | 862 |
_edge_maps.push_back(new _core_bits::RefCopy<From, Edge, |
844 | 863 |
EdgeRefMap, EdgeRef>(map)); |
845 | 864 |
return *this; |
846 | 865 |
} |
847 | 866 |
|
848 |
/// \brief |
|
867 |
/// \brief Copy the edge cross references into the given map. |
|
849 | 868 |
/// |
850 |
/// Copies the edge cross references (reverse |
|
851 |
/// references) into the given map. |
|
869 |
/// This function copies the edge cross references (reverse references) |
|
870 |
/// into the given map. The parameter should be a map, whose key type |
|
871 |
/// is the Edge type of the destination graph, while the value type is |
|
872 |
/// the Edge type of the source graph. |
|
852 | 873 |
template <typename EdgeCrossRef> |
853 | 874 |
GraphCopy& edgeCrossRef(EdgeCrossRef& map) { |
854 | 875 |
_edge_maps.push_back(new _core_bits::CrossRefCopy<From, |
855 | 876 |
Edge, EdgeRefMap, EdgeCrossRef>(map)); |
856 | 877 |
return *this; |
857 | 878 |
} |
858 | 879 |
|
859 |
/// \brief Make copy of the given map. |
|
880 |
/// \brief Make a copy of the given edge map. |
|
860 | 881 |
/// |
861 |
/// Makes copy of the given map for the newly created graph. |
|
862 |
/// The new map's key type is the to graph's edge type, |
|
863 |
/// and the copied map's key type is the from graph's edge |
|
864 |
/// type. |
|
865 |
template <typename ToMap, typename FromMap> |
|
866 |
GraphCopy& edgeMap(ToMap& tmap, const FromMap& map) { |
|
882 |
/// This function makes a copy of the given edge map for the newly |
|
883 |
/// created graph. |
|
884 |
/// The key type of the new map \c tmap should be the Edge type of the |
|
885 |
/// destination graph, and the key type of the original map \c map |
|
886 |
/// should be the Edge type of the source graph. |
|
887 |
template <typename FromMap, typename ToMap> |
|
888 |
GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) { |
|
867 | 889 |
_edge_maps.push_back(new _core_bits::MapCopy<From, Edge, |
868 |
EdgeRefMap, |
|
890 |
EdgeRefMap, FromMap, ToMap>(map, tmap)); |
|
869 | 891 |
return *this; |
870 | 892 |
} |
871 | 893 |
|
872 | 894 |
/// \brief Make a copy of the given edge. |
873 | 895 |
/// |
874 |
/// Make a copy of the given edge. |
|
875 |
GraphCopy& edge(TEdge& tedge, const Edge& sedge) { |
|
896 |
/// This function makes a copy of the given edge. |
|
897 |
GraphCopy& edge(const Edge& edge, TEdge& tedge) { |
|
876 | 898 |
_edge_maps.push_back(new _core_bits::ItemCopy<From, Edge, |
877 |
EdgeRefMap, TEdge>( |
|
899 |
EdgeRefMap, TEdge>(edge, tedge)); |
|
878 | 900 |
return *this; |
879 | 901 |
} |
880 | 902 |
|
881 |
/// \brief |
|
903 |
/// \brief Execute copying. |
|
882 | 904 |
/// |
883 |
/// |
|
905 |
/// This function executes the copying of the graph along with the |
|
906 |
/// copying of the assigned data. |
|
884 | 907 |
void run() { |
885 | 908 |
NodeRefMap nodeRefMap(_from); |
886 | 909 |
EdgeRefMap edgeRefMap(_from); |
887 |
ArcRefMap arcRefMap( |
|
910 |
ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap); |
|
888 | 911 |
_core_bits::GraphCopySelector<To>:: |
889 |
copy( |
|
912 |
copy(_from, _to, nodeRefMap, edgeRefMap); |
|
890 | 913 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
891 | 914 |
_node_maps[i]->copy(_from, nodeRefMap); |
892 | 915 |
} |
893 | 916 |
for (int i = 0; i < int(_edge_maps.size()); ++i) { |
894 | 917 |
_edge_maps[i]->copy(_from, edgeRefMap); |
895 | 918 |
} |
896 | 919 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
897 | 920 |
_arc_maps[i]->copy(_from, arcRefMap); |
898 | 921 |
} |
899 | 922 |
} |
900 | 923 |
|
901 | 924 |
private: |
902 | 925 |
|
903 | 926 |
const From& _from; |
904 | 927 |
To& _to; |
905 | 928 |
|
906 | 929 |
std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* > |
907 |
_node_maps; |
|
930 |
_node_maps; |
|
908 | 931 |
|
909 | 932 |
std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
910 |
_arc_maps; |
|
933 |
_arc_maps; |
|
911 | 934 |
|
912 | 935 |
std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* > |
913 |
_edge_maps; |
|
936 |
_edge_maps; |
|
914 | 937 |
|
915 | 938 |
}; |
916 | 939 |
|
917 | 940 |
/// \brief Copy a graph to another graph. |
918 | 941 |
/// |
919 |
/// Copy a graph to another graph. The complete usage of the |
|
920 |
/// function is detailed in the GraphCopy class, but a short |
|
921 |
/// |
|
942 |
/// This function copies a graph to another graph. |
|
943 |
/// The complete usage of it is detailed in the GraphCopy class, |
|
944 |
/// but a short example shows a basic work: |
|
922 | 945 |
///\code |
923 |
/// |
|
946 |
/// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run(); |
|
924 | 947 |
///\endcode |
925 | 948 |
/// |
926 | 949 |
/// After the copy the \c nr map will contain the mapping from the |
927 | 950 |
/// nodes of the \c from graph to the nodes of the \c to graph and |
928 |
/// \c ecr will contain the mapping from the arcs of the \c to graph |
|
929 |
/// to the arcs of the \c from graph. |
|
951 |
/// \c ecr will contain the mapping from the edges of the \c to graph |
|
952 |
/// to the edges of the \c from graph. |
|
930 | 953 |
/// |
931 | 954 |
/// \see GraphCopy |
932 |
template <typename To, typename From> |
|
933 |
GraphCopy<To, From> |
|
934 |
copyGraph(To& to, const From& from) { |
|
935 |
return GraphCopy<To, From>(to, from); |
|
955 |
template <typename From, typename To> |
|
956 |
GraphCopy<From, To> |
|
957 |
graphCopy(const From& from, To& to) { |
|
958 |
return GraphCopy<From, To>(from, to); |
|
936 | 959 |
} |
937 | 960 |
|
938 | 961 |
namespace _core_bits { |
939 | 962 |
|
940 | 963 |
template <typename Graph, typename Enable = void> |
941 | 964 |
struct FindArcSelector { |
942 | 965 |
typedef typename Graph::Node Node; |
943 | 966 |
typedef typename Graph::Arc Arc; |
944 | 967 |
static Arc find(const Graph &g, Node u, Node v, Arc e) { |
945 | 968 |
if (e == INVALID) { |
946 | 969 |
g.firstOut(e, u); |
947 | 970 |
} else { |
948 | 971 |
g.nextOut(e); |
949 | 972 |
} |
950 | 973 |
while (e != INVALID && g.target(e) != v) { |
951 | 974 |
g.nextOut(e); |
952 | 975 |
} |
953 | 976 |
return e; |
954 | 977 |
} |
955 | 978 |
}; |
956 | 979 |
|
957 | 980 |
template <typename Graph> |
958 | 981 |
struct FindArcSelector< |
959 | 982 |
Graph, |
960 |
typename enable_if<typename Graph:: |
|
983 |
typename enable_if<typename Graph::FindArcTag, void>::type> |
|
961 | 984 |
{ |
962 | 985 |
typedef typename Graph::Node Node; |
963 | 986 |
typedef typename Graph::Arc Arc; |
964 | 987 |
static Arc find(const Graph &g, Node u, Node v, Arc prev) { |
965 | 988 |
return g.findArc(u, v, prev); |
966 | 989 |
} |
967 | 990 |
}; |
968 | 991 |
} |
969 | 992 |
|
970 |
/// \brief |
|
993 |
/// \brief Find an arc between two nodes of a digraph. |
|
971 | 994 |
/// |
972 |
/// |
|
995 |
/// This function finds an arc from node \c u to node \c v in the |
|
996 |
/// digraph \c g. |
|
973 | 997 |
/// |
974 | 998 |
/// If \c prev is \ref INVALID (this is the default value), then |
975 | 999 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
976 | 1000 |
/// the next arc from \c u to \c v after \c prev. |
977 | 1001 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
978 | 1002 |
/// |
979 | 1003 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
980 | 1004 |
///\code |
981 |
/// for(Arc e=findArc(g,u,v);e!=INVALID;e=findArc(g,u,v,e)) { |
|
1005 |
/// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) { |
|
982 | 1006 |
/// ... |
983 | 1007 |
/// } |
984 | 1008 |
///\endcode |
985 | 1009 |
/// |
986 |
///\sa ArcLookUp |
|
987 |
///\sa AllArcLookUp |
|
988 |
///\ |
|
1010 |
/// \note \ref ConArcIt provides iterator interface for the same |
|
1011 |
/// functionality. |
|
1012 |
/// |
|
989 | 1013 |
///\sa ConArcIt |
1014 |
///\sa ArcLookUp, AllArcLookUp, DynArcLookUp |
|
990 | 1015 |
template <typename Graph> |
991 | 1016 |
inline typename Graph::Arc |
992 | 1017 |
findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
993 | 1018 |
typename Graph::Arc prev = INVALID) { |
994 | 1019 |
return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev); |
995 | 1020 |
} |
996 | 1021 |
|
997 |
/// \brief Iterator for iterating on arcs |
|
1022 |
/// \brief Iterator for iterating on parallel arcs connecting the same nodes. |
|
998 | 1023 |
/// |
999 |
/// Iterator for iterating on arcs connected the same nodes. It is |
|
1000 |
/// higher level interface for the findArc() function. You can |
|
1024 |
/// Iterator for iterating on parallel arcs connecting the same nodes. It is |
|
1025 |
/// a higher level interface for the \ref findArc() function. You can |
|
1001 | 1026 |
/// use it the following way: |
1002 | 1027 |
///\code |
1003 | 1028 |
/// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) { |
1004 | 1029 |
/// ... |
1005 | 1030 |
/// } |
1006 | 1031 |
///\endcode |
1007 | 1032 |
/// |
1008 | 1033 |
///\sa findArc() |
1009 |
///\sa ArcLookUp |
|
1010 |
///\sa AllArcLookUp |
|
1011 |
///\sa DynArcLookUp |
|
1034 |
///\sa ArcLookUp, AllArcLookUp, DynArcLookUp |
|
1012 | 1035 |
template <typename _Graph> |
1013 | 1036 |
class ConArcIt : public _Graph::Arc { |
1014 | 1037 |
public: |
1015 | 1038 |
|
1016 | 1039 |
typedef _Graph Graph; |
1017 | 1040 |
typedef typename Graph::Arc Parent; |
1018 | 1041 |
|
1019 | 1042 |
typedef typename Graph::Arc Arc; |
1020 | 1043 |
typedef typename Graph::Node Node; |
1021 | 1044 |
|
1022 | 1045 |
/// \brief Constructor. |
1023 | 1046 |
/// |
1024 |
/// Construct a new ConArcIt iterating on the arcs which |
|
1025 |
/// connects the \c u and \c v node. |
|
1047 |
/// Construct a new ConArcIt iterating on the arcs that |
|
1048 |
/// connects nodes \c u and \c v. |
|
1026 | 1049 |
ConArcIt(const Graph& g, Node u, Node v) : _graph(g) { |
1027 | 1050 |
Parent::operator=(findArc(_graph, u, v)); |
1028 | 1051 |
} |
1029 | 1052 |
|
1030 | 1053 |
/// \brief Constructor. |
1031 | 1054 |
/// |
1032 |
/// Construct a new ConArcIt which continues the iterating from |
|
1033 |
/// the \c e arc. |
|
1055 |
/// Construct a new ConArcIt that continues the iterating from arc \c a. |
|
1034 | 1056 |
ConArcIt(const Graph& g, Arc a) : Parent(a), _graph(g) {} |
1035 | 1057 |
|
1036 | 1058 |
/// \brief Increment operator. |
1037 | 1059 |
/// |
1038 | 1060 |
/// It increments the iterator and gives back the next arc. |
1039 | 1061 |
ConArcIt& operator++() { |
1040 | 1062 |
Parent::operator=(findArc(_graph, _graph.source(*this), |
1041 | 1063 |
_graph.target(*this), *this)); |
1042 | 1064 |
return *this; |
1043 | 1065 |
} |
1044 | 1066 |
private: |
1045 | 1067 |
const Graph& _graph; |
1046 | 1068 |
}; |
1047 | 1069 |
|
1048 | 1070 |
namespace _core_bits { |
1049 | 1071 |
|
1050 | 1072 |
template <typename Graph, typename Enable = void> |
1051 | 1073 |
struct FindEdgeSelector { |
1052 | 1074 |
typedef typename Graph::Node Node; |
1053 | 1075 |
typedef typename Graph::Edge Edge; |
1054 | 1076 |
static Edge find(const Graph &g, Node u, Node v, Edge e) { |
1055 | 1077 |
bool b; |
1056 | 1078 |
if (u != v) { |
1057 | 1079 |
if (e == INVALID) { |
1058 | 1080 |
g.firstInc(e, b, u); |
1059 | 1081 |
} else { |
1060 | 1082 |
b = g.u(e) == u; |
1061 | 1083 |
g.nextInc(e, b); |
1062 | 1084 |
} |
1063 | 1085 |
while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) { |
1064 | 1086 |
g.nextInc(e, b); |
1065 | 1087 |
} |
1066 | 1088 |
} else { |
1067 | 1089 |
if (e == INVALID) { |
1068 | 1090 |
g.firstInc(e, b, u); |
1069 | 1091 |
} else { |
1070 | 1092 |
b = true; |
1071 | 1093 |
g.nextInc(e, b); |
1072 | 1094 |
} |
1073 | 1095 |
while (e != INVALID && (!b || g.v(e) != v)) { |
1074 | 1096 |
g.nextInc(e, b); |
1075 | 1097 |
} |
1076 | 1098 |
} |
1077 | 1099 |
return e; |
1078 | 1100 |
} |
1079 | 1101 |
}; |
1080 | 1102 |
|
1081 | 1103 |
template <typename Graph> |
1082 | 1104 |
struct FindEdgeSelector< |
1083 | 1105 |
Graph, |
1084 | 1106 |
typename enable_if<typename Graph::FindEdgeTag, void>::type> |
1085 | 1107 |
{ |
1086 | 1108 |
typedef typename Graph::Node Node; |
1087 | 1109 |
typedef typename Graph::Edge Edge; |
1088 | 1110 |
static Edge find(const Graph &g, Node u, Node v, Edge prev) { |
1089 | 1111 |
return g.findEdge(u, v, prev); |
1090 | 1112 |
} |
1091 | 1113 |
}; |
1092 | 1114 |
} |
1093 | 1115 |
|
1094 |
/// \brief |
|
1116 |
/// \brief Find an edge between two nodes of a graph. |
|
1095 | 1117 |
/// |
1096 |
/// Finds an edge from node \c u to node \c v in graph \c g. |
|
1097 |
/// If the node \c u and node \c v is equal then each loop edge |
|
1118 |
/// This function finds an edge from node \c u to node \c v in graph \c g. |
|
1119 |
/// If node \c u and node \c v is equal then each loop edge |
|
1098 | 1120 |
/// will be enumerated once. |
1099 | 1121 |
/// |
1100 | 1122 |
/// If \c prev is \ref INVALID (this is the default value), then |
1101 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
|
1102 |
/// the next arc from \c u to \c v after \c prev. |
|
1103 |
/// |
|
1123 |
/// it finds the first edge from \c u to \c v. Otherwise it looks for |
|
1124 |
/// the next edge from \c u to \c v after \c prev. |
|
1125 |
/// \return The found edge or \ref INVALID if there is no such an edge. |
|
1104 | 1126 |
/// |
1105 |
/// Thus you can iterate through each |
|
1127 |
/// Thus you can iterate through each edge between \c u and \c v |
|
1128 |
/// as it follows. |
|
1106 | 1129 |
///\code |
1107 |
/// for(Edge e = findEdge(g,u,v); e != INVALID; |
|
1108 |
/// e = findEdge(g,u,v,e)) { |
|
1130 |
/// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) { |
|
1109 | 1131 |
/// ... |
1110 | 1132 |
/// } |
1111 | 1133 |
///\endcode |
1112 | 1134 |
/// |
1135 |
/// \note \ref ConEdgeIt provides iterator interface for the same |
|
1136 |
/// functionality. |
|
1137 |
/// |
|
1113 | 1138 |
///\sa ConEdgeIt |
1114 |
|
|
1115 | 1139 |
template <typename Graph> |
1116 | 1140 |
inline typename Graph::Edge |
1117 | 1141 |
findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
1118 | 1142 |
typename Graph::Edge p = INVALID) { |
1119 | 1143 |
return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p); |
1120 | 1144 |
} |
1121 | 1145 |
|
1122 |
/// \brief Iterator for iterating on edges |
|
1146 |
/// \brief Iterator for iterating on parallel edges connecting the same nodes. |
|
1123 | 1147 |
/// |
1124 |
/// Iterator for iterating on edges connected the same nodes. It is |
|
1125 |
/// higher level interface for the findEdge() function. You can |
|
1148 |
/// Iterator for iterating on parallel edges connecting the same nodes. |
|
1149 |
/// It is a higher level interface for the findEdge() function. You can |
|
1126 | 1150 |
/// use it the following way: |
1127 | 1151 |
///\code |
1128 |
/// for (ConEdgeIt<Graph> it(g, |
|
1152 |
/// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) { |
|
1129 | 1153 |
/// ... |
1130 | 1154 |
/// } |
1131 | 1155 |
///\endcode |
1132 | 1156 |
/// |
1133 | 1157 |
///\sa findEdge() |
1134 | 1158 |
template <typename _Graph> |
1135 | 1159 |
class ConEdgeIt : public _Graph::Edge { |
1136 | 1160 |
public: |
1137 | 1161 |
|
1138 | 1162 |
typedef _Graph Graph; |
1139 | 1163 |
typedef typename Graph::Edge Parent; |
1140 | 1164 |
|
1141 | 1165 |
typedef typename Graph::Edge Edge; |
1142 | 1166 |
typedef typename Graph::Node Node; |
1143 | 1167 |
|
1144 | 1168 |
/// \brief Constructor. |
1145 | 1169 |
/// |
1146 |
/// Construct a new ConEdgeIt iterating on the edges which |
|
1147 |
/// connects the \c u and \c v node. |
|
1170 |
/// Construct a new ConEdgeIt iterating on the edges that |
|
1171 |
/// connects nodes \c u and \c v. |
|
1148 | 1172 |
ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g) { |
1149 | 1173 |
Parent::operator=(findEdge(_graph, u, v)); |
1150 | 1174 |
} |
1151 | 1175 |
|
1152 | 1176 |
/// \brief Constructor. |
1153 | 1177 |
/// |
1154 |
/// Construct a new ConEdgeIt which continues the iterating from |
|
1155 |
/// the \c e edge. |
|
1178 |
/// Construct a new ConEdgeIt that continues iterating from edge \c e. |
|
1156 | 1179 |
ConEdgeIt(const Graph& g, Edge e) : Parent(e), _graph(g) {} |
1157 | 1180 |
|
1158 | 1181 |
/// \brief Increment operator. |
1159 | 1182 |
/// |
1160 | 1183 |
/// It increments the iterator and gives back the next edge. |
1161 | 1184 |
ConEdgeIt& operator++() { |
1162 | 1185 |
Parent::operator=(findEdge(_graph, _graph.u(*this), |
1163 | 1186 |
_graph.v(*this), *this)); |
1164 | 1187 |
return *this; |
1165 | 1188 |
} |
1166 | 1189 |
private: |
1167 | 1190 |
const Graph& _graph; |
1168 | 1191 |
}; |
1169 | 1192 |
|
1170 | 1193 |
|
1171 |
///Dynamic arc look |
|
1194 |
///Dynamic arc look-up between given endpoints. |
|
1172 | 1195 |
|
1173 | 1196 |
///Using this class, you can find an arc in a digraph from a given |
1174 |
///source to a given target in amortized time <em>O |
|
1197 |
///source to a given target in amortized time <em>O</em>(log<em>d</em>), |
|
1175 | 1198 |
///where <em>d</em> is the out-degree of the source node. |
1176 | 1199 |
/// |
1177 | 1200 |
///It is possible to find \e all parallel arcs between two nodes with |
1178 | 1201 |
///the \c operator() member. |
1179 | 1202 |
/// |
1180 |
///See the \ref ArcLookUp and \ref AllArcLookUp classes if your |
|
1181 |
///digraph is not changed so frequently. |
|
1203 |
///This is a dynamic data structure. Consider to use \ref ArcLookUp or |
|
1204 |
///\ref AllArcLookUp if your digraph is not changed so frequently. |
|
1182 | 1205 |
/// |
1183 |
///This class uses a self-adjusting binary search tree, Sleator's |
|
1184 |
///and Tarjan's Splay tree for guarantee the logarithmic amortized |
|
1185 |
/// |
|
1206 |
///This class uses a self-adjusting binary search tree, the Splay tree |
|
1207 |
///of Sleator and Tarjan to guarantee the logarithmic amortized |
|
1208 |
///time bound for arc look-ups. This class also guarantees the |
|
1186 | 1209 |
///optimal time bound in a constant factor for any distribution of |
1187 | 1210 |
///queries. |
1188 | 1211 |
/// |
1189 | 1212 |
///\tparam G The type of the underlying digraph. |
1190 | 1213 |
/// |
1191 | 1214 |
///\sa ArcLookUp |
1192 | 1215 |
///\sa AllArcLookUp |
1193 | 1216 |
template<class G> |
1194 | 1217 |
class DynArcLookUp |
1195 | 1218 |
: protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase |
1196 | 1219 |
{ |
1197 | 1220 |
public: |
1198 | 1221 |
typedef typename ItemSetTraits<G, typename G::Arc> |
1199 | 1222 |
::ItemNotifier::ObserverBase Parent; |
1200 | 1223 |
|
1201 | 1224 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
1202 | 1225 |
typedef G Digraph; |
1203 | 1226 |
|
1204 | 1227 |
protected: |
1205 | 1228 |
|
1206 | 1229 |
class AutoNodeMap : public ItemSetTraits<G, Node>::template Map<Arc>::Type { |
1207 | 1230 |
public: |
1208 | 1231 |
|
1209 | 1232 |
typedef typename ItemSetTraits<G, Node>::template Map<Arc>::Type Parent; |
1210 | 1233 |
|
1211 | 1234 |
AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {} |
1212 | 1235 |
|
1213 | 1236 |
virtual void add(const Node& node) { |
1214 | 1237 |
Parent::add(node); |
1215 | 1238 |
Parent::set(node, INVALID); |
1216 | 1239 |
} |
1217 | 1240 |
|
1218 | 1241 |
virtual void add(const std::vector<Node>& nodes) { |
1219 | 1242 |
Parent::add(nodes); |
1220 | 1243 |
for (int i = 0; i < int(nodes.size()); ++i) { |
1221 | 1244 |
Parent::set(nodes[i], INVALID); |
1222 | 1245 |
} |
1223 | 1246 |
} |
1224 | 1247 |
|
1225 | 1248 |
virtual void build() { |
1226 | 1249 |
Parent::build(); |
1227 | 1250 |
Node it; |
1228 | 1251 |
typename Parent::Notifier* nf = Parent::notifier(); |
1229 | 1252 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
1230 | 1253 |
Parent::set(it, INVALID); |
1231 | 1254 |
} |
1232 | 1255 |
} |
1233 | 1256 |
}; |
1234 | 1257 |
|
1235 | 1258 |
const Digraph &_g; |
1236 | 1259 |
AutoNodeMap _head; |
1237 | 1260 |
typename Digraph::template ArcMap<Arc> _parent; |
1238 | 1261 |
typename Digraph::template ArcMap<Arc> _left; |
1239 | 1262 |
typename Digraph::template ArcMap<Arc> _right; |
1240 | 1263 |
|
1241 | 1264 |
class ArcLess { |
1242 | 1265 |
const Digraph &g; |
1243 | 1266 |
public: |
1244 | 1267 |
ArcLess(const Digraph &_g) : g(_g) {} |
1245 | 1268 |
bool operator()(Arc a,Arc b) const |
1246 | 1269 |
{ |
1247 | 1270 |
return g.target(a)<g.target(b); |
1248 | 1271 |
} |
1249 | 1272 |
}; |
1250 | 1273 |
|
1251 | 1274 |
public: |
1252 | 1275 |
|
1253 | 1276 |
///Constructor |
1254 | 1277 |
|
1255 | 1278 |
///Constructor. |
1256 | 1279 |
/// |
1257 | 1280 |
///It builds up the search database. |
1258 | 1281 |
DynArcLookUp(const Digraph &g) |
1259 | 1282 |
: _g(g),_head(g),_parent(g),_left(g),_right(g) |
1260 | 1283 |
{ |
1261 | 1284 |
Parent::attach(_g.notifier(typename Digraph::Arc())); |
1262 | 1285 |
refresh(); |
1263 | 1286 |
} |
1264 | 1287 |
|
1265 | 1288 |
protected: |
1266 | 1289 |
|
1267 | 1290 |
virtual void add(const Arc& arc) { |
1268 | 1291 |
insert(arc); |
1269 | 1292 |
} |
1270 | 1293 |
|
1271 | 1294 |
virtual void add(const std::vector<Arc>& arcs) { |
1272 | 1295 |
for (int i = 0; i < int(arcs.size()); ++i) { |
1273 | 1296 |
insert(arcs[i]); |
1274 | 1297 |
} |
1275 | 1298 |
} |
1276 | 1299 |
|
1277 | 1300 |
virtual void erase(const Arc& arc) { |
1278 | 1301 |
remove(arc); |
1279 | 1302 |
} |
1280 | 1303 |
|
1281 | 1304 |
virtual void erase(const std::vector<Arc>& arcs) { |
1282 | 1305 |
for (int i = 0; i < int(arcs.size()); ++i) { |
1283 | 1306 |
remove(arcs[i]); |
1284 | 1307 |
} |
1285 | 1308 |
} |
1286 | 1309 |
|
1287 | 1310 |
virtual void build() { |
1288 | 1311 |
refresh(); |
1289 | 1312 |
} |
1290 | 1313 |
|
1291 | 1314 |
virtual void clear() { |
1292 | 1315 |
for(NodeIt n(_g);n!=INVALID;++n) { |
1293 | 1316 |
_head.set(n, INVALID); |
1294 | 1317 |
} |
1295 | 1318 |
} |
1296 | 1319 |
|
1297 | 1320 |
void insert(Arc arc) { |
1298 | 1321 |
Node s = _g.source(arc); |
1299 | 1322 |
Node t = _g.target(arc); |
1300 | 1323 |
_left.set(arc, INVALID); |
1301 | 1324 |
_right.set(arc, INVALID); |
1302 | 1325 |
|
1303 | 1326 |
Arc e = _head[s]; |
1304 | 1327 |
if (e == INVALID) { |
1305 | 1328 |
_head.set(s, arc); |
1306 | 1329 |
_parent.set(arc, INVALID); |
1307 | 1330 |
return; |
1308 | 1331 |
} |
1309 | 1332 |
while (true) { |
1310 | 1333 |
if (t < _g.target(e)) { |
1311 | 1334 |
if (_left[e] == INVALID) { |
1312 | 1335 |
_left.set(e, arc); |
1313 | 1336 |
_parent.set(arc, e); |
1314 | 1337 |
splay(arc); |
1315 | 1338 |
return; |
1316 | 1339 |
} else { |
1317 | 1340 |
e = _left[e]; |
1318 | 1341 |
} |
1319 | 1342 |
} else { |
1320 | 1343 |
if (_right[e] == INVALID) { |
1321 | 1344 |
_right.set(e, arc); |
1322 | 1345 |
_parent.set(arc, e); |
1323 | 1346 |
splay(arc); |
1324 | 1347 |
return; |
1325 | 1348 |
} else { |
1326 | 1349 |
e = _right[e]; |
1327 | 1350 |
} |
1328 | 1351 |
} |
1329 | 1352 |
} |
1330 | 1353 |
} |
1331 | 1354 |
|
1332 | 1355 |
void remove(Arc arc) { |
1333 | 1356 |
if (_left[arc] == INVALID) { |
1334 | 1357 |
if (_right[arc] != INVALID) { |
1335 | 1358 |
_parent.set(_right[arc], _parent[arc]); |
1336 | 1359 |
} |
1337 | 1360 |
if (_parent[arc] != INVALID) { |
1338 | 1361 |
if (_left[_parent[arc]] == arc) { |
1339 | 1362 |
_left.set(_parent[arc], _right[arc]); |
1340 | 1363 |
} else { |
1341 | 1364 |
_right.set(_parent[arc], _right[arc]); |
1342 | 1365 |
} |
1343 | 1366 |
} else { |
1344 | 1367 |
_head.set(_g.source(arc), _right[arc]); |
1345 | 1368 |
} |
1346 | 1369 |
} else if (_right[arc] == INVALID) { |
1347 | 1370 |
_parent.set(_left[arc], _parent[arc]); |
1348 | 1371 |
if (_parent[arc] != INVALID) { |
1349 | 1372 |
if (_left[_parent[arc]] == arc) { |
1350 | 1373 |
_left.set(_parent[arc], _left[arc]); |
1351 | 1374 |
} else { |
1352 | 1375 |
_right.set(_parent[arc], _left[arc]); |
1353 | 1376 |
} |
1354 | 1377 |
} else { |
1355 | 1378 |
_head.set(_g.source(arc), _left[arc]); |
1356 | 1379 |
} |
1357 | 1380 |
} else { |
1358 | 1381 |
Arc e = _left[arc]; |
1359 | 1382 |
if (_right[e] != INVALID) { |
1360 | 1383 |
e = _right[e]; |
1361 | 1384 |
while (_right[e] != INVALID) { |
1362 | 1385 |
e = _right[e]; |
1363 | 1386 |
} |
1364 | 1387 |
Arc s = _parent[e]; |
1365 | 1388 |
_right.set(_parent[e], _left[e]); |
1366 | 1389 |
if (_left[e] != INVALID) { |
1367 | 1390 |
_parent.set(_left[e], _parent[e]); |
1368 | 1391 |
} |
1369 | 1392 |
|
1370 | 1393 |
_left.set(e, _left[arc]); |
1371 | 1394 |
_parent.set(_left[arc], e); |
1372 | 1395 |
_right.set(e, _right[arc]); |
1373 | 1396 |
_parent.set(_right[arc], e); |
1374 | 1397 |
|
1375 | 1398 |
_parent.set(e, _parent[arc]); |
1376 | 1399 |
if (_parent[arc] != INVALID) { |
1377 | 1400 |
if (_left[_parent[arc]] == arc) { |
1378 | 1401 |
_left.set(_parent[arc], e); |
1379 | 1402 |
} else { |
1380 | 1403 |
_right.set(_parent[arc], e); |
1381 | 1404 |
} |
1382 | 1405 |
} |
1383 | 1406 |
splay(s); |
1384 | 1407 |
} else { |
1385 | 1408 |
_right.set(e, _right[arc]); |
1386 | 1409 |
_parent.set(_right[arc], e); |
1387 | 1410 |
_parent.set(e, _parent[arc]); |
1388 | 1411 |
|
1389 | 1412 |
if (_parent[arc] != INVALID) { |
1390 | 1413 |
if (_left[_parent[arc]] == arc) { |
1391 | 1414 |
_left.set(_parent[arc], e); |
1392 | 1415 |
} else { |
1393 | 1416 |
_right.set(_parent[arc], e); |
1394 | 1417 |
} |
1395 | 1418 |
} else { |
1396 | 1419 |
_head.set(_g.source(arc), e); |
1397 | 1420 |
} |
1398 | 1421 |
} |
1399 | 1422 |
} |
1400 | 1423 |
} |
1401 | 1424 |
|
1402 | 1425 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
1403 | 1426 |
{ |
1404 | 1427 |
int m=(a+b)/2; |
1405 | 1428 |
Arc me=v[m]; |
1406 | 1429 |
if (a < m) { |
1407 | 1430 |
Arc left = refreshRec(v,a,m-1); |
1408 | 1431 |
_left.set(me, left); |
1409 | 1432 |
_parent.set(left, me); |
1410 | 1433 |
} else { |
1411 | 1434 |
_left.set(me, INVALID); |
1412 | 1435 |
} |
1413 | 1436 |
if (m < b) { |
1414 | 1437 |
Arc right = refreshRec(v,m+1,b); |
1415 | 1438 |
_right.set(me, right); |
1416 | 1439 |
_parent.set(right, me); |
1417 | 1440 |
} else { |
1418 | 1441 |
_right.set(me, INVALID); |
1419 | 1442 |
} |
1420 | 1443 |
return me; |
1421 | 1444 |
} |
1422 | 1445 |
|
1423 | 1446 |
void refresh() { |
1424 | 1447 |
for(NodeIt n(_g);n!=INVALID;++n) { |
1425 | 1448 |
std::vector<Arc> v; |
1426 | 1449 |
for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a); |
1427 | 1450 |
if (!v.empty()) { |
1428 | 1451 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
1429 | 1452 |
Arc head = refreshRec(v,0,v.size()-1); |
1430 | 1453 |
_head.set(n, head); |
1431 | 1454 |
_parent.set(head, INVALID); |
1432 | 1455 |
} |
1433 | 1456 |
else _head.set(n, INVALID); |
1434 | 1457 |
} |
1435 | 1458 |
} |
1436 | 1459 |
|
1437 | 1460 |
void zig(Arc v) { |
1438 | 1461 |
Arc w = _parent[v]; |
1439 | 1462 |
_parent.set(v, _parent[w]); |
1440 | 1463 |
_parent.set(w, v); |
1441 | 1464 |
_left.set(w, _right[v]); |
1442 | 1465 |
_right.set(v, w); |
1443 | 1466 |
if (_parent[v] != INVALID) { |
1444 | 1467 |
if (_right[_parent[v]] == w) { |
1445 | 1468 |
_right.set(_parent[v], v); |
1446 | 1469 |
} else { |
1447 | 1470 |
_left.set(_parent[v], v); |
1448 | 1471 |
} |
1449 | 1472 |
} |
1450 | 1473 |
if (_left[w] != INVALID){ |
1451 | 1474 |
_parent.set(_left[w], w); |
1452 | 1475 |
} |
1453 | 1476 |
} |
1454 | 1477 |
|
1455 | 1478 |
void zag(Arc v) { |
1456 | 1479 |
Arc w = _parent[v]; |
1457 | 1480 |
_parent.set(v, _parent[w]); |
1458 | 1481 |
_parent.set(w, v); |
1459 | 1482 |
_right.set(w, _left[v]); |
1460 | 1483 |
_left.set(v, w); |
1461 | 1484 |
if (_parent[v] != INVALID){ |
1462 | 1485 |
if (_left[_parent[v]] == w) { |
1463 | 1486 |
_left.set(_parent[v], v); |
1464 | 1487 |
} else { |
1465 | 1488 |
_right.set(_parent[v], v); |
1466 | 1489 |
} |
1467 | 1490 |
} |
1468 | 1491 |
if (_right[w] != INVALID){ |
1469 | 1492 |
_parent.set(_right[w], w); |
1470 | 1493 |
} |
1471 | 1494 |
} |
1472 | 1495 |
|
1473 | 1496 |
void splay(Arc v) { |
1474 | 1497 |
while (_parent[v] != INVALID) { |
1475 | 1498 |
if (v == _left[_parent[v]]) { |
1476 | 1499 |
if (_parent[_parent[v]] == INVALID) { |
1477 | 1500 |
zig(v); |
1478 | 1501 |
} else { |
1479 | 1502 |
if (_parent[v] == _left[_parent[_parent[v]]]) { |
1480 | 1503 |
zig(_parent[v]); |
1481 | 1504 |
zig(v); |
1482 | 1505 |
} else { |
1483 | 1506 |
zig(v); |
1484 | 1507 |
zag(v); |
1485 | 1508 |
} |
1486 | 1509 |
} |
1487 | 1510 |
} else { |
1488 | 1511 |
if (_parent[_parent[v]] == INVALID) { |
1489 | 1512 |
zag(v); |
1490 | 1513 |
} else { |
1491 | 1514 |
if (_parent[v] == _left[_parent[_parent[v]]]) { |
1492 | 1515 |
zag(v); |
1493 | 1516 |
zig(v); |
1494 | 1517 |
} else { |
1495 | 1518 |
zag(_parent[v]); |
1496 | 1519 |
zag(v); |
1497 | 1520 |
} |
1498 | 1521 |
} |
1499 | 1522 |
} |
1500 | 1523 |
} |
1501 | 1524 |
_head[_g.source(v)] = v; |
1502 | 1525 |
} |
1503 | 1526 |
|
1504 | 1527 |
|
1505 | 1528 |
public: |
1506 | 1529 |
|
1507 | 1530 |
///Find an arc between two nodes. |
1508 | 1531 |
|
1509 | 1532 |
///Find an arc between two nodes. |
1510 |
///\param s The source node |
|
1511 |
///\param t The target node |
|
1533 |
///\param s The source node. |
|
1534 |
///\param t The target node. |
|
1512 | 1535 |
///\param p The previous arc between \c s and \c t. It it is INVALID or |
1513 | 1536 |
///not given, the operator finds the first appropriate arc. |
1514 | 1537 |
///\return An arc from \c s to \c t after \c p or |
1515 | 1538 |
///\ref INVALID if there is no more. |
1516 | 1539 |
/// |
1517 | 1540 |
///For example, you can count the number of arcs from \c u to \c v in the |
1518 | 1541 |
///following way. |
1519 | 1542 |
///\code |
1520 | 1543 |
///DynArcLookUp<ListDigraph> ae(g); |
1521 | 1544 |
///... |
1522 |
///int n=0; |
|
1523 |
///for(Arc e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++; |
|
1545 |
///int n = 0; |
|
1546 |
///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++; |
|
1524 | 1547 |
///\endcode |
1525 | 1548 |
/// |
1526 |
///Finding the arcs take at most <em>O |
|
1549 |
///Finding the arcs take at most <em>O</em>(log<em>d</em>) |
|
1527 | 1550 |
///amortized time, specifically, the time complexity of the lookups |
1528 | 1551 |
///is equal to the optimal search tree implementation for the |
1529 | 1552 |
///current query distribution in a constant factor. |
1530 | 1553 |
/// |
1531 | 1554 |
///\note This is a dynamic data structure, therefore the data |
1532 |
///structure is updated after each graph alteration. However, |
|
1533 |
///theoretically this data structure is faster than \c ArcLookUp |
|
1534 |
/// |
|
1555 |
///structure is updated after each graph alteration. Thus although |
|
1556 |
///this data structure is theoretically faster than \ref ArcLookUp |
|
1557 |
///and \ref AllArcLookup, it often provides worse performance than |
|
1535 | 1558 |
///them. |
1536 |
/// |
|
1537 | 1559 |
Arc operator()(Node s, Node t, Arc p = INVALID) const { |
1538 | 1560 |
if (p == INVALID) { |
1539 | 1561 |
Arc a = _head[s]; |
1540 | 1562 |
if (a == INVALID) return INVALID; |
1541 | 1563 |
Arc r = INVALID; |
1542 | 1564 |
while (true) { |
1543 | 1565 |
if (_g.target(a) < t) { |
1544 | 1566 |
if (_right[a] == INVALID) { |
1545 | 1567 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1546 | 1568 |
return r; |
1547 | 1569 |
} else { |
1548 | 1570 |
a = _right[a]; |
1549 | 1571 |
} |
1550 | 1572 |
} else { |
1551 | 1573 |
if (_g.target(a) == t) { |
1552 | 1574 |
r = a; |
1553 | 1575 |
} |
1554 | 1576 |
if (_left[a] == INVALID) { |
1555 | 1577 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1556 | 1578 |
return r; |
1557 | 1579 |
} else { |
1558 | 1580 |
a = _left[a]; |
1559 | 1581 |
} |
1560 | 1582 |
} |
1561 | 1583 |
} |
1562 | 1584 |
} else { |
1563 | 1585 |
Arc a = p; |
1564 | 1586 |
if (_right[a] != INVALID) { |
1565 | 1587 |
a = _right[a]; |
1566 | 1588 |
while (_left[a] != INVALID) { |
1567 | 1589 |
a = _left[a]; |
1568 | 1590 |
} |
1569 | 1591 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1570 | 1592 |
} else { |
1571 | 1593 |
while (_parent[a] != INVALID && _right[_parent[a]] == a) { |
1572 | 1594 |
a = _parent[a]; |
1573 | 1595 |
} |
1574 | 1596 |
if (_parent[a] == INVALID) { |
1575 | 1597 |
return INVALID; |
1576 | 1598 |
} else { |
1577 | 1599 |
a = _parent[a]; |
1578 | 1600 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1579 | 1601 |
} |
1580 | 1602 |
} |
1581 | 1603 |
if (_g.target(a) == t) return a; |
1582 | 1604 |
else return INVALID; |
1583 | 1605 |
} |
1584 | 1606 |
} |
1585 | 1607 |
|
1586 | 1608 |
}; |
1587 | 1609 |
|
1588 |
///Fast arc look |
|
1610 |
///Fast arc look-up between given endpoints. |
|
1589 | 1611 |
|
1590 | 1612 |
///Using this class, you can find an arc in a digraph from a given |
1591 |
///source to a given target in time <em>O(log |
|
1613 |
///source to a given target in time <em>O</em>(log<em>d</em>), |
|
1592 | 1614 |
///where <em>d</em> is the out-degree of the source node. |
1593 | 1615 |
/// |
1594 | 1616 |
///It is not possible to find \e all parallel arcs between two nodes. |
1595 | 1617 |
///Use \ref AllArcLookUp for this purpose. |
1596 | 1618 |
/// |
1597 |
///\warning This class is static, so you should refresh() (or at least |
|
1598 |
///refresh(Node)) this data structure |
|
1599 |
///whenever the digraph changes. This is a time consuming (superlinearly |
|
1600 |
///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs). |
|
1619 |
///\warning This class is static, so you should call refresh() (or at |
|
1620 |
///least refresh(Node)) to refresh this data structure whenever the |
|
1621 |
///digraph changes. This is a time consuming (superlinearly proportional |
|
1622 |
///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs). |
|
1601 | 1623 |
/// |
1602 | 1624 |
///\tparam G The type of the underlying digraph. |
1603 | 1625 |
/// |
1604 | 1626 |
///\sa DynArcLookUp |
1605 | 1627 |
///\sa AllArcLookUp |
1606 | 1628 |
template<class G> |
1607 | 1629 |
class ArcLookUp |
1608 | 1630 |
{ |
1609 | 1631 |
public: |
1610 | 1632 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
1611 | 1633 |
typedef G Digraph; |
1612 | 1634 |
|
1613 | 1635 |
protected: |
1614 | 1636 |
const Digraph &_g; |
1615 | 1637 |
typename Digraph::template NodeMap<Arc> _head; |
1616 | 1638 |
typename Digraph::template ArcMap<Arc> _left; |
1617 | 1639 |
typename Digraph::template ArcMap<Arc> _right; |
1618 | 1640 |
|
1619 | 1641 |
class ArcLess { |
1620 | 1642 |
const Digraph &g; |
1621 | 1643 |
public: |
1622 | 1644 |
ArcLess(const Digraph &_g) : g(_g) {} |
1623 | 1645 |
bool operator()(Arc a,Arc b) const |
1624 | 1646 |
{ |
1625 | 1647 |
return g.target(a)<g.target(b); |
1626 | 1648 |
} |
1627 | 1649 |
}; |
1628 | 1650 |
|
1629 | 1651 |
public: |
1630 | 1652 |
|
1631 | 1653 |
///Constructor |
1632 | 1654 |
|
1633 | 1655 |
///Constructor. |
1634 | 1656 |
/// |
1635 | 1657 |
///It builds up the search database, which remains valid until the digraph |
1636 | 1658 |
///changes. |
1637 | 1659 |
ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();} |
1638 | 1660 |
|
1639 | 1661 |
private: |
1640 | 1662 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
1641 | 1663 |
{ |
1642 | 1664 |
int m=(a+b)/2; |
1643 | 1665 |
Arc me=v[m]; |
1644 | 1666 |
_left[me] = a<m?refreshRec(v,a,m-1):INVALID; |
1645 | 1667 |
_right[me] = m<b?refreshRec(v,m+1,b):INVALID; |
1646 | 1668 |
return me; |
1647 | 1669 |
} |
1648 | 1670 |
public: |
1649 |
///Refresh the data structure at a node. |
|
1671 |
///Refresh the search data structure at a node. |
|
1650 | 1672 |
|
1651 | 1673 |
///Build up the search database of node \c n. |
1652 | 1674 |
/// |
1653 |
///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is |
|
1654 |
///the number of the outgoing arcs of \c n. |
|
1675 |
///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> |
|
1676 |
///is the number of the outgoing arcs of \c n. |
|
1655 | 1677 |
void refresh(Node n) |
1656 | 1678 |
{ |
1657 | 1679 |
std::vector<Arc> v; |
1658 | 1680 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e); |
1659 | 1681 |
if(v.size()) { |
1660 | 1682 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
1661 | 1683 |
_head[n]=refreshRec(v,0,v.size()-1); |
1662 | 1684 |
} |
1663 | 1685 |
else _head[n]=INVALID; |
1664 | 1686 |
} |
1665 | 1687 |
///Refresh the full data structure. |
1666 | 1688 |
|
1667 | 1689 |
///Build up the full search database. In fact, it simply calls |
1668 | 1690 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
1669 | 1691 |
/// |
1670 |
///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is |
|
1671 |
///the number of the arcs of \c n and <em>D</em> is the maximum |
|
1692 |
///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is |
|
1693 |
///the number of the arcs in the digraph and <em>D</em> is the maximum |
|
1672 | 1694 |
///out-degree of the digraph. |
1673 |
|
|
1674 | 1695 |
void refresh() |
1675 | 1696 |
{ |
1676 | 1697 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(n); |
1677 | 1698 |
} |
1678 | 1699 |
|
1679 | 1700 |
///Find an arc between two nodes. |
1680 | 1701 |
|
1681 |
///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where |
|
1682 |
/// <em>d</em> is the number of outgoing arcs of \c s. |
|
1683 |
///\param s The source node |
|
1684 |
///\param t The target node |
|
1702 |
///Find an arc between two nodes in time <em>O</em>(log<em>d</em>), where |
|
1703 |
///<em>d</em> is the number of outgoing arcs of \c s. |
|
1704 |
///\param s The source node. |
|
1705 |
///\param t The target node. |
|
1685 | 1706 |
///\return An arc from \c s to \c t if there exists, |
1686 | 1707 |
///\ref INVALID otherwise. |
1687 | 1708 |
/// |
1688 | 1709 |
///\warning If you change the digraph, refresh() must be called before using |
1689 | 1710 |
///this operator. If you change the outgoing arcs of |
1690 |
///a single node \c n, then |
|
1691 |
///\ref refresh(Node) "refresh(n)" is enough. |
|
1692 |
/// |
|
1711 |
///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough. |
|
1693 | 1712 |
Arc operator()(Node s, Node t) const |
1694 | 1713 |
{ |
1695 | 1714 |
Arc e; |
1696 | 1715 |
for(e=_head[s]; |
1697 | 1716 |
e!=INVALID&&_g.target(e)!=t; |
1698 | 1717 |
e = t < _g.target(e)?_left[e]:_right[e]) ; |
1699 | 1718 |
return e; |
1700 | 1719 |
} |
1701 | 1720 |
|
1702 | 1721 |
}; |
1703 | 1722 |
|
1704 |
///Fast look |
|
1723 |
///Fast look-up of all arcs between given endpoints. |
|
1705 | 1724 |
|
1706 | 1725 |
///This class is the same as \ref ArcLookUp, with the addition |
1707 |
///that it makes it possible to find all arcs between given |
|
1726 |
///that it makes it possible to find all parallel arcs between given |
|
1727 |
///endpoints. |
|
1708 | 1728 |
/// |
1709 |
///\warning This class is static, so you should refresh() (or at least |
|
1710 |
///refresh(Node)) this data structure |
|
1711 |
///whenever the digraph changes. This is a time consuming (superlinearly |
|
1712 |
///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs). |
|
1729 |
///\warning This class is static, so you should call refresh() (or at |
|
1730 |
///least refresh(Node)) to refresh this data structure whenever the |
|
1731 |
///digraph changes. This is a time consuming (superlinearly proportional |
|
1732 |
///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs). |
|
1713 | 1733 |
/// |
1714 | 1734 |
///\tparam G The type of the underlying digraph. |
1715 | 1735 |
/// |
1716 | 1736 |
///\sa DynArcLookUp |
1717 | 1737 |
///\sa ArcLookUp |
1718 | 1738 |
template<class G> |
1719 | 1739 |
class AllArcLookUp : public ArcLookUp<G> |
1720 | 1740 |
{ |
1721 | 1741 |
using ArcLookUp<G>::_g; |
1722 | 1742 |
using ArcLookUp<G>::_right; |
1723 | 1743 |
using ArcLookUp<G>::_left; |
1724 | 1744 |
using ArcLookUp<G>::_head; |
1725 | 1745 |
|
1726 | 1746 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
1727 | 1747 |
typedef G Digraph; |
1728 | 1748 |
|
1729 | 1749 |
typename Digraph::template ArcMap<Arc> _next; |
1730 | 1750 |
|
1731 | 1751 |
Arc refreshNext(Arc head,Arc next=INVALID) |
1732 | 1752 |
{ |
1733 | 1753 |
if(head==INVALID) return next; |
1734 | 1754 |
else { |
1735 | 1755 |
next=refreshNext(_right[head],next); |
1736 |
// _next[head]=next; |
|
1737 | 1756 |
_next[head]=( next!=INVALID && _g.target(next)==_g.target(head)) |
1738 | 1757 |
? next : INVALID; |
1739 | 1758 |
return refreshNext(_left[head],head); |
1740 | 1759 |
} |
1741 | 1760 |
} |
1742 | 1761 |
|
1743 | 1762 |
void refreshNext() |
1744 | 1763 |
{ |
1745 | 1764 |
for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]); |
1746 | 1765 |
} |
1747 | 1766 |
|
1748 | 1767 |
public: |
1749 | 1768 |
///Constructor |
1750 | 1769 |
|
1751 | 1770 |
///Constructor. |
1752 | 1771 |
/// |
1753 | 1772 |
///It builds up the search database, which remains valid until the digraph |
1754 | 1773 |
///changes. |
1755 | 1774 |
AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();} |
1756 | 1775 |
|
1757 | 1776 |
///Refresh the data structure at a node. |
1758 | 1777 |
|
1759 | 1778 |
///Build up the search database of node \c n. |
1760 | 1779 |
/// |
1761 |
///It runs in time <em>O(d</em>log<em>d |
|
1780 |
///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is |
|
1762 | 1781 |
///the number of the outgoing arcs of \c n. |
1763 |
|
|
1764 | 1782 |
void refresh(Node n) |
1765 | 1783 |
{ |
1766 | 1784 |
ArcLookUp<G>::refresh(n); |
1767 | 1785 |
refreshNext(_head[n]); |
1768 | 1786 |
} |
1769 | 1787 |
|
1770 | 1788 |
///Refresh the full data structure. |
1771 | 1789 |
|
1772 | 1790 |
///Build up the full search database. In fact, it simply calls |
1773 | 1791 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
1774 | 1792 |
/// |
1775 |
///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is |
|
1776 |
///the number of the arcs of \c n and <em>D</em> is the maximum |
|
1793 |
///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is |
|
1794 |
///the number of the arcs in the digraph and <em>D</em> is the maximum |
|
1777 | 1795 |
///out-degree of the digraph. |
1778 |
|
|
1779 | 1796 |
void refresh() |
1780 | 1797 |
{ |
1781 | 1798 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]); |
1782 | 1799 |
} |
1783 | 1800 |
|
1784 | 1801 |
///Find an arc between two nodes. |
1785 | 1802 |
|
1786 | 1803 |
///Find an arc between two nodes. |
1787 |
///\param s The source node |
|
1788 |
///\param t The target node |
|
1804 |
///\param s The source node. |
|
1805 |
///\param t The target node. |
|
1789 | 1806 |
///\param prev The previous arc between \c s and \c t. It it is INVALID or |
1790 | 1807 |
///not given, the operator finds the first appropriate arc. |
1791 | 1808 |
///\return An arc from \c s to \c t after \c prev or |
1792 | 1809 |
///\ref INVALID if there is no more. |
1793 | 1810 |
/// |
1794 | 1811 |
///For example, you can count the number of arcs from \c u to \c v in the |
1795 | 1812 |
///following way. |
1796 | 1813 |
///\code |
1797 | 1814 |
///AllArcLookUp<ListDigraph> ae(g); |
1798 | 1815 |
///... |
1799 |
///int n=0; |
|
1800 |
///for(Arc e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++; |
|
1816 |
///int n = 0; |
|
1817 |
///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++; |
|
1801 | 1818 |
///\endcode |
1802 | 1819 |
/// |
1803 |
///Finding the first arc take <em>O(</em>log<em>d)</em> time, where |
|
1804 |
/// <em>d</em> is the number of outgoing arcs of \c s. Then, the |
|
1820 |
///Finding the first arc take <em>O</em>(log<em>d</em>) time, where |
|
1821 |
///<em>d</em> is the number of outgoing arcs of \c s. Then, the |
|
1805 | 1822 |
///consecutive arcs are found in constant time. |
1806 | 1823 |
/// |
1807 | 1824 |
///\warning If you change the digraph, refresh() must be called before using |
1808 | 1825 |
///this operator. If you change the outgoing arcs of |
1809 |
///a single node \c n, then |
|
1810 |
///\ref refresh(Node) "refresh(n)" is enough. |
|
1826 |
///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough. |
|
1811 | 1827 |
/// |
1812 | 1828 |
#ifdef DOXYGEN |
1813 | 1829 |
Arc operator()(Node s, Node t, Arc prev=INVALID) const {} |
1814 | 1830 |
#else |
1815 | 1831 |
using ArcLookUp<G>::operator() ; |
1816 | 1832 |
Arc operator()(Node s, Node t, Arc prev) const |
1817 | 1833 |
{ |
1818 | 1834 |
return prev==INVALID?(*this)(s,t):_next[prev]; |
1819 | 1835 |
} |
1820 | 1836 |
#endif |
1821 | 1837 |
|
1822 | 1838 |
}; |
1823 | 1839 |
|
1824 | 1840 |
/// @} |
1825 | 1841 |
|
1826 | 1842 |
} //namespace lemon |
1827 | 1843 |
|
1828 | 1844 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
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_DFS_H |
20 | 20 |
#define LEMON_DFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 | 24 |
///\brief DFS algorithm. |
25 | 25 |
|
26 | 26 |
#include <lemon/list_graph.h> |
27 | 27 |
#include <lemon/bits/path_dump.h> |
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/error.h> |
30 | 30 |
#include <lemon/assert.h> |
31 | 31 |
#include <lemon/maps.h> |
32 | 32 |
#include <lemon/path.h> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
///Default traits class of Dfs class. |
37 | 37 |
|
38 | 38 |
///Default traits class of Dfs class. |
39 | 39 |
///\tparam GR Digraph type. |
40 | 40 |
template<class GR> |
41 | 41 |
struct DfsDefaultTraits |
42 | 42 |
{ |
43 | 43 |
///The type of the digraph the algorithm runs on. |
44 | 44 |
typedef GR Digraph; |
45 | 45 |
|
46 | 46 |
///\brief The type of the map that stores the predecessor |
47 | 47 |
///arcs of the %DFS paths. |
48 | 48 |
/// |
49 | 49 |
///The type of the map that stores the predecessor |
50 | 50 |
///arcs of the %DFS paths. |
51 | 51 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
52 | 52 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
53 | 53 |
///Instantiates a \ref PredMap. |
54 | 54 |
|
55 | 55 |
///This function instantiates a \ref PredMap. |
56 | 56 |
///\param g is the digraph, to which we would like to define the |
57 | 57 |
///\ref PredMap. |
58 |
///\todo The digraph alone may be insufficient to initialize |
|
59 | 58 |
static PredMap *createPredMap(const Digraph &g) |
60 | 59 |
{ |
61 | 60 |
return new PredMap(g); |
62 | 61 |
} |
63 | 62 |
|
64 | 63 |
///The type of the map that indicates which nodes are processed. |
65 | 64 |
|
66 | 65 |
///The type of the map that indicates which nodes are processed. |
67 | 66 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
68 |
///By default it is a NullMap. |
|
69 | 67 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
70 | 68 |
///Instantiates a \ref ProcessedMap. |
71 | 69 |
|
72 | 70 |
///This function instantiates a \ref ProcessedMap. |
73 | 71 |
///\param g is the digraph, to which |
74 | 72 |
///we would like to define the \ref ProcessedMap |
75 | 73 |
#ifdef DOXYGEN |
76 | 74 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
77 | 75 |
#else |
78 | 76 |
static ProcessedMap *createProcessedMap(const Digraph &) |
79 | 77 |
#endif |
80 | 78 |
{ |
81 | 79 |
return new ProcessedMap(); |
82 | 80 |
} |
83 | 81 |
|
84 | 82 |
///The type of the map that indicates which nodes are reached. |
85 | 83 |
|
86 | 84 |
///The type of the map that indicates which nodes are reached. |
87 | 85 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
88 | 86 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
89 | 87 |
///Instantiates a \ref ReachedMap. |
90 | 88 |
|
91 | 89 |
///This function instantiates a \ref ReachedMap. |
92 | 90 |
///\param g is the digraph, to which |
93 | 91 |
///we would like to define the \ref ReachedMap. |
94 | 92 |
static ReachedMap *createReachedMap(const Digraph &g) |
95 | 93 |
{ |
96 | 94 |
return new ReachedMap(g); |
97 | 95 |
} |
98 | 96 |
|
99 | 97 |
///The type of the map that stores the distances of the nodes. |
100 | 98 |
|
101 | 99 |
///The type of the map that stores the distances of the nodes. |
102 | 100 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
103 | 101 |
typedef typename Digraph::template NodeMap<int> DistMap; |
104 | 102 |
///Instantiates a \ref DistMap. |
105 | 103 |
|
106 | 104 |
///This function instantiates a \ref DistMap. |
107 | 105 |
///\param g is the digraph, to which we would like to define the |
108 | 106 |
///\ref DistMap. |
109 | 107 |
static DistMap *createDistMap(const Digraph &g) |
110 | 108 |
{ |
111 | 109 |
return new DistMap(g); |
112 | 110 |
} |
113 | 111 |
}; |
114 | 112 |
|
115 | 113 |
///%DFS algorithm class. |
116 | 114 |
|
117 | 115 |
///\ingroup search |
118 | 116 |
///This class provides an efficient implementation of the %DFS algorithm. |
119 | 117 |
/// |
120 | 118 |
///There is also a \ref dfs() "function-type interface" for the DFS |
121 | 119 |
///algorithm, which is convenient in the simplier cases and it can be |
122 | 120 |
///used easier. |
123 | 121 |
/// |
124 | 122 |
///\tparam GR The type of the digraph the algorithm runs on. |
125 | 123 |
///The default value is \ref ListDigraph. The value of GR is not used |
126 | 124 |
///directly by \ref Dfs, it is only passed to \ref DfsDefaultTraits. |
127 | 125 |
///\tparam TR Traits class to set various data types used by the algorithm. |
128 | 126 |
///The default traits class is |
129 | 127 |
///\ref DfsDefaultTraits "DfsDefaultTraits<GR>". |
130 | 128 |
///See \ref DfsDefaultTraits for the documentation of |
131 | 129 |
///a Dfs traits class. |
132 | 130 |
#ifdef DOXYGEN |
133 | 131 |
template <typename GR, |
134 | 132 |
typename TR> |
135 | 133 |
#else |
136 | 134 |
template <typename GR=ListDigraph, |
137 | 135 |
typename TR=DfsDefaultTraits<GR> > |
138 | 136 |
#endif |
139 | 137 |
class Dfs { |
140 | 138 |
public: |
141 | 139 |
///\ref Exception for uninitialized parameters. |
142 | 140 |
|
143 | 141 |
///This error represents problems in the initialization of the |
144 | 142 |
///parameters of the algorithm. |
145 | 143 |
class UninitializedParameter : public lemon::UninitializedParameter { |
146 | 144 |
public: |
147 | 145 |
virtual const char* what() const throw() { |
148 | 146 |
return "lemon::Dfs::UninitializedParameter"; |
149 | 147 |
} |
150 | 148 |
}; |
151 | 149 |
|
152 | 150 |
///The type of the digraph the algorithm runs on. |
153 | 151 |
typedef typename TR::Digraph Digraph; |
154 | 152 |
|
155 | 153 |
///\brief The type of the map that stores the predecessor arcs of the |
156 | 154 |
///DFS paths. |
157 | 155 |
typedef typename TR::PredMap PredMap; |
158 | 156 |
///The type of the map that stores the distances of the nodes. |
159 | 157 |
typedef typename TR::DistMap DistMap; |
160 | 158 |
///The type of the map that indicates which nodes are reached. |
161 | 159 |
typedef typename TR::ReachedMap ReachedMap; |
162 | 160 |
///The type of the map that indicates which nodes are processed. |
163 | 161 |
typedef typename TR::ProcessedMap ProcessedMap; |
164 | 162 |
///The type of the paths. |
165 | 163 |
typedef PredMapPath<Digraph, PredMap> Path; |
166 | 164 |
|
167 | 165 |
///The traits class. |
168 | 166 |
typedef TR Traits; |
169 | 167 |
|
170 | 168 |
private: |
171 | 169 |
|
172 | 170 |
typedef typename Digraph::Node Node; |
173 | 171 |
typedef typename Digraph::NodeIt NodeIt; |
174 | 172 |
typedef typename Digraph::Arc Arc; |
175 | 173 |
typedef typename Digraph::OutArcIt OutArcIt; |
176 | 174 |
|
177 | 175 |
//Pointer to the underlying digraph. |
178 | 176 |
const Digraph *G; |
179 | 177 |
//Pointer to the map of predecessor arcs. |
180 | 178 |
PredMap *_pred; |
181 | 179 |
//Indicates if _pred is locally allocated (true) or not. |
182 | 180 |
bool local_pred; |
183 | 181 |
//Pointer to the map of distances. |
184 | 182 |
DistMap *_dist; |
185 | 183 |
//Indicates if _dist is locally allocated (true) or not. |
186 | 184 |
bool local_dist; |
187 | 185 |
//Pointer to the map of reached status of the nodes. |
188 | 186 |
ReachedMap *_reached; |
189 | 187 |
//Indicates if _reached is locally allocated (true) or not. |
190 | 188 |
bool local_reached; |
191 | 189 |
//Pointer to the map of processed status of the nodes. |
192 | 190 |
ProcessedMap *_processed; |
193 | 191 |
//Indicates if _processed is locally allocated (true) or not. |
194 | 192 |
bool local_processed; |
195 | 193 |
|
196 | 194 |
std::vector<typename Digraph::OutArcIt> _stack; |
197 | 195 |
int _stack_head; |
198 | 196 |
|
199 |
///Creates the maps if necessary. |
|
200 |
///\todo Better memory allocation (instead of new). |
|
197 |
//Creates the maps if necessary. |
|
201 | 198 |
void create_maps() |
202 | 199 |
{ |
203 | 200 |
if(!_pred) { |
204 | 201 |
local_pred = true; |
205 | 202 |
_pred = Traits::createPredMap(*G); |
206 | 203 |
} |
207 | 204 |
if(!_dist) { |
208 | 205 |
local_dist = true; |
209 | 206 |
_dist = Traits::createDistMap(*G); |
210 | 207 |
} |
211 | 208 |
if(!_reached) { |
212 | 209 |
local_reached = true; |
213 | 210 |
_reached = Traits::createReachedMap(*G); |
214 | 211 |
} |
215 | 212 |
if(!_processed) { |
216 | 213 |
local_processed = true; |
217 | 214 |
_processed = Traits::createProcessedMap(*G); |
218 | 215 |
} |
219 | 216 |
} |
220 | 217 |
|
221 | 218 |
protected: |
222 | 219 |
|
223 | 220 |
Dfs() {} |
224 | 221 |
|
225 | 222 |
public: |
226 | 223 |
|
227 | 224 |
typedef Dfs Create; |
228 | 225 |
|
229 | 226 |
///\name Named template parameters |
230 | 227 |
|
231 | 228 |
///@{ |
232 | 229 |
|
233 | 230 |
template <class T> |
234 | 231 |
struct SetPredMapTraits : public Traits { |
235 | 232 |
typedef T PredMap; |
236 | 233 |
static PredMap *createPredMap(const Digraph &) |
237 | 234 |
{ |
238 | 235 |
throw UninitializedParameter(); |
239 | 236 |
} |
240 | 237 |
}; |
241 | 238 |
///\brief \ref named-templ-param "Named parameter" for setting |
242 | 239 |
///\ref PredMap type. |
243 | 240 |
/// |
244 | 241 |
///\ref named-templ-param "Named parameter" for setting |
245 | 242 |
///\ref PredMap type. |
246 | 243 |
template <class T> |
247 | 244 |
struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > { |
248 | 245 |
typedef Dfs<Digraph, SetPredMapTraits<T> > Create; |
249 | 246 |
}; |
250 | 247 |
|
251 | 248 |
template <class T> |
252 | 249 |
struct SetDistMapTraits : public Traits { |
253 | 250 |
typedef T DistMap; |
254 | 251 |
static DistMap *createDistMap(const Digraph &) |
255 | 252 |
{ |
256 | 253 |
throw UninitializedParameter(); |
257 | 254 |
} |
258 | 255 |
}; |
259 | 256 |
///\brief \ref named-templ-param "Named parameter" for setting |
260 | 257 |
///\ref DistMap type. |
261 | 258 |
/// |
262 | 259 |
///\ref named-templ-param "Named parameter" for setting |
263 | 260 |
///\ref DistMap type. |
264 | 261 |
template <class T> |
265 | 262 |
struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > { |
266 | 263 |
typedef Dfs<Digraph, SetDistMapTraits<T> > Create; |
267 | 264 |
}; |
268 | 265 |
|
269 | 266 |
template <class T> |
270 | 267 |
struct SetReachedMapTraits : public Traits { |
271 | 268 |
typedef T ReachedMap; |
272 | 269 |
static ReachedMap *createReachedMap(const Digraph &) |
273 | 270 |
{ |
274 | 271 |
throw UninitializedParameter(); |
275 | 272 |
} |
276 | 273 |
}; |
277 | 274 |
///\brief \ref named-templ-param "Named parameter" for setting |
278 | 275 |
///\ref ReachedMap type. |
279 | 276 |
/// |
280 | 277 |
///\ref named-templ-param "Named parameter" for setting |
281 | 278 |
///\ref ReachedMap type. |
282 | 279 |
template <class T> |
283 | 280 |
struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > { |
284 | 281 |
typedef Dfs< Digraph, SetReachedMapTraits<T> > Create; |
285 | 282 |
}; |
286 | 283 |
|
287 | 284 |
template <class T> |
288 | 285 |
struct SetProcessedMapTraits : public Traits { |
289 | 286 |
typedef T ProcessedMap; |
290 | 287 |
static ProcessedMap *createProcessedMap(const Digraph &) |
291 | 288 |
{ |
292 | 289 |
throw UninitializedParameter(); |
293 | 290 |
} |
294 | 291 |
}; |
295 | 292 |
///\brief \ref named-templ-param "Named parameter" for setting |
296 | 293 |
///\ref ProcessedMap type. |
297 | 294 |
/// |
298 | 295 |
///\ref named-templ-param "Named parameter" for setting |
299 | 296 |
///\ref ProcessedMap type. |
300 | 297 |
template <class T> |
301 | 298 |
struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > { |
302 | 299 |
typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create; |
303 | 300 |
}; |
304 | 301 |
|
305 | 302 |
struct SetStandardProcessedMapTraits : public Traits { |
306 | 303 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
307 | 304 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
308 | 305 |
{ |
309 | 306 |
return new ProcessedMap(g); |
310 | 307 |
} |
311 | 308 |
}; |
312 | 309 |
///\brief \ref named-templ-param "Named parameter" for setting |
313 | 310 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
314 | 311 |
/// |
315 | 312 |
///\ref named-templ-param "Named parameter" for setting |
316 | 313 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
317 | 314 |
///If you don't set it explicitly, it will be automatically allocated. |
318 | 315 |
struct SetStandardProcessedMap : |
319 | 316 |
public Dfs< Digraph, SetStandardProcessedMapTraits > { |
320 | 317 |
typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create; |
321 | 318 |
}; |
322 | 319 |
|
323 | 320 |
///@} |
324 | 321 |
|
325 | 322 |
public: |
326 | 323 |
|
327 | 324 |
///Constructor. |
328 | 325 |
|
329 | 326 |
///Constructor. |
330 | 327 |
///\param g The digraph the algorithm runs on. |
331 | 328 |
Dfs(const Digraph &g) : |
332 | 329 |
G(&g), |
333 | 330 |
_pred(NULL), local_pred(false), |
334 | 331 |
_dist(NULL), local_dist(false), |
335 | 332 |
_reached(NULL), local_reached(false), |
336 | 333 |
_processed(NULL), local_processed(false) |
337 | 334 |
{ } |
338 | 335 |
|
339 | 336 |
///Destructor. |
340 | 337 |
~Dfs() |
341 | 338 |
{ |
342 | 339 |
if(local_pred) delete _pred; |
343 | 340 |
if(local_dist) delete _dist; |
344 | 341 |
if(local_reached) delete _reached; |
345 | 342 |
if(local_processed) delete _processed; |
346 | 343 |
} |
347 | 344 |
|
348 | 345 |
///Sets the map that stores the predecessor arcs. |
349 | 346 |
|
350 | 347 |
///Sets the map that stores the predecessor arcs. |
351 | 348 |
///If you don't use this function before calling \ref run(), |
352 | 349 |
///it will allocate one. The destructor deallocates this |
353 | 350 |
///automatically allocated map, of course. |
354 | 351 |
///\return <tt> (*this) </tt> |
355 | 352 |
Dfs &predMap(PredMap &m) |
356 | 353 |
{ |
357 | 354 |
if(local_pred) { |
358 | 355 |
delete _pred; |
359 | 356 |
local_pred=false; |
360 | 357 |
} |
361 | 358 |
_pred = &m; |
362 | 359 |
return *this; |
363 | 360 |
} |
364 | 361 |
|
365 | 362 |
///Sets the map that indicates which nodes are reached. |
366 | 363 |
|
367 | 364 |
///Sets the map that indicates which nodes are reached. |
368 | 365 |
///If you don't use this function before calling \ref run(), |
369 | 366 |
///it will allocate one. The destructor deallocates this |
370 | 367 |
///automatically allocated map, of course. |
371 | 368 |
///\return <tt> (*this) </tt> |
372 | 369 |
Dfs &reachedMap(ReachedMap &m) |
373 | 370 |
{ |
374 | 371 |
if(local_reached) { |
375 | 372 |
delete _reached; |
376 | 373 |
local_reached=false; |
377 | 374 |
} |
378 | 375 |
_reached = &m; |
379 | 376 |
return *this; |
380 | 377 |
} |
381 | 378 |
|
382 | 379 |
///Sets the map that indicates which nodes are processed. |
383 | 380 |
|
384 | 381 |
///Sets the map that indicates which nodes are processed. |
385 | 382 |
///If you don't use this function before calling \ref run(), |
386 | 383 |
///it will allocate one. The destructor deallocates this |
387 | 384 |
///automatically allocated map, of course. |
388 | 385 |
///\return <tt> (*this) </tt> |
389 | 386 |
Dfs &processedMap(ProcessedMap &m) |
390 | 387 |
{ |
391 | 388 |
if(local_processed) { |
392 | 389 |
delete _processed; |
393 | 390 |
local_processed=false; |
394 | 391 |
} |
395 | 392 |
_processed = &m; |
396 | 393 |
return *this; |
397 | 394 |
} |
398 | 395 |
|
399 | 396 |
///Sets the map that stores the distances of the nodes. |
400 | 397 |
|
401 | 398 |
///Sets the map that stores the distances of the nodes calculated by |
402 | 399 |
///the algorithm. |
403 | 400 |
///If you don't use this function before calling \ref run(), |
404 | 401 |
///it will allocate one. The destructor deallocates this |
405 | 402 |
///automatically allocated map, of course. |
406 | 403 |
///\return <tt> (*this) </tt> |
407 | 404 |
Dfs &distMap(DistMap &m) |
408 | 405 |
{ |
409 | 406 |
if(local_dist) { |
410 | 407 |
delete _dist; |
411 | 408 |
local_dist=false; |
412 | 409 |
} |
413 | 410 |
_dist = &m; |
414 | 411 |
return *this; |
415 | 412 |
} |
416 | 413 |
|
417 | 414 |
public: |
418 | 415 |
|
419 | 416 |
///\name Execution control |
420 | 417 |
///The simplest way to execute the algorithm is to use |
421 | 418 |
///one of the member functions called \ref lemon::Dfs::run() "run()". |
422 | 419 |
///\n |
423 | 420 |
///If you need more control on the execution, first you must call |
424 | 421 |
///\ref lemon::Dfs::init() "init()", then you can add a source node |
425 | 422 |
///with \ref lemon::Dfs::addSource() "addSource()". |
426 | 423 |
///Finally \ref lemon::Dfs::start() "start()" will perform the |
427 | 424 |
///actual path computation. |
428 | 425 |
|
429 | 426 |
///@{ |
430 | 427 |
|
431 | 428 |
///Initializes the internal data structures. |
432 | 429 |
|
433 | 430 |
///Initializes the internal data structures. |
434 | 431 |
/// |
435 | 432 |
void init() |
436 | 433 |
{ |
437 | 434 |
create_maps(); |
438 | 435 |
_stack.resize(countNodes(*G)); |
439 | 436 |
_stack_head=-1; |
440 | 437 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
441 | 438 |
_pred->set(u,INVALID); |
442 | 439 |
_reached->set(u,false); |
443 | 440 |
_processed->set(u,false); |
444 | 441 |
} |
445 | 442 |
} |
446 | 443 |
|
447 | 444 |
///Adds a new source node. |
448 | 445 |
|
449 | 446 |
///Adds a new source node to the set of nodes to be processed. |
450 | 447 |
/// |
451 | 448 |
///\pre The stack must be empty. (Otherwise the algorithm gives |
452 | 449 |
///false results.) |
453 | 450 |
/// |
454 | 451 |
///\warning Distances will be wrong (or at least strange) in case of |
455 | 452 |
///multiple sources. |
456 | 453 |
void addSource(Node s) |
457 | 454 |
{ |
458 | 455 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
459 | 456 |
if(!(*_reached)[s]) |
460 | 457 |
{ |
461 | 458 |
_reached->set(s,true); |
462 | 459 |
_pred->set(s,INVALID); |
463 | 460 |
OutArcIt e(*G,s); |
464 | 461 |
if(e!=INVALID) { |
465 | 462 |
_stack[++_stack_head]=e; |
466 | 463 |
_dist->set(s,_stack_head); |
467 | 464 |
} |
468 | 465 |
else { |
469 | 466 |
_processed->set(s,true); |
470 | 467 |
_dist->set(s,0); |
471 | 468 |
} |
472 | 469 |
} |
473 | 470 |
} |
474 | 471 |
|
475 | 472 |
///Processes the next arc. |
476 | 473 |
|
477 | 474 |
///Processes the next arc. |
478 | 475 |
/// |
479 | 476 |
///\return The processed arc. |
480 | 477 |
/// |
481 | 478 |
///\pre The stack must not be empty. |
482 | 479 |
Arc processNextArc() |
483 | 480 |
{ |
484 | 481 |
Node m; |
485 | 482 |
Arc e=_stack[_stack_head]; |
486 | 483 |
if(!(*_reached)[m=G->target(e)]) { |
487 | 484 |
_pred->set(m,e); |
488 | 485 |
_reached->set(m,true); |
489 | 486 |
++_stack_head; |
490 | 487 |
_stack[_stack_head] = OutArcIt(*G, m); |
491 | 488 |
_dist->set(m,_stack_head); |
492 | 489 |
} |
493 | 490 |
else { |
494 | 491 |
m=G->source(e); |
495 | 492 |
++_stack[_stack_head]; |
496 | 493 |
} |
497 | 494 |
while(_stack_head>=0 && _stack[_stack_head]==INVALID) { |
498 | 495 |
_processed->set(m,true); |
499 | 496 |
--_stack_head; |
500 | 497 |
if(_stack_head>=0) { |
501 | 498 |
m=G->source(_stack[_stack_head]); |
502 | 499 |
++_stack[_stack_head]; |
503 | 500 |
} |
504 | 501 |
} |
505 | 502 |
return e; |
506 | 503 |
} |
507 | 504 |
|
508 | 505 |
///Next arc to be processed. |
509 | 506 |
|
510 | 507 |
///Next arc to be processed. |
511 | 508 |
/// |
512 | 509 |
///\return The next arc to be processed or \c INVALID if the stack |
513 | 510 |
///is empty. |
514 | 511 |
OutArcIt nextArc() const |
515 | 512 |
{ |
516 | 513 |
return _stack_head>=0?_stack[_stack_head]:INVALID; |
517 | 514 |
} |
518 | 515 |
|
519 | 516 |
///\brief Returns \c false if there are nodes |
520 | 517 |
///to be processed. |
521 | 518 |
/// |
522 | 519 |
///Returns \c false if there are nodes |
523 | 520 |
///to be processed in the queue (stack). |
524 | 521 |
bool emptyQueue() const { return _stack_head<0; } |
525 | 522 |
|
526 | 523 |
///Returns the number of the nodes to be processed. |
527 | 524 |
|
528 | 525 |
///Returns the number of the nodes to be processed in the queue (stack). |
529 | 526 |
int queueSize() const { return _stack_head+1; } |
530 | 527 |
|
531 | 528 |
///Executes the algorithm. |
532 | 529 |
|
533 | 530 |
///Executes the algorithm. |
534 | 531 |
/// |
535 | 532 |
///This method runs the %DFS algorithm from the root node |
536 | 533 |
///in order to compute the DFS path to each node. |
537 | 534 |
/// |
538 | 535 |
/// The algorithm computes |
539 | 536 |
///- the %DFS tree, |
540 | 537 |
///- the distance of each node from the root in the %DFS tree. |
541 | 538 |
/// |
542 | 539 |
///\pre init() must be called and a root node should be |
543 | 540 |
///added with addSource() before using this function. |
544 | 541 |
/// |
545 | 542 |
///\note <tt>d.start()</tt> is just a shortcut of the following code. |
546 | 543 |
///\code |
547 | 544 |
/// while ( !d.emptyQueue() ) { |
548 | 545 |
/// d.processNextArc(); |
549 | 546 |
/// } |
550 | 547 |
///\endcode |
551 | 548 |
void start() |
552 | 549 |
{ |
553 | 550 |
while ( !emptyQueue() ) processNextArc(); |
554 | 551 |
} |
555 | 552 |
|
556 | 553 |
///Executes the algorithm until the given target node is reached. |
557 | 554 |
|
558 | 555 |
///Executes the algorithm until the given target node is reached. |
559 | 556 |
/// |
560 | 557 |
///This method runs the %DFS algorithm from the root node |
561 | 558 |
///in order to compute the DFS path to \c t. |
562 | 559 |
/// |
563 | 560 |
///The algorithm computes |
564 | 561 |
///- the %DFS path to \c t, |
565 | 562 |
///- the distance of \c t from the root in the %DFS tree. |
566 | 563 |
/// |
567 | 564 |
///\pre init() must be called and a root node should be |
568 | 565 |
///added with addSource() before using this function. |
569 | 566 |
void start(Node t) |
570 | 567 |
{ |
571 | 568 |
while ( !emptyQueue() && G->target(_stack[_stack_head])!=t ) |
572 | 569 |
processNextArc(); |
573 | 570 |
} |
574 | 571 |
|
575 | 572 |
///Executes the algorithm until a condition is met. |
576 | 573 |
|
577 | 574 |
///Executes the algorithm until a condition is met. |
578 | 575 |
/// |
579 | 576 |
///This method runs the %DFS algorithm from the root node |
580 | 577 |
///until an arc \c a with <tt>am[a]</tt> true is found. |
581 | 578 |
/// |
582 | 579 |
///\param am A \c bool (or convertible) arc map. The algorithm |
583 | 580 |
///will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
584 | 581 |
/// |
585 | 582 |
///\return The reached arc \c a with <tt>am[a]</tt> true or |
586 | 583 |
///\c INVALID if no such arc was found. |
587 | 584 |
/// |
588 | 585 |
///\pre init() must be called and a root node should be |
589 | 586 |
///added with addSource() before using this function. |
590 | 587 |
/// |
591 | 588 |
///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
592 | 589 |
///not a node map. |
593 | 590 |
template<class ArcBoolMap> |
594 | 591 |
Arc start(const ArcBoolMap &am) |
595 | 592 |
{ |
596 | 593 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
597 | 594 |
processNextArc(); |
598 | 595 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
599 | 596 |
} |
600 | 597 |
|
601 | 598 |
///Runs the algorithm from the given source node. |
602 | 599 |
|
603 | 600 |
///This method runs the %DFS algorithm from node \c s |
604 | 601 |
///in order to compute the DFS path to each node. |
605 | 602 |
/// |
606 | 603 |
///The algorithm computes |
607 | 604 |
///- the %DFS tree, |
608 | 605 |
///- the distance of each node from the root in the %DFS tree. |
609 | 606 |
/// |
610 | 607 |
///\note <tt>d.run(s)</tt> is just a shortcut of the following code. |
611 | 608 |
///\code |
612 | 609 |
/// d.init(); |
613 | 610 |
/// d.addSource(s); |
614 | 611 |
/// d.start(); |
615 | 612 |
///\endcode |
616 | 613 |
void run(Node s) { |
617 | 614 |
init(); |
618 | 615 |
addSource(s); |
619 | 616 |
start(); |
620 | 617 |
} |
621 | 618 |
|
622 | 619 |
///Finds the %DFS path between \c s and \c t. |
623 | 620 |
|
624 | 621 |
///This method runs the %DFS algorithm from node \c s |
625 | 622 |
///in order to compute the DFS path to node \c t |
626 | 623 |
///(it stops searching when \c t is processed) |
627 | 624 |
/// |
628 | 625 |
///\return \c true if \c t is reachable form \c s. |
629 | 626 |
/// |
630 | 627 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is |
631 | 628 |
///just a shortcut of the following code. |
632 | 629 |
///\code |
633 | 630 |
/// d.init(); |
634 | 631 |
/// d.addSource(s); |
635 | 632 |
/// d.start(t); |
636 | 633 |
///\endcode |
637 | 634 |
bool run(Node s,Node t) { |
638 | 635 |
init(); |
639 | 636 |
addSource(s); |
640 | 637 |
start(t); |
641 | 638 |
return reached(t); |
642 | 639 |
} |
643 | 640 |
|
644 | 641 |
///Runs the algorithm to visit all nodes in the digraph. |
645 | 642 |
|
646 | 643 |
///This method runs the %DFS algorithm in order to compute the |
647 | 644 |
///%DFS path to each node. |
648 | 645 |
/// |
649 | 646 |
///The algorithm computes |
650 | 647 |
///- the %DFS tree, |
651 | 648 |
///- the distance of each node from the root in the %DFS tree. |
652 | 649 |
/// |
653 | 650 |
///\note <tt>d.run()</tt> is just a shortcut of the following code. |
654 | 651 |
///\code |
655 | 652 |
/// d.init(); |
656 | 653 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
657 | 654 |
/// if (!d.reached(n)) { |
658 | 655 |
/// d.addSource(n); |
659 | 656 |
/// d.start(); |
660 | 657 |
/// } |
661 | 658 |
/// } |
662 | 659 |
///\endcode |
663 | 660 |
void run() { |
664 | 661 |
init(); |
665 | 662 |
for (NodeIt it(*G); it != INVALID; ++it) { |
666 | 663 |
if (!reached(it)) { |
667 | 664 |
addSource(it); |
668 | 665 |
start(); |
669 | 666 |
} |
670 | 667 |
} |
671 | 668 |
} |
672 | 669 |
|
673 | 670 |
///@} |
674 | 671 |
|
675 | 672 |
///\name Query Functions |
676 | 673 |
///The result of the %DFS algorithm can be obtained using these |
677 | 674 |
///functions.\n |
678 | 675 |
///Either \ref lemon::Dfs::run() "run()" or \ref lemon::Dfs::start() |
679 | 676 |
///"start()" must be called before using them. |
680 | 677 |
|
681 | 678 |
///@{ |
682 | 679 |
|
683 | 680 |
///The DFS path to a node. |
684 | 681 |
|
685 | 682 |
///Returns the DFS path to a node. |
686 | 683 |
/// |
687 | 684 |
///\warning \c t should be reachable from the root. |
688 | 685 |
/// |
689 | 686 |
///\pre Either \ref run() or \ref start() must be called before |
690 | 687 |
///using this function. |
691 | 688 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
692 | 689 |
|
693 | 690 |
///The distance of a node from the root. |
694 | 691 |
|
695 | 692 |
///Returns the distance of a node from the root. |
696 | 693 |
/// |
697 | 694 |
///\warning If node \c v is not reachable from the root, then |
698 | 695 |
///the return value of this function is undefined. |
699 | 696 |
/// |
700 | 697 |
///\pre Either \ref run() or \ref start() must be called before |
701 | 698 |
///using this function. |
702 | 699 |
int dist(Node v) const { return (*_dist)[v]; } |
703 | 700 |
|
704 | 701 |
///Returns the 'previous arc' of the %DFS tree for a node. |
705 | 702 |
|
706 | 703 |
///This function returns the 'previous arc' of the %DFS tree for the |
707 | 704 |
///node \c v, i.e. it returns the last arc of a %DFS path from the |
708 | 705 |
///root to \c v. It is \c INVALID |
709 | 706 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
710 | 707 |
/// |
711 | 708 |
///The %DFS tree used here is equal to the %DFS tree used in |
712 | 709 |
///\ref predNode(). |
713 | 710 |
/// |
714 | 711 |
///\pre Either \ref run() or \ref start() must be called before using |
715 | 712 |
///this function. |
716 | 713 |
Arc predArc(Node v) const { return (*_pred)[v];} |
717 | 714 |
|
718 | 715 |
///Returns the 'previous node' of the %DFS tree. |
719 | 716 |
|
720 | 717 |
///This function returns the 'previous node' of the %DFS |
721 | 718 |
///tree for the node \c v, i.e. it returns the last but one node |
722 | 719 |
///from a %DFS path from the root to \c v. It is \c INVALID |
723 | 720 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
724 | 721 |
/// |
725 | 722 |
///The %DFS tree used here is equal to the %DFS tree used in |
726 | 723 |
///\ref predArc(). |
727 | 724 |
/// |
728 | 725 |
///\pre Either \ref run() or \ref start() must be called before |
729 | 726 |
///using this function. |
730 | 727 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
731 | 728 |
G->source((*_pred)[v]); } |
732 | 729 |
|
733 | 730 |
///\brief Returns a const reference to the node map that stores the |
734 | 731 |
///distances of the nodes. |
735 | 732 |
/// |
736 | 733 |
///Returns a const reference to the node map that stores the |
737 | 734 |
///distances of the nodes calculated by the algorithm. |
738 | 735 |
/// |
739 | 736 |
///\pre Either \ref run() or \ref init() |
740 | 737 |
///must be called before using this function. |
741 | 738 |
const DistMap &distMap() const { return *_dist;} |
742 | 739 |
|
743 | 740 |
///\brief Returns a const reference to the node map that stores the |
744 | 741 |
///predecessor arcs. |
745 | 742 |
/// |
746 | 743 |
///Returns a const reference to the node map that stores the predecessor |
747 | 744 |
///arcs, which form the DFS tree. |
748 | 745 |
/// |
749 | 746 |
///\pre Either \ref run() or \ref init() |
750 | 747 |
///must be called before using this function. |
751 | 748 |
const PredMap &predMap() const { return *_pred;} |
752 | 749 |
|
753 | 750 |
///Checks if a node is reachable from the root(s). |
754 | 751 |
|
755 | 752 |
///Returns \c true if \c v is reachable from the root(s). |
756 | 753 |
///\pre Either \ref run() or \ref start() |
757 | 754 |
///must be called before using this function. |
758 | 755 |
bool reached(Node v) const { return (*_reached)[v]; } |
759 | 756 |
|
760 | 757 |
///@} |
761 | 758 |
}; |
762 | 759 |
|
763 | 760 |
///Default traits class of dfs() function. |
764 | 761 |
|
765 | 762 |
///Default traits class of dfs() function. |
766 | 763 |
///\tparam GR Digraph type. |
767 | 764 |
template<class GR> |
768 | 765 |
struct DfsWizardDefaultTraits |
769 | 766 |
{ |
770 | 767 |
///The type of the digraph the algorithm runs on. |
771 | 768 |
typedef GR Digraph; |
772 | 769 |
|
773 | 770 |
///\brief The type of the map that stores the predecessor |
774 | 771 |
///arcs of the %DFS paths. |
775 | 772 |
/// |
776 | 773 |
///The type of the map that stores the predecessor |
777 | 774 |
///arcs of the %DFS paths. |
778 | 775 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
779 | 776 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
780 | 777 |
///Instantiates a \ref PredMap. |
781 | 778 |
|
782 | 779 |
///This function instantiates a \ref PredMap. |
783 | 780 |
///\param g is the digraph, to which we would like to define the |
784 | 781 |
///\ref PredMap. |
785 |
///\todo The digraph alone may be insufficient to initialize |
|
786 | 782 |
static PredMap *createPredMap(const Digraph &g) |
787 | 783 |
{ |
788 | 784 |
return new PredMap(g); |
789 | 785 |
} |
790 | 786 |
|
791 | 787 |
///The type of the map that indicates which nodes are processed. |
792 | 788 |
|
793 | 789 |
///The type of the map that indicates which nodes are processed. |
794 | 790 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
795 | 791 |
///By default it is a NullMap. |
796 | 792 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
797 | 793 |
///Instantiates a \ref ProcessedMap. |
798 | 794 |
|
799 | 795 |
///This function instantiates a \ref ProcessedMap. |
800 | 796 |
///\param g is the digraph, to which |
801 | 797 |
///we would like to define the \ref ProcessedMap. |
802 | 798 |
#ifdef DOXYGEN |
803 | 799 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
804 | 800 |
#else |
805 | 801 |
static ProcessedMap *createProcessedMap(const Digraph &) |
806 | 802 |
#endif |
807 | 803 |
{ |
808 | 804 |
return new ProcessedMap(); |
809 | 805 |
} |
810 | 806 |
|
811 | 807 |
///The type of the map that indicates which nodes are reached. |
812 | 808 |
|
813 | 809 |
///The type of the map that indicates which nodes are reached. |
814 | 810 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
815 | 811 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
816 | 812 |
///Instantiates a \ref ReachedMap. |
817 | 813 |
|
818 | 814 |
///This function instantiates a \ref ReachedMap. |
819 | 815 |
///\param g is the digraph, to which |
820 | 816 |
///we would like to define the \ref ReachedMap. |
821 | 817 |
static ReachedMap *createReachedMap(const Digraph &g) |
822 | 818 |
{ |
823 | 819 |
return new ReachedMap(g); |
824 | 820 |
} |
825 | 821 |
|
826 | 822 |
///The type of the map that stores the distances of the nodes. |
827 | 823 |
|
828 | 824 |
///The type of the map that stores the distances of the nodes. |
829 | 825 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
830 | 826 |
typedef typename Digraph::template NodeMap<int> DistMap; |
831 | 827 |
///Instantiates a \ref DistMap. |
832 | 828 |
|
833 | 829 |
///This function instantiates a \ref DistMap. |
834 | 830 |
///\param g is the digraph, to which we would like to define |
835 | 831 |
///the \ref DistMap |
836 | 832 |
static DistMap *createDistMap(const Digraph &g) |
837 | 833 |
{ |
838 | 834 |
return new DistMap(g); |
839 | 835 |
} |
840 | 836 |
|
841 | 837 |
///The type of the DFS paths. |
842 | 838 |
|
843 | 839 |
///The type of the DFS paths. |
844 | 840 |
///It must meet the \ref concepts::Path "Path" concept. |
845 | 841 |
typedef lemon::Path<Digraph> Path; |
846 | 842 |
}; |
847 | 843 |
|
848 | 844 |
/// Default traits class used by \ref DfsWizard |
849 | 845 |
|
850 | 846 |
/// To make it easier to use Dfs algorithm |
851 | 847 |
/// we have created a wizard class. |
852 | 848 |
/// This \ref DfsWizard class needs default traits, |
853 | 849 |
/// as well as the \ref Dfs class. |
854 | 850 |
/// The \ref DfsWizardBase is a class to be the default traits of the |
855 | 851 |
/// \ref DfsWizard class. |
856 | 852 |
template<class GR> |
857 | 853 |
class DfsWizardBase : public DfsWizardDefaultTraits<GR> |
858 | 854 |
{ |
859 | 855 |
|
860 | 856 |
typedef DfsWizardDefaultTraits<GR> Base; |
861 | 857 |
protected: |
862 | 858 |
//The type of the nodes in the digraph. |
863 | 859 |
typedef typename Base::Digraph::Node Node; |
864 | 860 |
|
865 | 861 |
//Pointer to the digraph the algorithm runs on. |
866 | 862 |
void *_g; |
867 | 863 |
//Pointer to the map of reached nodes. |
868 | 864 |
void *_reached; |
869 | 865 |
//Pointer to the map of processed nodes. |
870 | 866 |
void *_processed; |
871 | 867 |
//Pointer to the map of predecessors arcs. |
872 | 868 |
void *_pred; |
873 | 869 |
//Pointer to the map of distances. |
874 | 870 |
void *_dist; |
875 | 871 |
//Pointer to the DFS path to the target node. |
876 | 872 |
void *_path; |
877 | 873 |
//Pointer to the distance of the target node. |
878 | 874 |
int *_di; |
879 | 875 |
|
880 | 876 |
public: |
881 | 877 |
/// Constructor. |
882 | 878 |
|
883 | 879 |
/// This constructor does not require parameters, therefore it initiates |
884 | 880 |
/// all of the attributes to \c 0. |
885 | 881 |
DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
886 | 882 |
_dist(0), _path(0), _di(0) {} |
887 | 883 |
|
888 | 884 |
/// Constructor. |
889 | 885 |
|
890 | 886 |
/// This constructor requires one parameter, |
891 | 887 |
/// others are initiated to \c 0. |
892 | 888 |
/// \param g The digraph the algorithm runs on. |
893 | 889 |
DfsWizardBase(const GR &g) : |
894 | 890 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
895 | 891 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
896 | 892 |
|
897 | 893 |
}; |
898 | 894 |
|
899 | 895 |
/// Auxiliary class for the function-type interface of DFS algorithm. |
900 | 896 |
|
901 | 897 |
/// This auxiliary class is created to implement the |
902 | 898 |
/// \ref dfs() "function-type interface" of \ref Dfs algorithm. |
903 | 899 |
/// It does not have own \ref run() method, it uses the functions |
904 | 900 |
/// and features of the plain \ref Dfs. |
905 | 901 |
/// |
906 | 902 |
/// This class should only be used through the \ref dfs() function, |
907 | 903 |
/// which makes it easier to use the algorithm. |
908 | 904 |
template<class TR> |
909 | 905 |
class DfsWizard : public TR |
910 | 906 |
{ |
911 | 907 |
typedef TR Base; |
912 | 908 |
|
913 | 909 |
///The type of the digraph the algorithm runs on. |
914 | 910 |
typedef typename TR::Digraph Digraph; |
915 | 911 |
|
916 | 912 |
typedef typename Digraph::Node Node; |
917 | 913 |
typedef typename Digraph::NodeIt NodeIt; |
918 | 914 |
typedef typename Digraph::Arc Arc; |
919 | 915 |
typedef typename Digraph::OutArcIt OutArcIt; |
920 | 916 |
|
921 | 917 |
///\brief The type of the map that stores the predecessor |
922 | 918 |
///arcs of the DFS paths. |
923 | 919 |
typedef typename TR::PredMap PredMap; |
924 | 920 |
///\brief The type of the map that stores the distances of the nodes. |
925 | 921 |
typedef typename TR::DistMap DistMap; |
926 | 922 |
///\brief The type of the map that indicates which nodes are reached. |
927 | 923 |
typedef typename TR::ReachedMap ReachedMap; |
928 | 924 |
///\brief The type of the map that indicates which nodes are processed. |
929 | 925 |
typedef typename TR::ProcessedMap ProcessedMap; |
930 | 926 |
///The type of the DFS paths |
931 | 927 |
typedef typename TR::Path Path; |
932 | 928 |
|
933 | 929 |
public: |
934 | 930 |
|
935 | 931 |
/// Constructor. |
936 | 932 |
DfsWizard() : TR() {} |
937 | 933 |
|
938 | 934 |
/// Constructor that requires parameters. |
939 | 935 |
|
940 | 936 |
/// Constructor that requires parameters. |
941 | 937 |
/// These parameters will be the default values for the traits class. |
942 | 938 |
/// \param g The digraph the algorithm runs on. |
943 | 939 |
DfsWizard(const Digraph &g) : |
944 | 940 |
TR(g) {} |
945 | 941 |
|
946 | 942 |
///Copy constructor |
947 | 943 |
DfsWizard(const TR &b) : TR(b) {} |
948 | 944 |
|
949 | 945 |
~DfsWizard() {} |
950 | 946 |
|
951 | 947 |
///Runs DFS algorithm from the given source node. |
952 | 948 |
|
953 | 949 |
///This method runs DFS algorithm from node \c s |
954 | 950 |
///in order to compute the DFS path to each node. |
955 | 951 |
void run(Node s) |
956 | 952 |
{ |
957 | 953 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
958 | 954 |
if (Base::_pred) |
959 | 955 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
960 | 956 |
if (Base::_dist) |
961 | 957 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
962 | 958 |
if (Base::_reached) |
963 | 959 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
964 | 960 |
if (Base::_processed) |
965 | 961 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
966 | 962 |
if (s!=INVALID) |
967 | 963 |
alg.run(s); |
968 | 964 |
else |
969 | 965 |
alg.run(); |
970 | 966 |
} |
971 | 967 |
|
972 | 968 |
///Finds the DFS path between \c s and \c t. |
973 | 969 |
|
974 | 970 |
///This method runs DFS algorithm from node \c s |
975 | 971 |
///in order to compute the DFS path to node \c t |
976 | 972 |
///(it stops searching when \c t is processed). |
977 | 973 |
/// |
978 | 974 |
///\return \c true if \c t is reachable form \c s. |
979 | 975 |
bool run(Node s, Node t) |
980 | 976 |
{ |
981 | 977 |
if (s==INVALID || t==INVALID) throw UninitializedParameter(); |
982 | 978 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
983 | 979 |
if (Base::_pred) |
984 | 980 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
985 | 981 |
if (Base::_dist) |
986 | 982 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
987 | 983 |
if (Base::_reached) |
988 | 984 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
989 | 985 |
if (Base::_processed) |
990 | 986 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
991 | 987 |
alg.run(s,t); |
992 | 988 |
if (Base::_path) |
993 | 989 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
994 | 990 |
if (Base::_di) |
995 | 991 |
*Base::_di = alg.dist(t); |
996 | 992 |
return alg.reached(t); |
997 | 993 |
} |
998 | 994 |
|
999 | 995 |
///Runs DFS algorithm to visit all nodes in the digraph. |
1000 | 996 |
|
1001 | 997 |
///This method runs DFS algorithm in order to compute |
1002 | 998 |
///the DFS path to each node. |
1003 | 999 |
void run() |
1004 | 1000 |
{ |
1005 | 1001 |
run(INVALID); |
1006 | 1002 |
} |
1007 | 1003 |
|
1008 | 1004 |
template<class T> |
1009 | 1005 |
struct SetPredMapBase : public Base { |
1010 | 1006 |
typedef T PredMap; |
1011 | 1007 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1012 | 1008 |
SetPredMapBase(const TR &b) : TR(b) {} |
1013 | 1009 |
}; |
1014 | 1010 |
///\brief \ref named-func-param "Named parameter" |
1015 | 1011 |
///for setting \ref PredMap object. |
1016 | 1012 |
/// |
1017 | 1013 |
///\ref named-func-param "Named parameter" |
1018 | 1014 |
///for setting \ref PredMap object. |
1019 | 1015 |
template<class T> |
1020 | 1016 |
DfsWizard<SetPredMapBase<T> > predMap(const T &t) |
1021 | 1017 |
{ |
1022 | 1018 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1023 | 1019 |
return DfsWizard<SetPredMapBase<T> >(*this); |
1024 | 1020 |
} |
1025 | 1021 |
|
1026 | 1022 |
template<class T> |
1027 | 1023 |
struct SetReachedMapBase : public Base { |
1028 | 1024 |
typedef T ReachedMap; |
1029 | 1025 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
1030 | 1026 |
SetReachedMapBase(const TR &b) : TR(b) {} |
1031 | 1027 |
}; |
1032 | 1028 |
///\brief \ref named-func-param "Named parameter" |
1033 | 1029 |
///for setting \ref ReachedMap object. |
1034 | 1030 |
/// |
1035 | 1031 |
/// \ref named-func-param "Named parameter" |
1036 | 1032 |
///for setting \ref ReachedMap object. |
1037 | 1033 |
template<class T> |
1038 | 1034 |
DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
1039 | 1035 |
{ |
1040 | 1036 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1041 | 1037 |
return DfsWizard<SetReachedMapBase<T> >(*this); |
1042 | 1038 |
} |
1043 | 1039 |
|
1044 | 1040 |
template<class T> |
1045 | 1041 |
struct SetDistMapBase : public Base { |
1046 | 1042 |
typedef T DistMap; |
1047 | 1043 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1048 | 1044 |
SetDistMapBase(const TR &b) : TR(b) {} |
1049 | 1045 |
}; |
1050 | 1046 |
///\brief \ref named-func-param "Named parameter" |
1051 | 1047 |
///for setting \ref DistMap object. |
1052 | 1048 |
/// |
1053 | 1049 |
/// \ref named-func-param "Named parameter" |
1054 | 1050 |
///for setting \ref DistMap object. |
1055 | 1051 |
template<class T> |
1056 | 1052 |
DfsWizard<SetDistMapBase<T> > distMap(const T &t) |
1057 | 1053 |
{ |
1058 | 1054 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1059 | 1055 |
return DfsWizard<SetDistMapBase<T> >(*this); |
1060 | 1056 |
} |
1061 | 1057 |
|
1062 | 1058 |
template<class T> |
1063 | 1059 |
struct SetProcessedMapBase : public Base { |
1064 | 1060 |
typedef T ProcessedMap; |
1065 | 1061 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1066 | 1062 |
SetProcessedMapBase(const TR &b) : TR(b) {} |
1067 | 1063 |
}; |
1068 | 1064 |
///\brief \ref named-func-param "Named parameter" |
1069 | 1065 |
///for setting \ref ProcessedMap object. |
1070 | 1066 |
/// |
1071 | 1067 |
/// \ref named-func-param "Named parameter" |
1072 | 1068 |
///for setting \ref ProcessedMap object. |
1073 | 1069 |
template<class T> |
1074 | 1070 |
DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
1075 | 1071 |
{ |
1076 | 1072 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1077 | 1073 |
return DfsWizard<SetProcessedMapBase<T> >(*this); |
1078 | 1074 |
} |
1079 | 1075 |
|
1080 | 1076 |
template<class T> |
1081 | 1077 |
struct SetPathBase : public Base { |
1082 | 1078 |
typedef T Path; |
1083 | 1079 |
SetPathBase(const TR &b) : TR(b) {} |
1084 | 1080 |
}; |
1085 | 1081 |
///\brief \ref named-func-param "Named parameter" |
1086 | 1082 |
///for getting the DFS path to the target node. |
1087 | 1083 |
/// |
1088 | 1084 |
///\ref named-func-param "Named parameter" |
1089 | 1085 |
///for getting the DFS path to the target node. |
1090 | 1086 |
template<class T> |
1091 | 1087 |
DfsWizard<SetPathBase<T> > path(const T &t) |
1092 | 1088 |
{ |
1093 | 1089 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1094 | 1090 |
return DfsWizard<SetPathBase<T> >(*this); |
1095 | 1091 |
} |
1096 | 1092 |
|
1097 | 1093 |
///\brief \ref named-func-param "Named parameter" |
1098 | 1094 |
///for getting the distance of the target node. |
1099 | 1095 |
/// |
1100 | 1096 |
///\ref named-func-param "Named parameter" |
1101 | 1097 |
///for getting the distance of the target node. |
1102 | 1098 |
DfsWizard dist(const int &d) |
1103 | 1099 |
{ |
1104 | 1100 |
Base::_di=const_cast<int*>(&d); |
1105 | 1101 |
return *this; |
1106 | 1102 |
} |
1107 | 1103 |
|
1108 | 1104 |
}; |
1109 | 1105 |
|
1110 | 1106 |
///Function-type interface for DFS algorithm. |
1111 | 1107 |
|
1112 | 1108 |
///\ingroup search |
1113 | 1109 |
///Function-type interface for DFS algorithm. |
1114 | 1110 |
/// |
1115 | 1111 |
///This function also has several \ref named-func-param "named parameters", |
1116 | 1112 |
///they are declared as the members of class \ref DfsWizard. |
1117 | 1113 |
///The following examples show how to use these parameters. |
1118 | 1114 |
///\code |
1119 | 1115 |
/// // Compute the DFS tree |
1120 | 1116 |
/// dfs(g).predMap(preds).distMap(dists).run(s); |
1121 | 1117 |
/// |
1122 | 1118 |
/// // Compute the DFS path from s to t |
1123 | 1119 |
/// bool reached = dfs(g).path(p).dist(d).run(s,t); |
1124 | 1120 |
///\endcode |
1125 | 1121 |
|
1126 | 1122 |
///\warning Don't forget to put the \ref DfsWizard::run() "run()" |
1127 | 1123 |
///to the end of the parameter list. |
1128 | 1124 |
///\sa DfsWizard |
1129 | 1125 |
///\sa Dfs |
1130 | 1126 |
template<class GR> |
1131 | 1127 |
DfsWizard<DfsWizardBase<GR> > |
1132 | 1128 |
dfs(const GR &digraph) |
1133 | 1129 |
{ |
1134 | 1130 |
return DfsWizard<DfsWizardBase<GR> >(digraph); |
1135 | 1131 |
} |
1136 | 1132 |
|
1137 | 1133 |
#ifdef DOXYGEN |
1138 | 1134 |
/// \brief Visitor class for DFS. |
1139 | 1135 |
/// |
1140 | 1136 |
/// This class defines the interface of the DfsVisit events, and |
1141 | 1137 |
/// it could be the base of a real visitor class. |
1142 | 1138 |
template <typename _Digraph> |
1143 | 1139 |
struct DfsVisitor { |
1144 | 1140 |
typedef _Digraph Digraph; |
1145 | 1141 |
typedef typename Digraph::Arc Arc; |
1146 | 1142 |
typedef typename Digraph::Node Node; |
1147 | 1143 |
/// \brief Called for the source node of the DFS. |
1148 | 1144 |
/// |
1149 | 1145 |
/// This function is called for the source node of the DFS. |
1150 | 1146 |
void start(const Node& node) {} |
1151 | 1147 |
/// \brief Called when the source node is leaved. |
1152 | 1148 |
/// |
1153 | 1149 |
/// This function is called when the source node is leaved. |
1154 | 1150 |
void stop(const Node& node) {} |
1155 | 1151 |
/// \brief Called when a node is reached first time. |
1156 | 1152 |
/// |
1157 | 1153 |
/// This function is called when a node is reached first time. |
1158 | 1154 |
void reach(const Node& node) {} |
1159 | 1155 |
/// \brief Called when an arc reaches a new node. |
1160 | 1156 |
/// |
1161 | 1157 |
/// This function is called when the DFS finds an arc whose target node |
1162 | 1158 |
/// is not reached yet. |
1163 | 1159 |
void discover(const Arc& arc) {} |
1164 | 1160 |
/// \brief Called when an arc is examined but its target node is |
1165 | 1161 |
/// already discovered. |
1166 | 1162 |
/// |
1167 | 1163 |
/// This function is called when an arc is examined but its target node is |
1168 | 1164 |
/// already discovered. |
1169 | 1165 |
void examine(const Arc& arc) {} |
1170 | 1166 |
/// \brief Called when the DFS steps back from a node. |
1171 | 1167 |
/// |
1172 | 1168 |
/// This function is called when the DFS steps back from a node. |
1173 | 1169 |
void leave(const Node& node) {} |
1174 | 1170 |
/// \brief Called when the DFS steps back on an arc. |
1175 | 1171 |
/// |
1176 | 1172 |
/// This function is called when the DFS steps back on an arc. |
1177 | 1173 |
void backtrack(const Arc& arc) {} |
1178 | 1174 |
}; |
1179 | 1175 |
#else |
1180 | 1176 |
template <typename _Digraph> |
1181 | 1177 |
struct DfsVisitor { |
1182 | 1178 |
typedef _Digraph Digraph; |
1183 | 1179 |
typedef typename Digraph::Arc Arc; |
1184 | 1180 |
typedef typename Digraph::Node Node; |
1185 | 1181 |
void start(const Node&) {} |
1186 | 1182 |
void stop(const Node&) {} |
1187 | 1183 |
void reach(const Node&) {} |
1188 | 1184 |
void discover(const Arc&) {} |
1189 | 1185 |
void examine(const Arc&) {} |
1190 | 1186 |
void leave(const Node&) {} |
1191 | 1187 |
void backtrack(const Arc&) {} |
1192 | 1188 |
|
1193 | 1189 |
template <typename _Visitor> |
1194 | 1190 |
struct Constraints { |
1195 | 1191 |
void constraints() { |
1196 | 1192 |
Arc arc; |
1197 | 1193 |
Node node; |
1198 | 1194 |
visitor.start(node); |
1199 | 1195 |
visitor.stop(arc); |
1200 | 1196 |
visitor.reach(node); |
1201 | 1197 |
visitor.discover(arc); |
1202 | 1198 |
visitor.examine(arc); |
1203 | 1199 |
visitor.leave(node); |
1204 | 1200 |
visitor.backtrack(arc); |
1205 | 1201 |
} |
1206 | 1202 |
_Visitor& visitor; |
1207 | 1203 |
}; |
1208 | 1204 |
}; |
1209 | 1205 |
#endif |
1210 | 1206 |
|
1211 | 1207 |
/// \brief Default traits class of DfsVisit class. |
1212 | 1208 |
/// |
1213 | 1209 |
/// Default traits class of DfsVisit class. |
1214 | 1210 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1215 | 1211 |
template<class _Digraph> |
1216 | 1212 |
struct DfsVisitDefaultTraits { |
1217 | 1213 |
|
1218 | 1214 |
/// \brief The type of the digraph the algorithm runs on. |
1219 | 1215 |
typedef _Digraph Digraph; |
1220 | 1216 |
|
1221 | 1217 |
/// \brief The type of the map that indicates which nodes are reached. |
1222 | 1218 |
/// |
1223 | 1219 |
/// The type of the map that indicates which nodes are reached. |
1224 | 1220 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
1225 | 1221 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1226 | 1222 |
|
1227 | 1223 |
/// \brief Instantiates a \ref ReachedMap. |
1228 | 1224 |
/// |
1229 | 1225 |
/// This function instantiates a \ref ReachedMap. |
1230 | 1226 |
/// \param digraph is the digraph, to which |
1231 | 1227 |
/// we would like to define the \ref ReachedMap. |
1232 | 1228 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1233 | 1229 |
return new ReachedMap(digraph); |
1234 | 1230 |
} |
1235 | 1231 |
|
1236 | 1232 |
}; |
1237 | 1233 |
|
1238 | 1234 |
/// \ingroup search |
1239 | 1235 |
/// |
1240 | 1236 |
/// \brief %DFS algorithm class with visitor interface. |
1241 | 1237 |
/// |
1242 | 1238 |
/// This class provides an efficient implementation of the %DFS algorithm |
1243 | 1239 |
/// with visitor interface. |
1244 | 1240 |
/// |
1245 | 1241 |
/// The %DfsVisit class provides an alternative interface to the Dfs |
1246 | 1242 |
/// class. It works with callback mechanism, the DfsVisit object calls |
1247 | 1243 |
/// the member functions of the \c Visitor class on every DFS event. |
1248 | 1244 |
/// |
1249 | 1245 |
/// This interface of the DFS algorithm should be used in special cases |
1250 | 1246 |
/// when extra actions have to be performed in connection with certain |
1251 | 1247 |
/// events of the DFS algorithm. Otherwise consider to use Dfs or dfs() |
1252 | 1248 |
/// instead. |
1253 | 1249 |
/// |
1254 | 1250 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1255 | 1251 |
/// The default value is |
1256 | 1252 |
/// \ref ListDigraph. The value of _Digraph is not used directly by |
1257 | 1253 |
/// \ref DfsVisit, it is only passed to \ref DfsVisitDefaultTraits. |
1258 | 1254 |
/// \tparam _Visitor The Visitor type that is used by the algorithm. |
1259 | 1255 |
/// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty visitor, which |
1260 | 1256 |
/// does not observe the DFS events. If you want to observe the DFS |
1261 | 1257 |
/// events, you should implement your own visitor class. |
1262 | 1258 |
/// \tparam _Traits Traits class to set various data types used by the |
1263 | 1259 |
/// algorithm. The default traits class is |
1264 | 1260 |
/// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Digraph>". |
1265 | 1261 |
/// See \ref DfsVisitDefaultTraits for the documentation of |
1266 | 1262 |
/// a DFS visit traits class. |
1267 | 1263 |
#ifdef DOXYGEN |
1268 | 1264 |
template <typename _Digraph, typename _Visitor, typename _Traits> |
1269 | 1265 |
#else |
1270 | 1266 |
template <typename _Digraph = ListDigraph, |
1271 | 1267 |
typename _Visitor = DfsVisitor<_Digraph>, |
1272 | 1268 |
typename _Traits = DfsDefaultTraits<_Digraph> > |
1273 | 1269 |
#endif |
1274 | 1270 |
class DfsVisit { |
1275 | 1271 |
public: |
1276 | 1272 |
|
1277 | 1273 |
/// \brief \ref Exception for uninitialized parameters. |
1278 | 1274 |
/// |
1279 | 1275 |
/// This error represents problems in the initialization |
1280 | 1276 |
/// of the parameters of the algorithm. |
1281 | 1277 |
class UninitializedParameter : public lemon::UninitializedParameter { |
1282 | 1278 |
public: |
1283 | 1279 |
virtual const char* what() const throw() |
1284 | 1280 |
{ |
1285 | 1281 |
return "lemon::DfsVisit::UninitializedParameter"; |
1286 | 1282 |
} |
1287 | 1283 |
}; |
1288 | 1284 |
|
1289 | 1285 |
///The traits class. |
1290 | 1286 |
typedef _Traits Traits; |
1291 | 1287 |
|
1292 | 1288 |
///The type of the digraph the algorithm runs on. |
1293 | 1289 |
typedef typename Traits::Digraph Digraph; |
1294 | 1290 |
|
1295 | 1291 |
///The visitor type used by the algorithm. |
1296 | 1292 |
typedef _Visitor Visitor; |
1297 | 1293 |
|
1298 | 1294 |
///The type of the map that indicates which nodes are reached. |
1299 | 1295 |
typedef typename Traits::ReachedMap ReachedMap; |
1300 | 1296 |
|
1301 | 1297 |
private: |
1302 | 1298 |
|
1303 | 1299 |
typedef typename Digraph::Node Node; |
1304 | 1300 |
typedef typename Digraph::NodeIt NodeIt; |
1305 | 1301 |
typedef typename Digraph::Arc Arc; |
1306 | 1302 |
typedef typename Digraph::OutArcIt OutArcIt; |
1307 | 1303 |
|
1308 | 1304 |
//Pointer to the underlying digraph. |
1309 | 1305 |
const Digraph *_digraph; |
1310 | 1306 |
//Pointer to the visitor object. |
1311 | 1307 |
Visitor *_visitor; |
1312 | 1308 |
//Pointer to the map of reached status of the nodes. |
1313 | 1309 |
ReachedMap *_reached; |
1314 | 1310 |
//Indicates if _reached is locally allocated (true) or not. |
1315 | 1311 |
bool local_reached; |
1316 | 1312 |
|
1317 | 1313 |
std::vector<typename Digraph::Arc> _stack; |
1318 | 1314 |
int _stack_head; |
1319 | 1315 |
|
1320 |
///Creates the maps if necessary. |
|
1321 |
///\todo Better memory allocation (instead of new). |
|
1316 |
//Creates the maps if necessary. |
|
1322 | 1317 |
void create_maps() { |
1323 | 1318 |
if(!_reached) { |
1324 | 1319 |
local_reached = true; |
1325 | 1320 |
_reached = Traits::createReachedMap(*_digraph); |
1326 | 1321 |
} |
1327 | 1322 |
} |
1328 | 1323 |
|
1329 | 1324 |
protected: |
1330 | 1325 |
|
1331 | 1326 |
DfsVisit() {} |
1332 | 1327 |
|
1333 | 1328 |
public: |
1334 | 1329 |
|
1335 | 1330 |
typedef DfsVisit Create; |
1336 | 1331 |
|
1337 | 1332 |
/// \name Named template parameters |
1338 | 1333 |
|
1339 | 1334 |
///@{ |
1340 | 1335 |
template <class T> |
1341 | 1336 |
struct SetReachedMapTraits : public Traits { |
1342 | 1337 |
typedef T ReachedMap; |
1343 | 1338 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1344 | 1339 |
throw UninitializedParameter(); |
1345 | 1340 |
} |
1346 | 1341 |
}; |
1347 | 1342 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1348 | 1343 |
/// ReachedMap type. |
1349 | 1344 |
/// |
1350 | 1345 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
1351 | 1346 |
template <class T> |
1352 | 1347 |
struct SetReachedMap : public DfsVisit< Digraph, Visitor, |
1353 | 1348 |
SetReachedMapTraits<T> > { |
1354 | 1349 |
typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
1355 | 1350 |
}; |
1356 | 1351 |
///@} |
1357 | 1352 |
|
1358 | 1353 |
public: |
1359 | 1354 |
|
1360 | 1355 |
/// \brief Constructor. |
1361 | 1356 |
/// |
1362 | 1357 |
/// Constructor. |
1363 | 1358 |
/// |
1364 | 1359 |
/// \param digraph The digraph the algorithm runs on. |
1365 | 1360 |
/// \param visitor The visitor object of the algorithm. |
1366 | 1361 |
DfsVisit(const Digraph& digraph, Visitor& visitor) |
1367 | 1362 |
: _digraph(&digraph), _visitor(&visitor), |
1368 | 1363 |
_reached(0), local_reached(false) {} |
1369 | 1364 |
|
1370 | 1365 |
/// \brief Destructor. |
1371 | 1366 |
~DfsVisit() { |
1372 | 1367 |
if(local_reached) delete _reached; |
1373 | 1368 |
} |
1374 | 1369 |
|
1375 | 1370 |
/// \brief Sets the map that indicates which nodes are reached. |
1376 | 1371 |
/// |
1377 | 1372 |
/// Sets the map that indicates which nodes are reached. |
1378 | 1373 |
/// If you don't use this function before calling \ref run(), |
1379 | 1374 |
/// it will allocate one. The destructor deallocates this |
1380 | 1375 |
/// automatically allocated map, of course. |
1381 | 1376 |
/// \return <tt> (*this) </tt> |
1382 | 1377 |
DfsVisit &reachedMap(ReachedMap &m) { |
1383 | 1378 |
if(local_reached) { |
1384 | 1379 |
delete _reached; |
1385 | 1380 |
local_reached=false; |
1386 | 1381 |
} |
1387 | 1382 |
_reached = &m; |
1388 | 1383 |
return *this; |
1389 | 1384 |
} |
1390 | 1385 |
|
1391 | 1386 |
public: |
1392 | 1387 |
|
1393 | 1388 |
/// \name Execution control |
1394 | 1389 |
/// The simplest way to execute the algorithm is to use |
1395 | 1390 |
/// one of the member functions called \ref lemon::DfsVisit::run() |
1396 | 1391 |
/// "run()". |
1397 | 1392 |
/// \n |
1398 | 1393 |
/// If you need more control on the execution, first you must call |
1399 | 1394 |
/// \ref lemon::DfsVisit::init() "init()", then you can add several |
1400 | 1395 |
/// source nodes with \ref lemon::DfsVisit::addSource() "addSource()". |
1401 | 1396 |
/// Finally \ref lemon::DfsVisit::start() "start()" will perform the |
1402 | 1397 |
/// actual path computation. |
1403 | 1398 |
|
1404 | 1399 |
/// @{ |
1405 | 1400 |
|
1406 | 1401 |
/// \brief Initializes the internal data structures. |
1407 | 1402 |
/// |
1408 | 1403 |
/// Initializes the internal data structures. |
1409 | 1404 |
void init() { |
1410 | 1405 |
create_maps(); |
1411 | 1406 |
_stack.resize(countNodes(*_digraph)); |
1412 | 1407 |
_stack_head = -1; |
1413 | 1408 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1414 | 1409 |
_reached->set(u, false); |
1415 | 1410 |
} |
1416 | 1411 |
} |
1417 | 1412 |
|
1418 | 1413 |
///Adds a new source node. |
1419 | 1414 |
|
1420 | 1415 |
///Adds a new source node to the set of nodes to be processed. |
1421 | 1416 |
/// |
1422 | 1417 |
///\pre The stack must be empty. (Otherwise the algorithm gives |
1423 | 1418 |
///false results.) |
1424 | 1419 |
/// |
1425 | 1420 |
///\warning Distances will be wrong (or at least strange) in case of |
1426 | 1421 |
///multiple sources. |
1427 | 1422 |
void addSource(Node s) |
1428 | 1423 |
{ |
1429 | 1424 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
1430 | 1425 |
if(!(*_reached)[s]) { |
1431 | 1426 |
_reached->set(s,true); |
1432 | 1427 |
_visitor->start(s); |
1433 | 1428 |
_visitor->reach(s); |
1434 | 1429 |
Arc e; |
1435 | 1430 |
_digraph->firstOut(e, s); |
1436 | 1431 |
if (e != INVALID) { |
1437 | 1432 |
_stack[++_stack_head] = e; |
1438 | 1433 |
} else { |
1439 | 1434 |
_visitor->leave(s); |
1440 | 1435 |
} |
1441 | 1436 |
} |
1442 | 1437 |
} |
1443 | 1438 |
|
1444 | 1439 |
/// \brief Processes the next arc. |
1445 | 1440 |
/// |
1446 | 1441 |
/// Processes the next arc. |
1447 | 1442 |
/// |
1448 | 1443 |
/// \return The processed arc. |
1449 | 1444 |
/// |
1450 | 1445 |
/// \pre The stack must not be empty. |
1451 | 1446 |
Arc processNextArc() { |
1452 | 1447 |
Arc e = _stack[_stack_head]; |
1453 | 1448 |
Node m = _digraph->target(e); |
1454 | 1449 |
if(!(*_reached)[m]) { |
1455 | 1450 |
_visitor->discover(e); |
1456 | 1451 |
_visitor->reach(m); |
1457 | 1452 |
_reached->set(m, true); |
1458 | 1453 |
_digraph->firstOut(_stack[++_stack_head], m); |
1459 | 1454 |
} else { |
1460 | 1455 |
_visitor->examine(e); |
1461 | 1456 |
m = _digraph->source(e); |
1462 | 1457 |
_digraph->nextOut(_stack[_stack_head]); |
1463 | 1458 |
} |
1464 | 1459 |
while (_stack_head>=0 && _stack[_stack_head] == INVALID) { |
1465 | 1460 |
_visitor->leave(m); |
1466 | 1461 |
--_stack_head; |
1467 | 1462 |
if (_stack_head >= 0) { |
1468 | 1463 |
_visitor->backtrack(_stack[_stack_head]); |
1469 | 1464 |
m = _digraph->source(_stack[_stack_head]); |
1470 | 1465 |
_digraph->nextOut(_stack[_stack_head]); |
1471 | 1466 |
} else { |
1472 | 1467 |
_visitor->stop(m); |
1473 | 1468 |
} |
1474 | 1469 |
} |
1475 | 1470 |
return e; |
1476 | 1471 |
} |
1477 | 1472 |
|
1478 | 1473 |
/// \brief Next arc to be processed. |
1479 | 1474 |
/// |
1480 | 1475 |
/// Next arc to be processed. |
1481 | 1476 |
/// |
1482 | 1477 |
/// \return The next arc to be processed or INVALID if the stack is |
1483 | 1478 |
/// empty. |
1484 | 1479 |
Arc nextArc() const { |
1485 | 1480 |
return _stack_head >= 0 ? _stack[_stack_head] : INVALID; |
1486 | 1481 |
} |
1487 | 1482 |
|
1488 | 1483 |
/// \brief Returns \c false if there are nodes |
1489 | 1484 |
/// to be processed. |
1490 | 1485 |
/// |
1491 | 1486 |
/// Returns \c false if there are nodes |
1492 | 1487 |
/// to be processed in the queue (stack). |
1493 | 1488 |
bool emptyQueue() const { return _stack_head < 0; } |
1494 | 1489 |
|
1495 | 1490 |
/// \brief Returns the number of the nodes to be processed. |
1496 | 1491 |
/// |
1497 | 1492 |
/// Returns the number of the nodes to be processed in the queue (stack). |
1498 | 1493 |
int queueSize() const { return _stack_head + 1; } |
1499 | 1494 |
|
1500 | 1495 |
/// \brief Executes the algorithm. |
1501 | 1496 |
/// |
1502 | 1497 |
/// Executes the algorithm. |
1503 | 1498 |
/// |
1504 | 1499 |
/// This method runs the %DFS algorithm from the root node |
1505 | 1500 |
/// in order to compute the %DFS path to each node. |
1506 | 1501 |
/// |
1507 | 1502 |
/// The algorithm computes |
1508 | 1503 |
/// - the %DFS tree, |
1509 | 1504 |
/// - the distance of each node from the root in the %DFS tree. |
1510 | 1505 |
/// |
1511 | 1506 |
/// \pre init() must be called and a root node should be |
1512 | 1507 |
/// added with addSource() before using this function. |
1513 | 1508 |
/// |
1514 | 1509 |
/// \note <tt>d.start()</tt> is just a shortcut of the following code. |
1515 | 1510 |
/// \code |
1516 | 1511 |
/// while ( !d.emptyQueue() ) { |
1517 | 1512 |
/// d.processNextArc(); |
1518 | 1513 |
/// } |
1519 | 1514 |
/// \endcode |
1520 | 1515 |
void start() { |
1521 | 1516 |
while ( !emptyQueue() ) processNextArc(); |
1522 | 1517 |
} |
1523 | 1518 |
|
1524 | 1519 |
/// \brief Executes the algorithm until the given target node is reached. |
1525 | 1520 |
/// |
1526 | 1521 |
/// Executes the algorithm until the given target node is reached. |
1527 | 1522 |
/// |
1528 | 1523 |
/// This method runs the %DFS algorithm from the root node |
1529 | 1524 |
/// in order to compute the DFS path to \c t. |
1530 | 1525 |
/// |
1531 | 1526 |
/// The algorithm computes |
1532 | 1527 |
/// - the %DFS path to \c t, |
1533 | 1528 |
/// - the distance of \c t from the root in the %DFS tree. |
1534 | 1529 |
/// |
1535 | 1530 |
/// \pre init() must be called and a root node should be added |
1536 | 1531 |
/// with addSource() before using this function. |
1537 | 1532 |
void start(Node t) { |
1538 | 1533 |
while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != t ) |
1539 | 1534 |
processNextArc(); |
1540 | 1535 |
} |
1541 | 1536 |
|
1542 | 1537 |
/// \brief Executes the algorithm until a condition is met. |
1543 | 1538 |
/// |
1544 | 1539 |
/// Executes the algorithm until a condition is met. |
1545 | 1540 |
/// |
1546 | 1541 |
/// This method runs the %DFS algorithm from the root node |
1547 | 1542 |
/// until an arc \c a with <tt>am[a]</tt> true is found. |
1548 | 1543 |
/// |
1549 | 1544 |
/// \param am A \c bool (or convertible) arc map. The algorithm |
1550 | 1545 |
/// will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
1551 | 1546 |
/// |
1552 | 1547 |
/// \return The reached arc \c a with <tt>am[a]</tt> true or |
1553 | 1548 |
/// \c INVALID if no such arc was found. |
1554 | 1549 |
/// |
1555 | 1550 |
/// \pre init() must be called and a root node should be added |
1556 | 1551 |
/// with addSource() before using this function. |
1557 | 1552 |
/// |
1558 | 1553 |
/// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
1559 | 1554 |
/// not a node map. |
1560 | 1555 |
template <typename AM> |
1561 | 1556 |
Arc start(const AM &am) { |
1562 | 1557 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
1563 | 1558 |
processNextArc(); |
1564 | 1559 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
1565 | 1560 |
} |
1566 | 1561 |
|
1567 | 1562 |
/// \brief Runs the algorithm from the given source node. |
1568 | 1563 |
/// |
1569 | 1564 |
/// This method runs the %DFS algorithm from node \c s. |
1570 | 1565 |
/// in order to compute the DFS path to each node. |
1571 | 1566 |
/// |
1572 | 1567 |
/// The algorithm computes |
1573 | 1568 |
/// - the %DFS tree, |
1574 | 1569 |
/// - the distance of each node from the root in the %DFS tree. |
1575 | 1570 |
/// |
1576 | 1571 |
/// \note <tt>d.run(s)</tt> is just a shortcut of the following code. |
1577 | 1572 |
///\code |
1578 | 1573 |
/// d.init(); |
1579 | 1574 |
/// d.addSource(s); |
1580 | 1575 |
/// d.start(); |
1581 | 1576 |
///\endcode |
1582 | 1577 |
void run(Node s) { |
1583 | 1578 |
init(); |
1584 | 1579 |
addSource(s); |
1585 | 1580 |
start(); |
1586 | 1581 |
} |
1587 | 1582 |
|
1588 | 1583 |
/// \brief Finds the %DFS path between \c s and \c t. |
1589 | 1584 |
|
1590 | 1585 |
/// This method runs the %DFS algorithm from node \c s |
1591 | 1586 |
/// in order to compute the DFS path to node \c t |
1592 | 1587 |
/// (it stops searching when \c t is processed). |
1593 | 1588 |
/// |
1594 | 1589 |
/// \return \c true if \c t is reachable form \c s. |
1595 | 1590 |
/// |
1596 | 1591 |
/// \note Apart from the return value, <tt>d.run(s,t)</tt> is |
1597 | 1592 |
/// just a shortcut of the following code. |
1598 | 1593 |
///\code |
1599 | 1594 |
/// d.init(); |
1600 | 1595 |
/// d.addSource(s); |
1601 | 1596 |
/// d.start(t); |
1602 | 1597 |
///\endcode |
1603 | 1598 |
bool run(Node s,Node t) { |
1604 | 1599 |
init(); |
1605 | 1600 |
addSource(s); |
1606 | 1601 |
start(t); |
1607 | 1602 |
return reached(t); |
1608 | 1603 |
} |
1609 | 1604 |
|
1610 | 1605 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
1611 | 1606 |
|
1612 | 1607 |
/// This method runs the %DFS algorithm in order to |
1613 | 1608 |
/// compute the %DFS path to each node. |
1614 | 1609 |
/// |
1615 | 1610 |
/// The algorithm computes |
1616 | 1611 |
/// - the %DFS tree, |
1617 | 1612 |
/// - the distance of each node from the root in the %DFS tree. |
1618 | 1613 |
/// |
1619 | 1614 |
/// \note <tt>d.run()</tt> is just a shortcut of the following code. |
1620 | 1615 |
///\code |
1621 | 1616 |
/// d.init(); |
1622 | 1617 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
1623 | 1618 |
/// if (!d.reached(n)) { |
1624 | 1619 |
/// d.addSource(n); |
1625 | 1620 |
/// d.start(); |
1626 | 1621 |
/// } |
1627 | 1622 |
/// } |
1628 | 1623 |
///\endcode |
1629 | 1624 |
void run() { |
1630 | 1625 |
init(); |
1631 | 1626 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1632 | 1627 |
if (!reached(it)) { |
1633 | 1628 |
addSource(it); |
1634 | 1629 |
start(); |
1635 | 1630 |
} |
1636 | 1631 |
} |
1637 | 1632 |
} |
1638 | 1633 |
|
1639 | 1634 |
///@} |
1640 | 1635 |
|
1641 | 1636 |
/// \name Query Functions |
1642 | 1637 |
/// The result of the %DFS algorithm can be obtained using these |
1643 | 1638 |
/// functions.\n |
1644 | 1639 |
/// Either \ref lemon::DfsVisit::run() "run()" or |
1645 | 1640 |
/// \ref lemon::DfsVisit::start() "start()" must be called before |
1646 | 1641 |
/// using them. |
1647 | 1642 |
///@{ |
1648 | 1643 |
|
1649 | 1644 |
/// \brief Checks if a node is reachable from the root(s). |
1650 | 1645 |
/// |
1651 | 1646 |
/// Returns \c true if \c v is reachable from the root(s). |
1652 | 1647 |
/// \pre Either \ref run() or \ref start() |
1653 | 1648 |
/// must be called before using this function. |
1654 | 1649 |
bool reached(Node v) { return (*_reached)[v]; } |
1655 | 1650 |
|
1656 | 1651 |
///@} |
1657 | 1652 |
|
1658 | 1653 |
}; |
1659 | 1654 |
|
1660 | 1655 |
} //END OF NAMESPACE LEMON |
1661 | 1656 |
|
1662 | 1657 |
#endif |
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)