| [209] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- | 
|---|
| [100] | 2 | * | 
|---|
| [209] | 3 | * This file is a part of LEMON, a generic C++ optimization library. | 
|---|
| [100] | 4 | * | 
|---|
| [440] | 5 | * Copyright (C) 2003-2009 | 
|---|
| [100] | 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport | 
|---|
|  | 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). | 
|---|
|  | 8 | * | 
|---|
|  | 9 | * Permission to use, modify and distribute this software is granted | 
|---|
|  | 10 | * provided that this copyright notice appears in all copies. For | 
|---|
|  | 11 | * precise terms see the accompanying LICENSE file. | 
|---|
|  | 12 | * | 
|---|
|  | 13 | * This software is provided "AS IS" with no warranty of any kind, | 
|---|
|  | 14 | * express or implied, and with no claim as to its suitability for any | 
|---|
|  | 15 | * purpose. | 
|---|
|  | 16 | * | 
|---|
|  | 17 | */ | 
|---|
|  | 18 |  | 
|---|
| [171] | 19 | #include <lemon/concepts/digraph.h> | 
|---|
|  | 20 | #include <lemon/smart_graph.h> | 
|---|
| [100] | 21 | #include <lemon/list_graph.h> | 
|---|
| [228] | 22 | #include <lemon/lgf_reader.h> | 
|---|
| [100] | 23 | #include <lemon/bfs.h> | 
|---|
|  | 24 | #include <lemon/path.h> | 
|---|
| [171] | 25 |  | 
|---|
|  | 26 | #include "graph_test.h" | 
|---|
|  | 27 | #include "test_tools.h" | 
|---|
| [100] | 28 |  | 
|---|
|  | 29 | using namespace lemon; | 
|---|
|  | 30 |  | 
|---|
| [228] | 31 | char test_lgf[] = | 
|---|
|  | 32 | "@nodes\n" | 
|---|
|  | 33 | "label\n" | 
|---|
|  | 34 | "0\n" | 
|---|
|  | 35 | "1\n" | 
|---|
|  | 36 | "2\n" | 
|---|
|  | 37 | "3\n" | 
|---|
|  | 38 | "4\n" | 
|---|
|  | 39 | "5\n" | 
|---|
|  | 40 | "@arcs\n" | 
|---|
|  | 41 | "     label\n" | 
|---|
|  | 42 | "0 1  0\n" | 
|---|
|  | 43 | "1 2  1\n" | 
|---|
|  | 44 | "2 3  2\n" | 
|---|
|  | 45 | "3 4  3\n" | 
|---|
|  | 46 | "0 3  4\n" | 
|---|
|  | 47 | "0 3  5\n" | 
|---|
|  | 48 | "5 2  6\n" | 
|---|
|  | 49 | "@attributes\n" | 
|---|
|  | 50 | "source 0\n" | 
|---|
|  | 51 | "target 4\n"; | 
|---|
|  | 52 |  | 
|---|
| [209] | 53 | void checkBfsCompile() | 
|---|
| [100] | 54 | { | 
|---|
|  | 55 | typedef concepts::Digraph Digraph; | 
|---|
|  | 56 | typedef Bfs<Digraph> BType; | 
|---|
| [286] | 57 | typedef Digraph::Node Node; | 
|---|
|  | 58 | typedef Digraph::Arc Arc; | 
|---|
| [209] | 59 |  | 
|---|
| [100] | 60 | Digraph G; | 
|---|
| [585] | 61 | Node s, t, n; | 
|---|
| [286] | 62 | Arc e; | 
|---|
| [585] | 63 | int l, i; | 
|---|
| [100] | 64 | bool b; | 
|---|
|  | 65 | BType::DistMap d(G); | 
|---|
|  | 66 | BType::PredMap p(G); | 
|---|
| [286] | 67 | Path<Digraph> pp; | 
|---|
| [585] | 68 | concepts::ReadMap<Node,bool> nm; | 
|---|
| [209] | 69 |  | 
|---|
| [286] | 70 | { | 
|---|
|  | 71 | BType bfs_test(G); | 
|---|
| [585] | 72 | const BType& const_bfs_test = bfs_test; | 
|---|
| [209] | 73 |  | 
|---|
| [286] | 74 | bfs_test.run(s); | 
|---|
|  | 75 | bfs_test.run(s,t); | 
|---|
|  | 76 | bfs_test.run(); | 
|---|
| [209] | 77 |  | 
|---|
| [585] | 78 | bfs_test.init(); | 
|---|
|  | 79 | bfs_test.addSource(s); | 
|---|
|  | 80 | n = bfs_test.processNextNode(); | 
|---|
|  | 81 | n = bfs_test.processNextNode(t, b); | 
|---|
|  | 82 | n = bfs_test.processNextNode(nm, n); | 
|---|
|  | 83 | n = const_bfs_test.nextNode(); | 
|---|
|  | 84 | b = const_bfs_test.emptyQueue(); | 
|---|
|  | 85 | i = const_bfs_test.queueSize(); | 
|---|
|  | 86 |  | 
|---|
|  | 87 | bfs_test.start(); | 
|---|
|  | 88 | bfs_test.start(t); | 
|---|
|  | 89 | bfs_test.start(nm); | 
|---|
|  | 90 |  | 
|---|
|  | 91 | l  = const_bfs_test.dist(t); | 
|---|
|  | 92 | e  = const_bfs_test.predArc(t); | 
|---|
|  | 93 | s  = const_bfs_test.predNode(t); | 
|---|
|  | 94 | b  = const_bfs_test.reached(t); | 
|---|
|  | 95 | d  = const_bfs_test.distMap(); | 
|---|
|  | 96 | p  = const_bfs_test.predMap(); | 
|---|
|  | 97 | pp = const_bfs_test.path(t); | 
|---|
| [286] | 98 | } | 
|---|
|  | 99 | { | 
|---|
|  | 100 | BType | 
|---|
|  | 101 | ::SetPredMap<concepts::ReadWriteMap<Node,Arc> > | 
|---|
|  | 102 | ::SetDistMap<concepts::ReadWriteMap<Node,int> > | 
|---|
|  | 103 | ::SetReachedMap<concepts::ReadWriteMap<Node,bool> > | 
|---|
| [585] | 104 | ::SetStandardProcessedMap | 
|---|
| [286] | 105 | ::SetProcessedMap<concepts::WriteMap<Node,bool> > | 
|---|
|  | 106 | ::Create bfs_test(G); | 
|---|
| [585] | 107 |  | 
|---|
|  | 108 | concepts::ReadWriteMap<Node,Arc> pred_map; | 
|---|
|  | 109 | concepts::ReadWriteMap<Node,int> dist_map; | 
|---|
|  | 110 | concepts::ReadWriteMap<Node,bool> reached_map; | 
|---|
|  | 111 | concepts::WriteMap<Node,bool> processed_map; | 
|---|
|  | 112 |  | 
|---|
|  | 113 | bfs_test | 
|---|
|  | 114 | .predMap(pred_map) | 
|---|
|  | 115 | .distMap(dist_map) | 
|---|
|  | 116 | .reachedMap(reached_map) | 
|---|
|  | 117 | .processedMap(processed_map); | 
|---|
| [100] | 118 |  | 
|---|
| [286] | 119 | bfs_test.run(s); | 
|---|
|  | 120 | bfs_test.run(s,t); | 
|---|
|  | 121 | bfs_test.run(); | 
|---|
| [585] | 122 |  | 
|---|
|  | 123 | bfs_test.init(); | 
|---|
|  | 124 | bfs_test.addSource(s); | 
|---|
|  | 125 | n = bfs_test.processNextNode(); | 
|---|
|  | 126 | n = bfs_test.processNextNode(t, b); | 
|---|
|  | 127 | n = bfs_test.processNextNode(nm, n); | 
|---|
|  | 128 | n = bfs_test.nextNode(); | 
|---|
|  | 129 | b = bfs_test.emptyQueue(); | 
|---|
|  | 130 | i = bfs_test.queueSize(); | 
|---|
|  | 131 |  | 
|---|
|  | 132 | bfs_test.start(); | 
|---|
|  | 133 | bfs_test.start(t); | 
|---|
|  | 134 | bfs_test.start(nm); | 
|---|
| [286] | 135 |  | 
|---|
|  | 136 | l  = bfs_test.dist(t); | 
|---|
|  | 137 | e  = bfs_test.predArc(t); | 
|---|
|  | 138 | s  = bfs_test.predNode(t); | 
|---|
|  | 139 | b  = bfs_test.reached(t); | 
|---|
|  | 140 | pp = bfs_test.path(t); | 
|---|
|  | 141 | } | 
|---|
| [100] | 142 | } | 
|---|
|  | 143 |  | 
|---|
| [209] | 144 | void checkBfsFunctionCompile() | 
|---|
| [100] | 145 | { | 
|---|
|  | 146 | typedef int VType; | 
|---|
|  | 147 | typedef concepts::Digraph Digraph; | 
|---|
|  | 148 | typedef Digraph::Arc Arc; | 
|---|
|  | 149 | typedef Digraph::Node Node; | 
|---|
| [209] | 150 |  | 
|---|
| [100] | 151 | Digraph g; | 
|---|
| [278] | 152 | bool b; | 
|---|
|  | 153 | bfs(g).run(Node()); | 
|---|
|  | 154 | b=bfs(g).run(Node(),Node()); | 
|---|
|  | 155 | bfs(g).run(); | 
|---|
| [100] | 156 | bfs(g) | 
|---|
| [278] | 157 | .predMap(concepts::ReadWriteMap<Node,Arc>()) | 
|---|
|  | 158 | .distMap(concepts::ReadWriteMap<Node,VType>()) | 
|---|
| [100] | 159 | .reachedMap(concepts::ReadWriteMap<Node,bool>()) | 
|---|
|  | 160 | .processedMap(concepts::WriteMap<Node,bool>()) | 
|---|
|  | 161 | .run(Node()); | 
|---|
| [278] | 162 | b=bfs(g) | 
|---|
|  | 163 | .predMap(concepts::ReadWriteMap<Node,Arc>()) | 
|---|
|  | 164 | .distMap(concepts::ReadWriteMap<Node,VType>()) | 
|---|
|  | 165 | .reachedMap(concepts::ReadWriteMap<Node,bool>()) | 
|---|
|  | 166 | .processedMap(concepts::WriteMap<Node,bool>()) | 
|---|
|  | 167 | .path(concepts::Path<Digraph>()) | 
|---|
|  | 168 | .dist(VType()) | 
|---|
|  | 169 | .run(Node(),Node()); | 
|---|
|  | 170 | bfs(g) | 
|---|
|  | 171 | .predMap(concepts::ReadWriteMap<Node,Arc>()) | 
|---|
|  | 172 | .distMap(concepts::ReadWriteMap<Node,VType>()) | 
|---|
|  | 173 | .reachedMap(concepts::ReadWriteMap<Node,bool>()) | 
|---|
|  | 174 | .processedMap(concepts::WriteMap<Node,bool>()) | 
|---|
|  | 175 | .run(); | 
|---|
| [100] | 176 | } | 
|---|
|  | 177 |  | 
|---|
| [171] | 178 | template <class Digraph> | 
|---|
|  | 179 | void checkBfs() { | 
|---|
|  | 180 | TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); | 
|---|
| [100] | 181 |  | 
|---|
|  | 182 | Digraph G; | 
|---|
|  | 183 | Node s, t; | 
|---|
| [209] | 184 |  | 
|---|
| [228] | 185 | std::istringstream input(test_lgf); | 
|---|
| [293] | 186 | digraphReader(G, input). | 
|---|
| [228] | 187 | node("source", s). | 
|---|
|  | 188 | node("target", t). | 
|---|
|  | 189 | run(); | 
|---|
| [209] | 190 |  | 
|---|
| [100] | 191 | Bfs<Digraph> bfs_test(G); | 
|---|
|  | 192 | bfs_test.run(s); | 
|---|
| [209] | 193 |  | 
|---|
| [278] | 194 | check(bfs_test.dist(t)==2,"Bfs found a wrong path."); | 
|---|
| [100] | 195 |  | 
|---|
|  | 196 | Path<Digraph> p = bfs_test.path(t); | 
|---|
| [228] | 197 | check(p.length()==2,"path() found a wrong path."); | 
|---|
| [100] | 198 | check(checkPath(G, p),"path() found a wrong path."); | 
|---|
|  | 199 | check(pathSource(G, p) == s,"path() found a wrong path."); | 
|---|
|  | 200 | check(pathTarget(G, p) == t,"path() found a wrong path."); | 
|---|
| [209] | 201 |  | 
|---|
| [100] | 202 |  | 
|---|
| [228] | 203 | for(ArcIt a(G); a!=INVALID; ++a) { | 
|---|
|  | 204 | Node u=G.source(a); | 
|---|
|  | 205 | Node v=G.target(a); | 
|---|
| [100] | 206 | check( !bfs_test.reached(u) || | 
|---|
| [222] | 207 | (bfs_test.dist(v) <= bfs_test.dist(u)+1), | 
|---|
| [278] | 208 | "Wrong output. " << G.id(u) << "->" << G.id(v)); | 
|---|
| [100] | 209 | } | 
|---|
|  | 210 |  | 
|---|
| [222] | 211 | for(NodeIt v(G); v!=INVALID; ++v) { | 
|---|
| [228] | 212 | if (bfs_test.reached(v)) { | 
|---|
|  | 213 | check(v==s || bfs_test.predArc(v)!=INVALID, "Wrong tree."); | 
|---|
|  | 214 | if (bfs_test.predArc(v)!=INVALID ) { | 
|---|
|  | 215 | Arc a=bfs_test.predArc(v); | 
|---|
|  | 216 | Node u=G.source(a); | 
|---|
|  | 217 | check(u==bfs_test.predNode(v),"Wrong tree."); | 
|---|
|  | 218 | check(bfs_test.dist(v) - bfs_test.dist(u) == 1, | 
|---|
|  | 219 | "Wrong distance. Difference: " | 
|---|
| [278] | 220 | << std::abs(bfs_test.dist(v) - bfs_test.dist(u) - 1)); | 
|---|
| [228] | 221 | } | 
|---|
| [100] | 222 | } | 
|---|
|  | 223 | } | 
|---|
| [278] | 224 |  | 
|---|
|  | 225 | { | 
|---|
|  | 226 | NullMap<Node,Arc> myPredMap; | 
|---|
|  | 227 | bfs(G).predMap(myPredMap).run(s); | 
|---|
|  | 228 | } | 
|---|
| [100] | 229 | } | 
|---|
|  | 230 |  | 
|---|
| [171] | 231 | int main() | 
|---|
|  | 232 | { | 
|---|
|  | 233 | checkBfs<ListDigraph>(); | 
|---|
|  | 234 | checkBfs<SmartDigraph>(); | 
|---|
|  | 235 | return 0; | 
|---|
|  | 236 | } | 
|---|