COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/hugo/bfs.h @ 911:89a4fbb99cad

Last change on this file since 911:89a4fbb99cad was 911:89a4fbb99cad, checked in by Alpar Juttner, 20 years ago

Fix many doxygen command bugs.

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