| 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/bfs.h> |
| 23 | 23 |
#include <lemon/path.h> |
| 24 | 24 |
|
| 25 | 25 |
#include "graph_test.h" |
| 26 | 26 |
#include "test_tools.h" |
| 27 | 27 |
|
| 28 | 28 |
using namespace lemon; |
| 29 | 29 |
|
| 30 | 30 |
void checkBfsCompile() |
| 31 | 31 |
{
|
| 32 | 32 |
typedef concepts::Digraph Digraph; |
| 33 | 33 |
typedef Bfs<Digraph> BType; |
| 34 | 34 |
|
| 35 | 35 |
Digraph G; |
| 36 | 36 |
Digraph::Node n; |
| 37 | 37 |
Digraph::Arc e; |
| 38 | 38 |
int l; |
| 39 | 39 |
bool b; |
| 40 | 40 |
BType::DistMap d(G); |
| 41 | 41 |
BType::PredMap p(G); |
| 42 | 42 |
// BType::PredNodeMap pn(G); |
| 43 | 43 |
|
| 44 | 44 |
BType bfs_test(G); |
| 45 | 45 |
|
| 46 | 46 |
bfs_test.run(n); |
| 47 | 47 |
|
| 48 | 48 |
l = bfs_test.dist(n); |
| 49 | 49 |
e = bfs_test.predArc(n); |
| 50 | 50 |
n = bfs_test.predNode(n); |
| 51 | 51 |
d = bfs_test.distMap(); |
| 52 | 52 |
p = bfs_test.predMap(); |
| 53 | 53 |
// pn = bfs_test.predNodeMap(); |
| 54 | 54 |
b = bfs_test.reached(n); |
| 55 | 55 |
|
| 56 | 56 |
Path<Digraph> pp = bfs_test.path(n); |
| 57 | 57 |
} |
| 58 | 58 |
|
| 59 | 59 |
void checkBfsFunctionCompile() |
| 60 | 60 |
{
|
| 61 | 61 |
typedef int VType; |
| 62 | 62 |
typedef concepts::Digraph Digraph; |
| 63 | 63 |
typedef Digraph::Arc Arc; |
| 64 | 64 |
typedef Digraph::Node Node; |
| 65 | 65 |
|
| 66 | 66 |
Digraph g; |
| 67 | 67 |
bfs(g,Node()).run(); |
| 68 | 68 |
bfs(g).source(Node()).run(); |
| 69 | 69 |
bfs(g) |
| 70 | 70 |
.predMap(concepts::WriteMap<Node,Arc>()) |
| 71 | 71 |
.distMap(concepts::WriteMap<Node,VType>()) |
| 72 | 72 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
| 73 | 73 |
.processedMap(concepts::WriteMap<Node,bool>()) |
| 74 | 74 |
.run(Node()); |
| 75 | 75 |
} |
| 76 | 76 |
|
| 77 | 77 |
template <class Digraph> |
| 78 | 78 |
void checkBfs() {
|
| 79 | 79 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 80 | 80 |
|
| 81 | 81 |
Digraph G; |
| 82 | 82 |
Node s, t; |
| 83 | 83 |
PetStruct<Digraph> ps = addPetersen(G, 5); |
| 84 | 84 |
|
| 85 | 85 |
s=ps.outer[2]; |
| 86 | 86 |
t=ps.inner[0]; |
| 87 | 87 |
|
| 88 | 88 |
Bfs<Digraph> bfs_test(G); |
| 89 | 89 |
bfs_test.run(s); |
| 90 | 90 |
|
| 91 | 91 |
check(bfs_test.dist(t)==3,"Bfs found a wrong path." << bfs_test.dist(t)); |
| 92 | 92 |
|
| 93 | 93 |
Path<Digraph> p = bfs_test.path(t); |
| 94 | 94 |
check(p.length()==3,"path() found a wrong path."); |
| 95 | 95 |
check(checkPath(G, p),"path() found a wrong path."); |
| 96 | 96 |
check(pathSource(G, p) == s,"path() found a wrong path."); |
| 97 | 97 |
check(pathTarget(G, p) == t,"path() found a wrong path."); |
| 98 | 98 |
|
| 99 | 99 |
|
| 100 |
for(ArcIt e(G); e |
|
| 100 |
for(ArcIt e(G); e!=INVALID; ++e) {
|
|
| 101 | 101 |
Node u=G.source(e); |
| 102 | 102 |
Node v=G.target(e); |
| 103 | 103 |
check( !bfs_test.reached(u) || |
| 104 |
(bfs_test.dist(v) |
|
| 104 |
(bfs_test.dist(v) <= bfs_test.dist(u)+1), |
|
| 105 | 105 |
"Wrong output."); |
| 106 | 106 |
} |
| 107 | 107 |
|
| 108 |
for(NodeIt v(G); v |
|
| 108 |
for(NodeIt v(G); v!=INVALID; ++v) {
|
|
| 109 | 109 |
check(bfs_test.reached(v),"Each node should be reached."); |
| 110 | 110 |
if ( bfs_test.predArc(v)!=INVALID ) {
|
| 111 | 111 |
Arc e=bfs_test.predArc(v); |
| 112 | 112 |
Node u=G.source(e); |
| 113 | 113 |
check(u==bfs_test.predNode(v),"Wrong tree."); |
| 114 | 114 |
check(bfs_test.dist(v) - bfs_test.dist(u) == 1, |
| 115 | 115 |
"Wrong distance. Difference: " |
| 116 | 116 |
<< std::abs(bfs_test.dist(v) - bfs_test.dist(u) |
| 117 | 117 |
- 1)); |
| 118 | 118 |
} |
| 119 | 119 |
} |
| 120 | 120 |
} |
| 121 | 121 |
|
| 122 | 122 |
int main() |
| 123 | 123 |
{
|
| 124 | 124 |
checkBfs<ListDigraph>(); |
| 125 | 125 |
checkBfs<SmartDigraph>(); |
| 126 | 126 |
return 0; |
| 127 | 127 |
} |
0 comments (0 inline)