alpar@906
|
1 |
/* -*- C++ -*-
|
alpar@921
|
2 |
* src/lemon/bfs.h - Part of LEMON, a generic C++ optimization library
|
alpar@906
|
3 |
*
|
alpar@1164
|
4 |
* Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@906
|
5 |
* (Egervary Combinatorial Optimization Research Group, EGRES).
|
alpar@906
|
6 |
*
|
alpar@906
|
7 |
* Permission to use, modify and distribute this software is granted
|
alpar@906
|
8 |
* provided that this copyright notice appears in all copies. For
|
alpar@906
|
9 |
* precise terms see the accompanying LICENSE file.
|
alpar@906
|
10 |
*
|
alpar@906
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@906
|
12 |
* express or implied, and with no claim as to its suitability for any
|
alpar@906
|
13 |
* purpose.
|
alpar@906
|
14 |
*
|
alpar@906
|
15 |
*/
|
alpar@906
|
16 |
|
alpar@921
|
17 |
#ifndef LEMON_BFS_H
|
alpar@921
|
18 |
#define LEMON_BFS_H
|
alpar@774
|
19 |
|
alpar@774
|
20 |
///\ingroup flowalgs
|
alpar@774
|
21 |
///\file
|
alpar@774
|
22 |
///\brief Bfs algorithm.
|
alpar@774
|
23 |
///
|
alpar@774
|
24 |
///\todo Revise Manual.
|
alpar@774
|
25 |
|
alpar@921
|
26 |
#include <lemon/bin_heap.h>
|
alpar@921
|
27 |
#include <lemon/invalid.h>
|
klao@977
|
28 |
#include <lemon/graph_utils.h>
|
alpar@774
|
29 |
|
alpar@921
|
30 |
namespace lemon {
|
alpar@774
|
31 |
|
alpar@774
|
32 |
/// \addtogroup flowalgs
|
alpar@774
|
33 |
/// @{
|
alpar@774
|
34 |
|
alpar@781
|
35 |
///%BFS algorithm class.
|
alpar@774
|
36 |
|
alpar@781
|
37 |
///This class provides an efficient implementation of %BFS algorithm.
|
alpar@781
|
38 |
///\param GR The graph type the algorithm runs on.
|
alpar@781
|
39 |
///This class does the same as Dijkstra does with constant 1 edge length,
|
alpar@781
|
40 |
///but it is faster.
|
alpar@774
|
41 |
///
|
alpar@781
|
42 |
///\author Alpar Juttner
|
alpar@774
|
43 |
|
alpar@774
|
44 |
#ifdef DOXYGEN
|
alpar@774
|
45 |
template <typename GR>
|
alpar@774
|
46 |
#else
|
alpar@774
|
47 |
template <typename GR>
|
alpar@774
|
48 |
#endif
|
alpar@774
|
49 |
class Bfs{
|
alpar@774
|
50 |
public:
|
alpar@774
|
51 |
///The type of the underlying graph.
|
alpar@774
|
52 |
typedef GR Graph;
|
alpar@911
|
53 |
///\e
|
alpar@774
|
54 |
typedef typename Graph::Node Node;
|
alpar@911
|
55 |
///\e
|
alpar@774
|
56 |
typedef typename Graph::NodeIt NodeIt;
|
alpar@911
|
57 |
///\e
|
alpar@774
|
58 |
typedef typename Graph::Edge Edge;
|
alpar@911
|
59 |
///\e
|
alpar@774
|
60 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
alpar@774
|
61 |
|
alpar@774
|
62 |
///\brief The type of the map that stores the last
|
alpar@774
|
63 |
///edges of the shortest paths.
|
alpar@774
|
64 |
typedef typename Graph::template NodeMap<Edge> PredMap;
|
alpar@774
|
65 |
///\brief The type of the map that stores the last but one
|
alpar@774
|
66 |
///nodes of the shortest paths.
|
alpar@774
|
67 |
typedef typename Graph::template NodeMap<Node> PredNodeMap;
|
alpar@774
|
68 |
///The type of the map that stores the dists of the nodes.
|
alpar@774
|
69 |
typedef typename Graph::template NodeMap<int> DistMap;
|
alpar@774
|
70 |
|
alpar@774
|
71 |
private:
|
alpar@802
|
72 |
/// Pointer to the underlying graph.
|
alpar@774
|
73 |
const Graph *G;
|
alpar@802
|
74 |
///Pointer to the map of predecessors edges.
|
alpar@774
|
75 |
PredMap *predecessor;
|
alpar@802
|
76 |
///Indicates if \ref predecessor is locally allocated (\c true) or not.
|
alpar@774
|
77 |
bool local_predecessor;
|
alpar@802
|
78 |
///Pointer to the map of predecessors nodes.
|
alpar@774
|
79 |
PredNodeMap *pred_node;
|
alpar@802
|
80 |
///Indicates if \ref pred_node is locally allocated (\c true) or not.
|
alpar@774
|
81 |
bool local_pred_node;
|
alpar@802
|
82 |
///Pointer to the map of distances.
|
alpar@774
|
83 |
DistMap *distance;
|
alpar@802
|
84 |
///Indicates if \ref distance is locally allocated (\c true) or not.
|
alpar@774
|
85 |
bool local_distance;
|
alpar@774
|
86 |
|
alpar@802
|
87 |
///The source node of the last execution.
|
alpar@774
|
88 |
Node source;
|
alpar@774
|
89 |
|
alpar@774
|
90 |
|
alpar@781
|
91 |
///Initializes the maps.
|
alpar@774
|
92 |
void init_maps()
|
alpar@774
|
93 |
{
|
alpar@774
|
94 |
if(!predecessor) {
|
alpar@774
|
95 |
local_predecessor = true;
|
alpar@774
|
96 |
predecessor = new PredMap(*G);
|
alpar@774
|
97 |
}
|
alpar@774
|
98 |
if(!pred_node) {
|
alpar@774
|
99 |
local_pred_node = true;
|
alpar@774
|
100 |
pred_node = new PredNodeMap(*G);
|
alpar@774
|
101 |
}
|
alpar@774
|
102 |
if(!distance) {
|
alpar@774
|
103 |
local_distance = true;
|
alpar@774
|
104 |
distance = new DistMap(*G);
|
alpar@774
|
105 |
}
|
alpar@774
|
106 |
}
|
alpar@774
|
107 |
|
alpar@774
|
108 |
public :
|
alpar@802
|
109 |
///Constructor.
|
alpar@802
|
110 |
|
alpar@802
|
111 |
///\param _G the graph the algorithm will run on.
|
alpar@911
|
112 |
///
|
alpar@774
|
113 |
Bfs(const Graph& _G) :
|
alpar@774
|
114 |
G(&_G),
|
alpar@774
|
115 |
predecessor(NULL), local_predecessor(false),
|
alpar@774
|
116 |
pred_node(NULL), local_pred_node(false),
|
alpar@774
|
117 |
distance(NULL), local_distance(false)
|
alpar@774
|
118 |
{ }
|
alpar@774
|
119 |
|
alpar@802
|
120 |
///Destructor.
|
alpar@774
|
121 |
~Bfs()
|
alpar@774
|
122 |
{
|
alpar@774
|
123 |
if(local_predecessor) delete predecessor;
|
alpar@774
|
124 |
if(local_pred_node) delete pred_node;
|
alpar@774
|
125 |
if(local_distance) delete distance;
|
alpar@774
|
126 |
}
|
alpar@774
|
127 |
|
alpar@774
|
128 |
///Sets the map storing the predecessor edges.
|
alpar@774
|
129 |
|
alpar@774
|
130 |
///Sets the map storing the predecessor edges.
|
alpar@774
|
131 |
///If you don't use this function before calling \ref run(),
|
alpar@774
|
132 |
///it will allocate one. The destuctor deallocates this
|
alpar@774
|
133 |
///automatically allocated map, of course.
|
alpar@774
|
134 |
///\return <tt> (*this) </tt>
|
alpar@774
|
135 |
Bfs &setPredMap(PredMap &m)
|
alpar@774
|
136 |
{
|
alpar@774
|
137 |
if(local_predecessor) {
|
alpar@774
|
138 |
delete predecessor;
|
alpar@774
|
139 |
local_predecessor=false;
|
alpar@774
|
140 |
}
|
alpar@774
|
141 |
predecessor = &m;
|
alpar@774
|
142 |
return *this;
|
alpar@774
|
143 |
}
|
alpar@774
|
144 |
|
alpar@774
|
145 |
///Sets the map storing the predecessor nodes.
|
alpar@774
|
146 |
|
alpar@774
|
147 |
///Sets the map storing the predecessor nodes.
|
alpar@774
|
148 |
///If you don't use this function before calling \ref run(),
|
alpar@774
|
149 |
///it will allocate one. The destuctor deallocates this
|
alpar@774
|
150 |
///automatically allocated map, of course.
|
alpar@774
|
151 |
///\return <tt> (*this) </tt>
|
alpar@774
|
152 |
Bfs &setPredNodeMap(PredNodeMap &m)
|
alpar@774
|
153 |
{
|
alpar@774
|
154 |
if(local_pred_node) {
|
alpar@774
|
155 |
delete pred_node;
|
alpar@774
|
156 |
local_pred_node=false;
|
alpar@774
|
157 |
}
|
alpar@774
|
158 |
pred_node = &m;
|
alpar@774
|
159 |
return *this;
|
alpar@774
|
160 |
}
|
alpar@774
|
161 |
|
alpar@774
|
162 |
///Sets the map storing the distances calculated by the algorithm.
|
alpar@774
|
163 |
|
alpar@774
|
164 |
///Sets the map storing the distances calculated by the algorithm.
|
alpar@774
|
165 |
///If you don't use this function before calling \ref run(),
|
alpar@774
|
166 |
///it will allocate one. The destuctor deallocates this
|
alpar@774
|
167 |
///automatically allocated map, of course.
|
alpar@774
|
168 |
///\return <tt> (*this) </tt>
|
alpar@774
|
169 |
Bfs &setDistMap(DistMap &m)
|
alpar@774
|
170 |
{
|
alpar@774
|
171 |
if(local_distance) {
|
alpar@774
|
172 |
delete distance;
|
alpar@774
|
173 |
local_distance=false;
|
alpar@774
|
174 |
}
|
alpar@774
|
175 |
distance = &m;
|
alpar@774
|
176 |
return *this;
|
alpar@774
|
177 |
}
|
alpar@774
|
178 |
|
alpar@774
|
179 |
///Runs %BFS algorithm from node \c s.
|
alpar@774
|
180 |
|
alpar@774
|
181 |
///This method runs the %BFS algorithm from a root node \c s
|
alpar@774
|
182 |
///in order to
|
alpar@781
|
183 |
///compute a
|
alpar@774
|
184 |
///shortest path to each node. The algorithm computes
|
alpar@781
|
185 |
///- The %BFS tree.
|
alpar@774
|
186 |
///- The distance of each node from the root.
|
alpar@774
|
187 |
|
alpar@774
|
188 |
void run(Node s) {
|
alpar@774
|
189 |
|
alpar@774
|
190 |
init_maps();
|
alpar@774
|
191 |
|
alpar@774
|
192 |
source = s;
|
alpar@774
|
193 |
|
alpar@774
|
194 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
|
alpar@774
|
195 |
predecessor->set(u,INVALID);
|
alpar@774
|
196 |
pred_node->set(u,INVALID);
|
alpar@774
|
197 |
}
|
alpar@774
|
198 |
|
klao@946
|
199 |
int N = countNodes(*G);
|
alpar@774
|
200 |
std::vector<typename Graph::Node> Q(N);
|
alpar@774
|
201 |
int Qh=0;
|
alpar@774
|
202 |
int Qt=0;
|
alpar@774
|
203 |
|
alpar@774
|
204 |
Q[Qh++]=source;
|
alpar@774
|
205 |
distance->set(s, 0);
|
alpar@774
|
206 |
do {
|
alpar@774
|
207 |
Node m;
|
alpar@774
|
208 |
Node n=Q[Qt++];
|
alpar@774
|
209 |
int d= (*distance)[n]+1;
|
alpar@774
|
210 |
|
alpar@774
|
211 |
for(OutEdgeIt e(*G,n);e!=INVALID;++e)
|
alpar@986
|
212 |
if((m=G->target(e))!=s && (*predecessor)[m]==INVALID) {
|
alpar@774
|
213 |
Q[Qh++]=m;
|
alpar@774
|
214 |
predecessor->set(m,e);
|
alpar@774
|
215 |
pred_node->set(m,n);
|
alpar@774
|
216 |
distance->set(m,d);
|
alpar@774
|
217 |
}
|
alpar@774
|
218 |
} while(Qt!=Qh);
|
alpar@774
|
219 |
}
|
alpar@774
|
220 |
|
alpar@774
|
221 |
///The distance of a node from the root.
|
alpar@774
|
222 |
|
alpar@774
|
223 |
///Returns the distance of a node from the root.
|
alpar@774
|
224 |
///\pre \ref run() must be called before using this function.
|
alpar@774
|
225 |
///\warning If node \c v in unreachable from the root the return value
|
alpar@774
|
226 |
///of this funcion is undefined.
|
alpar@774
|
227 |
int dist(Node v) const { return (*distance)[v]; }
|
alpar@774
|
228 |
|
alpar@781
|
229 |
///Returns the 'previous edge' of the %BFS path tree.
|
alpar@774
|
230 |
|
alpar@781
|
231 |
///For a node \c v it returns the 'previous edge' of the %BFS tree,
|
alpar@781
|
232 |
///i.e. it returns the last edge of a shortest path from the root to \c
|
alpar@774
|
233 |
///v. It is \ref INVALID
|
alpar@774
|
234 |
///if \c v is unreachable from the root or if \c v=s. The
|
alpar@781
|
235 |
///%BFS tree used here is equal to the %BFS tree used in
|
alpar@774
|
236 |
///\ref predNode(Node v). \pre \ref run() must be called before using
|
alpar@774
|
237 |
///this function.
|
alpar@774
|
238 |
Edge pred(Node v) const { return (*predecessor)[v]; }
|
alpar@774
|
239 |
|
alpar@781
|
240 |
///Returns the 'previous node' of the %BFS tree.
|
alpar@774
|
241 |
|
alpar@781
|
242 |
///For a node \c v it returns the 'previous node' on the %BFS tree,
|
alpar@774
|
243 |
///i.e. it returns the last but one node from a shortest path from the
|
alpar@774
|
244 |
///root to \c /v. It is INVALID if \c v is unreachable from the root or if
|
alpar@781
|
245 |
///\c v=s. The shortest path tree used here is equal to the %BFS
|
alpar@774
|
246 |
///tree used in \ref pred(Node v). \pre \ref run() must be called before
|
alpar@774
|
247 |
///using this function.
|
alpar@774
|
248 |
Node predNode(Node v) const { return (*pred_node)[v]; }
|
alpar@774
|
249 |
|
alpar@774
|
250 |
///Returns a reference to the NodeMap of distances.
|
alpar@774
|
251 |
|
alpar@774
|
252 |
///Returns a reference to the NodeMap of distances. \pre \ref run() must
|
alpar@774
|
253 |
///be called before using this function.
|
alpar@774
|
254 |
const DistMap &distMap() const { return *distance;}
|
alpar@774
|
255 |
|
alpar@781
|
256 |
///Returns a reference to the %BFS tree map.
|
alpar@774
|
257 |
|
alpar@774
|
258 |
///Returns a reference to the NodeMap of the edges of the
|
alpar@781
|
259 |
///%BFS tree.
|
alpar@774
|
260 |
///\pre \ref run() must be called before using this function.
|
alpar@774
|
261 |
const PredMap &predMap() const { return *predecessor;}
|
alpar@774
|
262 |
|
alpar@781
|
263 |
///Returns a reference to the map of last but one nodes of shortest paths.
|
alpar@774
|
264 |
|
alpar@781
|
265 |
///Returns a reference to the NodeMap of the last but one nodes on the
|
alpar@781
|
266 |
///%BFS tree.
|
alpar@774
|
267 |
///\pre \ref run() must be called before using this function.
|
alpar@774
|
268 |
const PredNodeMap &predNodeMap() const { return *pred_node;}
|
alpar@774
|
269 |
|
alpar@774
|
270 |
///Checks if a node is reachable from the root.
|
alpar@774
|
271 |
|
alpar@774
|
272 |
///Returns \c true if \c v is reachable from the root.
|
alpar@802
|
273 |
///\note The root node is reported to be reached!
|
alpar@774
|
274 |
///
|
alpar@774
|
275 |
///\pre \ref run() must be called before using this function.
|
alpar@774
|
276 |
///
|
alpar@780
|
277 |
bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
|
alpar@774
|
278 |
|
alpar@774
|
279 |
};
|
alpar@774
|
280 |
|
alpar@774
|
281 |
/// @}
|
alpar@774
|
282 |
|
alpar@921
|
283 |
} //END OF NAMESPACE LEMON
|
alpar@774
|
284 |
|
alpar@774
|
285 |
#endif
|
alpar@774
|
286 |
|
alpar@774
|
287 |
|