0
4
0
... | ... |
@@ -29,246 +29,272 @@ |
29 | 29 |
|
30 | 30 |
struct InvalidType {}; |
31 | 31 |
|
32 | 32 |
template <typename _Graph, typename _Item> |
33 | 33 |
class ItemSetTraits {}; |
34 | 34 |
|
35 | 35 |
|
36 | 36 |
template <typename Graph, typename Enable = void> |
37 | 37 |
struct NodeNotifierIndicator { |
38 | 38 |
typedef InvalidType Type; |
39 | 39 |
}; |
40 | 40 |
template <typename Graph> |
41 | 41 |
struct NodeNotifierIndicator< |
42 | 42 |
Graph, |
43 | 43 |
typename enable_if<typename Graph::NodeNotifier::Notifier, void>::type |
44 | 44 |
> { |
45 | 45 |
typedef typename Graph::NodeNotifier Type; |
46 | 46 |
}; |
47 | 47 |
|
48 | 48 |
template <typename _Graph> |
49 | 49 |
class ItemSetTraits<_Graph, typename _Graph::Node> { |
50 | 50 |
public: |
51 | 51 |
|
52 | 52 |
typedef _Graph Graph; |
53 | 53 |
|
54 | 54 |
typedef typename Graph::Node Item; |
55 | 55 |
typedef typename Graph::NodeIt ItemIt; |
56 | 56 |
|
57 | 57 |
typedef typename NodeNotifierIndicator<Graph>::Type ItemNotifier; |
58 | 58 |
|
59 | 59 |
template <typename _Value> |
60 | 60 |
class Map : public Graph::template NodeMap<_Value> { |
61 | 61 |
public: |
62 | 62 |
typedef typename Graph::template NodeMap<_Value> Parent; |
63 | 63 |
typedef typename Graph::template NodeMap<_Value> Type; |
64 | 64 |
typedef typename Parent::Value Value; |
65 | 65 |
|
66 | 66 |
Map(const Graph& _digraph) : Parent(_digraph) {} |
67 | 67 |
Map(const Graph& _digraph, const Value& _value) |
68 | 68 |
: Parent(_digraph, _value) {} |
69 | 69 |
|
70 | 70 |
}; |
71 | 71 |
|
72 | 72 |
}; |
73 | 73 |
|
74 | 74 |
template <typename Graph, typename Enable = void> |
75 | 75 |
struct ArcNotifierIndicator { |
76 | 76 |
typedef InvalidType Type; |
77 | 77 |
}; |
78 | 78 |
template <typename Graph> |
79 | 79 |
struct ArcNotifierIndicator< |
80 | 80 |
Graph, |
81 | 81 |
typename enable_if<typename Graph::ArcNotifier::Notifier, void>::type |
82 | 82 |
> { |
83 | 83 |
typedef typename Graph::ArcNotifier Type; |
84 | 84 |
}; |
85 | 85 |
|
86 | 86 |
template <typename _Graph> |
87 | 87 |
class ItemSetTraits<_Graph, typename _Graph::Arc> { |
88 | 88 |
public: |
89 | 89 |
|
90 | 90 |
typedef _Graph Graph; |
91 | 91 |
|
92 | 92 |
typedef typename Graph::Arc Item; |
93 | 93 |
typedef typename Graph::ArcIt ItemIt; |
94 | 94 |
|
95 | 95 |
typedef typename ArcNotifierIndicator<Graph>::Type ItemNotifier; |
96 | 96 |
|
97 | 97 |
template <typename _Value> |
98 | 98 |
class Map : public Graph::template ArcMap<_Value> { |
99 | 99 |
public: |
100 | 100 |
typedef typename Graph::template ArcMap<_Value> Parent; |
101 | 101 |
typedef typename Graph::template ArcMap<_Value> Type; |
102 | 102 |
typedef typename Parent::Value Value; |
103 | 103 |
|
104 | 104 |
Map(const Graph& _digraph) : Parent(_digraph) {} |
105 | 105 |
Map(const Graph& _digraph, const Value& _value) |
106 | 106 |
: Parent(_digraph, _value) {} |
107 | 107 |
}; |
108 | 108 |
|
109 | 109 |
}; |
110 | 110 |
|
111 | 111 |
template <typename Graph, typename Enable = void> |
112 | 112 |
struct EdgeNotifierIndicator { |
113 | 113 |
typedef InvalidType Type; |
114 | 114 |
}; |
115 | 115 |
template <typename Graph> |
116 | 116 |
struct EdgeNotifierIndicator< |
117 | 117 |
Graph, |
118 | 118 |
typename enable_if<typename Graph::EdgeNotifier::Notifier, void>::type |
119 | 119 |
> { |
120 | 120 |
typedef typename Graph::EdgeNotifier Type; |
121 | 121 |
}; |
122 | 122 |
|
123 | 123 |
template <typename _Graph> |
124 | 124 |
class ItemSetTraits<_Graph, typename _Graph::Edge> { |
125 | 125 |
public: |
126 | 126 |
|
127 | 127 |
typedef _Graph Graph; |
128 | 128 |
|
129 | 129 |
typedef typename Graph::Edge Item; |
130 | 130 |
typedef typename Graph::EdgeIt ItemIt; |
131 | 131 |
|
132 | 132 |
typedef typename EdgeNotifierIndicator<Graph>::Type ItemNotifier; |
133 | 133 |
|
134 | 134 |
template <typename _Value> |
135 | 135 |
class Map : public Graph::template EdgeMap<_Value> { |
136 | 136 |
public: |
137 | 137 |
typedef typename Graph::template EdgeMap<_Value> Parent; |
138 | 138 |
typedef typename Graph::template EdgeMap<_Value> Type; |
139 | 139 |
typedef typename Parent::Value Value; |
140 | 140 |
|
141 | 141 |
Map(const Graph& _digraph) : Parent(_digraph) {} |
142 | 142 |
Map(const Graph& _digraph, const Value& _value) |
143 | 143 |
: Parent(_digraph, _value) {} |
144 | 144 |
}; |
145 | 145 |
|
146 | 146 |
}; |
147 | 147 |
|
148 | 148 |
template <typename Map, typename Enable = void> |
149 | 149 |
struct MapTraits { |
150 | 150 |
typedef False ReferenceMapTag; |
151 | 151 |
|
152 | 152 |
typedef typename Map::Key Key; |
153 | 153 |
typedef typename Map::Value Value; |
154 | 154 |
|
155 | 155 |
typedef Value ConstReturnValue; |
156 | 156 |
typedef Value ReturnValue; |
157 | 157 |
}; |
158 | 158 |
|
159 | 159 |
template <typename Map> |
160 | 160 |
struct MapTraits< |
161 | 161 |
Map, typename enable_if<typename Map::ReferenceMapTag, void>::type > |
162 | 162 |
{ |
163 | 163 |
typedef True ReferenceMapTag; |
164 | 164 |
|
165 | 165 |
typedef typename Map::Key Key; |
166 | 166 |
typedef typename Map::Value Value; |
167 | 167 |
|
168 | 168 |
typedef typename Map::ConstReference ConstReturnValue; |
169 | 169 |
typedef typename Map::Reference ReturnValue; |
170 | 170 |
|
171 | 171 |
typedef typename Map::ConstReference ConstReference; |
172 | 172 |
typedef typename Map::Reference Reference; |
173 | 173 |
}; |
174 | 174 |
|
175 | 175 |
template <typename MatrixMap, typename Enable = void> |
176 | 176 |
struct MatrixMapTraits { |
177 | 177 |
typedef False ReferenceMapTag; |
178 | 178 |
|
179 | 179 |
typedef typename MatrixMap::FirstKey FirstKey; |
180 | 180 |
typedef typename MatrixMap::SecondKey SecondKey; |
181 | 181 |
typedef typename MatrixMap::Value Value; |
182 | 182 |
|
183 | 183 |
typedef Value ConstReturnValue; |
184 | 184 |
typedef Value ReturnValue; |
185 | 185 |
}; |
186 | 186 |
|
187 | 187 |
template <typename MatrixMap> |
188 | 188 |
struct MatrixMapTraits< |
189 | 189 |
MatrixMap, typename enable_if<typename MatrixMap::ReferenceMapTag, |
190 | 190 |
void>::type > |
191 | 191 |
{ |
192 | 192 |
typedef True ReferenceMapTag; |
193 | 193 |
|
194 | 194 |
typedef typename MatrixMap::FirstKey FirstKey; |
195 | 195 |
typedef typename MatrixMap::SecondKey SecondKey; |
196 | 196 |
typedef typename MatrixMap::Value Value; |
197 | 197 |
|
198 | 198 |
typedef typename MatrixMap::ConstReference ConstReturnValue; |
199 | 199 |
typedef typename MatrixMap::Reference ReturnValue; |
200 | 200 |
|
201 | 201 |
typedef typename MatrixMap::ConstReference ConstReference; |
202 | 202 |
typedef typename MatrixMap::Reference Reference; |
203 | 203 |
}; |
204 | 204 |
|
205 | 205 |
// Indicators for the tags |
206 | 206 |
|
207 | 207 |
template <typename Graph, typename Enable = void> |
208 | 208 |
struct NodeNumTagIndicator { |
209 | 209 |
static const bool value = false; |
210 | 210 |
}; |
211 | 211 |
|
212 | 212 |
template <typename Graph> |
213 | 213 |
struct NodeNumTagIndicator< |
214 | 214 |
Graph, |
215 | 215 |
typename enable_if<typename Graph::NodeNumTag, void>::type |
216 | 216 |
> { |
217 | 217 |
static const bool value = true; |
218 | 218 |
}; |
219 | 219 |
|
220 | 220 |
template <typename Graph, typename Enable = void> |
221 |
struct ArcNumTagIndicator { |
|
222 |
static const bool value = false; |
|
223 |
}; |
|
224 |
|
|
225 |
template <typename Graph> |
|
226 |
struct ArcNumTagIndicator< |
|
227 |
Graph, |
|
228 |
typename enable_if<typename Graph::ArcNumTag, void>::type |
|
229 |
> { |
|
230 |
static const bool value = true; |
|
231 |
}; |
|
232 |
|
|
233 |
template <typename Graph, typename Enable = void> |
|
221 | 234 |
struct EdgeNumTagIndicator { |
222 | 235 |
static const bool value = false; |
223 | 236 |
}; |
224 | 237 |
|
225 | 238 |
template <typename Graph> |
226 | 239 |
struct EdgeNumTagIndicator< |
227 | 240 |
Graph, |
228 | 241 |
typename enable_if<typename Graph::EdgeNumTag, void>::type |
229 | 242 |
> { |
230 | 243 |
static const bool value = true; |
231 | 244 |
}; |
232 | 245 |
|
233 | 246 |
template <typename Graph, typename Enable = void> |
247 |
struct FindArcTagIndicator { |
|
248 |
static const bool value = false; |
|
249 |
}; |
|
250 |
|
|
251 |
template <typename Graph> |
|
252 |
struct FindArcTagIndicator< |
|
253 |
Graph, |
|
254 |
typename enable_if<typename Graph::FindArcTag, void>::type |
|
255 |
> { |
|
256 |
static const bool value = true; |
|
257 |
}; |
|
258 |
|
|
259 |
template <typename Graph, typename Enable = void> |
|
234 | 260 |
struct FindEdgeTagIndicator { |
235 | 261 |
static const bool value = false; |
236 | 262 |
}; |
237 | 263 |
|
238 | 264 |
template <typename Graph> |
239 | 265 |
struct FindEdgeTagIndicator< |
240 | 266 |
Graph, |
241 | 267 |
typename enable_if<typename Graph::FindEdgeTag, void>::type |
242 | 268 |
> { |
243 | 269 |
static const bool value = true; |
244 | 270 |
}; |
245 | 271 |
|
246 | 272 |
template <typename Graph, typename Enable = void> |
247 | 273 |
struct UndirectedTagIndicator { |
248 | 274 |
static const bool value = false; |
249 | 275 |
}; |
250 | 276 |
|
251 | 277 |
template <typename Graph> |
252 | 278 |
struct UndirectedTagIndicator< |
253 | 279 |
Graph, |
254 | 280 |
typename enable_if<typename Graph::UndirectedTag, void>::type |
255 | 281 |
> { |
256 | 282 |
static const bool value = true; |
257 | 283 |
}; |
258 | 284 |
|
259 | 285 |
template <typename Graph, typename Enable = void> |
260 | 286 |
struct BuildTagIndicator { |
261 | 287 |
static const bool value = false; |
262 | 288 |
}; |
263 | 289 |
|
264 | 290 |
template <typename Graph> |
265 | 291 |
struct BuildTagIndicator< |
266 | 292 |
Graph, |
267 | 293 |
typename enable_if<typename Graph::BuildTag, void>::type |
268 | 294 |
> { |
269 | 295 |
static const bool value = true; |
270 | 296 |
}; |
271 | 297 |
|
272 | 298 |
} |
273 | 299 |
|
274 | 300 |
#endif |
... | ... |
@@ -117,421 +117,423 @@ |
117 | 117 |
} |
118 | 118 |
|
119 | 119 |
void first(Arc& arc) const { |
120 | 120 |
arc._id = _arc_num - 1; |
121 | 121 |
} |
122 | 122 |
|
123 | 123 |
static void next(Arc& arc) { |
124 | 124 |
--arc._id; |
125 | 125 |
} |
126 | 126 |
|
127 | 127 |
void firstOut(Arc& arc, const Node& node) const { |
128 | 128 |
arc._id = (node._id + 1) * _node_num - 1; |
129 | 129 |
} |
130 | 130 |
|
131 | 131 |
void nextOut(Arc& arc) const { |
132 | 132 |
if (arc._id % _node_num == 0) arc._id = 0; |
133 | 133 |
--arc._id; |
134 | 134 |
} |
135 | 135 |
|
136 | 136 |
void firstIn(Arc& arc, const Node& node) const { |
137 | 137 |
arc._id = _arc_num + node._id - _node_num; |
138 | 138 |
} |
139 | 139 |
|
140 | 140 |
void nextIn(Arc& arc) const { |
141 | 141 |
arc._id -= _node_num; |
142 | 142 |
if (arc._id < 0) arc._id = -1; |
143 | 143 |
} |
144 | 144 |
|
145 | 145 |
}; |
146 | 146 |
|
147 | 147 |
typedef DigraphExtender<FullDigraphBase> ExtendedFullDigraphBase; |
148 | 148 |
|
149 | 149 |
/// \ingroup graphs |
150 | 150 |
/// |
151 | 151 |
/// \brief A full digraph class. |
152 | 152 |
/// |
153 | 153 |
/// This is a simple and fast directed full graph implementation. |
154 | 154 |
/// From each node go arcs to each node (including the source node), |
155 | 155 |
/// therefore the number of the arcs in the digraph is the square of |
156 | 156 |
/// the node number. This digraph type is completely static, so you |
157 | 157 |
/// can neither add nor delete either arcs or nodes, and it needs |
158 | 158 |
/// constant space in memory. |
159 | 159 |
/// |
160 | 160 |
/// This class conforms to the \ref concepts::Digraph "Digraph" concept |
161 | 161 |
/// and it also has an important extra feature that its maps are |
162 | 162 |
/// real \ref concepts::ReferenceMap "reference map"s. |
163 | 163 |
/// |
164 | 164 |
/// The \c FullDigraph and \c FullGraph classes are very similar, |
165 | 165 |
/// but there are two differences. While this class conforms only |
166 | 166 |
/// to the \ref concepts::Digraph "Digraph" concept, the \c FullGraph |
167 | 167 |
/// class conforms to the \ref concepts::Graph "Graph" concept, |
168 | 168 |
/// moreover \c FullGraph does not contain a loop arc for each |
169 | 169 |
/// node as \c FullDigraph does. |
170 | 170 |
/// |
171 | 171 |
/// \sa FullGraph |
172 | 172 |
class FullDigraph : public ExtendedFullDigraphBase { |
173 | 173 |
public: |
174 | 174 |
|
175 | 175 |
typedef ExtendedFullDigraphBase Parent; |
176 | 176 |
|
177 | 177 |
/// \brief Constructor |
178 | 178 |
FullDigraph() { construct(0); } |
179 | 179 |
|
180 | 180 |
/// \brief Constructor |
181 | 181 |
/// |
182 | 182 |
/// Constructor. |
183 | 183 |
/// \param n The number of the nodes. |
184 | 184 |
FullDigraph(int n) { construct(n); } |
185 | 185 |
|
186 | 186 |
/// \brief Resizes the digraph |
187 | 187 |
/// |
188 | 188 |
/// Resizes the digraph. The function will fully destroy and |
189 | 189 |
/// rebuild the digraph. This cause that the maps of the digraph will |
190 | 190 |
/// reallocated automatically and the previous values will be lost. |
191 | 191 |
void resize(int n) { |
192 | 192 |
Parent::notifier(Arc()).clear(); |
193 | 193 |
Parent::notifier(Node()).clear(); |
194 | 194 |
construct(n); |
195 | 195 |
Parent::notifier(Node()).build(); |
196 | 196 |
Parent::notifier(Arc()).build(); |
197 | 197 |
} |
198 | 198 |
|
199 | 199 |
/// \brief Returns the node with the given index. |
200 | 200 |
/// |
201 | 201 |
/// Returns the node with the given index. Since it is a static |
202 | 202 |
/// digraph its nodes can be indexed with integers from the range |
203 | 203 |
/// <tt>[0..nodeNum()-1]</tt>. |
204 | 204 |
/// \sa index() |
205 | 205 |
Node operator()(int ix) const { return Parent::operator()(ix); } |
206 | 206 |
|
207 | 207 |
/// \brief Returns the index of the given node. |
208 | 208 |
/// |
209 | 209 |
/// Returns the index of the given node. Since it is a static |
210 | 210 |
/// digraph its nodes can be indexed with integers from the range |
211 | 211 |
/// <tt>[0..nodeNum()-1]</tt>. |
212 | 212 |
/// \sa operator() |
213 | 213 |
int index(const Node& node) const { return Parent::index(node); } |
214 | 214 |
|
215 | 215 |
/// \brief Returns the arc connecting the given nodes. |
216 | 216 |
/// |
217 | 217 |
/// Returns the arc connecting the given nodes. |
218 | 218 |
Arc arc(const Node& u, const Node& v) const { |
219 | 219 |
return Parent::arc(u, v); |
220 | 220 |
} |
221 | 221 |
|
222 | 222 |
/// \brief Number of nodes. |
223 | 223 |
int nodeNum() const { return Parent::nodeNum(); } |
224 | 224 |
/// \brief Number of arcs. |
225 | 225 |
int arcNum() const { return Parent::arcNum(); } |
226 | 226 |
}; |
227 | 227 |
|
228 | 228 |
|
229 | 229 |
class FullGraphBase { |
230 | 230 |
int _node_num; |
231 | 231 |
int _edge_num; |
232 | 232 |
public: |
233 | 233 |
|
234 | 234 |
typedef FullGraphBase Graph; |
235 | 235 |
|
236 | 236 |
class Node; |
237 | 237 |
class Arc; |
238 | 238 |
class Edge; |
239 | 239 |
|
240 | 240 |
protected: |
241 | 241 |
|
242 | 242 |
FullGraphBase() {} |
243 | 243 |
|
244 | 244 |
void construct(int n) { _node_num = n; _edge_num = n * (n - 1) / 2; } |
245 | 245 |
|
246 | 246 |
int _uid(int e) const { |
247 | 247 |
int u = e / _node_num; |
248 | 248 |
int v = e % _node_num; |
249 | 249 |
return u < v ? u : _node_num - 2 - u; |
250 | 250 |
} |
251 | 251 |
|
252 | 252 |
int _vid(int e) const { |
253 | 253 |
int u = e / _node_num; |
254 | 254 |
int v = e % _node_num; |
255 | 255 |
return u < v ? v : _node_num - 1 - v; |
256 | 256 |
} |
257 | 257 |
|
258 | 258 |
void _uvid(int e, int& u, int& v) const { |
259 | 259 |
u = e / _node_num; |
260 | 260 |
v = e % _node_num; |
261 | 261 |
if (u >= v) { |
262 | 262 |
u = _node_num - 2 - u; |
263 | 263 |
v = _node_num - 1 - v; |
264 | 264 |
} |
265 | 265 |
} |
266 | 266 |
|
267 | 267 |
void _stid(int a, int& s, int& t) const { |
268 | 268 |
if ((a & 1) == 1) { |
269 | 269 |
_uvid(a >> 1, s, t); |
270 | 270 |
} else { |
271 | 271 |
_uvid(a >> 1, t, s); |
272 | 272 |
} |
273 | 273 |
} |
274 | 274 |
|
275 | 275 |
int _eid(int u, int v) const { |
276 | 276 |
if (u < (_node_num - 1) / 2) { |
277 | 277 |
return u * _node_num + v; |
278 | 278 |
} else { |
279 | 279 |
return (_node_num - 1 - u) * _node_num - v - 1; |
280 | 280 |
} |
281 | 281 |
} |
282 | 282 |
|
283 | 283 |
public: |
284 | 284 |
|
285 | 285 |
Node operator()(int ix) const { return Node(ix); } |
286 | 286 |
int index(const Node& node) const { return node._id; } |
287 | 287 |
|
288 | 288 |
Edge edge(const Node& u, const Node& v) const { |
289 | 289 |
if (u._id < v._id) { |
290 | 290 |
return Edge(_eid(u._id, v._id)); |
291 | 291 |
} else if (u._id != v._id) { |
292 | 292 |
return Edge(_eid(v._id, u._id)); |
293 | 293 |
} else { |
294 | 294 |
return INVALID; |
295 | 295 |
} |
296 | 296 |
} |
297 | 297 |
|
298 | 298 |
Arc arc(const Node& s, const Node& t) const { |
299 | 299 |
if (s._id < t._id) { |
300 | 300 |
return Arc((_eid(s._id, t._id) << 1) | 1); |
301 | 301 |
} else if (s._id != t._id) { |
302 | 302 |
return Arc(_eid(t._id, s._id) << 1); |
303 | 303 |
} else { |
304 | 304 |
return INVALID; |
305 | 305 |
} |
306 | 306 |
} |
307 | 307 |
|
308 | 308 |
typedef True NodeNumTag; |
309 |
typedef True ArcNumTag; |
|
309 | 310 |
typedef True EdgeNumTag; |
310 | 311 |
|
311 | 312 |
int nodeNum() const { return _node_num; } |
312 | 313 |
int arcNum() const { return 2 * _edge_num; } |
313 | 314 |
int edgeNum() const { return _edge_num; } |
314 | 315 |
|
315 | 316 |
static int id(Node node) { return node._id; } |
316 | 317 |
static int id(Arc arc) { return arc._id; } |
317 | 318 |
static int id(Edge edge) { return edge._id; } |
318 | 319 |
|
319 | 320 |
int maxNodeId() const { return _node_num-1; } |
320 | 321 |
int maxArcId() const { return 2 * _edge_num-1; } |
321 | 322 |
int maxEdgeId() const { return _edge_num-1; } |
322 | 323 |
|
323 | 324 |
static Node nodeFromId(int id) { return Node(id);} |
324 | 325 |
static Arc arcFromId(int id) { return Arc(id);} |
325 | 326 |
static Edge edgeFromId(int id) { return Edge(id);} |
326 | 327 |
|
327 | 328 |
Node u(Edge edge) const { |
328 | 329 |
return Node(_uid(edge._id)); |
329 | 330 |
} |
330 | 331 |
|
331 | 332 |
Node v(Edge edge) const { |
332 | 333 |
return Node(_vid(edge._id)); |
333 | 334 |
} |
334 | 335 |
|
335 | 336 |
Node source(Arc arc) const { |
336 | 337 |
return Node((arc._id & 1) == 1 ? |
337 | 338 |
_uid(arc._id >> 1) : _vid(arc._id >> 1)); |
338 | 339 |
} |
339 | 340 |
|
340 | 341 |
Node target(Arc arc) const { |
341 | 342 |
return Node((arc._id & 1) == 1 ? |
342 | 343 |
_vid(arc._id >> 1) : _uid(arc._id >> 1)); |
343 | 344 |
} |
344 | 345 |
|
345 | 346 |
typedef True FindEdgeTag; |
347 |
typedef True FindArcTag; |
|
346 | 348 |
|
347 | 349 |
Edge findEdge(Node u, Node v, Edge prev = INVALID) const { |
348 | 350 |
return prev != INVALID ? INVALID : edge(u, v); |
349 | 351 |
} |
350 | 352 |
|
351 | 353 |
Arc findArc(Node s, Node t, Arc prev = INVALID) const { |
352 | 354 |
return prev != INVALID ? INVALID : arc(s, t); |
353 | 355 |
} |
354 | 356 |
|
355 | 357 |
class Node { |
356 | 358 |
friend class FullGraphBase; |
357 | 359 |
|
358 | 360 |
protected: |
359 | 361 |
int _id; |
360 | 362 |
Node(int id) : _id(id) {} |
361 | 363 |
public: |
362 | 364 |
Node() {} |
363 | 365 |
Node (Invalid) { _id = -1; } |
364 | 366 |
bool operator==(const Node node) const {return _id == node._id;} |
365 | 367 |
bool operator!=(const Node node) const {return _id != node._id;} |
366 | 368 |
bool operator<(const Node node) const {return _id < node._id;} |
367 | 369 |
}; |
368 | 370 |
|
369 | 371 |
class Edge { |
370 | 372 |
friend class FullGraphBase; |
371 | 373 |
friend class Arc; |
372 | 374 |
|
373 | 375 |
protected: |
374 | 376 |
int _id; |
375 | 377 |
|
376 | 378 |
Edge(int id) : _id(id) {} |
377 | 379 |
|
378 | 380 |
public: |
379 | 381 |
Edge() { } |
380 | 382 |
Edge (Invalid) { _id = -1; } |
381 | 383 |
|
382 | 384 |
bool operator==(const Edge edge) const {return _id == edge._id;} |
383 | 385 |
bool operator!=(const Edge edge) const {return _id != edge._id;} |
384 | 386 |
bool operator<(const Edge edge) const {return _id < edge._id;} |
385 | 387 |
}; |
386 | 388 |
|
387 | 389 |
class Arc { |
388 | 390 |
friend class FullGraphBase; |
389 | 391 |
|
390 | 392 |
protected: |
391 | 393 |
int _id; |
392 | 394 |
|
393 | 395 |
Arc(int id) : _id(id) {} |
394 | 396 |
|
395 | 397 |
public: |
396 | 398 |
Arc() { } |
397 | 399 |
Arc (Invalid) { _id = -1; } |
398 | 400 |
|
399 | 401 |
operator Edge() const { return Edge(_id != -1 ? (_id >> 1) : -1); } |
400 | 402 |
|
401 | 403 |
bool operator==(const Arc arc) const {return _id == arc._id;} |
402 | 404 |
bool operator!=(const Arc arc) const {return _id != arc._id;} |
403 | 405 |
bool operator<(const Arc arc) const {return _id < arc._id;} |
404 | 406 |
}; |
405 | 407 |
|
406 | 408 |
static bool direction(Arc arc) { |
407 | 409 |
return (arc._id & 1) == 1; |
408 | 410 |
} |
409 | 411 |
|
410 | 412 |
static Arc direct(Edge edge, bool dir) { |
411 | 413 |
return Arc((edge._id << 1) | (dir ? 1 : 0)); |
412 | 414 |
} |
413 | 415 |
|
414 | 416 |
void first(Node& node) const { |
415 | 417 |
node._id = _node_num - 1; |
416 | 418 |
} |
417 | 419 |
|
418 | 420 |
static void next(Node& node) { |
419 | 421 |
--node._id; |
420 | 422 |
} |
421 | 423 |
|
422 | 424 |
void first(Arc& arc) const { |
423 | 425 |
arc._id = (_edge_num << 1) - 1; |
424 | 426 |
} |
425 | 427 |
|
426 | 428 |
static void next(Arc& arc) { |
427 | 429 |
--arc._id; |
428 | 430 |
} |
429 | 431 |
|
430 | 432 |
void first(Edge& edge) const { |
431 | 433 |
edge._id = _edge_num - 1; |
432 | 434 |
} |
433 | 435 |
|
434 | 436 |
static void next(Edge& edge) { |
435 | 437 |
--edge._id; |
436 | 438 |
} |
437 | 439 |
|
438 | 440 |
void firstOut(Arc& arc, const Node& node) const { |
439 | 441 |
int s = node._id, t = _node_num - 1; |
440 | 442 |
if (s < t) { |
441 | 443 |
arc._id = (_eid(s, t) << 1) | 1; |
442 | 444 |
} else { |
443 | 445 |
--t; |
444 | 446 |
arc._id = (t != -1 ? (_eid(t, s) << 1) : -1); |
445 | 447 |
} |
446 | 448 |
} |
447 | 449 |
|
448 | 450 |
void nextOut(Arc& arc) const { |
449 | 451 |
int s, t; |
450 | 452 |
_stid(arc._id, s, t); |
451 | 453 |
--t; |
452 | 454 |
if (s < t) { |
453 | 455 |
arc._id = (_eid(s, t) << 1) | 1; |
454 | 456 |
} else { |
455 | 457 |
if (s == t) --t; |
456 | 458 |
arc._id = (t != -1 ? (_eid(t, s) << 1) : -1); |
457 | 459 |
} |
458 | 460 |
} |
459 | 461 |
|
460 | 462 |
void firstIn(Arc& arc, const Node& node) const { |
461 | 463 |
int s = _node_num - 1, t = node._id; |
462 | 464 |
if (s > t) { |
463 | 465 |
arc._id = (_eid(t, s) << 1); |
464 | 466 |
} else { |
465 | 467 |
--s; |
466 | 468 |
arc._id = (s != -1 ? (_eid(s, t) << 1) | 1 : -1); |
467 | 469 |
} |
468 | 470 |
} |
469 | 471 |
|
470 | 472 |
void nextIn(Arc& arc) const { |
471 | 473 |
int s, t; |
472 | 474 |
_stid(arc._id, s, t); |
473 | 475 |
--s; |
474 | 476 |
if (s > t) { |
475 | 477 |
arc._id = (_eid(t, s) << 1); |
476 | 478 |
} else { |
477 | 479 |
if (s == t) --s; |
478 | 480 |
arc._id = (s != -1 ? (_eid(s, t) << 1) | 1 : -1); |
479 | 481 |
} |
480 | 482 |
} |
481 | 483 |
|
482 | 484 |
void firstInc(Edge& edge, bool& dir, const Node& node) const { |
483 | 485 |
int u = node._id, v = _node_num - 1; |
484 | 486 |
if (u < v) { |
485 | 487 |
edge._id = _eid(u, v); |
486 | 488 |
dir = true; |
487 | 489 |
} else { |
488 | 490 |
--v; |
489 | 491 |
edge._id = (v != -1 ? _eid(v, u) : -1); |
490 | 492 |
dir = false; |
491 | 493 |
} |
492 | 494 |
} |
493 | 495 |
|
494 | 496 |
void nextInc(Edge& edge, bool& dir) const { |
495 | 497 |
int u, v; |
496 | 498 |
if (dir) { |
497 | 499 |
_uvid(edge._id, u, v); |
498 | 500 |
--v; |
499 | 501 |
if (u < v) { |
500 | 502 |
edge._id = _eid(u, v); |
501 | 503 |
} else { |
502 | 504 |
--v; |
503 | 505 |
edge._id = (v != -1 ? _eid(v, u) : -1); |
504 | 506 |
dir = false; |
505 | 507 |
} |
506 | 508 |
} else { |
507 | 509 |
_uvid(edge._id, v, u); |
508 | 510 |
--v; |
509 | 511 |
edge._id = (v != -1 ? _eid(v, u) : -1); |
510 | 512 |
} |
511 | 513 |
} |
512 | 514 |
|
513 | 515 |
}; |
514 | 516 |
|
515 | 517 |
typedef GraphExtender<FullGraphBase> ExtendedFullGraphBase; |
516 | 518 |
|
517 | 519 |
/// \ingroup graphs |
518 | 520 |
/// |
519 | 521 |
/// \brief An undirected full graph class. |
520 | 522 |
/// |
521 | 523 |
/// This is a simple and fast undirected full graph |
522 | 524 |
/// implementation. From each node go edge to each other node, |
523 | 525 |
/// therefore the number of edges in the graph is \f$n(n-1)/2\f$. |
524 | 526 |
/// This graph type is completely static, so you can neither |
525 | 527 |
/// add nor delete either edges or nodes, and it needs constant |
526 | 528 |
/// space in memory. |
527 | 529 |
/// |
528 | 530 |
/// This class conforms to the \ref concepts::Graph "Graph" concept |
529 | 531 |
/// and it also has an important extra feature that its maps are |
530 | 532 |
/// real \ref concepts::ReferenceMap "reference map"s. |
531 | 533 |
/// |
532 | 534 |
/// The \c FullGraph and \c FullDigraph classes are very similar, |
533 | 535 |
/// but there are two differences. While the \c FullDigraph class |
534 | 536 |
/// conforms only to the \ref concepts::Digraph "Digraph" concept, |
535 | 537 |
/// this class conforms to the \ref concepts::Graph "Graph" concept, |
536 | 538 |
/// moreover \c FullGraph does not contain a loop arc for each |
537 | 539 |
/// node as \c FullDigraph does. |
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-2008 |
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 GRID_GRAPH_H |
20 | 20 |
#define GRID_GRAPH_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
#include <lemon/bits/graph_extender.h> |
24 | 24 |
#include <lemon/dim2.h> |
25 | 25 |
#include <lemon/assert.h> |
26 | 26 |
|
27 | 27 |
///\ingroup graphs |
28 | 28 |
///\file |
29 | 29 |
///\brief GridGraph class. |
30 | 30 |
|
31 | 31 |
namespace lemon { |
32 | 32 |
|
33 | 33 |
class GridGraphBase { |
34 | 34 |
|
35 | 35 |
public: |
36 | 36 |
|
37 | 37 |
typedef GridGraphBase Graph; |
38 | 38 |
|
39 | 39 |
class Node; |
40 | 40 |
class Edge; |
41 | 41 |
class Arc; |
42 | 42 |
|
43 | 43 |
public: |
44 | 44 |
|
45 | 45 |
GridGraphBase() {} |
46 | 46 |
|
47 | 47 |
protected: |
48 | 48 |
|
49 | 49 |
void construct(int width, int height) { |
50 | 50 |
_width = width; _height = height; |
51 | 51 |
_node_num = width * height; |
52 | 52 |
_edge_num = 2 * _node_num - width - height; |
53 | 53 |
_edge_limit = _node_num - _width; |
54 | 54 |
} |
55 | 55 |
|
56 | 56 |
public: |
57 | 57 |
|
58 | 58 |
Node operator()(int i, int j) const { |
59 | 59 |
LEMON_DEBUG(0 <= i && i < _width && |
60 | 60 |
0 <= j && j < _height, "Index out of range"); |
61 | 61 |
return Node(i + j * _width); |
62 | 62 |
} |
63 | 63 |
|
64 | 64 |
int col(Node n) const { |
65 | 65 |
return n._id % _width; |
66 | 66 |
} |
67 | 67 |
|
68 | 68 |
int row(Node n) const { |
69 | 69 |
return n._id / _width; |
70 | 70 |
} |
71 | 71 |
|
72 | 72 |
dim2::Point<int> pos(Node n) const { |
73 | 73 |
return dim2::Point<int>(col(n), row(n)); |
74 | 74 |
} |
75 | 75 |
|
76 | 76 |
int width() const { |
77 | 77 |
return _width; |
78 | 78 |
} |
79 | 79 |
|
80 | 80 |
int height() const { |
81 | 81 |
return _height; |
82 | 82 |
} |
83 | 83 |
|
84 | 84 |
typedef True NodeNumTag; |
85 |
typedef True EdgeNumTag; |
|
85 | 86 |
typedef True ArcNumTag; |
86 | 87 |
|
87 | 88 |
int nodeNum() const { return _node_num; } |
88 | 89 |
int edgeNum() const { return _edge_num; } |
89 | 90 |
int arcNum() const { return 2 * _edge_num; } |
90 | 91 |
|
91 | 92 |
Node u(Edge edge) const { |
92 | 93 |
if (edge._id < _edge_limit) { |
93 | 94 |
return edge._id; |
94 | 95 |
} else { |
95 | 96 |
return (edge._id - _edge_limit) % (_width - 1) + |
96 | 97 |
(edge._id - _edge_limit) / (_width - 1) * _width; |
97 | 98 |
} |
98 | 99 |
} |
99 | 100 |
|
100 | 101 |
Node v(Edge edge) const { |
101 | 102 |
if (edge._id < _edge_limit) { |
102 | 103 |
return edge._id + _width; |
103 | 104 |
} else { |
104 | 105 |
return (edge._id - _edge_limit) % (_width - 1) + |
105 | 106 |
(edge._id - _edge_limit) / (_width - 1) * _width + 1; |
106 | 107 |
} |
107 | 108 |
} |
108 | 109 |
|
109 | 110 |
Node source(Arc arc) const { |
110 | 111 |
return (arc._id & 1) == 1 ? u(arc) : v(arc); |
111 | 112 |
} |
112 | 113 |
|
113 | 114 |
Node target(Arc arc) const { |
114 | 115 |
return (arc._id & 1) == 1 ? v(arc) : u(arc); |
115 | 116 |
} |
116 | 117 |
|
117 | 118 |
static int id(Node node) { return node._id; } |
118 | 119 |
static int id(Edge edge) { return edge._id; } |
119 | 120 |
static int id(Arc arc) { return arc._id; } |
120 | 121 |
|
121 | 122 |
int maxNodeId() const { return _node_num - 1; } |
122 | 123 |
int maxEdgeId() const { return _edge_num - 1; } |
123 | 124 |
int maxArcId() const { return 2 * _edge_num - 1; } |
124 | 125 |
|
125 | 126 |
static Node nodeFromId(int id) { return Node(id);} |
126 | 127 |
static Edge edgeFromId(int id) { return Edge(id);} |
127 | 128 |
static Arc arcFromId(int id) { return Arc(id);} |
128 | 129 |
|
129 | 130 |
typedef True FindEdgeTag; |
131 |
typedef True FindArcTag; |
|
130 | 132 |
|
131 | 133 |
Edge findEdge(Node u, Node v, Edge prev = INVALID) const { |
132 | 134 |
if (prev != INVALID) return INVALID; |
133 | 135 |
if (v._id > u._id) { |
134 | 136 |
if (v._id - u._id == _width) |
135 | 137 |
return Edge(u._id); |
136 | 138 |
if (v._id - u._id == 1 && u._id % _width < _width - 1) { |
137 | 139 |
return Edge(u._id / _width * (_width - 1) + |
138 | 140 |
u._id % _width + _edge_limit); |
139 | 141 |
} |
140 | 142 |
} else { |
141 | 143 |
if (u._id - v._id == _width) |
142 | 144 |
return Edge(v._id); |
143 | 145 |
if (u._id - v._id == 1 && v._id % _width < _width - 1) { |
144 | 146 |
return Edge(v._id / _width * (_width - 1) + |
145 | 147 |
v._id % _width + _edge_limit); |
146 | 148 |
} |
147 | 149 |
} |
148 | 150 |
return INVALID; |
149 | 151 |
} |
150 | 152 |
|
151 | 153 |
Arc findArc(Node u, Node v, Arc prev = INVALID) const { |
152 | 154 |
if (prev != INVALID) return INVALID; |
153 | 155 |
if (v._id > u._id) { |
154 | 156 |
if (v._id - u._id == _width) |
155 | 157 |
return Arc((u._id << 1) | 1); |
156 | 158 |
if (v._id - u._id == 1 && u._id % _width < _width - 1) { |
157 | 159 |
return Arc(((u._id / _width * (_width - 1) + |
158 | 160 |
u._id % _width + _edge_limit) << 1) | 1); |
159 | 161 |
} |
160 | 162 |
} else { |
161 | 163 |
if (u._id - v._id == _width) |
162 | 164 |
return Arc(v._id << 1); |
163 | 165 |
if (u._id - v._id == 1 && v._id % _width < _width - 1) { |
164 | 166 |
return Arc((v._id / _width * (_width - 1) + |
165 | 167 |
v._id % _width + _edge_limit) << 1); |
166 | 168 |
} |
167 | 169 |
} |
168 | 170 |
return INVALID; |
169 | 171 |
} |
170 | 172 |
|
171 | 173 |
class Node { |
172 | 174 |
friend class GridGraphBase; |
173 | 175 |
|
174 | 176 |
protected: |
175 | 177 |
int _id; |
176 | 178 |
Node(int id) : _id(id) {} |
177 | 179 |
public: |
178 | 180 |
Node() {} |
179 | 181 |
Node (Invalid) : _id(-1) {} |
180 | 182 |
bool operator==(const Node node) const {return _id == node._id;} |
181 | 183 |
bool operator!=(const Node node) const {return _id != node._id;} |
182 | 184 |
bool operator<(const Node node) const {return _id < node._id;} |
183 | 185 |
}; |
184 | 186 |
|
185 | 187 |
class Edge { |
186 | 188 |
friend class GridGraphBase; |
187 | 189 |
friend class Arc; |
188 | 190 |
|
189 | 191 |
protected: |
190 | 192 |
int _id; |
191 | 193 |
|
192 | 194 |
Edge(int id) : _id(id) {} |
193 | 195 |
|
194 | 196 |
public: |
195 | 197 |
Edge() {} |
196 | 198 |
Edge (Invalid) : _id(-1) {} |
197 | 199 |
bool operator==(const Edge edge) const {return _id == edge._id;} |
198 | 200 |
bool operator!=(const Edge edge) const {return _id != edge._id;} |
199 | 201 |
bool operator<(const Edge edge) const {return _id < edge._id;} |
200 | 202 |
}; |
201 | 203 |
|
202 | 204 |
class Arc { |
203 | 205 |
friend class GridGraphBase; |
204 | 206 |
|
205 | 207 |
protected: |
206 | 208 |
int _id; |
207 | 209 |
|
208 | 210 |
Arc(int id) : _id(id) {} |
209 | 211 |
|
210 | 212 |
public: |
211 | 213 |
Arc() {} |
212 | 214 |
Arc (Invalid) : _id(-1) {} |
213 | 215 |
operator Edge() const { return _id != -1 ? Edge(_id >> 1) : INVALID; } |
214 | 216 |
bool operator==(const Arc arc) const {return _id == arc._id;} |
215 | 217 |
bool operator!=(const Arc arc) const {return _id != arc._id;} |
216 | 218 |
bool operator<(const Arc arc) const {return _id < arc._id;} |
217 | 219 |
}; |
218 | 220 |
|
219 | 221 |
static bool direction(Arc arc) { |
220 | 222 |
return (arc._id & 1) == 1; |
221 | 223 |
} |
222 | 224 |
|
223 | 225 |
static Arc direct(Edge edge, bool dir) { |
224 | 226 |
return Arc((edge._id << 1) | (dir ? 1 : 0)); |
225 | 227 |
} |
226 | 228 |
|
227 | 229 |
void first(Node& node) const { |
228 | 230 |
node._id = _node_num - 1; |
229 | 231 |
} |
230 | 232 |
|
231 | 233 |
static void next(Node& node) { |
232 | 234 |
--node._id; |
233 | 235 |
} |
234 | 236 |
|
235 | 237 |
void first(Edge& edge) const { |
236 | 238 |
edge._id = _edge_num - 1; |
237 | 239 |
} |
238 | 240 |
|
239 | 241 |
static void next(Edge& edge) { |
240 | 242 |
--edge._id; |
241 | 243 |
} |
242 | 244 |
|
243 | 245 |
void first(Arc& arc) const { |
244 | 246 |
arc._id = 2 * _edge_num - 1; |
245 | 247 |
} |
246 | 248 |
|
247 | 249 |
static void next(Arc& arc) { |
248 | 250 |
--arc._id; |
249 | 251 |
} |
250 | 252 |
|
251 | 253 |
void firstOut(Arc& arc, const Node& node) const { |
252 | 254 |
if (node._id % _width < _width - 1) { |
253 | 255 |
arc._id = (_edge_limit + node._id % _width + |
254 | 256 |
(node._id / _width) * (_width - 1)) << 1 | 1; |
255 | 257 |
return; |
256 | 258 |
} |
257 | 259 |
if (node._id < _node_num - _width) { |
258 | 260 |
arc._id = node._id << 1 | 1; |
259 | 261 |
return; |
260 | 262 |
} |
261 | 263 |
if (node._id % _width > 0) { |
262 | 264 |
arc._id = (_edge_limit + node._id % _width + |
263 | 265 |
(node._id / _width) * (_width - 1) - 1) << 1; |
264 | 266 |
return; |
265 | 267 |
} |
266 | 268 |
if (node._id >= _width) { |
267 | 269 |
arc._id = (node._id - _width) << 1; |
268 | 270 |
return; |
269 | 271 |
} |
270 | 272 |
arc._id = -1; |
271 | 273 |
} |
272 | 274 |
|
273 | 275 |
void nextOut(Arc& arc) const { |
274 | 276 |
int nid = arc._id >> 1; |
275 | 277 |
if ((arc._id & 1) == 1) { |
276 | 278 |
if (nid >= _edge_limit) { |
277 | 279 |
nid = (nid - _edge_limit) % (_width - 1) + |
278 | 280 |
(nid - _edge_limit) / (_width - 1) * _width; |
279 | 281 |
if (nid < _node_num - _width) { |
280 | 282 |
arc._id = nid << 1 | 1; |
281 | 283 |
return; |
282 | 284 |
} |
283 | 285 |
} |
284 | 286 |
if (nid % _width > 0) { |
285 | 287 |
arc._id = (_edge_limit + nid % _width + |
286 | 288 |
(nid / _width) * (_width - 1) - 1) << 1; |
287 | 289 |
return; |
288 | 290 |
} |
289 | 291 |
if (nid >= _width) { |
290 | 292 |
arc._id = (nid - _width) << 1; |
291 | 293 |
return; |
292 | 294 |
} |
293 | 295 |
} else { |
294 | 296 |
if (nid >= _edge_limit) { |
295 | 297 |
nid = (nid - _edge_limit) % (_width - 1) + |
296 | 298 |
(nid - _edge_limit) / (_width - 1) * _width + 1; |
297 | 299 |
if (nid >= _width) { |
298 | 300 |
arc._id = (nid - _width) << 1; |
299 | 301 |
return; |
300 | 302 |
} |
301 | 303 |
} |
302 | 304 |
} |
303 | 305 |
arc._id = -1; |
304 | 306 |
} |
305 | 307 |
|
306 | 308 |
void firstIn(Arc& arc, const Node& node) const { |
307 | 309 |
if (node._id % _width < _width - 1) { |
308 | 310 |
arc._id = (_edge_limit + node._id % _width + |
309 | 311 |
(node._id / _width) * (_width - 1)) << 1; |
310 | 312 |
return; |
311 | 313 |
} |
312 | 314 |
if (node._id < _node_num - _width) { |
313 | 315 |
arc._id = node._id << 1; |
314 | 316 |
return; |
315 | 317 |
} |
316 | 318 |
if (node._id % _width > 0) { |
317 | 319 |
arc._id = (_edge_limit + node._id % _width + |
318 | 320 |
(node._id / _width) * (_width - 1) - 1) << 1 | 1; |
319 | 321 |
return; |
320 | 322 |
} |
321 | 323 |
if (node._id >= _width) { |
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-2008 |
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_SMART_GRAPH_H |
20 | 20 |
#define LEMON_SMART_GRAPH_H |
21 | 21 |
|
22 | 22 |
///\ingroup graphs |
23 | 23 |
///\file |
24 | 24 |
///\brief SmartDigraph and SmartGraph classes. |
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
|
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/error.h> |
30 | 30 |
#include <lemon/bits/graph_extender.h> |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
class SmartDigraph; |
35 | 35 |
///Base of SmartDigraph |
36 | 36 |
|
37 | 37 |
///Base of SmartDigraph |
38 | 38 |
/// |
39 | 39 |
class SmartDigraphBase { |
40 | 40 |
protected: |
41 | 41 |
|
42 | 42 |
struct NodeT |
43 | 43 |
{ |
44 | 44 |
int first_in, first_out; |
45 | 45 |
NodeT() {} |
46 | 46 |
}; |
47 | 47 |
struct ArcT |
48 | 48 |
{ |
49 | 49 |
int target, source, next_in, next_out; |
50 | 50 |
ArcT() {} |
51 | 51 |
}; |
52 | 52 |
|
53 | 53 |
std::vector<NodeT> nodes; |
54 | 54 |
std::vector<ArcT> arcs; |
55 | 55 |
|
56 | 56 |
public: |
57 | 57 |
|
58 | 58 |
typedef SmartDigraphBase Graph; |
59 | 59 |
|
60 | 60 |
class Node; |
61 | 61 |
class Arc; |
62 | 62 |
|
63 | 63 |
public: |
64 | 64 |
|
65 | 65 |
SmartDigraphBase() : nodes(), arcs() { } |
66 | 66 |
SmartDigraphBase(const SmartDigraphBase &_g) |
67 | 67 |
: nodes(_g.nodes), arcs(_g.arcs) { } |
68 | 68 |
|
69 | 69 |
typedef True NodeNumTag; |
70 |
typedef True |
|
70 |
typedef True ArcNumTag; |
|
71 | 71 |
|
72 | 72 |
int nodeNum() const { return nodes.size(); } |
73 | 73 |
int arcNum() const { return arcs.size(); } |
74 | 74 |
|
75 | 75 |
int maxNodeId() const { return nodes.size()-1; } |
76 | 76 |
int maxArcId() const { return arcs.size()-1; } |
77 | 77 |
|
78 | 78 |
Node addNode() { |
79 | 79 |
int n = nodes.size(); |
80 | 80 |
nodes.push_back(NodeT()); |
81 | 81 |
nodes[n].first_in = -1; |
82 | 82 |
nodes[n].first_out = -1; |
83 | 83 |
return Node(n); |
84 | 84 |
} |
85 | 85 |
|
86 | 86 |
Arc addArc(Node u, Node v) { |
87 | 87 |
int n = arcs.size(); |
88 | 88 |
arcs.push_back(ArcT()); |
89 | 89 |
arcs[n].source = u._id; |
90 | 90 |
arcs[n].target = v._id; |
91 | 91 |
arcs[n].next_out = nodes[u._id].first_out; |
92 | 92 |
arcs[n].next_in = nodes[v._id].first_in; |
93 | 93 |
nodes[u._id].first_out = nodes[v._id].first_in = n; |
94 | 94 |
|
95 | 95 |
return Arc(n); |
96 | 96 |
} |
97 | 97 |
|
98 | 98 |
void clear() { |
99 | 99 |
arcs.clear(); |
100 | 100 |
nodes.clear(); |
101 | 101 |
} |
102 | 102 |
|
103 | 103 |
Node source(Arc a) const { return Node(arcs[a._id].source); } |
104 | 104 |
Node target(Arc a) const { return Node(arcs[a._id].target); } |
105 | 105 |
|
106 | 106 |
static int id(Node v) { return v._id; } |
107 | 107 |
static int id(Arc a) { return a._id; } |
108 | 108 |
|
109 | 109 |
static Node nodeFromId(int id) { return Node(id);} |
110 | 110 |
static Arc arcFromId(int id) { return Arc(id);} |
111 | 111 |
|
112 | 112 |
bool valid(Node n) const { |
113 | 113 |
return n._id >= 0 && n._id < static_cast<int>(nodes.size()); |
114 | 114 |
} |
115 | 115 |
bool valid(Arc a) const { |
116 | 116 |
return a._id >= 0 && a._id < static_cast<int>(arcs.size()); |
117 | 117 |
} |
118 | 118 |
|
119 | 119 |
class Node { |
120 | 120 |
friend class SmartDigraphBase; |
121 | 121 |
friend class SmartDigraph; |
122 | 122 |
|
123 | 123 |
protected: |
124 | 124 |
int _id; |
125 | 125 |
explicit Node(int id) : _id(id) {} |
126 | 126 |
public: |
127 | 127 |
Node() {} |
128 | 128 |
Node (Invalid) : _id(-1) {} |
129 | 129 |
bool operator==(const Node i) const {return _id == i._id;} |
130 | 130 |
bool operator!=(const Node i) const {return _id != i._id;} |
131 | 131 |
bool operator<(const Node i) const {return _id < i._id;} |
132 | 132 |
}; |
133 | 133 |
|
134 | 134 |
|
135 | 135 |
class Arc { |
136 | 136 |
friend class SmartDigraphBase; |
137 | 137 |
friend class SmartDigraph; |
138 | 138 |
|
139 | 139 |
protected: |
140 | 140 |
int _id; |
141 | 141 |
explicit Arc(int id) : _id(id) {} |
142 | 142 |
public: |
143 | 143 |
Arc() { } |
144 | 144 |
Arc (Invalid) : _id(-1) {} |
145 | 145 |
bool operator==(const Arc i) const {return _id == i._id;} |
146 | 146 |
bool operator!=(const Arc i) const {return _id != i._id;} |
147 | 147 |
bool operator<(const Arc i) const {return _id < i._id;} |
148 | 148 |
}; |
149 | 149 |
|
150 | 150 |
void first(Node& node) const { |
151 | 151 |
node._id = nodes.size() - 1; |
152 | 152 |
} |
153 | 153 |
|
154 | 154 |
static void next(Node& node) { |
155 | 155 |
--node._id; |
156 | 156 |
} |
157 | 157 |
|
158 | 158 |
void first(Arc& arc) const { |
159 | 159 |
arc._id = arcs.size() - 1; |
160 | 160 |
} |
161 | 161 |
|
162 | 162 |
static void next(Arc& arc) { |
163 | 163 |
--arc._id; |
164 | 164 |
} |
165 | 165 |
|
166 | 166 |
void firstOut(Arc& arc, const Node& node) const { |
167 | 167 |
arc._id = nodes[node._id].first_out; |
168 | 168 |
} |
169 | 169 |
|
170 | 170 |
void nextOut(Arc& arc) const { |
171 | 171 |
arc._id = arcs[arc._id].next_out; |
172 | 172 |
} |
173 | 173 |
|
174 | 174 |
void firstIn(Arc& arc, const Node& node) const { |
175 | 175 |
arc._id = nodes[node._id].first_in; |
176 | 176 |
} |
177 | 177 |
|
178 | 178 |
void nextIn(Arc& arc) const { |
179 | 179 |
arc._id = arcs[arc._id].next_in; |
180 | 180 |
} |
181 | 181 |
|
182 | 182 |
}; |
183 | 183 |
|
184 | 184 |
typedef DigraphExtender<SmartDigraphBase> ExtendedSmartDigraphBase; |
185 | 185 |
|
186 | 186 |
///\ingroup graphs |
187 | 187 |
/// |
188 | 188 |
///\brief A smart directed graph class. |
189 | 189 |
/// |
190 | 190 |
///This is a simple and fast digraph implementation. |
191 | 191 |
///It is also quite memory efficient, but at the price |
192 | 192 |
///that <b> it does support only limited (only stack-like) |
193 | 193 |
///node and arc deletions</b>. |
194 | 194 |
///It conforms to the \ref concepts::Digraph "Digraph concept" with |
195 | 195 |
///an important extra feature that its maps are real \ref |
196 | 196 |
///concepts::ReferenceMap "reference map"s. |
197 | 197 |
/// |
198 | 198 |
///\sa concepts::Digraph. |
199 | 199 |
class SmartDigraph : public ExtendedSmartDigraphBase { |
200 | 200 |
public: |
201 | 201 |
|
202 | 202 |
typedef ExtendedSmartDigraphBase Parent; |
203 | 203 |
|
204 | 204 |
private: |
205 | 205 |
|
206 | 206 |
///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead. |
207 | 207 |
|
208 | 208 |
///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead. |
209 | 209 |
/// |
210 | 210 |
SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {}; |
211 | 211 |
///\brief Assignment of SmartDigraph to another one is \e not allowed. |
212 | 212 |
///Use DigraphCopy() instead. |
213 | 213 |
|
214 | 214 |
///Assignment of SmartDigraph to another one is \e not allowed. |
215 | 215 |
///Use DigraphCopy() instead. |
216 | 216 |
void operator=(const SmartDigraph &) {} |
217 | 217 |
|
218 | 218 |
public: |
219 | 219 |
|
220 | 220 |
/// Constructor |
221 | 221 |
|
222 | 222 |
/// Constructor. |
223 | 223 |
/// |
224 | 224 |
SmartDigraph() {}; |
225 | 225 |
|
226 | 226 |
///Add a new node to the digraph. |
227 | 227 |
|
228 | 228 |
/// \return the new node. |
229 | 229 |
/// |
230 | 230 |
Node addNode() { return Parent::addNode(); } |
231 | 231 |
|
232 | 232 |
///Add a new arc to the digraph. |
233 | 233 |
|
234 | 234 |
///Add a new arc to the digraph with source node \c s |
235 | 235 |
///and target node \c t. |
236 | 236 |
///\return the new arc. |
237 | 237 |
Arc addArc(const Node& s, const Node& t) { |
238 | 238 |
return Parent::addArc(s, t); |
239 | 239 |
} |
240 | 240 |
|
241 | 241 |
/// \brief Using this it is possible to avoid the superfluous memory |
242 | 242 |
/// allocation. |
243 | 243 |
|
244 | 244 |
/// Using this it is possible to avoid the superfluous memory |
245 | 245 |
/// allocation: if you know that the digraph you want to build will |
246 | 246 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
247 | 247 |
/// then it is worth reserving space for this amount before starting |
248 | 248 |
/// to build the digraph. |
249 | 249 |
/// \sa reserveArc |
250 | 250 |
void reserveNode(int n) { nodes.reserve(n); }; |
251 | 251 |
|
252 | 252 |
/// \brief Using this it is possible to avoid the superfluous memory |
253 | 253 |
/// allocation. |
254 | 254 |
|
255 | 255 |
/// Using this it is possible to avoid the superfluous memory |
256 | 256 |
/// allocation: if you know that the digraph you want to build will |
257 | 257 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
258 | 258 |
/// then it is worth reserving space for this amount before starting |
259 | 259 |
/// to build the digraph. |
260 | 260 |
/// \sa reserveNode |
261 | 261 |
void reserveArc(int m) { arcs.reserve(m); }; |
262 | 262 |
|
0 comments (0 inline)