alpar@209
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
alpar@100
|
2 |
*
|
alpar@209
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
alpar@100
|
4 |
*
|
alpar@100
|
5 |
* Copyright (C) 2003-2008
|
alpar@100
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@100
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@100
|
8 |
*
|
alpar@100
|
9 |
* Permission to use, modify and distribute this software is granted
|
alpar@100
|
10 |
* provided that this copyright notice appears in all copies. For
|
alpar@100
|
11 |
* precise terms see the accompanying LICENSE file.
|
alpar@100
|
12 |
*
|
alpar@100
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@100
|
14 |
* express or implied, and with no claim as to its suitability for any
|
alpar@100
|
15 |
* purpose.
|
alpar@100
|
16 |
*
|
alpar@100
|
17 |
*/
|
alpar@100
|
18 |
|
alpar@100
|
19 |
#ifndef LEMON_BFS_H
|
alpar@100
|
20 |
#define LEMON_BFS_H
|
alpar@100
|
21 |
|
alpar@100
|
22 |
///\ingroup search
|
alpar@100
|
23 |
///\file
|
kpeter@244
|
24 |
///\brief BFS algorithm.
|
alpar@100
|
25 |
|
alpar@100
|
26 |
#include <lemon/list_graph.h>
|
alpar@100
|
27 |
#include <lemon/bits/path_dump.h>
|
deba@220
|
28 |
#include <lemon/core.h>
|
alpar@100
|
29 |
#include <lemon/error.h>
|
alpar@100
|
30 |
#include <lemon/maps.h>
|
kpeter@278
|
31 |
#include <lemon/path.h>
|
alpar@100
|
32 |
|
alpar@100
|
33 |
namespace lemon {
|
alpar@100
|
34 |
|
alpar@100
|
35 |
///Default traits class of Bfs class.
|
alpar@100
|
36 |
|
alpar@100
|
37 |
///Default traits class of Bfs class.
|
kpeter@157
|
38 |
///\tparam GR Digraph type.
|
alpar@100
|
39 |
template<class GR>
|
alpar@100
|
40 |
struct BfsDefaultTraits
|
alpar@100
|
41 |
{
|
kpeter@244
|
42 |
///The type of the digraph the algorithm runs on.
|
alpar@100
|
43 |
typedef GR Digraph;
|
kpeter@244
|
44 |
|
kpeter@244
|
45 |
///\brief The type of the map that stores the predecessor
|
alpar@100
|
46 |
///arcs of the shortest paths.
|
alpar@209
|
47 |
///
|
kpeter@244
|
48 |
///The type of the map that stores the predecessor
|
alpar@100
|
49 |
///arcs of the shortest paths.
|
alpar@100
|
50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept.
|
kpeter@244
|
51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
|
kpeter@244
|
52 |
///Instantiates a \ref PredMap.
|
alpar@209
|
53 |
|
alpar@209
|
54 |
///This function instantiates a \ref PredMap.
|
kpeter@244
|
55 |
///\param g is the digraph, to which we would like to define the
|
kpeter@244
|
56 |
///\ref PredMap.
|
alpar@100
|
57 |
///\todo The digraph alone may be insufficient to initialize
|
kpeter@244
|
58 |
static PredMap *createPredMap(const Digraph &g)
|
alpar@100
|
59 |
{
|
kpeter@244
|
60 |
return new PredMap(g);
|
alpar@100
|
61 |
}
|
kpeter@244
|
62 |
|
alpar@100
|
63 |
///The type of the map that indicates which nodes are processed.
|
alpar@209
|
64 |
|
alpar@100
|
65 |
///The type of the map that indicates which nodes are processed.
|
alpar@100
|
66 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept.
|
kpeter@244
|
67 |
///By default it is a NullMap.
|
alpar@100
|
68 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
|
kpeter@244
|
69 |
///Instantiates a \ref ProcessedMap.
|
alpar@209
|
70 |
|
alpar@209
|
71 |
///This function instantiates a \ref ProcessedMap.
|
alpar@100
|
72 |
///\param g is the digraph, to which
|
alpar@100
|
73 |
///we would like to define the \ref ProcessedMap
|
alpar@100
|
74 |
#ifdef DOXYGEN
|
kpeter@244
|
75 |
static ProcessedMap *createProcessedMap(const Digraph &g)
|
alpar@100
|
76 |
#else
|
kpeter@244
|
77 |
static ProcessedMap *createProcessedMap(const Digraph &)
|
alpar@100
|
78 |
#endif
|
alpar@100
|
79 |
{
|
alpar@100
|
80 |
return new ProcessedMap();
|
alpar@100
|
81 |
}
|
kpeter@244
|
82 |
|
alpar@100
|
83 |
///The type of the map that indicates which nodes are reached.
|
alpar@209
|
84 |
|
alpar@100
|
85 |
///The type of the map that indicates which nodes are reached.
|
kpeter@244
|
86 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
|
alpar@100
|
87 |
typedef typename Digraph::template NodeMap<bool> ReachedMap;
|
kpeter@244
|
88 |
///Instantiates a \ref ReachedMap.
|
alpar@209
|
89 |
|
alpar@209
|
90 |
///This function instantiates a \ref ReachedMap.
|
kpeter@244
|
91 |
///\param g is the digraph, to which
|
alpar@100
|
92 |
///we would like to define the \ref ReachedMap.
|
kpeter@244
|
93 |
static ReachedMap *createReachedMap(const Digraph &g)
|
alpar@100
|
94 |
{
|
kpeter@244
|
95 |
return new ReachedMap(g);
|
alpar@100
|
96 |
}
|
alpar@209
|
97 |
|
kpeter@244
|
98 |
///The type of the map that stores the distances of the nodes.
|
kpeter@244
|
99 |
|
kpeter@244
|
100 |
///The type of the map that stores the distances of the nodes.
|
alpar@100
|
101 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept.
|
alpar@100
|
102 |
typedef typename Digraph::template NodeMap<int> DistMap;
|
kpeter@244
|
103 |
///Instantiates a \ref DistMap.
|
alpar@209
|
104 |
|
alpar@209
|
105 |
///This function instantiates a \ref DistMap.
|
kpeter@244
|
106 |
///\param g is the digraph, to which we would like to define the
|
kpeter@244
|
107 |
///\ref DistMap.
|
kpeter@244
|
108 |
static DistMap *createDistMap(const Digraph &g)
|
alpar@100
|
109 |
{
|
kpeter@244
|
110 |
return new DistMap(g);
|
alpar@100
|
111 |
}
|
alpar@100
|
112 |
};
|
alpar@209
|
113 |
|
alpar@100
|
114 |
///%BFS algorithm class.
|
alpar@209
|
115 |
|
alpar@100
|
116 |
///\ingroup search
|
alpar@100
|
117 |
///This class provides an efficient implementation of the %BFS algorithm.
|
alpar@100
|
118 |
///
|
kpeter@278
|
119 |
///There is also a \ref bfs() "function-type interface" for the BFS
|
kpeter@244
|
120 |
///algorithm, which is convenient in the simplier cases and it can be
|
kpeter@244
|
121 |
///used easier.
|
kpeter@244
|
122 |
///
|
kpeter@244
|
123 |
///\tparam GR The type of the digraph the algorithm runs on.
|
kpeter@244
|
124 |
///The default value is \ref ListDigraph. The value of GR is not used
|
kpeter@244
|
125 |
///directly by \ref Bfs, it is only passed to \ref BfsDefaultTraits.
|
kpeter@157
|
126 |
///\tparam TR Traits class to set various data types used by the algorithm.
|
alpar@100
|
127 |
///The default traits class is
|
alpar@100
|
128 |
///\ref BfsDefaultTraits "BfsDefaultTraits<GR>".
|
alpar@100
|
129 |
///See \ref BfsDefaultTraits for the documentation of
|
alpar@100
|
130 |
///a Bfs traits class.
|
alpar@100
|
131 |
#ifdef DOXYGEN
|
alpar@100
|
132 |
template <typename GR,
|
alpar@209
|
133 |
typename TR>
|
alpar@100
|
134 |
#else
|
alpar@100
|
135 |
template <typename GR=ListDigraph,
|
alpar@209
|
136 |
typename TR=BfsDefaultTraits<GR> >
|
alpar@100
|
137 |
#endif
|
alpar@100
|
138 |
class Bfs {
|
alpar@100
|
139 |
public:
|
kpeter@244
|
140 |
///\ref Exception for uninitialized parameters.
|
kpeter@244
|
141 |
|
kpeter@244
|
142 |
///This error represents problems in the initialization of the
|
kpeter@244
|
143 |
///parameters of the algorithm.
|
alpar@100
|
144 |
class UninitializedParameter : public lemon::UninitializedParameter {
|
alpar@100
|
145 |
public:
|
alpar@100
|
146 |
virtual const char* what() const throw() {
|
alpar@209
|
147 |
return "lemon::Bfs::UninitializedParameter";
|
alpar@100
|
148 |
}
|
alpar@100
|
149 |
};
|
alpar@100
|
150 |
|
kpeter@244
|
151 |
///The type of the digraph the algorithm runs on.
|
alpar@100
|
152 |
typedef typename TR::Digraph Digraph;
|
alpar@209
|
153 |
|
kpeter@244
|
154 |
///\brief The type of the map that stores the predecessor arcs of the
|
kpeter@244
|
155 |
///shortest paths.
|
alpar@100
|
156 |
typedef typename TR::PredMap PredMap;
|
kpeter@244
|
157 |
///The type of the map that stores the distances of the nodes.
|
kpeter@244
|
158 |
typedef typename TR::DistMap DistMap;
|
kpeter@244
|
159 |
///The type of the map that indicates which nodes are reached.
|
alpar@100
|
160 |
typedef typename TR::ReachedMap ReachedMap;
|
kpeter@244
|
161 |
///The type of the map that indicates which nodes are processed.
|
alpar@100
|
162 |
typedef typename TR::ProcessedMap ProcessedMap;
|
kpeter@244
|
163 |
///The type of the paths.
|
kpeter@244
|
164 |
typedef PredMapPath<Digraph, PredMap> Path;
|
kpeter@244
|
165 |
|
kpeter@244
|
166 |
///The traits class.
|
kpeter@244
|
167 |
typedef TR Traits;
|
kpeter@244
|
168 |
|
alpar@100
|
169 |
private:
|
alpar@100
|
170 |
|
alpar@100
|
171 |
typedef typename Digraph::Node Node;
|
alpar@100
|
172 |
typedef typename Digraph::NodeIt NodeIt;
|
alpar@100
|
173 |
typedef typename Digraph::Arc Arc;
|
alpar@100
|
174 |
typedef typename Digraph::OutArcIt OutArcIt;
|
alpar@100
|
175 |
|
kpeter@244
|
176 |
//Pointer to the underlying digraph.
|
alpar@100
|
177 |
const Digraph *G;
|
kpeter@244
|
178 |
//Pointer to the map of predecessor arcs.
|
alpar@100
|
179 |
PredMap *_pred;
|
kpeter@244
|
180 |
//Indicates if _pred is locally allocated (true) or not.
|
alpar@100
|
181 |
bool local_pred;
|
kpeter@244
|
182 |
//Pointer to the map of distances.
|
alpar@100
|
183 |
DistMap *_dist;
|
kpeter@244
|
184 |
//Indicates if _dist is locally allocated (true) or not.
|
alpar@100
|
185 |
bool local_dist;
|
kpeter@244
|
186 |
//Pointer to the map of reached status of the nodes.
|
alpar@100
|
187 |
ReachedMap *_reached;
|
kpeter@244
|
188 |
//Indicates if _reached is locally allocated (true) or not.
|
alpar@100
|
189 |
bool local_reached;
|
kpeter@244
|
190 |
//Pointer to the map of processed status of the nodes.
|
alpar@100
|
191 |
ProcessedMap *_processed;
|
kpeter@244
|
192 |
//Indicates if _processed is locally allocated (true) or not.
|
alpar@100
|
193 |
bool local_processed;
|
alpar@100
|
194 |
|
alpar@100
|
195 |
std::vector<typename Digraph::Node> _queue;
|
alpar@100
|
196 |
int _queue_head,_queue_tail,_queue_next_dist;
|
alpar@100
|
197 |
int _curr_dist;
|
alpar@100
|
198 |
|
alpar@100
|
199 |
///Creates the maps if necessary.
|
alpar@100
|
200 |
///\todo Better memory allocation (instead of new).
|
alpar@209
|
201 |
void create_maps()
|
alpar@100
|
202 |
{
|
alpar@100
|
203 |
if(!_pred) {
|
alpar@209
|
204 |
local_pred = true;
|
alpar@209
|
205 |
_pred = Traits::createPredMap(*G);
|
alpar@100
|
206 |
}
|
alpar@100
|
207 |
if(!_dist) {
|
alpar@209
|
208 |
local_dist = true;
|
alpar@209
|
209 |
_dist = Traits::createDistMap(*G);
|
alpar@100
|
210 |
}
|
alpar@100
|
211 |
if(!_reached) {
|
alpar@209
|
212 |
local_reached = true;
|
alpar@209
|
213 |
_reached = Traits::createReachedMap(*G);
|
alpar@100
|
214 |
}
|
alpar@100
|
215 |
if(!_processed) {
|
alpar@209
|
216 |
local_processed = true;
|
alpar@209
|
217 |
_processed = Traits::createProcessedMap(*G);
|
alpar@100
|
218 |
}
|
alpar@100
|
219 |
}
|
alpar@100
|
220 |
|
alpar@100
|
221 |
protected:
|
alpar@209
|
222 |
|
alpar@100
|
223 |
Bfs() {}
|
alpar@209
|
224 |
|
alpar@100
|
225 |
public:
|
alpar@209
|
226 |
|
alpar@100
|
227 |
typedef Bfs Create;
|
alpar@100
|
228 |
|
alpar@100
|
229 |
///\name Named template parameters
|
alpar@100
|
230 |
|
alpar@100
|
231 |
///@{
|
alpar@100
|
232 |
|
alpar@100
|
233 |
template <class T>
|
kpeter@257
|
234 |
struct SetPredMapTraits : public Traits {
|
alpar@100
|
235 |
typedef T PredMap;
|
alpar@209
|
236 |
static PredMap *createPredMap(const Digraph &)
|
alpar@100
|
237 |
{
|
alpar@209
|
238 |
throw UninitializedParameter();
|
alpar@100
|
239 |
}
|
alpar@100
|
240 |
};
|
alpar@100
|
241 |
///\brief \ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
242 |
///\ref PredMap type.
|
alpar@100
|
243 |
///
|
kpeter@244
|
244 |
///\ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
245 |
///\ref PredMap type.
|
alpar@100
|
246 |
template <class T>
|
kpeter@257
|
247 |
struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > {
|
kpeter@257
|
248 |
typedef Bfs< Digraph, SetPredMapTraits<T> > Create;
|
alpar@100
|
249 |
};
|
alpar@209
|
250 |
|
alpar@100
|
251 |
template <class T>
|
kpeter@257
|
252 |
struct SetDistMapTraits : public Traits {
|
alpar@100
|
253 |
typedef T DistMap;
|
alpar@209
|
254 |
static DistMap *createDistMap(const Digraph &)
|
alpar@100
|
255 |
{
|
alpar@209
|
256 |
throw UninitializedParameter();
|
alpar@100
|
257 |
}
|
alpar@100
|
258 |
};
|
alpar@100
|
259 |
///\brief \ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
260 |
///\ref DistMap type.
|
alpar@100
|
261 |
///
|
kpeter@244
|
262 |
///\ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
263 |
///\ref DistMap type.
|
alpar@100
|
264 |
template <class T>
|
kpeter@257
|
265 |
struct SetDistMap : public Bfs< Digraph, SetDistMapTraits<T> > {
|
kpeter@257
|
266 |
typedef Bfs< Digraph, SetDistMapTraits<T> > Create;
|
alpar@100
|
267 |
};
|
alpar@209
|
268 |
|
alpar@100
|
269 |
template <class T>
|
kpeter@257
|
270 |
struct SetReachedMapTraits : public Traits {
|
alpar@100
|
271 |
typedef T ReachedMap;
|
alpar@209
|
272 |
static ReachedMap *createReachedMap(const Digraph &)
|
alpar@100
|
273 |
{
|
alpar@209
|
274 |
throw UninitializedParameter();
|
alpar@100
|
275 |
}
|
alpar@100
|
276 |
};
|
alpar@100
|
277 |
///\brief \ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
278 |
///\ref ReachedMap type.
|
alpar@100
|
279 |
///
|
kpeter@244
|
280 |
///\ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
281 |
///\ref ReachedMap type.
|
alpar@100
|
282 |
template <class T>
|
kpeter@257
|
283 |
struct SetReachedMap : public Bfs< Digraph, SetReachedMapTraits<T> > {
|
kpeter@257
|
284 |
typedef Bfs< Digraph, SetReachedMapTraits<T> > Create;
|
alpar@100
|
285 |
};
|
alpar@209
|
286 |
|
alpar@100
|
287 |
template <class T>
|
kpeter@257
|
288 |
struct SetProcessedMapTraits : public Traits {
|
alpar@100
|
289 |
typedef T ProcessedMap;
|
alpar@209
|
290 |
static ProcessedMap *createProcessedMap(const Digraph &)
|
alpar@100
|
291 |
{
|
alpar@209
|
292 |
throw UninitializedParameter();
|
alpar@100
|
293 |
}
|
alpar@100
|
294 |
};
|
alpar@100
|
295 |
///\brief \ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
296 |
///\ref ProcessedMap type.
|
alpar@100
|
297 |
///
|
kpeter@244
|
298 |
///\ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
299 |
///\ref ProcessedMap type.
|
alpar@100
|
300 |
template <class T>
|
kpeter@257
|
301 |
struct SetProcessedMap : public Bfs< Digraph, SetProcessedMapTraits<T> > {
|
kpeter@257
|
302 |
typedef Bfs< Digraph, SetProcessedMapTraits<T> > Create;
|
alpar@100
|
303 |
};
|
alpar@209
|
304 |
|
kpeter@257
|
305 |
struct SetStandardProcessedMapTraits : public Traits {
|
alpar@100
|
306 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap;
|
kpeter@244
|
307 |
static ProcessedMap *createProcessedMap(const Digraph &g)
|
alpar@100
|
308 |
{
|
kpeter@244
|
309 |
return new ProcessedMap(g);
|
alpar@100
|
310 |
}
|
alpar@100
|
311 |
};
|
kpeter@244
|
312 |
///\brief \ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
313 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
|
alpar@100
|
314 |
///
|
kpeter@244
|
315 |
///\ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
316 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
|
alpar@100
|
317 |
///If you don't set it explicitly, it will be automatically allocated.
|
kpeter@257
|
318 |
struct SetStandardProcessedMap :
|
kpeter@257
|
319 |
public Bfs< Digraph, SetStandardProcessedMapTraits > {
|
kpeter@257
|
320 |
typedef Bfs< Digraph, SetStandardProcessedMapTraits > Create;
|
alpar@100
|
321 |
};
|
alpar@209
|
322 |
|
alpar@100
|
323 |
///@}
|
alpar@100
|
324 |
|
alpar@209
|
325 |
public:
|
alpar@209
|
326 |
|
alpar@100
|
327 |
///Constructor.
|
alpar@209
|
328 |
|
kpeter@244
|
329 |
///Constructor.
|
kpeter@244
|
330 |
///\param g The digraph the algorithm runs on.
|
kpeter@244
|
331 |
Bfs(const Digraph &g) :
|
kpeter@244
|
332 |
G(&g),
|
alpar@100
|
333 |
_pred(NULL), local_pred(false),
|
alpar@100
|
334 |
_dist(NULL), local_dist(false),
|
alpar@100
|
335 |
_reached(NULL), local_reached(false),
|
alpar@100
|
336 |
_processed(NULL), local_processed(false)
|
alpar@100
|
337 |
{ }
|
alpar@209
|
338 |
|
alpar@100
|
339 |
///Destructor.
|
alpar@209
|
340 |
~Bfs()
|
alpar@100
|
341 |
{
|
alpar@100
|
342 |
if(local_pred) delete _pred;
|
alpar@100
|
343 |
if(local_dist) delete _dist;
|
alpar@100
|
344 |
if(local_reached) delete _reached;
|
alpar@100
|
345 |
if(local_processed) delete _processed;
|
alpar@100
|
346 |
}
|
alpar@100
|
347 |
|
kpeter@244
|
348 |
///Sets the map that stores the predecessor arcs.
|
alpar@100
|
349 |
|
kpeter@244
|
350 |
///Sets the map that stores the predecessor arcs.
|
alpar@100
|
351 |
///If you don't use this function before calling \ref run(),
|
alpar@100
|
352 |
///it will allocate one. The destructor deallocates this
|
alpar@100
|
353 |
///automatically allocated map, of course.
|
alpar@100
|
354 |
///\return <tt> (*this) </tt>
|
alpar@209
|
355 |
Bfs &predMap(PredMap &m)
|
alpar@100
|
356 |
{
|
alpar@100
|
357 |
if(local_pred) {
|
alpar@209
|
358 |
delete _pred;
|
alpar@209
|
359 |
local_pred=false;
|
alpar@100
|
360 |
}
|
alpar@100
|
361 |
_pred = &m;
|
alpar@100
|
362 |
return *this;
|
alpar@100
|
363 |
}
|
alpar@100
|
364 |
|
kpeter@244
|
365 |
///Sets the map that indicates which nodes are reached.
|
alpar@100
|
366 |
|
kpeter@244
|
367 |
///Sets the map that indicates which nodes are reached.
|
alpar@100
|
368 |
///If you don't use this function before calling \ref run(),
|
alpar@100
|
369 |
///it will allocate one. The destructor deallocates this
|
alpar@100
|
370 |
///automatically allocated map, of course.
|
alpar@100
|
371 |
///\return <tt> (*this) </tt>
|
alpar@209
|
372 |
Bfs &reachedMap(ReachedMap &m)
|
alpar@100
|
373 |
{
|
alpar@100
|
374 |
if(local_reached) {
|
alpar@209
|
375 |
delete _reached;
|
alpar@209
|
376 |
local_reached=false;
|
alpar@100
|
377 |
}
|
alpar@100
|
378 |
_reached = &m;
|
alpar@100
|
379 |
return *this;
|
alpar@100
|
380 |
}
|
alpar@100
|
381 |
|
kpeter@244
|
382 |
///Sets the map that indicates which nodes are processed.
|
alpar@100
|
383 |
|
kpeter@244
|
384 |
///Sets the map that indicates which nodes are processed.
|
alpar@100
|
385 |
///If you don't use this function before calling \ref run(),
|
alpar@100
|
386 |
///it will allocate one. The destructor deallocates this
|
alpar@100
|
387 |
///automatically allocated map, of course.
|
alpar@100
|
388 |
///\return <tt> (*this) </tt>
|
alpar@209
|
389 |
Bfs &processedMap(ProcessedMap &m)
|
alpar@100
|
390 |
{
|
alpar@100
|
391 |
if(local_processed) {
|
alpar@209
|
392 |
delete _processed;
|
alpar@209
|
393 |
local_processed=false;
|
alpar@100
|
394 |
}
|
alpar@100
|
395 |
_processed = &m;
|
alpar@100
|
396 |
return *this;
|
alpar@100
|
397 |
}
|
alpar@100
|
398 |
|
kpeter@244
|
399 |
///Sets the map that stores the distances of the nodes.
|
alpar@100
|
400 |
|
kpeter@244
|
401 |
///Sets the map that stores the distances of the nodes calculated by
|
kpeter@244
|
402 |
///the algorithm.
|
alpar@100
|
403 |
///If you don't use this function before calling \ref run(),
|
alpar@100
|
404 |
///it will allocate one. The destructor deallocates this
|
alpar@100
|
405 |
///automatically allocated map, of course.
|
alpar@100
|
406 |
///\return <tt> (*this) </tt>
|
alpar@209
|
407 |
Bfs &distMap(DistMap &m)
|
alpar@100
|
408 |
{
|
alpar@100
|
409 |
if(local_dist) {
|
alpar@209
|
410 |
delete _dist;
|
alpar@209
|
411 |
local_dist=false;
|
alpar@100
|
412 |
}
|
alpar@100
|
413 |
_dist = &m;
|
alpar@100
|
414 |
return *this;
|
alpar@100
|
415 |
}
|
alpar@100
|
416 |
|
alpar@100
|
417 |
public:
|
kpeter@244
|
418 |
|
alpar@100
|
419 |
///\name Execution control
|
alpar@100
|
420 |
///The simplest way to execute the algorithm is to use
|
kpeter@244
|
421 |
///one of the member functions called \ref lemon::Bfs::run() "run()".
|
alpar@100
|
422 |
///\n
|
kpeter@244
|
423 |
///If you need more control on the execution, first you must call
|
kpeter@244
|
424 |
///\ref lemon::Bfs::init() "init()", then you can add several source
|
kpeter@244
|
425 |
///nodes with \ref lemon::Bfs::addSource() "addSource()".
|
kpeter@244
|
426 |
///Finally \ref lemon::Bfs::start() "start()" will perform the
|
kpeter@244
|
427 |
///actual path computation.
|
alpar@100
|
428 |
|
alpar@100
|
429 |
///@{
|
alpar@100
|
430 |
|
kpeter@244
|
431 |
///Initializes the internal data structures.
|
kpeter@244
|
432 |
|
alpar@100
|
433 |
///Initializes the internal data structures.
|
alpar@100
|
434 |
///
|
alpar@100
|
435 |
void init()
|
alpar@100
|
436 |
{
|
alpar@100
|
437 |
create_maps();
|
alpar@100
|
438 |
_queue.resize(countNodes(*G));
|
alpar@100
|
439 |
_queue_head=_queue_tail=0;
|
alpar@100
|
440 |
_curr_dist=1;
|
alpar@100
|
441 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
|
alpar@209
|
442 |
_pred->set(u,INVALID);
|
alpar@209
|
443 |
_reached->set(u,false);
|
alpar@209
|
444 |
_processed->set(u,false);
|
alpar@100
|
445 |
}
|
alpar@100
|
446 |
}
|
alpar@209
|
447 |
|
alpar@100
|
448 |
///Adds a new source node.
|
alpar@100
|
449 |
|
alpar@100
|
450 |
///Adds a new source node to the set of nodes to be processed.
|
alpar@100
|
451 |
///
|
alpar@100
|
452 |
void addSource(Node s)
|
alpar@100
|
453 |
{
|
alpar@100
|
454 |
if(!(*_reached)[s])
|
alpar@209
|
455 |
{
|
alpar@209
|
456 |
_reached->set(s,true);
|
alpar@209
|
457 |
_pred->set(s,INVALID);
|
alpar@209
|
458 |
_dist->set(s,0);
|
alpar@209
|
459 |
_queue[_queue_head++]=s;
|
alpar@209
|
460 |
_queue_next_dist=_queue_head;
|
alpar@209
|
461 |
}
|
alpar@100
|
462 |
}
|
alpar@209
|
463 |
|
alpar@100
|
464 |
///Processes the next node.
|
alpar@100
|
465 |
|
alpar@100
|
466 |
///Processes the next node.
|
alpar@100
|
467 |
///
|
alpar@100
|
468 |
///\return The processed node.
|
alpar@100
|
469 |
///
|
kpeter@244
|
470 |
///\pre The queue must not be empty.
|
alpar@100
|
471 |
Node processNextNode()
|
alpar@100
|
472 |
{
|
alpar@100
|
473 |
if(_queue_tail==_queue_next_dist) {
|
alpar@209
|
474 |
_curr_dist++;
|
alpar@209
|
475 |
_queue_next_dist=_queue_head;
|
alpar@100
|
476 |
}
|
alpar@100
|
477 |
Node n=_queue[_queue_tail++];
|
alpar@100
|
478 |
_processed->set(n,true);
|
alpar@100
|
479 |
Node m;
|
alpar@100
|
480 |
for(OutArcIt e(*G,n);e!=INVALID;++e)
|
alpar@209
|
481 |
if(!(*_reached)[m=G->target(e)]) {
|
alpar@209
|
482 |
_queue[_queue_head++]=m;
|
alpar@209
|
483 |
_reached->set(m,true);
|
alpar@209
|
484 |
_pred->set(m,e);
|
alpar@209
|
485 |
_dist->set(m,_curr_dist);
|
alpar@209
|
486 |
}
|
alpar@100
|
487 |
return n;
|
alpar@100
|
488 |
}
|
alpar@100
|
489 |
|
alpar@100
|
490 |
///Processes the next node.
|
alpar@100
|
491 |
|
kpeter@244
|
492 |
///Processes the next node and checks if the given target node
|
alpar@100
|
493 |
///is reached. If the target node is reachable from the processed
|
kpeter@244
|
494 |
///node, then the \c reach parameter will be set to \c true.
|
alpar@100
|
495 |
///
|
alpar@100
|
496 |
///\param target The target node.
|
kpeter@244
|
497 |
///\retval reach Indicates if the target node is reached.
|
kpeter@244
|
498 |
///It should be initially \c false.
|
kpeter@244
|
499 |
///
|
alpar@100
|
500 |
///\return The processed node.
|
alpar@100
|
501 |
///
|
kpeter@244
|
502 |
///\pre The queue must not be empty.
|
alpar@100
|
503 |
Node processNextNode(Node target, bool& reach)
|
alpar@100
|
504 |
{
|
alpar@100
|
505 |
if(_queue_tail==_queue_next_dist) {
|
alpar@209
|
506 |
_curr_dist++;
|
alpar@209
|
507 |
_queue_next_dist=_queue_head;
|
alpar@100
|
508 |
}
|
alpar@100
|
509 |
Node n=_queue[_queue_tail++];
|
alpar@100
|
510 |
_processed->set(n,true);
|
alpar@100
|
511 |
Node m;
|
alpar@100
|
512 |
for(OutArcIt e(*G,n);e!=INVALID;++e)
|
alpar@209
|
513 |
if(!(*_reached)[m=G->target(e)]) {
|
alpar@209
|
514 |
_queue[_queue_head++]=m;
|
alpar@209
|
515 |
_reached->set(m,true);
|
alpar@209
|
516 |
_pred->set(m,e);
|
alpar@209
|
517 |
_dist->set(m,_curr_dist);
|
alpar@100
|
518 |
reach = reach || (target == m);
|
alpar@209
|
519 |
}
|
alpar@100
|
520 |
return n;
|
alpar@100
|
521 |
}
|
alpar@100
|
522 |
|
alpar@100
|
523 |
///Processes the next node.
|
alpar@100
|
524 |
|
kpeter@244
|
525 |
///Processes the next node and checks if at least one of reached
|
kpeter@244
|
526 |
///nodes has \c true value in the \c nm node map. If one node
|
kpeter@244
|
527 |
///with \c true value is reachable from the processed node, then the
|
kpeter@244
|
528 |
///\c rnode parameter will be set to the first of such nodes.
|
alpar@100
|
529 |
///
|
kpeter@244
|
530 |
///\param nm A \c bool (or convertible) node map that indicates the
|
kpeter@244
|
531 |
///possible targets.
|
alpar@100
|
532 |
///\retval rnode The reached target node.
|
kpeter@244
|
533 |
///It should be initially \c INVALID.
|
kpeter@244
|
534 |
///
|
alpar@100
|
535 |
///\return The processed node.
|
alpar@100
|
536 |
///
|
kpeter@244
|
537 |
///\pre The queue must not be empty.
|
alpar@100
|
538 |
template<class NM>
|
alpar@100
|
539 |
Node processNextNode(const NM& nm, Node& rnode)
|
alpar@100
|
540 |
{
|
alpar@100
|
541 |
if(_queue_tail==_queue_next_dist) {
|
alpar@209
|
542 |
_curr_dist++;
|
alpar@209
|
543 |
_queue_next_dist=_queue_head;
|
alpar@100
|
544 |
}
|
alpar@100
|
545 |
Node n=_queue[_queue_tail++];
|
alpar@100
|
546 |
_processed->set(n,true);
|
alpar@100
|
547 |
Node m;
|
alpar@100
|
548 |
for(OutArcIt e(*G,n);e!=INVALID;++e)
|
alpar@209
|
549 |
if(!(*_reached)[m=G->target(e)]) {
|
alpar@209
|
550 |
_queue[_queue_head++]=m;
|
alpar@209
|
551 |
_reached->set(m,true);
|
alpar@209
|
552 |
_pred->set(m,e);
|
alpar@209
|
553 |
_dist->set(m,_curr_dist);
|
alpar@209
|
554 |
if (nm[m] && rnode == INVALID) rnode = m;
|
alpar@209
|
555 |
}
|
alpar@100
|
556 |
return n;
|
alpar@100
|
557 |
}
|
alpar@209
|
558 |
|
kpeter@244
|
559 |
///The next node to be processed.
|
alpar@100
|
560 |
|
kpeter@244
|
561 |
///Returns the next node to be processed or \c INVALID if the queue
|
kpeter@244
|
562 |
///is empty.
|
kpeter@244
|
563 |
Node nextNode() const
|
alpar@209
|
564 |
{
|
alpar@100
|
565 |
return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID;
|
alpar@100
|
566 |
}
|
alpar@209
|
567 |
|
alpar@100
|
568 |
///\brief Returns \c false if there are nodes
|
kpeter@244
|
569 |
///to be processed.
|
alpar@100
|
570 |
///
|
alpar@100
|
571 |
///Returns \c false if there are nodes
|
kpeter@244
|
572 |
///to be processed in the queue.
|
kpeter@244
|
573 |
bool emptyQueue() const { return _queue_tail==_queue_head; }
|
kpeter@244
|
574 |
|
alpar@100
|
575 |
///Returns the number of the nodes to be processed.
|
alpar@209
|
576 |
|
alpar@100
|
577 |
///Returns the number of the nodes to be processed in the queue.
|
kpeter@244
|
578 |
int queueSize() const { return _queue_head-_queue_tail; }
|
alpar@209
|
579 |
|
alpar@100
|
580 |
///Executes the algorithm.
|
alpar@100
|
581 |
|
alpar@100
|
582 |
///Executes the algorithm.
|
alpar@100
|
583 |
///
|
kpeter@244
|
584 |
///This method runs the %BFS algorithm from the root node(s)
|
kpeter@244
|
585 |
///in order to compute the shortest path to each node.
|
alpar@100
|
586 |
///
|
kpeter@244
|
587 |
///The algorithm computes
|
kpeter@244
|
588 |
///- the shortest path tree (forest),
|
kpeter@244
|
589 |
///- the distance of each node from the root(s).
|
kpeter@244
|
590 |
///
|
kpeter@244
|
591 |
///\pre init() must be called and at least one root node should be
|
kpeter@244
|
592 |
///added with addSource() before using this function.
|
kpeter@244
|
593 |
///
|
kpeter@244
|
594 |
///\note <tt>b.start()</tt> is just a shortcut of the following code.
|
kpeter@244
|
595 |
///\code
|
kpeter@244
|
596 |
/// while ( !b.emptyQueue() ) {
|
kpeter@244
|
597 |
/// b.processNextNode();
|
kpeter@244
|
598 |
/// }
|
kpeter@244
|
599 |
///\endcode
|
alpar@100
|
600 |
void start()
|
alpar@100
|
601 |
{
|
alpar@100
|
602 |
while ( !emptyQueue() ) processNextNode();
|
alpar@100
|
603 |
}
|
alpar@209
|
604 |
|
kpeter@244
|
605 |
///Executes the algorithm until the given target node is reached.
|
alpar@100
|
606 |
|
kpeter@244
|
607 |
///Executes the algorithm until the given target node is reached.
|
alpar@100
|
608 |
///
|
alpar@100
|
609 |
///This method runs the %BFS algorithm from the root node(s)
|
alpar@100
|
610 |
///in order to compute the shortest path to \c dest.
|
kpeter@244
|
611 |
///
|
alpar@100
|
612 |
///The algorithm computes
|
kpeter@244
|
613 |
///- the shortest path to \c dest,
|
kpeter@244
|
614 |
///- the distance of \c dest from the root(s).
|
kpeter@244
|
615 |
///
|
kpeter@244
|
616 |
///\pre init() must be called and at least one root node should be
|
kpeter@244
|
617 |
///added with addSource() before using this function.
|
kpeter@244
|
618 |
///
|
kpeter@244
|
619 |
///\note <tt>b.start(t)</tt> is just a shortcut of the following code.
|
kpeter@244
|
620 |
///\code
|
kpeter@244
|
621 |
/// bool reach = false;
|
kpeter@244
|
622 |
/// while ( !b.emptyQueue() && !reach ) {
|
kpeter@244
|
623 |
/// b.processNextNode(t, reach);
|
kpeter@244
|
624 |
/// }
|
kpeter@244
|
625 |
///\endcode
|
alpar@100
|
626 |
void start(Node dest)
|
alpar@100
|
627 |
{
|
alpar@100
|
628 |
bool reach = false;
|
alpar@100
|
629 |
while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
|
alpar@100
|
630 |
}
|
alpar@209
|
631 |
|
alpar@100
|
632 |
///Executes the algorithm until a condition is met.
|
alpar@100
|
633 |
|
alpar@100
|
634 |
///Executes the algorithm until a condition is met.
|
alpar@100
|
635 |
///
|
kpeter@244
|
636 |
///This method runs the %BFS algorithm from the root node(s) in
|
kpeter@244
|
637 |
///order to compute the shortest path to a node \c v with
|
kpeter@244
|
638 |
/// <tt>nm[v]</tt> true, if such a node can be found.
|
alpar@100
|
639 |
///
|
kpeter@244
|
640 |
///\param nm A \c bool (or convertible) node map. The algorithm
|
kpeter@244
|
641 |
///will stop when it reaches a node \c v with <tt>nm[v]</tt> true.
|
alpar@100
|
642 |
///
|
alpar@100
|
643 |
///\return The reached node \c v with <tt>nm[v]</tt> true or
|
alpar@100
|
644 |
///\c INVALID if no such node was found.
|
kpeter@244
|
645 |
///
|
kpeter@244
|
646 |
///\pre init() must be called and at least one root node should be
|
kpeter@244
|
647 |
///added with addSource() before using this function.
|
kpeter@244
|
648 |
///
|
kpeter@244
|
649 |
///\note <tt>b.start(nm)</tt> is just a shortcut of the following code.
|
kpeter@244
|
650 |
///\code
|
kpeter@244
|
651 |
/// Node rnode = INVALID;
|
kpeter@244
|
652 |
/// while ( !b.emptyQueue() && rnode == INVALID ) {
|
kpeter@244
|
653 |
/// b.processNextNode(nm, rnode);
|
kpeter@244
|
654 |
/// }
|
kpeter@244
|
655 |
/// return rnode;
|
kpeter@244
|
656 |
///\endcode
|
kpeter@244
|
657 |
template<class NodeBoolMap>
|
kpeter@244
|
658 |
Node start(const NodeBoolMap &nm)
|
alpar@100
|
659 |
{
|
alpar@100
|
660 |
Node rnode = INVALID;
|
alpar@100
|
661 |
while ( !emptyQueue() && rnode == INVALID ) {
|
alpar@209
|
662 |
processNextNode(nm, rnode);
|
alpar@100
|
663 |
}
|
alpar@100
|
664 |
return rnode;
|
alpar@100
|
665 |
}
|
alpar@209
|
666 |
|
kpeter@244
|
667 |
///Runs the algorithm from the given node.
|
alpar@209
|
668 |
|
kpeter@244
|
669 |
///This method runs the %BFS algorithm from node \c s
|
kpeter@244
|
670 |
///in order to compute the shortest path to each node.
|
alpar@100
|
671 |
///
|
kpeter@244
|
672 |
///The algorithm computes
|
kpeter@244
|
673 |
///- the shortest path tree,
|
kpeter@244
|
674 |
///- the distance of each node from the root.
|
kpeter@244
|
675 |
///
|
kpeter@244
|
676 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code.
|
alpar@100
|
677 |
///\code
|
alpar@100
|
678 |
/// b.init();
|
alpar@100
|
679 |
/// b.addSource(s);
|
alpar@100
|
680 |
/// b.start();
|
alpar@100
|
681 |
///\endcode
|
alpar@100
|
682 |
void run(Node s) {
|
alpar@100
|
683 |
init();
|
alpar@100
|
684 |
addSource(s);
|
alpar@100
|
685 |
start();
|
alpar@100
|
686 |
}
|
alpar@209
|
687 |
|
alpar@100
|
688 |
///Finds the shortest path between \c s and \c t.
|
alpar@209
|
689 |
|
kpeter@244
|
690 |
///This method runs the %BFS algorithm from node \c s
|
kpeter@244
|
691 |
///in order to compute the shortest path to \c t.
|
alpar@100
|
692 |
///
|
kpeter@244
|
693 |
///\return The length of the shortest <tt>s</tt>--<tt>t</tt> path,
|
kpeter@244
|
694 |
///if \c t is reachable form \c s, \c 0 otherwise.
|
kpeter@244
|
695 |
///
|
kpeter@244
|
696 |
///\note Apart from the return value, <tt>b.run(s,t)</tt> is just a
|
kpeter@244
|
697 |
///shortcut of the following code.
|
alpar@100
|
698 |
///\code
|
alpar@100
|
699 |
/// b.init();
|
alpar@100
|
700 |
/// b.addSource(s);
|
alpar@100
|
701 |
/// b.start(t);
|
alpar@100
|
702 |
///\endcode
|
alpar@100
|
703 |
int run(Node s,Node t) {
|
alpar@100
|
704 |
init();
|
alpar@100
|
705 |
addSource(s);
|
alpar@100
|
706 |
start(t);
|
alpar@100
|
707 |
return reached(t) ? _curr_dist : 0;
|
alpar@100
|
708 |
}
|
alpar@209
|
709 |
|
kpeter@244
|
710 |
///Runs the algorithm to visit all nodes in the digraph.
|
kpeter@244
|
711 |
|
kpeter@244
|
712 |
///This method runs the %BFS algorithm in order to
|
kpeter@244
|
713 |
///compute the shortest path to each node.
|
kpeter@244
|
714 |
///
|
kpeter@244
|
715 |
///The algorithm computes
|
kpeter@244
|
716 |
///- the shortest path tree (forest),
|
kpeter@244
|
717 |
///- the distance of each node from the root(s).
|
kpeter@244
|
718 |
///
|
kpeter@244
|
719 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code.
|
kpeter@244
|
720 |
///\code
|
kpeter@244
|
721 |
/// b.init();
|
kpeter@244
|
722 |
/// for (NodeIt n(gr); n != INVALID; ++n) {
|
kpeter@244
|
723 |
/// if (!b.reached(n)) {
|
kpeter@244
|
724 |
/// b.addSource(n);
|
kpeter@244
|
725 |
/// b.start();
|
kpeter@244
|
726 |
/// }
|
kpeter@244
|
727 |
/// }
|
kpeter@244
|
728 |
///\endcode
|
kpeter@244
|
729 |
void run() {
|
kpeter@244
|
730 |
init();
|
kpeter@244
|
731 |
for (NodeIt n(*G); n != INVALID; ++n) {
|
kpeter@244
|
732 |
if (!reached(n)) {
|
kpeter@244
|
733 |
addSource(n);
|
kpeter@244
|
734 |
start();
|
kpeter@244
|
735 |
}
|
kpeter@244
|
736 |
}
|
kpeter@244
|
737 |
}
|
kpeter@244
|
738 |
|
alpar@100
|
739 |
///@}
|
alpar@100
|
740 |
|
alpar@100
|
741 |
///\name Query Functions
|
alpar@100
|
742 |
///The result of the %BFS algorithm can be obtained using these
|
alpar@100
|
743 |
///functions.\n
|
kpeter@244
|
744 |
///Either \ref lemon::Bfs::run() "run()" or \ref lemon::Bfs::start()
|
kpeter@244
|
745 |
///"start()" must be called before using them.
|
alpar@209
|
746 |
|
alpar@100
|
747 |
///@{
|
alpar@100
|
748 |
|
kpeter@244
|
749 |
///The shortest path to a node.
|
alpar@100
|
750 |
|
kpeter@244
|
751 |
///Returns the shortest path to a node.
|
kpeter@244
|
752 |
///
|
kpeter@244
|
753 |
///\warning \c t should be reachable from the root(s).
|
kpeter@244
|
754 |
///
|
kpeter@244
|
755 |
///\pre Either \ref run() or \ref start() must be called before
|
kpeter@244
|
756 |
///using this function.
|
kpeter@244
|
757 |
Path path(Node t) const { return Path(*G, *_pred, t); }
|
alpar@100
|
758 |
|
alpar@100
|
759 |
///The distance of a node from the root(s).
|
alpar@100
|
760 |
|
alpar@100
|
761 |
///Returns the distance of a node from the root(s).
|
kpeter@244
|
762 |
///
|
kpeter@244
|
763 |
///\warning If node \c v is not reachable from the root(s), then
|
kpeter@244
|
764 |
///the return value of this function is undefined.
|
kpeter@244
|
765 |
///
|
kpeter@244
|
766 |
///\pre Either \ref run() or \ref start() must be called before
|
kpeter@244
|
767 |
///using this function.
|
alpar@100
|
768 |
int dist(Node v) const { return (*_dist)[v]; }
|
alpar@100
|
769 |
|
kpeter@244
|
770 |
///Returns the 'previous arc' of the shortest path tree for a node.
|
alpar@100
|
771 |
|
kpeter@244
|
772 |
///This function returns the 'previous arc' of the shortest path
|
kpeter@244
|
773 |
///tree for the node \c v, i.e. it returns the last arc of a
|
kpeter@244
|
774 |
///shortest path from the root(s) to \c v. It is \c INVALID if \c v
|
kpeter@244
|
775 |
///is not reachable from the root(s) or if \c v is a root.
|
kpeter@244
|
776 |
///
|
kpeter@244
|
777 |
///The shortest path tree used here is equal to the shortest path
|
kpeter@244
|
778 |
///tree used in \ref predNode().
|
kpeter@244
|
779 |
///
|
kpeter@244
|
780 |
///\pre Either \ref run() or \ref start() must be called before
|
kpeter@244
|
781 |
///using this function.
|
alpar@100
|
782 |
Arc predArc(Node v) const { return (*_pred)[v];}
|
alpar@100
|
783 |
|
kpeter@244
|
784 |
///Returns the 'previous node' of the shortest path tree for a node.
|
alpar@100
|
785 |
|
kpeter@244
|
786 |
///This function returns the 'previous node' of the shortest path
|
kpeter@244
|
787 |
///tree for the node \c v, i.e. it returns the last but one node
|
kpeter@244
|
788 |
///from a shortest path from the root(s) to \c v. It is \c INVALID
|
kpeter@244
|
789 |
///if \c v is not reachable from the root(s) or if \c v is a root.
|
kpeter@244
|
790 |
///
|
alpar@100
|
791 |
///The shortest path tree used here is equal to the shortest path
|
alpar@100
|
792 |
///tree used in \ref predArc().
|
kpeter@244
|
793 |
///
|
alpar@100
|
794 |
///\pre Either \ref run() or \ref start() must be called before
|
alpar@100
|
795 |
///using this function.
|
alpar@100
|
796 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
|
alpar@209
|
797 |
G->source((*_pred)[v]); }
|
alpar@209
|
798 |
|
kpeter@244
|
799 |
///\brief Returns a const reference to the node map that stores the
|
kpeter@244
|
800 |
/// distances of the nodes.
|
kpeter@244
|
801 |
///
|
kpeter@244
|
802 |
///Returns a const reference to the node map that stores the distances
|
kpeter@244
|
803 |
///of the nodes calculated by the algorithm.
|
kpeter@244
|
804 |
///
|
kpeter@244
|
805 |
///\pre Either \ref run() or \ref init()
|
kpeter@244
|
806 |
///must be called before using this function.
|
alpar@100
|
807 |
const DistMap &distMap() const { return *_dist;}
|
alpar@209
|
808 |
|
kpeter@244
|
809 |
///\brief Returns a const reference to the node map that stores the
|
kpeter@244
|
810 |
///predecessor arcs.
|
kpeter@244
|
811 |
///
|
kpeter@244
|
812 |
///Returns a const reference to the node map that stores the predecessor
|
kpeter@244
|
813 |
///arcs, which form the shortest path tree.
|
kpeter@244
|
814 |
///
|
alpar@100
|
815 |
///\pre Either \ref run() or \ref init()
|
alpar@100
|
816 |
///must be called before using this function.
|
alpar@100
|
817 |
const PredMap &predMap() const { return *_pred;}
|
alpar@209
|
818 |
|
kpeter@244
|
819 |
///Checks if a node is reachable from the root(s).
|
alpar@100
|
820 |
|
kpeter@244
|
821 |
///Returns \c true if \c v is reachable from the root(s).
|
alpar@100
|
822 |
///\pre Either \ref run() or \ref start()
|
alpar@100
|
823 |
///must be called before using this function.
|
kpeter@244
|
824 |
bool reached(Node v) const { return (*_reached)[v]; }
|
alpar@209
|
825 |
|
alpar@100
|
826 |
///@}
|
alpar@100
|
827 |
};
|
alpar@100
|
828 |
|
kpeter@244
|
829 |
///Default traits class of bfs() function.
|
alpar@100
|
830 |
|
kpeter@244
|
831 |
///Default traits class of bfs() function.
|
kpeter@157
|
832 |
///\tparam GR Digraph type.
|
alpar@100
|
833 |
template<class GR>
|
alpar@100
|
834 |
struct BfsWizardDefaultTraits
|
alpar@100
|
835 |
{
|
kpeter@244
|
836 |
///The type of the digraph the algorithm runs on.
|
alpar@100
|
837 |
typedef GR Digraph;
|
kpeter@244
|
838 |
|
kpeter@244
|
839 |
///\brief The type of the map that stores the predecessor
|
alpar@100
|
840 |
///arcs of the shortest paths.
|
alpar@209
|
841 |
///
|
kpeter@244
|
842 |
///The type of the map that stores the predecessor
|
alpar@100
|
843 |
///arcs of the shortest paths.
|
alpar@100
|
844 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept.
|
kpeter@278
|
845 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
|
kpeter@244
|
846 |
///Instantiates a \ref PredMap.
|
alpar@209
|
847 |
|
alpar@209
|
848 |
///This function instantiates a \ref PredMap.
|
kpeter@244
|
849 |
///\param g is the digraph, to which we would like to define the
|
kpeter@244
|
850 |
///\ref PredMap.
|
alpar@100
|
851 |
///\todo The digraph alone may be insufficient to initialize
|
kpeter@244
|
852 |
static PredMap *createPredMap(const Digraph &g)
|
alpar@100
|
853 |
{
|
kpeter@278
|
854 |
return new PredMap(g);
|
alpar@100
|
855 |
}
|
alpar@100
|
856 |
|
alpar@100
|
857 |
///The type of the map that indicates which nodes are processed.
|
alpar@209
|
858 |
|
alpar@100
|
859 |
///The type of the map that indicates which nodes are processed.
|
alpar@100
|
860 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept.
|
kpeter@278
|
861 |
///By default it is a NullMap.
|
alpar@100
|
862 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
|
kpeter@244
|
863 |
///Instantiates a \ref ProcessedMap.
|
alpar@209
|
864 |
|
alpar@209
|
865 |
///This function instantiates a \ref ProcessedMap.
|
alpar@100
|
866 |
///\param g is the digraph, to which
|
kpeter@244
|
867 |
///we would like to define the \ref ProcessedMap.
|
alpar@100
|
868 |
#ifdef DOXYGEN
|
kpeter@244
|
869 |
static ProcessedMap *createProcessedMap(const Digraph &g)
|
alpar@100
|
870 |
#else
|
kpeter@244
|
871 |
static ProcessedMap *createProcessedMap(const Digraph &)
|
alpar@100
|
872 |
#endif
|
alpar@100
|
873 |
{
|
alpar@100
|
874 |
return new ProcessedMap();
|
alpar@100
|
875 |
}
|
kpeter@244
|
876 |
|
alpar@100
|
877 |
///The type of the map that indicates which nodes are reached.
|
alpar@209
|
878 |
|
alpar@100
|
879 |
///The type of the map that indicates which nodes are reached.
|
kpeter@244
|
880 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
|
alpar@100
|
881 |
typedef typename Digraph::template NodeMap<bool> ReachedMap;
|
kpeter@244
|
882 |
///Instantiates a \ref ReachedMap.
|
alpar@209
|
883 |
|
alpar@209
|
884 |
///This function instantiates a \ref ReachedMap.
|
kpeter@244
|
885 |
///\param g is the digraph, to which
|
alpar@100
|
886 |
///we would like to define the \ref ReachedMap.
|
kpeter@244
|
887 |
static ReachedMap *createReachedMap(const Digraph &g)
|
alpar@100
|
888 |
{
|
kpeter@244
|
889 |
return new ReachedMap(g);
|
alpar@100
|
890 |
}
|
alpar@209
|
891 |
|
kpeter@244
|
892 |
///The type of the map that stores the distances of the nodes.
|
kpeter@244
|
893 |
|
kpeter@244
|
894 |
///The type of the map that stores the distances of the nodes.
|
alpar@100
|
895 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept.
|
kpeter@278
|
896 |
typedef typename Digraph::template NodeMap<int> DistMap;
|
kpeter@244
|
897 |
///Instantiates a \ref DistMap.
|
alpar@209
|
898 |
|
alpar@209
|
899 |
///This function instantiates a \ref DistMap.
|
alpar@210
|
900 |
///\param g is the digraph, to which we would like to define
|
alpar@210
|
901 |
///the \ref DistMap
|
kpeter@244
|
902 |
static DistMap *createDistMap(const Digraph &g)
|
alpar@100
|
903 |
{
|
kpeter@278
|
904 |
return new DistMap(g);
|
alpar@100
|
905 |
}
|
kpeter@278
|
906 |
|
kpeter@278
|
907 |
///The type of the shortest paths.
|
kpeter@278
|
908 |
|
kpeter@278
|
909 |
///The type of the shortest paths.
|
kpeter@278
|
910 |
///It must meet the \ref concepts::Path "Path" concept.
|
kpeter@278
|
911 |
typedef lemon::Path<Digraph> Path;
|
alpar@100
|
912 |
};
|
alpar@209
|
913 |
|
kpeter@244
|
914 |
/// Default traits class used by \ref BfsWizard
|
alpar@100
|
915 |
|
alpar@100
|
916 |
/// To make it easier to use Bfs algorithm
|
kpeter@244
|
917 |
/// we have created a wizard class.
|
alpar@100
|
918 |
/// This \ref BfsWizard class needs default traits,
|
kpeter@244
|
919 |
/// as well as the \ref Bfs class.
|
alpar@100
|
920 |
/// The \ref BfsWizardBase is a class to be the default traits of the
|
alpar@100
|
921 |
/// \ref BfsWizard class.
|
alpar@100
|
922 |
template<class GR>
|
alpar@100
|
923 |
class BfsWizardBase : public BfsWizardDefaultTraits<GR>
|
alpar@100
|
924 |
{
|
alpar@100
|
925 |
|
alpar@100
|
926 |
typedef BfsWizardDefaultTraits<GR> Base;
|
alpar@100
|
927 |
protected:
|
kpeter@244
|
928 |
//The type of the nodes in the digraph.
|
alpar@100
|
929 |
typedef typename Base::Digraph::Node Node;
|
alpar@100
|
930 |
|
kpeter@244
|
931 |
//Pointer to the digraph the algorithm runs on.
|
alpar@100
|
932 |
void *_g;
|
kpeter@244
|
933 |
//Pointer to the map of reached nodes.
|
alpar@100
|
934 |
void *_reached;
|
kpeter@244
|
935 |
//Pointer to the map of processed nodes.
|
alpar@100
|
936 |
void *_processed;
|
kpeter@244
|
937 |
//Pointer to the map of predecessors arcs.
|
alpar@100
|
938 |
void *_pred;
|
kpeter@244
|
939 |
//Pointer to the map of distances.
|
alpar@100
|
940 |
void *_dist;
|
kpeter@278
|
941 |
//Pointer to the shortest path to the target node.
|
kpeter@278
|
942 |
void *_path;
|
kpeter@278
|
943 |
//Pointer to the distance of the target node.
|
kpeter@278
|
944 |
int *_di;
|
alpar@209
|
945 |
|
alpar@100
|
946 |
public:
|
alpar@100
|
947 |
/// Constructor.
|
alpar@209
|
948 |
|
alpar@100
|
949 |
/// This constructor does not require parameters, therefore it initiates
|
kpeter@278
|
950 |
/// all of the attributes to \c 0.
|
alpar@100
|
951 |
BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
|
kpeter@278
|
952 |
_dist(0), _path(0), _di(0) {}
|
alpar@100
|
953 |
|
alpar@100
|
954 |
/// Constructor.
|
alpar@209
|
955 |
|
kpeter@278
|
956 |
/// This constructor requires one parameter,
|
kpeter@278
|
957 |
/// others are initiated to \c 0.
|
kpeter@244
|
958 |
/// \param g The digraph the algorithm runs on.
|
kpeter@278
|
959 |
BfsWizardBase(const GR &g) :
|
alpar@209
|
960 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
|
kpeter@278
|
961 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {}
|
alpar@100
|
962 |
|
alpar@100
|
963 |
};
|
alpar@209
|
964 |
|
kpeter@278
|
965 |
/// Auxiliary class for the function-type interface of BFS algorithm.
|
alpar@100
|
966 |
|
kpeter@278
|
967 |
/// This auxiliary class is created to implement the
|
kpeter@278
|
968 |
/// \ref bfs() "function-type interface" of \ref Bfs algorithm.
|
kpeter@278
|
969 |
/// It does not have own \ref run() method, it uses the functions
|
kpeter@278
|
970 |
/// and features of the plain \ref Bfs.
|
alpar@100
|
971 |
///
|
kpeter@278
|
972 |
/// This class should only be used through the \ref bfs() function,
|
kpeter@278
|
973 |
/// which makes it easier to use the algorithm.
|
alpar@100
|
974 |
template<class TR>
|
alpar@100
|
975 |
class BfsWizard : public TR
|
alpar@100
|
976 |
{
|
alpar@100
|
977 |
typedef TR Base;
|
alpar@100
|
978 |
|
kpeter@244
|
979 |
///The type of the digraph the algorithm runs on.
|
alpar@100
|
980 |
typedef typename TR::Digraph Digraph;
|
kpeter@244
|
981 |
|
alpar@100
|
982 |
typedef typename Digraph::Node Node;
|
alpar@100
|
983 |
typedef typename Digraph::NodeIt NodeIt;
|
alpar@100
|
984 |
typedef typename Digraph::Arc Arc;
|
alpar@100
|
985 |
typedef typename Digraph::OutArcIt OutArcIt;
|
alpar@209
|
986 |
|
kpeter@244
|
987 |
///\brief The type of the map that stores the predecessor
|
alpar@100
|
988 |
///arcs of the shortest paths.
|
alpar@100
|
989 |
typedef typename TR::PredMap PredMap;
|
kpeter@244
|
990 |
///\brief The type of the map that stores the distances of the nodes.
|
alpar@100
|
991 |
typedef typename TR::DistMap DistMap;
|
kpeter@244
|
992 |
///\brief The type of the map that indicates which nodes are reached.
|
kpeter@244
|
993 |
typedef typename TR::ReachedMap ReachedMap;
|
kpeter@244
|
994 |
///\brief The type of the map that indicates which nodes are processed.
|
kpeter@244
|
995 |
typedef typename TR::ProcessedMap ProcessedMap;
|
kpeter@278
|
996 |
///The type of the shortest paths
|
kpeter@278
|
997 |
typedef typename TR::Path Path;
|
alpar@100
|
998 |
|
alpar@100
|
999 |
public:
|
kpeter@244
|
1000 |
|
alpar@100
|
1001 |
/// Constructor.
|
alpar@100
|
1002 |
BfsWizard() : TR() {}
|
alpar@100
|
1003 |
|
alpar@100
|
1004 |
/// Constructor that requires parameters.
|
alpar@100
|
1005 |
|
alpar@100
|
1006 |
/// Constructor that requires parameters.
|
alpar@100
|
1007 |
/// These parameters will be the default values for the traits class.
|
kpeter@278
|
1008 |
/// \param g The digraph the algorithm runs on.
|
kpeter@278
|
1009 |
BfsWizard(const Digraph &g) :
|
kpeter@278
|
1010 |
TR(g) {}
|
alpar@100
|
1011 |
|
alpar@100
|
1012 |
///Copy constructor
|
alpar@100
|
1013 |
BfsWizard(const TR &b) : TR(b) {}
|
alpar@100
|
1014 |
|
alpar@100
|
1015 |
~BfsWizard() {}
|
alpar@100
|
1016 |
|
kpeter@278
|
1017 |
///Runs BFS algorithm from the given source node.
|
alpar@209
|
1018 |
|
kpeter@278
|
1019 |
///This method runs BFS algorithm from node \c s
|
kpeter@278
|
1020 |
///in order to compute the shortest path to each node.
|
kpeter@278
|
1021 |
void run(Node s)
|
kpeter@278
|
1022 |
{
|
kpeter@278
|
1023 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
|
kpeter@278
|
1024 |
if (Base::_pred)
|
kpeter@278
|
1025 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
|
kpeter@278
|
1026 |
if (Base::_dist)
|
kpeter@278
|
1027 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
|
kpeter@278
|
1028 |
if (Base::_reached)
|
kpeter@278
|
1029 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
|
kpeter@278
|
1030 |
if (Base::_processed)
|
kpeter@278
|
1031 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
|
kpeter@278
|
1032 |
if (s!=INVALID)
|
kpeter@278
|
1033 |
alg.run(s);
|
kpeter@278
|
1034 |
else
|
kpeter@278
|
1035 |
alg.run();
|
kpeter@278
|
1036 |
}
|
kpeter@278
|
1037 |
|
kpeter@278
|
1038 |
///Finds the shortest path between \c s and \c t.
|
kpeter@278
|
1039 |
|
kpeter@278
|
1040 |
///This method runs BFS algorithm from node \c s
|
kpeter@278
|
1041 |
///in order to compute the shortest path to node \c t
|
kpeter@278
|
1042 |
///(it stops searching when \c t is processed).
|
kpeter@278
|
1043 |
///
|
kpeter@278
|
1044 |
///\return \c true if \c t is reachable form \c s.
|
kpeter@278
|
1045 |
bool run(Node s, Node t)
|
kpeter@278
|
1046 |
{
|
kpeter@278
|
1047 |
if (s==INVALID || t==INVALID) throw UninitializedParameter();
|
kpeter@278
|
1048 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
|
kpeter@278
|
1049 |
if (Base::_pred)
|
kpeter@278
|
1050 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
|
kpeter@278
|
1051 |
if (Base::_dist)
|
kpeter@278
|
1052 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
|
kpeter@278
|
1053 |
if (Base::_reached)
|
kpeter@278
|
1054 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
|
kpeter@278
|
1055 |
if (Base::_processed)
|
kpeter@278
|
1056 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
|
kpeter@278
|
1057 |
alg.run(s,t);
|
kpeter@278
|
1058 |
if (Base::_path)
|
kpeter@278
|
1059 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t);
|
kpeter@278
|
1060 |
if (Base::_di)
|
kpeter@278
|
1061 |
*Base::_di = alg.dist(t);
|
kpeter@278
|
1062 |
return alg.reached(t);
|
kpeter@278
|
1063 |
}
|
kpeter@278
|
1064 |
|
kpeter@278
|
1065 |
///Runs BFS algorithm to visit all nodes in the digraph.
|
kpeter@278
|
1066 |
|
kpeter@278
|
1067 |
///This method runs BFS algorithm in order to compute
|
kpeter@278
|
1068 |
///the shortest path to each node.
|
alpar@100
|
1069 |
void run()
|
alpar@100
|
1070 |
{
|
kpeter@278
|
1071 |
run(INVALID);
|
alpar@100
|
1072 |
}
|
alpar@209
|
1073 |
|
kpeter@244
|
1074 |
template<class T>
|
kpeter@257
|
1075 |
struct SetPredMapBase : public Base {
|
kpeter@244
|
1076 |
typedef T PredMap;
|
kpeter@244
|
1077 |
static PredMap *createPredMap(const Digraph &) { return 0; };
|
kpeter@257
|
1078 |
SetPredMapBase(const TR &b) : TR(b) {}
|
kpeter@244
|
1079 |
};
|
kpeter@278
|
1080 |
///\brief \ref named-func-param "Named parameter"
|
kpeter@244
|
1081 |
///for setting \ref PredMap object.
|
kpeter@244
|
1082 |
///
|
kpeter@278
|
1083 |
///\ref named-func-param "Named parameter"
|
kpeter@244
|
1084 |
///for setting \ref PredMap object.
|
kpeter@244
|
1085 |
template<class T>
|
kpeter@257
|
1086 |
BfsWizard<SetPredMapBase<T> > predMap(const T &t)
|
kpeter@244
|
1087 |
{
|
kpeter@244
|
1088 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
|
kpeter@257
|
1089 |
return BfsWizard<SetPredMapBase<T> >(*this);
|
kpeter@244
|
1090 |
}
|
kpeter@244
|
1091 |
|
kpeter@244
|
1092 |
template<class T>
|
kpeter@257
|
1093 |
struct SetReachedMapBase : public Base {
|
kpeter@244
|
1094 |
typedef T ReachedMap;
|
kpeter@244
|
1095 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; };
|
kpeter@257
|
1096 |
SetReachedMapBase(const TR &b) : TR(b) {}
|
kpeter@244
|
1097 |
};
|
kpeter@278
|
1098 |
///\brief \ref named-func-param "Named parameter"
|
kpeter@244
|
1099 |
///for setting \ref ReachedMap object.
|
kpeter@244
|
1100 |
///
|
kpeter@278
|
1101 |
/// \ref named-func-param "Named parameter"
|
kpeter@244
|
1102 |
///for setting \ref ReachedMap object.
|
kpeter@244
|
1103 |
template<class T>
|
kpeter@257
|
1104 |
BfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
|
kpeter@244
|
1105 |
{
|
kpeter@244
|
1106 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
|
kpeter@257
|
1107 |
return BfsWizard<SetReachedMapBase<T> >(*this);
|
kpeter@244
|
1108 |
}
|
kpeter@244
|
1109 |
|
kpeter@244
|
1110 |
template<class T>
|
kpeter@278
|
1111 |
struct SetDistMapBase : public Base {
|
kpeter@278
|
1112 |
typedef T DistMap;
|
kpeter@278
|
1113 |
static DistMap *createDistMap(const Digraph &) { return 0; };
|
kpeter@278
|
1114 |
SetDistMapBase(const TR &b) : TR(b) {}
|
kpeter@278
|
1115 |
};
|
kpeter@278
|
1116 |
///\brief \ref named-func-param "Named parameter"
|
kpeter@278
|
1117 |
///for setting \ref DistMap object.
|
kpeter@278
|
1118 |
///
|
kpeter@278
|
1119 |
/// \ref named-func-param "Named parameter"
|
kpeter@278
|
1120 |
///for setting \ref DistMap object.
|
kpeter@278
|
1121 |
template<class T>
|
kpeter@278
|
1122 |
BfsWizard<SetDistMapBase<T> > distMap(const T &t)
|
kpeter@278
|
1123 |
{
|
kpeter@278
|
1124 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
|
kpeter@278
|
1125 |
return BfsWizard<SetDistMapBase<T> >(*this);
|
kpeter@278
|
1126 |
}
|
kpeter@278
|
1127 |
|
kpeter@278
|
1128 |
template<class T>
|
kpeter@257
|
1129 |
struct SetProcessedMapBase : public Base {
|
kpeter@244
|
1130 |
typedef T ProcessedMap;
|
kpeter@244
|
1131 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
|
kpeter@257
|
1132 |
SetProcessedMapBase(const TR &b) : TR(b) {}
|
kpeter@244
|
1133 |
};
|
kpeter@278
|
1134 |
///\brief \ref named-func-param "Named parameter"
|
kpeter@244
|
1135 |
///for setting \ref ProcessedMap object.
|
kpeter@244
|
1136 |
///
|
kpeter@278
|
1137 |
/// \ref named-func-param "Named parameter"
|
kpeter@244
|
1138 |
///for setting \ref ProcessedMap object.
|
kpeter@244
|
1139 |
template<class T>
|
kpeter@257
|
1140 |
BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
|
kpeter@244
|
1141 |
{
|
kpeter@244
|
1142 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
|
kpeter@257
|
1143 |
return BfsWizard<SetProcessedMapBase<T> >(*this);
|
kpeter@244
|
1144 |
}
|
kpeter@244
|
1145 |
|
kpeter@244
|
1146 |
template<class T>
|
kpeter@278
|
1147 |
struct SetPathBase : public Base {
|
kpeter@278
|
1148 |
typedef T Path;
|
kpeter@278
|
1149 |
SetPathBase(const TR &b) : TR(b) {}
|
kpeter@244
|
1150 |
};
|
kpeter@278
|
1151 |
///\brief \ref named-func-param "Named parameter"
|
kpeter@278
|
1152 |
///for getting the shortest path to the target node.
|
kpeter@244
|
1153 |
///
|
kpeter@278
|
1154 |
///\ref named-func-param "Named parameter"
|
kpeter@278
|
1155 |
///for getting the shortest path to the target node.
|
kpeter@244
|
1156 |
template<class T>
|
kpeter@278
|
1157 |
BfsWizard<SetPathBase<T> > path(const T &t)
|
kpeter@244
|
1158 |
{
|
kpeter@278
|
1159 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
|
kpeter@278
|
1160 |
return BfsWizard<SetPathBase<T> >(*this);
|
kpeter@278
|
1161 |
}
|
kpeter@278
|
1162 |
|
kpeter@278
|
1163 |
///\brief \ref named-func-param "Named parameter"
|
kpeter@278
|
1164 |
///for getting the distance of the target node.
|
kpeter@278
|
1165 |
///
|
kpeter@278
|
1166 |
///\ref named-func-param "Named parameter"
|
kpeter@278
|
1167 |
///for getting the distance of the target node.
|
kpeter@278
|
1168 |
BfsWizard dist(const int &d)
|
kpeter@278
|
1169 |
{
|
kpeter@278
|
1170 |
Base::_di=const_cast<int*>(&d);
|
kpeter@278
|
1171 |
return *this;
|
kpeter@244
|
1172 |
}
|
kpeter@244
|
1173 |
|
alpar@100
|
1174 |
};
|
alpar@209
|
1175 |
|
kpeter@278
|
1176 |
///Function-type interface for BFS algorithm.
|
alpar@100
|
1177 |
|
alpar@100
|
1178 |
/// \ingroup search
|
kpeter@278
|
1179 |
///Function-type interface for BFS algorithm.
|
alpar@100
|
1180 |
///
|
kpeter@278
|
1181 |
///This function also has several \ref named-func-param "named parameters",
|
alpar@100
|
1182 |
///they are declared as the members of class \ref BfsWizard.
|
kpeter@278
|
1183 |
///The following examples show how to use these parameters.
|
alpar@100
|
1184 |
///\code
|
kpeter@278
|
1185 |
/// // Compute shortest path from node s to each node
|
kpeter@278
|
1186 |
/// bfs(g).predMap(preds).distMap(dists).run(s);
|
kpeter@278
|
1187 |
///
|
kpeter@278
|
1188 |
/// // Compute shortest path from s to t
|
kpeter@278
|
1189 |
/// bool reached = bfs(g).path(p).dist(d).run(s,t);
|
alpar@100
|
1190 |
///\endcode
|
alpar@100
|
1191 |
///\warning Don't forget to put the \ref BfsWizard::run() "run()"
|
alpar@100
|
1192 |
///to the end of the parameter list.
|
alpar@100
|
1193 |
///\sa BfsWizard
|
alpar@100
|
1194 |
///\sa Bfs
|
alpar@100
|
1195 |
template<class GR>
|
alpar@100
|
1196 |
BfsWizard<BfsWizardBase<GR> >
|
kpeter@278
|
1197 |
bfs(const GR &digraph)
|
alpar@100
|
1198 |
{
|
kpeter@278
|
1199 |
return BfsWizard<BfsWizardBase<GR> >(digraph);
|
alpar@100
|
1200 |
}
|
alpar@100
|
1201 |
|
alpar@100
|
1202 |
#ifdef DOXYGEN
|
kpeter@244
|
1203 |
/// \brief Visitor class for BFS.
|
alpar@209
|
1204 |
///
|
alpar@100
|
1205 |
/// This class defines the interface of the BfsVisit events, and
|
kpeter@244
|
1206 |
/// it could be the base of a real visitor class.
|
alpar@100
|
1207 |
template <typename _Digraph>
|
alpar@100
|
1208 |
struct BfsVisitor {
|
alpar@100
|
1209 |
typedef _Digraph Digraph;
|
alpar@100
|
1210 |
typedef typename Digraph::Arc Arc;
|
alpar@100
|
1211 |
typedef typename Digraph::Node Node;
|
kpeter@244
|
1212 |
/// \brief Called for the source node(s) of the BFS.
|
alpar@209
|
1213 |
///
|
kpeter@244
|
1214 |
/// This function is called for the source node(s) of the BFS.
|
kpeter@244
|
1215 |
void start(const Node& node) {}
|
kpeter@244
|
1216 |
/// \brief Called when a node is reached first time.
|
kpeter@244
|
1217 |
///
|
kpeter@244
|
1218 |
/// This function is called when a node is reached first time.
|
kpeter@244
|
1219 |
void reach(const Node& node) {}
|
kpeter@244
|
1220 |
/// \brief Called when a node is processed.
|
kpeter@244
|
1221 |
///
|
kpeter@244
|
1222 |
/// This function is called when a node is processed.
|
kpeter@244
|
1223 |
void process(const Node& node) {}
|
kpeter@244
|
1224 |
/// \brief Called when an arc reaches a new node.
|
kpeter@244
|
1225 |
///
|
kpeter@244
|
1226 |
/// This function is called when the BFS finds an arc whose target node
|
kpeter@244
|
1227 |
/// is not reached yet.
|
alpar@100
|
1228 |
void discover(const Arc& arc) {}
|
kpeter@244
|
1229 |
/// \brief Called when an arc is examined but its target node is
|
alpar@100
|
1230 |
/// already discovered.
|
alpar@209
|
1231 |
///
|
kpeter@244
|
1232 |
/// This function is called when an arc is examined but its target node is
|
alpar@100
|
1233 |
/// already discovered.
|
alpar@100
|
1234 |
void examine(const Arc& arc) {}
|
alpar@100
|
1235 |
};
|
alpar@100
|
1236 |
#else
|
alpar@100
|
1237 |
template <typename _Digraph>
|
alpar@100
|
1238 |
struct BfsVisitor {
|
alpar@100
|
1239 |
typedef _Digraph Digraph;
|
alpar@100
|
1240 |
typedef typename Digraph::Arc Arc;
|
alpar@100
|
1241 |
typedef typename Digraph::Node Node;
|
kpeter@244
|
1242 |
void start(const Node&) {}
|
kpeter@244
|
1243 |
void reach(const Node&) {}
|
kpeter@244
|
1244 |
void process(const Node&) {}
|
alpar@100
|
1245 |
void discover(const Arc&) {}
|
alpar@100
|
1246 |
void examine(const Arc&) {}
|
alpar@100
|
1247 |
|
alpar@100
|
1248 |
template <typename _Visitor>
|
alpar@100
|
1249 |
struct Constraints {
|
alpar@100
|
1250 |
void constraints() {
|
alpar@209
|
1251 |
Arc arc;
|
alpar@209
|
1252 |
Node node;
|
kpeter@244
|
1253 |
visitor.start(node);
|
kpeter@244
|
1254 |
visitor.reach(node);
|
kpeter@244
|
1255 |
visitor.process(node);
|
alpar@209
|
1256 |
visitor.discover(arc);
|
alpar@209
|
1257 |
visitor.examine(arc);
|
alpar@100
|
1258 |
}
|
alpar@100
|
1259 |
_Visitor& visitor;
|
alpar@100
|
1260 |
};
|
alpar@100
|
1261 |
};
|
alpar@100
|
1262 |
#endif
|
alpar@100
|
1263 |
|
alpar@100
|
1264 |
/// \brief Default traits class of BfsVisit class.
|
alpar@100
|
1265 |
///
|
alpar@100
|
1266 |
/// Default traits class of BfsVisit class.
|
kpeter@244
|
1267 |
/// \tparam _Digraph The type of the digraph the algorithm runs on.
|
alpar@100
|
1268 |
template<class _Digraph>
|
alpar@100
|
1269 |
struct BfsVisitDefaultTraits {
|
alpar@100
|
1270 |
|
kpeter@244
|
1271 |
/// \brief The type of the digraph the algorithm runs on.
|
alpar@100
|
1272 |
typedef _Digraph Digraph;
|
alpar@100
|
1273 |
|
alpar@100
|
1274 |
/// \brief The type of the map that indicates which nodes are reached.
|
alpar@209
|
1275 |
///
|
alpar@100
|
1276 |
/// The type of the map that indicates which nodes are reached.
|
kpeter@244
|
1277 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
|
alpar@100
|
1278 |
typedef typename Digraph::template NodeMap<bool> ReachedMap;
|
alpar@100
|
1279 |
|
kpeter@244
|
1280 |
/// \brief Instantiates a \ref ReachedMap.
|
alpar@100
|
1281 |
///
|
alpar@209
|
1282 |
/// This function instantiates a \ref ReachedMap.
|
alpar@100
|
1283 |
/// \param digraph is the digraph, to which
|
alpar@100
|
1284 |
/// we would like to define the \ref ReachedMap.
|
alpar@100
|
1285 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
alpar@100
|
1286 |
return new ReachedMap(digraph);
|
alpar@100
|
1287 |
}
|
alpar@100
|
1288 |
|
alpar@100
|
1289 |
};
|
alpar@100
|
1290 |
|
alpar@100
|
1291 |
/// \ingroup search
|
alpar@209
|
1292 |
///
|
kpeter@244
|
1293 |
/// \brief %BFS algorithm class with visitor interface.
|
alpar@209
|
1294 |
///
|
alpar@100
|
1295 |
/// This class provides an efficient implementation of the %BFS algorithm
|
alpar@100
|
1296 |
/// with visitor interface.
|
alpar@100
|
1297 |
///
|
alpar@100
|
1298 |
/// The %BfsVisit class provides an alternative interface to the Bfs
|
alpar@100
|
1299 |
/// class. It works with callback mechanism, the BfsVisit object calls
|
kpeter@244
|
1300 |
/// the member functions of the \c Visitor class on every BFS event.
|
alpar@100
|
1301 |
///
|
kpeter@252
|
1302 |
/// This interface of the BFS algorithm should be used in special cases
|
kpeter@252
|
1303 |
/// when extra actions have to be performed in connection with certain
|
kpeter@252
|
1304 |
/// events of the BFS algorithm. Otherwise consider to use Bfs or bfs()
|
kpeter@252
|
1305 |
/// instead.
|
kpeter@252
|
1306 |
///
|
kpeter@244
|
1307 |
/// \tparam _Digraph The type of the digraph the algorithm runs on.
|
alpar@210
|
1308 |
/// The default value is
|
kpeter@244
|
1309 |
/// \ref ListDigraph. The value of _Digraph is not used directly by
|
kpeter@244
|
1310 |
/// \ref BfsVisit, it is only passed to \ref BfsVisitDefaultTraits.
|
kpeter@244
|
1311 |
/// \tparam _Visitor The Visitor type that is used by the algorithm.
|
kpeter@244
|
1312 |
/// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty visitor, which
|
kpeter@244
|
1313 |
/// does not observe the BFS events. If you want to observe the BFS
|
kpeter@244
|
1314 |
/// events, you should implement your own visitor class.
|
alpar@209
|
1315 |
/// \tparam _Traits Traits class to set various data types used by the
|
alpar@100
|
1316 |
/// algorithm. The default traits class is
|
alpar@100
|
1317 |
/// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>".
|
alpar@100
|
1318 |
/// See \ref BfsVisitDefaultTraits for the documentation of
|
kpeter@244
|
1319 |
/// a BFS visit traits class.
|
alpar@100
|
1320 |
#ifdef DOXYGEN
|
alpar@100
|
1321 |
template <typename _Digraph, typename _Visitor, typename _Traits>
|
alpar@100
|
1322 |
#else
|
alpar@100
|
1323 |
template <typename _Digraph = ListDigraph,
|
alpar@209
|
1324 |
typename _Visitor = BfsVisitor<_Digraph>,
|
alpar@209
|
1325 |
typename _Traits = BfsDefaultTraits<_Digraph> >
|
alpar@100
|
1326 |
#endif
|
alpar@100
|
1327 |
class BfsVisit {
|
alpar@100
|
1328 |
public:
|
alpar@209
|
1329 |
|
alpar@100
|
1330 |
/// \brief \ref Exception for uninitialized parameters.
|
alpar@100
|
1331 |
///
|
alpar@100
|
1332 |
/// This error represents problems in the initialization
|
kpeter@244
|
1333 |
/// of the parameters of the algorithm.
|
alpar@100
|
1334 |
class UninitializedParameter : public lemon::UninitializedParameter {
|
alpar@100
|
1335 |
public:
|
alpar@209
|
1336 |
virtual const char* what() const throw()
|
alpar@100
|
1337 |
{
|
alpar@209
|
1338 |
return "lemon::BfsVisit::UninitializedParameter";
|
alpar@100
|
1339 |
}
|
alpar@100
|
1340 |
};
|
alpar@100
|
1341 |
|
kpeter@244
|
1342 |
///The traits class.
|
alpar@100
|
1343 |
typedef _Traits Traits;
|
alpar@100
|
1344 |
|
kpeter@244
|
1345 |
///The type of the digraph the algorithm runs on.
|
alpar@100
|
1346 |
typedef typename Traits::Digraph Digraph;
|
alpar@100
|
1347 |
|
kpeter@244
|
1348 |
///The visitor type used by the algorithm.
|
alpar@100
|
1349 |
typedef _Visitor Visitor;
|
alpar@100
|
1350 |
|
kpeter@244
|
1351 |
///The type of the map that indicates which nodes are reached.
|
alpar@100
|
1352 |
typedef typename Traits::ReachedMap ReachedMap;
|
alpar@100
|
1353 |
|
alpar@100
|
1354 |
private:
|
alpar@100
|
1355 |
|
alpar@100
|
1356 |
typedef typename Digraph::Node Node;
|
alpar@100
|
1357 |
typedef typename Digraph::NodeIt NodeIt;
|
alpar@100
|
1358 |
typedef typename Digraph::Arc Arc;
|
alpar@100
|
1359 |
typedef typename Digraph::OutArcIt OutArcIt;
|
alpar@100
|
1360 |
|
kpeter@244
|
1361 |
//Pointer to the underlying digraph.
|
alpar@100
|
1362 |
const Digraph *_digraph;
|
kpeter@244
|
1363 |
//Pointer to the visitor object.
|
alpar@100
|
1364 |
Visitor *_visitor;
|
kpeter@244
|
1365 |
//Pointer to the map of reached status of the nodes.
|
alpar@100
|
1366 |
ReachedMap *_reached;
|
kpeter@244
|
1367 |
//Indicates if _reached is locally allocated (true) or not.
|
alpar@100
|
1368 |
bool local_reached;
|
alpar@100
|
1369 |
|
alpar@100
|
1370 |
std::vector<typename Digraph::Node> _list;
|
alpar@100
|
1371 |
int _list_front, _list_back;
|
alpar@100
|
1372 |
|
kpeter@244
|
1373 |
///Creates the maps if necessary.
|
kpeter@244
|
1374 |
///\todo Better memory allocation (instead of new).
|
alpar@100
|
1375 |
void create_maps() {
|
alpar@100
|
1376 |
if(!_reached) {
|
alpar@209
|
1377 |
local_reached = true;
|
alpar@209
|
1378 |
_reached = Traits::createReachedMap(*_digraph);
|
alpar@100
|
1379 |
}
|
alpar@100
|
1380 |
}
|
alpar@100
|
1381 |
|
alpar@100
|
1382 |
protected:
|
alpar@100
|
1383 |
|
alpar@100
|
1384 |
BfsVisit() {}
|
alpar@209
|
1385 |
|
alpar@100
|
1386 |
public:
|
alpar@100
|
1387 |
|
alpar@100
|
1388 |
typedef BfsVisit Create;
|
alpar@100
|
1389 |
|
alpar@100
|
1390 |
/// \name Named template parameters
|
alpar@100
|
1391 |
|
alpar@100
|
1392 |
///@{
|
alpar@100
|
1393 |
template <class T>
|
kpeter@257
|
1394 |
struct SetReachedMapTraits : public Traits {
|
alpar@100
|
1395 |
typedef T ReachedMap;
|
alpar@100
|
1396 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
alpar@209
|
1397 |
throw UninitializedParameter();
|
alpar@100
|
1398 |
}
|
alpar@100
|
1399 |
};
|
alpar@209
|
1400 |
/// \brief \ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
1401 |
/// ReachedMap type.
|
alpar@100
|
1402 |
///
|
kpeter@244
|
1403 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type.
|
alpar@100
|
1404 |
template <class T>
|
kpeter@257
|
1405 |
struct SetReachedMap : public BfsVisit< Digraph, Visitor,
|
kpeter@257
|
1406 |
SetReachedMapTraits<T> > {
|
kpeter@257
|
1407 |
typedef BfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create;
|
alpar@100
|
1408 |
};
|
alpar@100
|
1409 |
///@}
|
alpar@100
|
1410 |
|
alpar@209
|
1411 |
public:
|
alpar@209
|
1412 |
|
alpar@100
|
1413 |
/// \brief Constructor.
|
alpar@100
|
1414 |
///
|
alpar@100
|
1415 |
/// Constructor.
|
alpar@100
|
1416 |
///
|
kpeter@244
|
1417 |
/// \param digraph The digraph the algorithm runs on.
|
kpeter@244
|
1418 |
/// \param visitor The visitor object of the algorithm.
|
alpar@209
|
1419 |
BfsVisit(const Digraph& digraph, Visitor& visitor)
|
alpar@100
|
1420 |
: _digraph(&digraph), _visitor(&visitor),
|
alpar@209
|
1421 |
_reached(0), local_reached(false) {}
|
alpar@209
|
1422 |
|
alpar@100
|
1423 |
/// \brief Destructor.
|
alpar@100
|
1424 |
~BfsVisit() {
|
alpar@100
|
1425 |
if(local_reached) delete _reached;
|
alpar@100
|
1426 |
}
|
alpar@100
|
1427 |
|
kpeter@244
|
1428 |
/// \brief Sets the map that indicates which nodes are reached.
|
alpar@100
|
1429 |
///
|
kpeter@244
|
1430 |
/// Sets the map that indicates which nodes are reached.
|
alpar@100
|
1431 |
/// If you don't use this function before calling \ref run(),
|
kpeter@244
|
1432 |
/// it will allocate one. The destructor deallocates this
|
alpar@100
|
1433 |
/// automatically allocated map, of course.
|
alpar@100
|
1434 |
/// \return <tt> (*this) </tt>
|
alpar@100
|
1435 |
BfsVisit &reachedMap(ReachedMap &m) {
|
alpar@100
|
1436 |
if(local_reached) {
|
alpar@209
|
1437 |
delete _reached;
|
alpar@209
|
1438 |
local_reached = false;
|
alpar@100
|
1439 |
}
|
alpar@100
|
1440 |
_reached = &m;
|
alpar@100
|
1441 |
return *this;
|
alpar@100
|
1442 |
}
|
alpar@100
|
1443 |
|
alpar@100
|
1444 |
public:
|
kpeter@244
|
1445 |
|
alpar@100
|
1446 |
/// \name Execution control
|
alpar@100
|
1447 |
/// The simplest way to execute the algorithm is to use
|
kpeter@244
|
1448 |
/// one of the member functions called \ref lemon::BfsVisit::run()
|
kpeter@244
|
1449 |
/// "run()".
|
alpar@100
|
1450 |
/// \n
|
kpeter@244
|
1451 |
/// If you need more control on the execution, first you must call
|
kpeter@244
|
1452 |
/// \ref lemon::BfsVisit::init() "init()", then you can add several
|
kpeter@244
|
1453 |
/// source nodes with \ref lemon::BfsVisit::addSource() "addSource()".
|
kpeter@244
|
1454 |
/// Finally \ref lemon::BfsVisit::start() "start()" will perform the
|
kpeter@244
|
1455 |
/// actual path computation.
|
alpar@100
|
1456 |
|
alpar@100
|
1457 |
/// @{
|
kpeter@244
|
1458 |
|
alpar@100
|
1459 |
/// \brief Initializes the internal data structures.
|
alpar@100
|
1460 |
///
|
alpar@100
|
1461 |
/// Initializes the internal data structures.
|
alpar@100
|
1462 |
void init() {
|
alpar@100
|
1463 |
create_maps();
|
alpar@100
|
1464 |
_list.resize(countNodes(*_digraph));
|
alpar@100
|
1465 |
_list_front = _list_back = -1;
|
alpar@100
|
1466 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
|
alpar@209
|
1467 |
_reached->set(u, false);
|
alpar@100
|
1468 |
}
|
alpar@100
|
1469 |
}
|
alpar@209
|
1470 |
|
alpar@100
|
1471 |
/// \brief Adds a new source node.
|
alpar@100
|
1472 |
///
|
alpar@100
|
1473 |
/// Adds a new source node to the set of nodes to be processed.
|
alpar@100
|
1474 |
void addSource(Node s) {
|
alpar@100
|
1475 |
if(!(*_reached)[s]) {
|
alpar@209
|
1476 |
_reached->set(s,true);
|
alpar@209
|
1477 |
_visitor->start(s);
|
alpar@209
|
1478 |
_visitor->reach(s);
|
alpar@100
|
1479 |
_list[++_list_back] = s;
|
alpar@209
|
1480 |
}
|
alpar@100
|
1481 |
}
|
alpar@209
|
1482 |
|
alpar@100
|
1483 |
/// \brief Processes the next node.
|
alpar@100
|
1484 |
///
|
alpar@100
|
1485 |
/// Processes the next node.
|
alpar@100
|
1486 |
///
|
alpar@100
|
1487 |
/// \return The processed node.
|
alpar@100
|
1488 |
///
|
kpeter@244
|
1489 |
/// \pre The queue must not be empty.
|
alpar@209
|
1490 |
Node processNextNode() {
|
alpar@100
|
1491 |
Node n = _list[++_list_front];
|
alpar@100
|
1492 |
_visitor->process(n);
|
alpar@100
|
1493 |
Arc e;
|
alpar@100
|
1494 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
|
alpar@100
|
1495 |
Node m = _digraph->target(e);
|
alpar@100
|
1496 |
if (!(*_reached)[m]) {
|
alpar@100
|
1497 |
_visitor->discover(e);
|
alpar@100
|
1498 |
_visitor->reach(m);
|
alpar@100
|
1499 |
_reached->set(m, true);
|
alpar@100
|
1500 |
_list[++_list_back] = m;
|
alpar@100
|
1501 |
} else {
|
alpar@100
|
1502 |
_visitor->examine(e);
|
alpar@100
|
1503 |
}
|
alpar@100
|
1504 |
}
|
alpar@100
|
1505 |
return n;
|
alpar@100
|
1506 |
}
|
alpar@100
|
1507 |
|
alpar@100
|
1508 |
/// \brief Processes the next node.
|
alpar@100
|
1509 |
///
|
kpeter@244
|
1510 |
/// Processes the next node and checks if the given target node
|
alpar@100
|
1511 |
/// is reached. If the target node is reachable from the processed
|
kpeter@244
|
1512 |
/// node, then the \c reach parameter will be set to \c true.
|
alpar@100
|
1513 |
///
|
alpar@100
|
1514 |
/// \param target The target node.
|
kpeter@244
|
1515 |
/// \retval reach Indicates if the target node is reached.
|
kpeter@244
|
1516 |
/// It should be initially \c false.
|
kpeter@244
|
1517 |
///
|
alpar@100
|
1518 |
/// \return The processed node.
|
alpar@100
|
1519 |
///
|
kpeter@244
|
1520 |
/// \pre The queue must not be empty.
|
alpar@100
|
1521 |
Node processNextNode(Node target, bool& reach) {
|
alpar@100
|
1522 |
Node n = _list[++_list_front];
|
alpar@100
|
1523 |
_visitor->process(n);
|
alpar@100
|
1524 |
Arc e;
|
alpar@100
|
1525 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
|
alpar@100
|
1526 |
Node m = _digraph->target(e);
|
alpar@100
|
1527 |
if (!(*_reached)[m]) {
|
alpar@100
|
1528 |
_visitor->discover(e);
|
alpar@100
|
1529 |
_visitor->reach(m);
|
alpar@100
|
1530 |
_reached->set(m, true);
|
alpar@100
|
1531 |
_list[++_list_back] = m;
|
alpar@100
|
1532 |
reach = reach || (target == m);
|
alpar@100
|
1533 |
} else {
|
alpar@100
|
1534 |
_visitor->examine(e);
|
alpar@100
|
1535 |
}
|
alpar@100
|
1536 |
}
|
alpar@100
|
1537 |
return n;
|
alpar@100
|
1538 |
}
|
alpar@100
|
1539 |
|
alpar@100
|
1540 |
/// \brief Processes the next node.
|
alpar@100
|
1541 |
///
|
kpeter@244
|
1542 |
/// Processes the next node and checks if at least one of reached
|
kpeter@244
|
1543 |
/// nodes has \c true value in the \c nm node map. If one node
|
kpeter@244
|
1544 |
/// with \c true value is reachable from the processed node, then the
|
kpeter@244
|
1545 |
/// \c rnode parameter will be set to the first of such nodes.
|
alpar@100
|
1546 |
///
|
kpeter@244
|
1547 |
/// \param nm A \c bool (or convertible) node map that indicates the
|
kpeter@244
|
1548 |
/// possible targets.
|
alpar@100
|
1549 |
/// \retval rnode The reached target node.
|
kpeter@244
|
1550 |
/// It should be initially \c INVALID.
|
kpeter@244
|
1551 |
///
|
alpar@100
|
1552 |
/// \return The processed node.
|
alpar@100
|
1553 |
///
|
kpeter@244
|
1554 |
/// \pre The queue must not be empty.
|
alpar@100
|
1555 |
template <typename NM>
|
alpar@100
|
1556 |
Node processNextNode(const NM& nm, Node& rnode) {
|
alpar@100
|
1557 |
Node n = _list[++_list_front];
|
alpar@100
|
1558 |
_visitor->process(n);
|
alpar@100
|
1559 |
Arc e;
|
alpar@100
|
1560 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
|
alpar@100
|
1561 |
Node m = _digraph->target(e);
|
alpar@100
|
1562 |
if (!(*_reached)[m]) {
|
alpar@100
|
1563 |
_visitor->discover(e);
|
alpar@100
|
1564 |
_visitor->reach(m);
|
alpar@100
|
1565 |
_reached->set(m, true);
|
alpar@100
|
1566 |
_list[++_list_back] = m;
|
alpar@100
|
1567 |
if (nm[m] && rnode == INVALID) rnode = m;
|
alpar@100
|
1568 |
} else {
|
alpar@100
|
1569 |
_visitor->examine(e);
|
alpar@100
|
1570 |
}
|
alpar@100
|
1571 |
}
|
alpar@100
|
1572 |
return n;
|
alpar@100
|
1573 |
}
|
alpar@100
|
1574 |
|
kpeter@244
|
1575 |
/// \brief The next node to be processed.
|
alpar@100
|
1576 |
///
|
kpeter@244
|
1577 |
/// Returns the next node to be processed or \c INVALID if the queue
|
kpeter@244
|
1578 |
/// is empty.
|
kpeter@244
|
1579 |
Node nextNode() const {
|
alpar@100
|
1580 |
return _list_front != _list_back ? _list[_list_front + 1] : INVALID;
|
alpar@100
|
1581 |
}
|
alpar@100
|
1582 |
|
alpar@100
|
1583 |
/// \brief Returns \c false if there are nodes
|
kpeter@244
|
1584 |
/// to be processed.
|
alpar@100
|
1585 |
///
|
alpar@100
|
1586 |
/// Returns \c false if there are nodes
|
kpeter@244
|
1587 |
/// to be processed in the queue.
|
kpeter@244
|
1588 |
bool emptyQueue() const { return _list_front == _list_back; }
|
alpar@100
|
1589 |
|
alpar@100
|
1590 |
/// \brief Returns the number of the nodes to be processed.
|
alpar@100
|
1591 |
///
|
alpar@100
|
1592 |
/// Returns the number of the nodes to be processed in the queue.
|
kpeter@244
|
1593 |
int queueSize() const { return _list_back - _list_front; }
|
alpar@209
|
1594 |
|
alpar@100
|
1595 |
/// \brief Executes the algorithm.
|
alpar@100
|
1596 |
///
|
alpar@100
|
1597 |
/// Executes the algorithm.
|
alpar@100
|
1598 |
///
|
kpeter@244
|
1599 |
/// This method runs the %BFS algorithm from the root node(s)
|
kpeter@244
|
1600 |
/// in order to compute the shortest path to each node.
|
kpeter@244
|
1601 |
///
|
kpeter@244
|
1602 |
/// The algorithm computes
|
kpeter@244
|
1603 |
/// - the shortest path tree (forest),
|
kpeter@244
|
1604 |
/// - the distance of each node from the root(s).
|
kpeter@244
|
1605 |
///
|
kpeter@244
|
1606 |
/// \pre init() must be called and at least one root node should be added
|
alpar@100
|
1607 |
/// with addSource() before using this function.
|
kpeter@244
|
1608 |
///
|
kpeter@244
|
1609 |
/// \note <tt>b.start()</tt> is just a shortcut of the following code.
|
kpeter@244
|
1610 |
/// \code
|
kpeter@244
|
1611 |
/// while ( !b.emptyQueue() ) {
|
kpeter@244
|
1612 |
/// b.processNextNode();
|
kpeter@244
|
1613 |
/// }
|
kpeter@244
|
1614 |
/// \endcode
|
alpar@100
|
1615 |
void start() {
|
alpar@100
|
1616 |
while ( !emptyQueue() ) processNextNode();
|
alpar@100
|
1617 |
}
|
alpar@209
|
1618 |
|
kpeter@244
|
1619 |
/// \brief Executes the algorithm until the given target node is reached.
|
alpar@100
|
1620 |
///
|
kpeter@244
|
1621 |
/// Executes the algorithm until the given target node is reached.
|
alpar@100
|
1622 |
///
|
kpeter@244
|
1623 |
/// This method runs the %BFS algorithm from the root node(s)
|
kpeter@244
|
1624 |
/// in order to compute the shortest path to \c dest.
|
kpeter@244
|
1625 |
///
|
kpeter@244
|
1626 |
/// The algorithm computes
|
kpeter@244
|
1627 |
/// - the shortest path to \c dest,
|
kpeter@244
|
1628 |
/// - the distance of \c dest from the root(s).
|
kpeter@244
|
1629 |
///
|
kpeter@244
|
1630 |
/// \pre init() must be called and at least one root node should be
|
kpeter@244
|
1631 |
/// added with addSource() before using this function.
|
kpeter@244
|
1632 |
///
|
kpeter@244
|
1633 |
/// \note <tt>b.start(t)</tt> is just a shortcut of the following code.
|
kpeter@244
|
1634 |
/// \code
|
kpeter@244
|
1635 |
/// bool reach = false;
|
kpeter@244
|
1636 |
/// while ( !b.emptyQueue() && !reach ) {
|
kpeter@244
|
1637 |
/// b.processNextNode(t, reach);
|
kpeter@244
|
1638 |
/// }
|
kpeter@244
|
1639 |
/// \endcode
|
alpar@100
|
1640 |
void start(Node dest) {
|
alpar@100
|
1641 |
bool reach = false;
|
alpar@100
|
1642 |
while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
|
alpar@100
|
1643 |
}
|
alpar@209
|
1644 |
|
alpar@100
|
1645 |
/// \brief Executes the algorithm until a condition is met.
|
alpar@100
|
1646 |
///
|
alpar@100
|
1647 |
/// Executes the algorithm until a condition is met.
|
alpar@100
|
1648 |
///
|
kpeter@244
|
1649 |
/// This method runs the %BFS algorithm from the root node(s) in
|
kpeter@244
|
1650 |
/// order to compute the shortest path to a node \c v with
|
kpeter@244
|
1651 |
/// <tt>nm[v]</tt> true, if such a node can be found.
|
alpar@100
|
1652 |
///
|
kpeter@244
|
1653 |
/// \param nm must be a bool (or convertible) node map. The
|
kpeter@244
|
1654 |
/// algorithm will stop when it reaches a node \c v with
|
alpar@100
|
1655 |
/// <tt>nm[v]</tt> true.
|
alpar@100
|
1656 |
///
|
kpeter@244
|
1657 |
/// \return The reached node \c v with <tt>nm[v]</tt> true or
|
kpeter@244
|
1658 |
/// \c INVALID if no such node was found.
|
kpeter@244
|
1659 |
///
|
kpeter@244
|
1660 |
/// \pre init() must be called and at least one root node should be
|
kpeter@244
|
1661 |
/// added with addSource() before using this function.
|
kpeter@244
|
1662 |
///
|
kpeter@244
|
1663 |
/// \note <tt>b.start(nm)</tt> is just a shortcut of the following code.
|
kpeter@244
|
1664 |
/// \code
|
kpeter@244
|
1665 |
/// Node rnode = INVALID;
|
kpeter@244
|
1666 |
/// while ( !b.emptyQueue() && rnode == INVALID ) {
|
kpeter@244
|
1667 |
/// b.processNextNode(nm, rnode);
|
kpeter@244
|
1668 |
/// }
|
kpeter@244
|
1669 |
/// return rnode;
|
kpeter@244
|
1670 |
/// \endcode
|
alpar@100
|
1671 |
template <typename NM>
|
alpar@100
|
1672 |
Node start(const NM &nm) {
|
alpar@100
|
1673 |
Node rnode = INVALID;
|
alpar@100
|
1674 |
while ( !emptyQueue() && rnode == INVALID ) {
|
alpar@209
|
1675 |
processNextNode(nm, rnode);
|
alpar@100
|
1676 |
}
|
alpar@100
|
1677 |
return rnode;
|
alpar@100
|
1678 |
}
|
alpar@100
|
1679 |
|
kpeter@244
|
1680 |
/// \brief Runs the algorithm from the given node.
|
alpar@100
|
1681 |
///
|
kpeter@244
|
1682 |
/// This method runs the %BFS algorithm from node \c s
|
kpeter@244
|
1683 |
/// in order to compute the shortest path to each node.
|
kpeter@244
|
1684 |
///
|
kpeter@244
|
1685 |
/// The algorithm computes
|
kpeter@244
|
1686 |
/// - the shortest path tree,
|
kpeter@244
|
1687 |
/// - the distance of each node from the root.
|
kpeter@244
|
1688 |
///
|
kpeter@244
|
1689 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code.
|
alpar@100
|
1690 |
///\code
|
alpar@100
|
1691 |
/// b.init();
|
alpar@100
|
1692 |
/// b.addSource(s);
|
alpar@100
|
1693 |
/// b.start();
|
alpar@100
|
1694 |
///\endcode
|
alpar@100
|
1695 |
void run(Node s) {
|
alpar@100
|
1696 |
init();
|
alpar@100
|
1697 |
addSource(s);
|
alpar@100
|
1698 |
start();
|
alpar@100
|
1699 |
}
|
alpar@100
|
1700 |
|
kpeter@244
|
1701 |
/// \brief Runs the algorithm to visit all nodes in the digraph.
|
alpar@209
|
1702 |
///
|
alpar@100
|
1703 |
/// This method runs the %BFS algorithm in order to
|
kpeter@244
|
1704 |
/// compute the shortest path to each node.
|
alpar@100
|
1705 |
///
|
kpeter@244
|
1706 |
/// The algorithm computes
|
kpeter@244
|
1707 |
/// - the shortest path tree (forest),
|
kpeter@244
|
1708 |
/// - the distance of each node from the root(s).
|
kpeter@244
|
1709 |
///
|
kpeter@244
|
1710 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code.
|
alpar@100
|
1711 |
///\code
|
alpar@100
|
1712 |
/// b.init();
|
kpeter@244
|
1713 |
/// for (NodeIt n(gr); n != INVALID; ++n) {
|
kpeter@244
|
1714 |
/// if (!b.reached(n)) {
|
kpeter@244
|
1715 |
/// b.addSource(n);
|
alpar@100
|
1716 |
/// b.start();
|
alpar@100
|
1717 |
/// }
|
alpar@100
|
1718 |
/// }
|
alpar@100
|
1719 |
///\endcode
|
alpar@100
|
1720 |
void run() {
|
alpar@100
|
1721 |
init();
|
alpar@100
|
1722 |
for (NodeIt it(*_digraph); it != INVALID; ++it) {
|
alpar@100
|
1723 |
if (!reached(it)) {
|
alpar@100
|
1724 |
addSource(it);
|
alpar@100
|
1725 |
start();
|
alpar@100
|
1726 |
}
|
alpar@100
|
1727 |
}
|
alpar@100
|
1728 |
}
|
kpeter@244
|
1729 |
|
alpar@100
|
1730 |
///@}
|
alpar@100
|
1731 |
|
alpar@100
|
1732 |
/// \name Query Functions
|
alpar@100
|
1733 |
/// The result of the %BFS algorithm can be obtained using these
|
alpar@100
|
1734 |
/// functions.\n
|
kpeter@244
|
1735 |
/// Either \ref lemon::BfsVisit::run() "run()" or
|
kpeter@244
|
1736 |
/// \ref lemon::BfsVisit::start() "start()" must be called before
|
kpeter@244
|
1737 |
/// using them.
|
alpar@100
|
1738 |
///@{
|
alpar@100
|
1739 |
|
kpeter@244
|
1740 |
/// \brief Checks if a node is reachable from the root(s).
|
alpar@100
|
1741 |
///
|
alpar@100
|
1742 |
/// Returns \c true if \c v is reachable from the root(s).
|
alpar@100
|
1743 |
/// \pre Either \ref run() or \ref start()
|
alpar@100
|
1744 |
/// must be called before using this function.
|
alpar@100
|
1745 |
bool reached(Node v) { return (*_reached)[v]; }
|
kpeter@244
|
1746 |
|
alpar@100
|
1747 |
///@}
|
kpeter@244
|
1748 |
|
alpar@100
|
1749 |
};
|
alpar@100
|
1750 |
|
alpar@100
|
1751 |
} //END OF NAMESPACE LEMON
|
alpar@100
|
1752 |
|
alpar@100
|
1753 |
#endif
|