Cross references turned off.
7 ///\brief %DFS algorithm.
9 ///\todo Revise Manual.
11 #include <hugo/bin_heap.h>
12 #include <hugo/invalid.h>
16 /// \addtogroup flowalgs
19 ///%DFS algorithm class.
21 ///This class provides an efficient implementation of %DFS algorithm.
23 ///\param GR The graph type the algorithm runs on.
25 ///\author Alpar Juttner
28 template <typename GR>
30 template <typename GR>
34 ///The type of the underlying graph.
37 typedef typename Graph::Node Node;
39 typedef typename Graph::NodeIt NodeIt;
41 typedef typename Graph::Edge Edge;
43 typedef typename Graph::OutEdgeIt OutEdgeIt;
45 ///\brief The type of the map that stores the last
46 ///edges of the paths on the %DFS tree.
47 typedef typename Graph::template NodeMap<Edge> PredMap;
48 ///\brief The type of the map that stores the last but one
49 ///nodes of the paths on the %DFS tree.
50 typedef typename Graph::template NodeMap<Node> PredNodeMap;
51 ///The type of the map that stores the dists of the nodes on the %DFS tree.
52 typedef typename Graph::template NodeMap<int> DistMap;
55 /// Pointer to the underlying graph.
57 ///Pointer to the map of predecessors edges.
59 ///Indicates if \ref predecessor is locally allocated (\c true) or not.
60 bool local_predecessor;
61 ///Pointer to the map of predecessors nodes.
62 PredNodeMap *pred_node;
63 ///Indicates if \ref pred_node is locally allocated (\c true) or not.
65 ///Pointer to the map of distances.
67 ///Indicates if \ref distance is locally allocated (\c true) or not.
70 ///The source node of the last execution.
74 ///Initializes the maps.
78 local_predecessor = true;
79 predecessor = new PredMap(*G);
82 local_pred_node = true;
83 pred_node = new PredNodeMap(*G);
86 local_distance = true;
87 distance = new DistMap(*G);
94 ///\param _G the graph the algorithm will run on.
95 Dfs(const Graph& _G) :
97 predecessor(NULL), local_predecessor(false),
98 pred_node(NULL), local_pred_node(false),
99 distance(NULL), local_distance(false)
105 if(local_predecessor) delete predecessor;
106 if(local_pred_node) delete pred_node;
107 if(local_distance) delete distance;
110 ///Sets the map storing the predecessor edges.
112 ///Sets the map storing the predecessor edges.
113 ///If you don't use this function before calling \ref run(),
114 ///it will allocate one. The destuctor deallocates this
115 ///automatically allocated map, of course.
116 ///\return <tt> (*this) </tt>
117 Dfs &setPredMap(PredMap &m)
119 if(local_predecessor) {
121 local_predecessor=false;
127 ///Sets the map storing the predecessor nodes.
129 ///Sets the map storing the predecessor nodes.
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 Dfs &setPredNodeMap(PredNodeMap &m)
136 if(local_pred_node) {
138 local_pred_node=false;
144 ///Sets the map storing the distances calculated by the algorithm.
146 ///Sets the map storing the distances calculated by the algorithm.
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 Dfs &setDistMap(DistMap &m)
155 local_distance=false;
161 ///Runs %DFS algorithm from node \c s.
163 ///This method runs the %DFS algorithm from a root node \c s
167 ///- the distance of each node from the root on this tree.
175 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
176 predecessor->set(u,INVALID);
177 pred_node->set(u,INVALID);
181 std::vector<typename Graph::OutEdgeIt> Q(N);
192 if((e=Q[Qh])!=INVALID)
193 if((m=G->head(e))!=s && (*predecessor)[m=G->head(e)]==INVALID) {
194 predecessor->set(m,e);
201 else if(--Qh>=0) n=G->tail(Q[Qh]);
205 ///The distance of a node from the root on the %DFS tree.
207 ///Returns the distance of a node from the root on the %DFS tree.
208 ///\pre \ref run() must be called before using this function.
209 ///\warning If node \c v in unreachable from the root the return value
210 ///of this funcion is undefined.
211 int dist(Node v) const { return (*distance)[v]; }
213 ///Returns the 'previous edge' of the %DFS path tree.
215 ///For a node \c v it returns the last edge of the path on the %DFS tree
216 ///from the root to \c
217 ///v. It is \ref INVALID
218 ///if \c v is unreachable from the root or if \c v=s. The
219 ///%DFS tree used here is equal to the %DFS tree used in
220 ///\ref predNode(Node v). \pre \ref run() must be called before using
222 Edge pred(Node v) const { return (*predecessor)[v]; }
224 ///Returns the 'previous node' of the %DFS tree.
226 ///For a node \c v it returns the 'previous node' on the %DFS tree,
227 ///i.e. it returns the last but one node of the path from the
228 ///root to \c /v on the %DFS tree.
229 ///It is INVALID if \c v is unreachable from the root or if
231 ///\pre \ref run() must be called before
232 ///using this function.
233 Node predNode(Node v) const { return (*pred_node)[v]; }
235 ///Returns a reference to the NodeMap of distances on the %DFS tree.
237 ///Returns a reference to the NodeMap of distances on the %DFS tree.
238 ///\pre \ref run() must
239 ///be called before using this function.
240 const DistMap &distMap() const { return *distance;}
242 ///Returns a reference to the %DFS tree map.
244 ///Returns a reference to the NodeMap of the edges of the
246 ///\pre \ref run() must be called before using this function.
247 const PredMap &predMap() const { return *predecessor;}
249 ///Returns a reference to the map of last but one nodes of the %DFS tree.
251 ///Returns a reference to the NodeMap of the last but one nodes of the paths
254 ///\pre \ref run() must be called before using this function.
255 const PredNodeMap &predNodeMap() const { return *pred_node;}
257 ///Checks if a node is reachable from the root.
259 ///Returns \c true if \c v is reachable from the root.
260 ///\note The root node is reported to be reached!
262 ///\pre \ref run() must be called before using this function.
264 bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
270 } //END OF NAMESPACE HUGO