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