gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge bugfix #392 to branch 1.1
0 2 0
merge 1.1
2 files changed with 14 insertions and 3 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
... ...
@@ -368,385 +368,385 @@
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(Node) "run()"
376 376
    ///or \ref init(), an instance will be allocated automatically.
377 377
    ///The destructor deallocates this automatically allocated map,
378 378
    ///of course.
379 379
    ///\return <tt> (*this) </tt>
380 380
    Dfs &processedMap(ProcessedMap &m)
381 381
    {
382 382
      if(local_processed) {
383 383
        delete _processed;
384 384
        local_processed=false;
385 385
      }
386 386
      _processed = &m;
387 387
      return *this;
388 388
    }
389 389

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

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

	
409 409
  public:
410 410

	
411 411
    ///\name Execution Control
412 412
    ///The simplest way to execute the DFS algorithm is to use one of the
413 413
    ///member functions called \ref run(Node) "run()".\n
414 414
    ///If you need more control on the execution, first you have to call
415 415
    ///\ref init(), then you can add a source node with \ref addSource()
416 416
    ///and perform the actual computation with \ref start().
417 417
    ///This procedure can be repeated if there are nodes that have not
418 418
    ///been reached.
419 419

	
420 420
    ///@{
421 421

	
422 422
    ///\brief Initializes the internal data structures.
423 423
    ///
424 424
    ///Initializes the internal data structures.
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
    ///wrong results. (One of the outgoing arcs of all the source nodes
443 443
    ///except for the last one will not be visited and distances will
444 444
    ///also be wrong.)
445 445
    void addSource(Node s)
446 446
    {
447 447
      LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
448 448
      if(!(*_reached)[s])
449 449
        {
450 450
          _reached->set(s,true);
451 451
          _pred->set(s,INVALID);
452 452
          OutArcIt e(*G,s);
453 453
          if(e!=INVALID) {
454 454
            _stack[++_stack_head]=e;
455 455
            _dist->set(s,_stack_head);
456 456
          }
457 457
          else {
458 458
            _processed->set(s,true);
459 459
            _dist->set(s,0);
460 460
          }
461 461
        }
462 462
    }
463 463

	
464 464
    ///Processes the next arc.
465 465

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

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

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

	
508 508
    ///Returns \c false if there are nodes to be processed.
509 509

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

	
514 514
    ///Returns the number of the nodes to be processed.
515 515

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

	
520 520
    ///Executes the algorithm.
521 521

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

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

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

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

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

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

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

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

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

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

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

	
662 662
    ///@}
663 663

	
664 664
    ///\name Query Functions
665 665
    ///The results of the DFS algorithm can be obtained using these
666 666
    ///functions.\n
667 667
    ///Either \ref run(Node) "run()" or \ref start() should be called
668 668
    ///before using them.
669 669

	
670 670
    ///@{
671 671

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

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

	
682 682
    ///The distance of a node from the root(s).
683 683

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

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

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

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

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

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

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

	
742 742
    ///Checks if a node is reached from the root(s).
743 743

	
744 744
    ///Returns \c true if \c v is reached from the root(s).
745 745
    ///
746 746
    ///\pre Either \ref run(Node) "run()" or \ref init()
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

	
... ...
@@ -1320,318 +1320,318 @@
1320 1320
      }
1321 1321
    };
1322 1322
    /// \brief \ref named-templ-param "Named parameter" for setting
1323 1323
    /// ReachedMap type.
1324 1324
    ///
1325 1325
    /// \ref named-templ-param "Named parameter" for setting ReachedMap type.
1326 1326
    template <class T>
1327 1327
    struct SetReachedMap : public DfsVisit< Digraph, Visitor,
1328 1328
                                            SetReachedMapTraits<T> > {
1329 1329
      typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create;
1330 1330
    };
1331 1331
    ///@}
1332 1332

	
1333 1333
  public:
1334 1334

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

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

	
1350 1350
    /// \brief Sets the map that indicates which nodes are reached.
1351 1351
    ///
1352 1352
    /// Sets the map that indicates which nodes are reached.
1353 1353
    /// If you don't use this function before calling \ref run(Node) "run()"
1354 1354
    /// or \ref init(), an instance will be allocated automatically.
1355 1355
    /// The destructor deallocates this automatically allocated map,
1356 1356
    /// 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 DFS algorithm is to use one of the
1371 1371
    /// member functions called \ref run(Node) "run()".\n
1372 1372
    /// If you need more control on the execution, first you have to call
1373 1373
    /// \ref init(), then you can add a source node with \ref addSource()
1374 1374
    /// and perform the actual computation with \ref start().
1375 1375
    /// This procedure can be repeated if there are nodes that have not
1376 1376
    /// been reached.
1377 1377

	
1378 1378
    /// @{
1379 1379

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

	
1392 1392
    /// \brief Adds a new source node.
1393 1393
    ///
1394 1394
    /// Adds a new source node to the set of nodes to be processed.
1395 1395
    ///
1396 1396
    /// \pre The stack must be empty. Otherwise the algorithm gives
1397 1397
    /// wrong results. (One of the outgoing arcs of all the source nodes
1398 1398
    /// except for the last one will not be visited and distances will
1399 1399
    /// also be wrong.)
1400 1400
    void addSource(Node s)
1401 1401
    {
1402 1402
      LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
1403 1403
      if(!(*_reached)[s]) {
1404 1404
          _reached->set(s,true);
1405 1405
          _visitor->start(s);
1406 1406
          _visitor->reach(s);
1407 1407
          Arc e;
1408 1408
          _digraph->firstOut(e, s);
1409 1409
          if (e != INVALID) {
1410 1410
            _stack[++_stack_head] = e;
1411 1411
          } else {
1412 1412
            _visitor->leave(s);
1413 1413
            _visitor->stop(s);
1414 1414
          }
1415 1415
        }
1416 1416
    }
1417 1417

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

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

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

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

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

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

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

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

	
1562 1562
    /// \brief Finds the %DFS path between \c s and \c t.
1563 1563

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

	
1584 1584
    /// \brief Runs the algorithm to visit all nodes in the digraph.
1585 1585

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

	
1613 1613
    ///@}
1614 1614

	
1615 1615
    /// \name Query Functions
1616 1616
    /// The results of the DFS algorithm can be obtained using these
1617 1617
    /// functions.\n
1618 1618
    /// Either \ref run(Node) "run()" or \ref start() should be called
1619 1619
    /// before using them.
1620 1620

	
1621 1621
    ///@{
1622 1622

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

	
1631 1631
    ///@}
1632 1632

	
1633 1633
  };
1634 1634

	
1635 1635
} //END OF NAMESPACE LEMON
1636 1636

	
1637 1637
#endif
Ignore 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-2009
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, i;
66 69
  bool b;
67 70
  DType::DistMap d(G);
68 71
  DType::PredMap p(G);
69 72
  Path<Digraph> pp;
70 73
  concepts::ReadMap<Arc,bool> am;
71 74

	
72 75
  {
73 76
    DType dfs_test(G);
74 77
    const DType& const_dfs_test = dfs_test;
75 78

	
76 79
    dfs_test.run(s);
77 80
    dfs_test.run(s,t);
78 81
    dfs_test.run();
79 82

	
80 83
    dfs_test.init();
81 84
    dfs_test.addSource(s);
82 85
    e = dfs_test.processNextArc();
83 86
    e = const_dfs_test.nextArc();
84 87
    b = const_dfs_test.emptyQueue();
85 88
    i = const_dfs_test.queueSize();
86 89
    
87 90
    dfs_test.start();
88 91
    dfs_test.start(t);
89 92
    dfs_test.start(am);
90 93

	
91 94
    l  = const_dfs_test.dist(t);
92 95
    e  = const_dfs_test.predArc(t);
93 96
    s  = const_dfs_test.predNode(t);
94 97
    b  = const_dfs_test.reached(t);
95 98
    d  = const_dfs_test.distMap();
96 99
    p  = const_dfs_test.predMap();
97 100
    pp = const_dfs_test.path(t);
98 101
  }
99 102
  {
100 103
    DType
101 104
      ::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
102 105
      ::SetDistMap<concepts::ReadWriteMap<Node,int> >
103 106
      ::SetReachedMap<concepts::ReadWriteMap<Node,bool> >
104 107
      ::SetStandardProcessedMap
105 108
      ::SetProcessedMap<concepts::WriteMap<Node,bool> >
106 109
      ::Create dfs_test(G);
107 110

	
108 111
    concepts::ReadWriteMap<Node,Arc> pred_map;
109 112
    concepts::ReadWriteMap<Node,int> dist_map;
110 113
    concepts::ReadWriteMap<Node,bool> reached_map;
111 114
    concepts::WriteMap<Node,bool> processed_map;
112 115
    
113 116
    dfs_test
114 117
      .predMap(pred_map)
115 118
      .distMap(dist_map)
116 119
      .reachedMap(reached_map)
117 120
      .processedMap(processed_map);
118 121

	
119 122
    dfs_test.run(s);
120 123
    dfs_test.run(s,t);
121 124
    dfs_test.run();
122 125
    dfs_test.init();
123 126

	
124 127
    dfs_test.addSource(s);
125 128
    e = dfs_test.processNextArc();
126 129
    e = dfs_test.nextArc();
127 130
    b = dfs_test.emptyQueue();
128 131
    i = dfs_test.queueSize();
129 132
    
130 133
    dfs_test.start();
131 134
    dfs_test.start(t);
132 135
    dfs_test.start(am);
133 136

	
134 137
    l  = dfs_test.dist(t);
135 138
    e  = dfs_test.predArc(t);
136 139
    s  = dfs_test.predNode(t);
137 140
    b  = dfs_test.reached(t);
138 141
    pp = dfs_test.path(t);
139 142
  }
140 143
}
141 144

	
142 145
void checkDfsFunctionCompile()
143 146
{
144 147
  typedef int VType;
145 148
  typedef concepts::Digraph Digraph;
146 149
  typedef Digraph::Arc Arc;
147 150
  typedef Digraph::Node Node;
148 151

	
149 152
  Digraph g;
150 153
  bool b;
151 154
  dfs(g).run(Node());
152 155
  b=dfs(g).run(Node(),Node());
153 156
  dfs(g).run();
154 157
  dfs(g)
155 158
    .predMap(concepts::ReadWriteMap<Node,Arc>())
156 159
    .distMap(concepts::ReadWriteMap<Node,VType>())
157 160
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
158 161
    .processedMap(concepts::WriteMap<Node,bool>())
159 162
    .run(Node());
160 163
  b=dfs(g)
161 164
    .predMap(concepts::ReadWriteMap<Node,Arc>())
162 165
    .distMap(concepts::ReadWriteMap<Node,VType>())
163 166
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
164 167
    .processedMap(concepts::WriteMap<Node,bool>())
165 168
    .path(concepts::Path<Digraph>())
166 169
    .dist(VType())
167 170
    .run(Node(),Node());
168 171
  dfs(g)
169 172
    .predMap(concepts::ReadWriteMap<Node,Arc>())
170 173
    .distMap(concepts::ReadWriteMap<Node,VType>())
171 174
    .reachedMap(concepts::ReadWriteMap<Node,bool>())
172 175
    .processedMap(concepts::WriteMap<Node,bool>())
173 176
    .run();
174 177
}
175 178

	
176 179
template <class Digraph>
177 180
void checkDfs() {
178 181
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
179 182

	
180 183
  Digraph G;
181 184
  Node s, t;
185
  Node s1, t1;
182 186

	
183 187
  std::istringstream input(test_lgf);
184 188
  digraphReader(G, input).
185 189
    node("source", s).
186 190
    node("target", t).
191
    node("source1", s1).
192
    node("target1", t1).
187 193
    run();
188 194

	
189 195
  Dfs<Digraph> dfs_test(G);
190 196
  dfs_test.run(s);
191 197

	
192 198
  Path<Digraph> p = dfs_test.path(t);
193 199
  check(p.length() == dfs_test.dist(t),"path() found a wrong path.");
194 200
  check(checkPath(G, p),"path() found a wrong path.");
195 201
  check(pathSource(G, p) == s,"path() found a wrong path.");
196 202
  check(pathTarget(G, p) == t,"path() found a wrong path.");
197 203

	
198 204
  for(NodeIt v(G); v!=INVALID; ++v) {
199 205
    if (dfs_test.reached(v)) {
200 206
      check(v==s || dfs_test.predArc(v)!=INVALID, "Wrong tree.");
201 207
      if (dfs_test.predArc(v)!=INVALID ) {
202 208
        Arc e=dfs_test.predArc(v);
203 209
        Node u=G.source(e);
204 210
        check(u==dfs_test.predNode(v),"Wrong tree.");
205 211
        check(dfs_test.dist(v) - dfs_test.dist(u) == 1,
206 212
              "Wrong distance. (" << dfs_test.dist(u) << "->"
207 213
              << dfs_test.dist(v) << ")");
208 214
      }
209 215
    }
210 216
  }
211 217

	
212 218
  {
219
  Dfs<Digraph> dfs(G);
220
  check(dfs.run(s1,t1) && dfs.reached(t1),"Node 3 is reachable from Node 6.");
221
  }
222
  
223
  {
213 224
    NullMap<Node,Arc> myPredMap;
214 225
    dfs(G).predMap(myPredMap).run(s);
215 226
  }
216 227
}
217 228

	
218 229
int main()
219 230
{
220 231
  checkDfs<ListDigraph>();
221 232
  checkDfs<SmartDigraph>();
222 233
  return 0;
223 234
}
0 comments (0 inline)