1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BIN_HEAP_H |
20 | 20 |
#define LEMON_BIN_HEAP_H |
21 | 21 |
|
22 | 22 |
///\ingroup auxdat |
23 | 23 |
///\file |
24 | 24 |
///\brief Binary Heap implementation. |
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
#include <utility> |
28 | 28 |
#include <functional> |
29 | 29 |
|
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 | 32 |
///\ingroup auxdat |
33 | 33 |
/// |
34 | 34 |
///\brief A Binary Heap implementation. |
35 | 35 |
/// |
36 | 36 |
///This class implements the \e binary \e heap data structure. |
37 | 37 |
/// |
38 | 38 |
///A \e heap is a data structure for storing items with specified values |
39 | 39 |
///called \e priorities in such a way that finding the item with minimum |
40 | 40 |
///priority is efficient. \c Comp specifies the ordering of the priorities. |
41 | 41 |
///In a heap one can change the priority of an item, add or erase an |
42 | 42 |
///item, etc. |
43 | 43 |
/// |
44 | 44 |
///\tparam PR Type of the priority of the items. |
45 | 45 |
///\tparam IM A read and writable item map with int values, used internally |
46 | 46 |
///to handle the cross references. |
47 | 47 |
///\tparam Comp A functor class for the ordering of the priorities. |
48 | 48 |
///The default is \c std::less<PR>. |
49 | 49 |
/// |
50 | 50 |
///\sa FibHeap |
51 | 51 |
///\sa Dijkstra |
52 | 52 |
template <typename PR, typename IM, typename Comp = std::less<PR> > |
53 | 53 |
class BinHeap { |
54 | 54 |
|
55 | 55 |
public: |
56 | 56 |
///\e |
57 | 57 |
typedef IM ItemIntMap; |
58 | 58 |
///\e |
59 | 59 |
typedef PR Prio; |
60 | 60 |
///\e |
61 | 61 |
typedef typename ItemIntMap::Key Item; |
62 | 62 |
///\e |
63 | 63 |
typedef std::pair<Item,Prio> Pair; |
64 | 64 |
///\e |
65 | 65 |
typedef Comp Compare; |
66 | 66 |
|
67 | 67 |
/// \brief Type to represent the items states. |
68 | 68 |
/// |
69 | 69 |
/// Each Item element have a state associated to it. It may be "in heap", |
70 | 70 |
/// "pre heap" or "post heap". The latter two are indifferent from the |
71 | 71 |
/// heap's point of view, but may be useful to the user. |
72 | 72 |
/// |
73 | 73 |
/// The item-int map must be initialized in such way that it assigns |
74 | 74 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
75 | 75 |
enum State { |
76 |
IN_HEAP = 0, ///< \e |
|
77 |
PRE_HEAP = -1, ///< \e |
|
78 |
|
|
76 |
IN_HEAP = 0, ///< = 0. |
|
77 |
PRE_HEAP = -1, ///< = -1. |
|
78 |
POST_HEAP = -2 ///< = -2. |
|
79 | 79 |
}; |
80 | 80 |
|
81 | 81 |
private: |
82 | 82 |
std::vector<Pair> _data; |
83 | 83 |
Compare _comp; |
84 | 84 |
ItemIntMap &_iim; |
85 | 85 |
|
86 | 86 |
public: |
87 | 87 |
/// \brief The constructor. |
88 | 88 |
/// |
89 | 89 |
/// The constructor. |
90 | 90 |
/// \param map should be given to the constructor, since it is used |
91 | 91 |
/// internally to handle the cross references. The value of the map |
92 | 92 |
/// must be \c PRE_HEAP (<tt>-1</tt>) for every item. |
93 | 93 |
explicit BinHeap(ItemIntMap &map) : _iim(map) {} |
94 | 94 |
|
95 | 95 |
/// \brief The constructor. |
96 | 96 |
/// |
97 | 97 |
/// The constructor. |
98 | 98 |
/// \param map should be given to the constructor, since it is used |
99 | 99 |
/// internally to handle the cross references. The value of the map |
100 | 100 |
/// should be PRE_HEAP (-1) for each element. |
101 | 101 |
/// |
102 | 102 |
/// \param comp The comparator function object. |
103 | 103 |
BinHeap(ItemIntMap &map, const Compare &comp) |
104 | 104 |
: _iim(map), _comp(comp) {} |
105 | 105 |
|
106 | 106 |
|
107 | 107 |
/// The number of items stored in the heap. |
108 | 108 |
/// |
109 | 109 |
/// \brief Returns the number of items stored in the heap. |
110 | 110 |
int size() const { return _data.size(); } |
111 | 111 |
|
112 | 112 |
/// \brief Checks if the heap stores no items. |
113 | 113 |
/// |
114 | 114 |
/// Returns \c true if and only if the heap stores no items. |
115 | 115 |
bool empty() const { return _data.empty(); } |
116 | 116 |
|
117 | 117 |
/// \brief Make empty this heap. |
118 | 118 |
/// |
119 | 119 |
/// Make empty this heap. It does not change the cross reference map. |
120 | 120 |
/// If you want to reuse what is not surely empty you should first clear |
121 | 121 |
/// the heap and after that you should set the cross reference map for |
122 | 122 |
/// each item to \c PRE_HEAP. |
123 | 123 |
void clear() { |
124 | 124 |
_data.clear(); |
125 | 125 |
} |
126 | 126 |
|
127 | 127 |
private: |
128 | 128 |
static int parent(int i) { return (i-1)/2; } |
129 | 129 |
|
130 | 130 |
static int second_child(int i) { return 2*i+2; } |
131 | 131 |
bool less(const Pair &p1, const Pair &p2) const { |
132 | 132 |
return _comp(p1.second, p2.second); |
133 | 133 |
} |
134 | 134 |
|
135 | 135 |
int bubble_up(int hole, Pair p) { |
136 | 136 |
int par = parent(hole); |
137 | 137 |
while( hole>0 && less(p,_data[par]) ) { |
138 | 138 |
move(_data[par],hole); |
139 | 139 |
hole = par; |
140 | 140 |
par = parent(hole); |
141 | 141 |
} |
142 | 142 |
move(p, hole); |
143 | 143 |
return hole; |
144 | 144 |
} |
145 | 145 |
|
146 | 146 |
int bubble_down(int hole, Pair p, int length) { |
147 | 147 |
int child = second_child(hole); |
148 | 148 |
while(child < length) { |
149 | 149 |
if( less(_data[child-1], _data[child]) ) { |
150 | 150 |
--child; |
151 | 151 |
} |
152 | 152 |
if( !less(_data[child], p) ) |
153 | 153 |
goto ok; |
154 | 154 |
move(_data[child], hole); |
155 | 155 |
hole = child; |
156 | 156 |
child = second_child(hole); |
157 | 157 |
} |
158 | 158 |
child--; |
159 | 159 |
if( child<length && less(_data[child], p) ) { |
160 | 160 |
move(_data[child], hole); |
161 | 161 |
hole=child; |
162 | 162 |
} |
163 | 163 |
ok: |
164 | 164 |
move(p, hole); |
165 | 165 |
return hole; |
166 | 166 |
} |
167 | 167 |
|
168 | 168 |
void move(const Pair &p, int i) { |
169 | 169 |
_data[i] = p; |
170 | 170 |
_iim.set(p.first, i); |
171 | 171 |
} |
172 | 172 |
|
173 | 173 |
public: |
174 | 174 |
/// \brief Insert a pair of item and priority into the heap. |
175 | 175 |
/// |
176 | 176 |
/// Adds \c p.first to the heap with priority \c p.second. |
177 | 177 |
/// \param p The pair to insert. |
178 | 178 |
void push(const Pair &p) { |
179 | 179 |
int n = _data.size(); |
180 | 180 |
_data.resize(n+1); |
181 | 181 |
bubble_up(n, p); |
182 | 182 |
} |
183 | 183 |
|
184 | 184 |
/// \brief Insert an item into the heap with the given heap. |
185 | 185 |
/// |
186 | 186 |
/// Adds \c i to the heap with priority \c p. |
187 | 187 |
/// \param i The item to insert. |
188 | 188 |
/// \param p The priority of the item. |
189 | 189 |
void push(const Item &i, const Prio &p) { push(Pair(i,p)); } |
190 | 190 |
|
191 | 191 |
/// \brief Returns the item with minimum priority relative to \c Compare. |
192 | 192 |
/// |
193 | 193 |
/// This method returns the item with minimum priority relative to \c |
194 | 194 |
/// Compare. |
195 | 195 |
/// \pre The heap must be nonempty. |
196 | 196 |
Item top() const { |
197 | 197 |
return _data[0].first; |
198 | 198 |
} |
199 | 199 |
|
200 | 200 |
/// \brief Returns the minimum priority relative to \c Compare. |
201 | 201 |
/// |
202 | 202 |
/// It returns the minimum priority relative to \c Compare. |
203 | 203 |
/// \pre The heap must be nonempty. |
204 | 204 |
Prio prio() const { |
205 | 205 |
return _data[0].second; |
206 | 206 |
} |
207 | 207 |
|
208 | 208 |
/// \brief Deletes the item with minimum priority relative to \c Compare. |
209 | 209 |
/// |
210 | 210 |
/// This method deletes the item with minimum priority relative to \c |
211 | 211 |
/// Compare from the heap. |
212 | 212 |
/// \pre The heap must be non-empty. |
213 | 213 |
void pop() { |
214 | 214 |
int n = _data.size()-1; |
215 | 215 |
_iim.set(_data[0].first, POST_HEAP); |
216 | 216 |
if (n > 0) { |
217 | 217 |
bubble_down(0, _data[n], n); |
218 | 218 |
} |
219 | 219 |
_data.pop_back(); |
220 | 220 |
} |
221 | 221 |
|
222 | 222 |
/// \brief Deletes \c i from the heap. |
223 | 223 |
/// |
224 | 224 |
/// This method deletes item \c i from the heap. |
225 | 225 |
/// \param i The item to erase. |
226 | 226 |
/// \pre The item should be in the heap. |
227 | 227 |
void erase(const Item &i) { |
228 | 228 |
int h = _iim[i]; |
229 | 229 |
int n = _data.size()-1; |
230 | 230 |
_iim.set(_data[h].first, POST_HEAP); |
231 | 231 |
if( h < n ) { |
232 | 232 |
if ( bubble_up(h, _data[n]) == h) { |
233 | 233 |
bubble_down(h, _data[n], n); |
234 | 234 |
} |
235 | 235 |
} |
236 | 236 |
_data.pop_back(); |
237 | 237 |
} |
238 | 238 |
|
239 | 239 |
|
240 | 240 |
/// \brief Returns the priority of \c i. |
241 | 241 |
/// |
242 | 242 |
/// This function returns the priority of item \c i. |
243 | 243 |
/// \param i The item. |
244 | 244 |
/// \pre \c i must be in the heap. |
245 | 245 |
Prio operator[](const Item &i) const { |
246 | 246 |
int idx = _iim[i]; |
247 | 247 |
return _data[idx].second; |
248 | 248 |
} |
249 | 249 |
|
250 | 250 |
/// \brief \c i gets to the heap with priority \c p independently |
251 | 251 |
/// if \c i was already there. |
252 | 252 |
/// |
253 | 253 |
/// This method calls \ref push(\c i, \c p) if \c i is not stored |
254 | 254 |
/// in the heap and sets the priority of \c i to \c p otherwise. |
255 | 255 |
/// \param i The item. |
256 | 256 |
/// \param p The priority. |
257 | 257 |
void set(const Item &i, const Prio &p) { |
258 | 258 |
int idx = _iim[i]; |
259 | 259 |
if( idx < 0 ) { |
260 | 260 |
push(i,p); |
261 | 261 |
} |
262 | 262 |
else if( _comp(p, _data[idx].second) ) { |
263 | 263 |
bubble_up(idx, Pair(i,p)); |
264 | 264 |
} |
265 | 265 |
else { |
266 | 266 |
bubble_down(idx, Pair(i,p), _data.size()); |
267 | 267 |
} |
268 | 268 |
} |
269 | 269 |
|
270 | 270 |
/// \brief Decreases the priority of \c i to \c p. |
271 | 271 |
/// |
272 | 272 |
/// This method decreases the priority of item \c i to \c p. |
273 | 273 |
/// \param i The item. |
274 | 274 |
/// \param p The priority. |
275 | 275 |
/// \pre \c i must be stored in the heap with priority at least \c |
276 | 276 |
/// p relative to \c Compare. |
277 | 277 |
void decrease(const Item &i, const Prio &p) { |
278 | 278 |
int idx = _iim[i]; |
279 | 279 |
bubble_up(idx, Pair(i,p)); |
280 | 280 |
} |
281 | 281 |
|
282 | 282 |
/// \brief Increases the priority of \c i to \c p. |
283 | 283 |
/// |
284 | 284 |
/// This method sets the priority of item \c i to \c p. |
285 | 285 |
/// \param i The item. |
286 | 286 |
/// \param p The priority. |
287 | 287 |
/// \pre \c i must be stored in the heap with priority at most \c |
288 | 288 |
/// p relative to \c Compare. |
289 | 289 |
void increase(const Item &i, const Prio &p) { |
290 | 290 |
int idx = _iim[i]; |
291 | 291 |
bubble_down(idx, Pair(i,p), _data.size()); |
292 | 292 |
} |
293 | 293 |
|
294 | 294 |
/// \brief Returns if \c item is in, has already been in, or has |
295 | 295 |
/// never been in the heap. |
296 | 296 |
/// |
297 | 297 |
/// This method returns PRE_HEAP if \c item has never been in the |
298 | 298 |
/// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP |
299 | 299 |
/// otherwise. In the latter case it is possible that \c item will |
300 | 300 |
/// get back to the heap again. |
301 | 301 |
/// \param i The item. |
302 | 302 |
State state(const Item &i) const { |
303 | 303 |
int s = _iim[i]; |
304 | 304 |
if( s>=0 ) |
305 | 305 |
s=0; |
306 | 306 |
return State(s); |
307 | 307 |
} |
308 | 308 |
|
309 | 309 |
/// \brief Sets the state of the \c item in the heap. |
310 | 310 |
/// |
311 | 311 |
/// Sets the state of the \c item in the heap. It can be used to |
312 | 312 |
/// manually clear the heap when it is important to achive the |
313 | 313 |
/// better time complexity. |
314 | 314 |
/// \param i The item. |
315 | 315 |
/// \param st The state. It should not be \c IN_HEAP. |
316 | 316 |
void state(const Item& i, State st) { |
317 | 317 |
switch (st) { |
318 | 318 |
case POST_HEAP: |
319 | 319 |
case PRE_HEAP: |
320 | 320 |
if (state(i) == IN_HEAP) { |
321 | 321 |
erase(i); |
322 | 322 |
} |
323 | 323 |
_iim[i] = st; |
324 | 324 |
break; |
325 | 325 |
case IN_HEAP: |
326 | 326 |
break; |
327 | 327 |
} |
328 | 328 |
} |
329 | 329 |
|
330 | 330 |
/// \brief Replaces an item in the heap. |
331 | 331 |
/// |
332 | 332 |
/// The \c i item is replaced with \c j item. The \c i item should |
333 | 333 |
/// be in the heap, while the \c j should be out of the heap. The |
334 | 334 |
/// \c i item will out of the heap and \c j will be in the heap |
335 | 335 |
/// with the same prioriority as prevoiusly the \c i item. |
336 | 336 |
void replace(const Item& i, const Item& j) { |
337 | 337 |
int idx = _iim[i]; |
338 | 338 |
_iim.set(i, _iim[j]); |
339 | 339 |
_iim.set(j, idx); |
340 | 340 |
_data[idx].first = j; |
341 | 341 |
} |
342 | 342 |
|
343 | 343 |
}; // class BinHeap |
344 | 344 |
|
345 | 345 |
} // namespace lemon |
346 | 346 |
|
347 | 347 |
#endif // LEMON_BIN_HEAP_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup graph_concepts |
20 | 20 |
///\file |
21 | 21 |
///\brief The concept of graph components. |
22 | 22 |
|
23 | 23 |
#ifndef LEMON_CONCEPTS_GRAPH_COMPONENTS_H |
24 | 24 |
#define LEMON_CONCEPTS_GRAPH_COMPONENTS_H |
25 | 25 |
|
26 | 26 |
#include <lemon/core.h> |
27 | 27 |
#include <lemon/concepts/maps.h> |
28 | 28 |
|
29 | 29 |
#include <lemon/bits/alteration_notifier.h> |
30 | 30 |
|
31 | 31 |
namespace lemon { |
32 | 32 |
namespace concepts { |
33 | 33 |
|
34 | 34 |
/// \brief Concept class for \c Node, \c Arc and \c Edge types. |
35 | 35 |
/// |
36 | 36 |
/// This class describes the concept of \c Node, \c Arc and \c Edge |
37 | 37 |
/// subtypes of digraph and graph types. |
38 | 38 |
/// |
39 | 39 |
/// \note This class is a template class so that we can use it to |
40 | 40 |
/// create graph skeleton classes. The reason for this is that \c Node |
41 | 41 |
/// and \c Arc (or \c Edge) types should \e not derive from the same |
42 | 42 |
/// base class. For \c Node you should instantiate it with character |
43 | 43 |
/// \c 'n', for \c Arc with \c 'a' and for \c Edge with \c 'e'. |
44 | 44 |
#ifndef DOXYGEN |
45 | 45 |
template <char sel = '0'> |
46 | 46 |
#endif |
47 | 47 |
class GraphItem { |
48 | 48 |
public: |
49 | 49 |
/// \brief Default constructor. |
50 | 50 |
/// |
51 | 51 |
/// Default constructor. |
52 | 52 |
/// \warning The default constructor is not required to set |
53 | 53 |
/// the item to some well-defined value. So you should consider it |
54 | 54 |
/// as uninitialized. |
55 | 55 |
GraphItem() {} |
56 | 56 |
|
57 | 57 |
/// \brief Copy constructor. |
58 | 58 |
/// |
59 | 59 |
/// Copy constructor. |
60 | 60 |
GraphItem(const GraphItem &) {} |
61 | 61 |
|
62 | 62 |
/// \brief Constructor for conversion from \c INVALID. |
63 | 63 |
/// |
64 | 64 |
/// Constructor for conversion from \c INVALID. |
65 | 65 |
/// It initializes the item to be invalid. |
66 | 66 |
/// \sa Invalid for more details. |
67 | 67 |
GraphItem(Invalid) {} |
68 | 68 |
|
69 | 69 |
/// \brief Assignment operator. |
70 | 70 |
/// |
71 | 71 |
/// Assignment operator for the item. |
72 | 72 |
GraphItem& operator=(const GraphItem&) { return *this; } |
73 | 73 |
|
74 | 74 |
/// \brief Equality operator. |
75 | 75 |
/// |
76 | 76 |
/// Equality operator. |
77 | 77 |
bool operator==(const GraphItem&) const { return false; } |
78 | 78 |
|
79 | 79 |
/// \brief Inequality operator. |
80 | 80 |
/// |
81 | 81 |
/// Inequality operator. |
82 | 82 |
bool operator!=(const GraphItem&) const { return false; } |
83 | 83 |
|
84 | 84 |
/// \brief Ordering operator. |
85 | 85 |
/// |
86 | 86 |
/// This operator defines an ordering of the items. |
87 | 87 |
/// It makes possible to use graph item types as key types in |
88 | 88 |
/// associative containers (e.g. \c std::map). |
89 | 89 |
/// |
90 | 90 |
/// \note This operator only have to define some strict ordering of |
91 | 91 |
/// the items; this order has nothing to do with the iteration |
92 | 92 |
/// ordering of the items. |
93 | 93 |
bool operator<(const GraphItem&) const { return false; } |
94 | 94 |
|
95 | 95 |
template<typename _GraphItem> |
96 | 96 |
struct Constraints { |
97 | 97 |
void constraints() { |
98 | 98 |
_GraphItem i1; |
99 | 99 |
_GraphItem i2 = i1; |
100 | 100 |
_GraphItem i3 = INVALID; |
101 | 101 |
|
102 | 102 |
i1 = i2 = i3; |
103 | 103 |
|
104 | 104 |
bool b; |
105 | 105 |
b = (ia == ib) && (ia != ib); |
106 | 106 |
b = (ia == INVALID) && (ib != INVALID); |
107 | 107 |
b = (ia < ib); |
108 | 108 |
} |
109 | 109 |
|
110 | 110 |
const _GraphItem &ia; |
111 | 111 |
const _GraphItem &ib; |
112 | 112 |
}; |
113 | 113 |
}; |
114 | 114 |
|
115 | 115 |
/// \brief Base skeleton class for directed graphs. |
116 | 116 |
/// |
117 | 117 |
/// This class describes the base interface of directed graph types. |
118 | 118 |
/// All digraph %concepts have to conform to this class. |
119 | 119 |
/// It just provides types for nodes and arcs and functions |
120 | 120 |
/// to get the source and the target nodes of arcs. |
121 | 121 |
class BaseDigraphComponent { |
122 | 122 |
public: |
123 | 123 |
|
124 | 124 |
typedef BaseDigraphComponent Digraph; |
125 | 125 |
|
126 | 126 |
/// \brief Node class of the digraph. |
127 | 127 |
/// |
128 | 128 |
/// This class represents the nodes of the digraph. |
129 | 129 |
typedef GraphItem<'n'> Node; |
130 | 130 |
|
131 | 131 |
/// \brief Arc class of the digraph. |
132 | 132 |
/// |
133 | 133 |
/// This class represents the arcs of the digraph. |
134 | 134 |
typedef GraphItem<'a'> Arc; |
135 | 135 |
|
136 | 136 |
/// \brief Return the source node of an arc. |
137 | 137 |
/// |
138 | 138 |
/// This function returns the source node of an arc. |
139 | 139 |
Node source(const Arc&) const { return INVALID; } |
140 | 140 |
|
141 | 141 |
/// \brief Return the target node of an arc. |
142 | 142 |
/// |
143 | 143 |
/// This function returns the target node of an arc. |
144 | 144 |
Node target(const Arc&) const { return INVALID; } |
145 | 145 |
|
146 | 146 |
/// \brief Return the opposite node on the given arc. |
147 | 147 |
/// |
148 | 148 |
/// This function returns the opposite node on the given arc. |
149 | 149 |
Node oppositeNode(const Node&, const Arc&) const { |
150 | 150 |
return INVALID; |
151 | 151 |
} |
152 | 152 |
|
153 | 153 |
template <typename _Digraph> |
154 | 154 |
struct Constraints { |
155 | 155 |
typedef typename _Digraph::Node Node; |
156 | 156 |
typedef typename _Digraph::Arc Arc; |
157 | 157 |
|
158 | 158 |
void constraints() { |
159 | 159 |
checkConcept<GraphItem<'n'>, Node>(); |
160 | 160 |
checkConcept<GraphItem<'a'>, Arc>(); |
161 | 161 |
{ |
162 | 162 |
Node n; |
163 | 163 |
Arc e(INVALID); |
164 | 164 |
n = digraph.source(e); |
165 | 165 |
n = digraph.target(e); |
166 | 166 |
n = digraph.oppositeNode(n, e); |
167 | 167 |
} |
168 | 168 |
} |
169 | 169 |
|
170 | 170 |
const _Digraph& digraph; |
171 | 171 |
}; |
172 | 172 |
}; |
173 | 173 |
|
174 | 174 |
/// \brief Base skeleton class for undirected graphs. |
175 | 175 |
/// |
176 | 176 |
/// This class describes the base interface of undirected graph types. |
177 | 177 |
/// All graph %concepts have to conform to this class. |
178 | 178 |
/// It extends the interface of \ref BaseDigraphComponent with an |
179 | 179 |
/// \c Edge type and functions to get the end nodes of edges, |
180 | 180 |
/// to convert from arcs to edges and to get both direction of edges. |
181 | 181 |
class BaseGraphComponent : public BaseDigraphComponent { |
182 | 182 |
public: |
183 | 183 |
typedef BaseDigraphComponent::Node Node; |
184 | 184 |
typedef BaseDigraphComponent::Arc Arc; |
185 | 185 |
|
186 | 186 |
/// \brief Undirected edge class of the graph. |
187 | 187 |
/// |
188 | 188 |
/// This class represents the undirected edges of the graph. |
189 | 189 |
/// Undirected graphs can be used as directed graphs, each edge is |
190 | 190 |
/// represented by two opposite directed arcs. |
191 | 191 |
class Edge : public GraphItem<'e'> { |
192 | 192 |
public: |
193 | 193 |
typedef GraphItem<'e'> Parent; |
194 | 194 |
|
195 | 195 |
/// \brief Default constructor. |
196 | 196 |
/// |
197 | 197 |
/// Default constructor. |
198 | 198 |
/// \warning The default constructor is not required to set |
199 | 199 |
/// the item to some well-defined value. So you should consider it |
200 | 200 |
/// as uninitialized. |
201 | 201 |
Edge() {} |
202 | 202 |
|
203 | 203 |
/// \brief Copy constructor. |
204 | 204 |
/// |
205 | 205 |
/// Copy constructor. |
206 | 206 |
Edge(const Edge &) : Parent() {} |
207 | 207 |
|
208 | 208 |
/// \brief Constructor for conversion from \c INVALID. |
209 | 209 |
/// |
210 | 210 |
/// Constructor for conversion from \c INVALID. |
211 | 211 |
/// It initializes the item to be invalid. |
212 | 212 |
/// \sa Invalid for more details. |
213 | 213 |
Edge(Invalid) {} |
214 | 214 |
|
215 | 215 |
/// \brief Constructor for conversion from an arc. |
216 | 216 |
/// |
217 | 217 |
/// Constructor for conversion from an arc. |
218 | 218 |
/// Besides the core graph item functionality each arc should |
219 | 219 |
/// be convertible to the represented edge. |
220 | 220 |
Edge(const Arc&) {} |
221 | 221 |
|
222 | 222 |
/// \brief Assign an arc to an edge. |
223 | 223 |
/// |
224 | 224 |
/// This function assigns an arc to an edge. |
225 | 225 |
/// Besides the core graph item functionality each arc should |
226 | 226 |
/// be convertible to the represented edge. |
227 | 227 |
Edge& operator=(const Arc&) { return *this; } |
228 | 228 |
}; |
229 | 229 |
|
230 | 230 |
/// \brief Return one end node of an edge. |
231 | 231 |
/// |
232 | 232 |
/// This function returns one end node of an edge. |
233 | 233 |
Node u(const Edge&) const { return INVALID; } |
234 | 234 |
|
235 | 235 |
/// \brief Return the other end node of an edge. |
236 | 236 |
/// |
237 | 237 |
/// This function returns the other end node of an edge. |
238 | 238 |
Node v(const Edge&) const { return INVALID; } |
239 | 239 |
|
240 | 240 |
/// \brief Return a directed arc related to an edge. |
241 | 241 |
/// |
242 | 242 |
/// This function returns a directed arc from its direction and the |
243 | 243 |
/// represented edge. |
244 | 244 |
Arc direct(const Edge&, bool) const { return INVALID; } |
245 | 245 |
|
246 | 246 |
/// \brief Return a directed arc related to an edge. |
247 | 247 |
/// |
248 | 248 |
/// This function returns a directed arc from its source node and the |
249 | 249 |
/// represented edge. |
250 | 250 |
Arc direct(const Edge&, const Node&) const { return INVALID; } |
251 | 251 |
|
252 | 252 |
/// \brief Return the direction of the arc. |
253 | 253 |
/// |
254 | 254 |
/// Returns the direction of the arc. Each arc represents an |
255 | 255 |
/// edge with a direction. It gives back the |
256 | 256 |
/// direction. |
257 | 257 |
bool direction(const Arc&) const { return true; } |
258 | 258 |
|
259 | 259 |
/// \brief Return the opposite arc. |
260 | 260 |
/// |
261 | 261 |
/// This function returns the opposite arc, i.e. the arc representing |
262 | 262 |
/// the same edge and has opposite direction. |
263 | 263 |
Arc oppositeArc(const Arc&) const { return INVALID; } |
264 | 264 |
|
265 | 265 |
template <typename _Graph> |
266 | 266 |
struct Constraints { |
267 | 267 |
typedef typename _Graph::Node Node; |
268 | 268 |
typedef typename _Graph::Arc Arc; |
269 | 269 |
typedef typename _Graph::Edge Edge; |
270 | 270 |
|
271 | 271 |
void constraints() { |
272 | 272 |
checkConcept<BaseDigraphComponent, _Graph>(); |
273 | 273 |
checkConcept<GraphItem<'e'>, Edge>(); |
274 | 274 |
{ |
275 | 275 |
Node n; |
276 | 276 |
Edge ue(INVALID); |
277 | 277 |
Arc e; |
278 | 278 |
n = graph.u(ue); |
279 | 279 |
n = graph.v(ue); |
280 | 280 |
e = graph.direct(ue, true); |
281 | 281 |
e = graph.direct(ue, false); |
282 | 282 |
e = graph.direct(ue, n); |
283 | 283 |
e = graph.oppositeArc(e); |
284 | 284 |
ue = e; |
285 | 285 |
bool d = graph.direction(e); |
286 | 286 |
ignore_unused_variable_warning(d); |
287 | 287 |
} |
288 | 288 |
} |
289 | 289 |
|
290 | 290 |
const _Graph& graph; |
291 | 291 |
}; |
292 | 292 |
|
293 | 293 |
}; |
294 | 294 |
|
295 | 295 |
/// \brief Skeleton class for \e idable directed graphs. |
296 | 296 |
/// |
297 | 297 |
/// This class describes the interface of \e idable directed graphs. |
298 | 298 |
/// It extends \ref BaseDigraphComponent with the core ID functions. |
299 | 299 |
/// The ids of the items must be unique and immutable. |
300 | 300 |
/// This concept is part of the Digraph concept. |
301 | 301 |
template <typename BAS = BaseDigraphComponent> |
302 | 302 |
class IDableDigraphComponent : public BAS { |
303 | 303 |
public: |
304 | 304 |
|
305 | 305 |
typedef BAS Base; |
306 | 306 |
typedef typename Base::Node Node; |
307 | 307 |
typedef typename Base::Arc Arc; |
308 | 308 |
|
309 | 309 |
/// \brief Return a unique integer id for the given node. |
310 | 310 |
/// |
311 | 311 |
/// This function returns a unique integer id for the given node. |
312 | 312 |
int id(const Node&) const { return -1; } |
313 | 313 |
|
314 | 314 |
/// \brief Return the node by its unique id. |
315 | 315 |
/// |
316 | 316 |
/// This function returns the node by its unique id. |
317 | 317 |
/// If the digraph does not contain a node with the given id, |
318 | 318 |
/// then the result of the function is undefined. |
319 | 319 |
Node nodeFromId(int) const { return INVALID; } |
320 | 320 |
|
321 | 321 |
/// \brief Return a unique integer id for the given arc. |
322 | 322 |
/// |
323 | 323 |
/// This function returns a unique integer id for the given arc. |
324 | 324 |
int id(const Arc&) const { return -1; } |
325 | 325 |
|
326 | 326 |
/// \brief Return the arc by its unique id. |
327 | 327 |
/// |
328 | 328 |
/// This function returns the arc by its unique id. |
329 | 329 |
/// If the digraph does not contain an arc with the given id, |
330 | 330 |
/// then the result of the function is undefined. |
331 | 331 |
Arc arcFromId(int) const { return INVALID; } |
332 | 332 |
|
333 | 333 |
/// \brief Return an integer greater or equal to the maximum |
334 | 334 |
/// node id. |
335 | 335 |
/// |
336 | 336 |
/// This function returns an integer greater or equal to the |
337 | 337 |
/// maximum node id. |
338 | 338 |
int maxNodeId() const { return -1; } |
339 | 339 |
|
340 | 340 |
/// \brief Return an integer greater or equal to the maximum |
341 | 341 |
/// arc id. |
342 | 342 |
/// |
343 | 343 |
/// This function returns an integer greater or equal to the |
344 | 344 |
/// maximum arc id. |
345 | 345 |
int maxArcId() const { return -1; } |
346 | 346 |
|
347 | 347 |
template <typename _Digraph> |
348 | 348 |
struct Constraints { |
349 | 349 |
|
350 | 350 |
void constraints() { |
351 | 351 |
checkConcept<Base, _Digraph >(); |
352 | 352 |
typename _Digraph::Node node; |
353 | 353 |
int nid = digraph.id(node); |
354 | 354 |
nid = digraph.id(node); |
355 | 355 |
node = digraph.nodeFromId(nid); |
356 | 356 |
typename _Digraph::Arc arc; |
357 | 357 |
int eid = digraph.id(arc); |
358 | 358 |
eid = digraph.id(arc); |
359 | 359 |
arc = digraph.arcFromId(eid); |
360 | 360 |
|
361 | 361 |
nid = digraph.maxNodeId(); |
362 | 362 |
ignore_unused_variable_warning(nid); |
363 | 363 |
eid = digraph.maxArcId(); |
364 | 364 |
ignore_unused_variable_warning(eid); |
365 | 365 |
} |
366 | 366 |
|
367 | 367 |
const _Digraph& digraph; |
368 | 368 |
}; |
369 | 369 |
}; |
370 | 370 |
|
371 | 371 |
/// \brief Skeleton class for \e idable undirected graphs. |
372 | 372 |
/// |
373 | 373 |
/// This class describes the interface of \e idable undirected |
374 | 374 |
/// graphs. It extends \ref IDableDigraphComponent with the core ID |
375 | 375 |
/// functions of undirected graphs. |
376 | 376 |
/// The ids of the items must be unique and immutable. |
377 | 377 |
/// This concept is part of the Graph concept. |
378 | 378 |
template <typename BAS = BaseGraphComponent> |
379 | 379 |
class IDableGraphComponent : public IDableDigraphComponent<BAS> { |
380 | 380 |
public: |
381 | 381 |
|
382 | 382 |
typedef BAS Base; |
383 | 383 |
typedef typename Base::Edge Edge; |
384 | 384 |
|
385 | 385 |
using IDableDigraphComponent<Base>::id; |
386 | 386 |
|
387 | 387 |
/// \brief Return a unique integer id for the given edge. |
388 | 388 |
/// |
389 | 389 |
/// This function returns a unique integer id for the given edge. |
390 | 390 |
int id(const Edge&) const { return -1; } |
391 | 391 |
|
392 | 392 |
/// \brief Return the edge by its unique id. |
393 | 393 |
/// |
394 | 394 |
/// This function returns the edge by its unique id. |
395 | 395 |
/// If the graph does not contain an edge with the given id, |
396 | 396 |
/// then the result of the function is undefined. |
397 | 397 |
Edge edgeFromId(int) const { return INVALID; } |
398 | 398 |
|
399 | 399 |
/// \brief Return an integer greater or equal to the maximum |
400 | 400 |
/// edge id. |
401 | 401 |
/// |
402 | 402 |
/// This function returns an integer greater or equal to the |
403 | 403 |
/// maximum edge id. |
404 | 404 |
int maxEdgeId() const { return -1; } |
405 | 405 |
|
406 | 406 |
template <typename _Graph> |
407 | 407 |
struct Constraints { |
408 | 408 |
|
409 | 409 |
void constraints() { |
410 | 410 |
checkConcept<IDableDigraphComponent<Base>, _Graph >(); |
411 | 411 |
typename _Graph::Edge edge; |
412 | 412 |
int ueid = graph.id(edge); |
413 | 413 |
ueid = graph.id(edge); |
414 | 414 |
edge = graph.edgeFromId(ueid); |
415 | 415 |
ueid = graph.maxEdgeId(); |
416 | 416 |
ignore_unused_variable_warning(ueid); |
417 | 417 |
} |
418 | 418 |
|
419 | 419 |
const _Graph& graph; |
420 | 420 |
}; |
421 | 421 |
}; |
422 | 422 |
|
423 | 423 |
/// \brief Concept class for \c NodeIt, \c ArcIt and \c EdgeIt types. |
424 | 424 |
/// |
425 | 425 |
/// This class describes the concept of \c NodeIt, \c ArcIt and |
426 | 426 |
/// \c EdgeIt subtypes of digraph and graph types. |
427 | 427 |
template <typename GR, typename Item> |
428 | 428 |
class GraphItemIt : public Item { |
429 | 429 |
public: |
430 | 430 |
/// \brief Default constructor. |
431 | 431 |
/// |
432 | 432 |
/// Default constructor. |
433 | 433 |
/// \warning The default constructor is not required to set |
434 | 434 |
/// the iterator to some well-defined value. So you should consider it |
435 | 435 |
/// as uninitialized. |
436 | 436 |
GraphItemIt() {} |
437 | 437 |
|
438 | 438 |
/// \brief Copy constructor. |
439 | 439 |
/// |
440 | 440 |
/// Copy constructor. |
441 | 441 |
GraphItemIt(const GraphItemIt& it) : Item(it) {} |
442 | 442 |
|
443 | 443 |
/// \brief Constructor that sets the iterator to the first item. |
444 | 444 |
/// |
445 | 445 |
/// Constructor that sets the iterator to the first item. |
446 | 446 |
explicit GraphItemIt(const GR&) {} |
447 | 447 |
|
448 | 448 |
/// \brief Constructor for conversion from \c INVALID. |
449 | 449 |
/// |
450 | 450 |
/// Constructor for conversion from \c INVALID. |
451 | 451 |
/// It initializes the iterator to be invalid. |
452 | 452 |
/// \sa Invalid for more details. |
453 | 453 |
GraphItemIt(Invalid) {} |
454 | 454 |
|
455 | 455 |
/// \brief Assignment operator. |
456 | 456 |
/// |
457 | 457 |
/// Assignment operator for the iterator. |
458 | 458 |
GraphItemIt& operator=(const GraphItemIt&) { return *this; } |
459 | 459 |
|
460 | 460 |
/// \brief Increment the iterator. |
461 | 461 |
/// |
462 | 462 |
/// This operator increments the iterator, i.e. assigns it to the |
463 | 463 |
/// next item. |
464 | 464 |
GraphItemIt& operator++() { return *this; } |
465 | 465 |
|
466 | 466 |
/// \brief Equality operator |
467 | 467 |
/// |
468 | 468 |
/// Equality operator. |
469 | 469 |
/// Two iterators are equal if and only if they point to the |
470 | 470 |
/// same object or both are invalid. |
471 | 471 |
bool operator==(const GraphItemIt&) const { return true;} |
472 | 472 |
|
473 | 473 |
/// \brief Inequality operator |
474 | 474 |
/// |
475 | 475 |
/// Inequality operator. |
476 | 476 |
/// Two iterators are equal if and only if they point to the |
477 | 477 |
/// same object or both are invalid. |
478 | 478 |
bool operator!=(const GraphItemIt&) const { return true;} |
479 | 479 |
|
480 | 480 |
template<typename _GraphItemIt> |
481 | 481 |
struct Constraints { |
482 | 482 |
void constraints() { |
483 | 483 |
checkConcept<GraphItem<>, _GraphItemIt>(); |
484 | 484 |
_GraphItemIt it1(g); |
485 | 485 |
_GraphItemIt it2; |
486 | 486 |
_GraphItemIt it3 = it1; |
487 | 487 |
_GraphItemIt it4 = INVALID; |
488 | 488 |
|
489 | 489 |
it2 = ++it1; |
490 | 490 |
++it2 = it1; |
491 | 491 |
++(++it1); |
492 | 492 |
|
493 | 493 |
Item bi = it1; |
494 | 494 |
bi = it2; |
495 | 495 |
} |
496 | 496 |
const GR& g; |
497 | 497 |
}; |
498 | 498 |
}; |
499 | 499 |
|
500 | 500 |
/// \brief Concept class for \c InArcIt, \c OutArcIt and |
501 | 501 |
/// \c IncEdgeIt types. |
502 | 502 |
/// |
503 | 503 |
/// This class describes the concept of \c InArcIt, \c OutArcIt |
504 | 504 |
/// and \c IncEdgeIt subtypes of digraph and graph types. |
505 | 505 |
/// |
506 | 506 |
/// \note Since these iterator classes do not inherit from the same |
507 | 507 |
/// base class, there is an additional template parameter (selector) |
508 | 508 |
/// \c sel. For \c InArcIt you should instantiate it with character |
509 | 509 |
/// \c 'i', for \c OutArcIt with \c 'o' and for \c IncEdgeIt with \c 'e'. |
510 | 510 |
template <typename GR, |
511 | 511 |
typename Item = typename GR::Arc, |
512 | 512 |
typename Base = typename GR::Node, |
513 | 513 |
char sel = '0'> |
514 | 514 |
class GraphIncIt : public Item { |
515 | 515 |
public: |
516 | 516 |
/// \brief Default constructor. |
517 | 517 |
/// |
518 | 518 |
/// Default constructor. |
519 | 519 |
/// \warning The default constructor is not required to set |
520 | 520 |
/// the iterator to some well-defined value. So you should consider it |
521 | 521 |
/// as uninitialized. |
522 | 522 |
GraphIncIt() {} |
523 | 523 |
|
524 | 524 |
/// \brief Copy constructor. |
525 | 525 |
/// |
526 | 526 |
/// Copy constructor. |
527 | 527 |
GraphIncIt(const GraphIncIt& it) : Item(it) {} |
528 | 528 |
|
529 | 529 |
/// \brief Constructor that sets the iterator to the first |
530 | 530 |
/// incoming or outgoing arc. |
531 | 531 |
/// |
532 | 532 |
/// Constructor that sets the iterator to the first arc |
533 | 533 |
/// incoming to or outgoing from the given node. |
534 | 534 |
explicit GraphIncIt(const GR&, const Base&) {} |
535 | 535 |
|
536 | 536 |
/// \brief Constructor for conversion from \c INVALID. |
537 | 537 |
/// |
538 | 538 |
/// Constructor for conversion from \c INVALID. |
539 | 539 |
/// It initializes the iterator to be invalid. |
540 | 540 |
/// \sa Invalid for more details. |
541 | 541 |
GraphIncIt(Invalid) {} |
542 | 542 |
|
543 | 543 |
/// \brief Assignment operator. |
544 | 544 |
/// |
545 | 545 |
/// Assignment operator for the iterator. |
546 | 546 |
GraphIncIt& operator=(const GraphIncIt&) { return *this; } |
547 | 547 |
|
548 | 548 |
/// \brief Increment the iterator. |
549 | 549 |
/// |
550 | 550 |
/// This operator increments the iterator, i.e. assigns it to the |
551 | 551 |
/// next arc incoming to or outgoing from the given node. |
552 | 552 |
GraphIncIt& operator++() { return *this; } |
553 | 553 |
|
554 | 554 |
/// \brief Equality operator |
555 | 555 |
/// |
556 | 556 |
/// Equality operator. |
557 | 557 |
/// Two iterators are equal if and only if they point to the |
558 | 558 |
/// same object or both are invalid. |
559 | 559 |
bool operator==(const GraphIncIt&) const { return true;} |
560 | 560 |
|
561 | 561 |
/// \brief Inequality operator |
562 | 562 |
/// |
563 | 563 |
/// Inequality operator. |
564 | 564 |
/// Two iterators are equal if and only if they point to the |
565 | 565 |
/// same object or both are invalid. |
566 | 566 |
bool operator!=(const GraphIncIt&) const { return true;} |
567 | 567 |
|
568 | 568 |
template <typename _GraphIncIt> |
569 | 569 |
struct Constraints { |
570 | 570 |
void constraints() { |
571 | 571 |
checkConcept<GraphItem<sel>, _GraphIncIt>(); |
572 | 572 |
_GraphIncIt it1(graph, node); |
573 | 573 |
_GraphIncIt it2; |
574 | 574 |
_GraphIncIt it3 = it1; |
575 | 575 |
_GraphIncIt it4 = INVALID; |
576 | 576 |
|
577 | 577 |
it2 = ++it1; |
578 | 578 |
++it2 = it1; |
579 | 579 |
++(++it1); |
580 | 580 |
Item e = it1; |
581 | 581 |
e = it2; |
582 | 582 |
} |
583 | 583 |
const Base& node; |
584 | 584 |
const GR& graph; |
585 | 585 |
}; |
586 | 586 |
}; |
587 | 587 |
|
588 | 588 |
/// \brief Skeleton class for iterable directed graphs. |
589 | 589 |
/// |
590 | 590 |
/// This class describes the interface of iterable directed |
591 | 591 |
/// graphs. It extends \ref BaseDigraphComponent with the core |
592 | 592 |
/// iterable interface. |
593 | 593 |
/// This concept is part of the Digraph concept. |
594 | 594 |
template <typename BAS = BaseDigraphComponent> |
595 | 595 |
class IterableDigraphComponent : public BAS { |
596 | 596 |
|
597 | 597 |
public: |
598 | 598 |
|
599 | 599 |
typedef BAS Base; |
600 | 600 |
typedef typename Base::Node Node; |
601 | 601 |
typedef typename Base::Arc Arc; |
602 | 602 |
|
603 | 603 |
typedef IterableDigraphComponent Digraph; |
604 | 604 |
|
605 |
/// \name Base |
|
605 |
/// \name Base Iteration |
|
606 | 606 |
/// |
607 | 607 |
/// This interface provides functions for iteration on digraph items. |
608 | 608 |
/// |
609 | 609 |
/// @{ |
610 | 610 |
|
611 | 611 |
/// \brief Return the first node. |
612 | 612 |
/// |
613 | 613 |
/// This function gives back the first node in the iteration order. |
614 | 614 |
void first(Node&) const {} |
615 | 615 |
|
616 | 616 |
/// \brief Return the next node. |
617 | 617 |
/// |
618 | 618 |
/// This function gives back the next node in the iteration order. |
619 | 619 |
void next(Node&) const {} |
620 | 620 |
|
621 | 621 |
/// \brief Return the first arc. |
622 | 622 |
/// |
623 | 623 |
/// This function gives back the first arc in the iteration order. |
624 | 624 |
void first(Arc&) const {} |
625 | 625 |
|
626 | 626 |
/// \brief Return the next arc. |
627 | 627 |
/// |
628 | 628 |
/// This function gives back the next arc in the iteration order. |
629 | 629 |
void next(Arc&) const {} |
630 | 630 |
|
631 | 631 |
/// \brief Return the first arc incomming to the given node. |
632 | 632 |
/// |
633 | 633 |
/// This function gives back the first arc incomming to the |
634 | 634 |
/// given node. |
635 | 635 |
void firstIn(Arc&, const Node&) const {} |
636 | 636 |
|
637 | 637 |
/// \brief Return the next arc incomming to the given node. |
638 | 638 |
/// |
639 | 639 |
/// This function gives back the next arc incomming to the |
640 | 640 |
/// given node. |
641 | 641 |
void nextIn(Arc&) const {} |
642 | 642 |
|
643 | 643 |
/// \brief Return the first arc outgoing form the given node. |
644 | 644 |
/// |
645 | 645 |
/// This function gives back the first arc outgoing form the |
646 | 646 |
/// given node. |
647 | 647 |
void firstOut(Arc&, const Node&) const {} |
648 | 648 |
|
649 | 649 |
/// \brief Return the next arc outgoing form the given node. |
650 | 650 |
/// |
651 | 651 |
/// This function gives back the next arc outgoing form the |
652 | 652 |
/// given node. |
653 | 653 |
void nextOut(Arc&) const {} |
654 | 654 |
|
655 | 655 |
/// @} |
656 | 656 |
|
657 |
/// \name Class |
|
657 |
/// \name Class Based Iteration |
|
658 | 658 |
/// |
659 | 659 |
/// This interface provides iterator classes for digraph items. |
660 | 660 |
/// |
661 | 661 |
/// @{ |
662 | 662 |
|
663 | 663 |
/// \brief This iterator goes through each node. |
664 | 664 |
/// |
665 | 665 |
/// This iterator goes through each node. |
666 | 666 |
/// |
667 | 667 |
typedef GraphItemIt<Digraph, Node> NodeIt; |
668 | 668 |
|
669 | 669 |
/// \brief This iterator goes through each arc. |
670 | 670 |
/// |
671 | 671 |
/// This iterator goes through each arc. |
672 | 672 |
/// |
673 | 673 |
typedef GraphItemIt<Digraph, Arc> ArcIt; |
674 | 674 |
|
675 | 675 |
/// \brief This iterator goes trough the incoming arcs of a node. |
676 | 676 |
/// |
677 | 677 |
/// This iterator goes trough the \e incoming arcs of a certain node |
678 | 678 |
/// of a digraph. |
679 | 679 |
typedef GraphIncIt<Digraph, Arc, Node, 'i'> InArcIt; |
680 | 680 |
|
681 | 681 |
/// \brief This iterator goes trough the outgoing arcs of a node. |
682 | 682 |
/// |
683 | 683 |
/// This iterator goes trough the \e outgoing arcs of a certain node |
684 | 684 |
/// of a digraph. |
685 | 685 |
typedef GraphIncIt<Digraph, Arc, Node, 'o'> OutArcIt; |
686 | 686 |
|
687 | 687 |
/// \brief The base node of the iterator. |
688 | 688 |
/// |
689 | 689 |
/// This function gives back the base node of the iterator. |
690 | 690 |
/// It is always the target node of the pointed arc. |
691 | 691 |
Node baseNode(const InArcIt&) const { return INVALID; } |
692 | 692 |
|
693 | 693 |
/// \brief The running node of the iterator. |
694 | 694 |
/// |
695 | 695 |
/// This function gives back the running node of the iterator. |
696 | 696 |
/// It is always the source node of the pointed arc. |
697 | 697 |
Node runningNode(const InArcIt&) const { return INVALID; } |
698 | 698 |
|
699 | 699 |
/// \brief The base node of the iterator. |
700 | 700 |
/// |
701 | 701 |
/// This function gives back the base node of the iterator. |
702 | 702 |
/// It is always the source node of the pointed arc. |
703 | 703 |
Node baseNode(const OutArcIt&) const { return INVALID; } |
704 | 704 |
|
705 | 705 |
/// \brief The running node of the iterator. |
706 | 706 |
/// |
707 | 707 |
/// This function gives back the running node of the iterator. |
708 | 708 |
/// It is always the target node of the pointed arc. |
709 | 709 |
Node runningNode(const OutArcIt&) const { return INVALID; } |
710 | 710 |
|
711 | 711 |
/// @} |
712 | 712 |
|
713 | 713 |
template <typename _Digraph> |
714 | 714 |
struct Constraints { |
715 | 715 |
void constraints() { |
716 | 716 |
checkConcept<Base, _Digraph>(); |
717 | 717 |
|
718 | 718 |
{ |
719 | 719 |
typename _Digraph::Node node(INVALID); |
720 | 720 |
typename _Digraph::Arc arc(INVALID); |
721 | 721 |
{ |
722 | 722 |
digraph.first(node); |
723 | 723 |
digraph.next(node); |
724 | 724 |
} |
725 | 725 |
{ |
726 | 726 |
digraph.first(arc); |
727 | 727 |
digraph.next(arc); |
728 | 728 |
} |
729 | 729 |
{ |
730 | 730 |
digraph.firstIn(arc, node); |
731 | 731 |
digraph.nextIn(arc); |
732 | 732 |
} |
733 | 733 |
{ |
734 | 734 |
digraph.firstOut(arc, node); |
735 | 735 |
digraph.nextOut(arc); |
736 | 736 |
} |
737 | 737 |
} |
738 | 738 |
|
739 | 739 |
{ |
740 | 740 |
checkConcept<GraphItemIt<_Digraph, typename _Digraph::Arc>, |
741 | 741 |
typename _Digraph::ArcIt >(); |
742 | 742 |
checkConcept<GraphItemIt<_Digraph, typename _Digraph::Node>, |
743 | 743 |
typename _Digraph::NodeIt >(); |
744 | 744 |
checkConcept<GraphIncIt<_Digraph, typename _Digraph::Arc, |
745 | 745 |
typename _Digraph::Node, 'i'>, typename _Digraph::InArcIt>(); |
746 | 746 |
checkConcept<GraphIncIt<_Digraph, typename _Digraph::Arc, |
747 | 747 |
typename _Digraph::Node, 'o'>, typename _Digraph::OutArcIt>(); |
748 | 748 |
|
749 | 749 |
typename _Digraph::Node n; |
750 | 750 |
const typename _Digraph::InArcIt iait(INVALID); |
751 | 751 |
const typename _Digraph::OutArcIt oait(INVALID); |
752 | 752 |
n = digraph.baseNode(iait); |
753 | 753 |
n = digraph.runningNode(iait); |
754 | 754 |
n = digraph.baseNode(oait); |
755 | 755 |
n = digraph.runningNode(oait); |
756 | 756 |
ignore_unused_variable_warning(n); |
757 | 757 |
} |
758 | 758 |
} |
759 | 759 |
|
760 | 760 |
const _Digraph& digraph; |
761 | 761 |
}; |
762 | 762 |
}; |
763 | 763 |
|
764 | 764 |
/// \brief Skeleton class for iterable undirected graphs. |
765 | 765 |
/// |
766 | 766 |
/// This class describes the interface of iterable undirected |
767 | 767 |
/// graphs. It extends \ref IterableDigraphComponent with the core |
768 | 768 |
/// iterable interface of undirected graphs. |
769 | 769 |
/// This concept is part of the Graph concept. |
770 | 770 |
template <typename BAS = BaseGraphComponent> |
771 | 771 |
class IterableGraphComponent : public IterableDigraphComponent<BAS> { |
772 | 772 |
public: |
773 | 773 |
|
774 | 774 |
typedef BAS Base; |
775 | 775 |
typedef typename Base::Node Node; |
776 | 776 |
typedef typename Base::Arc Arc; |
777 | 777 |
typedef typename Base::Edge Edge; |
778 | 778 |
|
779 | 779 |
|
780 | 780 |
typedef IterableGraphComponent Graph; |
781 | 781 |
|
782 |
/// \name Base |
|
782 |
/// \name Base Iteration |
|
783 | 783 |
/// |
784 | 784 |
/// This interface provides functions for iteration on edges. |
785 | 785 |
/// |
786 | 786 |
/// @{ |
787 | 787 |
|
788 | 788 |
using IterableDigraphComponent<Base>::first; |
789 | 789 |
using IterableDigraphComponent<Base>::next; |
790 | 790 |
|
791 | 791 |
/// \brief Return the first edge. |
792 | 792 |
/// |
793 | 793 |
/// This function gives back the first edge in the iteration order. |
794 | 794 |
void first(Edge&) const {} |
795 | 795 |
|
796 | 796 |
/// \brief Return the next edge. |
797 | 797 |
/// |
798 | 798 |
/// This function gives back the next edge in the iteration order. |
799 | 799 |
void next(Edge&) const {} |
800 | 800 |
|
801 | 801 |
/// \brief Return the first edge incident to the given node. |
802 | 802 |
/// |
803 | 803 |
/// This function gives back the first edge incident to the given |
804 | 804 |
/// node. The bool parameter gives back the direction for which the |
805 | 805 |
/// source node of the directed arc representing the edge is the |
806 | 806 |
/// given node. |
807 | 807 |
void firstInc(Edge&, bool&, const Node&) const {} |
808 | 808 |
|
809 | 809 |
/// \brief Gives back the next of the edges from the |
810 | 810 |
/// given node. |
811 | 811 |
/// |
812 | 812 |
/// This function gives back the next edge incident to the given |
813 | 813 |
/// node. The bool parameter should be used as \c firstInc() use it. |
814 | 814 |
void nextInc(Edge&, bool&) const {} |
815 | 815 |
|
816 | 816 |
using IterableDigraphComponent<Base>::baseNode; |
817 | 817 |
using IterableDigraphComponent<Base>::runningNode; |
818 | 818 |
|
819 | 819 |
/// @} |
820 | 820 |
|
821 |
/// \name Class |
|
821 |
/// \name Class Based Iteration |
|
822 | 822 |
/// |
823 | 823 |
/// This interface provides iterator classes for edges. |
824 | 824 |
/// |
825 | 825 |
/// @{ |
826 | 826 |
|
827 | 827 |
/// \brief This iterator goes through each edge. |
828 | 828 |
/// |
829 | 829 |
/// This iterator goes through each edge. |
830 | 830 |
typedef GraphItemIt<Graph, Edge> EdgeIt; |
831 | 831 |
|
832 | 832 |
/// \brief This iterator goes trough the incident edges of a |
833 | 833 |
/// node. |
834 | 834 |
/// |
835 | 835 |
/// This iterator goes trough the incident edges of a certain |
836 | 836 |
/// node of a graph. |
837 | 837 |
typedef GraphIncIt<Graph, Edge, Node, 'e'> IncEdgeIt; |
838 | 838 |
|
839 | 839 |
/// \brief The base node of the iterator. |
840 | 840 |
/// |
841 | 841 |
/// This function gives back the base node of the iterator. |
842 | 842 |
Node baseNode(const IncEdgeIt&) const { return INVALID; } |
843 | 843 |
|
844 | 844 |
/// \brief The running node of the iterator. |
845 | 845 |
/// |
846 | 846 |
/// This function gives back the running node of the iterator. |
847 | 847 |
Node runningNode(const IncEdgeIt&) const { return INVALID; } |
848 | 848 |
|
849 | 849 |
/// @} |
850 | 850 |
|
851 | 851 |
template <typename _Graph> |
852 | 852 |
struct Constraints { |
853 | 853 |
void constraints() { |
854 | 854 |
checkConcept<IterableDigraphComponent<Base>, _Graph>(); |
855 | 855 |
|
856 | 856 |
{ |
857 | 857 |
typename _Graph::Node node(INVALID); |
858 | 858 |
typename _Graph::Edge edge(INVALID); |
859 | 859 |
bool dir; |
860 | 860 |
{ |
861 | 861 |
graph.first(edge); |
862 | 862 |
graph.next(edge); |
863 | 863 |
} |
864 | 864 |
{ |
865 | 865 |
graph.firstInc(edge, dir, node); |
866 | 866 |
graph.nextInc(edge, dir); |
867 | 867 |
} |
868 | 868 |
|
869 | 869 |
} |
870 | 870 |
|
871 | 871 |
{ |
872 | 872 |
checkConcept<GraphItemIt<_Graph, typename _Graph::Edge>, |
873 | 873 |
typename _Graph::EdgeIt >(); |
874 | 874 |
checkConcept<GraphIncIt<_Graph, typename _Graph::Edge, |
875 | 875 |
typename _Graph::Node, 'e'>, typename _Graph::IncEdgeIt>(); |
876 | 876 |
|
877 | 877 |
typename _Graph::Node n; |
878 | 878 |
const typename _Graph::IncEdgeIt ieit(INVALID); |
879 | 879 |
n = graph.baseNode(ieit); |
880 | 880 |
n = graph.runningNode(ieit); |
881 | 881 |
} |
882 | 882 |
} |
883 | 883 |
|
884 | 884 |
const _Graph& graph; |
885 | 885 |
}; |
886 | 886 |
}; |
887 | 887 |
|
888 | 888 |
/// \brief Skeleton class for alterable directed graphs. |
889 | 889 |
/// |
890 | 890 |
/// This class describes the interface of alterable directed |
891 | 891 |
/// graphs. It extends \ref BaseDigraphComponent with the alteration |
892 | 892 |
/// notifier interface. It implements |
893 | 893 |
/// an observer-notifier pattern for each digraph item. More |
894 | 894 |
/// obsevers can be registered into the notifier and whenever an |
895 | 895 |
/// alteration occured in the digraph all the observers will be |
896 | 896 |
/// notified about it. |
897 | 897 |
template <typename BAS = BaseDigraphComponent> |
898 | 898 |
class AlterableDigraphComponent : public BAS { |
899 | 899 |
public: |
900 | 900 |
|
901 | 901 |
typedef BAS Base; |
902 | 902 |
typedef typename Base::Node Node; |
903 | 903 |
typedef typename Base::Arc Arc; |
904 | 904 |
|
905 | 905 |
|
906 | 906 |
/// Node alteration notifier class. |
907 | 907 |
typedef AlterationNotifier<AlterableDigraphComponent, Node> |
908 | 908 |
NodeNotifier; |
909 | 909 |
/// Arc alteration notifier class. |
910 | 910 |
typedef AlterationNotifier<AlterableDigraphComponent, Arc> |
911 | 911 |
ArcNotifier; |
912 | 912 |
|
913 | 913 |
/// \brief Return the node alteration notifier. |
914 | 914 |
/// |
915 | 915 |
/// This function gives back the node alteration notifier. |
916 | 916 |
NodeNotifier& notifier(Node) const { |
917 | 917 |
return NodeNotifier(); |
918 | 918 |
} |
919 | 919 |
|
920 | 920 |
/// \brief Return the arc alteration notifier. |
921 | 921 |
/// |
922 | 922 |
/// This function gives back the arc alteration notifier. |
923 | 923 |
ArcNotifier& notifier(Arc) const { |
924 | 924 |
return ArcNotifier(); |
925 | 925 |
} |
926 | 926 |
|
927 | 927 |
template <typename _Digraph> |
928 | 928 |
struct Constraints { |
929 | 929 |
void constraints() { |
930 | 930 |
checkConcept<Base, _Digraph>(); |
931 | 931 |
typename _Digraph::NodeNotifier& nn |
932 | 932 |
= digraph.notifier(typename _Digraph::Node()); |
933 | 933 |
|
934 | 934 |
typename _Digraph::ArcNotifier& en |
935 | 935 |
= digraph.notifier(typename _Digraph::Arc()); |
936 | 936 |
|
937 | 937 |
ignore_unused_variable_warning(nn); |
938 | 938 |
ignore_unused_variable_warning(en); |
939 | 939 |
} |
940 | 940 |
|
941 | 941 |
const _Digraph& digraph; |
942 | 942 |
}; |
943 | 943 |
}; |
944 | 944 |
|
945 | 945 |
/// \brief Skeleton class for alterable undirected graphs. |
946 | 946 |
/// |
947 | 947 |
/// This class describes the interface of alterable undirected |
948 | 948 |
/// graphs. It extends \ref AlterableDigraphComponent with the alteration |
949 | 949 |
/// notifier interface of undirected graphs. It implements |
950 | 950 |
/// an observer-notifier pattern for the edges. More |
951 | 951 |
/// obsevers can be registered into the notifier and whenever an |
952 | 952 |
/// alteration occured in the graph all the observers will be |
953 | 953 |
/// notified about it. |
954 | 954 |
template <typename BAS = BaseGraphComponent> |
955 | 955 |
class AlterableGraphComponent : public AlterableDigraphComponent<BAS> { |
956 | 956 |
public: |
957 | 957 |
|
958 | 958 |
typedef BAS Base; |
959 | 959 |
typedef typename Base::Edge Edge; |
960 | 960 |
|
961 | 961 |
|
962 | 962 |
/// Edge alteration notifier class. |
963 | 963 |
typedef AlterationNotifier<AlterableGraphComponent, Edge> |
964 | 964 |
EdgeNotifier; |
965 | 965 |
|
966 | 966 |
/// \brief Return the edge alteration notifier. |
967 | 967 |
/// |
968 | 968 |
/// This function gives back the edge alteration notifier. |
969 | 969 |
EdgeNotifier& notifier(Edge) const { |
970 | 970 |
return EdgeNotifier(); |
971 | 971 |
} |
972 | 972 |
|
973 | 973 |
template <typename _Graph> |
974 | 974 |
struct Constraints { |
975 | 975 |
void constraints() { |
976 | 976 |
checkConcept<AlterableDigraphComponent<Base>, _Graph>(); |
977 | 977 |
typename _Graph::EdgeNotifier& uen |
978 | 978 |
= graph.notifier(typename _Graph::Edge()); |
979 | 979 |
ignore_unused_variable_warning(uen); |
980 | 980 |
} |
981 | 981 |
|
982 | 982 |
const _Graph& graph; |
983 | 983 |
}; |
984 | 984 |
}; |
985 | 985 |
|
986 | 986 |
/// \brief Concept class for standard graph maps. |
987 | 987 |
/// |
988 | 988 |
/// This class describes the concept of standard graph maps, i.e. |
989 | 989 |
/// the \c NodeMap, \c ArcMap and \c EdgeMap subtypes of digraph and |
990 | 990 |
/// graph types, which can be used for associating data to graph items. |
991 | 991 |
/// The standard graph maps must conform to the ReferenceMap concept. |
992 | 992 |
template <typename GR, typename K, typename V> |
993 | 993 |
class GraphMap : public ReferenceMap<K, V, V&, const V&> { |
994 | 994 |
public: |
995 | 995 |
|
996 | 996 |
typedef ReadWriteMap<K, V> Parent; |
997 | 997 |
|
998 | 998 |
/// The graph type of the map. |
999 | 999 |
typedef GR Graph; |
1000 | 1000 |
/// The key type of the map. |
1001 | 1001 |
typedef K Key; |
1002 | 1002 |
/// The value type of the map. |
1003 | 1003 |
typedef V Value; |
1004 | 1004 |
/// The reference type of the map. |
1005 | 1005 |
typedef Value& Reference; |
1006 | 1006 |
/// The const reference type of the map. |
1007 | 1007 |
typedef const Value& ConstReference; |
1008 | 1008 |
|
1009 | 1009 |
// The reference map tag. |
1010 | 1010 |
typedef True ReferenceMapTag; |
1011 | 1011 |
|
1012 | 1012 |
/// \brief Construct a new map. |
1013 | 1013 |
/// |
1014 | 1014 |
/// Construct a new map for the graph. |
1015 | 1015 |
explicit GraphMap(const Graph&) {} |
1016 | 1016 |
/// \brief Construct a new map with default value. |
1017 | 1017 |
/// |
1018 | 1018 |
/// Construct a new map for the graph and initalize the values. |
1019 | 1019 |
GraphMap(const Graph&, const Value&) {} |
1020 | 1020 |
|
1021 | 1021 |
private: |
1022 | 1022 |
/// \brief Copy constructor. |
1023 | 1023 |
/// |
1024 | 1024 |
/// Copy Constructor. |
1025 | 1025 |
GraphMap(const GraphMap&) : Parent() {} |
1026 | 1026 |
|
1027 | 1027 |
/// \brief Assignment operator. |
1028 | 1028 |
/// |
1029 | 1029 |
/// Assignment operator. It does not mofify the underlying graph, |
1030 | 1030 |
/// it just iterates on the current item set and set the map |
1031 | 1031 |
/// with the value returned by the assigned map. |
1032 | 1032 |
template <typename CMap> |
1033 | 1033 |
GraphMap& operator=(const CMap&) { |
1034 | 1034 |
checkConcept<ReadMap<Key, Value>, CMap>(); |
1035 | 1035 |
return *this; |
1036 | 1036 |
} |
1037 | 1037 |
|
1038 | 1038 |
public: |
1039 | 1039 |
template<typename _Map> |
1040 | 1040 |
struct Constraints { |
1041 | 1041 |
void constraints() { |
1042 | 1042 |
checkConcept |
1043 | 1043 |
<ReferenceMap<Key, Value, Value&, const Value&>, _Map>(); |
1044 | 1044 |
_Map m1(g); |
1045 | 1045 |
_Map m2(g,t); |
1046 | 1046 |
|
1047 | 1047 |
// Copy constructor |
1048 | 1048 |
// _Map m3(m); |
1049 | 1049 |
|
1050 | 1050 |
// Assignment operator |
1051 | 1051 |
// ReadMap<Key, Value> cmap; |
1052 | 1052 |
// m3 = cmap; |
1053 | 1053 |
|
1054 | 1054 |
ignore_unused_variable_warning(m1); |
1055 | 1055 |
ignore_unused_variable_warning(m2); |
1056 | 1056 |
// ignore_unused_variable_warning(m3); |
1057 | 1057 |
} |
1058 | 1058 |
|
1059 | 1059 |
const _Map &m; |
1060 | 1060 |
const Graph &g; |
1061 | 1061 |
const typename GraphMap::Value &t; |
1062 | 1062 |
}; |
1063 | 1063 |
|
1064 | 1064 |
}; |
1065 | 1065 |
|
1066 | 1066 |
/// \brief Skeleton class for mappable directed graphs. |
1067 | 1067 |
/// |
1068 | 1068 |
/// This class describes the interface of mappable directed graphs. |
1069 | 1069 |
/// It extends \ref BaseDigraphComponent with the standard digraph |
1070 | 1070 |
/// map classes, namely \c NodeMap and \c ArcMap. |
1071 | 1071 |
/// This concept is part of the Digraph concept. |
1072 | 1072 |
template <typename BAS = BaseDigraphComponent> |
1073 | 1073 |
class MappableDigraphComponent : public BAS { |
1074 | 1074 |
public: |
1075 | 1075 |
|
1076 | 1076 |
typedef BAS Base; |
1077 | 1077 |
typedef typename Base::Node Node; |
1078 | 1078 |
typedef typename Base::Arc Arc; |
1079 | 1079 |
|
1080 | 1080 |
typedef MappableDigraphComponent Digraph; |
1081 | 1081 |
|
1082 | 1082 |
/// \brief Standard graph map for the nodes. |
1083 | 1083 |
/// |
1084 | 1084 |
/// Standard graph map for the nodes. |
1085 | 1085 |
/// It conforms to the ReferenceMap concept. |
1086 | 1086 |
template <typename V> |
1087 | 1087 |
class NodeMap : public GraphMap<MappableDigraphComponent, Node, V> { |
1088 | 1088 |
public: |
1089 | 1089 |
typedef GraphMap<MappableDigraphComponent, Node, V> Parent; |
1090 | 1090 |
|
1091 | 1091 |
/// \brief Construct a new map. |
1092 | 1092 |
/// |
1093 | 1093 |
/// Construct a new map for the digraph. |
1094 | 1094 |
explicit NodeMap(const MappableDigraphComponent& digraph) |
1095 | 1095 |
: Parent(digraph) {} |
1096 | 1096 |
|
1097 | 1097 |
/// \brief Construct a new map with default value. |
1098 | 1098 |
/// |
1099 | 1099 |
/// Construct a new map for the digraph and initalize the values. |
1100 | 1100 |
NodeMap(const MappableDigraphComponent& digraph, const V& value) |
1101 | 1101 |
: Parent(digraph, value) {} |
1102 | 1102 |
|
1103 | 1103 |
private: |
1104 | 1104 |
/// \brief Copy constructor. |
1105 | 1105 |
/// |
1106 | 1106 |
/// Copy Constructor. |
1107 | 1107 |
NodeMap(const NodeMap& nm) : Parent(nm) {} |
1108 | 1108 |
|
1109 | 1109 |
/// \brief Assignment operator. |
1110 | 1110 |
/// |
1111 | 1111 |
/// Assignment operator. |
1112 | 1112 |
template <typename CMap> |
1113 | 1113 |
NodeMap& operator=(const CMap&) { |
1114 | 1114 |
checkConcept<ReadMap<Node, V>, CMap>(); |
1115 | 1115 |
return *this; |
1116 | 1116 |
} |
1117 | 1117 |
|
1118 | 1118 |
}; |
1119 | 1119 |
|
1120 | 1120 |
/// \brief Standard graph map for the arcs. |
1121 | 1121 |
/// |
1122 | 1122 |
/// Standard graph map for the arcs. |
1123 | 1123 |
/// It conforms to the ReferenceMap concept. |
1124 | 1124 |
template <typename V> |
1125 | 1125 |
class ArcMap : public GraphMap<MappableDigraphComponent, Arc, V> { |
1126 | 1126 |
public: |
1127 | 1127 |
typedef GraphMap<MappableDigraphComponent, Arc, V> Parent; |
1128 | 1128 |
|
1129 | 1129 |
/// \brief Construct a new map. |
1130 | 1130 |
/// |
1131 | 1131 |
/// Construct a new map for the digraph. |
1132 | 1132 |
explicit ArcMap(const MappableDigraphComponent& digraph) |
1133 | 1133 |
: Parent(digraph) {} |
1134 | 1134 |
|
1135 | 1135 |
/// \brief Construct a new map with default value. |
1136 | 1136 |
/// |
1137 | 1137 |
/// Construct a new map for the digraph and initalize the values. |
1138 | 1138 |
ArcMap(const MappableDigraphComponent& digraph, const V& value) |
1139 | 1139 |
: Parent(digraph, value) {} |
1140 | 1140 |
|
1141 | 1141 |
private: |
1142 | 1142 |
/// \brief Copy constructor. |
1143 | 1143 |
/// |
1144 | 1144 |
/// Copy Constructor. |
1145 | 1145 |
ArcMap(const ArcMap& nm) : Parent(nm) {} |
1146 | 1146 |
|
1147 | 1147 |
/// \brief Assignment operator. |
1148 | 1148 |
/// |
1149 | 1149 |
/// Assignment operator. |
1150 | 1150 |
template <typename CMap> |
1151 | 1151 |
ArcMap& operator=(const CMap&) { |
1152 | 1152 |
checkConcept<ReadMap<Arc, V>, CMap>(); |
1153 | 1153 |
return *this; |
1154 | 1154 |
} |
1155 | 1155 |
|
1156 | 1156 |
}; |
1157 | 1157 |
|
1158 | 1158 |
|
1159 | 1159 |
template <typename _Digraph> |
1160 | 1160 |
struct Constraints { |
1161 | 1161 |
|
1162 | 1162 |
struct Dummy { |
1163 | 1163 |
int value; |
1164 | 1164 |
Dummy() : value(0) {} |
1165 | 1165 |
Dummy(int _v) : value(_v) {} |
1166 | 1166 |
}; |
1167 | 1167 |
|
1168 | 1168 |
void constraints() { |
1169 | 1169 |
checkConcept<Base, _Digraph>(); |
1170 | 1170 |
{ // int map test |
1171 | 1171 |
typedef typename _Digraph::template NodeMap<int> IntNodeMap; |
1172 | 1172 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Node, int>, |
1173 | 1173 |
IntNodeMap >(); |
1174 | 1174 |
} { // bool map test |
1175 | 1175 |
typedef typename _Digraph::template NodeMap<bool> BoolNodeMap; |
1176 | 1176 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Node, bool>, |
1177 | 1177 |
BoolNodeMap >(); |
1178 | 1178 |
} { // Dummy map test |
1179 | 1179 |
typedef typename _Digraph::template NodeMap<Dummy> DummyNodeMap; |
1180 | 1180 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Node, Dummy>, |
1181 | 1181 |
DummyNodeMap >(); |
1182 | 1182 |
} |
1183 | 1183 |
|
1184 | 1184 |
{ // int map test |
1185 | 1185 |
typedef typename _Digraph::template ArcMap<int> IntArcMap; |
1186 | 1186 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, int>, |
1187 | 1187 |
IntArcMap >(); |
1188 | 1188 |
} { // bool map test |
1189 | 1189 |
typedef typename _Digraph::template ArcMap<bool> BoolArcMap; |
1190 | 1190 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, bool>, |
1191 | 1191 |
BoolArcMap >(); |
1192 | 1192 |
} { // Dummy map test |
1193 | 1193 |
typedef typename _Digraph::template ArcMap<Dummy> DummyArcMap; |
1194 | 1194 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, Dummy>, |
1195 | 1195 |
DummyArcMap >(); |
1196 | 1196 |
} |
1197 | 1197 |
} |
1198 | 1198 |
|
1199 | 1199 |
const _Digraph& digraph; |
1200 | 1200 |
}; |
1201 | 1201 |
}; |
1202 | 1202 |
|
1203 | 1203 |
/// \brief Skeleton class for mappable undirected graphs. |
1204 | 1204 |
/// |
1205 | 1205 |
/// This class describes the interface of mappable undirected graphs. |
1206 | 1206 |
/// It extends \ref MappableDigraphComponent with the standard graph |
1207 | 1207 |
/// map class for edges (\c EdgeMap). |
1208 | 1208 |
/// This concept is part of the Graph concept. |
1209 | 1209 |
template <typename BAS = BaseGraphComponent> |
1210 | 1210 |
class MappableGraphComponent : public MappableDigraphComponent<BAS> { |
1211 | 1211 |
public: |
1212 | 1212 |
|
1213 | 1213 |
typedef BAS Base; |
1214 | 1214 |
typedef typename Base::Edge Edge; |
1215 | 1215 |
|
1216 | 1216 |
typedef MappableGraphComponent Graph; |
1217 | 1217 |
|
1218 | 1218 |
/// \brief Standard graph map for the edges. |
1219 | 1219 |
/// |
1220 | 1220 |
/// Standard graph map for the edges. |
1221 | 1221 |
/// It conforms to the ReferenceMap concept. |
1222 | 1222 |
template <typename V> |
1223 | 1223 |
class EdgeMap : public GraphMap<MappableGraphComponent, Edge, V> { |
1224 | 1224 |
public: |
1225 | 1225 |
typedef GraphMap<MappableGraphComponent, Edge, V> Parent; |
1226 | 1226 |
|
1227 | 1227 |
/// \brief Construct a new map. |
1228 | 1228 |
/// |
1229 | 1229 |
/// Construct a new map for the graph. |
1230 | 1230 |
explicit EdgeMap(const MappableGraphComponent& graph) |
1231 | 1231 |
: Parent(graph) {} |
1232 | 1232 |
|
1233 | 1233 |
/// \brief Construct a new map with default value. |
1234 | 1234 |
/// |
1235 | 1235 |
/// Construct a new map for the graph and initalize the values. |
1236 | 1236 |
EdgeMap(const MappableGraphComponent& graph, const V& value) |
1237 | 1237 |
: Parent(graph, value) {} |
1238 | 1238 |
|
1239 | 1239 |
private: |
1240 | 1240 |
/// \brief Copy constructor. |
1241 | 1241 |
/// |
1242 | 1242 |
/// Copy Constructor. |
1243 | 1243 |
EdgeMap(const EdgeMap& nm) : Parent(nm) {} |
1244 | 1244 |
|
1245 | 1245 |
/// \brief Assignment operator. |
1246 | 1246 |
/// |
1247 | 1247 |
/// Assignment operator. |
1248 | 1248 |
template <typename CMap> |
1249 | 1249 |
EdgeMap& operator=(const CMap&) { |
1250 | 1250 |
checkConcept<ReadMap<Edge, V>, CMap>(); |
1251 | 1251 |
return *this; |
1252 | 1252 |
} |
1253 | 1253 |
|
1254 | 1254 |
}; |
1255 | 1255 |
|
1256 | 1256 |
|
1257 | 1257 |
template <typename _Graph> |
1258 | 1258 |
struct Constraints { |
1259 | 1259 |
|
1260 | 1260 |
struct Dummy { |
1261 | 1261 |
int value; |
1262 | 1262 |
Dummy() : value(0) {} |
1263 | 1263 |
Dummy(int _v) : value(_v) {} |
1264 | 1264 |
}; |
1265 | 1265 |
|
1266 | 1266 |
void constraints() { |
1267 | 1267 |
checkConcept<MappableDigraphComponent<Base>, _Graph>(); |
1268 | 1268 |
|
1269 | 1269 |
{ // int map test |
1270 | 1270 |
typedef typename _Graph::template EdgeMap<int> IntEdgeMap; |
1271 | 1271 |
checkConcept<GraphMap<_Graph, typename _Graph::Edge, int>, |
1272 | 1272 |
IntEdgeMap >(); |
1273 | 1273 |
} { // bool map test |
1274 | 1274 |
typedef typename _Graph::template EdgeMap<bool> BoolEdgeMap; |
1275 | 1275 |
checkConcept<GraphMap<_Graph, typename _Graph::Edge, bool>, |
1276 | 1276 |
BoolEdgeMap >(); |
1277 | 1277 |
} { // Dummy map test |
1278 | 1278 |
typedef typename _Graph::template EdgeMap<Dummy> DummyEdgeMap; |
1279 | 1279 |
checkConcept<GraphMap<_Graph, typename _Graph::Edge, Dummy>, |
1280 | 1280 |
DummyEdgeMap >(); |
1281 | 1281 |
} |
1282 | 1282 |
} |
1283 | 1283 |
|
1284 | 1284 |
const _Graph& graph; |
1285 | 1285 |
}; |
1286 | 1286 |
}; |
1287 | 1287 |
|
1288 | 1288 |
/// \brief Skeleton class for extendable directed graphs. |
1289 | 1289 |
/// |
1290 | 1290 |
/// This class describes the interface of extendable directed graphs. |
1291 | 1291 |
/// It extends \ref BaseDigraphComponent with functions for adding |
1292 | 1292 |
/// nodes and arcs to the digraph. |
1293 | 1293 |
/// This concept requires \ref AlterableDigraphComponent. |
1294 | 1294 |
template <typename BAS = BaseDigraphComponent> |
1295 | 1295 |
class ExtendableDigraphComponent : public BAS { |
1296 | 1296 |
public: |
1297 | 1297 |
typedef BAS Base; |
1298 | 1298 |
|
1299 | 1299 |
typedef typename Base::Node Node; |
1300 | 1300 |
typedef typename Base::Arc Arc; |
1301 | 1301 |
|
1302 | 1302 |
/// \brief Add a new node to the digraph. |
1303 | 1303 |
/// |
1304 | 1304 |
/// This function adds a new node to the digraph. |
1305 | 1305 |
Node addNode() { |
1306 | 1306 |
return INVALID; |
1307 | 1307 |
} |
1308 | 1308 |
|
1309 | 1309 |
/// \brief Add a new arc connecting the given two nodes. |
1310 | 1310 |
/// |
1311 | 1311 |
/// This function adds a new arc connecting the given two nodes |
1312 | 1312 |
/// of the digraph. |
1313 | 1313 |
Arc addArc(const Node&, const Node&) { |
1314 | 1314 |
return INVALID; |
1315 | 1315 |
} |
1316 | 1316 |
|
1317 | 1317 |
template <typename _Digraph> |
1318 | 1318 |
struct Constraints { |
1319 | 1319 |
void constraints() { |
1320 | 1320 |
checkConcept<Base, _Digraph>(); |
1321 | 1321 |
typename _Digraph::Node node_a, node_b; |
1322 | 1322 |
node_a = digraph.addNode(); |
1323 | 1323 |
node_b = digraph.addNode(); |
1324 | 1324 |
typename _Digraph::Arc arc; |
1325 | 1325 |
arc = digraph.addArc(node_a, node_b); |
1326 | 1326 |
} |
1327 | 1327 |
|
1328 | 1328 |
_Digraph& digraph; |
1329 | 1329 |
}; |
1330 | 1330 |
}; |
1331 | 1331 |
|
1332 | 1332 |
/// \brief Skeleton class for extendable undirected graphs. |
1333 | 1333 |
/// |
1334 | 1334 |
/// This class describes the interface of extendable undirected graphs. |
1335 | 1335 |
/// It extends \ref BaseGraphComponent with functions for adding |
1336 | 1336 |
/// nodes and edges to the graph. |
1337 | 1337 |
/// This concept requires \ref AlterableGraphComponent. |
1338 | 1338 |
template <typename BAS = BaseGraphComponent> |
1339 | 1339 |
class ExtendableGraphComponent : public BAS { |
1340 | 1340 |
public: |
1341 | 1341 |
|
1342 | 1342 |
typedef BAS Base; |
1343 | 1343 |
typedef typename Base::Node Node; |
1344 | 1344 |
typedef typename Base::Edge Edge; |
1345 | 1345 |
|
1346 | 1346 |
/// \brief Add a new node to the digraph. |
1347 | 1347 |
/// |
1348 | 1348 |
/// This function adds a new node to the digraph. |
1349 | 1349 |
Node addNode() { |
1350 | 1350 |
return INVALID; |
1351 | 1351 |
} |
1352 | 1352 |
|
1353 | 1353 |
/// \brief Add a new edge connecting the given two nodes. |
1354 | 1354 |
/// |
1355 | 1355 |
/// This function adds a new edge connecting the given two nodes |
1356 | 1356 |
/// of the graph. |
1357 | 1357 |
Edge addEdge(const Node&, const Node&) { |
1358 | 1358 |
return INVALID; |
1359 | 1359 |
} |
1360 | 1360 |
|
1361 | 1361 |
template <typename _Graph> |
1362 | 1362 |
struct Constraints { |
1363 | 1363 |
void constraints() { |
1364 | 1364 |
checkConcept<Base, _Graph>(); |
1365 | 1365 |
typename _Graph::Node node_a, node_b; |
1366 | 1366 |
node_a = graph.addNode(); |
1367 | 1367 |
node_b = graph.addNode(); |
1368 | 1368 |
typename _Graph::Edge edge; |
1369 | 1369 |
edge = graph.addEdge(node_a, node_b); |
1370 | 1370 |
} |
1371 | 1371 |
|
1372 | 1372 |
_Graph& graph; |
1373 | 1373 |
}; |
1374 | 1374 |
}; |
1375 | 1375 |
|
1376 | 1376 |
/// \brief Skeleton class for erasable directed graphs. |
1377 | 1377 |
/// |
1378 | 1378 |
/// This class describes the interface of erasable directed graphs. |
1379 | 1379 |
/// It extends \ref BaseDigraphComponent with functions for removing |
1380 | 1380 |
/// nodes and arcs from the digraph. |
1381 | 1381 |
/// This concept requires \ref AlterableDigraphComponent. |
1382 | 1382 |
template <typename BAS = BaseDigraphComponent> |
1383 | 1383 |
class ErasableDigraphComponent : public BAS { |
1384 | 1384 |
public: |
1385 | 1385 |
|
1386 | 1386 |
typedef BAS Base; |
1387 | 1387 |
typedef typename Base::Node Node; |
1388 | 1388 |
typedef typename Base::Arc Arc; |
1389 | 1389 |
|
1390 | 1390 |
/// \brief Erase a node from the digraph. |
1391 | 1391 |
/// |
1392 | 1392 |
/// This function erases the given node from the digraph and all arcs |
1393 | 1393 |
/// connected to the node. |
1394 | 1394 |
void erase(const Node&) {} |
1395 | 1395 |
|
1396 | 1396 |
/// \brief Erase an arc from the digraph. |
1397 | 1397 |
/// |
1398 | 1398 |
/// This function erases the given arc from the digraph. |
1399 | 1399 |
void erase(const Arc&) {} |
1400 | 1400 |
|
1401 | 1401 |
template <typename _Digraph> |
1402 | 1402 |
struct Constraints { |
1403 | 1403 |
void constraints() { |
1404 | 1404 |
checkConcept<Base, _Digraph>(); |
1405 | 1405 |
const typename _Digraph::Node node(INVALID); |
1406 | 1406 |
digraph.erase(node); |
1407 | 1407 |
const typename _Digraph::Arc arc(INVALID); |
1408 | 1408 |
digraph.erase(arc); |
1409 | 1409 |
} |
1410 | 1410 |
|
1411 | 1411 |
_Digraph& digraph; |
1412 | 1412 |
}; |
1413 | 1413 |
}; |
1414 | 1414 |
|
1415 | 1415 |
/// \brief Skeleton class for erasable undirected graphs. |
1416 | 1416 |
/// |
1417 | 1417 |
/// This class describes the interface of erasable undirected graphs. |
1418 | 1418 |
/// It extends \ref BaseGraphComponent with functions for removing |
1419 | 1419 |
/// nodes and edges from the graph. |
1420 | 1420 |
/// This concept requires \ref AlterableGraphComponent. |
1421 | 1421 |
template <typename BAS = BaseGraphComponent> |
1422 | 1422 |
class ErasableGraphComponent : public BAS { |
1423 | 1423 |
public: |
1424 | 1424 |
|
1425 | 1425 |
typedef BAS Base; |
1426 | 1426 |
typedef typename Base::Node Node; |
1427 | 1427 |
typedef typename Base::Edge Edge; |
1428 | 1428 |
|
1429 | 1429 |
/// \brief Erase a node from the graph. |
1430 | 1430 |
/// |
1431 | 1431 |
/// This function erases the given node from the graph and all edges |
1432 | 1432 |
/// connected to the node. |
1433 | 1433 |
void erase(const Node&) {} |
1434 | 1434 |
|
1435 | 1435 |
/// \brief Erase an edge from the digraph. |
1436 | 1436 |
/// |
1437 | 1437 |
/// This function erases the given edge from the digraph. |
1438 | 1438 |
void erase(const Edge&) {} |
1439 | 1439 |
|
1440 | 1440 |
template <typename _Graph> |
1441 | 1441 |
struct Constraints { |
1442 | 1442 |
void constraints() { |
1443 | 1443 |
checkConcept<Base, _Graph>(); |
1444 | 1444 |
const typename _Graph::Node node(INVALID); |
1445 | 1445 |
graph.erase(node); |
1446 | 1446 |
const typename _Graph::Edge edge(INVALID); |
1447 | 1447 |
graph.erase(edge); |
1448 | 1448 |
} |
1449 | 1449 |
|
1450 | 1450 |
_Graph& graph; |
1451 | 1451 |
}; |
1452 | 1452 |
}; |
1453 | 1453 |
|
1454 | 1454 |
/// \brief Skeleton class for clearable directed graphs. |
1455 | 1455 |
/// |
1456 | 1456 |
/// This class describes the interface of clearable directed graphs. |
1457 | 1457 |
/// It extends \ref BaseDigraphComponent with a function for clearing |
1458 | 1458 |
/// the digraph. |
1459 | 1459 |
/// This concept requires \ref AlterableDigraphComponent. |
1460 | 1460 |
template <typename BAS = BaseDigraphComponent> |
1461 | 1461 |
class ClearableDigraphComponent : public BAS { |
1462 | 1462 |
public: |
1463 | 1463 |
|
1464 | 1464 |
typedef BAS Base; |
1465 | 1465 |
|
1466 | 1466 |
/// \brief Erase all nodes and arcs from the digraph. |
1467 | 1467 |
/// |
1468 | 1468 |
/// This function erases all nodes and arcs from the digraph. |
1469 | 1469 |
void clear() {} |
1470 | 1470 |
|
1471 | 1471 |
template <typename _Digraph> |
1472 | 1472 |
struct Constraints { |
1473 | 1473 |
void constraints() { |
1474 | 1474 |
checkConcept<Base, _Digraph>(); |
1475 | 1475 |
digraph.clear(); |
1476 | 1476 |
} |
1477 | 1477 |
|
1478 | 1478 |
_Digraph& digraph; |
1479 | 1479 |
}; |
1480 | 1480 |
}; |
1481 | 1481 |
|
1482 | 1482 |
/// \brief Skeleton class for clearable undirected graphs. |
1483 | 1483 |
/// |
1484 | 1484 |
/// This class describes the interface of clearable undirected graphs. |
1485 | 1485 |
/// It extends \ref BaseGraphComponent with a function for clearing |
1486 | 1486 |
/// the graph. |
1487 | 1487 |
/// This concept requires \ref AlterableGraphComponent. |
1488 | 1488 |
template <typename BAS = BaseGraphComponent> |
1489 | 1489 |
class ClearableGraphComponent : public ClearableDigraphComponent<BAS> { |
1490 | 1490 |
public: |
1491 | 1491 |
|
1492 | 1492 |
typedef BAS Base; |
1493 | 1493 |
|
1494 | 1494 |
/// \brief Erase all nodes and edges from the graph. |
1495 | 1495 |
/// |
1496 | 1496 |
/// This function erases all nodes and edges from the graph. |
1497 | 1497 |
void clear() {} |
1498 | 1498 |
|
1499 | 1499 |
template <typename _Graph> |
1500 | 1500 |
struct Constraints { |
1501 | 1501 |
void constraints() { |
1502 | 1502 |
checkConcept<Base, _Graph>(); |
1503 | 1503 |
graph.clear(); |
1504 | 1504 |
} |
1505 | 1505 |
|
1506 | 1506 |
_Graph& graph; |
1507 | 1507 |
}; |
1508 | 1508 |
}; |
1509 | 1509 |
|
1510 | 1510 |
} |
1511 | 1511 |
|
1512 | 1512 |
} |
1513 | 1513 |
|
1514 | 1514 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup concept |
20 | 20 |
///\file |
21 | 21 |
///\brief The concept of heaps. |
22 | 22 |
|
23 | 23 |
#ifndef LEMON_CONCEPTS_HEAP_H |
24 | 24 |
#define LEMON_CONCEPTS_HEAP_H |
25 | 25 |
|
26 | 26 |
#include <lemon/core.h> |
27 | 27 |
#include <lemon/concept_check.h> |
28 | 28 |
|
29 | 29 |
namespace lemon { |
30 | 30 |
|
31 | 31 |
namespace concepts { |
32 | 32 |
|
33 | 33 |
/// \addtogroup concept |
34 | 34 |
/// @{ |
35 | 35 |
|
36 | 36 |
/// \brief The heap concept. |
37 | 37 |
/// |
38 | 38 |
/// Concept class describing the main interface of heaps. A \e heap |
39 | 39 |
/// is a data structure for storing items with specified values called |
40 | 40 |
/// \e priorities in such a way that finding the item with minimum |
41 | 41 |
/// priority is efficient. In a heap one can change the priority of an |
42 | 42 |
/// item, add or erase an item, etc. |
43 | 43 |
/// |
44 | 44 |
/// \tparam PR Type of the priority of the items. |
45 | 45 |
/// \tparam IM A read and writable item map with int values, used |
46 | 46 |
/// internally to handle the cross references. |
47 | 47 |
/// \tparam Comp A functor class for the ordering of the priorities. |
48 | 48 |
/// The default is \c std::less<PR>. |
49 | 49 |
#ifdef DOXYGEN |
50 | 50 |
template <typename PR, typename IM, typename Comp = std::less<PR> > |
51 | 51 |
#else |
52 | 52 |
template <typename PR, typename IM> |
53 | 53 |
#endif |
54 | 54 |
class Heap { |
55 | 55 |
public: |
56 | 56 |
|
57 | 57 |
/// Type of the item-int map. |
58 | 58 |
typedef IM ItemIntMap; |
59 | 59 |
/// Type of the priorities. |
60 | 60 |
typedef PR Prio; |
61 | 61 |
/// Type of the items stored in the heap. |
62 | 62 |
typedef typename ItemIntMap::Key Item; |
63 | 63 |
|
64 | 64 |
/// \brief Type to represent the states of the items. |
65 | 65 |
/// |
66 | 66 |
/// Each item has a state associated to it. It can be "in heap", |
67 | 67 |
/// "pre heap" or "post heap". The later two are indifferent |
68 | 68 |
/// from the point of view of the heap, but may be useful for |
69 | 69 |
/// the user. |
70 | 70 |
/// |
71 | 71 |
/// The item-int map must be initialized in such way that it assigns |
72 | 72 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
73 | 73 |
enum State { |
74 |
IN_HEAP = 0, ///< The "in heap" state constant. |
|
75 |
PRE_HEAP = -1, ///< The "pre heap" state constant. |
|
76 |
|
|
74 |
IN_HEAP = 0, ///< = 0. The "in heap" state constant. |
|
75 |
PRE_HEAP = -1, ///< = -1. The "pre heap" state constant. |
|
76 |
POST_HEAP = -2 ///< = -2. The "post heap" state constant. |
|
77 | 77 |
}; |
78 | 78 |
|
79 | 79 |
/// \brief The constructor. |
80 | 80 |
/// |
81 | 81 |
/// The constructor. |
82 | 82 |
/// \param map A map that assigns \c int values to keys of type |
83 | 83 |
/// \c Item. It is used internally by the heap implementations to |
84 | 84 |
/// handle the cross references. The assigned value must be |
85 | 85 |
/// \c PRE_HEAP (<tt>-1</tt>) for every item. |
86 | 86 |
explicit Heap(ItemIntMap &map) {} |
87 | 87 |
|
88 | 88 |
/// \brief The number of items stored in the heap. |
89 | 89 |
/// |
90 | 90 |
/// Returns the number of items stored in the heap. |
91 | 91 |
int size() const { return 0; } |
92 | 92 |
|
93 | 93 |
/// \brief Checks if the heap is empty. |
94 | 94 |
/// |
95 | 95 |
/// Returns \c true if the heap is empty. |
96 | 96 |
bool empty() const { return false; } |
97 | 97 |
|
98 | 98 |
/// \brief Makes the heap empty. |
99 | 99 |
/// |
100 | 100 |
/// Makes the heap empty. |
101 | 101 |
void clear(); |
102 | 102 |
|
103 | 103 |
/// \brief Inserts an item into the heap with the given priority. |
104 | 104 |
/// |
105 | 105 |
/// Inserts the given item into the heap with the given priority. |
106 | 106 |
/// \param i The item to insert. |
107 | 107 |
/// \param p The priority of the item. |
108 | 108 |
void push(const Item &i, const Prio &p) {} |
109 | 109 |
|
110 | 110 |
/// \brief Returns the item having minimum priority. |
111 | 111 |
/// |
112 | 112 |
/// Returns the item having minimum priority. |
113 | 113 |
/// \pre The heap must be non-empty. |
114 | 114 |
Item top() const {} |
115 | 115 |
|
116 | 116 |
/// \brief The minimum priority. |
117 | 117 |
/// |
118 | 118 |
/// Returns the minimum priority. |
119 | 119 |
/// \pre The heap must be non-empty. |
120 | 120 |
Prio prio() const {} |
121 | 121 |
|
122 | 122 |
/// \brief Removes the item having minimum priority. |
123 | 123 |
/// |
124 | 124 |
/// Removes the item having minimum priority. |
125 | 125 |
/// \pre The heap must be non-empty. |
126 | 126 |
void pop() {} |
127 | 127 |
|
128 | 128 |
/// \brief Removes an item from the heap. |
129 | 129 |
/// |
130 | 130 |
/// Removes the given item from the heap if it is already stored. |
131 | 131 |
/// \param i The item to delete. |
132 | 132 |
void erase(const Item &i) {} |
133 | 133 |
|
134 | 134 |
/// \brief The priority of an item. |
135 | 135 |
/// |
136 | 136 |
/// Returns the priority of the given item. |
137 | 137 |
/// \param i The item. |
138 | 138 |
/// \pre \c i must be in the heap. |
139 | 139 |
Prio operator[](const Item &i) const {} |
140 | 140 |
|
141 | 141 |
/// \brief Sets the priority of an item or inserts it, if it is |
142 | 142 |
/// not stored in the heap. |
143 | 143 |
/// |
144 | 144 |
/// This method sets the priority of the given item if it is |
145 | 145 |
/// already stored in the heap. |
146 | 146 |
/// Otherwise it inserts the given item with the given priority. |
147 | 147 |
/// |
148 | 148 |
/// \param i The item. |
149 | 149 |
/// \param p The priority. |
150 | 150 |
void set(const Item &i, const Prio &p) {} |
151 | 151 |
|
152 | 152 |
/// \brief Decreases the priority of an item to the given value. |
153 | 153 |
/// |
154 | 154 |
/// Decreases the priority of an item to the given value. |
155 | 155 |
/// \param i The item. |
156 | 156 |
/// \param p The priority. |
157 | 157 |
/// \pre \c i must be stored in the heap with priority at least \c p. |
158 | 158 |
void decrease(const Item &i, const Prio &p) {} |
159 | 159 |
|
160 | 160 |
/// \brief Increases the priority of an item to the given value. |
161 | 161 |
/// |
162 | 162 |
/// Increases the priority of an item to the given value. |
163 | 163 |
/// \param i The item. |
164 | 164 |
/// \param p The priority. |
165 | 165 |
/// \pre \c i must be stored in the heap with priority at most \c p. |
166 | 166 |
void increase(const Item &i, const Prio &p) {} |
167 | 167 |
|
168 | 168 |
/// \brief Returns if an item is in, has already been in, or has |
169 | 169 |
/// never been in the heap. |
170 | 170 |
/// |
171 | 171 |
/// This method returns \c PRE_HEAP if the given item has never |
172 | 172 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
173 | 173 |
/// and \c POST_HEAP otherwise. |
174 | 174 |
/// In the latter case it is possible that the item will get back |
175 | 175 |
/// to the heap again. |
176 | 176 |
/// \param i The item. |
177 | 177 |
State state(const Item &i) const {} |
178 | 178 |
|
179 | 179 |
/// \brief Sets the state of an item in the heap. |
180 | 180 |
/// |
181 | 181 |
/// Sets the state of the given item in the heap. It can be used |
182 | 182 |
/// to manually clear the heap when it is important to achive the |
183 | 183 |
/// better time complexity. |
184 | 184 |
/// \param i The item. |
185 | 185 |
/// \param st The state. It should not be \c IN_HEAP. |
186 | 186 |
void state(const Item& i, State st) {} |
187 | 187 |
|
188 | 188 |
|
189 | 189 |
template <typename _Heap> |
190 | 190 |
struct Constraints { |
191 | 191 |
public: |
192 | 192 |
void constraints() { |
193 | 193 |
typedef typename _Heap::Item OwnItem; |
194 | 194 |
typedef typename _Heap::Prio OwnPrio; |
195 | 195 |
typedef typename _Heap::State OwnState; |
196 | 196 |
|
197 | 197 |
Item item; |
198 | 198 |
Prio prio; |
199 | 199 |
item=Item(); |
200 | 200 |
prio=Prio(); |
201 | 201 |
ignore_unused_variable_warning(item); |
202 | 202 |
ignore_unused_variable_warning(prio); |
203 | 203 |
|
204 | 204 |
OwnItem own_item; |
205 | 205 |
OwnPrio own_prio; |
206 | 206 |
OwnState own_state; |
207 | 207 |
own_item=Item(); |
208 | 208 |
own_prio=Prio(); |
209 | 209 |
ignore_unused_variable_warning(own_item); |
210 | 210 |
ignore_unused_variable_warning(own_prio); |
211 | 211 |
ignore_unused_variable_warning(own_state); |
212 | 212 |
|
213 | 213 |
_Heap heap1(map); |
214 | 214 |
_Heap heap2 = heap1; |
215 | 215 |
ignore_unused_variable_warning(heap1); |
216 | 216 |
ignore_unused_variable_warning(heap2); |
217 | 217 |
|
218 | 218 |
int s = heap.size(); |
219 | 219 |
ignore_unused_variable_warning(s); |
220 | 220 |
bool e = heap.empty(); |
221 | 221 |
ignore_unused_variable_warning(e); |
222 | 222 |
|
223 | 223 |
prio = heap.prio(); |
224 | 224 |
item = heap.top(); |
225 | 225 |
prio = heap[item]; |
226 | 226 |
own_prio = heap.prio(); |
227 | 227 |
own_item = heap.top(); |
228 | 228 |
own_prio = heap[own_item]; |
229 | 229 |
|
230 | 230 |
heap.push(item, prio); |
231 | 231 |
heap.push(own_item, own_prio); |
232 | 232 |
heap.pop(); |
233 | 233 |
|
234 | 234 |
heap.set(item, prio); |
235 | 235 |
heap.decrease(item, prio); |
236 | 236 |
heap.increase(item, prio); |
237 | 237 |
heap.set(own_item, own_prio); |
238 | 238 |
heap.decrease(own_item, own_prio); |
239 | 239 |
heap.increase(own_item, own_prio); |
240 | 240 |
|
241 | 241 |
heap.erase(item); |
242 | 242 |
heap.erase(own_item); |
243 | 243 |
heap.clear(); |
244 | 244 |
|
245 | 245 |
own_state = heap.state(own_item); |
246 | 246 |
heap.state(own_item, own_state); |
247 | 247 |
|
248 | 248 |
own_state = _Heap::PRE_HEAP; |
249 | 249 |
own_state = _Heap::IN_HEAP; |
250 | 250 |
own_state = _Heap::POST_HEAP; |
251 | 251 |
} |
252 | 252 |
|
253 | 253 |
_Heap& heap; |
254 | 254 |
ItemIntMap& map; |
255 | 255 |
}; |
256 | 256 |
}; |
257 | 257 |
|
258 | 258 |
/// @} |
259 | 259 |
} // namespace lemon |
260 | 260 |
} |
261 | 261 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_DFS_H |
20 | 20 |
#define LEMON_DFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 | 24 |
///\brief DFS algorithm. |
25 | 25 |
|
26 | 26 |
#include <lemon/list_graph.h> |
27 | 27 |
#include <lemon/bits/path_dump.h> |
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/error.h> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/path.h> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
///Default traits class of Dfs class. |
36 | 36 |
|
37 | 37 |
///Default traits class of Dfs class. |
38 | 38 |
///\tparam GR Digraph type. |
39 | 39 |
template<class GR> |
40 | 40 |
struct DfsDefaultTraits |
41 | 41 |
{ |
42 | 42 |
///The type of the digraph the algorithm runs on. |
43 | 43 |
typedef GR Digraph; |
44 | 44 |
|
45 | 45 |
///\brief The type of the map that stores the predecessor |
46 | 46 |
///arcs of the %DFS paths. |
47 | 47 |
/// |
48 | 48 |
///The type of the map that stores the predecessor |
49 | 49 |
///arcs of the %DFS paths. |
50 | 50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
52 | 52 |
///Instantiates a \c PredMap. |
53 | 53 |
|
54 | 54 |
///This function instantiates a \ref PredMap. |
55 | 55 |
///\param g is the digraph, to which we would like to define the |
56 | 56 |
///\ref PredMap. |
57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
58 | 58 |
{ |
59 | 59 |
return new PredMap(g); |
60 | 60 |
} |
61 | 61 |
|
62 | 62 |
///The type of the map that indicates which nodes are processed. |
63 | 63 |
|
64 | 64 |
///The type of the map that indicates which nodes are processed. |
65 | 65 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
66 | 66 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
67 | 67 |
///Instantiates a \c ProcessedMap. |
68 | 68 |
|
69 | 69 |
///This function instantiates a \ref ProcessedMap. |
70 | 70 |
///\param g is the digraph, to which |
71 | 71 |
///we would like to define the \ref ProcessedMap. |
72 | 72 |
#ifdef DOXYGEN |
73 | 73 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
74 | 74 |
#else |
75 | 75 |
static ProcessedMap *createProcessedMap(const Digraph &) |
76 | 76 |
#endif |
77 | 77 |
{ |
78 | 78 |
return new ProcessedMap(); |
79 | 79 |
} |
80 | 80 |
|
81 | 81 |
///The type of the map that indicates which nodes are reached. |
82 | 82 |
|
83 | 83 |
///The type of the map that indicates which nodes are reached. |
84 | 84 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
85 | 85 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
86 | 86 |
///Instantiates a \c ReachedMap. |
87 | 87 |
|
88 | 88 |
///This function instantiates a \ref ReachedMap. |
89 | 89 |
///\param g is the digraph, to which |
90 | 90 |
///we would like to define the \ref ReachedMap. |
91 | 91 |
static ReachedMap *createReachedMap(const Digraph &g) |
92 | 92 |
{ |
93 | 93 |
return new ReachedMap(g); |
94 | 94 |
} |
95 | 95 |
|
96 | 96 |
///The type of the map that stores the distances of the nodes. |
97 | 97 |
|
98 | 98 |
///The type of the map that stores the distances of the nodes. |
99 | 99 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
100 | 100 |
typedef typename Digraph::template NodeMap<int> DistMap; |
101 | 101 |
///Instantiates a \c DistMap. |
102 | 102 |
|
103 | 103 |
///This function instantiates a \ref DistMap. |
104 | 104 |
///\param g is the digraph, to which we would like to define the |
105 | 105 |
///\ref DistMap. |
106 | 106 |
static DistMap *createDistMap(const Digraph &g) |
107 | 107 |
{ |
108 | 108 |
return new DistMap(g); |
109 | 109 |
} |
110 | 110 |
}; |
111 | 111 |
|
112 | 112 |
///%DFS algorithm class. |
113 | 113 |
|
114 | 114 |
///\ingroup search |
115 | 115 |
///This class provides an efficient implementation of the %DFS algorithm. |
116 | 116 |
/// |
117 | 117 |
///There is also a \ref dfs() "function-type interface" for the DFS |
118 | 118 |
///algorithm, which is convenient in the simplier cases and it can be |
119 | 119 |
///used easier. |
120 | 120 |
/// |
121 | 121 |
///\tparam GR The type of the digraph the algorithm runs on. |
122 | 122 |
///The default type is \ref ListDigraph. |
123 | 123 |
#ifdef DOXYGEN |
124 | 124 |
template <typename GR, |
125 | 125 |
typename TR> |
126 | 126 |
#else |
127 | 127 |
template <typename GR=ListDigraph, |
128 | 128 |
typename TR=DfsDefaultTraits<GR> > |
129 | 129 |
#endif |
130 | 130 |
class Dfs { |
131 | 131 |
public: |
132 | 132 |
|
133 | 133 |
///The type of the digraph the algorithm runs on. |
134 | 134 |
typedef typename TR::Digraph Digraph; |
135 | 135 |
|
136 | 136 |
///\brief The type of the map that stores the predecessor arcs of the |
137 | 137 |
///DFS paths. |
138 | 138 |
typedef typename TR::PredMap PredMap; |
139 | 139 |
///The type of the map that stores the distances of the nodes. |
140 | 140 |
typedef typename TR::DistMap DistMap; |
141 | 141 |
///The type of the map that indicates which nodes are reached. |
142 | 142 |
typedef typename TR::ReachedMap ReachedMap; |
143 | 143 |
///The type of the map that indicates which nodes are processed. |
144 | 144 |
typedef typename TR::ProcessedMap ProcessedMap; |
145 | 145 |
///The type of the paths. |
146 | 146 |
typedef PredMapPath<Digraph, PredMap> Path; |
147 | 147 |
|
148 | 148 |
///The \ref DfsDefaultTraits "traits class" of the algorithm. |
149 | 149 |
typedef TR Traits; |
150 | 150 |
|
151 | 151 |
private: |
152 | 152 |
|
153 | 153 |
typedef typename Digraph::Node Node; |
154 | 154 |
typedef typename Digraph::NodeIt NodeIt; |
155 | 155 |
typedef typename Digraph::Arc Arc; |
156 | 156 |
typedef typename Digraph::OutArcIt OutArcIt; |
157 | 157 |
|
158 | 158 |
//Pointer to the underlying digraph. |
159 | 159 |
const Digraph *G; |
160 | 160 |
//Pointer to the map of predecessor arcs. |
161 | 161 |
PredMap *_pred; |
162 | 162 |
//Indicates if _pred is locally allocated (true) or not. |
163 | 163 |
bool local_pred; |
164 | 164 |
//Pointer to the map of distances. |
165 | 165 |
DistMap *_dist; |
166 | 166 |
//Indicates if _dist is locally allocated (true) or not. |
167 | 167 |
bool local_dist; |
168 | 168 |
//Pointer to the map of reached status of the nodes. |
169 | 169 |
ReachedMap *_reached; |
170 | 170 |
//Indicates if _reached is locally allocated (true) or not. |
171 | 171 |
bool local_reached; |
172 | 172 |
//Pointer to the map of processed status of the nodes. |
173 | 173 |
ProcessedMap *_processed; |
174 | 174 |
//Indicates if _processed is locally allocated (true) or not. |
175 | 175 |
bool local_processed; |
176 | 176 |
|
177 | 177 |
std::vector<typename Digraph::OutArcIt> _stack; |
178 | 178 |
int _stack_head; |
179 | 179 |
|
180 | 180 |
//Creates the maps if necessary. |
181 | 181 |
void create_maps() |
182 | 182 |
{ |
183 | 183 |
if(!_pred) { |
184 | 184 |
local_pred = true; |
185 | 185 |
_pred = Traits::createPredMap(*G); |
186 | 186 |
} |
187 | 187 |
if(!_dist) { |
188 | 188 |
local_dist = true; |
189 | 189 |
_dist = Traits::createDistMap(*G); |
190 | 190 |
} |
191 | 191 |
if(!_reached) { |
192 | 192 |
local_reached = true; |
193 | 193 |
_reached = Traits::createReachedMap(*G); |
194 | 194 |
} |
195 | 195 |
if(!_processed) { |
196 | 196 |
local_processed = true; |
197 | 197 |
_processed = Traits::createProcessedMap(*G); |
198 | 198 |
} |
199 | 199 |
} |
200 | 200 |
|
201 | 201 |
protected: |
202 | 202 |
|
203 | 203 |
Dfs() {} |
204 | 204 |
|
205 | 205 |
public: |
206 | 206 |
|
207 | 207 |
typedef Dfs Create; |
208 | 208 |
|
209 |
///\name Named |
|
209 |
///\name Named Template Parameters |
|
210 | 210 |
|
211 | 211 |
///@{ |
212 | 212 |
|
213 | 213 |
template <class T> |
214 | 214 |
struct SetPredMapTraits : public Traits { |
215 | 215 |
typedef T PredMap; |
216 | 216 |
static PredMap *createPredMap(const Digraph &) |
217 | 217 |
{ |
218 | 218 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
219 | 219 |
return 0; // ignore warnings |
220 | 220 |
} |
221 | 221 |
}; |
222 | 222 |
///\brief \ref named-templ-param "Named parameter" for setting |
223 | 223 |
///\c PredMap type. |
224 | 224 |
/// |
225 | 225 |
///\ref named-templ-param "Named parameter" for setting |
226 | 226 |
///\c PredMap type. |
227 | 227 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
228 | 228 |
template <class T> |
229 | 229 |
struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > { |
230 | 230 |
typedef Dfs<Digraph, SetPredMapTraits<T> > Create; |
231 | 231 |
}; |
232 | 232 |
|
233 | 233 |
template <class T> |
234 | 234 |
struct SetDistMapTraits : public Traits { |
235 | 235 |
typedef T DistMap; |
236 | 236 |
static DistMap *createDistMap(const Digraph &) |
237 | 237 |
{ |
238 | 238 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
239 | 239 |
return 0; // ignore warnings |
240 | 240 |
} |
241 | 241 |
}; |
242 | 242 |
///\brief \ref named-templ-param "Named parameter" for setting |
243 | 243 |
///\c DistMap type. |
244 | 244 |
/// |
245 | 245 |
///\ref named-templ-param "Named parameter" for setting |
246 | 246 |
///\c DistMap type. |
247 | 247 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
248 | 248 |
template <class T> |
249 | 249 |
struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > { |
250 | 250 |
typedef Dfs<Digraph, SetDistMapTraits<T> > Create; |
251 | 251 |
}; |
252 | 252 |
|
253 | 253 |
template <class T> |
254 | 254 |
struct SetReachedMapTraits : public Traits { |
255 | 255 |
typedef T ReachedMap; |
256 | 256 |
static ReachedMap *createReachedMap(const Digraph &) |
257 | 257 |
{ |
258 | 258 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
259 | 259 |
return 0; // ignore warnings |
260 | 260 |
} |
261 | 261 |
}; |
262 | 262 |
///\brief \ref named-templ-param "Named parameter" for setting |
263 | 263 |
///\c ReachedMap type. |
264 | 264 |
/// |
265 | 265 |
///\ref named-templ-param "Named parameter" for setting |
266 | 266 |
///\c ReachedMap type. |
267 | 267 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
268 | 268 |
template <class T> |
269 | 269 |
struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > { |
270 | 270 |
typedef Dfs< Digraph, SetReachedMapTraits<T> > Create; |
271 | 271 |
}; |
272 | 272 |
|
273 | 273 |
template <class T> |
274 | 274 |
struct SetProcessedMapTraits : public Traits { |
275 | 275 |
typedef T ProcessedMap; |
276 | 276 |
static ProcessedMap *createProcessedMap(const Digraph &) |
277 | 277 |
{ |
278 | 278 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
279 | 279 |
return 0; // ignore warnings |
280 | 280 |
} |
281 | 281 |
}; |
282 | 282 |
///\brief \ref named-templ-param "Named parameter" for setting |
283 | 283 |
///\c ProcessedMap type. |
284 | 284 |
/// |
285 | 285 |
///\ref named-templ-param "Named parameter" for setting |
286 | 286 |
///\c ProcessedMap type. |
287 | 287 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
288 | 288 |
template <class T> |
289 | 289 |
struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > { |
290 | 290 |
typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create; |
291 | 291 |
}; |
292 | 292 |
|
293 | 293 |
struct SetStandardProcessedMapTraits : public Traits { |
294 | 294 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
295 | 295 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
296 | 296 |
{ |
297 | 297 |
return new ProcessedMap(g); |
298 | 298 |
} |
299 | 299 |
}; |
300 | 300 |
///\brief \ref named-templ-param "Named parameter" for setting |
301 | 301 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
302 | 302 |
/// |
303 | 303 |
///\ref named-templ-param "Named parameter" for setting |
304 | 304 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
305 | 305 |
///If you don't set it explicitly, it will be automatically allocated. |
306 | 306 |
struct SetStandardProcessedMap : |
307 | 307 |
public Dfs< Digraph, SetStandardProcessedMapTraits > { |
308 | 308 |
typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create; |
309 | 309 |
}; |
310 | 310 |
|
311 | 311 |
///@} |
312 | 312 |
|
313 | 313 |
public: |
314 | 314 |
|
315 | 315 |
///Constructor. |
316 | 316 |
|
317 | 317 |
///Constructor. |
318 | 318 |
///\param g The digraph the algorithm runs on. |
319 | 319 |
Dfs(const Digraph &g) : |
320 | 320 |
G(&g), |
321 | 321 |
_pred(NULL), local_pred(false), |
322 | 322 |
_dist(NULL), local_dist(false), |
323 | 323 |
_reached(NULL), local_reached(false), |
324 | 324 |
_processed(NULL), local_processed(false) |
325 | 325 |
{ } |
326 | 326 |
|
327 | 327 |
///Destructor. |
328 | 328 |
~Dfs() |
329 | 329 |
{ |
330 | 330 |
if(local_pred) delete _pred; |
331 | 331 |
if(local_dist) delete _dist; |
332 | 332 |
if(local_reached) delete _reached; |
333 | 333 |
if(local_processed) delete _processed; |
334 | 334 |
} |
335 | 335 |
|
336 | 336 |
///Sets the map that stores the predecessor arcs. |
337 | 337 |
|
338 | 338 |
///Sets the map that stores the predecessor arcs. |
339 | 339 |
///If you don't use this function before calling \ref run(Node) "run()" |
340 | 340 |
///or \ref init(), an instance will be allocated automatically. |
341 | 341 |
///The destructor deallocates this automatically allocated map, |
342 | 342 |
///of course. |
343 | 343 |
///\return <tt> (*this) </tt> |
344 | 344 |
Dfs &predMap(PredMap &m) |
345 | 345 |
{ |
346 | 346 |
if(local_pred) { |
347 | 347 |
delete _pred; |
348 | 348 |
local_pred=false; |
349 | 349 |
} |
350 | 350 |
_pred = &m; |
351 | 351 |
return *this; |
352 | 352 |
} |
353 | 353 |
|
354 | 354 |
///Sets the map that indicates which nodes are reached. |
355 | 355 |
|
356 | 356 |
///Sets the map that indicates which nodes are reached. |
357 | 357 |
///If you don't use this function before calling \ref run(Node) "run()" |
358 | 358 |
///or \ref init(), an instance will be allocated automatically. |
359 | 359 |
///The destructor deallocates this automatically allocated map, |
360 | 360 |
///of course. |
361 | 361 |
///\return <tt> (*this) </tt> |
362 | 362 |
Dfs &reachedMap(ReachedMap &m) |
363 | 363 |
{ |
364 | 364 |
if(local_reached) { |
365 | 365 |
delete _reached; |
366 | 366 |
local_reached=false; |
367 | 367 |
} |
368 | 368 |
_reached = &m; |
369 | 369 |
return *this; |
370 | 370 |
} |
371 | 371 |
|
372 | 372 |
///Sets the map that indicates which nodes are processed. |
373 | 373 |
|
374 | 374 |
///Sets the map that indicates which nodes are processed. |
375 | 375 |
///If you don't use this function before calling \ref run(Node) "run()" |
376 | 376 |
///or \ref init(), an instance will be allocated automatically. |
377 | 377 |
///The destructor deallocates this automatically allocated map, |
378 | 378 |
///of course. |
379 | 379 |
///\return <tt> (*this) </tt> |
380 | 380 |
Dfs &processedMap(ProcessedMap &m) |
381 | 381 |
{ |
382 | 382 |
if(local_processed) { |
383 | 383 |
delete _processed; |
384 | 384 |
local_processed=false; |
385 | 385 |
} |
386 | 386 |
_processed = &m; |
387 | 387 |
return *this; |
388 | 388 |
} |
389 | 389 |
|
390 | 390 |
///Sets the map that stores the distances of the nodes. |
391 | 391 |
|
392 | 392 |
///Sets the map that stores the distances of the nodes calculated by |
393 | 393 |
///the algorithm. |
394 | 394 |
///If you don't use this function before calling \ref run(Node) "run()" |
395 | 395 |
///or \ref init(), an instance will be allocated automatically. |
396 | 396 |
///The destructor deallocates this automatically allocated map, |
397 | 397 |
///of course. |
398 | 398 |
///\return <tt> (*this) </tt> |
399 | 399 |
Dfs &distMap(DistMap &m) |
400 | 400 |
{ |
401 | 401 |
if(local_dist) { |
402 | 402 |
delete _dist; |
403 | 403 |
local_dist=false; |
404 | 404 |
} |
405 | 405 |
_dist = &m; |
406 | 406 |
return *this; |
407 | 407 |
} |
408 | 408 |
|
409 | 409 |
public: |
410 | 410 |
|
411 | 411 |
///\name Execution Control |
412 | 412 |
///The simplest way to execute the DFS algorithm is to use one of the |
413 | 413 |
///member functions called \ref run(Node) "run()".\n |
414 | 414 |
///If you need more control on the execution, first you have to call |
415 | 415 |
///\ref init(), then you can add a source node with \ref addSource() |
416 | 416 |
///and perform the actual computation with \ref start(). |
417 | 417 |
///This procedure can be repeated if there are nodes that have not |
418 | 418 |
///been reached. |
419 | 419 |
|
420 | 420 |
///@{ |
421 | 421 |
|
422 | 422 |
///\brief Initializes the internal data structures. |
423 | 423 |
/// |
424 | 424 |
///Initializes the internal data structures. |
425 | 425 |
void init() |
426 | 426 |
{ |
427 | 427 |
create_maps(); |
428 | 428 |
_stack.resize(countNodes(*G)); |
429 | 429 |
_stack_head=-1; |
430 | 430 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
431 | 431 |
_pred->set(u,INVALID); |
432 | 432 |
_reached->set(u,false); |
433 | 433 |
_processed->set(u,false); |
434 | 434 |
} |
435 | 435 |
} |
436 | 436 |
|
437 | 437 |
///Adds a new source node. |
438 | 438 |
|
439 | 439 |
///Adds a new source node to the set of nodes to be processed. |
440 | 440 |
/// |
441 | 441 |
///\pre The stack must be empty. Otherwise the algorithm gives |
442 | 442 |
///wrong results. (One of the outgoing arcs of all the source nodes |
443 | 443 |
///except for the last one will not be visited and distances will |
444 | 444 |
///also be wrong.) |
445 | 445 |
void addSource(Node s) |
446 | 446 |
{ |
447 | 447 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
448 | 448 |
if(!(*_reached)[s]) |
449 | 449 |
{ |
450 | 450 |
_reached->set(s,true); |
451 | 451 |
_pred->set(s,INVALID); |
452 | 452 |
OutArcIt e(*G,s); |
453 | 453 |
if(e!=INVALID) { |
454 | 454 |
_stack[++_stack_head]=e; |
455 | 455 |
_dist->set(s,_stack_head); |
456 | 456 |
} |
457 | 457 |
else { |
458 | 458 |
_processed->set(s,true); |
459 | 459 |
_dist->set(s,0); |
460 | 460 |
} |
461 | 461 |
} |
462 | 462 |
} |
463 | 463 |
|
464 | 464 |
///Processes the next arc. |
465 | 465 |
|
466 | 466 |
///Processes the next arc. |
467 | 467 |
/// |
468 | 468 |
///\return The processed arc. |
469 | 469 |
/// |
470 | 470 |
///\pre The stack must not be empty. |
471 | 471 |
Arc processNextArc() |
472 | 472 |
{ |
473 | 473 |
Node m; |
474 | 474 |
Arc e=_stack[_stack_head]; |
475 | 475 |
if(!(*_reached)[m=G->target(e)]) { |
476 | 476 |
_pred->set(m,e); |
477 | 477 |
_reached->set(m,true); |
478 | 478 |
++_stack_head; |
479 | 479 |
_stack[_stack_head] = OutArcIt(*G, m); |
480 | 480 |
_dist->set(m,_stack_head); |
481 | 481 |
} |
482 | 482 |
else { |
483 | 483 |
m=G->source(e); |
484 | 484 |
++_stack[_stack_head]; |
485 | 485 |
} |
486 | 486 |
while(_stack_head>=0 && _stack[_stack_head]==INVALID) { |
487 | 487 |
_processed->set(m,true); |
488 | 488 |
--_stack_head; |
489 | 489 |
if(_stack_head>=0) { |
490 | 490 |
m=G->source(_stack[_stack_head]); |
491 | 491 |
++_stack[_stack_head]; |
492 | 492 |
} |
493 | 493 |
} |
494 | 494 |
return e; |
495 | 495 |
} |
496 | 496 |
|
497 | 497 |
///Next arc to be processed. |
498 | 498 |
|
499 | 499 |
///Next arc to be processed. |
500 | 500 |
/// |
501 | 501 |
///\return The next arc to be processed or \c INVALID if the stack |
502 | 502 |
///is empty. |
503 | 503 |
OutArcIt nextArc() const |
504 | 504 |
{ |
505 | 505 |
return _stack_head>=0?_stack[_stack_head]:INVALID; |
506 | 506 |
} |
507 | 507 |
|
508 | 508 |
///Returns \c false if there are nodes to be processed. |
509 | 509 |
|
510 | 510 |
///Returns \c false if there are nodes to be processed |
511 | 511 |
///in the queue (stack). |
512 | 512 |
bool emptyQueue() const { return _stack_head<0; } |
513 | 513 |
|
514 | 514 |
///Returns the number of the nodes to be processed. |
515 | 515 |
|
516 | 516 |
///Returns the number of the nodes to be processed |
517 | 517 |
///in the queue (stack). |
518 | 518 |
int queueSize() const { return _stack_head+1; } |
519 | 519 |
|
520 | 520 |
///Executes the algorithm. |
521 | 521 |
|
522 | 522 |
///Executes the algorithm. |
523 | 523 |
/// |
524 | 524 |
///This method runs the %DFS algorithm from the root node |
525 | 525 |
///in order to compute the DFS path to each node. |
526 | 526 |
/// |
527 | 527 |
/// The algorithm computes |
528 | 528 |
///- the %DFS tree, |
529 | 529 |
///- the distance of each node from the root in the %DFS tree. |
530 | 530 |
/// |
531 | 531 |
///\pre init() must be called and a root node should be |
532 | 532 |
///added with addSource() before using this function. |
533 | 533 |
/// |
534 | 534 |
///\note <tt>d.start()</tt> is just a shortcut of the following code. |
535 | 535 |
///\code |
536 | 536 |
/// while ( !d.emptyQueue() ) { |
537 | 537 |
/// d.processNextArc(); |
538 | 538 |
/// } |
539 | 539 |
///\endcode |
540 | 540 |
void start() |
541 | 541 |
{ |
542 | 542 |
while ( !emptyQueue() ) processNextArc(); |
543 | 543 |
} |
544 | 544 |
|
545 | 545 |
///Executes the algorithm until the given target node is reached. |
546 | 546 |
|
547 | 547 |
///Executes the algorithm until the given target node is reached. |
548 | 548 |
/// |
549 | 549 |
///This method runs the %DFS algorithm from the root node |
550 | 550 |
///in order to compute the DFS path to \c t. |
551 | 551 |
/// |
552 | 552 |
///The algorithm computes |
553 | 553 |
///- the %DFS path to \c t, |
554 | 554 |
///- the distance of \c t from the root in the %DFS tree. |
555 | 555 |
/// |
556 | 556 |
///\pre init() must be called and a root node should be |
557 | 557 |
///added with addSource() before using this function. |
558 | 558 |
void start(Node t) |
559 | 559 |
{ |
560 | 560 |
while ( !emptyQueue() && G->target(_stack[_stack_head])!=t ) |
561 | 561 |
processNextArc(); |
562 | 562 |
} |
563 | 563 |
|
564 | 564 |
///Executes the algorithm until a condition is met. |
565 | 565 |
|
566 | 566 |
///Executes the algorithm until a condition is met. |
567 | 567 |
/// |
568 | 568 |
///This method runs the %DFS algorithm from the root node |
569 | 569 |
///until an arc \c a with <tt>am[a]</tt> true is found. |
570 | 570 |
/// |
571 | 571 |
///\param am A \c bool (or convertible) arc map. The algorithm |
572 | 572 |
///will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
573 | 573 |
/// |
574 | 574 |
///\return The reached arc \c a with <tt>am[a]</tt> true or |
575 | 575 |
///\c INVALID if no such arc was found. |
576 | 576 |
/// |
577 | 577 |
///\pre init() must be called and a root node should be |
578 | 578 |
///added with addSource() before using this function. |
579 | 579 |
/// |
580 | 580 |
///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
581 | 581 |
///not a node map. |
582 | 582 |
template<class ArcBoolMap> |
583 | 583 |
Arc start(const ArcBoolMap &am) |
584 | 584 |
{ |
585 | 585 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
586 | 586 |
processNextArc(); |
587 | 587 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
588 | 588 |
} |
589 | 589 |
|
590 | 590 |
///Runs the algorithm from the given source node. |
591 | 591 |
|
592 | 592 |
///This method runs the %DFS algorithm from node \c s |
593 | 593 |
///in order to compute the DFS path to each node. |
594 | 594 |
/// |
595 | 595 |
///The algorithm computes |
596 | 596 |
///- the %DFS tree, |
597 | 597 |
///- the distance of each node from the root in the %DFS tree. |
598 | 598 |
/// |
599 | 599 |
///\note <tt>d.run(s)</tt> is just a shortcut of the following code. |
600 | 600 |
///\code |
601 | 601 |
/// d.init(); |
602 | 602 |
/// d.addSource(s); |
603 | 603 |
/// d.start(); |
604 | 604 |
///\endcode |
605 | 605 |
void run(Node s) { |
606 | 606 |
init(); |
607 | 607 |
addSource(s); |
608 | 608 |
start(); |
609 | 609 |
} |
610 | 610 |
|
611 | 611 |
///Finds the %DFS path between \c s and \c t. |
612 | 612 |
|
613 | 613 |
///This method runs the %DFS algorithm from node \c s |
614 | 614 |
///in order to compute the DFS path to node \c t |
615 | 615 |
///(it stops searching when \c t is processed) |
616 | 616 |
/// |
617 | 617 |
///\return \c true if \c t is reachable form \c s. |
618 | 618 |
/// |
619 | 619 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is |
620 | 620 |
///just a shortcut of the following code. |
621 | 621 |
///\code |
622 | 622 |
/// d.init(); |
623 | 623 |
/// d.addSource(s); |
624 | 624 |
/// d.start(t); |
625 | 625 |
///\endcode |
626 | 626 |
bool run(Node s,Node t) { |
627 | 627 |
init(); |
628 | 628 |
addSource(s); |
629 | 629 |
start(t); |
630 | 630 |
return reached(t); |
631 | 631 |
} |
632 | 632 |
|
633 | 633 |
///Runs the algorithm to visit all nodes in the digraph. |
634 | 634 |
|
635 | 635 |
///This method runs the %DFS algorithm in order to compute the |
636 | 636 |
///%DFS path to each node. |
637 | 637 |
/// |
638 | 638 |
///The algorithm computes |
639 | 639 |
///- the %DFS tree (forest), |
640 | 640 |
///- the distance of each node from the root(s) in the %DFS tree. |
641 | 641 |
/// |
642 | 642 |
///\note <tt>d.run()</tt> is just a shortcut of the following code. |
643 | 643 |
///\code |
644 | 644 |
/// d.init(); |
645 | 645 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
646 | 646 |
/// if (!d.reached(n)) { |
647 | 647 |
/// d.addSource(n); |
648 | 648 |
/// d.start(); |
649 | 649 |
/// } |
650 | 650 |
/// } |
651 | 651 |
///\endcode |
652 | 652 |
void run() { |
653 | 653 |
init(); |
654 | 654 |
for (NodeIt it(*G); it != INVALID; ++it) { |
655 | 655 |
if (!reached(it)) { |
656 | 656 |
addSource(it); |
657 | 657 |
start(); |
658 | 658 |
} |
659 | 659 |
} |
660 | 660 |
} |
661 | 661 |
|
662 | 662 |
///@} |
663 | 663 |
|
664 | 664 |
///\name Query Functions |
665 | 665 |
///The results of the DFS algorithm can be obtained using these |
666 | 666 |
///functions.\n |
667 | 667 |
///Either \ref run(Node) "run()" or \ref start() should be called |
668 | 668 |
///before using them. |
669 | 669 |
|
670 | 670 |
///@{ |
671 | 671 |
|
672 | 672 |
///The DFS path to a node. |
673 | 673 |
|
674 | 674 |
///Returns the DFS path to a node. |
675 | 675 |
/// |
676 | 676 |
///\warning \c t should be reached from the root(s). |
677 | 677 |
/// |
678 | 678 |
///\pre Either \ref run(Node) "run()" or \ref init() |
679 | 679 |
///must be called before using this function. |
680 | 680 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
681 | 681 |
|
682 | 682 |
///The distance of a node from the root(s). |
683 | 683 |
|
684 | 684 |
///Returns the distance of a node from the root(s). |
685 | 685 |
/// |
686 | 686 |
///\warning If node \c v is not reached from the root(s), then |
687 | 687 |
///the return value of this function is undefined. |
688 | 688 |
/// |
689 | 689 |
///\pre Either \ref run(Node) "run()" or \ref init() |
690 | 690 |
///must be called before using this function. |
691 | 691 |
int dist(Node v) const { return (*_dist)[v]; } |
692 | 692 |
|
693 | 693 |
///Returns the 'previous arc' of the %DFS tree for a node. |
694 | 694 |
|
695 | 695 |
///This function returns the 'previous arc' of the %DFS tree for the |
696 | 696 |
///node \c v, i.e. it returns the last arc of a %DFS path from a |
697 | 697 |
///root to \c v. It is \c INVALID if \c v is not reached from the |
698 | 698 |
///root(s) or if \c v is a root. |
699 | 699 |
/// |
700 | 700 |
///The %DFS tree used here is equal to the %DFS tree used in |
701 | 701 |
///\ref predNode(). |
702 | 702 |
/// |
703 | 703 |
///\pre Either \ref run(Node) "run()" or \ref init() |
704 | 704 |
///must be called before using this function. |
705 | 705 |
Arc predArc(Node v) const { return (*_pred)[v];} |
706 | 706 |
|
707 | 707 |
///Returns the 'previous node' of the %DFS tree. |
708 | 708 |
|
709 | 709 |
///This function returns the 'previous node' of the %DFS |
710 | 710 |
///tree for the node \c v, i.e. it returns the last but one node |
711 | 711 |
///from a %DFS path from a root to \c v. It is \c INVALID |
712 | 712 |
///if \c v is not reached from the root(s) or if \c v is a root. |
713 | 713 |
/// |
714 | 714 |
///The %DFS tree used here is equal to the %DFS tree used in |
715 | 715 |
///\ref predArc(). |
716 | 716 |
/// |
717 | 717 |
///\pre Either \ref run(Node) "run()" or \ref init() |
718 | 718 |
///must be called before using this function. |
719 | 719 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
720 | 720 |
G->source((*_pred)[v]); } |
721 | 721 |
|
722 | 722 |
///\brief Returns a const reference to the node map that stores the |
723 | 723 |
///distances of the nodes. |
724 | 724 |
/// |
725 | 725 |
///Returns a const reference to the node map that stores the |
726 | 726 |
///distances of the nodes calculated by the algorithm. |
727 | 727 |
/// |
728 | 728 |
///\pre Either \ref run(Node) "run()" or \ref init() |
729 | 729 |
///must be called before using this function. |
730 | 730 |
const DistMap &distMap() const { return *_dist;} |
731 | 731 |
|
732 | 732 |
///\brief Returns a const reference to the node map that stores the |
733 | 733 |
///predecessor arcs. |
734 | 734 |
/// |
735 | 735 |
///Returns a const reference to the node map that stores the predecessor |
736 | 736 |
///arcs, which form the DFS tree. |
737 | 737 |
/// |
738 | 738 |
///\pre Either \ref run(Node) "run()" or \ref init() |
739 | 739 |
///must be called before using this function. |
740 | 740 |
const PredMap &predMap() const { return *_pred;} |
741 | 741 |
|
742 | 742 |
///Checks if a node is reached from the root(s). |
743 | 743 |
|
744 | 744 |
///Returns \c true if \c v is reached from the root(s). |
745 | 745 |
/// |
746 | 746 |
///\pre Either \ref run(Node) "run()" or \ref init() |
747 | 747 |
///must be called before using this function. |
748 | 748 |
bool reached(Node v) const { return (*_reached)[v]; } |
749 | 749 |
|
750 | 750 |
///@} |
751 | 751 |
}; |
752 | 752 |
|
753 | 753 |
///Default traits class of dfs() function. |
754 | 754 |
|
755 | 755 |
///Default traits class of dfs() function. |
756 | 756 |
///\tparam GR Digraph type. |
757 | 757 |
template<class GR> |
758 | 758 |
struct DfsWizardDefaultTraits |
759 | 759 |
{ |
760 | 760 |
///The type of the digraph the algorithm runs on. |
761 | 761 |
typedef GR Digraph; |
762 | 762 |
|
763 | 763 |
///\brief The type of the map that stores the predecessor |
764 | 764 |
///arcs of the %DFS paths. |
765 | 765 |
/// |
766 | 766 |
///The type of the map that stores the predecessor |
767 | 767 |
///arcs of the %DFS paths. |
768 | 768 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
769 | 769 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
770 | 770 |
///Instantiates a PredMap. |
771 | 771 |
|
772 | 772 |
///This function instantiates a PredMap. |
773 | 773 |
///\param g is the digraph, to which we would like to define the |
774 | 774 |
///PredMap. |
775 | 775 |
static PredMap *createPredMap(const Digraph &g) |
776 | 776 |
{ |
777 | 777 |
return new PredMap(g); |
778 | 778 |
} |
779 | 779 |
|
780 | 780 |
///The type of the map that indicates which nodes are processed. |
781 | 781 |
|
782 | 782 |
///The type of the map that indicates which nodes are processed. |
783 | 783 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
784 | 784 |
///By default it is a NullMap. |
785 | 785 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
786 | 786 |
///Instantiates a ProcessedMap. |
787 | 787 |
|
788 | 788 |
///This function instantiates a ProcessedMap. |
789 | 789 |
///\param g is the digraph, to which |
790 | 790 |
///we would like to define the ProcessedMap. |
791 | 791 |
#ifdef DOXYGEN |
792 | 792 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
793 | 793 |
#else |
794 | 794 |
static ProcessedMap *createProcessedMap(const Digraph &) |
795 | 795 |
#endif |
796 | 796 |
{ |
797 | 797 |
return new ProcessedMap(); |
798 | 798 |
} |
799 | 799 |
|
800 | 800 |
///The type of the map that indicates which nodes are reached. |
801 | 801 |
|
802 | 802 |
///The type of the map that indicates which nodes are reached. |
803 | 803 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
804 | 804 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
805 | 805 |
///Instantiates a ReachedMap. |
806 | 806 |
|
807 | 807 |
///This function instantiates a ReachedMap. |
808 | 808 |
///\param g is the digraph, to which |
809 | 809 |
///we would like to define the ReachedMap. |
810 | 810 |
static ReachedMap *createReachedMap(const Digraph &g) |
811 | 811 |
{ |
812 | 812 |
return new ReachedMap(g); |
813 | 813 |
} |
814 | 814 |
|
815 | 815 |
///The type of the map that stores the distances of the nodes. |
816 | 816 |
|
817 | 817 |
///The type of the map that stores the distances of the nodes. |
818 | 818 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
819 | 819 |
typedef typename Digraph::template NodeMap<int> DistMap; |
820 | 820 |
///Instantiates a DistMap. |
821 | 821 |
|
822 | 822 |
///This function instantiates a DistMap. |
823 | 823 |
///\param g is the digraph, to which we would like to define |
824 | 824 |
///the DistMap |
825 | 825 |
static DistMap *createDistMap(const Digraph &g) |
826 | 826 |
{ |
827 | 827 |
return new DistMap(g); |
828 | 828 |
} |
829 | 829 |
|
830 | 830 |
///The type of the DFS paths. |
831 | 831 |
|
832 | 832 |
///The type of the DFS paths. |
833 | 833 |
///It must meet the \ref concepts::Path "Path" concept. |
834 | 834 |
typedef lemon::Path<Digraph> Path; |
835 | 835 |
}; |
836 | 836 |
|
837 | 837 |
/// Default traits class used by DfsWizard |
838 | 838 |
|
839 | 839 |
/// To make it easier to use Dfs algorithm |
840 | 840 |
/// we have created a wizard class. |
841 | 841 |
/// This \ref DfsWizard class needs default traits, |
842 | 842 |
/// as well as the \ref Dfs class. |
843 | 843 |
/// The \ref DfsWizardBase is a class to be the default traits of the |
844 | 844 |
/// \ref DfsWizard class. |
845 | 845 |
template<class GR> |
846 | 846 |
class DfsWizardBase : public DfsWizardDefaultTraits<GR> |
847 | 847 |
{ |
848 | 848 |
|
849 | 849 |
typedef DfsWizardDefaultTraits<GR> Base; |
850 | 850 |
protected: |
851 | 851 |
//The type of the nodes in the digraph. |
852 | 852 |
typedef typename Base::Digraph::Node Node; |
853 | 853 |
|
854 | 854 |
//Pointer to the digraph the algorithm runs on. |
855 | 855 |
void *_g; |
856 | 856 |
//Pointer to the map of reached nodes. |
857 | 857 |
void *_reached; |
858 | 858 |
//Pointer to the map of processed nodes. |
859 | 859 |
void *_processed; |
860 | 860 |
//Pointer to the map of predecessors arcs. |
861 | 861 |
void *_pred; |
862 | 862 |
//Pointer to the map of distances. |
863 | 863 |
void *_dist; |
864 | 864 |
//Pointer to the DFS path to the target node. |
865 | 865 |
void *_path; |
866 | 866 |
//Pointer to the distance of the target node. |
867 | 867 |
int *_di; |
868 | 868 |
|
869 | 869 |
public: |
870 | 870 |
/// Constructor. |
871 | 871 |
|
872 | 872 |
/// This constructor does not require parameters, therefore it initiates |
873 | 873 |
/// all of the attributes to \c 0. |
874 | 874 |
DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
875 | 875 |
_dist(0), _path(0), _di(0) {} |
876 | 876 |
|
877 | 877 |
/// Constructor. |
878 | 878 |
|
879 | 879 |
/// This constructor requires one parameter, |
880 | 880 |
/// others are initiated to \c 0. |
881 | 881 |
/// \param g The digraph the algorithm runs on. |
882 | 882 |
DfsWizardBase(const GR &g) : |
883 | 883 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
884 | 884 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
885 | 885 |
|
886 | 886 |
}; |
887 | 887 |
|
888 | 888 |
/// Auxiliary class for the function-type interface of DFS algorithm. |
889 | 889 |
|
890 | 890 |
/// This auxiliary class is created to implement the |
891 | 891 |
/// \ref dfs() "function-type interface" of \ref Dfs algorithm. |
892 | 892 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
893 | 893 |
/// functions and features of the plain \ref Dfs. |
894 | 894 |
/// |
895 | 895 |
/// This class should only be used through the \ref dfs() function, |
896 | 896 |
/// which makes it easier to use the algorithm. |
897 | 897 |
template<class TR> |
898 | 898 |
class DfsWizard : public TR |
899 | 899 |
{ |
900 | 900 |
typedef TR Base; |
901 | 901 |
|
902 | 902 |
///The type of the digraph the algorithm runs on. |
903 | 903 |
typedef typename TR::Digraph Digraph; |
904 | 904 |
|
905 | 905 |
typedef typename Digraph::Node Node; |
906 | 906 |
typedef typename Digraph::NodeIt NodeIt; |
907 | 907 |
typedef typename Digraph::Arc Arc; |
908 | 908 |
typedef typename Digraph::OutArcIt OutArcIt; |
909 | 909 |
|
910 | 910 |
///\brief The type of the map that stores the predecessor |
911 | 911 |
///arcs of the DFS paths. |
912 | 912 |
typedef typename TR::PredMap PredMap; |
913 | 913 |
///\brief The type of the map that stores the distances of the nodes. |
914 | 914 |
typedef typename TR::DistMap DistMap; |
915 | 915 |
///\brief The type of the map that indicates which nodes are reached. |
916 | 916 |
typedef typename TR::ReachedMap ReachedMap; |
917 | 917 |
///\brief The type of the map that indicates which nodes are processed. |
918 | 918 |
typedef typename TR::ProcessedMap ProcessedMap; |
919 | 919 |
///The type of the DFS paths |
920 | 920 |
typedef typename TR::Path Path; |
921 | 921 |
|
922 | 922 |
public: |
923 | 923 |
|
924 | 924 |
/// Constructor. |
925 | 925 |
DfsWizard() : TR() {} |
926 | 926 |
|
927 | 927 |
/// Constructor that requires parameters. |
928 | 928 |
|
929 | 929 |
/// Constructor that requires parameters. |
930 | 930 |
/// These parameters will be the default values for the traits class. |
931 | 931 |
/// \param g The digraph the algorithm runs on. |
932 | 932 |
DfsWizard(const Digraph &g) : |
933 | 933 |
TR(g) {} |
934 | 934 |
|
935 | 935 |
///Copy constructor |
936 | 936 |
DfsWizard(const TR &b) : TR(b) {} |
937 | 937 |
|
938 | 938 |
~DfsWizard() {} |
939 | 939 |
|
940 | 940 |
///Runs DFS algorithm from the given source node. |
941 | 941 |
|
942 | 942 |
///This method runs DFS algorithm from node \c s |
943 | 943 |
///in order to compute the DFS path to each node. |
944 | 944 |
void run(Node s) |
945 | 945 |
{ |
946 | 946 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
947 | 947 |
if (Base::_pred) |
948 | 948 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
949 | 949 |
if (Base::_dist) |
950 | 950 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
951 | 951 |
if (Base::_reached) |
952 | 952 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
953 | 953 |
if (Base::_processed) |
954 | 954 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
955 | 955 |
if (s!=INVALID) |
956 | 956 |
alg.run(s); |
957 | 957 |
else |
958 | 958 |
alg.run(); |
959 | 959 |
} |
960 | 960 |
|
961 | 961 |
///Finds the DFS path between \c s and \c t. |
962 | 962 |
|
963 | 963 |
///This method runs DFS algorithm from node \c s |
964 | 964 |
///in order to compute the DFS path to node \c t |
965 | 965 |
///(it stops searching when \c t is processed). |
966 | 966 |
/// |
967 | 967 |
///\return \c true if \c t is reachable form \c s. |
968 | 968 |
bool run(Node s, Node t) |
969 | 969 |
{ |
970 | 970 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
971 | 971 |
if (Base::_pred) |
972 | 972 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
973 | 973 |
if (Base::_dist) |
974 | 974 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
975 | 975 |
if (Base::_reached) |
976 | 976 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
977 | 977 |
if (Base::_processed) |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_DIJKSTRA_H |
20 | 20 |
#define LEMON_DIJKSTRA_H |
21 | 21 |
|
22 | 22 |
///\ingroup shortest_path |
23 | 23 |
///\file |
24 | 24 |
///\brief Dijkstra algorithm. |
25 | 25 |
|
26 | 26 |
#include <limits> |
27 | 27 |
#include <lemon/list_graph.h> |
28 | 28 |
#include <lemon/bin_heap.h> |
29 | 29 |
#include <lemon/bits/path_dump.h> |
30 | 30 |
#include <lemon/core.h> |
31 | 31 |
#include <lemon/error.h> |
32 | 32 |
#include <lemon/maps.h> |
33 | 33 |
#include <lemon/path.h> |
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
/// \brief Default operation traits for the Dijkstra algorithm class. |
38 | 38 |
/// |
39 | 39 |
/// This operation traits class defines all computational operations and |
40 | 40 |
/// constants which are used in the Dijkstra algorithm. |
41 | 41 |
template <typename V> |
42 | 42 |
struct DijkstraDefaultOperationTraits { |
43 | 43 |
/// \e |
44 | 44 |
typedef V Value; |
45 | 45 |
/// \brief Gives back the zero value of the type. |
46 | 46 |
static Value zero() { |
47 | 47 |
return static_cast<Value>(0); |
48 | 48 |
} |
49 | 49 |
/// \brief Gives back the sum of the given two elements. |
50 | 50 |
static Value plus(const Value& left, const Value& right) { |
51 | 51 |
return left + right; |
52 | 52 |
} |
53 | 53 |
/// \brief Gives back true only if the first value is less than the second. |
54 | 54 |
static bool less(const Value& left, const Value& right) { |
55 | 55 |
return left < right; |
56 | 56 |
} |
57 | 57 |
}; |
58 | 58 |
|
59 | 59 |
///Default traits class of Dijkstra class. |
60 | 60 |
|
61 | 61 |
///Default traits class of Dijkstra class. |
62 | 62 |
///\tparam GR The type of the digraph. |
63 | 63 |
///\tparam LEN The type of the length map. |
64 | 64 |
template<typename GR, typename LEN> |
65 | 65 |
struct DijkstraDefaultTraits |
66 | 66 |
{ |
67 | 67 |
///The type of the digraph the algorithm runs on. |
68 | 68 |
typedef GR Digraph; |
69 | 69 |
|
70 | 70 |
///The type of the map that stores the arc lengths. |
71 | 71 |
|
72 | 72 |
///The type of the map that stores the arc lengths. |
73 | 73 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
74 | 74 |
typedef LEN LengthMap; |
75 | 75 |
///The type of the length of the arcs. |
76 | 76 |
typedef typename LEN::Value Value; |
77 | 77 |
|
78 | 78 |
/// Operation traits for %Dijkstra algorithm. |
79 | 79 |
|
80 | 80 |
/// This class defines the operations that are used in the algorithm. |
81 | 81 |
/// \see DijkstraDefaultOperationTraits |
82 | 82 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
83 | 83 |
|
84 | 84 |
/// The cross reference type used by the heap. |
85 | 85 |
|
86 | 86 |
/// The cross reference type used by the heap. |
87 | 87 |
/// Usually it is \c Digraph::NodeMap<int>. |
88 | 88 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
89 | 89 |
///Instantiates a \c HeapCrossRef. |
90 | 90 |
|
91 | 91 |
///This function instantiates a \ref HeapCrossRef. |
92 | 92 |
/// \param g is the digraph, to which we would like to define the |
93 | 93 |
/// \ref HeapCrossRef. |
94 | 94 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
95 | 95 |
{ |
96 | 96 |
return new HeapCrossRef(g); |
97 | 97 |
} |
98 | 98 |
|
99 | 99 |
///The heap type used by the %Dijkstra algorithm. |
100 | 100 |
|
101 | 101 |
///The heap type used by the Dijkstra algorithm. |
102 | 102 |
/// |
103 | 103 |
///\sa BinHeap |
104 | 104 |
///\sa Dijkstra |
105 | 105 |
typedef BinHeap<typename LEN::Value, HeapCrossRef, std::less<Value> > Heap; |
106 | 106 |
///Instantiates a \c Heap. |
107 | 107 |
|
108 | 108 |
///This function instantiates a \ref Heap. |
109 | 109 |
static Heap *createHeap(HeapCrossRef& r) |
110 | 110 |
{ |
111 | 111 |
return new Heap(r); |
112 | 112 |
} |
113 | 113 |
|
114 | 114 |
///\brief The type of the map that stores the predecessor |
115 | 115 |
///arcs of the shortest paths. |
116 | 116 |
/// |
117 | 117 |
///The type of the map that stores the predecessor |
118 | 118 |
///arcs of the shortest paths. |
119 | 119 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
120 | 120 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
121 | 121 |
///Instantiates a \c PredMap. |
122 | 122 |
|
123 | 123 |
///This function instantiates a \ref PredMap. |
124 | 124 |
///\param g is the digraph, to which we would like to define the |
125 | 125 |
///\ref PredMap. |
126 | 126 |
static PredMap *createPredMap(const Digraph &g) |
127 | 127 |
{ |
128 | 128 |
return new PredMap(g); |
129 | 129 |
} |
130 | 130 |
|
131 | 131 |
///The type of the map that indicates which nodes are processed. |
132 | 132 |
|
133 | 133 |
///The type of the map that indicates which nodes are processed. |
134 | 134 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
135 | 135 |
///By default it is a NullMap. |
136 | 136 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
137 | 137 |
///Instantiates a \c ProcessedMap. |
138 | 138 |
|
139 | 139 |
///This function instantiates a \ref ProcessedMap. |
140 | 140 |
///\param g is the digraph, to which |
141 | 141 |
///we would like to define the \ref ProcessedMap. |
142 | 142 |
#ifdef DOXYGEN |
143 | 143 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
144 | 144 |
#else |
145 | 145 |
static ProcessedMap *createProcessedMap(const Digraph &) |
146 | 146 |
#endif |
147 | 147 |
{ |
148 | 148 |
return new ProcessedMap(); |
149 | 149 |
} |
150 | 150 |
|
151 | 151 |
///The type of the map that stores the distances of the nodes. |
152 | 152 |
|
153 | 153 |
///The type of the map that stores the distances of the nodes. |
154 | 154 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
155 | 155 |
typedef typename Digraph::template NodeMap<typename LEN::Value> DistMap; |
156 | 156 |
///Instantiates a \c DistMap. |
157 | 157 |
|
158 | 158 |
///This function instantiates a \ref DistMap. |
159 | 159 |
///\param g is the digraph, to which we would like to define |
160 | 160 |
///the \ref DistMap. |
161 | 161 |
static DistMap *createDistMap(const Digraph &g) |
162 | 162 |
{ |
163 | 163 |
return new DistMap(g); |
164 | 164 |
} |
165 | 165 |
}; |
166 | 166 |
|
167 | 167 |
///%Dijkstra algorithm class. |
168 | 168 |
|
169 | 169 |
/// \ingroup shortest_path |
170 | 170 |
///This class provides an efficient implementation of the %Dijkstra algorithm. |
171 | 171 |
/// |
172 | 172 |
///The arc lengths are passed to the algorithm using a |
173 | 173 |
///\ref concepts::ReadMap "ReadMap", |
174 | 174 |
///so it is easy to change it to any kind of length. |
175 | 175 |
///The type of the length is determined by the |
176 | 176 |
///\ref concepts::ReadMap::Value "Value" of the length map. |
177 | 177 |
///It is also possible to change the underlying priority heap. |
178 | 178 |
/// |
179 | 179 |
///There is also a \ref dijkstra() "function-type interface" for the |
180 | 180 |
///%Dijkstra algorithm, which is convenient in the simplier cases and |
181 | 181 |
///it can be used easier. |
182 | 182 |
/// |
183 | 183 |
///\tparam GR The type of the digraph the algorithm runs on. |
184 | 184 |
///The default type is \ref ListDigraph. |
185 | 185 |
///\tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies |
186 | 186 |
///the lengths of the arcs. |
187 | 187 |
///It is read once for each arc, so the map may involve in |
188 | 188 |
///relatively time consuming process to compute the arc lengths if |
189 | 189 |
///it is necessary. The default map type is \ref |
190 | 190 |
///concepts::Digraph::ArcMap "GR::ArcMap<int>". |
191 | 191 |
#ifdef DOXYGEN |
192 | 192 |
template <typename GR, typename LEN, typename TR> |
193 | 193 |
#else |
194 | 194 |
template <typename GR=ListDigraph, |
195 | 195 |
typename LEN=typename GR::template ArcMap<int>, |
196 | 196 |
typename TR=DijkstraDefaultTraits<GR,LEN> > |
197 | 197 |
#endif |
198 | 198 |
class Dijkstra { |
199 | 199 |
public: |
200 | 200 |
|
201 | 201 |
///The type of the digraph the algorithm runs on. |
202 | 202 |
typedef typename TR::Digraph Digraph; |
203 | 203 |
|
204 | 204 |
///The type of the length of the arcs. |
205 | 205 |
typedef typename TR::LengthMap::Value Value; |
206 | 206 |
///The type of the map that stores the arc lengths. |
207 | 207 |
typedef typename TR::LengthMap LengthMap; |
208 | 208 |
///\brief The type of the map that stores the predecessor arcs of the |
209 | 209 |
///shortest paths. |
210 | 210 |
typedef typename TR::PredMap PredMap; |
211 | 211 |
///The type of the map that stores the distances of the nodes. |
212 | 212 |
typedef typename TR::DistMap DistMap; |
213 | 213 |
///The type of the map that indicates which nodes are processed. |
214 | 214 |
typedef typename TR::ProcessedMap ProcessedMap; |
215 | 215 |
///The type of the paths. |
216 | 216 |
typedef PredMapPath<Digraph, PredMap> Path; |
217 | 217 |
///The cross reference type used for the current heap. |
218 | 218 |
typedef typename TR::HeapCrossRef HeapCrossRef; |
219 | 219 |
///The heap type used by the algorithm. |
220 | 220 |
typedef typename TR::Heap Heap; |
221 | 221 |
///\brief The \ref DijkstraDefaultOperationTraits "operation traits class" |
222 | 222 |
///of the algorithm. |
223 | 223 |
typedef typename TR::OperationTraits OperationTraits; |
224 | 224 |
|
225 | 225 |
///The \ref DijkstraDefaultTraits "traits class" of the algorithm. |
226 | 226 |
typedef TR Traits; |
227 | 227 |
|
228 | 228 |
private: |
229 | 229 |
|
230 | 230 |
typedef typename Digraph::Node Node; |
231 | 231 |
typedef typename Digraph::NodeIt NodeIt; |
232 | 232 |
typedef typename Digraph::Arc Arc; |
233 | 233 |
typedef typename Digraph::OutArcIt OutArcIt; |
234 | 234 |
|
235 | 235 |
//Pointer to the underlying digraph. |
236 | 236 |
const Digraph *G; |
237 | 237 |
//Pointer to the length map. |
238 | 238 |
const LengthMap *_length; |
239 | 239 |
//Pointer to the map of predecessors arcs. |
240 | 240 |
PredMap *_pred; |
241 | 241 |
//Indicates if _pred is locally allocated (true) or not. |
242 | 242 |
bool local_pred; |
243 | 243 |
//Pointer to the map of distances. |
244 | 244 |
DistMap *_dist; |
245 | 245 |
//Indicates if _dist is locally allocated (true) or not. |
246 | 246 |
bool local_dist; |
247 | 247 |
//Pointer to the map of processed status of the nodes. |
248 | 248 |
ProcessedMap *_processed; |
249 | 249 |
//Indicates if _processed is locally allocated (true) or not. |
250 | 250 |
bool local_processed; |
251 | 251 |
//Pointer to the heap cross references. |
252 | 252 |
HeapCrossRef *_heap_cross_ref; |
253 | 253 |
//Indicates if _heap_cross_ref is locally allocated (true) or not. |
254 | 254 |
bool local_heap_cross_ref; |
255 | 255 |
//Pointer to the heap. |
256 | 256 |
Heap *_heap; |
257 | 257 |
//Indicates if _heap is locally allocated (true) or not. |
258 | 258 |
bool local_heap; |
259 | 259 |
|
260 | 260 |
//Creates the maps if necessary. |
261 | 261 |
void create_maps() |
262 | 262 |
{ |
263 | 263 |
if(!_pred) { |
264 | 264 |
local_pred = true; |
265 | 265 |
_pred = Traits::createPredMap(*G); |
266 | 266 |
} |
267 | 267 |
if(!_dist) { |
268 | 268 |
local_dist = true; |
269 | 269 |
_dist = Traits::createDistMap(*G); |
270 | 270 |
} |
271 | 271 |
if(!_processed) { |
272 | 272 |
local_processed = true; |
273 | 273 |
_processed = Traits::createProcessedMap(*G); |
274 | 274 |
} |
275 | 275 |
if (!_heap_cross_ref) { |
276 | 276 |
local_heap_cross_ref = true; |
277 | 277 |
_heap_cross_ref = Traits::createHeapCrossRef(*G); |
278 | 278 |
} |
279 | 279 |
if (!_heap) { |
280 | 280 |
local_heap = true; |
281 | 281 |
_heap = Traits::createHeap(*_heap_cross_ref); |
282 | 282 |
} |
283 | 283 |
} |
284 | 284 |
|
285 | 285 |
public: |
286 | 286 |
|
287 | 287 |
typedef Dijkstra Create; |
288 | 288 |
|
289 |
///\name Named |
|
289 |
///\name Named Template Parameters |
|
290 | 290 |
|
291 | 291 |
///@{ |
292 | 292 |
|
293 | 293 |
template <class T> |
294 | 294 |
struct SetPredMapTraits : public Traits { |
295 | 295 |
typedef T PredMap; |
296 | 296 |
static PredMap *createPredMap(const Digraph &) |
297 | 297 |
{ |
298 | 298 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
299 | 299 |
return 0; // ignore warnings |
300 | 300 |
} |
301 | 301 |
}; |
302 | 302 |
///\brief \ref named-templ-param "Named parameter" for setting |
303 | 303 |
///\c PredMap type. |
304 | 304 |
/// |
305 | 305 |
///\ref named-templ-param "Named parameter" for setting |
306 | 306 |
///\c PredMap type. |
307 | 307 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
308 | 308 |
template <class T> |
309 | 309 |
struct SetPredMap |
310 | 310 |
: public Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > { |
311 | 311 |
typedef Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > Create; |
312 | 312 |
}; |
313 | 313 |
|
314 | 314 |
template <class T> |
315 | 315 |
struct SetDistMapTraits : public Traits { |
316 | 316 |
typedef T DistMap; |
317 | 317 |
static DistMap *createDistMap(const Digraph &) |
318 | 318 |
{ |
319 | 319 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
320 | 320 |
return 0; // ignore warnings |
321 | 321 |
} |
322 | 322 |
}; |
323 | 323 |
///\brief \ref named-templ-param "Named parameter" for setting |
324 | 324 |
///\c DistMap type. |
325 | 325 |
/// |
326 | 326 |
///\ref named-templ-param "Named parameter" for setting |
327 | 327 |
///\c DistMap type. |
328 | 328 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
329 | 329 |
template <class T> |
330 | 330 |
struct SetDistMap |
331 | 331 |
: public Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > { |
332 | 332 |
typedef Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > Create; |
333 | 333 |
}; |
334 | 334 |
|
335 | 335 |
template <class T> |
336 | 336 |
struct SetProcessedMapTraits : public Traits { |
337 | 337 |
typedef T ProcessedMap; |
338 | 338 |
static ProcessedMap *createProcessedMap(const Digraph &) |
339 | 339 |
{ |
340 | 340 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
341 | 341 |
return 0; // ignore warnings |
342 | 342 |
} |
343 | 343 |
}; |
344 | 344 |
///\brief \ref named-templ-param "Named parameter" for setting |
345 | 345 |
///\c ProcessedMap type. |
346 | 346 |
/// |
347 | 347 |
///\ref named-templ-param "Named parameter" for setting |
348 | 348 |
///\c ProcessedMap type. |
349 | 349 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
350 | 350 |
template <class T> |
351 | 351 |
struct SetProcessedMap |
352 | 352 |
: public Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > { |
353 | 353 |
typedef Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > Create; |
354 | 354 |
}; |
355 | 355 |
|
356 | 356 |
struct SetStandardProcessedMapTraits : public Traits { |
357 | 357 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
358 | 358 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
359 | 359 |
{ |
360 | 360 |
return new ProcessedMap(g); |
361 | 361 |
} |
362 | 362 |
}; |
363 | 363 |
///\brief \ref named-templ-param "Named parameter" for setting |
364 | 364 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
365 | 365 |
/// |
366 | 366 |
///\ref named-templ-param "Named parameter" for setting |
367 | 367 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
368 | 368 |
///If you don't set it explicitly, it will be automatically allocated. |
369 | 369 |
struct SetStandardProcessedMap |
370 | 370 |
: public Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > { |
371 | 371 |
typedef Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > |
372 | 372 |
Create; |
373 | 373 |
}; |
374 | 374 |
|
375 | 375 |
template <class H, class CR> |
376 | 376 |
struct SetHeapTraits : public Traits { |
377 | 377 |
typedef CR HeapCrossRef; |
378 | 378 |
typedef H Heap; |
379 | 379 |
static HeapCrossRef *createHeapCrossRef(const Digraph &) { |
380 | 380 |
LEMON_ASSERT(false, "HeapCrossRef is not initialized"); |
381 | 381 |
return 0; // ignore warnings |
382 | 382 |
} |
383 | 383 |
static Heap *createHeap(HeapCrossRef &) |
384 | 384 |
{ |
385 | 385 |
LEMON_ASSERT(false, "Heap is not initialized"); |
386 | 386 |
return 0; // ignore warnings |
387 | 387 |
} |
388 | 388 |
}; |
389 | 389 |
///\brief \ref named-templ-param "Named parameter" for setting |
390 | 390 |
///heap and cross reference types |
391 | 391 |
/// |
392 | 392 |
///\ref named-templ-param "Named parameter" for setting heap and cross |
393 | 393 |
///reference types. If this named parameter is used, then external |
394 | 394 |
///heap and cross reference objects must be passed to the algorithm |
395 | 395 |
///using the \ref heap() function before calling \ref run(Node) "run()" |
396 | 396 |
///or \ref init(). |
397 | 397 |
///\sa SetStandardHeap |
398 | 398 |
template <class H, class CR = typename Digraph::template NodeMap<int> > |
399 | 399 |
struct SetHeap |
400 | 400 |
: public Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > { |
401 | 401 |
typedef Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > Create; |
402 | 402 |
}; |
403 | 403 |
|
404 | 404 |
template <class H, class CR> |
405 | 405 |
struct SetStandardHeapTraits : public Traits { |
406 | 406 |
typedef CR HeapCrossRef; |
407 | 407 |
typedef H Heap; |
408 | 408 |
static HeapCrossRef *createHeapCrossRef(const Digraph &G) { |
409 | 409 |
return new HeapCrossRef(G); |
410 | 410 |
} |
411 | 411 |
static Heap *createHeap(HeapCrossRef &R) |
412 | 412 |
{ |
413 | 413 |
return new Heap(R); |
414 | 414 |
} |
415 | 415 |
}; |
416 | 416 |
///\brief \ref named-templ-param "Named parameter" for setting |
417 | 417 |
///heap and cross reference types with automatic allocation |
418 | 418 |
/// |
419 | 419 |
///\ref named-templ-param "Named parameter" for setting heap and cross |
420 | 420 |
///reference types with automatic allocation. |
421 | 421 |
///They should have standard constructor interfaces to be able to |
422 | 422 |
///automatically created by the algorithm (i.e. the digraph should be |
423 | 423 |
///passed to the constructor of the cross reference and the cross |
424 | 424 |
///reference should be passed to the constructor of the heap). |
425 | 425 |
///However external heap and cross reference objects could also be |
426 | 426 |
///passed to the algorithm using the \ref heap() function before |
427 | 427 |
///calling \ref run(Node) "run()" or \ref init(). |
428 | 428 |
///\sa SetHeap |
429 | 429 |
template <class H, class CR = typename Digraph::template NodeMap<int> > |
430 | 430 |
struct SetStandardHeap |
431 | 431 |
: public Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > { |
432 | 432 |
typedef Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > |
433 | 433 |
Create; |
434 | 434 |
}; |
435 | 435 |
|
436 | 436 |
template <class T> |
437 | 437 |
struct SetOperationTraitsTraits : public Traits { |
438 | 438 |
typedef T OperationTraits; |
439 | 439 |
}; |
440 | 440 |
|
441 | 441 |
/// \brief \ref named-templ-param "Named parameter" for setting |
442 | 442 |
///\c OperationTraits type |
443 | 443 |
/// |
444 | 444 |
///\ref named-templ-param "Named parameter" for setting |
445 | 445 |
///\c OperationTraits type. |
446 | 446 |
template <class T> |
447 | 447 |
struct SetOperationTraits |
448 | 448 |
: public Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > { |
449 | 449 |
typedef Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > |
450 | 450 |
Create; |
451 | 451 |
}; |
452 | 452 |
|
453 | 453 |
///@} |
454 | 454 |
|
455 | 455 |
protected: |
456 | 456 |
|
457 | 457 |
Dijkstra() {} |
458 | 458 |
|
459 | 459 |
public: |
460 | 460 |
|
461 | 461 |
///Constructor. |
462 | 462 |
|
463 | 463 |
///Constructor. |
464 | 464 |
///\param g The digraph the algorithm runs on. |
465 | 465 |
///\param length The length map used by the algorithm. |
466 | 466 |
Dijkstra(const Digraph& g, const LengthMap& length) : |
467 | 467 |
G(&g), _length(&length), |
468 | 468 |
_pred(NULL), local_pred(false), |
469 | 469 |
_dist(NULL), local_dist(false), |
470 | 470 |
_processed(NULL), local_processed(false), |
471 | 471 |
_heap_cross_ref(NULL), local_heap_cross_ref(false), |
472 | 472 |
_heap(NULL), local_heap(false) |
473 | 473 |
{ } |
474 | 474 |
|
475 | 475 |
///Destructor. |
476 | 476 |
~Dijkstra() |
477 | 477 |
{ |
478 | 478 |
if(local_pred) delete _pred; |
479 | 479 |
if(local_dist) delete _dist; |
480 | 480 |
if(local_processed) delete _processed; |
481 | 481 |
if(local_heap_cross_ref) delete _heap_cross_ref; |
482 | 482 |
if(local_heap) delete _heap; |
483 | 483 |
} |
484 | 484 |
|
485 | 485 |
///Sets the length map. |
486 | 486 |
|
487 | 487 |
///Sets the length map. |
488 | 488 |
///\return <tt> (*this) </tt> |
489 | 489 |
Dijkstra &lengthMap(const LengthMap &m) |
490 | 490 |
{ |
491 | 491 |
_length = &m; |
492 | 492 |
return *this; |
493 | 493 |
} |
494 | 494 |
|
495 | 495 |
///Sets the map that stores the predecessor arcs. |
496 | 496 |
|
497 | 497 |
///Sets the map that stores the predecessor arcs. |
498 | 498 |
///If you don't use this function before calling \ref run(Node) "run()" |
499 | 499 |
///or \ref init(), an instance will be allocated automatically. |
500 | 500 |
///The destructor deallocates this automatically allocated map, |
501 | 501 |
///of course. |
502 | 502 |
///\return <tt> (*this) </tt> |
503 | 503 |
Dijkstra &predMap(PredMap &m) |
504 | 504 |
{ |
505 | 505 |
if(local_pred) { |
506 | 506 |
delete _pred; |
507 | 507 |
local_pred=false; |
508 | 508 |
} |
509 | 509 |
_pred = &m; |
510 | 510 |
return *this; |
511 | 511 |
} |
512 | 512 |
|
513 | 513 |
///Sets the map that indicates which nodes are processed. |
514 | 514 |
|
515 | 515 |
///Sets the map that indicates which nodes are processed. |
516 | 516 |
///If you don't use this function before calling \ref run(Node) "run()" |
517 | 517 |
///or \ref init(), an instance will be allocated automatically. |
518 | 518 |
///The destructor deallocates this automatically allocated map, |
519 | 519 |
///of course. |
520 | 520 |
///\return <tt> (*this) </tt> |
521 | 521 |
Dijkstra &processedMap(ProcessedMap &m) |
522 | 522 |
{ |
523 | 523 |
if(local_processed) { |
524 | 524 |
delete _processed; |
525 | 525 |
local_processed=false; |
526 | 526 |
} |
527 | 527 |
_processed = &m; |
528 | 528 |
return *this; |
529 | 529 |
} |
530 | 530 |
|
531 | 531 |
///Sets the map that stores the distances of the nodes. |
532 | 532 |
|
533 | 533 |
///Sets the map that stores the distances of the nodes calculated by the |
534 | 534 |
///algorithm. |
535 | 535 |
///If you don't use this function before calling \ref run(Node) "run()" |
536 | 536 |
///or \ref init(), an instance will be allocated automatically. |
537 | 537 |
///The destructor deallocates this automatically allocated map, |
538 | 538 |
///of course. |
539 | 539 |
///\return <tt> (*this) </tt> |
540 | 540 |
Dijkstra &distMap(DistMap &m) |
541 | 541 |
{ |
542 | 542 |
if(local_dist) { |
543 | 543 |
delete _dist; |
544 | 544 |
local_dist=false; |
545 | 545 |
} |
546 | 546 |
_dist = &m; |
547 | 547 |
return *this; |
548 | 548 |
} |
549 | 549 |
|
550 | 550 |
///Sets the heap and the cross reference used by algorithm. |
551 | 551 |
|
552 | 552 |
///Sets the heap and the cross reference used by algorithm. |
553 | 553 |
///If you don't use this function before calling \ref run(Node) "run()" |
554 | 554 |
///or \ref init(), heap and cross reference instances will be |
555 | 555 |
///allocated automatically. |
556 | 556 |
///The destructor deallocates these automatically allocated objects, |
557 | 557 |
///of course. |
558 | 558 |
///\return <tt> (*this) </tt> |
559 | 559 |
Dijkstra &heap(Heap& hp, HeapCrossRef &cr) |
560 | 560 |
{ |
561 | 561 |
if(local_heap_cross_ref) { |
562 | 562 |
delete _heap_cross_ref; |
563 | 563 |
local_heap_cross_ref=false; |
564 | 564 |
} |
565 | 565 |
_heap_cross_ref = &cr; |
566 | 566 |
if(local_heap) { |
567 | 567 |
delete _heap; |
568 | 568 |
local_heap=false; |
569 | 569 |
} |
570 | 570 |
_heap = &hp; |
571 | 571 |
return *this; |
572 | 572 |
} |
573 | 573 |
|
574 | 574 |
private: |
575 | 575 |
|
576 | 576 |
void finalizeNodeData(Node v,Value dst) |
577 | 577 |
{ |
578 | 578 |
_processed->set(v,true); |
579 | 579 |
_dist->set(v, dst); |
580 | 580 |
} |
581 | 581 |
|
582 | 582 |
public: |
583 | 583 |
|
584 | 584 |
///\name Execution Control |
585 | 585 |
///The simplest way to execute the %Dijkstra algorithm is to use |
586 | 586 |
///one of the member functions called \ref run(Node) "run()".\n |
587 | 587 |
///If you need more control on the execution, first you have to call |
588 | 588 |
///\ref init(), then you can add several source nodes with |
589 | 589 |
///\ref addSource(). Finally the actual path computation can be |
590 | 590 |
///performed with one of the \ref start() functions. |
591 | 591 |
|
592 | 592 |
///@{ |
593 | 593 |
|
594 | 594 |
///\brief Initializes the internal data structures. |
595 | 595 |
/// |
596 | 596 |
///Initializes the internal data structures. |
597 | 597 |
void init() |
598 | 598 |
{ |
599 | 599 |
create_maps(); |
600 | 600 |
_heap->clear(); |
601 | 601 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
602 | 602 |
_pred->set(u,INVALID); |
603 | 603 |
_processed->set(u,false); |
604 | 604 |
_heap_cross_ref->set(u,Heap::PRE_HEAP); |
605 | 605 |
} |
606 | 606 |
} |
607 | 607 |
|
608 | 608 |
///Adds a new source node. |
609 | 609 |
|
610 | 610 |
///Adds a new source node to the priority heap. |
611 | 611 |
///The optional second parameter is the initial distance of the node. |
612 | 612 |
/// |
613 | 613 |
///The function checks if the node has already been added to the heap and |
614 | 614 |
///it is pushed to the heap only if either it was not in the heap |
615 | 615 |
///or the shortest path found till then is shorter than \c dst. |
616 | 616 |
void addSource(Node s,Value dst=OperationTraits::zero()) |
617 | 617 |
{ |
618 | 618 |
if(_heap->state(s) != Heap::IN_HEAP) { |
619 | 619 |
_heap->push(s,dst); |
620 | 620 |
} else if(OperationTraits::less((*_heap)[s], dst)) { |
621 | 621 |
_heap->set(s,dst); |
622 | 622 |
_pred->set(s,INVALID); |
623 | 623 |
} |
624 | 624 |
} |
625 | 625 |
|
626 | 626 |
///Processes the next node in the priority heap |
627 | 627 |
|
628 | 628 |
///Processes the next node in the priority heap. |
629 | 629 |
/// |
630 | 630 |
///\return The processed node. |
631 | 631 |
/// |
632 | 632 |
///\warning The priority heap must not be empty. |
633 | 633 |
Node processNextNode() |
634 | 634 |
{ |
635 | 635 |
Node v=_heap->top(); |
636 | 636 |
Value oldvalue=_heap->prio(); |
637 | 637 |
_heap->pop(); |
638 | 638 |
finalizeNodeData(v,oldvalue); |
639 | 639 |
|
640 | 640 |
for(OutArcIt e(*G,v); e!=INVALID; ++e) { |
641 | 641 |
Node w=G->target(e); |
642 | 642 |
switch(_heap->state(w)) { |
643 | 643 |
case Heap::PRE_HEAP: |
644 | 644 |
_heap->push(w,OperationTraits::plus(oldvalue, (*_length)[e])); |
645 | 645 |
_pred->set(w,e); |
646 | 646 |
break; |
647 | 647 |
case Heap::IN_HEAP: |
648 | 648 |
{ |
649 | 649 |
Value newvalue = OperationTraits::plus(oldvalue, (*_length)[e]); |
650 | 650 |
if ( OperationTraits::less(newvalue, (*_heap)[w]) ) { |
651 | 651 |
_heap->decrease(w, newvalue); |
652 | 652 |
_pred->set(w,e); |
653 | 653 |
} |
654 | 654 |
} |
655 | 655 |
break; |
656 | 656 |
case Heap::POST_HEAP: |
657 | 657 |
break; |
658 | 658 |
} |
659 | 659 |
} |
660 | 660 |
return v; |
661 | 661 |
} |
662 | 662 |
|
663 | 663 |
///The next node to be processed. |
664 | 664 |
|
665 | 665 |
///Returns the next node to be processed or \c INVALID if the |
666 | 666 |
///priority heap is empty. |
667 | 667 |
Node nextNode() const |
668 | 668 |
{ |
669 | 669 |
return !_heap->empty()?_heap->top():INVALID; |
670 | 670 |
} |
671 | 671 |
|
672 | 672 |
///Returns \c false if there are nodes to be processed. |
673 | 673 |
|
674 | 674 |
///Returns \c false if there are nodes to be processed |
675 | 675 |
///in the priority heap. |
676 | 676 |
bool emptyQueue() const { return _heap->empty(); } |
677 | 677 |
|
678 | 678 |
///Returns the number of the nodes to be processed. |
679 | 679 |
|
680 | 680 |
///Returns the number of the nodes to be processed |
681 | 681 |
///in the priority heap. |
682 | 682 |
int queueSize() const { return _heap->size(); } |
683 | 683 |
|
684 | 684 |
///Executes the algorithm. |
685 | 685 |
|
686 | 686 |
///Executes the algorithm. |
687 | 687 |
/// |
688 | 688 |
///This method runs the %Dijkstra algorithm from the root node(s) |
689 | 689 |
///in order to compute the shortest path to each node. |
690 | 690 |
/// |
691 | 691 |
///The algorithm computes |
692 | 692 |
///- the shortest path tree (forest), |
693 | 693 |
///- the distance of each node from the root(s). |
694 | 694 |
/// |
695 | 695 |
///\pre init() must be called and at least one root node should be |
696 | 696 |
///added with addSource() before using this function. |
697 | 697 |
/// |
698 | 698 |
///\note <tt>d.start()</tt> is just a shortcut of the following code. |
699 | 699 |
///\code |
700 | 700 |
/// while ( !d.emptyQueue() ) { |
701 | 701 |
/// d.processNextNode(); |
702 | 702 |
/// } |
703 | 703 |
///\endcode |
704 | 704 |
void start() |
705 | 705 |
{ |
706 | 706 |
while ( !emptyQueue() ) processNextNode(); |
707 | 707 |
} |
708 | 708 |
|
709 | 709 |
///Executes the algorithm until the given target node is processed. |
710 | 710 |
|
711 | 711 |
///Executes the algorithm until the given target node is processed. |
712 | 712 |
/// |
713 | 713 |
///This method runs the %Dijkstra algorithm from the root node(s) |
714 | 714 |
///in order to compute the shortest path to \c t. |
715 | 715 |
/// |
716 | 716 |
///The algorithm computes |
717 | 717 |
///- the shortest path to \c t, |
718 | 718 |
///- the distance of \c t from the root(s). |
719 | 719 |
/// |
720 | 720 |
///\pre init() must be called and at least one root node should be |
721 | 721 |
///added with addSource() before using this function. |
722 | 722 |
void start(Node t) |
723 | 723 |
{ |
724 | 724 |
while ( !_heap->empty() && _heap->top()!=t ) processNextNode(); |
725 | 725 |
if ( !_heap->empty() ) { |
726 | 726 |
finalizeNodeData(_heap->top(),_heap->prio()); |
727 | 727 |
_heap->pop(); |
728 | 728 |
} |
729 | 729 |
} |
730 | 730 |
|
731 | 731 |
///Executes the algorithm until a condition is met. |
732 | 732 |
|
733 | 733 |
///Executes the algorithm until a condition is met. |
734 | 734 |
/// |
735 | 735 |
///This method runs the %Dijkstra algorithm from the root node(s) in |
736 | 736 |
///order to compute the shortest path to a node \c v with |
737 | 737 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
738 | 738 |
/// |
739 | 739 |
///\param nm A \c bool (or convertible) node map. The algorithm |
740 | 740 |
///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. |
741 | 741 |
/// |
742 | 742 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
743 | 743 |
///\c INVALID if no such node was found. |
744 | 744 |
/// |
745 | 745 |
///\pre init() must be called and at least one root node should be |
746 | 746 |
///added with addSource() before using this function. |
747 | 747 |
template<class NodeBoolMap> |
748 | 748 |
Node start(const NodeBoolMap &nm) |
749 | 749 |
{ |
750 | 750 |
while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode(); |
751 | 751 |
if ( _heap->empty() ) return INVALID; |
752 | 752 |
finalizeNodeData(_heap->top(),_heap->prio()); |
753 | 753 |
return _heap->top(); |
754 | 754 |
} |
755 | 755 |
|
756 | 756 |
///Runs the algorithm from the given source node. |
757 | 757 |
|
758 | 758 |
///This method runs the %Dijkstra algorithm from node \c s |
759 | 759 |
///in order to compute the shortest path to each node. |
760 | 760 |
/// |
761 | 761 |
///The algorithm computes |
762 | 762 |
///- the shortest path tree, |
763 | 763 |
///- the distance of each node from the root. |
764 | 764 |
/// |
765 | 765 |
///\note <tt>d.run(s)</tt> is just a shortcut of the following code. |
766 | 766 |
///\code |
767 | 767 |
/// d.init(); |
768 | 768 |
/// d.addSource(s); |
769 | 769 |
/// d.start(); |
770 | 770 |
///\endcode |
771 | 771 |
void run(Node s) { |
772 | 772 |
init(); |
773 | 773 |
addSource(s); |
774 | 774 |
start(); |
775 | 775 |
} |
776 | 776 |
|
777 | 777 |
///Finds the shortest path between \c s and \c t. |
778 | 778 |
|
779 | 779 |
///This method runs the %Dijkstra algorithm from node \c s |
780 | 780 |
///in order to compute the shortest path to node \c t |
781 | 781 |
///(it stops searching when \c t is processed). |
782 | 782 |
/// |
783 | 783 |
///\return \c true if \c t is reachable form \c s. |
784 | 784 |
/// |
785 | 785 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is just a |
786 | 786 |
///shortcut of the following code. |
787 | 787 |
///\code |
788 | 788 |
/// d.init(); |
789 | 789 |
/// d.addSource(s); |
790 | 790 |
/// d.start(t); |
791 | 791 |
///\endcode |
792 | 792 |
bool run(Node s,Node t) { |
793 | 793 |
init(); |
794 | 794 |
addSource(s); |
795 | 795 |
start(t); |
796 | 796 |
return (*_heap_cross_ref)[t] == Heap::POST_HEAP; |
797 | 797 |
} |
798 | 798 |
|
799 | 799 |
///@} |
800 | 800 |
|
801 | 801 |
///\name Query Functions |
802 | 802 |
///The results of the %Dijkstra algorithm can be obtained using these |
803 | 803 |
///functions.\n |
804 | 804 |
///Either \ref run(Node) "run()" or \ref start() should be called |
805 | 805 |
///before using them. |
806 | 806 |
|
807 | 807 |
///@{ |
808 | 808 |
|
809 | 809 |
///The shortest path to a node. |
810 | 810 |
|
811 | 811 |
///Returns the shortest path to a node. |
812 | 812 |
/// |
813 | 813 |
///\warning \c t should be reached from the root(s). |
814 | 814 |
/// |
815 | 815 |
///\pre Either \ref run(Node) "run()" or \ref init() |
816 | 816 |
///must be called before using this function. |
817 | 817 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
818 | 818 |
|
819 | 819 |
///The distance of a node from the root(s). |
820 | 820 |
|
821 | 821 |
///Returns the distance of a node from the root(s). |
822 | 822 |
/// |
823 | 823 |
///\warning If node \c v is not reached from the root(s), then |
824 | 824 |
///the return value of this function is undefined. |
825 | 825 |
/// |
826 | 826 |
///\pre Either \ref run(Node) "run()" or \ref init() |
827 | 827 |
///must be called before using this function. |
828 | 828 |
Value dist(Node v) const { return (*_dist)[v]; } |
829 | 829 |
|
830 | 830 |
///Returns the 'previous arc' of the shortest path tree for a node. |
831 | 831 |
|
832 | 832 |
///This function returns the 'previous arc' of the shortest path |
833 | 833 |
///tree for the node \c v, i.e. it returns the last arc of a |
834 | 834 |
///shortest path from a root to \c v. It is \c INVALID if \c v |
835 | 835 |
///is not reached from the root(s) or if \c v is a root. |
836 | 836 |
/// |
837 | 837 |
///The shortest path tree used here is equal to the shortest path |
838 | 838 |
///tree used in \ref predNode(). |
839 | 839 |
/// |
840 | 840 |
///\pre Either \ref run(Node) "run()" or \ref init() |
841 | 841 |
///must be called before using this function. |
842 | 842 |
Arc predArc(Node v) const { return (*_pred)[v]; } |
843 | 843 |
|
844 | 844 |
///Returns the 'previous node' of the shortest path tree for a node. |
845 | 845 |
|
846 | 846 |
///This function returns the 'previous node' of the shortest path |
847 | 847 |
///tree for the node \c v, i.e. it returns the last but one node |
848 | 848 |
///from a shortest path from a root to \c v. It is \c INVALID |
849 | 849 |
///if \c v is not reached from the root(s) or if \c v is a root. |
850 | 850 |
/// |
851 | 851 |
///The shortest path tree used here is equal to the shortest path |
852 | 852 |
///tree used in \ref predArc(). |
853 | 853 |
/// |
854 | 854 |
///\pre Either \ref run(Node) "run()" or \ref init() |
855 | 855 |
///must be called before using this function. |
856 | 856 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
857 | 857 |
G->source((*_pred)[v]); } |
858 | 858 |
|
859 | 859 |
///\brief Returns a const reference to the node map that stores the |
860 | 860 |
///distances of the nodes. |
861 | 861 |
/// |
862 | 862 |
///Returns a const reference to the node map that stores the distances |
863 | 863 |
///of the nodes calculated by the algorithm. |
864 | 864 |
/// |
865 | 865 |
///\pre Either \ref run(Node) "run()" or \ref init() |
866 | 866 |
///must be called before using this function. |
867 | 867 |
const DistMap &distMap() const { return *_dist;} |
868 | 868 |
|
869 | 869 |
///\brief Returns a const reference to the node map that stores the |
870 | 870 |
///predecessor arcs. |
871 | 871 |
/// |
872 | 872 |
///Returns a const reference to the node map that stores the predecessor |
873 | 873 |
///arcs, which form the shortest path tree. |
874 | 874 |
/// |
875 | 875 |
///\pre Either \ref run(Node) "run()" or \ref init() |
876 | 876 |
///must be called before using this function. |
877 | 877 |
const PredMap &predMap() const { return *_pred;} |
878 | 878 |
|
879 | 879 |
///Checks if a node is reached from the root(s). |
880 | 880 |
|
881 | 881 |
///Returns \c true if \c v is reached from the root(s). |
882 | 882 |
/// |
883 | 883 |
///\pre Either \ref run(Node) "run()" or \ref init() |
884 | 884 |
///must be called before using this function. |
885 | 885 |
bool reached(Node v) const { return (*_heap_cross_ref)[v] != |
886 | 886 |
Heap::PRE_HEAP; } |
887 | 887 |
|
888 | 888 |
///Checks if a node is processed. |
889 | 889 |
|
890 | 890 |
///Returns \c true if \c v is processed, i.e. the shortest |
891 | 891 |
///path to \c v has already found. |
892 | 892 |
/// |
893 | 893 |
///\pre Either \ref run(Node) "run()" or \ref init() |
894 | 894 |
///must be called before using this function. |
895 | 895 |
bool processed(Node v) const { return (*_heap_cross_ref)[v] == |
896 | 896 |
Heap::POST_HEAP; } |
897 | 897 |
|
898 | 898 |
///The current distance of a node from the root(s). |
899 | 899 |
|
900 | 900 |
///Returns the current distance of a node from the root(s). |
901 | 901 |
///It may be decreased in the following processes. |
902 | 902 |
/// |
903 | 903 |
///\pre Either \ref run(Node) "run()" or \ref init() |
904 | 904 |
///must be called before using this function and |
905 | 905 |
///node \c v must be reached but not necessarily processed. |
906 | 906 |
Value currentDist(Node v) const { |
907 | 907 |
return processed(v) ? (*_dist)[v] : (*_heap)[v]; |
908 | 908 |
} |
909 | 909 |
|
910 | 910 |
///@} |
911 | 911 |
}; |
912 | 912 |
|
913 | 913 |
|
914 | 914 |
///Default traits class of dijkstra() function. |
915 | 915 |
|
916 | 916 |
///Default traits class of dijkstra() function. |
917 | 917 |
///\tparam GR The type of the digraph. |
918 | 918 |
///\tparam LEN The type of the length map. |
919 | 919 |
template<class GR, class LEN> |
920 | 920 |
struct DijkstraWizardDefaultTraits |
921 | 921 |
{ |
922 | 922 |
///The type of the digraph the algorithm runs on. |
923 | 923 |
typedef GR Digraph; |
924 | 924 |
///The type of the map that stores the arc lengths. |
925 | 925 |
|
926 | 926 |
///The type of the map that stores the arc lengths. |
927 | 927 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
928 | 928 |
typedef LEN LengthMap; |
929 | 929 |
///The type of the length of the arcs. |
930 | 930 |
typedef typename LEN::Value Value; |
931 | 931 |
|
932 | 932 |
/// Operation traits for Dijkstra algorithm. |
933 | 933 |
|
934 | 934 |
/// This class defines the operations that are used in the algorithm. |
935 | 935 |
/// \see DijkstraDefaultOperationTraits |
936 | 936 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
937 | 937 |
|
938 | 938 |
/// The cross reference type used by the heap. |
939 | 939 |
|
940 | 940 |
/// The cross reference type used by the heap. |
941 | 941 |
/// Usually it is \c Digraph::NodeMap<int>. |
942 | 942 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
943 | 943 |
///Instantiates a \ref HeapCrossRef. |
944 | 944 |
|
945 | 945 |
///This function instantiates a \ref HeapCrossRef. |
946 | 946 |
/// \param g is the digraph, to which we would like to define the |
947 | 947 |
/// HeapCrossRef. |
948 | 948 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
949 | 949 |
{ |
950 | 950 |
return new HeapCrossRef(g); |
951 | 951 |
} |
952 | 952 |
|
953 | 953 |
///The heap type used by the Dijkstra algorithm. |
954 | 954 |
|
955 | 955 |
///The heap type used by the Dijkstra algorithm. |
956 | 956 |
/// |
957 | 957 |
///\sa BinHeap |
958 | 958 |
///\sa Dijkstra |
959 | 959 |
typedef BinHeap<Value, typename Digraph::template NodeMap<int>, |
960 | 960 |
std::less<Value> > Heap; |
961 | 961 |
|
962 | 962 |
///Instantiates a \ref Heap. |
963 | 963 |
|
964 | 964 |
///This function instantiates a \ref Heap. |
965 | 965 |
/// \param r is the HeapCrossRef which is used. |
966 | 966 |
static Heap *createHeap(HeapCrossRef& r) |
967 | 967 |
{ |
968 | 968 |
return new Heap(r); |
969 | 969 |
} |
970 | 970 |
|
971 | 971 |
///\brief The type of the map that stores the predecessor |
972 | 972 |
///arcs of the shortest paths. |
973 | 973 |
/// |
974 | 974 |
///The type of the map that stores the predecessor |
975 | 975 |
///arcs of the shortest paths. |
976 | 976 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
977 | 977 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
978 | 978 |
///Instantiates a PredMap. |
979 | 979 |
|
980 | 980 |
///This function instantiates a PredMap. |
981 | 981 |
///\param g is the digraph, to which we would like to define the |
982 | 982 |
///PredMap. |
983 | 983 |
static PredMap *createPredMap(const Digraph &g) |
984 | 984 |
{ |
985 | 985 |
return new PredMap(g); |
986 | 986 |
} |
987 | 987 |
|
988 | 988 |
///The type of the map that indicates which nodes are processed. |
989 | 989 |
|
990 | 990 |
///The type of the map that indicates which nodes are processed. |
991 | 991 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
992 | 992 |
///By default it is a NullMap. |
993 | 993 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
994 | 994 |
///Instantiates a ProcessedMap. |
995 | 995 |
|
996 | 996 |
///This function instantiates a ProcessedMap. |
997 | 997 |
///\param g is the digraph, to which |
998 | 998 |
///we would like to define the ProcessedMap. |
999 | 999 |
#ifdef DOXYGEN |
1000 | 1000 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
1001 | 1001 |
#else |
1002 | 1002 |
static ProcessedMap *createProcessedMap(const Digraph &) |
1003 | 1003 |
#endif |
1004 | 1004 |
{ |
1005 | 1005 |
return new ProcessedMap(); |
1006 | 1006 |
} |
1007 | 1007 |
|
1008 | 1008 |
///The type of the map that stores the distances of the nodes. |
1009 | 1009 |
|
1010 | 1010 |
///The type of the map that stores the distances of the nodes. |
1011 | 1011 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
1012 | 1012 |
typedef typename Digraph::template NodeMap<typename LEN::Value> DistMap; |
1013 | 1013 |
///Instantiates a DistMap. |
1014 | 1014 |
|
1015 | 1015 |
///This function instantiates a DistMap. |
1016 | 1016 |
///\param g is the digraph, to which we would like to define |
1017 | 1017 |
///the DistMap |
1018 | 1018 |
static DistMap *createDistMap(const Digraph &g) |
1019 | 1019 |
{ |
1020 | 1020 |
return new DistMap(g); |
1021 | 1021 |
} |
1022 | 1022 |
|
1023 | 1023 |
///The type of the shortest paths. |
1024 | 1024 |
|
1025 | 1025 |
///The type of the shortest paths. |
1026 | 1026 |
///It must meet the \ref concepts::Path "Path" concept. |
1027 | 1027 |
typedef lemon::Path<Digraph> Path; |
1028 | 1028 |
}; |
1029 | 1029 |
|
1030 | 1030 |
/// Default traits class used by DijkstraWizard |
1031 | 1031 |
|
1032 | 1032 |
/// To make it easier to use Dijkstra algorithm |
1033 | 1033 |
/// we have created a wizard class. |
1034 | 1034 |
/// This \ref DijkstraWizard class needs default traits, |
1035 | 1035 |
/// as well as the \ref Dijkstra class. |
1036 | 1036 |
/// The \ref DijkstraWizardBase is a class to be the default traits of the |
1037 | 1037 |
/// \ref DijkstraWizard class. |
1038 | 1038 |
template<typename GR, typename LEN> |
1039 | 1039 |
class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LEN> |
1040 | 1040 |
{ |
1041 | 1041 |
typedef DijkstraWizardDefaultTraits<GR,LEN> Base; |
1042 | 1042 |
protected: |
1043 | 1043 |
//The type of the nodes in the digraph. |
1044 | 1044 |
typedef typename Base::Digraph::Node Node; |
1045 | 1045 |
|
1046 | 1046 |
//Pointer to the digraph the algorithm runs on. |
1047 | 1047 |
void *_g; |
1048 | 1048 |
//Pointer to the length map. |
1049 | 1049 |
void *_length; |
1050 | 1050 |
//Pointer to the map of processed nodes. |
1051 | 1051 |
void *_processed; |
1052 | 1052 |
//Pointer to the map of predecessors arcs. |
1053 | 1053 |
void *_pred; |
1054 | 1054 |
//Pointer to the map of distances. |
1055 | 1055 |
void *_dist; |
1056 | 1056 |
//Pointer to the shortest path to the target node. |
1057 | 1057 |
void *_path; |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_DIMACS_H |
20 | 20 |
#define LEMON_DIMACS_H |
21 | 21 |
|
22 | 22 |
#include <iostream> |
23 | 23 |
#include <string> |
24 | 24 |
#include <vector> |
25 | 25 |
#include <limits> |
26 | 26 |
#include <lemon/maps.h> |
27 | 27 |
#include <lemon/error.h> |
28 | 28 |
/// \ingroup dimacs_group |
29 | 29 |
/// \file |
30 | 30 |
/// \brief DIMACS file format reader. |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
/// \addtogroup dimacs_group |
35 | 35 |
/// @{ |
36 | 36 |
|
37 | 37 |
/// DIMACS file type descriptor. |
38 | 38 |
struct DimacsDescriptor |
39 | 39 |
{ |
40 |
///File type enum |
|
41 |
enum Type |
|
42 |
{ |
|
43 |
NONE, MIN, MAX, SP, MAT |
|
44 |
|
|
40 |
///\brief DIMACS file type enum |
|
41 |
/// |
|
42 |
///DIMACS file type enum. |
|
43 |
enum Type { |
|
44 |
NONE, ///< Undefined type. |
|
45 |
MIN, ///< DIMACS file type for minimum cost flow problems. |
|
46 |
MAX, ///< DIMACS file type for maximum flow problems. |
|
47 |
SP, ///< DIMACS file type for shostest path problems. |
|
48 |
MAT ///< DIMACS file type for plain graphs and matching problems. |
|
49 |
}; |
|
45 | 50 |
///The file type |
46 | 51 |
Type type; |
47 | 52 |
///The number of nodes in the graph |
48 | 53 |
int nodeNum; |
49 | 54 |
///The number of edges in the graph |
50 | 55 |
int edgeNum; |
51 | 56 |
int lineShift; |
52 |
/// |
|
57 |
///Constructor. It sets the type to \c NONE. |
|
53 | 58 |
DimacsDescriptor() : type(NONE) {} |
54 | 59 |
}; |
55 | 60 |
|
56 | 61 |
///Discover the type of a DIMACS file |
57 | 62 |
|
58 |
///It starts seeking the beginning of the file for the problem type |
|
59 |
///and size info. The found data is returned in a special struct |
|
60 |
///that can be evaluated and passed to the appropriate reader |
|
61 |
///function. |
|
63 |
///This function starts seeking the beginning of the given file for the |
|
64 |
///problem type and size info. |
|
65 |
///The found data is returned in a special struct that can be evaluated |
|
66 |
///and passed to the appropriate reader function. |
|
62 | 67 |
DimacsDescriptor dimacsType(std::istream& is) |
63 | 68 |
{ |
64 | 69 |
DimacsDescriptor r; |
65 | 70 |
std::string problem,str; |
66 | 71 |
char c; |
67 | 72 |
r.lineShift=0; |
68 | 73 |
while (is >> c) |
69 | 74 |
switch(c) |
70 | 75 |
{ |
71 | 76 |
case 'p': |
72 | 77 |
if(is >> problem >> r.nodeNum >> r.edgeNum) |
73 | 78 |
{ |
74 | 79 |
getline(is, str); |
75 | 80 |
r.lineShift++; |
76 | 81 |
if(problem=="min") r.type=DimacsDescriptor::MIN; |
77 | 82 |
else if(problem=="max") r.type=DimacsDescriptor::MAX; |
78 | 83 |
else if(problem=="sp") r.type=DimacsDescriptor::SP; |
79 | 84 |
else if(problem=="mat") r.type=DimacsDescriptor::MAT; |
80 | 85 |
else throw FormatError("Unknown problem type"); |
81 | 86 |
return r; |
82 | 87 |
} |
83 | 88 |
else |
84 | 89 |
{ |
85 | 90 |
throw FormatError("Missing or wrong problem type declaration."); |
86 | 91 |
} |
87 | 92 |
break; |
88 | 93 |
case 'c': |
89 | 94 |
getline(is, str); |
90 | 95 |
r.lineShift++; |
91 | 96 |
break; |
92 | 97 |
default: |
93 | 98 |
throw FormatError("Unknown DIMACS declaration."); |
94 | 99 |
} |
95 | 100 |
throw FormatError("Missing problem type declaration."); |
96 | 101 |
} |
97 | 102 |
|
98 | 103 |
|
99 |
|
|
100 |
/// DIMACS minimum cost flow reader function. |
|
104 |
/// \brief DIMACS minimum cost flow reader function. |
|
101 | 105 |
/// |
102 | 106 |
/// This function reads a minimum cost flow instance from DIMACS format, |
103 | 107 |
/// i.e. from a DIMACS file having a line starting with |
104 | 108 |
/// \code |
105 | 109 |
/// p min |
106 | 110 |
/// \endcode |
107 | 111 |
/// At the beginning, \c g is cleared by \c g.clear(). The supply |
108 | 112 |
/// amount of the nodes are written to the \c supply node map |
109 | 113 |
/// (they are signed values). The lower bounds, capacities and costs |
110 | 114 |
/// of the arcs are written to the \c lower, \c capacity and \c cost |
111 | 115 |
/// arc maps. |
112 | 116 |
/// |
113 | 117 |
/// If the capacity of an arc is less than the lower bound, it will |
114 | 118 |
/// be set to "infinite" instead. The actual value of "infinite" is |
115 | 119 |
/// contolled by the \c infty parameter. If it is 0 (the default value), |
116 | 120 |
/// \c std::numeric_limits<Capacity>::infinity() will be used if available, |
117 | 121 |
/// \c std::numeric_limits<Capacity>::max() otherwise. If \c infty is set to |
118 | 122 |
/// a non-zero value, that value will be used as "infinite". |
119 | 123 |
/// |
120 | 124 |
/// If the file type was previously evaluated by dimacsType(), then |
121 | 125 |
/// the descriptor struct should be given by the \c dest parameter. |
122 | 126 |
template <typename Digraph, typename LowerMap, |
123 | 127 |
typename CapacityMap, typename CostMap, |
124 | 128 |
typename SupplyMap> |
125 | 129 |
void readDimacsMin(std::istream& is, |
126 | 130 |
Digraph &g, |
127 | 131 |
LowerMap& lower, |
128 | 132 |
CapacityMap& capacity, |
129 | 133 |
CostMap& cost, |
130 | 134 |
SupplyMap& supply, |
131 | 135 |
typename CapacityMap::Value infty = 0, |
132 | 136 |
DimacsDescriptor desc=DimacsDescriptor()) |
133 | 137 |
{ |
134 | 138 |
g.clear(); |
135 | 139 |
std::vector<typename Digraph::Node> nodes; |
136 | 140 |
typename Digraph::Arc e; |
137 | 141 |
std::string problem, str; |
138 | 142 |
char c; |
139 | 143 |
int i, j; |
140 | 144 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
141 | 145 |
if(desc.type!=DimacsDescriptor::MIN) |
142 | 146 |
throw FormatError("Problem type mismatch"); |
143 | 147 |
|
144 | 148 |
nodes.resize(desc.nodeNum + 1); |
145 | 149 |
for (int k = 1; k <= desc.nodeNum; ++k) { |
146 | 150 |
nodes[k] = g.addNode(); |
147 | 151 |
supply.set(nodes[k], 0); |
148 | 152 |
} |
149 | 153 |
|
150 | 154 |
typename SupplyMap::Value sup; |
151 | 155 |
typename CapacityMap::Value low; |
152 | 156 |
typename CapacityMap::Value cap; |
153 | 157 |
typename CostMap::Value co; |
154 | 158 |
typedef typename CapacityMap::Value Capacity; |
155 | 159 |
if(infty==0) |
156 | 160 |
infty = std::numeric_limits<Capacity>::has_infinity ? |
157 | 161 |
std::numeric_limits<Capacity>::infinity() : |
158 | 162 |
std::numeric_limits<Capacity>::max(); |
159 | 163 |
|
160 | 164 |
while (is >> c) { |
161 | 165 |
switch (c) { |
162 | 166 |
case 'c': // comment line |
163 | 167 |
getline(is, str); |
164 | 168 |
break; |
165 | 169 |
case 'n': // node definition line |
166 | 170 |
is >> i >> sup; |
167 | 171 |
getline(is, str); |
168 | 172 |
supply.set(nodes[i], sup); |
169 | 173 |
break; |
170 | 174 |
case 'a': // arc definition line |
171 | 175 |
is >> i >> j >> low >> cap >> co; |
172 | 176 |
getline(is, str); |
173 | 177 |
e = g.addArc(nodes[i], nodes[j]); |
174 | 178 |
lower.set(e, low); |
175 | 179 |
if (cap >= low) |
176 | 180 |
capacity.set(e, cap); |
177 | 181 |
else |
178 | 182 |
capacity.set(e, infty); |
179 | 183 |
cost.set(e, co); |
180 | 184 |
break; |
181 | 185 |
} |
182 | 186 |
} |
183 | 187 |
} |
184 | 188 |
|
185 | 189 |
template<typename Digraph, typename CapacityMap> |
186 | 190 |
void _readDimacs(std::istream& is, |
187 | 191 |
Digraph &g, |
188 | 192 |
CapacityMap& capacity, |
189 | 193 |
typename Digraph::Node &s, |
190 | 194 |
typename Digraph::Node &t, |
191 | 195 |
typename CapacityMap::Value infty = 0, |
192 | 196 |
DimacsDescriptor desc=DimacsDescriptor()) { |
193 | 197 |
g.clear(); |
194 | 198 |
s=t=INVALID; |
195 | 199 |
std::vector<typename Digraph::Node> nodes; |
196 | 200 |
typename Digraph::Arc e; |
197 | 201 |
char c, d; |
198 | 202 |
int i, j; |
199 | 203 |
typename CapacityMap::Value _cap; |
200 | 204 |
std::string str; |
201 | 205 |
nodes.resize(desc.nodeNum + 1); |
202 | 206 |
for (int k = 1; k <= desc.nodeNum; ++k) { |
203 | 207 |
nodes[k] = g.addNode(); |
204 | 208 |
} |
205 | 209 |
typedef typename CapacityMap::Value Capacity; |
206 | 210 |
|
207 | 211 |
if(infty==0) |
208 | 212 |
infty = std::numeric_limits<Capacity>::has_infinity ? |
209 | 213 |
std::numeric_limits<Capacity>::infinity() : |
210 | 214 |
std::numeric_limits<Capacity>::max(); |
211 | 215 |
|
212 | 216 |
while (is >> c) { |
213 | 217 |
switch (c) { |
214 | 218 |
case 'c': // comment line |
215 | 219 |
getline(is, str); |
216 | 220 |
break; |
217 | 221 |
case 'n': // node definition line |
218 | 222 |
if (desc.type==DimacsDescriptor::SP) { // shortest path problem |
219 | 223 |
is >> i; |
220 | 224 |
getline(is, str); |
221 | 225 |
s = nodes[i]; |
222 | 226 |
} |
223 | 227 |
if (desc.type==DimacsDescriptor::MAX) { // max flow problem |
224 | 228 |
is >> i >> d; |
225 | 229 |
getline(is, str); |
226 | 230 |
if (d == 's') s = nodes[i]; |
227 | 231 |
if (d == 't') t = nodes[i]; |
228 | 232 |
} |
229 | 233 |
break; |
230 | 234 |
case 'a': // arc definition line |
231 | 235 |
if (desc.type==DimacsDescriptor::SP) { |
232 | 236 |
is >> i >> j >> _cap; |
233 | 237 |
getline(is, str); |
234 | 238 |
e = g.addArc(nodes[i], nodes[j]); |
235 | 239 |
capacity.set(e, _cap); |
236 | 240 |
} |
237 | 241 |
else if (desc.type==DimacsDescriptor::MAX) { |
238 | 242 |
is >> i >> j >> _cap; |
239 | 243 |
getline(is, str); |
240 | 244 |
e = g.addArc(nodes[i], nodes[j]); |
241 | 245 |
if (_cap >= 0) |
242 | 246 |
capacity.set(e, _cap); |
243 | 247 |
else |
244 | 248 |
capacity.set(e, infty); |
245 | 249 |
} |
246 | 250 |
else { |
247 | 251 |
is >> i >> j; |
248 | 252 |
getline(is, str); |
249 | 253 |
g.addArc(nodes[i], nodes[j]); |
250 | 254 |
} |
251 | 255 |
break; |
252 | 256 |
} |
253 | 257 |
} |
254 | 258 |
} |
255 | 259 |
|
256 |
/// DIMACS maximum flow reader function. |
|
260 |
/// \brief DIMACS maximum flow reader function. |
|
257 | 261 |
/// |
258 | 262 |
/// This function reads a maximum flow instance from DIMACS format, |
259 | 263 |
/// i.e. from a DIMACS file having a line starting with |
260 | 264 |
/// \code |
261 | 265 |
/// p max |
262 | 266 |
/// \endcode |
263 | 267 |
/// At the beginning, \c g is cleared by \c g.clear(). The arc |
264 | 268 |
/// capacities are written to the \c capacity arc map and \c s and |
265 | 269 |
/// \c t are set to the source and the target nodes. |
266 | 270 |
/// |
267 | 271 |
/// If the capacity of an arc is negative, it will |
268 | 272 |
/// be set to "infinite" instead. The actual value of "infinite" is |
269 | 273 |
/// contolled by the \c infty parameter. If it is 0 (the default value), |
270 | 274 |
/// \c std::numeric_limits<Capacity>::infinity() will be used if available, |
271 | 275 |
/// \c std::numeric_limits<Capacity>::max() otherwise. If \c infty is set to |
272 | 276 |
/// a non-zero value, that value will be used as "infinite". |
273 | 277 |
/// |
274 | 278 |
/// If the file type was previously evaluated by dimacsType(), then |
275 | 279 |
/// the descriptor struct should be given by the \c dest parameter. |
276 | 280 |
template<typename Digraph, typename CapacityMap> |
277 | 281 |
void readDimacsMax(std::istream& is, |
278 | 282 |
Digraph &g, |
279 | 283 |
CapacityMap& capacity, |
280 | 284 |
typename Digraph::Node &s, |
281 | 285 |
typename Digraph::Node &t, |
282 | 286 |
typename CapacityMap::Value infty = 0, |
283 | 287 |
DimacsDescriptor desc=DimacsDescriptor()) { |
284 | 288 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
285 | 289 |
if(desc.type!=DimacsDescriptor::MAX) |
286 | 290 |
throw FormatError("Problem type mismatch"); |
287 | 291 |
_readDimacs(is,g,capacity,s,t,infty,desc); |
288 | 292 |
} |
289 | 293 |
|
290 |
/// DIMACS shortest path reader function. |
|
294 |
/// \brief DIMACS shortest path reader function. |
|
291 | 295 |
/// |
292 | 296 |
/// This function reads a shortest path instance from DIMACS format, |
293 | 297 |
/// i.e. from a DIMACS file having a line starting with |
294 | 298 |
/// \code |
295 | 299 |
/// p sp |
296 | 300 |
/// \endcode |
297 | 301 |
/// At the beginning, \c g is cleared by \c g.clear(). The arc |
298 | 302 |
/// lengths are written to the \c length arc map and \c s is set to the |
299 | 303 |
/// source node. |
300 | 304 |
/// |
301 | 305 |
/// If the file type was previously evaluated by dimacsType(), then |
302 | 306 |
/// the descriptor struct should be given by the \c dest parameter. |
303 | 307 |
template<typename Digraph, typename LengthMap> |
304 | 308 |
void readDimacsSp(std::istream& is, |
305 | 309 |
Digraph &g, |
306 | 310 |
LengthMap& length, |
307 | 311 |
typename Digraph::Node &s, |
308 | 312 |
DimacsDescriptor desc=DimacsDescriptor()) { |
309 | 313 |
typename Digraph::Node t; |
310 | 314 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
311 | 315 |
if(desc.type!=DimacsDescriptor::SP) |
312 | 316 |
throw FormatError("Problem type mismatch"); |
313 | 317 |
_readDimacs(is, g, length, s, t, 0, desc); |
314 | 318 |
} |
315 | 319 |
|
316 |
/// DIMACS capacitated digraph reader function. |
|
320 |
/// \brief DIMACS capacitated digraph reader function. |
|
317 | 321 |
/// |
318 | 322 |
/// This function reads an arc capacitated digraph instance from |
319 | 323 |
/// DIMACS 'max' or 'sp' format. |
320 | 324 |
/// At the beginning, \c g is cleared by \c g.clear() |
321 | 325 |
/// and the arc capacities/lengths are written to the \c capacity |
322 | 326 |
/// arc map. |
323 | 327 |
/// |
324 | 328 |
/// In case of the 'max' format, if the capacity of an arc is negative, |
325 | 329 |
/// it will |
326 | 330 |
/// be set to "infinite" instead. The actual value of "infinite" is |
327 | 331 |
/// contolled by the \c infty parameter. If it is 0 (the default value), |
328 | 332 |
/// \c std::numeric_limits<Capacity>::infinity() will be used if available, |
329 | 333 |
/// \c std::numeric_limits<Capacity>::max() otherwise. If \c infty is set to |
330 | 334 |
/// a non-zero value, that value will be used as "infinite". |
331 | 335 |
/// |
332 | 336 |
/// If the file type was previously evaluated by dimacsType(), then |
333 | 337 |
/// the descriptor struct should be given by the \c dest parameter. |
334 | 338 |
template<typename Digraph, typename CapacityMap> |
335 | 339 |
void readDimacsCap(std::istream& is, |
336 | 340 |
Digraph &g, |
337 | 341 |
CapacityMap& capacity, |
338 | 342 |
typename CapacityMap::Value infty = 0, |
339 | 343 |
DimacsDescriptor desc=DimacsDescriptor()) { |
340 | 344 |
typename Digraph::Node u,v; |
341 | 345 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
342 | 346 |
if(desc.type!=DimacsDescriptor::MAX || desc.type!=DimacsDescriptor::SP) |
343 | 347 |
throw FormatError("Problem type mismatch"); |
344 | 348 |
_readDimacs(is, g, capacity, u, v, infty, desc); |
345 | 349 |
} |
346 | 350 |
|
347 | 351 |
template<typename Graph> |
348 | 352 |
typename enable_if<lemon::UndirectedTagIndicator<Graph>,void>::type |
349 | 353 |
_addArcEdge(Graph &g, typename Graph::Node s, typename Graph::Node t, |
350 | 354 |
dummy<0> = 0) |
351 | 355 |
{ |
352 | 356 |
g.addEdge(s,t); |
353 | 357 |
} |
354 | 358 |
template<typename Graph> |
355 | 359 |
typename disable_if<lemon::UndirectedTagIndicator<Graph>,void>::type |
356 | 360 |
_addArcEdge(Graph &g, typename Graph::Node s, typename Graph::Node t, |
357 | 361 |
dummy<1> = 1) |
358 | 362 |
{ |
359 | 363 |
g.addArc(s,t); |
360 | 364 |
} |
361 | 365 |
|
362 |
/// DIMACS plain (di)graph reader function. |
|
366 |
/// \brief DIMACS plain (di)graph reader function. |
|
363 | 367 |
/// |
364 |
/// This function reads a (di)graph without any designated nodes and |
|
365 |
/// maps from DIMACS format, i.e. from DIMACS files having a line |
|
366 |
/// |
|
368 |
/// This function reads a plain (di)graph without any designated nodes |
|
369 |
/// and maps (e.g. a matching instance) from DIMACS format, i.e. from |
|
370 |
/// DIMACS files having a line starting with |
|
367 | 371 |
/// \code |
368 | 372 |
/// p mat |
369 | 373 |
/// \endcode |
370 | 374 |
/// At the beginning, \c g is cleared by \c g.clear(). |
371 | 375 |
/// |
372 | 376 |
/// If the file type was previously evaluated by dimacsType(), then |
373 | 377 |
/// the descriptor struct should be given by the \c dest parameter. |
374 | 378 |
template<typename Graph> |
375 | 379 |
void readDimacsMat(std::istream& is, Graph &g, |
376 | 380 |
DimacsDescriptor desc=DimacsDescriptor()) |
377 | 381 |
{ |
378 | 382 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
379 | 383 |
if(desc.type!=DimacsDescriptor::MAT) |
380 | 384 |
throw FormatError("Problem type mismatch"); |
381 | 385 |
|
382 | 386 |
g.clear(); |
383 | 387 |
std::vector<typename Graph::Node> nodes; |
384 | 388 |
char c; |
385 | 389 |
int i, j; |
386 | 390 |
std::string str; |
387 | 391 |
nodes.resize(desc.nodeNum + 1); |
388 | 392 |
for (int k = 1; k <= desc.nodeNum; ++k) { |
389 | 393 |
nodes[k] = g.addNode(); |
390 | 394 |
} |
391 | 395 |
|
392 | 396 |
while (is >> c) { |
393 | 397 |
switch (c) { |
394 | 398 |
case 'c': // comment line |
395 | 399 |
getline(is, str); |
396 | 400 |
break; |
397 | 401 |
case 'n': // node definition line |
398 | 402 |
break; |
399 | 403 |
case 'a': // arc definition line |
400 | 404 |
is >> i >> j; |
401 | 405 |
getline(is, str); |
402 | 406 |
_addArcEdge(g,nodes[i], nodes[j]); |
403 | 407 |
break; |
404 | 408 |
} |
405 | 409 |
} |
406 | 410 |
} |
407 | 411 |
|
408 | 412 |
/// DIMACS plain digraph writer function. |
409 | 413 |
/// |
410 | 414 |
/// This function writes a digraph without any designated nodes and |
411 | 415 |
/// maps into DIMACS format, i.e. into DIMACS file having a line |
412 | 416 |
/// starting with |
413 | 417 |
/// \code |
414 | 418 |
/// p mat |
415 | 419 |
/// \endcode |
416 | 420 |
/// If \c comment is not empty, then it will be printed in the first line |
417 | 421 |
/// prefixed by 'c'. |
418 | 422 |
template<typename Digraph> |
419 | 423 |
void writeDimacsMat(std::ostream& os, const Digraph &g, |
420 | 424 |
std::string comment="") { |
421 | 425 |
typedef typename Digraph::NodeIt NodeIt; |
422 | 426 |
typedef typename Digraph::ArcIt ArcIt; |
423 | 427 |
|
424 | 428 |
if(!comment.empty()) |
425 | 429 |
os << "c " << comment << std::endl; |
426 | 430 |
os << "p mat " << g.nodeNum() << " " << g.arcNum() << std::endl; |
427 | 431 |
|
428 | 432 |
typename Digraph::template NodeMap<int> nodes(g); |
429 | 433 |
int i = 1; |
430 | 434 |
for(NodeIt v(g); v != INVALID; ++v) { |
431 | 435 |
nodes.set(v, i); |
432 | 436 |
++i; |
433 | 437 |
} |
434 | 438 |
for(ArcIt e(g); e != INVALID; ++e) { |
435 | 439 |
os << "a " << nodes[g.source(e)] << " " << nodes[g.target(e)] |
436 | 440 |
<< std::endl; |
437 | 441 |
} |
438 | 442 |
} |
439 | 443 |
|
440 | 444 |
/// @} |
441 | 445 |
|
442 | 446 |
} //namespace lemon |
443 | 447 |
|
444 | 448 |
#endif //LEMON_DIMACS_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_GRAPH_TO_EPS_H |
20 | 20 |
#define LEMON_GRAPH_TO_EPS_H |
21 | 21 |
|
22 | 22 |
#include<iostream> |
23 | 23 |
#include<fstream> |
24 | 24 |
#include<sstream> |
25 | 25 |
#include<algorithm> |
26 | 26 |
#include<vector> |
27 | 27 |
|
28 | 28 |
#ifndef WIN32 |
29 | 29 |
#include<sys/time.h> |
30 | 30 |
#include<ctime> |
31 | 31 |
#else |
32 | 32 |
#include<lemon/bits/windows.h> |
33 | 33 |
#endif |
34 | 34 |
|
35 | 35 |
#include<lemon/math.h> |
36 | 36 |
#include<lemon/core.h> |
37 | 37 |
#include<lemon/dim2.h> |
38 | 38 |
#include<lemon/maps.h> |
39 | 39 |
#include<lemon/color.h> |
40 | 40 |
#include<lemon/bits/bezier.h> |
41 | 41 |
#include<lemon/error.h> |
42 | 42 |
|
43 | 43 |
|
44 | 44 |
///\ingroup eps_io |
45 | 45 |
///\file |
46 | 46 |
///\brief A well configurable tool for visualizing graphs |
47 | 47 |
|
48 | 48 |
namespace lemon { |
49 | 49 |
|
50 | 50 |
namespace _graph_to_eps_bits { |
51 | 51 |
template<class MT> |
52 | 52 |
class _NegY { |
53 | 53 |
public: |
54 | 54 |
typedef typename MT::Key Key; |
55 | 55 |
typedef typename MT::Value Value; |
56 | 56 |
const MT ↦ |
57 | 57 |
int yscale; |
58 | 58 |
_NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {} |
59 | 59 |
Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);} |
60 | 60 |
}; |
61 | 61 |
} |
62 | 62 |
|
63 | 63 |
///Default traits class of GraphToEps |
64 | 64 |
|
65 | 65 |
///Default traits class of \ref GraphToEps. |
66 | 66 |
/// |
67 | 67 |
///\param GR is the type of the underlying graph. |
68 | 68 |
template<class GR> |
69 | 69 |
struct DefaultGraphToEpsTraits |
70 | 70 |
{ |
71 | 71 |
typedef GR Graph; |
72 | 72 |
typedef typename Graph::Node Node; |
73 | 73 |
typedef typename Graph::NodeIt NodeIt; |
74 | 74 |
typedef typename Graph::Arc Arc; |
75 | 75 |
typedef typename Graph::ArcIt ArcIt; |
76 | 76 |
typedef typename Graph::InArcIt InArcIt; |
77 | 77 |
typedef typename Graph::OutArcIt OutArcIt; |
78 | 78 |
|
79 | 79 |
|
80 | 80 |
const Graph &g; |
81 | 81 |
|
82 | 82 |
std::ostream& os; |
83 | 83 |
|
84 | 84 |
typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType; |
85 | 85 |
CoordsMapType _coords; |
86 | 86 |
ConstMap<typename Graph::Node,double > _nodeSizes; |
87 | 87 |
ConstMap<typename Graph::Node,int > _nodeShapes; |
88 | 88 |
|
89 | 89 |
ConstMap<typename Graph::Node,Color > _nodeColors; |
90 | 90 |
ConstMap<typename Graph::Arc,Color > _arcColors; |
91 | 91 |
|
92 | 92 |
ConstMap<typename Graph::Arc,double > _arcWidths; |
93 | 93 |
|
94 | 94 |
double _arcWidthScale; |
95 | 95 |
|
96 | 96 |
double _nodeScale; |
97 | 97 |
double _xBorder, _yBorder; |
98 | 98 |
double _scale; |
99 | 99 |
double _nodeBorderQuotient; |
100 | 100 |
|
101 | 101 |
bool _drawArrows; |
102 | 102 |
double _arrowLength, _arrowWidth; |
103 | 103 |
|
104 | 104 |
bool _showNodes, _showArcs; |
105 | 105 |
|
106 | 106 |
bool _enableParallel; |
107 | 107 |
double _parArcDist; |
108 | 108 |
|
109 | 109 |
bool _showNodeText; |
110 | 110 |
ConstMap<typename Graph::Node,bool > _nodeTexts; |
111 | 111 |
double _nodeTextSize; |
112 | 112 |
|
113 | 113 |
bool _showNodePsText; |
114 | 114 |
ConstMap<typename Graph::Node,bool > _nodePsTexts; |
115 | 115 |
char *_nodePsTextsPreamble; |
116 | 116 |
|
117 | 117 |
bool _undirected; |
118 | 118 |
|
119 | 119 |
bool _pleaseRemoveOsStream; |
120 | 120 |
|
121 | 121 |
bool _scaleToA4; |
122 | 122 |
|
123 | 123 |
std::string _title; |
124 | 124 |
std::string _copyright; |
125 | 125 |
|
126 | 126 |
enum NodeTextColorType |
127 | 127 |
{ DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType; |
128 | 128 |
ConstMap<typename Graph::Node,Color > _nodeTextColors; |
129 | 129 |
|
130 | 130 |
bool _autoNodeScale; |
131 | 131 |
bool _autoArcWidthScale; |
132 | 132 |
|
133 | 133 |
bool _absoluteNodeSizes; |
134 | 134 |
bool _absoluteArcWidths; |
135 | 135 |
|
136 | 136 |
bool _negY; |
137 | 137 |
|
138 | 138 |
bool _preScale; |
139 | 139 |
///Constructor |
140 | 140 |
|
141 | 141 |
///Constructor |
142 | 142 |
///\param gr Reference to the graph to be printed. |
143 | 143 |
///\param ost Reference to the output stream. |
144 | 144 |
///By default it is <tt>std::cout</tt>. |
145 | 145 |
///\param pros If it is \c true, then the \c ostream referenced by \c os |
146 | 146 |
///will be explicitly deallocated by the destructor. |
147 | 147 |
DefaultGraphToEpsTraits(const GR &gr, std::ostream& ost = std::cout, |
148 | 148 |
bool pros = false) : |
149 | 149 |
g(gr), os(ost), |
150 | 150 |
_coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0), |
151 | 151 |
_nodeColors(WHITE), _arcColors(BLACK), |
152 | 152 |
_arcWidths(1.0), _arcWidthScale(0.003), |
153 | 153 |
_nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0), |
154 | 154 |
_nodeBorderQuotient(.1), |
155 | 155 |
_drawArrows(false), _arrowLength(1), _arrowWidth(0.3), |
156 | 156 |
_showNodes(true), _showArcs(true), |
157 | 157 |
_enableParallel(false), _parArcDist(1), |
158 | 158 |
_showNodeText(false), _nodeTexts(false), _nodeTextSize(1), |
159 | 159 |
_showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0), |
160 | 160 |
_undirected(lemon::UndirectedTagIndicator<GR>::value), |
161 | 161 |
_pleaseRemoveOsStream(pros), _scaleToA4(false), |
162 | 162 |
_nodeTextColorType(SAME_COL), _nodeTextColors(BLACK), |
163 | 163 |
_autoNodeScale(false), |
164 | 164 |
_autoArcWidthScale(false), |
165 | 165 |
_absoluteNodeSizes(false), |
166 | 166 |
_absoluteArcWidths(false), |
167 | 167 |
_negY(false), |
168 | 168 |
_preScale(true) |
169 | 169 |
{} |
170 | 170 |
}; |
171 | 171 |
|
172 | 172 |
///Auxiliary class to implement the named parameters of \ref graphToEps() |
173 | 173 |
|
174 | 174 |
///Auxiliary class to implement the named parameters of \ref graphToEps(). |
175 | 175 |
/// |
176 | 176 |
///For detailed examples see the \ref graph_to_eps_demo.cc demo file. |
177 | 177 |
template<class T> class GraphToEps : public T |
178 | 178 |
{ |
179 | 179 |
// Can't believe it is required by the C++ standard |
180 | 180 |
using T::g; |
181 | 181 |
using T::os; |
182 | 182 |
|
183 | 183 |
using T::_coords; |
184 | 184 |
using T::_nodeSizes; |
185 | 185 |
using T::_nodeShapes; |
186 | 186 |
using T::_nodeColors; |
187 | 187 |
using T::_arcColors; |
188 | 188 |
using T::_arcWidths; |
189 | 189 |
|
190 | 190 |
using T::_arcWidthScale; |
191 | 191 |
using T::_nodeScale; |
192 | 192 |
using T::_xBorder; |
193 | 193 |
using T::_yBorder; |
194 | 194 |
using T::_scale; |
195 | 195 |
using T::_nodeBorderQuotient; |
196 | 196 |
|
197 | 197 |
using T::_drawArrows; |
198 | 198 |
using T::_arrowLength; |
199 | 199 |
using T::_arrowWidth; |
200 | 200 |
|
201 | 201 |
using T::_showNodes; |
202 | 202 |
using T::_showArcs; |
203 | 203 |
|
204 | 204 |
using T::_enableParallel; |
205 | 205 |
using T::_parArcDist; |
206 | 206 |
|
207 | 207 |
using T::_showNodeText; |
208 | 208 |
using T::_nodeTexts; |
209 | 209 |
using T::_nodeTextSize; |
210 | 210 |
|
211 | 211 |
using T::_showNodePsText; |
212 | 212 |
using T::_nodePsTexts; |
213 | 213 |
using T::_nodePsTextsPreamble; |
214 | 214 |
|
215 | 215 |
using T::_undirected; |
216 | 216 |
|
217 | 217 |
using T::_pleaseRemoveOsStream; |
218 | 218 |
|
219 | 219 |
using T::_scaleToA4; |
220 | 220 |
|
221 | 221 |
using T::_title; |
222 | 222 |
using T::_copyright; |
223 | 223 |
|
224 | 224 |
using T::NodeTextColorType; |
225 | 225 |
using T::CUST_COL; |
226 | 226 |
using T::DIST_COL; |
227 | 227 |
using T::DIST_BW; |
228 | 228 |
using T::_nodeTextColorType; |
229 | 229 |
using T::_nodeTextColors; |
230 | 230 |
|
231 | 231 |
using T::_autoNodeScale; |
232 | 232 |
using T::_autoArcWidthScale; |
233 | 233 |
|
234 | 234 |
using T::_absoluteNodeSizes; |
235 | 235 |
using T::_absoluteArcWidths; |
236 | 236 |
|
237 | 237 |
|
238 | 238 |
using T::_negY; |
239 | 239 |
using T::_preScale; |
240 | 240 |
|
241 | 241 |
// dradnats ++C eht yb deriuqer si ti eveileb t'naC |
242 | 242 |
|
243 | 243 |
typedef typename T::Graph Graph; |
244 | 244 |
typedef typename Graph::Node Node; |
245 | 245 |
typedef typename Graph::NodeIt NodeIt; |
246 | 246 |
typedef typename Graph::Arc Arc; |
247 | 247 |
typedef typename Graph::ArcIt ArcIt; |
248 | 248 |
typedef typename Graph::InArcIt InArcIt; |
249 | 249 |
typedef typename Graph::OutArcIt OutArcIt; |
250 | 250 |
|
251 | 251 |
static const int INTERPOL_PREC; |
252 | 252 |
static const double A4HEIGHT; |
253 | 253 |
static const double A4WIDTH; |
254 | 254 |
static const double A4BORDER; |
255 | 255 |
|
256 | 256 |
bool dontPrint; |
257 | 257 |
|
258 | 258 |
public: |
259 | 259 |
///Node shapes |
260 | 260 |
|
261 | 261 |
///Node shapes. |
262 | 262 |
/// |
263 | 263 |
enum NodeShapes { |
264 | 264 |
/// = 0 |
265 | 265 |
///\image html nodeshape_0.png |
266 | 266 |
///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm |
267 | 267 |
CIRCLE=0, |
268 | 268 |
/// = 1 |
269 | 269 |
///\image html nodeshape_1.png |
270 | 270 |
///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm |
271 |
/// |
|
272 | 271 |
SQUARE=1, |
273 | 272 |
/// = 2 |
274 | 273 |
///\image html nodeshape_2.png |
275 | 274 |
///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm |
276 |
/// |
|
277 | 275 |
DIAMOND=2, |
278 | 276 |
/// = 3 |
279 | 277 |
///\image html nodeshape_3.png |
280 |
///\image latex nodeshape_2.eps "MALE shape (4)" width=2cm |
|
281 |
/// |
|
278 |
///\image latex nodeshape_3.eps "MALE shape (3)" width=2cm |
|
282 | 279 |
MALE=3, |
283 | 280 |
/// = 4 |
284 | 281 |
///\image html nodeshape_4.png |
285 |
///\image latex nodeshape_2.eps "FEMALE shape (4)" width=2cm |
|
286 |
/// |
|
282 |
///\image latex nodeshape_4.eps "FEMALE shape (4)" width=2cm |
|
287 | 283 |
FEMALE=4 |
288 | 284 |
}; |
289 | 285 |
|
290 | 286 |
private: |
291 | 287 |
class arcLess { |
292 | 288 |
const Graph &g; |
293 | 289 |
public: |
294 | 290 |
arcLess(const Graph &_g) : g(_g) {} |
295 | 291 |
bool operator()(Arc a,Arc b) const |
296 | 292 |
{ |
297 | 293 |
Node ai=std::min(g.source(a),g.target(a)); |
298 | 294 |
Node aa=std::max(g.source(a),g.target(a)); |
299 | 295 |
Node bi=std::min(g.source(b),g.target(b)); |
300 | 296 |
Node ba=std::max(g.source(b),g.target(b)); |
301 | 297 |
return ai<bi || |
302 | 298 |
(ai==bi && (aa < ba || |
303 | 299 |
(aa==ba && ai==g.source(a) && bi==g.target(b)))); |
304 | 300 |
} |
305 | 301 |
}; |
306 | 302 |
bool isParallel(Arc e,Arc f) const |
307 | 303 |
{ |
308 | 304 |
return (g.source(e)==g.source(f)&& |
309 | 305 |
g.target(e)==g.target(f)) || |
310 | 306 |
(g.source(e)==g.target(f)&& |
311 | 307 |
g.target(e)==g.source(f)); |
312 | 308 |
} |
313 | 309 |
template<class TT> |
314 | 310 |
static std::string psOut(const dim2::Point<TT> &p) |
315 | 311 |
{ |
316 | 312 |
std::ostringstream os; |
317 | 313 |
os << p.x << ' ' << p.y; |
318 | 314 |
return os.str(); |
319 | 315 |
} |
320 | 316 |
static std::string psOut(const Color &c) |
321 | 317 |
{ |
322 | 318 |
std::ostringstream os; |
323 | 319 |
os << c.red() << ' ' << c.green() << ' ' << c.blue(); |
324 | 320 |
return os.str(); |
325 | 321 |
} |
326 | 322 |
|
327 | 323 |
public: |
328 | 324 |
GraphToEps(const T &t) : T(t), dontPrint(false) {}; |
329 | 325 |
|
330 | 326 |
template<class X> struct CoordsTraits : public T { |
331 | 327 |
typedef X CoordsMapType; |
332 | 328 |
const X &_coords; |
333 | 329 |
CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {} |
334 | 330 |
}; |
335 | 331 |
///Sets the map of the node coordinates |
336 | 332 |
|
337 | 333 |
///Sets the map of the node coordinates. |
338 | 334 |
///\param x must be a node map with \ref dim2::Point "dim2::Point<double>" or |
339 | 335 |
///\ref dim2::Point "dim2::Point<int>" values. |
340 | 336 |
template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) { |
341 | 337 |
dontPrint=true; |
342 | 338 |
return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x)); |
343 | 339 |
} |
344 | 340 |
template<class X> struct NodeSizesTraits : public T { |
345 | 341 |
const X &_nodeSizes; |
346 | 342 |
NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {} |
347 | 343 |
}; |
348 | 344 |
///Sets the map of the node sizes |
349 | 345 |
|
350 | 346 |
///Sets the map of the node sizes. |
351 | 347 |
///\param x must be a node map with \c double (or convertible) values. |
352 | 348 |
template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x) |
353 | 349 |
{ |
354 | 350 |
dontPrint=true; |
355 | 351 |
return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x)); |
356 | 352 |
} |
357 | 353 |
template<class X> struct NodeShapesTraits : public T { |
358 | 354 |
const X &_nodeShapes; |
359 | 355 |
NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {} |
360 | 356 |
}; |
361 | 357 |
///Sets the map of the node shapes |
362 | 358 |
|
363 | 359 |
///Sets the map of the node shapes. |
364 | 360 |
///The available shape values |
365 | 361 |
///can be found in \ref NodeShapes "enum NodeShapes". |
366 | 362 |
///\param x must be a node map with \c int (or convertible) values. |
367 | 363 |
///\sa NodeShapes |
368 | 364 |
template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x) |
369 | 365 |
{ |
370 | 366 |
dontPrint=true; |
371 | 367 |
return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x)); |
372 | 368 |
} |
373 | 369 |
template<class X> struct NodeTextsTraits : public T { |
374 | 370 |
const X &_nodeTexts; |
375 | 371 |
NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {} |
376 | 372 |
}; |
377 | 373 |
///Sets the text printed on the nodes |
378 | 374 |
|
379 | 375 |
///Sets the text printed on the nodes. |
380 | 376 |
///\param x must be a node map with type that can be pushed to a standard |
381 | 377 |
///\c ostream. |
382 | 378 |
template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x) |
383 | 379 |
{ |
384 | 380 |
dontPrint=true; |
385 | 381 |
_showNodeText=true; |
386 | 382 |
return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x)); |
387 | 383 |
} |
388 | 384 |
template<class X> struct NodePsTextsTraits : public T { |
389 | 385 |
const X &_nodePsTexts; |
390 | 386 |
NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {} |
391 | 387 |
}; |
392 | 388 |
///Inserts a PostScript block to the nodes |
393 | 389 |
|
394 | 390 |
///With this command it is possible to insert a verbatim PostScript |
395 | 391 |
///block to the nodes. |
396 | 392 |
///The PS current point will be moved to the center of the node before |
397 | 393 |
///the PostScript block inserted. |
398 | 394 |
/// |
399 | 395 |
///Before and after the block a newline character is inserted so you |
400 | 396 |
///don't have to bother with the separators. |
401 | 397 |
/// |
402 | 398 |
///\param x must be a node map with type that can be pushed to a standard |
403 | 399 |
///\c ostream. |
404 | 400 |
/// |
405 | 401 |
///\sa nodePsTextsPreamble() |
406 | 402 |
template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x) |
407 | 403 |
{ |
408 | 404 |
dontPrint=true; |
409 | 405 |
_showNodePsText=true; |
410 | 406 |
return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x)); |
411 | 407 |
} |
412 | 408 |
template<class X> struct ArcWidthsTraits : public T { |
413 | 409 |
const X &_arcWidths; |
414 | 410 |
ArcWidthsTraits(const T &t,const X &x) : T(t), _arcWidths(x) {} |
415 | 411 |
}; |
416 | 412 |
///Sets the map of the arc widths |
417 | 413 |
|
418 | 414 |
///Sets the map of the arc widths. |
419 | 415 |
///\param x must be an arc map with \c double (or convertible) values. |
420 | 416 |
template<class X> GraphToEps<ArcWidthsTraits<X> > arcWidths(const X &x) |
421 | 417 |
{ |
422 | 418 |
dontPrint=true; |
423 | 419 |
return GraphToEps<ArcWidthsTraits<X> >(ArcWidthsTraits<X>(*this,x)); |
424 | 420 |
} |
425 | 421 |
|
426 | 422 |
template<class X> struct NodeColorsTraits : public T { |
427 | 423 |
const X &_nodeColors; |
428 | 424 |
NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {} |
429 | 425 |
}; |
430 | 426 |
///Sets the map of the node colors |
431 | 427 |
|
432 | 428 |
///Sets the map of the node colors. |
433 | 429 |
///\param x must be a node map with \ref Color values. |
434 | 430 |
/// |
435 | 431 |
///\sa Palette |
436 | 432 |
template<class X> GraphToEps<NodeColorsTraits<X> > |
437 | 433 |
nodeColors(const X &x) |
438 | 434 |
{ |
439 | 435 |
dontPrint=true; |
440 | 436 |
return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x)); |
441 | 437 |
} |
442 | 438 |
template<class X> struct NodeTextColorsTraits : public T { |
443 | 439 |
const X &_nodeTextColors; |
444 | 440 |
NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {} |
445 | 441 |
}; |
446 | 442 |
///Sets the map of the node text colors |
447 | 443 |
|
448 | 444 |
///Sets the map of the node text colors. |
449 | 445 |
///\param x must be a node map with \ref Color values. |
450 | 446 |
/// |
451 | 447 |
///\sa Palette |
452 | 448 |
template<class X> GraphToEps<NodeTextColorsTraits<X> > |
453 | 449 |
nodeTextColors(const X &x) |
454 | 450 |
{ |
455 | 451 |
dontPrint=true; |
456 | 452 |
_nodeTextColorType=CUST_COL; |
457 | 453 |
return GraphToEps<NodeTextColorsTraits<X> > |
458 | 454 |
(NodeTextColorsTraits<X>(*this,x)); |
459 | 455 |
} |
460 | 456 |
template<class X> struct ArcColorsTraits : public T { |
461 | 457 |
const X &_arcColors; |
462 | 458 |
ArcColorsTraits(const T &t,const X &x) : T(t), _arcColors(x) {} |
463 | 459 |
}; |
464 | 460 |
///Sets the map of the arc colors |
465 | 461 |
|
466 | 462 |
///Sets the map of the arc colors. |
467 | 463 |
///\param x must be an arc map with \ref Color values. |
468 | 464 |
/// |
469 | 465 |
///\sa Palette |
470 | 466 |
template<class X> GraphToEps<ArcColorsTraits<X> > |
471 | 467 |
arcColors(const X &x) |
472 | 468 |
{ |
473 | 469 |
dontPrint=true; |
474 | 470 |
return GraphToEps<ArcColorsTraits<X> >(ArcColorsTraits<X>(*this,x)); |
475 | 471 |
} |
476 | 472 |
///Sets a global scale factor for node sizes |
477 | 473 |
|
478 | 474 |
///Sets a global scale factor for node sizes. |
479 | 475 |
/// |
480 | 476 |
/// If nodeSizes() is not given, this function simply sets the node |
481 | 477 |
/// sizes to \c d. If nodeSizes() is given, but |
482 | 478 |
/// autoNodeScale() is not, then the node size given by |
483 | 479 |
/// nodeSizes() will be multiplied by the value \c d. |
484 | 480 |
/// If both nodeSizes() and autoNodeScale() are used, then the |
485 | 481 |
/// node sizes will be scaled in such a way that the greatest size will be |
486 | 482 |
/// equal to \c d. |
487 | 483 |
/// \sa nodeSizes() |
488 | 484 |
/// \sa autoNodeScale() |
489 | 485 |
GraphToEps<T> &nodeScale(double d=.01) {_nodeScale=d;return *this;} |
490 | 486 |
///Turns on/off the automatic node size scaling. |
491 | 487 |
|
492 | 488 |
///Turns on/off the automatic node size scaling. |
493 | 489 |
/// |
494 | 490 |
///\sa nodeScale() |
495 | 491 |
/// |
496 | 492 |
GraphToEps<T> &autoNodeScale(bool b=true) { |
497 | 493 |
_autoNodeScale=b;return *this; |
498 | 494 |
} |
499 | 495 |
|
500 | 496 |
///Turns on/off the absolutematic node size scaling. |
501 | 497 |
|
502 | 498 |
///Turns on/off the absolutematic node size scaling. |
503 | 499 |
/// |
504 | 500 |
///\sa nodeScale() |
505 | 501 |
/// |
506 | 502 |
GraphToEps<T> &absoluteNodeSizes(bool b=true) { |
507 | 503 |
_absoluteNodeSizes=b;return *this; |
508 | 504 |
} |
509 | 505 |
|
510 | 506 |
///Negates the Y coordinates. |
511 | 507 |
GraphToEps<T> &negateY(bool b=true) { |
512 | 508 |
_negY=b;return *this; |
513 | 509 |
} |
514 | 510 |
|
515 | 511 |
///Turn on/off pre-scaling |
516 | 512 |
|
517 | 513 |
///By default graphToEps() rescales the whole image in order to avoid |
518 | 514 |
///very big or very small bounding boxes. |
519 | 515 |
/// |
520 | 516 |
///This (p)rescaling can be turned off with this function. |
521 | 517 |
/// |
522 | 518 |
GraphToEps<T> &preScale(bool b=true) { |
523 | 519 |
_preScale=b;return *this; |
524 | 520 |
} |
525 | 521 |
|
526 | 522 |
///Sets a global scale factor for arc widths |
527 | 523 |
|
528 | 524 |
/// Sets a global scale factor for arc widths. |
529 | 525 |
/// |
530 | 526 |
/// If arcWidths() is not given, this function simply sets the arc |
531 | 527 |
/// widths to \c d. If arcWidths() is given, but |
532 | 528 |
/// autoArcWidthScale() is not, then the arc withs given by |
533 | 529 |
/// arcWidths() will be multiplied by the value \c d. |
534 | 530 |
/// If both arcWidths() and autoArcWidthScale() are used, then the |
535 | 531 |
/// arc withs will be scaled in such a way that the greatest width will be |
536 | 532 |
/// equal to \c d. |
537 | 533 |
GraphToEps<T> &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;} |
538 | 534 |
///Turns on/off the automatic arc width scaling. |
539 | 535 |
|
540 | 536 |
///Turns on/off the automatic arc width scaling. |
541 | 537 |
/// |
542 | 538 |
///\sa arcWidthScale() |
543 | 539 |
/// |
544 | 540 |
GraphToEps<T> &autoArcWidthScale(bool b=true) { |
545 | 541 |
_autoArcWidthScale=b;return *this; |
546 | 542 |
} |
547 | 543 |
///Turns on/off the absolutematic arc width scaling. |
548 | 544 |
|
549 | 545 |
///Turns on/off the absolutematic arc width scaling. |
550 | 546 |
/// |
551 | 547 |
///\sa arcWidthScale() |
552 | 548 |
/// |
553 | 549 |
GraphToEps<T> &absoluteArcWidths(bool b=true) { |
554 | 550 |
_absoluteArcWidths=b;return *this; |
555 | 551 |
} |
556 | 552 |
///Sets a global scale factor for the whole picture |
557 | 553 |
GraphToEps<T> &scale(double d) {_scale=d;return *this;} |
558 | 554 |
///Sets the width of the border around the picture |
559 | 555 |
GraphToEps<T> &border(double b=10) {_xBorder=_yBorder=b;return *this;} |
560 | 556 |
///Sets the width of the border around the picture |
561 | 557 |
GraphToEps<T> &border(double x, double y) { |
562 | 558 |
_xBorder=x;_yBorder=y;return *this; |
563 | 559 |
} |
564 | 560 |
///Sets whether to draw arrows |
565 | 561 |
GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;} |
566 | 562 |
///Sets the length of the arrowheads |
567 | 563 |
GraphToEps<T> &arrowLength(double d=1.0) {_arrowLength*=d;return *this;} |
568 | 564 |
///Sets the width of the arrowheads |
569 | 565 |
GraphToEps<T> &arrowWidth(double d=.3) {_arrowWidth*=d;return *this;} |
570 | 566 |
|
571 | 567 |
///Scales the drawing to fit to A4 page |
572 | 568 |
GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;} |
573 | 569 |
|
574 | 570 |
///Enables parallel arcs |
575 | 571 |
GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;} |
576 | 572 |
|
577 | 573 |
///Sets the distance between parallel arcs |
578 | 574 |
GraphToEps<T> &parArcDist(double d) {_parArcDist*=d;return *this;} |
579 | 575 |
|
580 | 576 |
///Hides the arcs |
581 | 577 |
GraphToEps<T> &hideArcs(bool b=true) {_showArcs=!b;return *this;} |
582 | 578 |
///Hides the nodes |
583 | 579 |
GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;} |
584 | 580 |
|
585 | 581 |
///Sets the size of the node texts |
586 | 582 |
GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;} |
587 | 583 |
|
588 | 584 |
///Sets the color of the node texts to be different from the node color |
589 | 585 |
|
590 | 586 |
///Sets the color of the node texts to be as different from the node color |
591 | 587 |
///as it is possible. |
592 | 588 |
GraphToEps<T> &distantColorNodeTexts() |
593 | 589 |
{_nodeTextColorType=DIST_COL;return *this;} |
594 | 590 |
///Sets the color of the node texts to be black or white and always visible. |
595 | 591 |
|
596 | 592 |
///Sets the color of the node texts to be black or white according to |
597 | 593 |
///which is more different from the node color. |
598 | 594 |
GraphToEps<T> &distantBWNodeTexts() |
599 | 595 |
{_nodeTextColorType=DIST_BW;return *this;} |
600 | 596 |
|
601 | 597 |
///Gives a preamble block for node Postscript block. |
602 | 598 |
|
603 | 599 |
///Gives a preamble block for node Postscript block. |
604 | 600 |
/// |
605 | 601 |
///\sa nodePsTexts() |
606 | 602 |
GraphToEps<T> & nodePsTextsPreamble(const char *str) { |
607 | 603 |
_nodePsTextsPreamble=str ;return *this; |
608 | 604 |
} |
609 | 605 |
///Sets whether the graph is undirected |
610 | 606 |
|
611 | 607 |
///Sets whether the graph is undirected. |
612 | 608 |
/// |
613 | 609 |
///This setting is the default for undirected graphs. |
614 | 610 |
/// |
615 | 611 |
///\sa directed() |
616 | 612 |
GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;} |
617 | 613 |
|
618 | 614 |
///Sets whether the graph is directed |
619 | 615 |
|
620 | 616 |
///Sets whether the graph is directed. |
621 | 617 |
///Use it to show the edges as a pair of directed ones. |
622 | 618 |
/// |
623 | 619 |
///This setting is the default for digraphs. |
624 | 620 |
/// |
625 | 621 |
///\sa undirected() |
626 | 622 |
GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;} |
627 | 623 |
|
628 | 624 |
///Sets the title. |
629 | 625 |
|
630 | 626 |
///Sets the title of the generated image, |
631 | 627 |
///namely it inserts a <tt>%%Title:</tt> DSC field to the header of |
632 | 628 |
///the EPS file. |
633 | 629 |
GraphToEps<T> &title(const std::string &t) {_title=t;return *this;} |
634 | 630 |
///Sets the copyright statement. |
635 | 631 |
|
636 | 632 |
///Sets the copyright statement of the generated image, |
637 | 633 |
///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of |
638 | 634 |
///the EPS file. |
639 | 635 |
GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;} |
640 | 636 |
|
641 | 637 |
protected: |
642 | 638 |
bool isInsideNode(dim2::Point<double> p, double r,int t) |
643 | 639 |
{ |
644 | 640 |
switch(t) { |
645 | 641 |
case CIRCLE: |
646 | 642 |
case MALE: |
647 | 643 |
case FEMALE: |
648 | 644 |
return p.normSquare()<=r*r; |
649 | 645 |
case SQUARE: |
650 | 646 |
return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r; |
651 | 647 |
case DIAMOND: |
652 | 648 |
return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r; |
653 | 649 |
} |
654 | 650 |
return false; |
655 | 651 |
} |
656 | 652 |
|
657 | 653 |
public: |
658 | 654 |
~GraphToEps() { } |
659 | 655 |
|
660 | 656 |
///Draws the graph. |
661 | 657 |
|
662 | 658 |
///Like other functions using |
663 | 659 |
///\ref named-templ-func-param "named template parameters", |
664 | 660 |
///this function calls the algorithm itself, i.e. in this case |
665 | 661 |
///it draws the graph. |
666 | 662 |
void run() { |
667 | 663 |
const double EPSILON=1e-9; |
668 | 664 |
if(dontPrint) return; |
669 | 665 |
|
670 | 666 |
_graph_to_eps_bits::_NegY<typename T::CoordsMapType> |
671 | 667 |
mycoords(_coords,_negY); |
672 | 668 |
|
673 | 669 |
os << "%!PS-Adobe-2.0 EPSF-2.0\n"; |
674 | 670 |
if(_title.size()>0) os << "%%Title: " << _title << '\n'; |
675 | 671 |
if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n'; |
676 | 672 |
os << "%%Creator: LEMON, graphToEps()\n"; |
677 | 673 |
|
678 | 674 |
{ |
679 | 675 |
os << "%%CreationDate: "; |
680 | 676 |
#ifndef WIN32 |
681 | 677 |
timeval tv; |
682 | 678 |
gettimeofday(&tv, 0); |
683 | 679 |
|
684 | 680 |
char cbuf[26]; |
685 | 681 |
ctime_r(&tv.tv_sec,cbuf); |
686 | 682 |
os << cbuf; |
687 | 683 |
#else |
688 | 684 |
os << bits::getWinFormattedDate(); |
689 | 685 |
#endif |
690 | 686 |
} |
691 | 687 |
os << std::endl; |
692 | 688 |
|
693 | 689 |
if (_autoArcWidthScale) { |
694 | 690 |
double max_w=0; |
695 | 691 |
for(ArcIt e(g);e!=INVALID;++e) |
696 | 692 |
max_w=std::max(double(_arcWidths[e]),max_w); |
697 | 693 |
if(max_w>EPSILON) { |
698 | 694 |
_arcWidthScale/=max_w; |
699 | 695 |
} |
700 | 696 |
} |
701 | 697 |
|
702 | 698 |
if (_autoNodeScale) { |
703 | 699 |
double max_s=0; |
704 | 700 |
for(NodeIt n(g);n!=INVALID;++n) |
705 | 701 |
max_s=std::max(double(_nodeSizes[n]),max_s); |
706 | 702 |
if(max_s>EPSILON) { |
707 | 703 |
_nodeScale/=max_s; |
708 | 704 |
} |
709 | 705 |
} |
710 | 706 |
|
711 | 707 |
double diag_len = 1; |
712 | 708 |
if(!(_absoluteNodeSizes&&_absoluteArcWidths)) { |
713 | 709 |
dim2::Box<double> bb; |
714 | 710 |
for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]); |
715 | 711 |
if (bb.empty()) { |
716 | 712 |
bb = dim2::Box<double>(dim2::Point<double>(0,0)); |
717 | 713 |
} |
718 | 714 |
diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare()); |
719 | 715 |
if(diag_len<EPSILON) diag_len = 1; |
720 | 716 |
if(!_absoluteNodeSizes) _nodeScale*=diag_len; |
721 | 717 |
if(!_absoluteArcWidths) _arcWidthScale*=diag_len; |
722 | 718 |
} |
723 | 719 |
|
724 | 720 |
dim2::Box<double> bb; |
725 | 721 |
for(NodeIt n(g);n!=INVALID;++n) { |
726 | 722 |
double ns=_nodeSizes[n]*_nodeScale; |
727 | 723 |
dim2::Point<double> p(ns,ns); |
728 | 724 |
switch(_nodeShapes[n]) { |
729 | 725 |
case CIRCLE: |
730 | 726 |
case SQUARE: |
731 | 727 |
case DIAMOND: |
732 | 728 |
bb.add(p+mycoords[n]); |
733 | 729 |
bb.add(-p+mycoords[n]); |
734 | 730 |
break; |
735 | 731 |
case MALE: |
736 | 732 |
bb.add(-p+mycoords[n]); |
737 | 733 |
bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]); |
738 | 734 |
break; |
739 | 735 |
case FEMALE: |
740 | 736 |
bb.add(p+mycoords[n]); |
741 | 737 |
bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]); |
742 | 738 |
break; |
743 | 739 |
} |
744 | 740 |
} |
745 | 741 |
if (bb.empty()) { |
746 | 742 |
bb = dim2::Box<double>(dim2::Point<double>(0,0)); |
747 | 743 |
} |
748 | 744 |
|
749 | 745 |
if(_scaleToA4) |
750 | 746 |
os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n"; |
751 | 747 |
else { |
752 | 748 |
if(_preScale) { |
753 | 749 |
//Rescale so that BoundingBox won't be neither to big nor too small. |
754 | 750 |
while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10; |
755 | 751 |
while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10; |
756 | 752 |
} |
757 | 753 |
|
758 | 754 |
os << "%%BoundingBox: " |
759 | 755 |
<< int(floor(bb.left() * _scale - _xBorder)) << ' ' |
760 | 756 |
<< int(floor(bb.bottom() * _scale - _yBorder)) << ' ' |
761 | 757 |
<< int(ceil(bb.right() * _scale + _xBorder)) << ' ' |
762 | 758 |
<< int(ceil(bb.top() * _scale + _yBorder)) << '\n'; |
763 | 759 |
} |
764 | 760 |
|
765 | 761 |
os << "%%EndComments\n"; |
766 | 762 |
|
767 | 763 |
//x1 y1 x2 y2 x3 y3 cr cg cb w |
768 | 764 |
os << "/lb { setlinewidth setrgbcolor newpath moveto\n" |
769 | 765 |
<< " 4 2 roll 1 index 1 index curveto stroke } bind def\n"; |
770 | 766 |
os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke }" |
771 | 767 |
<< " bind def\n"; |
772 | 768 |
//x y r |
773 | 769 |
os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath }" |
774 | 770 |
<< " bind def\n"; |
775 | 771 |
//x y r |
776 | 772 |
os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n" |
777 | 773 |
<< " 2 index 1 index sub 2 index 2 index add lineto\n" |
778 | 774 |
<< " 2 index 1 index sub 2 index 2 index sub lineto\n" |
779 | 775 |
<< " 2 index 1 index add 2 index 2 index sub lineto\n" |
780 | 776 |
<< " closepath pop pop pop} bind def\n"; |
781 | 777 |
//x y r |
782 | 778 |
os << "/di { newpath 2 index 1 index add 2 index moveto\n" |
783 | 779 |
<< " 2 index 2 index 2 index add lineto\n" |
784 | 780 |
<< " 2 index 1 index sub 2 index lineto\n" |
785 | 781 |
<< " 2 index 2 index 2 index sub lineto\n" |
786 | 782 |
<< " closepath pop pop pop} bind def\n"; |
787 | 783 |
// x y r cr cg cb |
788 | 784 |
os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n" |
789 | 785 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
790 | 786 |
<< " } bind def\n"; |
791 | 787 |
os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n" |
792 | 788 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n" |
793 | 789 |
<< " } bind def\n"; |
794 | 790 |
os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n" |
795 | 791 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n" |
796 | 792 |
<< " } bind def\n"; |
797 | 793 |
os << "/nfemale { 0 0 0 setrgbcolor 3 index " |
798 | 794 |
<< _nodeBorderQuotient/(1+_nodeBorderQuotient) |
799 | 795 |
<< " 1.5 mul mul setlinewidth\n" |
800 | 796 |
<< " newpath 5 index 5 index moveto " |
801 | 797 |
<< "5 index 5 index 5 index 3.01 mul sub\n" |
802 | 798 |
<< " lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub" |
803 | 799 |
<< " moveto\n" |
804 | 800 |
<< " 5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto " |
805 | 801 |
<< "stroke\n" |
806 | 802 |
<< " 5 index 5 index 5 index c fill\n" |
807 | 803 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
808 | 804 |
<< " } bind def\n"; |
809 | 805 |
os << "/nmale {\n" |
810 | 806 |
<< " 0 0 0 setrgbcolor 3 index " |
811 | 807 |
<< _nodeBorderQuotient/(1+_nodeBorderQuotient) |
812 | 808 |
<<" 1.5 mul mul setlinewidth\n" |
813 | 809 |
<< " newpath 5 index 5 index moveto\n" |
814 | 810 |
<< " 5 index 4 index 1 mul 1.5 mul add\n" |
815 | 811 |
<< " 5 index 5 index 3 sqrt 1.5 mul mul add\n" |
816 | 812 |
<< " 1 index 1 index lineto\n" |
817 | 813 |
<< " 1 index 1 index 7 index sub moveto\n" |
818 | 814 |
<< " 1 index 1 index lineto\n" |
819 | 815 |
<< " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub" |
820 | 816 |
<< " lineto\n" |
821 | 817 |
<< " stroke\n" |
822 | 818 |
<< " 5 index 5 index 5 index c fill\n" |
823 | 819 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
824 | 820 |
<< " } bind def\n"; |
825 | 821 |
|
826 | 822 |
|
827 | 823 |
os << "/arrl " << _arrowLength << " def\n"; |
828 | 824 |
os << "/arrw " << _arrowWidth << " def\n"; |
829 | 825 |
// l dx_norm dy_norm |
830 | 826 |
os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n"; |
831 | 827 |
//len w dx_norm dy_norm x1 y1 cr cg cb |
832 | 828 |
os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx " |
833 | 829 |
<< "exch def\n" |
834 | 830 |
<< " /w exch def /len exch def\n" |
835 | 831 |
//<< "0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke" |
836 | 832 |
<< " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n" |
837 | 833 |
<< " len w sub arrl sub dx dy lrl\n" |
838 | 834 |
<< " arrw dy dx neg lrl\n" |
839 | 835 |
<< " dx arrl w add mul dy w 2 div arrw add mul sub\n" |
840 | 836 |
<< " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n" |
841 | 837 |
<< " dx arrl w add mul neg dy w 2 div arrw add mul sub\n" |
842 | 838 |
<< " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n" |
843 | 839 |
<< " arrw dy dx neg lrl\n" |
844 | 840 |
<< " len w sub arrl sub neg dx dy lrl\n" |
845 | 841 |
<< " closepath fill } bind def\n"; |
846 | 842 |
os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n" |
847 | 843 |
<< " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n"; |
848 | 844 |
|
849 | 845 |
os << "\ngsave\n"; |
850 | 846 |
if(_scaleToA4) |
851 | 847 |
if(bb.height()>bb.width()) { |
852 | 848 |
double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(), |
853 | 849 |
(A4WIDTH-2*A4BORDER)/bb.width()); |
854 | 850 |
os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' ' |
855 | 851 |
<< ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER |
856 | 852 |
<< " translate\n" |
857 | 853 |
<< sc << " dup scale\n" |
858 | 854 |
<< -bb.left() << ' ' << -bb.bottom() << " translate\n"; |
859 | 855 |
} |
860 | 856 |
else { |
861 | 857 |
double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(), |
862 | 858 |
(A4WIDTH-2*A4BORDER)/bb.height()); |
863 | 859 |
os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' ' |
864 | 860 |
<< ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER |
865 | 861 |
<< " translate\n" |
866 | 862 |
<< sc << " dup scale\n90 rotate\n" |
867 | 863 |
<< -bb.left() << ' ' << -bb.top() << " translate\n"; |
868 | 864 |
} |
869 | 865 |
else if(_scale!=1.0) os << _scale << " dup scale\n"; |
870 | 866 |
|
871 | 867 |
if(_showArcs) { |
872 | 868 |
os << "%Arcs:\ngsave\n"; |
873 | 869 |
if(_enableParallel) { |
874 | 870 |
std::vector<Arc> el; |
875 | 871 |
for(ArcIt e(g);e!=INVALID;++e) |
876 | 872 |
if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0 |
877 | 873 |
&&g.source(e)!=g.target(e)) |
878 | 874 |
el.push_back(e); |
879 | 875 |
std::sort(el.begin(),el.end(),arcLess(g)); |
880 | 876 |
|
881 | 877 |
typename std::vector<Arc>::iterator j; |
882 | 878 |
for(typename std::vector<Arc>::iterator i=el.begin();i!=el.end();i=j) { |
883 | 879 |
for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ; |
884 | 880 |
|
885 | 881 |
double sw=0; |
886 | 882 |
for(typename std::vector<Arc>::iterator e=i;e!=j;++e) |
887 | 883 |
sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist; |
888 | 884 |
sw-=_parArcDist; |
889 | 885 |
sw/=-2.0; |
890 | 886 |
dim2::Point<double> |
891 | 887 |
dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]); |
892 | 888 |
double l=std::sqrt(dvec.normSquare()); |
893 | 889 |
dim2::Point<double> d(dvec/std::max(l,EPSILON)); |
894 | 890 |
dim2::Point<double> m; |
895 | 891 |
// m=dim2::Point<double>(mycoords[g.target(*i)]+ |
896 | 892 |
// mycoords[g.source(*i)])/2.0; |
897 | 893 |
|
898 | 894 |
// m=dim2::Point<double>(mycoords[g.source(*i)])+ |
899 | 895 |
// dvec*(double(_nodeSizes[g.source(*i)])/ |
900 | 896 |
// (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)])); |
901 | 897 |
|
902 | 898 |
m=dim2::Point<double>(mycoords[g.source(*i)])+ |
903 | 899 |
d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0; |
904 | 900 |
|
905 | 901 |
for(typename std::vector<Arc>::iterator e=i;e!=j;++e) { |
906 | 902 |
sw+=_arcWidths[*e]*_arcWidthScale/2.0; |
907 | 903 |
dim2::Point<double> mm=m+rot90(d)*sw/.75; |
908 | 904 |
if(_drawArrows) { |
909 | 905 |
int node_shape; |
910 | 906 |
dim2::Point<double> s=mycoords[g.source(*e)]; |
911 | 907 |
dim2::Point<double> t=mycoords[g.target(*e)]; |
912 | 908 |
double rn=_nodeSizes[g.target(*e)]*_nodeScale; |
913 | 909 |
node_shape=_nodeShapes[g.target(*e)]; |
914 | 910 |
dim2::Bezier3 bez(s,mm,mm,t); |
915 | 911 |
double t1=0,t2=1; |
916 | 912 |
for(int ii=0;ii<INTERPOL_PREC;++ii) |
917 | 913 |
if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2; |
918 | 914 |
else t1=(t1+t2)/2; |
919 | 915 |
dim2::Point<double> apoint=bez((t1+t2)/2); |
920 | 916 |
rn = _arrowLength+_arcWidths[*e]*_arcWidthScale; |
921 | 917 |
rn*=rn; |
922 | 918 |
t2=(t1+t2)/2;t1=0; |
923 | 919 |
for(int ii=0;ii<INTERPOL_PREC;++ii) |
924 | 920 |
if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2; |
925 | 921 |
else t2=(t1+t2)/2; |
926 | 922 |
dim2::Point<double> linend=bez((t1+t2)/2); |
927 | 923 |
bez=bez.before((t1+t2)/2); |
928 | 924 |
// rn=_nodeSizes[g.source(*e)]*_nodeScale; |
929 | 925 |
// node_shape=_nodeShapes[g.source(*e)]; |
930 | 926 |
// t1=0;t2=1; |
931 | 927 |
// for(int i=0;i<INTERPOL_PREC;++i) |
932 | 928 |
// if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) |
933 | 929 |
// t1=(t1+t2)/2; |
934 | 930 |
// else t2=(t1+t2)/2; |
935 | 931 |
// bez=bez.after((t1+t2)/2); |
936 | 932 |
os << _arcWidths[*e]*_arcWidthScale << " setlinewidth " |
937 | 933 |
<< _arcColors[*e].red() << ' ' |
938 | 934 |
<< _arcColors[*e].green() << ' ' |
939 | 935 |
<< _arcColors[*e].blue() << " setrgbcolor newpath\n" |
940 | 936 |
<< bez.p1.x << ' ' << bez.p1.y << " moveto\n" |
941 | 937 |
<< bez.p2.x << ' ' << bez.p2.y << ' ' |
942 | 938 |
<< bez.p3.x << ' ' << bez.p3.y << ' ' |
943 | 939 |
<< bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n"; |
944 | 940 |
dim2::Point<double> dd(rot90(linend-apoint)); |
945 | 941 |
dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/ |
946 | 942 |
std::sqrt(dd.normSquare()); |
947 | 943 |
os << "newpath " << psOut(apoint) << " moveto " |
948 | 944 |
<< psOut(linend+dd) << " lineto " |
949 | 945 |
<< psOut(linend-dd) << " lineto closepath fill\n"; |
950 | 946 |
} |
951 | 947 |
else { |
952 | 948 |
os << mycoords[g.source(*e)].x << ' ' |
953 | 949 |
<< mycoords[g.source(*e)].y << ' ' |
954 | 950 |
<< mm.x << ' ' << mm.y << ' ' |
955 | 951 |
<< mycoords[g.target(*e)].x << ' ' |
956 | 952 |
<< mycoords[g.target(*e)].y << ' ' |
957 | 953 |
<< _arcColors[*e].red() << ' ' |
958 | 954 |
<< _arcColors[*e].green() << ' ' |
959 | 955 |
<< _arcColors[*e].blue() << ' ' |
960 | 956 |
<< _arcWidths[*e]*_arcWidthScale << " lb\n"; |
961 | 957 |
} |
962 | 958 |
sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist; |
963 | 959 |
} |
964 | 960 |
} |
965 | 961 |
} |
966 | 962 |
else for(ArcIt e(g);e!=INVALID;++e) |
967 | 963 |
if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0 |
968 | 964 |
&&g.source(e)!=g.target(e)) { |
969 | 965 |
if(_drawArrows) { |
970 | 966 |
dim2::Point<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]); |
971 | 967 |
double rn=_nodeSizes[g.target(e)]*_nodeScale; |
972 | 968 |
int node_shape=_nodeShapes[g.target(e)]; |
973 | 969 |
double t1=0,t2=1; |
974 | 970 |
for(int i=0;i<INTERPOL_PREC;++i) |
975 | 971 |
if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2; |
976 | 972 |
else t2=(t1+t2)/2; |
977 | 973 |
double l=std::sqrt(d.normSquare()); |
978 | 974 |
d/=l; |
979 | 975 |
|
980 | 976 |
os << l*(1-(t1+t2)/2) << ' ' |
981 | 977 |
<< _arcWidths[e]*_arcWidthScale << ' ' |
982 | 978 |
<< d.x << ' ' << d.y << ' ' |
983 | 979 |
<< mycoords[g.source(e)].x << ' ' |
984 | 980 |
<< mycoords[g.source(e)].y << ' ' |
985 | 981 |
<< _arcColors[e].red() << ' ' |
986 | 982 |
<< _arcColors[e].green() << ' ' |
987 | 983 |
<< _arcColors[e].blue() << " arr\n"; |
988 | 984 |
} |
989 | 985 |
else os << mycoords[g.source(e)].x << ' ' |
990 | 986 |
<< mycoords[g.source(e)].y << ' ' |
991 | 987 |
<< mycoords[g.target(e)].x << ' ' |
992 | 988 |
<< mycoords[g.target(e)].y << ' ' |
993 | 989 |
<< _arcColors[e].red() << ' ' |
994 | 990 |
<< _arcColors[e].green() << ' ' |
995 | 991 |
<< _arcColors[e].blue() << ' ' |
996 | 992 |
<< _arcWidths[e]*_arcWidthScale << " l\n"; |
997 | 993 |
} |
998 | 994 |
os << "grestore\n"; |
999 | 995 |
} |
1000 | 996 |
if(_showNodes) { |
1001 | 997 |
os << "%Nodes:\ngsave\n"; |
1002 | 998 |
for(NodeIt n(g);n!=INVALID;++n) { |
1003 | 999 |
os << mycoords[n].x << ' ' << mycoords[n].y << ' ' |
1004 | 1000 |
<< _nodeSizes[n]*_nodeScale << ' ' |
1005 | 1001 |
<< _nodeColors[n].red() << ' ' |
1006 | 1002 |
<< _nodeColors[n].green() << ' ' |
1007 | 1003 |
<< _nodeColors[n].blue() << ' '; |
1008 | 1004 |
switch(_nodeShapes[n]) { |
1009 | 1005 |
case CIRCLE: |
1010 | 1006 |
os<< "nc";break; |
1011 | 1007 |
case SQUARE: |
1012 | 1008 |
os<< "nsq";break; |
1013 | 1009 |
case DIAMOND: |
1014 | 1010 |
os<< "ndi";break; |
1015 | 1011 |
case MALE: |
1016 | 1012 |
os<< "nmale";break; |
1017 | 1013 |
case FEMALE: |
1018 | 1014 |
os<< "nfemale";break; |
1019 | 1015 |
} |
1020 | 1016 |
os<<'\n'; |
1021 | 1017 |
} |
1022 | 1018 |
os << "grestore\n"; |
1023 | 1019 |
} |
1024 | 1020 |
if(_showNodeText) { |
1025 | 1021 |
os << "%Node texts:\ngsave\n"; |
1026 | 1022 |
os << "/fosi " << _nodeTextSize << " def\n"; |
1027 | 1023 |
os << "(Helvetica) findfont fosi scalefont setfont\n"; |
1028 | 1024 |
for(NodeIt n(g);n!=INVALID;++n) { |
1029 | 1025 |
switch(_nodeTextColorType) { |
1030 | 1026 |
case DIST_COL: |
1031 | 1027 |
os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n"; |
1032 | 1028 |
break; |
1033 | 1029 |
case DIST_BW: |
1034 | 1030 |
os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n"; |
1035 | 1031 |
break; |
1036 | 1032 |
case CUST_COL: |
1037 | 1033 |
os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n"; |
1038 | 1034 |
break; |
1039 | 1035 |
default: |
1040 | 1036 |
os << "0 0 0 setrgbcolor\n"; |
1041 | 1037 |
} |
1042 | 1038 |
os << mycoords[n].x << ' ' << mycoords[n].y |
1043 | 1039 |
<< " (" << _nodeTexts[n] << ") cshow\n"; |
1044 | 1040 |
} |
1045 | 1041 |
os << "grestore\n"; |
1046 | 1042 |
} |
1047 | 1043 |
if(_showNodePsText) { |
1048 | 1044 |
os << "%Node PS blocks:\ngsave\n"; |
1049 | 1045 |
for(NodeIt n(g);n!=INVALID;++n) |
1050 | 1046 |
os << mycoords[n].x << ' ' << mycoords[n].y |
1051 | 1047 |
<< " moveto\n" << _nodePsTexts[n] << "\n"; |
1052 | 1048 |
os << "grestore\n"; |
1053 | 1049 |
} |
1054 | 1050 |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_KRUSKAL_H |
20 | 20 |
#define LEMON_KRUSKAL_H |
21 | 21 |
|
22 | 22 |
#include <algorithm> |
23 | 23 |
#include <vector> |
24 | 24 |
#include <lemon/unionfind.h> |
25 | 25 |
#include <lemon/maps.h> |
26 | 26 |
|
27 | 27 |
#include <lemon/core.h> |
28 | 28 |
#include <lemon/bits/traits.h> |
29 | 29 |
|
30 | 30 |
///\ingroup spantree |
31 | 31 |
///\file |
32 | 32 |
///\brief Kruskal's algorithm to compute a minimum cost spanning tree |
33 | 33 |
/// |
34 | 34 |
///Kruskal's algorithm to compute a minimum cost spanning tree. |
35 | 35 |
/// |
36 | 36 |
|
37 | 37 |
namespace lemon { |
38 | 38 |
|
39 | 39 |
namespace _kruskal_bits { |
40 | 40 |
|
41 | 41 |
// Kruskal for directed graphs. |
42 | 42 |
|
43 | 43 |
template <typename Digraph, typename In, typename Out> |
44 | 44 |
typename disable_if<lemon::UndirectedTagIndicator<Digraph>, |
45 | 45 |
typename In::value_type::second_type >::type |
46 | 46 |
kruskal(const Digraph& digraph, const In& in, Out& out,dummy<0> = 0) { |
47 | 47 |
typedef typename In::value_type::second_type Value; |
48 | 48 |
typedef typename Digraph::template NodeMap<int> IndexMap; |
49 | 49 |
typedef typename Digraph::Node Node; |
50 | 50 |
|
51 | 51 |
IndexMap index(digraph); |
52 | 52 |
UnionFind<IndexMap> uf(index); |
53 | 53 |
for (typename Digraph::NodeIt it(digraph); it != INVALID; ++it) { |
54 | 54 |
uf.insert(it); |
55 | 55 |
} |
56 | 56 |
|
57 | 57 |
Value tree_value = 0; |
58 | 58 |
for (typename In::const_iterator it = in.begin(); it != in.end(); ++it) { |
59 | 59 |
if (uf.join(digraph.target(it->first),digraph.source(it->first))) { |
60 | 60 |
out.set(it->first, true); |
61 | 61 |
tree_value += it->second; |
62 | 62 |
} |
63 | 63 |
else { |
64 | 64 |
out.set(it->first, false); |
65 | 65 |
} |
66 | 66 |
} |
67 | 67 |
return tree_value; |
68 | 68 |
} |
69 | 69 |
|
70 | 70 |
// Kruskal for undirected graphs. |
71 | 71 |
|
72 | 72 |
template <typename Graph, typename In, typename Out> |
73 | 73 |
typename enable_if<lemon::UndirectedTagIndicator<Graph>, |
74 | 74 |
typename In::value_type::second_type >::type |
75 | 75 |
kruskal(const Graph& graph, const In& in, Out& out,dummy<1> = 1) { |
76 | 76 |
typedef typename In::value_type::second_type Value; |
77 | 77 |
typedef typename Graph::template NodeMap<int> IndexMap; |
78 | 78 |
typedef typename Graph::Node Node; |
79 | 79 |
|
80 | 80 |
IndexMap index(graph); |
81 | 81 |
UnionFind<IndexMap> uf(index); |
82 | 82 |
for (typename Graph::NodeIt it(graph); it != INVALID; ++it) { |
83 | 83 |
uf.insert(it); |
84 | 84 |
} |
85 | 85 |
|
86 | 86 |
Value tree_value = 0; |
87 | 87 |
for (typename In::const_iterator it = in.begin(); it != in.end(); ++it) { |
88 | 88 |
if (uf.join(graph.u(it->first),graph.v(it->first))) { |
89 | 89 |
out.set(it->first, true); |
90 | 90 |
tree_value += it->second; |
91 | 91 |
} |
92 | 92 |
else { |
93 | 93 |
out.set(it->first, false); |
94 | 94 |
} |
95 | 95 |
} |
96 | 96 |
return tree_value; |
97 | 97 |
} |
98 | 98 |
|
99 | 99 |
|
100 | 100 |
template <typename Sequence> |
101 | 101 |
struct PairComp { |
102 | 102 |
typedef typename Sequence::value_type Value; |
103 | 103 |
bool operator()(const Value& left, const Value& right) { |
104 | 104 |
return left.second < right.second; |
105 | 105 |
} |
106 | 106 |
}; |
107 | 107 |
|
108 | 108 |
template <typename In, typename Enable = void> |
109 | 109 |
struct SequenceInputIndicator { |
110 | 110 |
static const bool value = false; |
111 | 111 |
}; |
112 | 112 |
|
113 | 113 |
template <typename In> |
114 | 114 |
struct SequenceInputIndicator<In, |
115 | 115 |
typename exists<typename In::value_type::first_type>::type> { |
116 | 116 |
static const bool value = true; |
117 | 117 |
}; |
118 | 118 |
|
119 | 119 |
template <typename In, typename Enable = void> |
120 | 120 |
struct MapInputIndicator { |
121 | 121 |
static const bool value = false; |
122 | 122 |
}; |
123 | 123 |
|
124 | 124 |
template <typename In> |
125 | 125 |
struct MapInputIndicator<In, |
126 | 126 |
typename exists<typename In::Value>::type> { |
127 | 127 |
static const bool value = true; |
128 | 128 |
}; |
129 | 129 |
|
130 | 130 |
template <typename In, typename Enable = void> |
131 | 131 |
struct SequenceOutputIndicator { |
132 | 132 |
static const bool value = false; |
133 | 133 |
}; |
134 | 134 |
|
135 | 135 |
template <typename Out> |
136 | 136 |
struct SequenceOutputIndicator<Out, |
137 | 137 |
typename exists<typename Out::value_type>::type> { |
138 | 138 |
static const bool value = true; |
139 | 139 |
}; |
140 | 140 |
|
141 | 141 |
template <typename Out, typename Enable = void> |
142 | 142 |
struct MapOutputIndicator { |
143 | 143 |
static const bool value = false; |
144 | 144 |
}; |
145 | 145 |
|
146 | 146 |
template <typename Out> |
147 | 147 |
struct MapOutputIndicator<Out, |
148 | 148 |
typename exists<typename Out::Value>::type> { |
149 | 149 |
static const bool value = true; |
150 | 150 |
}; |
151 | 151 |
|
152 | 152 |
template <typename In, typename InEnable = void> |
153 | 153 |
struct KruskalValueSelector {}; |
154 | 154 |
|
155 | 155 |
template <typename In> |
156 | 156 |
struct KruskalValueSelector<In, |
157 | 157 |
typename enable_if<SequenceInputIndicator<In>, void>::type> |
158 | 158 |
{ |
159 | 159 |
typedef typename In::value_type::second_type Value; |
160 | 160 |
}; |
161 | 161 |
|
162 | 162 |
template <typename In> |
163 | 163 |
struct KruskalValueSelector<In, |
164 | 164 |
typename enable_if<MapInputIndicator<In>, void>::type> |
165 | 165 |
{ |
166 | 166 |
typedef typename In::Value Value; |
167 | 167 |
}; |
168 | 168 |
|
169 | 169 |
template <typename Graph, typename In, typename Out, |
170 | 170 |
typename InEnable = void> |
171 | 171 |
struct KruskalInputSelector {}; |
172 | 172 |
|
173 | 173 |
template <typename Graph, typename In, typename Out, |
174 | 174 |
typename InEnable = void> |
175 | 175 |
struct KruskalOutputSelector {}; |
176 | 176 |
|
177 | 177 |
template <typename Graph, typename In, typename Out> |
178 | 178 |
struct KruskalInputSelector<Graph, In, Out, |
179 | 179 |
typename enable_if<SequenceInputIndicator<In>, void>::type > |
180 | 180 |
{ |
181 | 181 |
typedef typename In::value_type::second_type Value; |
182 | 182 |
|
183 | 183 |
static Value kruskal(const Graph& graph, const In& in, Out& out) { |
184 | 184 |
return KruskalOutputSelector<Graph, In, Out>:: |
185 | 185 |
kruskal(graph, in, out); |
186 | 186 |
} |
187 | 187 |
|
188 | 188 |
}; |
189 | 189 |
|
190 | 190 |
template <typename Graph, typename In, typename Out> |
191 | 191 |
struct KruskalInputSelector<Graph, In, Out, |
192 | 192 |
typename enable_if<MapInputIndicator<In>, void>::type > |
193 | 193 |
{ |
194 | 194 |
typedef typename In::Value Value; |
195 | 195 |
static Value kruskal(const Graph& graph, const In& in, Out& out) { |
196 | 196 |
typedef typename In::Key MapArc; |
197 | 197 |
typedef typename In::Value Value; |
198 | 198 |
typedef typename ItemSetTraits<Graph, MapArc>::ItemIt MapArcIt; |
199 | 199 |
typedef std::vector<std::pair<MapArc, Value> > Sequence; |
200 | 200 |
Sequence seq; |
201 | 201 |
|
202 | 202 |
for (MapArcIt it(graph); it != INVALID; ++it) { |
203 | 203 |
seq.push_back(std::make_pair(it, in[it])); |
204 | 204 |
} |
205 | 205 |
|
206 | 206 |
std::sort(seq.begin(), seq.end(), PairComp<Sequence>()); |
207 | 207 |
return KruskalOutputSelector<Graph, Sequence, Out>:: |
208 | 208 |
kruskal(graph, seq, out); |
209 | 209 |
} |
210 | 210 |
}; |
211 | 211 |
|
212 | 212 |
template <typename T> |
213 | 213 |
struct RemoveConst { |
214 | 214 |
typedef T type; |
215 | 215 |
}; |
216 | 216 |
|
217 | 217 |
template <typename T> |
218 | 218 |
struct RemoveConst<const T> { |
219 | 219 |
typedef T type; |
220 | 220 |
}; |
221 | 221 |
|
222 | 222 |
template <typename Graph, typename In, typename Out> |
223 | 223 |
struct KruskalOutputSelector<Graph, In, Out, |
224 | 224 |
typename enable_if<SequenceOutputIndicator<Out>, void>::type > |
225 | 225 |
{ |
226 | 226 |
typedef typename In::value_type::second_type Value; |
227 | 227 |
|
228 | 228 |
static Value kruskal(const Graph& graph, const In& in, Out& out) { |
229 | 229 |
typedef LoggerBoolMap<typename RemoveConst<Out>::type> Map; |
230 | 230 |
Map map(out); |
231 | 231 |
return _kruskal_bits::kruskal(graph, in, map); |
232 | 232 |
} |
233 | 233 |
|
234 | 234 |
}; |
235 | 235 |
|
236 | 236 |
template <typename Graph, typename In, typename Out> |
237 | 237 |
struct KruskalOutputSelector<Graph, In, Out, |
238 | 238 |
typename enable_if<MapOutputIndicator<Out>, void>::type > |
239 | 239 |
{ |
240 | 240 |
typedef typename In::value_type::second_type Value; |
241 | 241 |
|
242 | 242 |
static Value kruskal(const Graph& graph, const In& in, Out& out) { |
243 | 243 |
return _kruskal_bits::kruskal(graph, in, out); |
244 | 244 |
} |
245 | 245 |
}; |
246 | 246 |
|
247 | 247 |
} |
248 | 248 |
|
249 | 249 |
/// \ingroup spantree |
250 | 250 |
/// |
251 |
/// \brief Kruskal algorithm |
|
251 |
/// \brief Kruskal's algorithm for finding a minimum cost spanning tree of |
|
252 | 252 |
/// a graph. |
253 | 253 |
/// |
254 | 254 |
/// This function runs Kruskal's algorithm to find a minimum cost |
255 |
/// spanning tree. |
|
255 |
/// spanning tree of a graph. |
|
256 | 256 |
/// Due to some C++ hacking, it accepts various input and output types. |
257 | 257 |
/// |
258 | 258 |
/// \param g The graph the algorithm runs on. |
259 | 259 |
/// It can be either \ref concepts::Digraph "directed" or |
260 | 260 |
/// \ref concepts::Graph "undirected". |
261 | 261 |
/// If the graph is directed, the algorithm consider it to be |
262 | 262 |
/// undirected by disregarding the direction of the arcs. |
263 | 263 |
/// |
264 | 264 |
/// \param in This object is used to describe the arc/edge costs. |
265 | 265 |
/// It can be one of the following choices. |
266 | 266 |
/// - An STL compatible 'Forward Container' with |
267 |
/// <tt>std::pair<GR::Arc,X></tt> or |
|
268 |
/// <tt>std::pair<GR::Edge,X></tt> as its <tt>value_type</tt>, where |
|
269 |
/// |
|
267 |
/// <tt>std::pair<GR::Arc,C></tt> or |
|
268 |
/// <tt>std::pair<GR::Edge,C></tt> as its <tt>value_type</tt>, where |
|
269 |
/// \c C is the type of the costs. The pairs indicates the arcs/edges |
|
270 | 270 |
/// along with the assigned cost. <em>They must be in a |
271 | 271 |
/// cost-ascending order.</em> |
272 | 272 |
/// - Any readable arc/edge map. The values of the map indicate the |
273 | 273 |
/// arc/edge costs. |
274 | 274 |
/// |
275 | 275 |
/// \retval out Here we also have a choice. |
276 |
/// - It can be a writable \c bool arc/edge map. After running the |
|
277 |
/// algorithm it will contain the found minimum cost spanning |
|
276 |
/// - It can be a writable arc/edge map with \c bool value type. After |
|
277 |
/// running the algorithm it will contain the found minimum cost spanning |
|
278 | 278 |
/// tree: the value of an arc/edge will be set to \c true if it belongs |
279 | 279 |
/// to the tree, otherwise it will be set to \c false. The value of |
280 | 280 |
/// each arc/edge will be set exactly once. |
281 | 281 |
/// - It can also be an iteraror of an STL Container with |
282 | 282 |
/// <tt>GR::Arc</tt> or <tt>GR::Edge</tt> as its |
283 | 283 |
/// <tt>value_type</tt>. The algorithm copies the elements of the |
284 | 284 |
/// found tree into this sequence. For example, if we know that the |
285 | 285 |
/// spanning tree of the graph \c g has say 53 arcs, then we can |
286 | 286 |
/// put its arcs into an STL vector \c tree with a code like this. |
287 | 287 |
///\code |
288 | 288 |
/// std::vector<Arc> tree(53); |
289 | 289 |
/// kruskal(g,cost,tree.begin()); |
290 | 290 |
///\endcode |
291 | 291 |
/// Or if we don't know in advance the size of the tree, we can |
292 | 292 |
/// write this. |
293 | 293 |
///\code |
294 | 294 |
/// std::vector<Arc> tree; |
295 | 295 |
/// kruskal(g,cost,std::back_inserter(tree)); |
296 | 296 |
///\endcode |
297 | 297 |
/// |
298 | 298 |
/// \return The total cost of the found spanning tree. |
299 | 299 |
/// |
300 | 300 |
/// \note If the input graph is not (weakly) connected, a spanning |
301 | 301 |
/// forest is calculated instead of a spanning tree. |
302 | 302 |
|
303 | 303 |
#ifdef DOXYGEN |
304 |
template <class Graph, class In, class Out> |
|
305 |
Value kruskal(GR const& g, const In& in, Out& out) |
|
304 |
template <typename Graph, typename In, typename Out> |
|
305 |
Value kruskal(const Graph& g, const In& in, Out& out) |
|
306 | 306 |
#else |
307 | 307 |
template <class Graph, class In, class Out> |
308 | 308 |
inline typename _kruskal_bits::KruskalValueSelector<In>::Value |
309 | 309 |
kruskal(const Graph& graph, const In& in, Out& out) |
310 | 310 |
#endif |
311 | 311 |
{ |
312 | 312 |
return _kruskal_bits::KruskalInputSelector<Graph, In, Out>:: |
313 | 313 |
kruskal(graph, in, out); |
314 | 314 |
} |
315 | 315 |
|
316 | 316 |
|
317 |
|
|
318 |
|
|
319 | 317 |
template <class Graph, class In, class Out> |
320 | 318 |
inline typename _kruskal_bits::KruskalValueSelector<In>::Value |
321 | 319 |
kruskal(const Graph& graph, const In& in, const Out& out) |
322 | 320 |
{ |
323 | 321 |
return _kruskal_bits::KruskalInputSelector<Graph, In, const Out>:: |
324 | 322 |
kruskal(graph, in, out); |
325 | 323 |
} |
326 | 324 |
|
327 | 325 |
} //namespace lemon |
328 | 326 |
|
329 | 327 |
#endif //LEMON_KRUSKAL_H |
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)