Fix a bug noticed by deba.
2 * src/lemon/bfs.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
22 ///\brief Bfs algorithm.
24 ///\todo Revise Manual.
26 #include <lemon/bin_heap.h>
27 #include <lemon/invalid.h>
28 #include <lemon/graph_utils.h>
32 /// \addtogroup flowalgs
35 ///%BFS algorithm class.
37 ///This class provides an efficient implementation of %BFS algorithm.
38 ///\param GR The graph type the algorithm runs on.
39 ///This class does the same as Dijkstra does with constant 1 edge length,
42 ///\author Alpar Juttner
45 template <typename GR>
47 template <typename GR>
51 ///The type of the underlying graph.
54 typedef typename Graph::Node Node;
56 typedef typename Graph::NodeIt NodeIt;
58 typedef typename Graph::Edge Edge;
60 typedef typename Graph::OutEdgeIt OutEdgeIt;
62 ///\brief The type of the map that stores the last
63 ///edges of the shortest paths.
64 typedef typename Graph::template NodeMap<Edge> PredMap;
65 ///\brief The type of the map that stores the last but one
66 ///nodes of the shortest paths.
67 typedef typename Graph::template NodeMap<Node> PredNodeMap;
68 ///The type of the map that stores the dists of the nodes.
69 typedef typename Graph::template NodeMap<int> DistMap;
72 /// Pointer to the underlying graph.
74 ///Pointer to the map of predecessors edges.
76 ///Indicates if \ref predecessor is locally allocated (\c true) or not.
77 bool local_predecessor;
78 ///Pointer to the map of predecessors nodes.
79 PredNodeMap *pred_node;
80 ///Indicates if \ref pred_node is locally allocated (\c true) or not.
82 ///Pointer to the map of distances.
84 ///Indicates if \ref distance is locally allocated (\c true) or not.
87 ///The source node of the last execution.
91 ///Initializes the maps.
95 local_predecessor = true;
96 predecessor = new PredMap(*G);
99 local_pred_node = true;
100 pred_node = new PredNodeMap(*G);
103 local_distance = true;
104 distance = new DistMap(*G);
111 ///\param _G the graph the algorithm will run on.
113 Bfs(const Graph& _G) :
115 predecessor(NULL), local_predecessor(false),
116 pred_node(NULL), local_pred_node(false),
117 distance(NULL), local_distance(false)
123 if(local_predecessor) delete predecessor;
124 if(local_pred_node) delete pred_node;
125 if(local_distance) delete distance;
128 ///Sets the map storing the predecessor edges.
130 ///Sets the map storing the predecessor edges.
131 ///If you don't use this function before calling \ref run(),
132 ///it will allocate one. The destuctor deallocates this
133 ///automatically allocated map, of course.
134 ///\return <tt> (*this) </tt>
135 Bfs &setPredMap(PredMap &m)
137 if(local_predecessor) {
139 local_predecessor=false;
145 ///Sets the map storing the predecessor nodes.
147 ///Sets the map storing the predecessor nodes.
148 ///If you don't use this function before calling \ref run(),
149 ///it will allocate one. The destuctor deallocates this
150 ///automatically allocated map, of course.
151 ///\return <tt> (*this) </tt>
152 Bfs &setPredNodeMap(PredNodeMap &m)
154 if(local_pred_node) {
156 local_pred_node=false;
162 ///Sets the map storing the distances calculated by the algorithm.
164 ///Sets the map storing the distances calculated by the algorithm.
165 ///If you don't use this function before calling \ref run(),
166 ///it will allocate one. The destuctor deallocates this
167 ///automatically allocated map, of course.
168 ///\return <tt> (*this) </tt>
169 Bfs &setDistMap(DistMap &m)
173 local_distance=false;
179 ///Runs %BFS algorithm from node \c s.
181 ///This method runs the %BFS algorithm from a root node \c s
184 ///shortest path to each node. The algorithm computes
186 ///- The distance of each node from the root.
194 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
195 predecessor->set(u,INVALID);
196 pred_node->set(u,INVALID);
199 int N = countNodes(*G);
200 std::vector<typename Graph::Node> Q(N);
209 int d= (*distance)[n]+1;
211 for(OutEdgeIt e(*G,n);e!=INVALID;++e)
212 if((m=G->target(e))!=s && (*predecessor)[m]==INVALID) {
214 predecessor->set(m,e);
221 ///The distance of a node from the root.
223 ///Returns the distance of a node from the root.
224 ///\pre \ref run() must be called before using this function.
225 ///\warning If node \c v in unreachable from the root the return value
226 ///of this funcion is undefined.
227 int dist(Node v) const { return (*distance)[v]; }
229 ///Returns the 'previous edge' of the %BFS path tree.
231 ///For a node \c v it returns the 'previous edge' of the %BFS tree,
232 ///i.e. it returns the last edge of a shortest path from the root to \c
233 ///v. It is \ref INVALID
234 ///if \c v is unreachable from the root or if \c v=s. The
235 ///%BFS tree used here is equal to the %BFS tree used in
236 ///\ref predNode(Node v). \pre \ref run() must be called before using
238 Edge pred(Node v) const { return (*predecessor)[v]; }
240 ///Returns the 'previous node' of the %BFS tree.
242 ///For a node \c v it returns the 'previous node' on the %BFS tree,
243 ///i.e. it returns the last but one node from a shortest path from the
244 ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
245 ///\c v=s. The shortest path tree used here is equal to the %BFS
246 ///tree used in \ref pred(Node v). \pre \ref run() must be called before
247 ///using this function.
248 Node predNode(Node v) const { return (*pred_node)[v]; }
250 ///Returns a reference to the NodeMap of distances.
252 ///Returns a reference to the NodeMap of distances. \pre \ref run() must
253 ///be called before using this function.
254 const DistMap &distMap() const { return *distance;}
256 ///Returns a reference to the %BFS tree map.
258 ///Returns a reference to the NodeMap of the edges of the
260 ///\pre \ref run() must be called before using this function.
261 const PredMap &predMap() const { return *predecessor;}
263 ///Returns a reference to the map of last but one nodes of shortest paths.
265 ///Returns a reference to the NodeMap of the last but one nodes on the
267 ///\pre \ref run() must be called before using this function.
268 const PredNodeMap &predNodeMap() const { return *pred_node;}
270 ///Checks if a node is reachable from the root.
272 ///Returns \c true if \c v is reachable from the root.
273 ///\note The root node is reported to be reached!
275 ///\pre \ref run() must be called before using this function.
277 bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
283 } //END OF NAMESPACE LEMON