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