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.
36 typedef typename Graph::Node Node;
37 typedef typename Graph::NodeIt NodeIt;
38 typedef typename Graph::Edge Edge;
39 typedef typename Graph::OutEdgeIt OutEdgeIt;
41 ///\brief The type of the map that stores the last
42 ///edges of the paths on the %DFS tree.
43 typedef typename Graph::template NodeMap<Edge> PredMap;
44 ///\brief The type of the map that stores the last but one
45 ///nodes of the paths on the %DFS tree.
46 typedef typename Graph::template NodeMap<Node> PredNodeMap;
47 ///The type of the map that stores the dists of the nodes on the %DFS tree.
48 typedef typename Graph::template NodeMap<int> DistMap;
53 bool local_predecessor;
54 PredNodeMap *pred_node;
59 //The source node of the last execution.
63 ///Initializes the maps.
67 local_predecessor = true;
68 predecessor = new PredMap(*G);
71 local_pred_node = true;
72 pred_node = new PredNodeMap(*G);
75 local_distance = true;
76 distance = new DistMap(*G);
81 Dfs(const Graph& _G) :
83 predecessor(NULL), local_predecessor(false),
84 pred_node(NULL), local_pred_node(false),
85 distance(NULL), local_distance(false)
90 if(local_predecessor) delete predecessor;
91 if(local_pred_node) delete pred_node;
92 if(local_distance) delete distance;
95 ///Sets the graph the algorithm will run on.
97 ///Sets the graph the algorithm will run on.
98 ///\return <tt> (*this) </tt>
99 ///\bug What about maps?
100 ///\todo It may be unnecessary
101 Dfs &setGraph(const Graph &_G)
106 ///Sets the map storing the predecessor edges.
108 ///Sets the map storing the predecessor edges.
109 ///If you don't use this function before calling \ref run(),
110 ///it will allocate one. The destuctor deallocates this
111 ///automatically allocated map, of course.
112 ///\return <tt> (*this) </tt>
113 Dfs &setPredMap(PredMap &m)
115 if(local_predecessor) {
117 local_predecessor=false;
123 ///Sets the map storing the predecessor nodes.
125 ///Sets the map storing the predecessor nodes.
126 ///If you don't use this function before calling \ref run(),
127 ///it will allocate one. The destuctor deallocates this
128 ///automatically allocated map, of course.
129 ///\return <tt> (*this) </tt>
130 Dfs &setPredNodeMap(PredNodeMap &m)
132 if(local_pred_node) {
134 local_pred_node=false;
140 ///Sets the map storing the distances calculated by the algorithm.
142 ///Sets the map storing the distances calculated by the algorithm.
143 ///If you don't use this function before calling \ref run(),
144 ///it will allocate one. The destuctor deallocates this
145 ///automatically allocated map, of course.
146 ///\return <tt> (*this) </tt>
147 Dfs &setDistMap(DistMap &m)
151 local_distance=false;
157 ///Runs %DFS algorithm from node \c s.
159 ///This method runs the %DFS algorithm from a root node \c s
163 ///- the distance of each node from the root on this tree.
171 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
172 predecessor->set(u,INVALID);
173 pred_node->set(u,INVALID);
177 std::vector<typename Graph::OutEdgeIt> Q(N);
188 if((e=Q[Qh])!=INVALID)
189 if((m=G->head(e))!=s && (*predecessor)[m=G->head(e)]==INVALID) {
190 predecessor->set(m,e);
197 else if(--Qh>=0) n=G->tail(Q[Qh]);
201 ///The distance of a node from the root on the %DFS tree.
203 ///Returns the distance of a node from the root on the %DFS tree.
204 ///\pre \ref run() must be called before using this function.
205 ///\warning If node \c v in unreachable from the root the return value
206 ///of this funcion is undefined.
207 int dist(Node v) const { return (*distance)[v]; }
209 ///Returns the 'previous edge' of the %DFS path tree.
211 ///For a node \c v it returns the last edge of the path on the %DFS tree
212 ///from the root to \c
213 ///v. It is \ref INVALID
214 ///if \c v is unreachable from the root or if \c v=s. The
215 ///%DFS tree used here is equal to the %DFS tree used in
216 ///\ref predNode(Node v). \pre \ref run() must be called before using
218 Edge pred(Node v) const { return (*predecessor)[v]; }
220 ///Returns the 'previous node' of the %DFS tree.
222 ///For a node \c v it returns the 'previous node' on the %DFS tree,
223 ///i.e. it returns the last but one node of the path from the
224 ///root to \c /v on the %DFS tree.
225 ///It is INVALID if \c v is unreachable from the root or if
227 ///\pre \ref run() must be called before
228 ///using this function.
229 Node predNode(Node v) const { return (*pred_node)[v]; }
231 ///Returns a reference to the NodeMap of distances on the %DFS tree.
233 ///Returns a reference to the NodeMap of distances on the %DFS tree.
234 ///\pre \ref run() must
235 ///be called before using this function.
236 const DistMap &distMap() const { return *distance;}
238 ///Returns a reference to the %DFS tree map.
240 ///Returns a reference to the NodeMap of the edges of the
242 ///\pre \ref run() must be called before using this function.
243 const PredMap &predMap() const { return *predecessor;}
245 ///Returns a reference to the map of last but one nodes of the %DFS tree.
247 ///Returns a reference to the NodeMap of the last but one nodes of the paths
250 ///\pre \ref run() must be called before using this function.
251 const PredNodeMap &predNodeMap() const { return *pred_node;}
253 ///Checks if a node is reachable from the root.
255 ///Returns \c true if \c v is reachable from the root.
256 ///\warning The root node is reported to be reached!
258 ///\pre \ref run() must be called before using this function.
260 bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
266 } //END OF NAMESPACE HUGO