gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Bug fix in Dfs::start(s,t) (#392)
0 2 0
default
2 files changed with 14 insertions and 3 deletions:
↑ Collapse diff ↑
Ignore white space 524288 line context
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/maps.h>
31 31
#include <lemon/path.h>
32 32

	
33 33
namespace lemon {
34 34

	
35 35
  ///Default traits class of Dfs class.
36 36

	
37 37
  ///Default traits class of Dfs class.
38 38
  ///\tparam GR Digraph type.
39 39
  template<class GR>
40 40
  struct DfsDefaultTraits
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 %DFS paths.
47 47
    ///
48 48
    ///The type of the map that stores the predecessor
49 49
    ///arcs of the %DFS 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 PredMap.
53 53

	
54 54
    ///This function instantiates a PredMap.
55 55
    ///\param g is the digraph, to which we would like to define the
56 56
    ///PredMap.
57 57
    static PredMap *createPredMap(const Digraph &g)
58 58
    {
59 59
      return new PredMap(g);
60 60
    }
61 61

	
62 62
    ///The type of the map that indicates which nodes are processed.
63 63

	
64 64
    ///The type of the map that indicates which nodes are processed.
65 65
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
66 66
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
67 67
    ///Instantiates a ProcessedMap.
68 68

	
69 69
    ///This function instantiates a ProcessedMap.
70 70
    ///\param g is the digraph, to which
71 71
    ///we would like to define the ProcessedMap
72 72
#ifdef DOXYGEN
73 73
    static ProcessedMap *createProcessedMap(const Digraph &g)
74 74
#else
75 75
    static ProcessedMap *createProcessedMap(const Digraph &)
76 76
#endif
77 77
    {
78 78
      return new ProcessedMap();
79 79
    }
80 80

	
81 81
    ///The type of the map that indicates which nodes are reached.
82 82

	
83 83
    ///The type of the map that indicates which nodes are reached.
84 84
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
85 85
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
86 86
    ///Instantiates a ReachedMap.
87 87

	
88 88
    ///This function instantiates a ReachedMap.
89 89
    ///\param g is the digraph, to which
90 90
    ///we would like to define the ReachedMap.
91 91
    static ReachedMap *createReachedMap(const Digraph &g)
92 92
    {
93 93
      return new ReachedMap(g);
94 94
    }
95 95

	
96 96
    ///The type of the map that stores the distances of the nodes.
97 97

	
98 98
    ///The type of the map that stores the distances of the nodes.
99 99
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
100 100
    typedef typename Digraph::template NodeMap<int> DistMap;
101 101
    ///Instantiates a DistMap.
102 102

	
103 103
    ///This function instantiates a DistMap.
104 104
    ///\param g is the digraph, to which we would like to define the
105 105
    ///DistMap.
106 106
    static DistMap *createDistMap(const Digraph &g)
107 107
    {
108 108
      return new DistMap(g);
109 109
    }
110 110
  };
111 111

	
112 112
  ///%DFS algorithm class.
113 113

	
114 114
  ///\ingroup search
115 115
  ///This class provides an efficient implementation of the %DFS algorithm.
116 116
  ///
117 117
  ///There is also a \ref dfs() "function-type interface" for the DFS
118 118
  ///algorithm, which is convenient in the simplier cases and it can be
119 119
  ///used easier.
120 120
  ///
121 121
  ///\tparam GR The type of the digraph the algorithm runs on.
122 122
  ///The default value is \ref ListDigraph. The value of GR is not used
123 123
  ///directly by \ref Dfs, it is only passed to \ref DfsDefaultTraits.
124 124
  ///\tparam TR Traits class to set various data types used by the algorithm.
125 125
  ///The default traits class is
126 126
  ///\ref DfsDefaultTraits "DfsDefaultTraits<GR>".
127 127
  ///See \ref DfsDefaultTraits for the documentation of
128 128
  ///a Dfs traits class.
129 129
#ifdef DOXYGEN
130 130
  template <typename GR,
131 131
            typename TR>
132 132
#else
133 133
  template <typename GR=ListDigraph,
134 134
            typename TR=DfsDefaultTraits<GR> >
135 135
#endif
136 136
  class Dfs {
137 137
  public:
138 138

	
139 139
    ///The type of the digraph the algorithm runs on.
140 140
    typedef typename TR::Digraph Digraph;
141 141

	
142 142
    ///\brief The type of the map that stores the predecessor arcs of the
143 143
    ///DFS paths.
144 144
    typedef typename TR::PredMap PredMap;
145 145
    ///The type of the map that stores the distances of the nodes.
146 146
    typedef typename TR::DistMap DistMap;
147 147
    ///The type of the map that indicates which nodes are reached.
148 148
    typedef typename TR::ReachedMap ReachedMap;
149 149
    ///The type of the map that indicates which nodes are processed.
150 150
    typedef typename TR::ProcessedMap ProcessedMap;
151 151
    ///The type of the paths.
152 152
    typedef PredMapPath<Digraph, PredMap> Path;
153 153

	
154 154
    ///The traits class.
155 155
    typedef TR Traits;
156 156

	
157 157
  private:
158 158

	
159 159
    typedef typename Digraph::Node Node;
160 160
    typedef typename Digraph::NodeIt NodeIt;
161 161
    typedef typename Digraph::Arc Arc;
162 162
    typedef typename Digraph::OutArcIt OutArcIt;
163 163

	
164 164
    //Pointer to the underlying digraph.
165 165
    const Digraph *G;
166 166
    //Pointer to the map of predecessor arcs.
167 167
    PredMap *_pred;
168 168
    //Indicates if _pred is locally allocated (true) or not.
169 169
    bool local_pred;
170 170
    //Pointer to the map of distances.
171 171
    DistMap *_dist;
172 172
    //Indicates if _dist is locally allocated (true) or not.
173 173
    bool local_dist;
174 174
    //Pointer to the map of reached status of the nodes.
175 175
    ReachedMap *_reached;
176 176
    //Indicates if _reached is locally allocated (true) or not.
177 177
    bool local_reached;
178 178
    //Pointer to the map of processed status of the nodes.
179 179
    ProcessedMap *_processed;
180 180
    //Indicates if _processed is locally allocated (true) or not.
181 181
    bool local_processed;
182 182

	
183 183
    std::vector<typename Digraph::OutArcIt> _stack;
184 184
    int _stack_head;
185 185

	
186 186
    //Creates the maps if necessary.
187 187
    void create_maps()
188 188
    {
189 189
      if(!_pred) {
190 190
        local_pred = true;
191 191
        _pred = Traits::createPredMap(*G);
192 192
      }
193 193
      if(!_dist) {
194 194
        local_dist = true;
195 195
        _dist = Traits::createDistMap(*G);
196 196
      }
197 197
      if(!_reached) {
198 198
        local_reached = true;
199 199
        _reached = Traits::createReachedMap(*G);
200 200
      }
201 201
      if(!_processed) {
202 202
        local_processed = true;
203 203
        _processed = Traits::createProcessedMap(*G);
204 204
      }
205 205
    }
206 206

	
207 207
  protected:
208 208

	
209 209
    Dfs() {}
210 210

	
211 211
  public:
212 212

	
213 213
    typedef Dfs Create;
214 214

	
215 215
    ///\name Named template parameters
216 216

	
217 217
    ///@{
218 218

	
219 219
    template <class T>
220 220
    struct SetPredMapTraits : public Traits {
221 221
      typedef T PredMap;
222 222
      static PredMap *createPredMap(const Digraph &)
223 223
      {
224 224
        LEMON_ASSERT(false, "PredMap is not initialized");
225 225
        return 0; // ignore warnings
226 226
      }
227 227
    };
228 228
    ///\brief \ref named-templ-param "Named parameter" for setting
229 229
    ///PredMap type.
230 230
    ///
231 231
    ///\ref named-templ-param "Named parameter" for setting
232 232
    ///PredMap type.
233 233
    template <class T>
234 234
    struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > {
235 235
      typedef Dfs<Digraph, SetPredMapTraits<T> > Create;
236 236
    };
237 237

	
238 238
    template <class T>
239 239
    struct SetDistMapTraits : public Traits {
240 240
      typedef T DistMap;
241 241
      static DistMap *createDistMap(const Digraph &)
242 242
      {
243 243
        LEMON_ASSERT(false, "DistMap is not initialized");
244 244
        return 0; // ignore warnings
245 245
      }
246 246
    };
247 247
    ///\brief \ref named-templ-param "Named parameter" for setting
248 248
    ///DistMap type.
249 249
    ///
250 250
    ///\ref named-templ-param "Named parameter" for setting
251 251
    ///DistMap type.
252 252
    template <class T>
253 253
    struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > {
254 254
      typedef Dfs<Digraph, SetDistMapTraits<T> > Create;
255 255
    };
256 256

	
257 257
    template <class T>
258 258
    struct SetReachedMapTraits : public Traits {
259 259
      typedef T ReachedMap;
260 260
      static ReachedMap *createReachedMap(const Digraph &)
261 261
      {
262 262
        LEMON_ASSERT(false, "ReachedMap is not initialized");
263 263
        return 0; // ignore warnings
264 264
      }
265 265
    };
266 266
    ///\brief \ref named-templ-param "Named parameter" for setting
267 267
    ///ReachedMap type.
268 268
    ///
269 269
    ///\ref named-templ-param "Named parameter" for setting
270 270
    ///ReachedMap type.
271 271
    template <class T>
272 272
    struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
273 273
      typedef Dfs< Digraph, SetReachedMapTraits<T> > Create;
274 274
    };
275 275

	
276 276
    template <class T>
277 277
    struct SetProcessedMapTraits : public Traits {
278 278
      typedef T ProcessedMap;
279 279
      static ProcessedMap *createProcessedMap(const Digraph &)
280 280
      {
281 281
        LEMON_ASSERT(false, "ProcessedMap is not initialized");
282 282
        return 0; // ignore warnings
283 283
      }
284 284
    };
285 285
    ///\brief \ref named-templ-param "Named parameter" for setting
286 286
    ///ProcessedMap type.
287 287
    ///
288 288
    ///\ref named-templ-param "Named parameter" for setting
289 289
    ///ProcessedMap type.
290 290
    template <class T>
291 291
    struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
292 292
      typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create;
293 293
    };
294 294

	
295 295
    struct SetStandardProcessedMapTraits : public Traits {
296 296
      typedef typename Digraph::template NodeMap<bool> ProcessedMap;
297 297
      static ProcessedMap *createProcessedMap(const Digraph &g)
298 298
      {
299 299
        return new ProcessedMap(g);
300 300
      }
301 301
    };
302 302
    ///\brief \ref named-templ-param "Named parameter" for setting
303 303
    ///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
304 304
    ///
305 305
    ///\ref named-templ-param "Named parameter" for setting
306 306
    ///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
307 307
    ///If you don't set it explicitly, it will be automatically allocated.
308 308
    struct SetStandardProcessedMap :
309 309
      public Dfs< Digraph, SetStandardProcessedMapTraits > {
310 310
      typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create;
311 311
    };
312 312

	
313 313
    ///@}
314 314

	
315 315
  public:
316 316

	
317 317
    ///Constructor.
318 318

	
319 319
    ///Constructor.
320 320
    ///\param g The digraph the algorithm runs on.
321 321
    Dfs(const Digraph &g) :
322 322
      G(&g),
323 323
      _pred(NULL), local_pred(false),
324 324
      _dist(NULL), local_dist(false),
325 325
      _reached(NULL), local_reached(false),
326 326
      _processed(NULL), local_processed(false)
327 327
    { }
328 328

	
329 329
    ///Destructor.
330 330
    ~Dfs()
331 331
    {
332 332
      if(local_pred) delete _pred;
333 333
      if(local_dist) delete _dist;
334 334
      if(local_reached) delete _reached;
335 335
      if(local_processed) delete _processed;
336 336
    }
337 337

	
338 338
    ///Sets the map that stores the predecessor arcs.
339 339

	
340 340
    ///Sets the map that stores the predecessor arcs.
341 341
    ///If you don't use this function before calling \ref run(),
342 342
    ///it will allocate one. The destructor deallocates this
343 343
    ///automatically allocated map, of course.
344 344
    ///\return <tt> (*this) </tt>
345 345
    Dfs &predMap(PredMap &m)
346 346
    {
347 347
      if(local_pred) {
348 348
        delete _pred;
349 349
        local_pred=false;
350 350
      }
351 351
      _pred = &m;
352 352
      return *this;
353 353
    }
354 354

	
355 355
    ///Sets the map that indicates which nodes are reached.
356 356

	
357 357
    ///Sets the map that indicates which nodes are reached.
358 358
    ///If you don't use this function before calling \ref run(),
359 359
    ///it will allocate one. The destructor deallocates this
360 360
    ///automatically allocated map, of course.
361 361
    ///\return <tt> (*this) </tt>
362 362
    Dfs &reachedMap(ReachedMap &m)
363 363
    {
364 364
      if(local_reached) {
365 365
        delete _reached;
366 366
        local_reached=false;
367 367
      }
368 368
      _reached = &m;
369 369
      return *this;
370 370
    }
371 371

	
372 372
    ///Sets the map that indicates which nodes are processed.
373 373

	
374 374
    ///Sets the map that indicates which nodes are processed.
375 375
    ///If you don't use this function before calling \ref run(),
376 376
    ///it will allocate one. The destructor deallocates this
377 377
    ///automatically allocated map, of course.
378 378
    ///\return <tt> (*this) </tt>
379 379
    Dfs &processedMap(ProcessedMap &m)
380 380
    {
381 381
      if(local_processed) {
382 382
        delete _processed;
383 383
        local_processed=false;
384 384
      }
385 385
      _processed = &m;
386 386
      return *this;
387 387
    }
388 388

	
389 389
    ///Sets the map that stores the distances of the nodes.
390 390

	
391 391
    ///Sets the map that stores the distances of the nodes calculated by
392 392
    ///the algorithm.
393 393
    ///If you don't use this function before calling \ref run(),
394 394
    ///it will allocate one. The destructor deallocates this
395 395
    ///automatically allocated map, of course.
396 396
    ///\return <tt> (*this) </tt>
397 397
    Dfs &distMap(DistMap &m)
398 398
    {
399 399
      if(local_dist) {
400 400
        delete _dist;
401 401
        local_dist=false;
402 402
      }
403 403
      _dist = &m;
404 404
      return *this;
405 405
    }
406 406

	
407 407
  public:
408 408

	
409 409
    ///\name Execution control
410 410
    ///The simplest way to execute the algorithm is to use
411 411
    ///one of the member functions called \ref lemon::Dfs::run() "run()".
412 412
    ///\n
413 413
    ///If you need more control on the execution, first you must call
414 414
    ///\ref lemon::Dfs::init() "init()", then you can add a source node
415 415
    ///with \ref lemon::Dfs::addSource() "addSource()".
416 416
    ///Finally \ref lemon::Dfs::start() "start()" will perform the
417 417
    ///actual path computation.
418 418

	
419 419
    ///@{
420 420

	
421 421
    ///Initializes the internal data structures.
422 422

	
423 423
    ///Initializes the internal data structures.
424 424
    ///
425 425
    void init()
426 426
    {
427 427
      create_maps();
428 428
      _stack.resize(countNodes(*G));
429 429
      _stack_head=-1;
430 430
      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
431 431
        _pred->set(u,INVALID);
432 432
        _reached->set(u,false);
433 433
        _processed->set(u,false);
434 434
      }
435 435
    }
436 436

	
437 437
    ///Adds a new source node.
438 438

	
439 439
    ///Adds a new source node to the set of nodes to be processed.
440 440
    ///
441 441
    ///\pre The stack must be empty. (Otherwise the algorithm gives
442 442
    ///false results.)
443 443
    ///
444 444
    ///\warning Distances will be wrong (or at least strange) in case of
445 445
    ///multiple sources.
446 446
    void addSource(Node s)
447 447
    {
448 448
      LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
449 449
      if(!(*_reached)[s])
450 450
        {
451 451
          _reached->set(s,true);
452 452
          _pred->set(s,INVALID);
453 453
          OutArcIt e(*G,s);
454 454
          if(e!=INVALID) {
455 455
            _stack[++_stack_head]=e;
456 456
            _dist->set(s,_stack_head);
457 457
          }
458 458
          else {
459 459
            _processed->set(s,true);
460 460
            _dist->set(s,0);
461 461
          }
462 462
        }
463 463
    }
464 464

	
465 465
    ///Processes the next arc.
466 466

	
467 467
    ///Processes the next arc.
468 468
    ///
469 469
    ///\return The processed arc.
470 470
    ///
471 471
    ///\pre The stack must not be empty.
472 472
    Arc processNextArc()
473 473
    {
474 474
      Node m;
475 475
      Arc e=_stack[_stack_head];
476 476
      if(!(*_reached)[m=G->target(e)]) {
477 477
        _pred->set(m,e);
478 478
        _reached->set(m,true);
479 479
        ++_stack_head;
480 480
        _stack[_stack_head] = OutArcIt(*G, m);
481 481
        _dist->set(m,_stack_head);
482 482
      }
483 483
      else {
484 484
        m=G->source(e);
485 485
        ++_stack[_stack_head];
486 486
      }
487 487
      while(_stack_head>=0 && _stack[_stack_head]==INVALID) {
488 488
        _processed->set(m,true);
489 489
        --_stack_head;
490 490
        if(_stack_head>=0) {
491 491
          m=G->source(_stack[_stack_head]);
492 492
          ++_stack[_stack_head];
493 493
        }
494 494
      }
495 495
      return e;
496 496
    }
497 497

	
498 498
    ///Next arc to be processed.
499 499

	
500 500
    ///Next arc to be processed.
501 501
    ///
502 502
    ///\return The next arc to be processed or \c INVALID if the stack
503 503
    ///is empty.
504 504
    OutArcIt nextArc() const
505 505
    {
506 506
      return _stack_head>=0?_stack[_stack_head]:INVALID;
507 507
    }
508 508

	
509 509
    ///\brief Returns \c false if there are nodes
510 510
    ///to be processed.
511 511
    ///
512 512
    ///Returns \c false if there are nodes
513 513
    ///to be processed in the queue (stack).
514 514
    bool emptyQueue() const { return _stack_head<0; }
515 515

	
516 516
    ///Returns the number of the nodes to be processed.
517 517

	
518 518
    ///Returns the number of the nodes to be processed in the queue (stack).
519 519
    int queueSize() const { return _stack_head+1; }
520 520

	
521 521
    ///Executes the algorithm.
522 522

	
523 523
    ///Executes the algorithm.
524 524
    ///
525 525
    ///This method runs the %DFS algorithm from the root node
526 526
    ///in order to compute the DFS path to each node.
527 527
    ///
528 528
    /// The algorithm computes
529 529
    ///- the %DFS tree,
530 530
    ///- the distance of each node from the root in the %DFS tree.
531 531
    ///
532 532
    ///\pre init() must be called and a root node should be
533 533
    ///added with addSource() before using this function.
534 534
    ///
535 535
    ///\note <tt>d.start()</tt> is just a shortcut of the following code.
536 536
    ///\code
537 537
    ///  while ( !d.emptyQueue() ) {
538 538
    ///    d.processNextArc();
539 539
    ///  }
540 540
    ///\endcode
541 541
    void start()
542 542
    {
543 543
      while ( !emptyQueue() ) processNextArc();
544 544
    }
545 545

	
546 546
    ///Executes the algorithm until the given target node is reached.
547 547

	
548 548
    ///Executes the algorithm until the given target node is reached.
549 549
    ///
550 550
    ///This method runs the %DFS algorithm from the root node
551 551
    ///in order to compute the DFS path to \c t.
552 552
    ///
553 553
    ///The algorithm computes
554 554
    ///- the %DFS path to \c t,
555 555
    ///- the distance of \c t from the root in the %DFS tree.
556 556
    ///
557 557
    ///\pre init() must be called and a root node should be
558 558
    ///added with addSource() before using this function.
559 559
    void start(Node t)
560 560
    {
561
      while ( !emptyQueue() && G->target(_stack[_stack_head])!=t )
561
      while ( !emptyQueue() && !(*_reached)[t] )
562 562
        processNextArc();
563 563
    }
564 564

	
565 565
    ///Executes the algorithm until a condition is met.
566 566

	
567 567
    ///Executes the algorithm until a condition is met.
568 568
    ///
569 569
    ///This method runs the %DFS algorithm from the root node
570 570
    ///until an arc \c a with <tt>am[a]</tt> true is found.
571 571
    ///
572 572
    ///\param am A \c bool (or convertible) arc map. The algorithm
573 573
    ///will stop when it reaches an arc \c a with <tt>am[a]</tt> true.
574 574
    ///
575 575
    ///\return The reached arc \c a with <tt>am[a]</tt> true or
576 576
    ///\c INVALID if no such arc was found.
577 577
    ///
578 578
    ///\pre init() must be called and a root node should be
579 579
    ///added with addSource() before using this function.
580 580
    ///
581 581
    ///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map,
582 582
    ///not a node map.
583 583
    template<class ArcBoolMap>
584 584
    Arc start(const ArcBoolMap &am)
585 585
    {
586 586
      while ( !emptyQueue() && !am[_stack[_stack_head]] )
587 587
        processNextArc();
588 588
      return emptyQueue() ? INVALID : _stack[_stack_head];
589 589
    }
590 590

	
591 591
    ///Runs the algorithm from the given source node.
592 592

	
593 593
    ///This method runs the %DFS algorithm from node \c s
594 594
    ///in order to compute the DFS path to each node.
595 595
    ///
596 596
    ///The algorithm computes
597 597
    ///- the %DFS tree,
598 598
    ///- the distance of each node from the root in the %DFS tree.
599 599
    ///
600 600
    ///\note <tt>d.run(s)</tt> is just a shortcut of the following code.
601 601
    ///\code
602 602
    ///  d.init();
603 603
    ///  d.addSource(s);
604 604
    ///  d.start();
605 605
    ///\endcode
606 606
    void run(Node s) {
607 607
      init();
608 608
      addSource(s);
609 609
      start();
610 610
    }
611 611

	
612 612
    ///Finds the %DFS path between \c s and \c t.
613 613

	
614 614
    ///This method runs the %DFS algorithm from node \c s
615 615
    ///in order to compute the DFS path to node \c t
616 616
    ///(it stops searching when \c t is processed)
617 617
    ///
618 618
    ///\return \c true if \c t is reachable form \c s.
619 619
    ///
620 620
    ///\note Apart from the return value, <tt>d.run(s,t)</tt> is
621 621
    ///just a shortcut of the following code.
622 622
    ///\code
623 623
    ///  d.init();
624 624
    ///  d.addSource(s);
625 625
    ///  d.start(t);
626 626
    ///\endcode
627 627
    bool run(Node s,Node t) {
628 628
      init();
629 629
      addSource(s);
630 630
      start(t);
631 631
      return reached(t);
632 632
    }
633 633

	
634 634
    ///Runs the algorithm to visit all nodes in the digraph.
635 635

	
636 636
    ///This method runs the %DFS algorithm in order to compute the
637 637
    ///%DFS path to each node.
638 638
    ///
639 639
    ///The algorithm computes
640 640
    ///- the %DFS tree,
641 641
    ///- the distance of each node from the root in the %DFS tree.
642 642
    ///
643 643
    ///\note <tt>d.run()</tt> is just a shortcut of the following code.
644 644
    ///\code
645 645
    ///  d.init();
646 646
    ///  for (NodeIt n(digraph); n != INVALID; ++n) {
647 647
    ///    if (!d.reached(n)) {
648 648
    ///      d.addSource(n);
649 649
    ///      d.start();
650 650
    ///    }
651 651
    ///  }
652 652
    ///\endcode
653 653
    void run() {
654 654
      init();
655 655
      for (NodeIt it(*G); it != INVALID; ++it) {
656 656
        if (!reached(it)) {
657 657
          addSource(it);
658 658
          start();
659 659
        }
660 660
      }
661 661
    }
662 662

	
663 663
    ///@}
664 664

	
665 665
    ///\name Query Functions
666 666
    ///The result of the %DFS algorithm can be obtained using these
667 667
    ///functions.\n
668 668
    ///Either \ref lemon::Dfs::run() "run()" or \ref lemon::Dfs::start()
669 669
    ///"start()" must be called before using them.
670 670

	
671 671
    ///@{
672 672

	
673 673
    ///The DFS path to a node.
674 674

	
675 675
    ///Returns the DFS path to a node.
676 676
    ///
677 677
    ///\warning \c t should be reachable from the root.
678 678
    ///
679 679
    ///\pre Either \ref run() or \ref start() must be called before
680 680
    ///using this function.
681 681
    Path path(Node t) const { return Path(*G, *_pred, t); }
682 682

	
683 683
    ///The distance of a node from the root.
684 684

	
685 685
    ///Returns the distance of a node from the root.
686 686
    ///
687 687
    ///\warning If node \c v is not reachable from the root, then
688 688
    ///the return value of this function is undefined.
689 689
    ///
690 690
    ///\pre Either \ref run() or \ref start() must be called before
691 691
    ///using this function.
692 692
    int dist(Node v) const { return (*_dist)[v]; }
693 693

	
694 694
    ///Returns the 'previous arc' of the %DFS tree for a node.
695 695

	
696 696
    ///This function returns the 'previous arc' of the %DFS tree for the
697 697
    ///node \c v, i.e. it returns the last arc of a %DFS path from the
698 698
    ///root to \c v. It is \c INVALID
699 699
    ///if \c v is not reachable from the root(s) or if \c v is a root.
700 700
    ///
701 701
    ///The %DFS tree used here is equal to the %DFS tree used in
702 702
    ///\ref predNode().
703 703
    ///
704 704
    ///\pre Either \ref run() or \ref start() must be called before using
705 705
    ///this function.
706 706
    Arc predArc(Node v) const { return (*_pred)[v];}
707 707

	
708 708
    ///Returns the 'previous node' of the %DFS tree.
709 709

	
710 710
    ///This function returns the 'previous node' of the %DFS
711 711
    ///tree for the node \c v, i.e. it returns the last but one node
712 712
    ///from a %DFS path from the root to \c v. It is \c INVALID
713 713
    ///if \c v is not reachable from the root(s) or if \c v is a root.
714 714
    ///
715 715
    ///The %DFS tree used here is equal to the %DFS tree used in
716 716
    ///\ref predArc().
717 717
    ///
718 718
    ///\pre Either \ref run() or \ref start() must be called before
719 719
    ///using this function.
720 720
    Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
721 721
                                  G->source((*_pred)[v]); }
722 722

	
723 723
    ///\brief Returns a const reference to the node map that stores the
724 724
    ///distances of the nodes.
725 725
    ///
726 726
    ///Returns a const reference to the node map that stores the
727 727
    ///distances of the nodes calculated by the algorithm.
728 728
    ///
729 729
    ///\pre Either \ref run() or \ref init()
730 730
    ///must be called before using this function.
731 731
    const DistMap &distMap() const { return *_dist;}
732 732

	
733 733
    ///\brief Returns a const reference to the node map that stores the
734 734
    ///predecessor arcs.
735 735
    ///
736 736
    ///Returns a const reference to the node map that stores the predecessor
737 737
    ///arcs, which form the DFS tree.
738 738
    ///
739 739
    ///\pre Either \ref run() or \ref init()
740 740
    ///must be called before using this function.
741 741
    const PredMap &predMap() const { return *_pred;}
742 742

	
743 743
    ///Checks if a node is reachable from the root(s).
744 744

	
745 745
    ///Returns \c true if \c v is reachable from the root(s).
746 746
    ///\pre Either \ref run() or \ref start()
747 747
    ///must be called before using this function.
748 748
    bool reached(Node v) const { return (*_reached)[v]; }
749 749

	
750 750
    ///@}
751 751
  };
752 752

	
753 753
  ///Default traits class of dfs() function.
754 754

	
755 755
  ///Default traits class of dfs() function.
756 756
  ///\tparam GR Digraph type.
757 757
  template<class GR>
758 758
  struct DfsWizardDefaultTraits
759 759
  {
760 760
    ///The type of the digraph the algorithm runs on.
761 761
    typedef GR Digraph;
762 762

	
763 763
    ///\brief The type of the map that stores the predecessor
764 764
    ///arcs of the %DFS paths.
765 765
    ///
766 766
    ///The type of the map that stores the predecessor
767 767
    ///arcs of the %DFS paths.
768 768
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
769 769
    typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
770 770
    ///Instantiates a PredMap.
771 771

	
772 772
    ///This function instantiates a PredMap.
773 773
    ///\param g is the digraph, to which we would like to define the
774 774
    ///PredMap.
775 775
    static PredMap *createPredMap(const Digraph &g)
776 776
    {
777 777
      return new PredMap(g);
778 778
    }
779 779

	
780 780
    ///The type of the map that indicates which nodes are processed.
781 781

	
782 782
    ///The type of the map that indicates which nodes are processed.
783 783
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
784 784
    ///By default it is a NullMap.
785 785
    typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
786 786
    ///Instantiates a ProcessedMap.
787 787

	
788 788
    ///This function instantiates a ProcessedMap.
789 789
    ///\param g is the digraph, to which
790 790
    ///we would like to define the ProcessedMap.
791 791
#ifdef DOXYGEN
792 792
    static ProcessedMap *createProcessedMap(const Digraph &g)
793 793
#else
794 794
    static ProcessedMap *createProcessedMap(const Digraph &)
795 795
#endif
796 796
    {
797 797
      return new ProcessedMap();
798 798
    }
799 799

	
800 800
    ///The type of the map that indicates which nodes are reached.
801 801

	
802 802
    ///The type of the map that indicates which nodes are reached.
803 803
    ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
804 804
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
805 805
    ///Instantiates a ReachedMap.
806 806

	
807 807
    ///This function instantiates a ReachedMap.
808 808
    ///\param g is the digraph, to which
809 809
    ///we would like to define the ReachedMap.
810 810
    static ReachedMap *createReachedMap(const Digraph &g)
811 811
    {
812 812
      return new ReachedMap(g);
813 813
    }
814 814

	
815 815
    ///The type of the map that stores the distances of the nodes.
816 816

	
817 817
    ///The type of the map that stores the distances of the nodes.
818 818
    ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
819 819
    typedef typename Digraph::template NodeMap<int> DistMap;
820 820
    ///Instantiates a DistMap.
821 821

	
822 822
    ///This function instantiates a DistMap.
823 823
    ///\param g is the digraph, to which we would like to define
824 824
    ///the DistMap
825 825
    static DistMap *createDistMap(const Digraph &g)
826 826
    {
827 827
      return new DistMap(g);
828 828
    }
829 829

	
830 830
    ///The type of the DFS paths.
831 831

	
832 832
    ///The type of the DFS paths.
833 833
    ///It must meet the \ref concepts::Path "Path" concept.
834 834
    typedef lemon::Path<Digraph> Path;
835 835
  };
836 836

	
837 837
  /// Default traits class used by DfsWizard
838 838

	
839 839
  /// To make it easier to use Dfs algorithm
840 840
  /// we have created a wizard class.
841 841
  /// This \ref DfsWizard class needs default traits,
842 842
  /// as well as the \ref Dfs class.
843 843
  /// The \ref DfsWizardBase is a class to be the default traits of the
844 844
  /// \ref DfsWizard class.
845 845
  template<class GR>
846 846
  class DfsWizardBase : public DfsWizardDefaultTraits<GR>
847 847
  {
848 848

	
849 849
    typedef DfsWizardDefaultTraits<GR> Base;
850 850
  protected:
851 851
    //The type of the nodes in the digraph.
852 852
    typedef typename Base::Digraph::Node Node;
853 853

	
854 854
    //Pointer to the digraph the algorithm runs on.
855 855
    void *_g;
856 856
    //Pointer to the map of reached nodes.
857 857
    void *_reached;
858 858
    //Pointer to the map of processed nodes.
859 859
    void *_processed;
860 860
    //Pointer to the map of predecessors arcs.
861 861
    void *_pred;
862 862
    //Pointer to the map of distances.
863 863
    void *_dist;
864 864
    //Pointer to the DFS path to the target node.
865 865
    void *_path;
866 866
    //Pointer to the distance of the target node.
867 867
    int *_di;
868 868

	
869 869
    public:
870 870
    /// Constructor.
871 871

	
872 872
    /// This constructor does not require parameters, therefore it initiates
873 873
    /// all of the attributes to \c 0.
874 874
    DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
875 875
                      _dist(0), _path(0), _di(0) {}
876 876

	
877 877
    /// Constructor.
878 878

	
879 879
    /// This constructor requires one parameter,
880 880
    /// others are initiated to \c 0.
881 881
    /// \param g The digraph the algorithm runs on.
882 882
    DfsWizardBase(const GR &g) :
883 883
      _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
884 884
      _reached(0), _processed(0), _pred(0), _dist(0),  _path(0), _di(0) {}
885 885

	
886 886
  };
887 887

	
888 888
  /// Auxiliary class for the function-type interface of DFS algorithm.
889 889

	
890 890
  /// This auxiliary class is created to implement the
891 891
  /// \ref dfs() "function-type interface" of \ref Dfs algorithm.
892 892
  /// It does not have own \ref run() method, it uses the functions
893 893
  /// and features of the plain \ref Dfs.
894 894
  ///
895 895
  /// This class should only be used through the \ref dfs() function,
896 896
  /// which makes it easier to use the algorithm.
897 897
  template<class TR>
898 898
  class DfsWizard : public TR
899 899
  {
900 900
    typedef TR Base;
901 901

	
902 902
    ///The type of the digraph the algorithm runs on.
903 903
    typedef typename TR::Digraph Digraph;
904 904

	
905 905
    typedef typename Digraph::Node Node;
906 906
    typedef typename Digraph::NodeIt NodeIt;
907 907
    typedef typename Digraph::Arc Arc;
908 908
    typedef typename Digraph::OutArcIt OutArcIt;
909 909

	
910 910
    ///\brief The type of the map that stores the predecessor
911 911
    ///arcs of the DFS paths.
912 912
    typedef typename TR::PredMap PredMap;
913 913
    ///\brief The type of the map that stores the distances of the nodes.
914 914
    typedef typename TR::DistMap DistMap;
915 915
    ///\brief The type of the map that indicates which nodes are reached.
916 916
    typedef typename TR::ReachedMap ReachedMap;
917 917
    ///\brief The type of the map that indicates which nodes are processed.
918 918
    typedef typename TR::ProcessedMap ProcessedMap;
919 919
    ///The type of the DFS paths
920 920
    typedef typename TR::Path Path;
921 921

	
922 922
  public:
923 923

	
924 924
    /// Constructor.
925 925
    DfsWizard() : TR() {}
926 926

	
927 927
    /// Constructor that requires parameters.
928 928

	
929 929
    /// Constructor that requires parameters.
930 930
    /// These parameters will be the default values for the traits class.
931 931
    /// \param g The digraph the algorithm runs on.
932 932
    DfsWizard(const Digraph &g) :
933 933
      TR(g) {}
934 934

	
935 935
    ///Copy constructor
936 936
    DfsWizard(const TR &b) : TR(b) {}
937 937

	
938 938
    ~DfsWizard() {}
939 939

	
940 940
    ///Runs DFS algorithm from the given source node.
941 941

	
942 942
    ///This method runs DFS algorithm from node \c s
943 943
    ///in order to compute the DFS path to each node.
944 944
    void run(Node s)
945 945
    {
946 946
      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
947 947
      if (Base::_pred)
948 948
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
949 949
      if (Base::_dist)
950 950
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
951 951
      if (Base::_reached)
952 952
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
953 953
      if (Base::_processed)
954 954
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
955 955
      if (s!=INVALID)
956 956
        alg.run(s);
957 957
      else
958 958
        alg.run();
959 959
    }
960 960

	
961 961
    ///Finds the DFS path between \c s and \c t.
962 962

	
963 963
    ///This method runs DFS algorithm from node \c s
964 964
    ///in order to compute the DFS path to node \c t
965 965
    ///(it stops searching when \c t is processed).
966 966
    ///
967 967
    ///\return \c true if \c t is reachable form \c s.
968 968
    bool run(Node s, Node t)
969 969
    {
970 970
      Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
971 971
      if (Base::_pred)
972 972
        alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
973 973
      if (Base::_dist)
974 974
        alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
975 975
      if (Base::_reached)
976 976
        alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
977 977
      if (Base::_processed)
978 978
        alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
979 979
      alg.run(s,t);
980 980
      if (Base::_path)
981 981
        *reinterpret_cast<Path*>(Base::_path) = alg.path(t);
982 982
      if (Base::_di)
983 983
        *Base::_di = alg.dist(t);
984 984
      return alg.reached(t);
985 985
      }
986 986

	
987 987
    ///Runs DFS algorithm to visit all nodes in the digraph.
988 988

	
989 989
    ///This method runs DFS algorithm in order to compute
990 990
    ///the DFS path to each node.
991 991
    void run()
992 992
    {
993 993
      run(INVALID);
994 994
    }
995 995

	
996 996
    template<class T>
997 997
    struct SetPredMapBase : public Base {
998 998
      typedef T PredMap;
999 999
      static PredMap *createPredMap(const Digraph &) { return 0; };
1000 1000
      SetPredMapBase(const TR &b) : TR(b) {}
1001 1001
    };
1002 1002
    ///\brief \ref named-func-param "Named parameter"
1003 1003
    ///for setting PredMap object.
1004 1004
    ///
1005 1005
    ///\ref named-func-param "Named parameter"
1006 1006
    ///for setting PredMap object.
1007 1007
    template<class T>
1008 1008
    DfsWizard<SetPredMapBase<T> > predMap(const T &t)
1009 1009
    {
1010 1010
      Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
1011 1011
      return DfsWizard<SetPredMapBase<T> >(*this);
1012 1012
    }
1013 1013

	
1014 1014
    template<class T>
1015 1015
    struct SetReachedMapBase : public Base {
1016 1016
      typedef T ReachedMap;
1017 1017
      static ReachedMap *createReachedMap(const Digraph &) { return 0; };
1018 1018
      SetReachedMapBase(const TR &b) : TR(b) {}
1019 1019
    };
1020 1020
    ///\brief \ref named-func-param "Named parameter"
1021 1021
    ///for setting ReachedMap object.
1022 1022
    ///
1023 1023
    /// \ref named-func-param "Named parameter"
1024 1024
    ///for setting ReachedMap object.
1025 1025
    template<class T>
1026 1026
    DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
1027 1027
    {
1028 1028
      Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
1029 1029
      return DfsWizard<SetReachedMapBase<T> >(*this);
1030 1030
    }
1031 1031

	
1032 1032
    template<class T>
1033 1033
    struct SetDistMapBase : public Base {
1034 1034
      typedef T DistMap;
1035 1035
      static DistMap *createDistMap(const Digraph &) { return 0; };
1036 1036
      SetDistMapBase(const TR &b) : TR(b) {}
1037 1037
    };
1038 1038
    ///\brief \ref named-func-param "Named parameter"
1039 1039
    ///for setting DistMap object.
1040 1040
    ///
1041 1041
    /// \ref named-func-param "Named parameter"
1042 1042
    ///for setting DistMap object.
1043 1043
    template<class T>
1044 1044
    DfsWizard<SetDistMapBase<T> > distMap(const T &t)
1045 1045
    {
1046 1046
      Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1047 1047
      return DfsWizard<SetDistMapBase<T> >(*this);
1048 1048
    }
1049 1049

	
1050 1050
    template<class T>
1051 1051
    struct SetProcessedMapBase : public Base {
1052 1052
      typedef T ProcessedMap;
1053 1053
      static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
1054 1054
      SetProcessedMapBase(const TR &b) : TR(b) {}
1055 1055
    };
1056 1056
    ///\brief \ref named-func-param "Named parameter"
1057 1057
    ///for setting ProcessedMap object.
1058 1058
    ///
1059 1059
    /// \ref named-func-param "Named parameter"
1060 1060
    ///for setting ProcessedMap object.
1061 1061
    template<class T>
1062 1062
    DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
1063 1063
    {
1064 1064
      Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
1065 1065
      return DfsWizard<SetProcessedMapBase<T> >(*this);
1066 1066
    }
1067 1067

	
1068 1068
    template<class T>
1069 1069
    struct SetPathBase : public Base {
1070 1070
      typedef T Path;
1071 1071
      SetPathBase(const TR &b) : TR(b) {}
1072 1072
    };
1073 1073
    ///\brief \ref named-func-param "Named parameter"
1074 1074
    ///for getting the DFS path to the target node.
1075 1075
    ///
1076 1076
    ///\ref named-func-param "Named parameter"
1077 1077
    ///for getting the DFS path to the target node.
1078 1078
    template<class T>
1079 1079
    DfsWizard<SetPathBase<T> > path(const T &t)
1080 1080
    {
1081 1081
      Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
1082 1082
      return DfsWizard<SetPathBase<T> >(*this);
1083 1083
    }
1084 1084

	
1085 1085
    ///\brief \ref named-func-param "Named parameter"
1086 1086
    ///for getting the distance of the target node.
1087 1087
    ///
1088 1088
    ///\ref named-func-param "Named parameter"
1089 1089
    ///for getting the distance of the target node.
1090 1090
    DfsWizard dist(const int &d)
1091 1091
    {
1092 1092
      Base::_di=const_cast<int*>(&d);
1093 1093
      return *this;
1094 1094
    }
1095 1095

	
1096 1096
  };
1097 1097

	
1098 1098
  ///Function-type interface for DFS algorithm.
1099 1099

	
1100 1100
  ///\ingroup search
1101 1101
  ///Function-type interface for DFS algorithm.
1102 1102
  ///
1103 1103
  ///This function also has several \ref named-func-param "named parameters",
1104 1104
  ///they are declared as the members of class \ref DfsWizard.
1105 1105
  ///The following examples show how to use these parameters.
1106 1106
  ///\code
1107 1107
  ///  // Compute the DFS tree
1108 1108
  ///  dfs(g).predMap(preds).distMap(dists).run(s);
1109 1109
  ///
1110 1110
  ///  // Compute the DFS path from s to t
1111 1111
  ///  bool reached = dfs(g).path(p).dist(d).run(s,t);
1112 1112
  ///\endcode
1113 1113

	
1114 1114
  ///\warning Don't forget to put the \ref DfsWizard::run() "run()"
1115 1115
  ///to the end of the parameter list.
1116 1116
  ///\sa DfsWizard
1117 1117
  ///\sa Dfs
1118 1118
  template<class GR>
1119 1119
  DfsWizard<DfsWizardBase<GR> >
1120 1120
  dfs(const GR &digraph)
1121 1121
  {
1122 1122
    return DfsWizard<DfsWizardBase<GR> >(digraph);
1123 1123
  }
1124 1124

	
1125 1125
#ifdef DOXYGEN
1126 1126
  /// \brief Visitor class for DFS.
1127 1127
  ///
1128 1128
  /// This class defines the interface of the DfsVisit events, and
1129 1129
  /// it could be the base of a real visitor class.
1130 1130
  template <typename _Digraph>
1131 1131
  struct DfsVisitor {
1132 1132
    typedef _Digraph Digraph;
1133 1133
    typedef typename Digraph::Arc Arc;
1134 1134
    typedef typename Digraph::Node Node;
1135 1135
    /// \brief Called for the source node of the DFS.
1136 1136
    ///
1137 1137
    /// This function is called for the source node of the DFS.
1138 1138
    void start(const Node& node) {}
1139 1139
    /// \brief Called when the source node is leaved.
1140 1140
    ///
1141 1141
    /// This function is called when the source node is leaved.
1142 1142
    void stop(const Node& node) {}
1143 1143
    /// \brief Called when a node is reached first time.
1144 1144
    ///
1145 1145
    /// This function is called when a node is reached first time.
1146 1146
    void reach(const Node& node) {}
1147 1147
    /// \brief Called when an arc reaches a new node.
1148 1148
    ///
1149 1149
    /// This function is called when the DFS finds an arc whose target node
1150 1150
    /// is not reached yet.
1151 1151
    void discover(const Arc& arc) {}
1152 1152
    /// \brief Called when an arc is examined but its target node is
1153 1153
    /// already discovered.
1154 1154
    ///
1155 1155
    /// This function is called when an arc is examined but its target node is
1156 1156
    /// already discovered.
1157 1157
    void examine(const Arc& arc) {}
1158 1158
    /// \brief Called when the DFS steps back from a node.
1159 1159
    ///
1160 1160
    /// This function is called when the DFS steps back from a node.
1161 1161
    void leave(const Node& node) {}
1162 1162
    /// \brief Called when the DFS steps back on an arc.
1163 1163
    ///
1164 1164
    /// This function is called when the DFS steps back on an arc.
1165 1165
    void backtrack(const Arc& arc) {}
1166 1166
  };
1167 1167
#else
1168 1168
  template <typename _Digraph>
1169 1169
  struct DfsVisitor {
1170 1170
    typedef _Digraph Digraph;
1171 1171
    typedef typename Digraph::Arc Arc;
1172 1172
    typedef typename Digraph::Node Node;
1173 1173
    void start(const Node&) {}
1174 1174
    void stop(const Node&) {}
1175 1175
    void reach(const Node&) {}
1176 1176
    void discover(const Arc&) {}
1177 1177
    void examine(const Arc&) {}
1178 1178
    void leave(const Node&) {}
1179 1179
    void backtrack(const Arc&) {}
1180 1180

	
1181 1181
    template <typename _Visitor>
1182 1182
    struct Constraints {
1183 1183
      void constraints() {
1184 1184
        Arc arc;
1185 1185
        Node node;
1186 1186
        visitor.start(node);
1187 1187
        visitor.stop(arc);
1188 1188
        visitor.reach(node);
1189 1189
        visitor.discover(arc);
1190 1190
        visitor.examine(arc);
1191 1191
        visitor.leave(node);
1192 1192
        visitor.backtrack(arc);
1193 1193
      }
1194 1194
      _Visitor& visitor;
1195 1195
    };
1196 1196
  };
1197 1197
#endif
1198 1198

	
1199 1199
  /// \brief Default traits class of DfsVisit class.
1200 1200
  ///
1201 1201
  /// Default traits class of DfsVisit class.
1202 1202
  /// \tparam _Digraph The type of the digraph the algorithm runs on.
1203 1203
  template<class _Digraph>
1204 1204
  struct DfsVisitDefaultTraits {
1205 1205

	
1206 1206
    /// \brief The type of the digraph the algorithm runs on.
1207 1207
    typedef _Digraph Digraph;
1208 1208

	
1209 1209
    /// \brief The type of the map that indicates which nodes are reached.
1210 1210
    ///
1211 1211
    /// The type of the map that indicates which nodes are reached.
1212 1212
    /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
1213 1213
    typedef typename Digraph::template NodeMap<bool> ReachedMap;
1214 1214

	
1215 1215
    /// \brief Instantiates a ReachedMap.
1216 1216
    ///
1217 1217
    /// This function instantiates a ReachedMap.
1218 1218
    /// \param digraph is the digraph, to which
1219 1219
    /// we would like to define the ReachedMap.
1220 1220
    static ReachedMap *createReachedMap(const Digraph &digraph) {
1221 1221
      return new ReachedMap(digraph);
1222 1222
    }
1223 1223

	
1224 1224
  };
1225 1225

	
1226 1226
  /// \ingroup search
1227 1227
  ///
1228 1228
  /// \brief %DFS algorithm class with visitor interface.
1229 1229
  ///
1230 1230
  /// This class provides an efficient implementation of the %DFS algorithm
1231 1231
  /// with visitor interface.
1232 1232
  ///
1233 1233
  /// The %DfsVisit class provides an alternative interface to the Dfs
1234 1234
  /// class. It works with callback mechanism, the DfsVisit object calls
1235 1235
  /// the member functions of the \c Visitor class on every DFS event.
1236 1236
  ///
1237 1237
  /// This interface of the DFS algorithm should be used in special cases
1238 1238
  /// when extra actions have to be performed in connection with certain
1239 1239
  /// events of the DFS algorithm. Otherwise consider to use Dfs or dfs()
1240 1240
  /// instead.
1241 1241
  ///
1242 1242
  /// \tparam _Digraph The type of the digraph the algorithm runs on.
1243 1243
  /// The default value is
1244 1244
  /// \ref ListDigraph. The value of _Digraph is not used directly by
1245 1245
  /// \ref DfsVisit, it is only passed to \ref DfsVisitDefaultTraits.
1246 1246
  /// \tparam _Visitor The Visitor type that is used by the algorithm.
1247 1247
  /// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty visitor, which
1248 1248
  /// does not observe the DFS events. If you want to observe the DFS
1249 1249
  /// events, you should implement your own visitor class.
1250 1250
  /// \tparam _Traits Traits class to set various data types used by the
1251 1251
  /// algorithm. The default traits class is
1252 1252
  /// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Digraph>".
1253 1253
  /// See \ref DfsVisitDefaultTraits for the documentation of
1254 1254
  /// a DFS visit traits class.
1255 1255
#ifdef DOXYGEN
1256 1256
  template <typename _Digraph, typename _Visitor, typename _Traits>
1257 1257
#else
1258 1258
  template <typename _Digraph = ListDigraph,
1259 1259
            typename _Visitor = DfsVisitor<_Digraph>,
1260 1260
            typename _Traits = DfsVisitDefaultTraits<_Digraph> >
1261 1261
#endif
1262 1262
  class DfsVisit {
1263 1263
  public:
1264 1264

	
1265 1265
    ///The traits class.
1266 1266
    typedef _Traits Traits;
1267 1267

	
1268 1268
    ///The type of the digraph the algorithm runs on.
1269 1269
    typedef typename Traits::Digraph Digraph;
1270 1270

	
1271 1271
    ///The visitor type used by the algorithm.
1272 1272
    typedef _Visitor Visitor;
1273 1273

	
1274 1274
    ///The type of the map that indicates which nodes are reached.
1275 1275
    typedef typename Traits::ReachedMap ReachedMap;
1276 1276

	
1277 1277
  private:
1278 1278

	
1279 1279
    typedef typename Digraph::Node Node;
1280 1280
    typedef typename Digraph::NodeIt NodeIt;
1281 1281
    typedef typename Digraph::Arc Arc;
1282 1282
    typedef typename Digraph::OutArcIt OutArcIt;
1283 1283

	
1284 1284
    //Pointer to the underlying digraph.
1285 1285
    const Digraph *_digraph;
1286 1286
    //Pointer to the visitor object.
1287 1287
    Visitor *_visitor;
1288 1288
    //Pointer to the map of reached status of the nodes.
1289 1289
    ReachedMap *_reached;
1290 1290
    //Indicates if _reached is locally allocated (true) or not.
1291 1291
    bool local_reached;
1292 1292

	
1293 1293
    std::vector<typename Digraph::Arc> _stack;
1294 1294
    int _stack_head;
1295 1295

	
1296 1296
    //Creates the maps if necessary.
1297 1297
    void create_maps() {
1298 1298
      if(!_reached) {
1299 1299
        local_reached = true;
1300 1300
        _reached = Traits::createReachedMap(*_digraph);
1301 1301
      }
1302 1302
    }
1303 1303

	
1304 1304
  protected:
1305 1305

	
1306 1306
    DfsVisit() {}
1307 1307

	
1308 1308
  public:
1309 1309

	
1310 1310
    typedef DfsVisit Create;
1311 1311

	
1312 1312
    /// \name Named template parameters
1313 1313

	
1314 1314
    ///@{
1315 1315
    template <class T>
1316 1316
    struct SetReachedMapTraits : public Traits {
1317 1317
      typedef T ReachedMap;
1318 1318
      static ReachedMap *createReachedMap(const Digraph &digraph) {
1319 1319
        LEMON_ASSERT(false, "ReachedMap is not initialized");
1320 1320
        return 0; // ignore warnings
1321 1321
      }
1322 1322
    };
1323 1323
    /// \brief \ref named-templ-param "Named parameter" for setting
1324 1324
    /// ReachedMap type.
1325 1325
    ///
1326 1326
    /// \ref named-templ-param "Named parameter" for setting ReachedMap type.
1327 1327
    template <class T>
1328 1328
    struct SetReachedMap : public DfsVisit< Digraph, Visitor,
1329 1329
                                            SetReachedMapTraits<T> > {
1330 1330
      typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create;
1331 1331
    };
1332 1332
    ///@}
1333 1333

	
1334 1334
  public:
1335 1335

	
1336 1336
    /// \brief Constructor.
1337 1337
    ///
1338 1338
    /// Constructor.
1339 1339
    ///
1340 1340
    /// \param digraph The digraph the algorithm runs on.
1341 1341
    /// \param visitor The visitor object of the algorithm.
1342 1342
    DfsVisit(const Digraph& digraph, Visitor& visitor)
1343 1343
      : _digraph(&digraph), _visitor(&visitor),
1344 1344
        _reached(0), local_reached(false) {}
1345 1345

	
1346 1346
    /// \brief Destructor.
1347 1347
    ~DfsVisit() {
1348 1348
      if(local_reached) delete _reached;
1349 1349
    }
1350 1350

	
1351 1351
    /// \brief Sets the map that indicates which nodes are reached.
1352 1352
    ///
1353 1353
    /// Sets the map that indicates which nodes are reached.
1354 1354
    /// If you don't use this function before calling \ref run(),
1355 1355
    /// it will allocate one. The destructor deallocates this
1356 1356
    /// automatically allocated map, of course.
1357 1357
    /// \return <tt> (*this) </tt>
1358 1358
    DfsVisit &reachedMap(ReachedMap &m) {
1359 1359
      if(local_reached) {
1360 1360
        delete _reached;
1361 1361
        local_reached=false;
1362 1362
      }
1363 1363
      _reached = &m;
1364 1364
      return *this;
1365 1365
    }
1366 1366

	
1367 1367
  public:
1368 1368

	
1369 1369
    /// \name Execution control
1370 1370
    /// The simplest way to execute the algorithm is to use
1371 1371
    /// one of the member functions called \ref lemon::DfsVisit::run()
1372 1372
    /// "run()".
1373 1373
    /// \n
1374 1374
    /// If you need more control on the execution, first you must call
1375 1375
    /// \ref lemon::DfsVisit::init() "init()", then you can add several
1376 1376
    /// source nodes with \ref lemon::DfsVisit::addSource() "addSource()".
1377 1377
    /// Finally \ref lemon::DfsVisit::start() "start()" will perform the
1378 1378
    /// actual path computation.
1379 1379

	
1380 1380
    /// @{
1381 1381

	
1382 1382
    /// \brief Initializes the internal data structures.
1383 1383
    ///
1384 1384
    /// Initializes the internal data structures.
1385 1385
    void init() {
1386 1386
      create_maps();
1387 1387
      _stack.resize(countNodes(*_digraph));
1388 1388
      _stack_head = -1;
1389 1389
      for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
1390 1390
        _reached->set(u, false);
1391 1391
      }
1392 1392
    }
1393 1393

	
1394 1394
    ///Adds a new source node.
1395 1395

	
1396 1396
    ///Adds a new source node to the set of nodes to be processed.
1397 1397
    ///
1398 1398
    ///\pre The stack must be empty. (Otherwise the algorithm gives
1399 1399
    ///false results.)
1400 1400
    ///
1401 1401
    ///\warning Distances will be wrong (or at least strange) in case of
1402 1402
    ///multiple sources.
1403 1403
    void addSource(Node s)
1404 1404
    {
1405 1405
      LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
1406 1406
      if(!(*_reached)[s]) {
1407 1407
          _reached->set(s,true);
1408 1408
          _visitor->start(s);
1409 1409
          _visitor->reach(s);
1410 1410
          Arc e;
1411 1411
          _digraph->firstOut(e, s);
1412 1412
          if (e != INVALID) {
1413 1413
            _stack[++_stack_head] = e;
1414 1414
          } else {
1415 1415
            _visitor->leave(s);
1416 1416
          }
1417 1417
        }
1418 1418
    }
1419 1419

	
1420 1420
    /// \brief Processes the next arc.
1421 1421
    ///
1422 1422
    /// Processes the next arc.
1423 1423
    ///
1424 1424
    /// \return The processed arc.
1425 1425
    ///
1426 1426
    /// \pre The stack must not be empty.
1427 1427
    Arc processNextArc() {
1428 1428
      Arc e = _stack[_stack_head];
1429 1429
      Node m = _digraph->target(e);
1430 1430
      if(!(*_reached)[m]) {
1431 1431
        _visitor->discover(e);
1432 1432
        _visitor->reach(m);
1433 1433
        _reached->set(m, true);
1434 1434
        _digraph->firstOut(_stack[++_stack_head], m);
1435 1435
      } else {
1436 1436
        _visitor->examine(e);
1437 1437
        m = _digraph->source(e);
1438 1438
        _digraph->nextOut(_stack[_stack_head]);
1439 1439
      }
1440 1440
      while (_stack_head>=0 && _stack[_stack_head] == INVALID) {
1441 1441
        _visitor->leave(m);
1442 1442
        --_stack_head;
1443 1443
        if (_stack_head >= 0) {
1444 1444
          _visitor->backtrack(_stack[_stack_head]);
1445 1445
          m = _digraph->source(_stack[_stack_head]);
1446 1446
          _digraph->nextOut(_stack[_stack_head]);
1447 1447
        } else {
1448 1448
          _visitor->stop(m);
1449 1449
        }
1450 1450
      }
1451 1451
      return e;
1452 1452
    }
1453 1453

	
1454 1454
    /// \brief Next arc to be processed.
1455 1455
    ///
1456 1456
    /// Next arc to be processed.
1457 1457
    ///
1458 1458
    /// \return The next arc to be processed or INVALID if the stack is
1459 1459
    /// empty.
1460 1460
    Arc nextArc() const {
1461 1461
      return _stack_head >= 0 ? _stack[_stack_head] : INVALID;
1462 1462
    }
1463 1463

	
1464 1464
    /// \brief Returns \c false if there are nodes
1465 1465
    /// to be processed.
1466 1466
    ///
1467 1467
    /// Returns \c false if there are nodes
1468 1468
    /// to be processed in the queue (stack).
1469 1469
    bool emptyQueue() const { return _stack_head < 0; }
1470 1470

	
1471 1471
    /// \brief Returns the number of the nodes to be processed.
1472 1472
    ///
1473 1473
    /// Returns the number of the nodes to be processed in the queue (stack).
1474 1474
    int queueSize() const { return _stack_head + 1; }
1475 1475

	
1476 1476
    /// \brief Executes the algorithm.
1477 1477
    ///
1478 1478
    /// Executes the algorithm.
1479 1479
    ///
1480 1480
    /// This method runs the %DFS algorithm from the root node
1481 1481
    /// in order to compute the %DFS path to each node.
1482 1482
    ///
1483 1483
    /// The algorithm computes
1484 1484
    /// - the %DFS tree,
1485 1485
    /// - the distance of each node from the root in the %DFS tree.
1486 1486
    ///
1487 1487
    /// \pre init() must be called and a root node should be
1488 1488
    /// added with addSource() before using this function.
1489 1489
    ///
1490 1490
    /// \note <tt>d.start()</tt> is just a shortcut of the following code.
1491 1491
    /// \code
1492 1492
    ///   while ( !d.emptyQueue() ) {
1493 1493
    ///     d.processNextArc();
1494 1494
    ///   }
1495 1495
    /// \endcode
1496 1496
    void start() {
1497 1497
      while ( !emptyQueue() ) processNextArc();
1498 1498
    }
1499 1499

	
1500 1500
    /// \brief Executes the algorithm until the given target node is reached.
1501 1501
    ///
1502 1502
    /// Executes the algorithm until the given target node is reached.
1503 1503
    ///
1504 1504
    /// This method runs the %DFS algorithm from the root node
1505 1505
    /// in order to compute the DFS path to \c t.
1506 1506
    ///
1507 1507
    /// The algorithm computes
1508 1508
    /// - the %DFS path to \c t,
1509 1509
    /// - the distance of \c t from the root in the %DFS tree.
1510 1510
    ///
1511 1511
    /// \pre init() must be called and a root node should be added
1512 1512
    /// with addSource() before using this function.
1513 1513
    void start(Node t) {
1514
      while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != t )
1514
      while ( !emptyQueue() && !(*_reached)[t] )
1515 1515
        processNextArc();
1516 1516
    }
1517 1517

	
1518 1518
    /// \brief Executes the algorithm until a condition is met.
1519 1519
    ///
1520 1520
    /// Executes the algorithm until a condition is met.
1521 1521
    ///
1522 1522
    /// This method runs the %DFS algorithm from the root node
1523 1523
    /// until an arc \c a with <tt>am[a]</tt> true is found.
1524 1524
    ///
1525 1525
    /// \param am A \c bool (or convertible) arc map. The algorithm
1526 1526
    /// will stop when it reaches an arc \c a with <tt>am[a]</tt> true.
1527 1527
    ///
1528 1528
    /// \return The reached arc \c a with <tt>am[a]</tt> true or
1529 1529
    /// \c INVALID if no such arc was found.
1530 1530
    ///
1531 1531
    /// \pre init() must be called and a root node should be added
1532 1532
    /// with addSource() before using this function.
1533 1533
    ///
1534 1534
    /// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map,
1535 1535
    /// not a node map.
1536 1536
    template <typename AM>
1537 1537
    Arc start(const AM &am) {
1538 1538
      while ( !emptyQueue() && !am[_stack[_stack_head]] )
1539 1539
        processNextArc();
1540 1540
      return emptyQueue() ? INVALID : _stack[_stack_head];
1541 1541
    }
1542 1542

	
1543 1543
    /// \brief Runs the algorithm from the given source node.
1544 1544
    ///
1545 1545
    /// This method runs the %DFS algorithm from node \c s.
1546 1546
    /// in order to compute the DFS path to each node.
1547 1547
    ///
1548 1548
    /// The algorithm computes
1549 1549
    /// - the %DFS tree,
1550 1550
    /// - the distance of each node from the root in the %DFS tree.
1551 1551
    ///
1552 1552
    /// \note <tt>d.run(s)</tt> is just a shortcut of the following code.
1553 1553
    ///\code
1554 1554
    ///   d.init();
1555 1555
    ///   d.addSource(s);
1556 1556
    ///   d.start();
1557 1557
    ///\endcode
1558 1558
    void run(Node s) {
1559 1559
      init();
1560 1560
      addSource(s);
1561 1561
      start();
1562 1562
    }
1563 1563

	
1564 1564
    /// \brief Finds the %DFS path between \c s and \c t.
1565 1565

	
1566 1566
    /// This method runs the %DFS algorithm from node \c s
1567 1567
    /// in order to compute the DFS path to node \c t
1568 1568
    /// (it stops searching when \c t is processed).
1569 1569
    ///
1570 1570
    /// \return \c true if \c t is reachable form \c s.
1571 1571
    ///
1572 1572
    /// \note Apart from the return value, <tt>d.run(s,t)</tt> is
1573 1573
    /// just a shortcut of the following code.
1574 1574
    ///\code
1575 1575
    ///   d.init();
1576 1576
    ///   d.addSource(s);
1577 1577
    ///   d.start(t);
1578 1578
    ///\endcode
1579 1579
    bool run(Node s,Node t) {
1580 1580
      init();
1581 1581
      addSource(s);
1582 1582
      start(t);
1583 1583
      return reached(t);
1584 1584
    }
1585 1585

	
1586 1586
    /// \brief Runs the algorithm to visit all nodes in the digraph.
1587 1587

	
1588 1588
    /// This method runs the %DFS algorithm in order to
1589 1589
    /// compute the %DFS path to each node.
1590 1590
    ///
1591 1591
    /// The algorithm computes
1592 1592
    /// - the %DFS tree,
1593 1593
    /// - the distance of each node from the root in the %DFS tree.
1594 1594
    ///
1595 1595
    /// \note <tt>d.run()</tt> is just a shortcut of the following code.
1596 1596
    ///\code
1597 1597
    ///   d.init();
1598 1598
    ///   for (NodeIt n(digraph); n != INVALID; ++n) {
1599 1599
    ///     if (!d.reached(n)) {
1600 1600
    ///       d.addSource(n);
1601 1601
    ///       d.start();
1602 1602
    ///     }
1603 1603
    ///   }
1604 1604
    ///\endcode
1605 1605
    void run() {
1606 1606
      init();
1607 1607
      for (NodeIt it(*_digraph); it != INVALID; ++it) {
1608 1608
        if (!reached(it)) {
1609 1609
          addSource(it);
1610 1610
          start();
1611 1611
        }
1612 1612
      }
1613 1613
    }
1614 1614

	
1615 1615
    ///@}
1616 1616

	
1617 1617
    /// \name Query Functions
1618 1618
    /// The result of the %DFS algorithm can be obtained using these
1619 1619
    /// functions.\n
1620 1620
    /// Either \ref lemon::DfsVisit::run() "run()" or
1621 1621
    /// \ref lemon::DfsVisit::start() "start()" must be called before
1622 1622
    /// using them.
1623 1623
    ///@{
1624 1624

	
1625 1625
    /// \brief Checks if a node is reachable from the root(s).
1626 1626
    ///
1627 1627
    /// Returns \c true if \c v is reachable from the root(s).
1628 1628
    /// \pre Either \ref run() or \ref start()
1629 1629
    /// must be called before using this function.
1630 1630
    bool reached(Node v) { return (*_reached)[v]; }
1631 1631

	
1632 1632
    ///@}
1633 1633

	
1634 1634
  };
1635 1635

	
1636 1636
} //END OF NAMESPACE LEMON
1637 1637

	
1638 1638
#endif
Ignore white space 6 line context
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
#include <lemon/concepts/digraph.h>
20 20
#include <lemon/smart_graph.h>
21 21
#include <lemon/list_graph.h>
22 22
#include <lemon/lgf_reader.h>
23 23
#include <lemon/dfs.h>
24 24
#include <lemon/path.h>
25 25

	
26 26
#include "graph_test.h"
27 27
#include "test_tools.h"
28 28

	
29 29
using namespace lemon;
30 30

	
31 31
char test_lgf[] =
32 32
  "@nodes\n"
33 33
  "label\n"
34 34
  "0\n"
35 35
  "1\n"
36 36
  "2\n"
37 37
  "3\n"
38 38
  "4\n"
39 39
  "5\n"
40 40
  "6\n"
41 41
  "@arcs\n"
42 42
  "     label\n"
43 43
  "0 1  0\n"
44 44
  "1 2  1\n"
45 45
  "2 3  2\n"
46 46
  "1 4  3\n"
47 47
  "4 2  4\n"
48 48
  "4 5  5\n"
49 49
  "5 0  6\n"
50 50
  "6 3  7\n"
51 51
  "@attributes\n"
52 52
  "source 0\n"
53
  "target 5\n";
53
  "target 5\n"
54
  "source1 6\n"
55
  "target1 3\n";
56

	
54 57

	
55 58
void checkDfsCompile()
56 59
{
57 60
  typedef concepts::Digraph Digraph;
58 61
  typedef Dfs<Digraph> DType;
59 62
  typedef Digraph::Node Node;
60 63
  typedef Digraph::Arc Arc;
61 64

	
62 65
  Digraph G;
63 66
  Node s, t;
64 67
  Arc e;
65 68
  int l;
66 69
  bool b;
67 70
  DType::DistMap d(G);
68 71
  DType::PredMap p(G);
69 72
  Path<Digraph> pp;
70 73

	
71 74
  {
72 75
    DType dfs_test(G);
73 76

	
74 77
    dfs_test.run(s);
75 78
    dfs_test.run(s,t);
76 79
    dfs_test.run();
77 80

	
78 81
    l  = dfs_test.dist(t);
79 82
    e  = dfs_test.predArc(t);
80 83
    s  = dfs_test.predNode(t);
81 84
    b  = dfs_test.reached(t);
82 85
    d  = dfs_test.distMap();
83 86
    p  = dfs_test.predMap();
84 87
    pp = dfs_test.path(t);
85 88
  }
86 89
  {
87 90
    DType
88 91
      ::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
89 92
      ::SetDistMap<concepts::ReadWriteMap<Node,int> >
90 93
      ::SetReachedMap<concepts::ReadWriteMap<Node,bool> >
91 94
      ::SetProcessedMap<concepts::WriteMap<Node,bool> >
92 95
      ::SetStandardProcessedMap
93 96
      ::Create dfs_test(G);
94 97

	
95 98
    dfs_test.run(s);
96 99
    dfs_test.run(s,t);
97 100
    dfs_test.run();
98 101

	
99 102
    l  = dfs_test.dist(t);
100 103
    e  = dfs_test.predArc(t);
101 104
    s  = dfs_test.predNode(t);
102 105
    b  = dfs_test.reached(t);
103 106
    pp = dfs_test.path(t);
104 107
  }
105 108
}
106 109

	
107 110
void checkDfsFunctionCompile()
108 111
{
109 112
  typedef int VType;
110 113
  typedef concepts::Digraph Digraph;
111 114
  typedef Digraph::Arc Arc;
112 115
  typedef Digraph::Node Node;
113 116

	
114 117
  Digraph g;
115 118
  bool b;
116 119
  dfs(g).run(Node());
117 120
  b=dfs(g).run(Node(),Node());
118 121
  dfs(g).run();
119 122
  dfs(g)
120 123
    .predMap(concepts::ReadWriteMap<Node,Arc>())
121 124
    .distMap(concepts::ReadWriteMap<Node,VType>())
122 125
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
123 126
    .processedMap(concepts::WriteMap<Node,bool>())
124 127
    .run(Node());
125 128
  b=dfs(g)
126 129
    .predMap(concepts::ReadWriteMap<Node,Arc>())
127 130
    .distMap(concepts::ReadWriteMap<Node,VType>())
128 131
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
129 132
    .processedMap(concepts::WriteMap<Node,bool>())
130 133
    .path(concepts::Path<Digraph>())
131 134
    .dist(VType())
132 135
    .run(Node(),Node());
133 136
  dfs(g)
134 137
    .predMap(concepts::ReadWriteMap<Node,Arc>())
135 138
    .distMap(concepts::ReadWriteMap<Node,VType>())
136 139
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
137 140
    .processedMap(concepts::WriteMap<Node,bool>())
138 141
    .run();
139 142
}
140 143

	
141 144
template <class Digraph>
142 145
void checkDfs() {
143 146
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
144 147

	
145 148
  Digraph G;
146 149
  Node s, t;
150
  Node s1, t1;
147 151

	
148 152
  std::istringstream input(test_lgf);
149 153
  digraphReader(G, input).
150 154
    node("source", s).
151 155
    node("target", t).
156
    node("source1", s1).
157
    node("target1", t1).
152 158
    run();
153 159

	
154 160
  Dfs<Digraph> dfs_test(G);
155 161
  dfs_test.run(s);
156 162

	
157 163
  Path<Digraph> p = dfs_test.path(t);
158 164
  check(p.length() == dfs_test.dist(t),"path() found a wrong path.");
159 165
  check(checkPath(G, p),"path() found a wrong path.");
160 166
  check(pathSource(G, p) == s,"path() found a wrong path.");
161 167
  check(pathTarget(G, p) == t,"path() found a wrong path.");
162 168

	
163 169
  for(NodeIt v(G); v!=INVALID; ++v) {
164 170
    if (dfs_test.reached(v)) {
165 171
      check(v==s || dfs_test.predArc(v)!=INVALID, "Wrong tree.");
166 172
      if (dfs_test.predArc(v)!=INVALID ) {
167 173
        Arc e=dfs_test.predArc(v);
168 174
        Node u=G.source(e);
169 175
        check(u==dfs_test.predNode(v),"Wrong tree.");
170 176
        check(dfs_test.dist(v) - dfs_test.dist(u) == 1,
171 177
              "Wrong distance. (" << dfs_test.dist(u) << "->"
172 178
              << dfs_test.dist(v) << ")");
173 179
      }
174 180
    }
175 181
  }
176 182

	
177 183
  {
184
  Dfs<Digraph> dfs(G);
185
  check(dfs.run(s1,t1) && dfs.reached(t1),"Node 3 is reachable from Node 6.");
186
  }
187
  
188
  {
178 189
    NullMap<Node,Arc> myPredMap;
179 190
    dfs(G).predMap(myPredMap).run(s);
180 191
  }
181 192
}
182 193

	
183 194
int main()
184 195
{
185 196
  checkDfs<ListDigraph>();
186 197
  checkDfs<SmartDigraph>();
187 198
  return 0;
188 199
}
0 comments (0 inline)