| ... | ... |
@@ -513,97 +513,97 @@ |
| 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() && |
|
| 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(); |
| ... | ... |
@@ -1466,97 +1466,97 @@ |
| 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() && |
|
| 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 |
} |
| ... | ... |
@@ -5,184 +5,195 @@ |
| 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)