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 ↑
Show white space 384 line context
... ...
@@ -369,385 +369,385 @@
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.
... ...
@@ -1322,317 +1322,317 @@
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
Show white space 384 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)