gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Merge bugfix #392
0 2 0
merge default
2 files changed with 14 insertions and 3 deletions:
↑ Collapse diff ↑
Ignore white space 96 line context
... ...
@@ -520,97 +520,97 @@
520 520
    bool emptyQueue() const { return _stack_head<0; }
521 521

	
522 522
    ///Returns the number of the nodes to be processed.
523 523

	
524 524
    ///Returns the number of the nodes to be processed
525 525
    ///in the queue (stack).
526 526
    int queueSize() const { return _stack_head+1; }
527 527

	
528 528
    ///Executes the algorithm.
529 529

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

	
553 553
    ///Executes the algorithm until the given target node is reached.
554 554

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

	
572 572
    ///Executes the algorithm until a condition is met.
573 573

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

	
598 598
    ///Runs the algorithm from the given source node.
599 599

	
600 600
    ///This method runs the %DFS algorithm from node \c s
601 601
    ///in order to compute the DFS path to each node.
602 602
    ///
603 603
    ///The algorithm computes
604 604
    ///- the %DFS tree,
605 605
    ///- the distance of each node from the root in the %DFS tree.
606 606
    ///
607 607
    ///\note <tt>d.run(s)</tt> is just a shortcut of the following code.
608 608
    ///\code
609 609
    ///  d.init();
610 610
    ///  d.addSource(s);
611 611
    ///  d.start();
612 612
    ///\endcode
613 613
    void run(Node s) {
614 614
      init();
615 615
      addSource(s);
616 616
      start();
... ...
@@ -1467,97 +1467,97 @@
1467 1467
    ///
1468 1468
    /// Returns \c false if there are nodes
1469 1469
    /// to be processed in the queue (stack).
1470 1470
    bool emptyQueue() const { return _stack_head < 0; }
1471 1471

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

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

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

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

	
1544 1544
    /// \brief Runs the algorithm from the given source node.
1545 1545
    ///
1546 1546
    /// This method runs the %DFS algorithm from node \c s.
1547 1547
    /// in order to compute the DFS path to each node.
1548 1548
    ///
1549 1549
    /// The algorithm computes
1550 1550
    /// - the %DFS tree,
1551 1551
    /// - the distance of each node from the root in the %DFS tree.
1552 1552
    ///
1553 1553
    /// \note <tt>d.run(s)</tt> is just a shortcut of the following code.
1554 1554
    ///\code
1555 1555
    ///   d.init();
1556 1556
    ///   d.addSource(s);
1557 1557
    ///   d.start();
1558 1558
    ///\endcode
1559 1559
    void run(Node s) {
1560 1560
      init();
1561 1561
      addSource(s);
1562 1562
      start();
1563 1563
    }
Ignore white space 6 line context
... ...
@@ -5,97 +5,100 @@
5 5
 * Copyright (C) 2003-2010
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> >
... ...
@@ -134,90 +137,98 @@
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)