1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
19 #include <lemon/concepts/digraph.h>
20 #include <lemon/smart_graph.h>
21 #include <lemon/list_graph.h>
22 #include <lemon/lgf_reader.h>
23 #include <lemon/bfs.h>
24 #include <lemon/path.h>
26 #include "graph_test.h"
27 #include "test_tools.h"
29 using namespace lemon;
53 void checkBfsCompile()
55 typedef concepts::Digraph Digraph;
56 typedef Bfs<Digraph> BType;
57 typedef Digraph::Node Node;
58 typedef Digraph::Arc Arc;
64 ignore_unused_variable_warning(l,i);
69 concepts::ReadMap<Node,bool> nm;
73 const BType& const_bfs_test = bfs_test;
80 bfs_test.addSource(s);
81 n = bfs_test.processNextNode();
82 n = bfs_test.processNextNode(t, b);
83 n = bfs_test.processNextNode(nm, n);
84 n = const_bfs_test.nextNode();
85 b = const_bfs_test.emptyQueue();
86 i = const_bfs_test.queueSize();
92 l = const_bfs_test.dist(t);
93 e = const_bfs_test.predArc(t);
94 s = const_bfs_test.predNode(t);
95 b = const_bfs_test.reached(t);
96 d = const_bfs_test.distMap();
97 p = const_bfs_test.predMap();
98 pp = const_bfs_test.path(t);
102 ::SetPredMap<concepts::ReadWriteMap<Node,Arc> >
103 ::SetDistMap<concepts::ReadWriteMap<Node,int> >
104 ::SetReachedMap<concepts::ReadWriteMap<Node,bool> >
105 ::SetStandardProcessedMap
106 ::SetProcessedMap<concepts::WriteMap<Node,bool> >
107 ::Create bfs_test(G);
109 concepts::ReadWriteMap<Node,Arc> pred_map;
110 concepts::ReadWriteMap<Node,int> dist_map;
111 concepts::ReadWriteMap<Node,bool> reached_map;
112 concepts::WriteMap<Node,bool> processed_map;
117 .reachedMap(reached_map)
118 .processedMap(processed_map);
125 bfs_test.addSource(s);
126 n = bfs_test.processNextNode();
127 n = bfs_test.processNextNode(t, b);
128 n = bfs_test.processNextNode(nm, n);
129 n = bfs_test.nextNode();
130 b = bfs_test.emptyQueue();
131 i = bfs_test.queueSize();
137 l = bfs_test.dist(t);
138 e = bfs_test.predArc(t);
139 s = bfs_test.predNode(t);
140 b = bfs_test.reached(t);
141 pp = bfs_test.path(t);
145 void checkBfsFunctionCompile()
148 typedef concepts::Digraph Digraph;
149 typedef Digraph::Arc Arc;
150 typedef Digraph::Node Node;
154 ignore_unused_variable_warning(b);
157 b=bfs(g).run(Node(),Node());
160 .predMap(concepts::ReadWriteMap<Node,Arc>())
161 .distMap(concepts::ReadWriteMap<Node,VType>())
162 .reachedMap(concepts::ReadWriteMap<Node,bool>())
163 .processedMap(concepts::WriteMap<Node,bool>())
166 .predMap(concepts::ReadWriteMap<Node,Arc>())
167 .distMap(concepts::ReadWriteMap<Node,VType>())
168 .reachedMap(concepts::ReadWriteMap<Node,bool>())
169 .processedMap(concepts::WriteMap<Node,bool>())
170 .path(concepts::Path<Digraph>())
174 .predMap(concepts::ReadWriteMap<Node,Arc>())
175 .distMap(concepts::ReadWriteMap<Node,VType>())
176 .reachedMap(concepts::ReadWriteMap<Node,bool>())
177 .processedMap(concepts::WriteMap<Node,bool>())
181 template <class Digraph>
183 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
188 std::istringstream input(test_lgf);
189 digraphReader(G, input).
194 Bfs<Digraph> bfs_test(G);
197 check(bfs_test.dist(t)==2,"Bfs found a wrong path.");
199 Path<Digraph> p = bfs_test.path(t);
200 check(p.length()==2,"path() found a wrong path.");
201 check(checkPath(G, p),"path() found a wrong path.");
202 check(pathSource(G, p) == s,"path() found a wrong path.");
203 check(pathTarget(G, p) == t,"path() found a wrong path.");
206 for(ArcIt a(G); a!=INVALID; ++a) {
209 check( !bfs_test.reached(u) ||
210 (bfs_test.dist(v) <= bfs_test.dist(u)+1),
211 "Wrong output. " << G.id(u) << "->" << G.id(v));
214 for(NodeIt v(G); v!=INVALID; ++v) {
215 if (bfs_test.reached(v)) {
216 check(v==s || bfs_test.predArc(v)!=INVALID, "Wrong tree.");
217 if (bfs_test.predArc(v)!=INVALID ) {
218 Arc a=bfs_test.predArc(v);
220 check(u==bfs_test.predNode(v),"Wrong tree.");
221 check(bfs_test.dist(v) - bfs_test.dist(u) == 1,
222 "Wrong distance. Difference: "
223 << std::abs(bfs_test.dist(v) - bfs_test.dist(u) - 1));
229 NullMap<Node,Arc> myPredMap;
230 bfs(G).predMap(myPredMap).run(s);
236 checkBfs<ListDigraph>();
237 checkBfs<SmartDigraph>();