0
7
0
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_ADAPTORS_H |
20 | 20 |
#define LEMON_ADAPTORS_H |
21 | 21 |
|
22 | 22 |
/// \ingroup graph_adaptors |
23 | 23 |
/// \file |
24 | 24 |
/// \brief Adaptor classes for digraphs and graphs |
25 | 25 |
/// |
26 | 26 |
/// This file contains several useful adaptors for digraphs and graphs. |
27 | 27 |
|
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/maps.h> |
30 | 30 |
#include <lemon/bits/variant.h> |
31 | 31 |
|
32 | 32 |
#include <lemon/bits/graph_adaptor_extender.h> |
33 |
#include <lemon/bits/map_extender.h> |
|
33 | 34 |
#include <lemon/tolerance.h> |
34 | 35 |
|
35 | 36 |
#include <algorithm> |
36 | 37 |
|
37 | 38 |
namespace lemon { |
38 | 39 |
|
39 | 40 |
#ifdef _MSC_VER |
40 | 41 |
#define LEMON_SCOPE_FIX(OUTER, NESTED) OUTER::NESTED |
41 | 42 |
#else |
42 | 43 |
#define LEMON_SCOPE_FIX(OUTER, NESTED) typename OUTER::template NESTED |
43 | 44 |
#endif |
44 | 45 |
|
45 | 46 |
template<typename DGR> |
46 | 47 |
class DigraphAdaptorBase { |
47 | 48 |
public: |
48 | 49 |
typedef DGR Digraph; |
49 | 50 |
typedef DigraphAdaptorBase Adaptor; |
50 | 51 |
|
51 | 52 |
protected: |
52 | 53 |
DGR* _digraph; |
53 | 54 |
DigraphAdaptorBase() : _digraph(0) { } |
54 | 55 |
void initialize(DGR& digraph) { _digraph = &digraph; } |
55 | 56 |
|
56 | 57 |
public: |
57 | 58 |
DigraphAdaptorBase(DGR& digraph) : _digraph(&digraph) { } |
58 | 59 |
|
59 | 60 |
typedef typename DGR::Node Node; |
60 | 61 |
typedef typename DGR::Arc Arc; |
61 | 62 |
|
62 | 63 |
void first(Node& i) const { _digraph->first(i); } |
63 | 64 |
void first(Arc& i) const { _digraph->first(i); } |
64 | 65 |
void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); } |
65 | 66 |
void firstOut(Arc& i, const Node& n ) const { _digraph->firstOut(i, n); } |
66 | 67 |
|
67 | 68 |
void next(Node& i) const { _digraph->next(i); } |
68 | 69 |
void next(Arc& i) const { _digraph->next(i); } |
69 | 70 |
void nextIn(Arc& i) const { _digraph->nextIn(i); } |
70 | 71 |
void nextOut(Arc& i) const { _digraph->nextOut(i); } |
71 | 72 |
|
72 | 73 |
Node source(const Arc& a) const { return _digraph->source(a); } |
73 | 74 |
Node target(const Arc& a) const { return _digraph->target(a); } |
74 | 75 |
|
75 | 76 |
typedef NodeNumTagIndicator<DGR> NodeNumTag; |
76 | 77 |
int nodeNum() const { return _digraph->nodeNum(); } |
77 | 78 |
|
78 | 79 |
typedef ArcNumTagIndicator<DGR> ArcNumTag; |
79 | 80 |
int arcNum() const { return _digraph->arcNum(); } |
80 | 81 |
|
81 | 82 |
typedef FindArcTagIndicator<DGR> FindArcTag; |
82 | 83 |
Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) const { |
83 | 84 |
return _digraph->findArc(u, v, prev); |
84 | 85 |
} |
85 | 86 |
|
86 | 87 |
Node addNode() { return _digraph->addNode(); } |
87 | 88 |
Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); } |
88 | 89 |
|
89 | 90 |
void erase(const Node& n) { _digraph->erase(n); } |
90 | 91 |
void erase(const Arc& a) { _digraph->erase(a); } |
91 | 92 |
|
92 | 93 |
void clear() { _digraph->clear(); } |
93 | 94 |
|
94 | 95 |
int id(const Node& n) const { return _digraph->id(n); } |
95 | 96 |
int id(const Arc& a) const { return _digraph->id(a); } |
96 | 97 |
|
97 | 98 |
Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); } |
98 | 99 |
Arc arcFromId(int ix) const { return _digraph->arcFromId(ix); } |
99 | 100 |
|
100 | 101 |
int maxNodeId() const { return _digraph->maxNodeId(); } |
101 | 102 |
int maxArcId() const { return _digraph->maxArcId(); } |
102 | 103 |
|
103 | 104 |
typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier; |
104 | 105 |
NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } |
105 | 106 |
|
106 | 107 |
typedef typename ItemSetTraits<DGR, Arc>::ItemNotifier ArcNotifier; |
107 | 108 |
ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); } |
108 | 109 |
|
109 | 110 |
template <typename V> |
110 | 111 |
class NodeMap : public DGR::template NodeMap<V> { |
111 | 112 |
public: |
112 | 113 |
|
113 | 114 |
typedef typename DGR::template NodeMap<V> Parent; |
114 | 115 |
|
115 | 116 |
explicit NodeMap(const Adaptor& adaptor) |
116 | 117 |
: Parent(*adaptor._digraph) {} |
117 | 118 |
|
118 | 119 |
NodeMap(const Adaptor& adaptor, const V& value) |
119 | 120 |
: Parent(*adaptor._digraph, value) { } |
120 | 121 |
|
121 | 122 |
private: |
122 | 123 |
NodeMap& operator=(const NodeMap& cmap) { |
123 | 124 |
return operator=<NodeMap>(cmap); |
124 | 125 |
} |
125 | 126 |
|
126 | 127 |
template <typename CMap> |
127 | 128 |
NodeMap& operator=(const CMap& cmap) { |
128 | 129 |
Parent::operator=(cmap); |
129 | 130 |
return *this; |
130 | 131 |
} |
131 | 132 |
|
132 | 133 |
}; |
133 | 134 |
|
134 | 135 |
template <typename V> |
135 | 136 |
class ArcMap : public DGR::template ArcMap<V> { |
136 | 137 |
public: |
137 | 138 |
|
138 | 139 |
typedef typename DGR::template ArcMap<V> Parent; |
139 | 140 |
|
140 | 141 |
explicit ArcMap(const DigraphAdaptorBase<DGR>& adaptor) |
141 | 142 |
: Parent(*adaptor._digraph) {} |
142 | 143 |
|
143 | 144 |
ArcMap(const DigraphAdaptorBase<DGR>& adaptor, const V& value) |
144 | 145 |
: Parent(*adaptor._digraph, value) {} |
145 | 146 |
|
146 | 147 |
private: |
147 | 148 |
ArcMap& operator=(const ArcMap& cmap) { |
148 | 149 |
return operator=<ArcMap>(cmap); |
149 | 150 |
} |
150 | 151 |
|
151 | 152 |
template <typename CMap> |
152 | 153 |
ArcMap& operator=(const CMap& cmap) { |
153 | 154 |
Parent::operator=(cmap); |
154 | 155 |
return *this; |
155 | 156 |
} |
156 | 157 |
|
157 | 158 |
}; |
158 | 159 |
|
159 | 160 |
}; |
160 | 161 |
|
161 | 162 |
template<typename GR> |
162 | 163 |
class GraphAdaptorBase { |
163 | 164 |
public: |
164 | 165 |
typedef GR Graph; |
165 | 166 |
|
166 | 167 |
protected: |
167 | 168 |
GR* _graph; |
168 | 169 |
|
169 | 170 |
GraphAdaptorBase() : _graph(0) {} |
170 | 171 |
|
171 | 172 |
void initialize(GR& graph) { _graph = &graph; } |
172 | 173 |
|
173 | 174 |
public: |
174 | 175 |
GraphAdaptorBase(GR& graph) : _graph(&graph) {} |
175 | 176 |
|
176 | 177 |
typedef typename GR::Node Node; |
177 | 178 |
typedef typename GR::Arc Arc; |
178 | 179 |
typedef typename GR::Edge Edge; |
179 | 180 |
|
180 | 181 |
void first(Node& i) const { _graph->first(i); } |
181 | 182 |
void first(Arc& i) const { _graph->first(i); } |
182 | 183 |
void first(Edge& i) const { _graph->first(i); } |
183 | 184 |
void firstIn(Arc& i, const Node& n) const { _graph->firstIn(i, n); } |
184 | 185 |
void firstOut(Arc& i, const Node& n ) const { _graph->firstOut(i, n); } |
185 | 186 |
void firstInc(Edge &i, bool &d, const Node &n) const { |
186 | 187 |
_graph->firstInc(i, d, n); |
187 | 188 |
} |
188 | 189 |
|
189 | 190 |
void next(Node& i) const { _graph->next(i); } |
190 | 191 |
void next(Arc& i) const { _graph->next(i); } |
191 | 192 |
void next(Edge& i) const { _graph->next(i); } |
192 | 193 |
void nextIn(Arc& i) const { _graph->nextIn(i); } |
193 | 194 |
void nextOut(Arc& i) const { _graph->nextOut(i); } |
194 | 195 |
void nextInc(Edge &i, bool &d) const { _graph->nextInc(i, d); } |
195 | 196 |
|
196 | 197 |
Node u(const Edge& e) const { return _graph->u(e); } |
197 | 198 |
Node v(const Edge& e) const { return _graph->v(e); } |
198 | 199 |
|
199 | 200 |
Node source(const Arc& a) const { return _graph->source(a); } |
200 | 201 |
Node target(const Arc& a) const { return _graph->target(a); } |
201 | 202 |
|
202 | 203 |
typedef NodeNumTagIndicator<Graph> NodeNumTag; |
203 | 204 |
int nodeNum() const { return _graph->nodeNum(); } |
204 | 205 |
|
205 | 206 |
typedef ArcNumTagIndicator<Graph> ArcNumTag; |
206 | 207 |
int arcNum() const { return _graph->arcNum(); } |
207 | 208 |
|
208 | 209 |
typedef EdgeNumTagIndicator<Graph> EdgeNumTag; |
209 | 210 |
int edgeNum() const { return _graph->edgeNum(); } |
210 | 211 |
|
211 | 212 |
typedef FindArcTagIndicator<Graph> FindArcTag; |
212 | 213 |
Arc findArc(const Node& u, const Node& v, |
213 | 214 |
const Arc& prev = INVALID) const { |
214 | 215 |
return _graph->findArc(u, v, prev); |
215 | 216 |
} |
216 | 217 |
|
217 | 218 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
218 | 219 |
Edge findEdge(const Node& u, const Node& v, |
219 | 220 |
const Edge& prev = INVALID) const { |
220 | 221 |
return _graph->findEdge(u, v, prev); |
221 | 222 |
} |
222 | 223 |
|
223 | 224 |
Node addNode() { return _graph->addNode(); } |
224 | 225 |
Edge addEdge(const Node& u, const Node& v) { return _graph->addEdge(u, v); } |
225 | 226 |
|
226 | 227 |
void erase(const Node& i) { _graph->erase(i); } |
227 | 228 |
void erase(const Edge& i) { _graph->erase(i); } |
228 | 229 |
|
229 | 230 |
void clear() { _graph->clear(); } |
230 | 231 |
|
231 | 232 |
bool direction(const Arc& a) const { return _graph->direction(a); } |
232 | 233 |
Arc direct(const Edge& e, bool d) const { return _graph->direct(e, d); } |
233 | 234 |
|
234 | 235 |
int id(const Node& v) const { return _graph->id(v); } |
235 | 236 |
int id(const Arc& a) const { return _graph->id(a); } |
236 | 237 |
int id(const Edge& e) const { return _graph->id(e); } |
237 | 238 |
|
238 | 239 |
Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); } |
239 | 240 |
Arc arcFromId(int ix) const { return _graph->arcFromId(ix); } |
240 | 241 |
Edge edgeFromId(int ix) const { return _graph->edgeFromId(ix); } |
241 | 242 |
|
242 | 243 |
int maxNodeId() const { return _graph->maxNodeId(); } |
243 | 244 |
int maxArcId() const { return _graph->maxArcId(); } |
244 | 245 |
int maxEdgeId() const { return _graph->maxEdgeId(); } |
245 | 246 |
|
246 | 247 |
typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier; |
247 | 248 |
NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); } |
248 | 249 |
|
249 | 250 |
typedef typename ItemSetTraits<GR, Arc>::ItemNotifier ArcNotifier; |
250 | 251 |
ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); } |
251 | 252 |
|
252 | 253 |
typedef typename ItemSetTraits<GR, Edge>::ItemNotifier EdgeNotifier; |
253 | 254 |
EdgeNotifier& notifier(Edge) const { return _graph->notifier(Edge()); } |
254 | 255 |
|
255 | 256 |
template <typename V> |
256 | 257 |
class NodeMap : public GR::template NodeMap<V> { |
257 | 258 |
public: |
258 | 259 |
typedef typename GR::template NodeMap<V> Parent; |
259 | 260 |
explicit NodeMap(const GraphAdaptorBase<GR>& adapter) |
260 | 261 |
: Parent(*adapter._graph) {} |
261 | 262 |
NodeMap(const GraphAdaptorBase<GR>& adapter, const V& value) |
262 | 263 |
: Parent(*adapter._graph, value) {} |
263 | 264 |
|
264 | 265 |
private: |
265 | 266 |
NodeMap& operator=(const NodeMap& cmap) { |
266 | 267 |
return operator=<NodeMap>(cmap); |
267 | 268 |
} |
268 | 269 |
|
269 | 270 |
template <typename CMap> |
270 | 271 |
NodeMap& operator=(const CMap& cmap) { |
271 | 272 |
Parent::operator=(cmap); |
272 | 273 |
return *this; |
273 | 274 |
} |
274 | 275 |
|
275 | 276 |
}; |
276 | 277 |
|
277 | 278 |
template <typename V> |
278 | 279 |
class ArcMap : public GR::template ArcMap<V> { |
279 | 280 |
public: |
280 | 281 |
typedef typename GR::template ArcMap<V> Parent; |
281 | 282 |
explicit ArcMap(const GraphAdaptorBase<GR>& adapter) |
282 | 283 |
: Parent(*adapter._graph) {} |
283 | 284 |
ArcMap(const GraphAdaptorBase<GR>& adapter, const V& value) |
284 | 285 |
: Parent(*adapter._graph, value) {} |
285 | 286 |
|
286 | 287 |
private: |
287 | 288 |
ArcMap& operator=(const ArcMap& cmap) { |
288 | 289 |
return operator=<ArcMap>(cmap); |
1 | 1 |
/* -*- C++ -*- |
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_BITS_EDGE_SET_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_EDGE_SET_EXTENDER_H |
21 | 21 |
|
22 |
#include <lemon/core.h> |
|
22 | 23 |
#include <lemon/error.h> |
23 | 24 |
#include <lemon/bits/default_map.h> |
25 |
#include <lemon/bits/map_extender.h> |
|
24 | 26 |
|
25 | 27 |
///\ingroup digraphbits |
26 | 28 |
///\file |
27 | 29 |
///\brief Extenders for the arc set types |
28 | 30 |
namespace lemon { |
29 | 31 |
|
30 | 32 |
/// \ingroup digraphbits |
31 | 33 |
/// |
32 | 34 |
/// \brief Extender for the ArcSets |
33 | 35 |
template <typename Base> |
34 | 36 |
class ArcSetExtender : public Base { |
35 | 37 |
public: |
36 | 38 |
|
37 | 39 |
typedef Base Parent; |
38 | 40 |
typedef ArcSetExtender Digraph; |
39 | 41 |
|
40 | 42 |
// Base extensions |
41 | 43 |
|
42 | 44 |
typedef typename Parent::Node Node; |
43 | 45 |
typedef typename Parent::Arc Arc; |
44 | 46 |
|
45 | 47 |
int maxId(Node) const { |
46 | 48 |
return Parent::maxNodeId(); |
47 | 49 |
} |
48 | 50 |
|
49 | 51 |
int maxId(Arc) const { |
50 | 52 |
return Parent::maxArcId(); |
51 | 53 |
} |
52 | 54 |
|
53 | 55 |
Node fromId(int id, Node) const { |
54 | 56 |
return Parent::nodeFromId(id); |
55 | 57 |
} |
56 | 58 |
|
57 | 59 |
Arc fromId(int id, Arc) const { |
58 | 60 |
return Parent::arcFromId(id); |
59 | 61 |
} |
60 | 62 |
|
61 | 63 |
Node oppositeNode(const Node &n, const Arc &e) const { |
62 | 64 |
if (n == Parent::source(e)) |
63 | 65 |
return Parent::target(e); |
64 | 66 |
else if(n==Parent::target(e)) |
65 | 67 |
return Parent::source(e); |
66 | 68 |
else |
67 | 69 |
return INVALID; |
68 | 70 |
} |
69 | 71 |
|
70 | 72 |
|
71 | 73 |
// Alteration notifier extensions |
72 | 74 |
|
73 | 75 |
/// The arc observer registry. |
74 | 76 |
typedef AlterationNotifier<ArcSetExtender, Arc> ArcNotifier; |
75 | 77 |
|
76 | 78 |
protected: |
77 | 79 |
|
78 | 80 |
mutable ArcNotifier arc_notifier; |
79 | 81 |
|
80 | 82 |
public: |
81 | 83 |
|
82 | 84 |
using Parent::notifier; |
83 | 85 |
|
84 | 86 |
/// \brief Gives back the arc alteration notifier. |
85 | 87 |
/// |
86 | 88 |
/// Gives back the arc alteration notifier. |
87 | 89 |
ArcNotifier& notifier(Arc) const { |
88 | 90 |
return arc_notifier; |
89 | 91 |
} |
90 | 92 |
|
91 | 93 |
// Iterable extensions |
92 | 94 |
|
93 | 95 |
class NodeIt : public Node { |
94 | 96 |
const Digraph* digraph; |
95 | 97 |
public: |
96 | 98 |
|
97 | 99 |
NodeIt() {} |
98 | 100 |
|
99 | 101 |
NodeIt(Invalid i) : Node(i) { } |
100 | 102 |
|
101 | 103 |
explicit NodeIt(const Digraph& _graph) : digraph(&_graph) { |
102 | 104 |
_graph.first(static_cast<Node&>(*this)); |
103 | 105 |
} |
104 | 106 |
|
105 | 107 |
NodeIt(const Digraph& _graph, const Node& node) |
106 | 108 |
: Node(node), digraph(&_graph) {} |
107 | 109 |
|
108 | 110 |
NodeIt& operator++() { |
109 | 111 |
digraph->next(*this); |
110 | 112 |
return *this; |
111 | 113 |
} |
112 | 114 |
|
113 | 115 |
}; |
114 | 116 |
|
115 | 117 |
|
116 | 118 |
class ArcIt : public Arc { |
117 | 119 |
const Digraph* digraph; |
118 | 120 |
public: |
119 | 121 |
|
120 | 122 |
ArcIt() { } |
121 | 123 |
|
122 | 124 |
ArcIt(Invalid i) : Arc(i) { } |
123 | 125 |
|
124 | 126 |
explicit ArcIt(const Digraph& _graph) : digraph(&_graph) { |
125 | 127 |
_graph.first(static_cast<Arc&>(*this)); |
126 | 128 |
} |
127 | 129 |
|
128 | 130 |
ArcIt(const Digraph& _graph, const Arc& e) : |
129 | 131 |
Arc(e), digraph(&_graph) { } |
130 | 132 |
|
131 | 133 |
ArcIt& operator++() { |
132 | 134 |
digraph->next(*this); |
133 | 135 |
return *this; |
134 | 136 |
} |
135 | 137 |
|
136 | 138 |
}; |
137 | 139 |
|
138 | 140 |
|
139 | 141 |
class OutArcIt : public Arc { |
140 | 142 |
const Digraph* digraph; |
141 | 143 |
public: |
142 | 144 |
|
143 | 145 |
OutArcIt() { } |
144 | 146 |
|
145 | 147 |
OutArcIt(Invalid i) : Arc(i) { } |
146 | 148 |
|
147 | 149 |
OutArcIt(const Digraph& _graph, const Node& node) |
148 | 150 |
: digraph(&_graph) { |
149 | 151 |
_graph.firstOut(*this, node); |
150 | 152 |
} |
151 | 153 |
|
152 | 154 |
OutArcIt(const Digraph& _graph, const Arc& arc) |
153 | 155 |
: Arc(arc), digraph(&_graph) {} |
154 | 156 |
|
155 | 157 |
OutArcIt& operator++() { |
156 | 158 |
digraph->nextOut(*this); |
157 | 159 |
return *this; |
158 | 160 |
} |
159 | 161 |
|
160 | 162 |
}; |
161 | 163 |
|
162 | 164 |
|
163 | 165 |
class InArcIt : public Arc { |
164 | 166 |
const Digraph* digraph; |
165 | 167 |
public: |
166 | 168 |
|
167 | 169 |
InArcIt() { } |
168 | 170 |
|
169 | 171 |
InArcIt(Invalid i) : Arc(i) { } |
170 | 172 |
|
171 | 173 |
InArcIt(const Digraph& _graph, const Node& node) |
172 | 174 |
: digraph(&_graph) { |
173 | 175 |
_graph.firstIn(*this, node); |
174 | 176 |
} |
175 | 177 |
|
176 | 178 |
InArcIt(const Digraph& _graph, const Arc& arc) : |
177 | 179 |
Arc(arc), digraph(&_graph) {} |
178 | 180 |
|
179 | 181 |
InArcIt& operator++() { |
180 | 182 |
digraph->nextIn(*this); |
181 | 183 |
return *this; |
182 | 184 |
} |
183 | 185 |
|
184 | 186 |
}; |
185 | 187 |
|
186 | 188 |
/// \brief Base node of the iterator |
187 | 189 |
/// |
188 | 190 |
/// Returns the base node (ie. the source in this case) of the iterator |
189 | 191 |
Node baseNode(const OutArcIt &e) const { |
190 | 192 |
return Parent::source(static_cast<const Arc&>(e)); |
191 | 193 |
} |
192 | 194 |
/// \brief Running node of the iterator |
193 | 195 |
/// |
194 | 196 |
/// Returns the running node (ie. the target in this case) of the |
195 | 197 |
/// iterator |
196 | 198 |
Node runningNode(const OutArcIt &e) const { |
197 | 199 |
return Parent::target(static_cast<const Arc&>(e)); |
198 | 200 |
} |
199 | 201 |
|
200 | 202 |
/// \brief Base node of the iterator |
201 | 203 |
/// |
202 | 204 |
/// Returns the base node (ie. the target in this case) of the iterator |
203 | 205 |
Node baseNode(const InArcIt &e) const { |
204 | 206 |
return Parent::target(static_cast<const Arc&>(e)); |
205 | 207 |
} |
206 | 208 |
/// \brief Running node of the iterator |
207 | 209 |
/// |
208 | 210 |
/// Returns the running node (ie. the source in this case) of the |
209 | 211 |
/// iterator |
210 | 212 |
Node runningNode(const InArcIt &e) const { |
211 | 213 |
return Parent::source(static_cast<const Arc&>(e)); |
212 | 214 |
} |
213 | 215 |
|
214 | 216 |
using Parent::first; |
215 | 217 |
|
216 | 218 |
// Mappable extension |
217 | 219 |
|
218 | 220 |
template <typename _Value> |
219 | 221 |
class ArcMap |
220 | 222 |
: public MapExtender<DefaultMap<Digraph, Arc, _Value> > { |
221 | 223 |
public: |
222 | 224 |
typedef ArcSetExtender Digraph; |
223 | 225 |
typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent; |
224 | 226 |
|
225 | 227 |
explicit ArcMap(const Digraph& _g) |
226 | 228 |
: Parent(_g) {} |
227 | 229 |
ArcMap(const Digraph& _g, const _Value& _v) |
228 | 230 |
: Parent(_g, _v) {} |
229 | 231 |
|
230 | 232 |
ArcMap& operator=(const ArcMap& cmap) { |
231 | 233 |
return operator=<ArcMap>(cmap); |
232 | 234 |
} |
233 | 235 |
|
234 | 236 |
template <typename CMap> |
235 | 237 |
ArcMap& operator=(const CMap& cmap) { |
236 | 238 |
Parent::operator=(cmap); |
237 | 239 |
return *this; |
238 | 240 |
} |
239 | 241 |
|
240 | 242 |
}; |
241 | 243 |
|
242 | 244 |
|
243 | 245 |
// Alteration extension |
244 | 246 |
|
245 | 247 |
Arc addArc(const Node& from, const Node& to) { |
246 | 248 |
Arc arc = Parent::addArc(from, to); |
247 | 249 |
notifier(Arc()).add(arc); |
248 | 250 |
return arc; |
249 | 251 |
} |
250 | 252 |
|
251 | 253 |
void clear() { |
252 | 254 |
notifier(Arc()).clear(); |
253 | 255 |
Parent::clear(); |
254 | 256 |
} |
255 | 257 |
|
256 | 258 |
void erase(const Arc& arc) { |
257 | 259 |
notifier(Arc()).erase(arc); |
258 | 260 |
Parent::erase(arc); |
259 | 261 |
} |
260 | 262 |
|
261 | 263 |
ArcSetExtender() { |
262 | 264 |
arc_notifier.setContainer(*this); |
263 | 265 |
} |
264 | 266 |
|
265 | 267 |
~ArcSetExtender() { |
266 | 268 |
arc_notifier.clear(); |
267 | 269 |
} |
268 | 270 |
|
269 | 271 |
}; |
270 | 272 |
|
271 | 273 |
|
272 | 274 |
/// \ingroup digraphbits |
273 | 275 |
/// |
274 | 276 |
/// \brief Extender for the EdgeSets |
275 | 277 |
template <typename Base> |
276 | 278 |
class EdgeSetExtender : public Base { |
277 | 279 |
|
278 | 280 |
public: |
279 | 281 |
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_BITS_PRED_MAP_PATH_H |
20 | 20 |
#define LEMON_BITS_PRED_MAP_PATH_H |
21 | 21 |
|
22 |
#include <lemon/core.h> |
|
23 |
#include <lemon/concept_check.h> |
|
24 |
|
|
22 | 25 |
namespace lemon { |
23 | 26 |
|
24 | 27 |
template <typename _Digraph, typename _PredMap> |
25 | 28 |
class PredMapPath { |
26 | 29 |
public: |
27 | 30 |
typedef True RevPathTag; |
28 | 31 |
|
29 | 32 |
typedef _Digraph Digraph; |
30 | 33 |
typedef typename Digraph::Arc Arc; |
31 | 34 |
typedef _PredMap PredMap; |
32 | 35 |
|
33 | 36 |
PredMapPath(const Digraph& _digraph, const PredMap& _predMap, |
34 | 37 |
typename Digraph::Node _target) |
35 | 38 |
: digraph(_digraph), predMap(_predMap), target(_target) {} |
36 | 39 |
|
37 | 40 |
int length() const { |
38 | 41 |
int len = 0; |
39 | 42 |
typename Digraph::Node node = target; |
40 | 43 |
typename Digraph::Arc arc; |
41 | 44 |
while ((arc = predMap[node]) != INVALID) { |
42 | 45 |
node = digraph.source(arc); |
43 | 46 |
++len; |
44 | 47 |
} |
45 | 48 |
return len; |
46 | 49 |
} |
47 | 50 |
|
48 | 51 |
bool empty() const { |
49 | 52 |
return predMap[target] != INVALID; |
50 | 53 |
} |
51 | 54 |
|
52 | 55 |
class RevArcIt { |
53 | 56 |
public: |
54 | 57 |
RevArcIt() {} |
55 | 58 |
RevArcIt(Invalid) : path(0), current(INVALID) {} |
56 | 59 |
RevArcIt(const PredMapPath& _path) |
57 | 60 |
: path(&_path), current(_path.target) { |
58 | 61 |
if (path->predMap[current] == INVALID) current = INVALID; |
59 | 62 |
} |
60 | 63 |
|
61 | 64 |
operator const typename Digraph::Arc() const { |
62 | 65 |
return path->predMap[current]; |
63 | 66 |
} |
64 | 67 |
|
65 | 68 |
RevArcIt& operator++() { |
66 | 69 |
current = path->digraph.source(path->predMap[current]); |
67 | 70 |
if (path->predMap[current] == INVALID) current = INVALID; |
68 | 71 |
return *this; |
69 | 72 |
} |
70 | 73 |
|
71 | 74 |
bool operator==(const RevArcIt& e) const { |
72 | 75 |
return current == e.current; |
73 | 76 |
} |
74 | 77 |
|
75 | 78 |
bool operator!=(const RevArcIt& e) const { |
76 | 79 |
return current != e.current; |
77 | 80 |
} |
78 | 81 |
|
79 | 82 |
bool operator<(const RevArcIt& e) const { |
80 | 83 |
return current < e.current; |
81 | 84 |
} |
82 | 85 |
|
83 | 86 |
private: |
84 | 87 |
const PredMapPath* path; |
85 | 88 |
typename Digraph::Node current; |
86 | 89 |
}; |
87 | 90 |
|
88 | 91 |
private: |
89 | 92 |
const Digraph& digraph; |
90 | 93 |
const PredMap& predMap; |
91 | 94 |
typename Digraph::Node target; |
92 | 95 |
}; |
93 | 96 |
|
94 | 97 |
|
95 | 98 |
template <typename _Digraph, typename _PredMatrixMap> |
96 | 99 |
class PredMatrixMapPath { |
97 | 100 |
public: |
98 | 101 |
typedef True RevPathTag; |
99 | 102 |
|
100 | 103 |
typedef _Digraph Digraph; |
101 | 104 |
typedef typename Digraph::Arc Arc; |
102 | 105 |
typedef _PredMatrixMap PredMatrixMap; |
103 | 106 |
|
104 | 107 |
PredMatrixMapPath(const Digraph& _digraph, |
105 | 108 |
const PredMatrixMap& _predMatrixMap, |
106 | 109 |
typename Digraph::Node _source, |
107 | 110 |
typename Digraph::Node _target) |
108 | 111 |
: digraph(_digraph), predMatrixMap(_predMatrixMap), |
109 | 112 |
source(_source), target(_target) {} |
110 | 113 |
|
111 | 114 |
int length() const { |
112 | 115 |
int len = 0; |
113 | 116 |
typename Digraph::Node node = target; |
114 | 117 |
typename Digraph::Arc arc; |
115 | 118 |
while ((arc = predMatrixMap(source, node)) != INVALID) { |
116 | 119 |
node = digraph.source(arc); |
117 | 120 |
++len; |
118 | 121 |
} |
119 | 122 |
return len; |
120 | 123 |
} |
121 | 124 |
|
122 | 125 |
bool empty() const { |
123 | 126 |
return source != target; |
124 | 127 |
} |
125 | 128 |
|
126 | 129 |
class RevArcIt { |
127 | 130 |
public: |
128 | 131 |
RevArcIt() {} |
129 | 132 |
RevArcIt(Invalid) : path(0), current(INVALID) {} |
130 | 133 |
RevArcIt(const PredMatrixMapPath& _path) |
131 | 134 |
: path(&_path), current(_path.target) { |
132 | 135 |
if (path->predMatrixMap(path->source, current) == INVALID) |
133 | 136 |
current = INVALID; |
134 | 137 |
} |
135 | 138 |
|
136 | 139 |
operator const typename Digraph::Arc() const { |
137 | 140 |
return path->predMatrixMap(path->source, current); |
138 | 141 |
} |
139 | 142 |
|
140 | 143 |
RevArcIt& operator++() { |
141 | 144 |
current = |
142 | 145 |
path->digraph.source(path->predMatrixMap(path->source, current)); |
143 | 146 |
if (path->predMatrixMap(path->source, current) == INVALID) |
144 | 147 |
current = INVALID; |
145 | 148 |
return *this; |
146 | 149 |
} |
147 | 150 |
|
148 | 151 |
bool operator==(const RevArcIt& e) const { |
149 | 152 |
return current == e.current; |
150 | 153 |
} |
151 | 154 |
|
152 | 155 |
bool operator!=(const RevArcIt& e) const { |
153 | 156 |
return current != e.current; |
154 | 157 |
} |
155 | 158 |
|
156 | 159 |
bool operator<(const RevArcIt& e) const { |
157 | 160 |
return current < e.current; |
158 | 161 |
} |
159 | 162 |
|
160 | 163 |
private: |
161 | 164 |
const PredMatrixMapPath* path; |
162 | 165 |
typename Digraph::Node current; |
163 | 166 |
}; |
164 | 167 |
|
165 | 168 |
private: |
166 | 169 |
const Digraph& digraph; |
167 | 170 |
const PredMatrixMap& predMatrixMap; |
168 | 171 |
typename Digraph::Node source; |
169 | 172 |
typename Digraph::Node target; |
170 | 173 |
}; |
171 | 174 |
|
172 | 175 |
} |
173 | 176 |
|
174 | 177 |
#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-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_BITS_SOLVER_BITS_H |
20 | 20 |
#define LEMON_BITS_SOLVER_BITS_H |
21 | 21 |
|
22 |
#include <vector> |
|
23 |
|
|
22 | 24 |
namespace lemon { |
23 | 25 |
|
24 | 26 |
namespace _solver_bits { |
25 | 27 |
|
26 | 28 |
class VarIndex { |
27 | 29 |
private: |
28 | 30 |
struct ItemT { |
29 | 31 |
int prev, next; |
30 | 32 |
int index; |
31 | 33 |
}; |
32 | 34 |
std::vector<ItemT> items; |
33 | 35 |
int first_item, last_item, first_free_item; |
34 | 36 |
|
35 | 37 |
std::vector<int> cross; |
36 | 38 |
|
37 | 39 |
public: |
38 | 40 |
|
39 | 41 |
VarIndex() |
40 | 42 |
: first_item(-1), last_item(-1), first_free_item(-1) { |
41 | 43 |
} |
42 | 44 |
|
43 | 45 |
void clear() { |
44 | 46 |
first_item = -1; |
45 | 47 |
first_free_item = -1; |
46 | 48 |
items.clear(); |
47 | 49 |
cross.clear(); |
48 | 50 |
} |
49 | 51 |
|
50 | 52 |
int addIndex(int idx) { |
51 | 53 |
int n; |
52 | 54 |
if (first_free_item == -1) { |
53 | 55 |
n = items.size(); |
54 | 56 |
items.push_back(ItemT()); |
55 | 57 |
} else { |
56 | 58 |
n = first_free_item; |
57 | 59 |
first_free_item = items[n].next; |
58 | 60 |
if (first_free_item != -1) { |
59 | 61 |
items[first_free_item].prev = -1; |
60 | 62 |
} |
61 | 63 |
} |
62 | 64 |
items[n].index = idx; |
63 | 65 |
if (static_cast<int>(cross.size()) <= idx) { |
64 | 66 |
cross.resize(idx + 1, -1); |
65 | 67 |
} |
66 | 68 |
cross[idx] = n; |
67 | 69 |
|
68 | 70 |
items[n].prev = last_item; |
69 | 71 |
items[n].next = -1; |
70 | 72 |
if (last_item != -1) { |
71 | 73 |
items[last_item].next = n; |
72 | 74 |
} else { |
73 | 75 |
first_item = n; |
74 | 76 |
} |
75 | 77 |
last_item = n; |
76 | 78 |
|
77 | 79 |
return n; |
78 | 80 |
} |
79 | 81 |
|
80 | 82 |
int addIndex(int idx, int n) { |
81 | 83 |
while (n >= static_cast<int>(items.size())) { |
82 | 84 |
items.push_back(ItemT()); |
83 | 85 |
items.back().prev = -1; |
84 | 86 |
items.back().next = first_free_item; |
85 | 87 |
if (first_free_item != -1) { |
86 | 88 |
items[first_free_item].prev = items.size() - 1; |
87 | 89 |
} |
88 | 90 |
first_free_item = items.size() - 1; |
89 | 91 |
} |
90 | 92 |
if (items[n].next != -1) { |
91 | 93 |
items[items[n].next].prev = items[n].prev; |
92 | 94 |
} |
93 | 95 |
if (items[n].prev != -1) { |
94 | 96 |
items[items[n].prev].next = items[n].next; |
95 | 97 |
} else { |
96 | 98 |
first_free_item = items[n].next; |
97 | 99 |
} |
98 | 100 |
|
99 | 101 |
items[n].index = idx; |
100 | 102 |
if (static_cast<int>(cross.size()) <= idx) { |
101 | 103 |
cross.resize(idx + 1, -1); |
102 | 104 |
} |
103 | 105 |
cross[idx] = n; |
104 | 106 |
|
105 | 107 |
items[n].prev = last_item; |
106 | 108 |
items[n].next = -1; |
107 | 109 |
if (last_item != -1) { |
108 | 110 |
items[last_item].next = n; |
109 | 111 |
} else { |
110 | 112 |
first_item = n; |
111 | 113 |
} |
112 | 114 |
last_item = n; |
113 | 115 |
|
114 | 116 |
return n; |
115 | 117 |
} |
116 | 118 |
|
117 | 119 |
void eraseIndex(int idx) { |
118 | 120 |
int n = cross[idx]; |
119 | 121 |
|
120 | 122 |
if (items[n].prev != -1) { |
121 | 123 |
items[items[n].prev].next = items[n].next; |
122 | 124 |
} else { |
123 | 125 |
first_item = items[n].next; |
124 | 126 |
} |
125 | 127 |
if (items[n].next != -1) { |
126 | 128 |
items[items[n].next].prev = items[n].prev; |
127 | 129 |
} else { |
128 | 130 |
last_item = items[n].prev; |
129 | 131 |
} |
130 | 132 |
|
131 | 133 |
if (first_free_item != -1) { |
132 | 134 |
items[first_free_item].prev = n; |
133 | 135 |
} |
134 | 136 |
items[n].next = first_free_item; |
135 | 137 |
items[n].prev = -1; |
136 | 138 |
first_free_item = n; |
137 | 139 |
|
138 | 140 |
while (!cross.empty() && cross.back() == -1) { |
139 | 141 |
cross.pop_back(); |
140 | 142 |
} |
141 | 143 |
} |
142 | 144 |
|
143 | 145 |
int maxIndex() const { |
144 | 146 |
return cross.size() - 1; |
145 | 147 |
} |
146 | 148 |
|
147 | 149 |
void shiftIndices(int idx) { |
148 | 150 |
for (int i = idx + 1; i < static_cast<int>(cross.size()); ++i) { |
149 | 151 |
cross[i - 1] = cross[i]; |
150 | 152 |
if (cross[i] != -1) { |
151 | 153 |
--items[cross[i]].index; |
152 | 154 |
} |
153 | 155 |
} |
154 | 156 |
cross.back() = -1; |
155 | 157 |
cross.pop_back(); |
156 | 158 |
while (!cross.empty() && cross.back() == -1) { |
157 | 159 |
cross.pop_back(); |
158 | 160 |
} |
159 | 161 |
} |
160 | 162 |
|
161 | 163 |
void relocateIndex(int idx, int jdx) { |
162 | 164 |
cross[idx] = cross[jdx]; |
163 | 165 |
items[cross[jdx]].index = idx; |
164 | 166 |
cross[jdx] = -1; |
165 | 167 |
|
166 | 168 |
while (!cross.empty() && cross.back() == -1) { |
167 | 169 |
cross.pop_back(); |
168 | 170 |
} |
169 | 171 |
} |
170 | 172 |
|
171 | 173 |
int operator[](int idx) const { |
172 | 174 |
return cross[idx]; |
173 | 175 |
} |
174 | 176 |
|
175 | 177 |
int operator()(int fdx) const { |
176 | 178 |
return items[fdx].index; |
177 | 179 |
} |
178 | 180 |
|
179 | 181 |
void firstItem(int& fdx) const { |
180 | 182 |
fdx = first_item; |
181 | 183 |
} |
182 | 184 |
|
183 | 185 |
void nextItem(int& fdx) const { |
184 | 186 |
fdx = items[fdx].next; |
185 | 187 |
} |
186 | 188 |
|
187 | 189 |
}; |
188 | 190 |
} |
189 | 191 |
} |
190 | 192 |
|
191 | 193 |
#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_CONCEPT_HEAP_H |
24 | 24 |
#define LEMON_CONCEPT_HEAP_H |
25 | 25 |
|
26 | 26 |
#include <lemon/core.h> |
27 |
#include <lemon/concept_check.h> |
|
27 | 28 |
|
28 | 29 |
namespace lemon { |
29 | 30 |
|
30 | 31 |
namespace concepts { |
31 | 32 |
|
32 | 33 |
/// \addtogroup concept |
33 | 34 |
/// @{ |
34 | 35 |
|
35 | 36 |
/// \brief The heap concept. |
36 | 37 |
/// |
37 | 38 |
/// Concept class describing the main interface of heaps. |
38 | 39 |
template <typename Priority, typename ItemIntMap> |
39 | 40 |
class Heap { |
40 | 41 |
public: |
41 | 42 |
|
42 | 43 |
/// Type of the items stored in the heap. |
43 | 44 |
typedef typename ItemIntMap::Key Item; |
44 | 45 |
|
45 | 46 |
/// Type of the priorities. |
46 | 47 |
typedef Priority Prio; |
47 | 48 |
|
48 | 49 |
/// \brief Type to represent the states of the items. |
49 | 50 |
/// |
50 | 51 |
/// Each item has a state associated to it. It can be "in heap", |
51 | 52 |
/// "pre heap" or "post heap". The later two are indifferent |
52 | 53 |
/// from the point of view of the heap, but may be useful for |
53 | 54 |
/// the user. |
54 | 55 |
/// |
55 | 56 |
/// The \c ItemIntMap must be initialized in such a way, that it |
56 | 57 |
/// assigns \c PRE_HEAP (<tt>-1</tt>) to every item. |
57 | 58 |
enum State { |
58 | 59 |
IN_HEAP = 0, |
59 | 60 |
PRE_HEAP = -1, |
60 | 61 |
POST_HEAP = -2 |
61 | 62 |
}; |
62 | 63 |
|
63 | 64 |
/// \brief The constructor. |
64 | 65 |
/// |
65 | 66 |
/// The constructor. |
66 | 67 |
/// \param map A map that assigns \c int values to keys of type |
67 | 68 |
/// \c Item. It is used internally by the heap implementations to |
68 | 69 |
/// handle the cross references. The assigned value must be |
69 | 70 |
/// \c PRE_HEAP (<tt>-1</tt>) for every item. |
70 | 71 |
explicit Heap(ItemIntMap &map) {} |
71 | 72 |
|
72 | 73 |
/// \brief The number of items stored in the heap. |
73 | 74 |
/// |
74 | 75 |
/// Returns the number of items stored in the heap. |
75 | 76 |
int size() const { return 0; } |
76 | 77 |
|
77 | 78 |
/// \brief Checks if the heap is empty. |
78 | 79 |
/// |
79 | 80 |
/// Returns \c true if the heap is empty. |
80 | 81 |
bool empty() const { return false; } |
81 | 82 |
|
82 | 83 |
/// \brief Makes the heap empty. |
83 | 84 |
/// |
84 | 85 |
/// Makes the heap empty. |
85 | 86 |
void clear(); |
86 | 87 |
|
87 | 88 |
/// \brief Inserts an item into the heap with the given priority. |
88 | 89 |
/// |
89 | 90 |
/// Inserts the given item into the heap with the given priority. |
90 | 91 |
/// \param i The item to insert. |
91 | 92 |
/// \param p The priority of the item. |
92 | 93 |
void push(const Item &i, const Prio &p) {} |
93 | 94 |
|
94 | 95 |
/// \brief Returns the item having minimum priority. |
95 | 96 |
/// |
96 | 97 |
/// Returns the item having minimum priority. |
97 | 98 |
/// \pre The heap must be non-empty. |
98 | 99 |
Item top() const {} |
99 | 100 |
|
100 | 101 |
/// \brief The minimum priority. |
101 | 102 |
/// |
102 | 103 |
/// Returns the minimum priority. |
103 | 104 |
/// \pre The heap must be non-empty. |
104 | 105 |
Prio prio() const {} |
105 | 106 |
|
106 | 107 |
/// \brief Removes the item having minimum priority. |
107 | 108 |
/// |
108 | 109 |
/// Removes the item having minimum priority. |
109 | 110 |
/// \pre The heap must be non-empty. |
110 | 111 |
void pop() {} |
111 | 112 |
|
112 | 113 |
/// \brief Removes an item from the heap. |
113 | 114 |
/// |
114 | 115 |
/// Removes the given item from the heap if it is already stored. |
115 | 116 |
/// \param i The item to delete. |
116 | 117 |
void erase(const Item &i) {} |
117 | 118 |
|
118 | 119 |
/// \brief The priority of an item. |
119 | 120 |
/// |
120 | 121 |
/// Returns the priority of the given item. |
121 | 122 |
/// \pre \c i must be in the heap. |
122 | 123 |
/// \param i The item. |
123 | 124 |
Prio operator[](const Item &i) const {} |
124 | 125 |
|
125 | 126 |
/// \brief Sets the priority of an item or inserts it, if it is |
126 | 127 |
/// not stored in the heap. |
127 | 128 |
/// |
128 | 129 |
/// This method sets the priority of the given item if it is |
129 | 130 |
/// already stored in the heap. |
130 | 131 |
/// Otherwise it inserts the given item with the given priority. |
131 | 132 |
/// |
132 | 133 |
/// \param i The item. |
133 | 134 |
/// \param p The priority. |
134 | 135 |
void set(const Item &i, const Prio &p) {} |
135 | 136 |
|
136 | 137 |
/// \brief Decreases the priority of an item to the given value. |
137 | 138 |
/// |
138 | 139 |
/// Decreases the priority of an item to the given value. |
139 | 140 |
/// \pre \c i must be stored in the heap with priority at least \c p. |
140 | 141 |
/// \param i The item. |
141 | 142 |
/// \param p The priority. |
142 | 143 |
void decrease(const Item &i, const Prio &p) {} |
143 | 144 |
|
144 | 145 |
/// \brief Increases the priority of an item to the given value. |
145 | 146 |
/// |
146 | 147 |
/// Increases the priority of an item to the given value. |
147 | 148 |
/// \pre \c i must be stored in the heap with priority at most \c p. |
148 | 149 |
/// \param i The item. |
149 | 150 |
/// \param p The priority. |
150 | 151 |
void increase(const Item &i, const Prio &p) {} |
151 | 152 |
|
152 | 153 |
/// \brief Returns if an item is in, has already been in, or has |
153 | 154 |
/// never been in the heap. |
154 | 155 |
/// |
155 | 156 |
/// This method returns \c PRE_HEAP if the given item has never |
156 | 157 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
157 | 158 |
/// and \c POST_HEAP otherwise. |
158 | 159 |
/// In the latter case it is possible that the item will get back |
159 | 160 |
/// to the heap again. |
160 | 161 |
/// \param i The item. |
161 | 162 |
State state(const Item &i) const {} |
162 | 163 |
|
163 | 164 |
/// \brief Sets the state of an item in the heap. |
164 | 165 |
/// |
165 | 166 |
/// Sets the state of the given item in the heap. It can be used |
166 | 167 |
/// to manually clear the heap when it is important to achive the |
167 | 168 |
/// better time complexity. |
168 | 169 |
/// \param i The item. |
169 | 170 |
/// \param st The state. It should not be \c IN_HEAP. |
170 | 171 |
void state(const Item& i, State st) {} |
171 | 172 |
|
172 | 173 |
|
173 | 174 |
template <typename _Heap> |
174 | 175 |
struct Constraints { |
175 | 176 |
public: |
176 | 177 |
void constraints() { |
177 | 178 |
typedef typename _Heap::Item OwnItem; |
178 | 179 |
typedef typename _Heap::Prio OwnPrio; |
179 | 180 |
typedef typename _Heap::State OwnState; |
180 | 181 |
|
181 | 182 |
Item item; |
182 | 183 |
Prio prio; |
183 | 184 |
item=Item(); |
184 | 185 |
prio=Prio(); |
185 | 186 |
ignore_unused_variable_warning(item); |
186 | 187 |
ignore_unused_variable_warning(prio); |
187 | 188 |
|
188 | 189 |
OwnItem own_item; |
189 | 190 |
OwnPrio own_prio; |
190 | 191 |
OwnState own_state; |
191 | 192 |
own_item=Item(); |
192 | 193 |
own_prio=Prio(); |
193 | 194 |
ignore_unused_variable_warning(own_item); |
194 | 195 |
ignore_unused_variable_warning(own_prio); |
195 | 196 |
ignore_unused_variable_warning(own_state); |
196 | 197 |
|
197 | 198 |
_Heap heap1(map); |
198 | 199 |
_Heap heap2 = heap1; |
199 | 200 |
ignore_unused_variable_warning(heap1); |
200 | 201 |
ignore_unused_variable_warning(heap2); |
201 | 202 |
|
202 | 203 |
int s = heap.size(); |
203 | 204 |
ignore_unused_variable_warning(s); |
204 | 205 |
bool e = heap.empty(); |
205 | 206 |
ignore_unused_variable_warning(e); |
206 | 207 |
|
207 | 208 |
prio = heap.prio(); |
208 | 209 |
item = heap.top(); |
209 | 210 |
prio = heap[item]; |
210 | 211 |
own_prio = heap.prio(); |
211 | 212 |
own_item = heap.top(); |
212 | 213 |
own_prio = heap[own_item]; |
213 | 214 |
|
214 | 215 |
heap.push(item, prio); |
215 | 216 |
heap.push(own_item, own_prio); |
216 | 217 |
heap.pop(); |
217 | 218 |
|
218 | 219 |
heap.set(item, prio); |
219 | 220 |
heap.decrease(item, prio); |
220 | 221 |
heap.increase(item, prio); |
221 | 222 |
heap.set(own_item, own_prio); |
222 | 223 |
heap.decrease(own_item, own_prio); |
223 | 224 |
heap.increase(own_item, own_prio); |
224 | 225 |
|
225 | 226 |
heap.erase(item); |
226 | 227 |
heap.erase(own_item); |
227 | 228 |
heap.clear(); |
228 | 229 |
|
229 | 230 |
own_state = heap.state(own_item); |
230 | 231 |
heap.state(own_item, own_state); |
231 | 232 |
|
232 | 233 |
own_state = _Heap::PRE_HEAP; |
233 | 234 |
own_state = _Heap::IN_HEAP; |
234 | 235 |
own_state = _Heap::POST_HEAP; |
235 | 236 |
} |
236 | 237 |
|
237 | 238 |
_Heap& heap; |
238 | 239 |
ItemIntMap& map; |
239 | 240 |
}; |
240 | 241 |
}; |
241 | 242 |
|
242 | 243 |
/// @} |
243 | 244 |
} // namespace lemon |
244 | 245 |
} |
245 | 246 |
#endif // LEMON_CONCEPT_PATH_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_ELEVATOR_H |
20 | 20 |
#define LEMON_ELEVATOR_H |
21 | 21 |
|
22 | 22 |
///\ingroup auxdat |
23 | 23 |
///\file |
24 | 24 |
///\brief Elevator class |
25 | 25 |
/// |
26 | 26 |
///Elevator class implements an efficient data structure |
27 | 27 |
///for labeling items in push-relabel type algorithms. |
28 | 28 |
/// |
29 | 29 |
|
30 |
#include <lemon/core.h> |
|
30 | 31 |
#include <lemon/bits/traits.h> |
31 | 32 |
|
32 | 33 |
namespace lemon { |
33 | 34 |
|
34 | 35 |
///Class for handling "labels" in push-relabel type algorithms. |
35 | 36 |
|
36 | 37 |
///A class for handling "labels" in push-relabel type algorithms. |
37 | 38 |
/// |
38 | 39 |
///\ingroup auxdat |
39 | 40 |
///Using this class you can assign "labels" (nonnegative integer numbers) |
40 | 41 |
///to the edges or nodes of a graph, manipulate and query them through |
41 | 42 |
///operations typically arising in "push-relabel" type algorithms. |
42 | 43 |
/// |
43 | 44 |
///Each item is either \em active or not, and you can also choose a |
44 | 45 |
///highest level active item. |
45 | 46 |
/// |
46 | 47 |
///\sa LinkedElevator |
47 | 48 |
/// |
48 | 49 |
///\param Graph Type of the underlying graph. |
49 | 50 |
///\param Item Type of the items the data is assigned to (Graph::Node, |
50 | 51 |
///Graph::Arc, Graph::Edge). |
51 | 52 |
template<class Graph, class Item> |
52 | 53 |
class Elevator |
53 | 54 |
{ |
54 | 55 |
public: |
55 | 56 |
|
56 | 57 |
typedef Item Key; |
57 | 58 |
typedef int Value; |
58 | 59 |
|
59 | 60 |
private: |
60 | 61 |
|
61 | 62 |
typedef Item *Vit; |
62 | 63 |
typedef typename ItemSetTraits<Graph,Item>::template Map<Vit>::Type VitMap; |
63 | 64 |
typedef typename ItemSetTraits<Graph,Item>::template Map<int>::Type IntMap; |
64 | 65 |
|
65 | 66 |
const Graph &_g; |
66 | 67 |
int _max_level; |
67 | 68 |
int _item_num; |
68 | 69 |
VitMap _where; |
69 | 70 |
IntMap _level; |
70 | 71 |
std::vector<Item> _items; |
71 | 72 |
std::vector<Vit> _first; |
72 | 73 |
std::vector<Vit> _last_active; |
73 | 74 |
|
74 | 75 |
int _highest_active; |
75 | 76 |
|
76 | 77 |
void copy(Item i, Vit p) |
77 | 78 |
{ |
78 | 79 |
_where.set(*p=i,p); |
79 | 80 |
} |
80 | 81 |
void copy(Vit s, Vit p) |
81 | 82 |
{ |
82 | 83 |
if(s!=p) |
83 | 84 |
{ |
84 | 85 |
Item i=*s; |
85 | 86 |
*p=i; |
86 | 87 |
_where.set(i,p); |
87 | 88 |
} |
88 | 89 |
} |
89 | 90 |
void swap(Vit i, Vit j) |
90 | 91 |
{ |
91 | 92 |
Item ti=*i; |
92 | 93 |
Vit ct = _where[ti]; |
93 | 94 |
_where.set(ti,_where[*i=*j]); |
94 | 95 |
_where.set(*j,ct); |
95 | 96 |
*j=ti; |
96 | 97 |
} |
97 | 98 |
|
98 | 99 |
public: |
99 | 100 |
|
100 | 101 |
///Constructor with given maximum level. |
101 | 102 |
|
102 | 103 |
///Constructor with given maximum level. |
103 | 104 |
/// |
104 | 105 |
///\param graph The underlying graph. |
105 | 106 |
///\param max_level The maximum allowed level. |
106 | 107 |
///Set the range of the possible labels to <tt>[0..max_level]</tt>. |
107 | 108 |
Elevator(const Graph &graph,int max_level) : |
108 | 109 |
_g(graph), |
109 | 110 |
_max_level(max_level), |
110 | 111 |
_item_num(_max_level), |
111 | 112 |
_where(graph), |
112 | 113 |
_level(graph,0), |
113 | 114 |
_items(_max_level), |
114 | 115 |
_first(_max_level+2), |
115 | 116 |
_last_active(_max_level+2), |
116 | 117 |
_highest_active(-1) {} |
117 | 118 |
///Constructor. |
118 | 119 |
|
119 | 120 |
///Constructor. |
120 | 121 |
/// |
121 | 122 |
///\param graph The underlying graph. |
122 | 123 |
///Set the range of the possible labels to <tt>[0..max_level]</tt>, |
123 | 124 |
///where \c max_level is equal to the number of labeled items in the graph. |
124 | 125 |
Elevator(const Graph &graph) : |
125 | 126 |
_g(graph), |
126 | 127 |
_max_level(countItems<Graph, Item>(graph)), |
127 | 128 |
_item_num(_max_level), |
128 | 129 |
_where(graph), |
129 | 130 |
_level(graph,0), |
130 | 131 |
_items(_max_level), |
131 | 132 |
_first(_max_level+2), |
132 | 133 |
_last_active(_max_level+2), |
133 | 134 |
_highest_active(-1) |
134 | 135 |
{ |
135 | 136 |
} |
136 | 137 |
|
137 | 138 |
///Activate item \c i. |
138 | 139 |
|
139 | 140 |
///Activate item \c i. |
140 | 141 |
///\pre Item \c i shouldn't be active before. |
141 | 142 |
void activate(Item i) |
142 | 143 |
{ |
143 | 144 |
const int l=_level[i]; |
144 | 145 |
swap(_where[i],++_last_active[l]); |
145 | 146 |
if(l>_highest_active) _highest_active=l; |
146 | 147 |
} |
147 | 148 |
|
148 | 149 |
///Deactivate item \c i. |
149 | 150 |
|
150 | 151 |
///Deactivate item \c i. |
151 | 152 |
///\pre Item \c i must be active before. |
152 | 153 |
void deactivate(Item i) |
153 | 154 |
{ |
154 | 155 |
swap(_where[i],_last_active[_level[i]]--); |
155 | 156 |
while(_highest_active>=0 && |
156 | 157 |
_last_active[_highest_active]<_first[_highest_active]) |
157 | 158 |
_highest_active--; |
158 | 159 |
} |
159 | 160 |
|
160 | 161 |
///Query whether item \c i is active |
161 | 162 |
bool active(Item i) const { return _where[i]<=_last_active[_level[i]]; } |
162 | 163 |
|
163 | 164 |
///Return the level of item \c i. |
164 | 165 |
int operator[](Item i) const { return _level[i]; } |
165 | 166 |
|
166 | 167 |
///Return the number of items on level \c l. |
167 | 168 |
int onLevel(int l) const |
168 | 169 |
{ |
169 | 170 |
return _first[l+1]-_first[l]; |
170 | 171 |
} |
171 | 172 |
///Return true if level \c l is empty. |
172 | 173 |
bool emptyLevel(int l) const |
173 | 174 |
{ |
174 | 175 |
return _first[l+1]-_first[l]==0; |
175 | 176 |
} |
176 | 177 |
///Return the number of items above level \c l. |
177 | 178 |
int aboveLevel(int l) const |
178 | 179 |
{ |
179 | 180 |
return _first[_max_level+1]-_first[l+1]; |
180 | 181 |
} |
181 | 182 |
///Return the number of active items on level \c l. |
182 | 183 |
int activesOnLevel(int l) const |
183 | 184 |
{ |
184 | 185 |
return _last_active[l]-_first[l]+1; |
185 | 186 |
} |
186 | 187 |
///Return true if there is no active item on level \c l. |
187 | 188 |
bool activeFree(int l) const |
188 | 189 |
{ |
189 | 190 |
return _last_active[l]<_first[l]; |
190 | 191 |
} |
191 | 192 |
///Return the maximum allowed level. |
192 | 193 |
int maxLevel() const |
193 | 194 |
{ |
194 | 195 |
return _max_level; |
195 | 196 |
} |
196 | 197 |
|
197 | 198 |
///\name Highest Active Item |
198 | 199 |
///Functions for working with the highest level |
199 | 200 |
///active item. |
200 | 201 |
|
201 | 202 |
///@{ |
202 | 203 |
|
203 | 204 |
///Return a highest level active item. |
204 | 205 |
|
205 | 206 |
///Return a highest level active item or INVALID if there is no active |
206 | 207 |
///item. |
207 | 208 |
Item highestActive() const |
208 | 209 |
{ |
209 | 210 |
return _highest_active>=0?*_last_active[_highest_active]:INVALID; |
210 | 211 |
} |
211 | 212 |
|
212 | 213 |
///Return the highest active level. |
213 | 214 |
|
214 | 215 |
///Return the level of the highest active item or -1 if there is no active |
215 | 216 |
///item. |
216 | 217 |
int highestActiveLevel() const |
217 | 218 |
{ |
218 | 219 |
return _highest_active; |
219 | 220 |
} |
220 | 221 |
|
221 | 222 |
///Lift the highest active item by one. |
222 | 223 |
|
223 | 224 |
///Lift the item returned by highestActive() by one. |
224 | 225 |
/// |
225 | 226 |
void liftHighestActive() |
226 | 227 |
{ |
227 | 228 |
Item it = *_last_active[_highest_active]; |
228 | 229 |
_level.set(it,_level[it]+1); |
229 | 230 |
swap(_last_active[_highest_active]--,_last_active[_highest_active+1]); |
230 | 231 |
--_first[++_highest_active]; |
231 | 232 |
} |
232 | 233 |
|
233 | 234 |
///Lift the highest active item to the given level. |
234 | 235 |
|
235 | 236 |
///Lift the item returned by highestActive() to level \c new_level. |
236 | 237 |
/// |
237 | 238 |
///\warning \c new_level must be strictly higher |
238 | 239 |
///than the current level. |
239 | 240 |
/// |
240 | 241 |
void liftHighestActive(int new_level) |
241 | 242 |
{ |
242 | 243 |
const Item li = *_last_active[_highest_active]; |
243 | 244 |
|
244 | 245 |
copy(--_first[_highest_active+1],_last_active[_highest_active]--); |
245 | 246 |
for(int l=_highest_active+1;l<new_level;l++) |
246 | 247 |
{ |
247 | 248 |
copy(--_first[l+1],_first[l]); |
248 | 249 |
--_last_active[l]; |
249 | 250 |
} |
250 | 251 |
copy(li,_first[new_level]); |
251 | 252 |
_level.set(li,new_level); |
252 | 253 |
_highest_active=new_level; |
253 | 254 |
} |
254 | 255 |
|
255 | 256 |
///Lift the highest active item to the top level. |
256 | 257 |
|
257 | 258 |
///Lift the item returned by highestActive() to the top level and |
258 | 259 |
///deactivate it. |
259 | 260 |
void liftHighestActiveToTop() |
260 | 261 |
{ |
261 | 262 |
const Item li = *_last_active[_highest_active]; |
262 | 263 |
|
263 | 264 |
copy(--_first[_highest_active+1],_last_active[_highest_active]--); |
264 | 265 |
for(int l=_highest_active+1;l<_max_level;l++) |
265 | 266 |
{ |
266 | 267 |
copy(--_first[l+1],_first[l]); |
267 | 268 |
--_last_active[l]; |
268 | 269 |
} |
269 | 270 |
copy(li,_first[_max_level]); |
270 | 271 |
--_last_active[_max_level]; |
271 | 272 |
_level.set(li,_max_level); |
272 | 273 |
|
273 | 274 |
while(_highest_active>=0 && |
274 | 275 |
_last_active[_highest_active]<_first[_highest_active]) |
275 | 276 |
_highest_active--; |
276 | 277 |
} |
277 | 278 |
|
278 | 279 |
///@} |
279 | 280 |
|
280 | 281 |
///\name Active Item on Certain Level |
281 | 282 |
///Functions for working with the active items. |
282 | 283 |
|
283 | 284 |
///@{ |
284 | 285 |
|
285 | 286 |
///Return an active item on level \c l. |
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_SUURBALLE_H |
20 | 20 |
#define LEMON_SUURBALLE_H |
21 | 21 |
|
22 | 22 |
///\ingroup shortest_path |
23 | 23 |
///\file |
24 | 24 |
///\brief An algorithm for finding arc-disjoint paths between two |
25 | 25 |
/// nodes having minimum total length. |
26 | 26 |
|
27 | 27 |
#include <vector> |
28 | 28 |
#include <lemon/bin_heap.h> |
29 | 29 |
#include <lemon/path.h> |
30 |
#include <lemon/list_graph.h> |
|
31 |
#include <lemon/maps.h> |
|
30 | 32 |
|
31 | 33 |
namespace lemon { |
32 | 34 |
|
33 | 35 |
/// \addtogroup shortest_path |
34 | 36 |
/// @{ |
35 | 37 |
|
36 | 38 |
/// \brief Algorithm for finding arc-disjoint paths between two nodes |
37 | 39 |
/// having minimum total length. |
38 | 40 |
/// |
39 | 41 |
/// \ref lemon::Suurballe "Suurballe" implements an algorithm for |
40 | 42 |
/// finding arc-disjoint paths having minimum total length (cost) |
41 | 43 |
/// from a given source node to a given target node in a digraph. |
42 | 44 |
/// |
43 | 45 |
/// In fact, this implementation is the specialization of the |
44 | 46 |
/// \ref CapacityScaling "successive shortest path" algorithm. |
45 | 47 |
/// |
46 | 48 |
/// \tparam Digraph The digraph type the algorithm runs on. |
47 | 49 |
/// The default value is \c ListDigraph. |
48 | 50 |
/// \tparam LengthMap The type of the length (cost) map. |
49 | 51 |
/// The default value is <tt>Digraph::ArcMap<int></tt>. |
50 | 52 |
/// |
51 | 53 |
/// \warning Length values should be \e non-negative \e integers. |
52 | 54 |
/// |
53 | 55 |
/// \note For finding node-disjoint paths this algorithm can be used |
54 | 56 |
/// with \ref SplitNodes. |
55 | 57 |
#ifdef DOXYGEN |
56 | 58 |
template <typename Digraph, typename LengthMap> |
57 | 59 |
#else |
58 | 60 |
template < typename Digraph = ListDigraph, |
59 | 61 |
typename LengthMap = typename Digraph::template ArcMap<int> > |
60 | 62 |
#endif |
61 | 63 |
class Suurballe |
62 | 64 |
{ |
63 | 65 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
64 | 66 |
|
65 | 67 |
typedef typename LengthMap::Value Length; |
66 | 68 |
typedef ConstMap<Arc, int> ConstArcMap; |
67 | 69 |
typedef typename Digraph::template NodeMap<Arc> PredMap; |
68 | 70 |
|
69 | 71 |
public: |
70 | 72 |
|
71 | 73 |
/// The type of the flow map. |
72 | 74 |
typedef typename Digraph::template ArcMap<int> FlowMap; |
73 | 75 |
/// The type of the potential map. |
74 | 76 |
typedef typename Digraph::template NodeMap<Length> PotentialMap; |
75 | 77 |
/// The type of the path structures. |
76 | 78 |
typedef SimplePath<Digraph> Path; |
77 | 79 |
|
78 | 80 |
private: |
79 | 81 |
|
80 | 82 |
/// \brief Special implementation of the Dijkstra algorithm |
81 | 83 |
/// for finding shortest paths in the residual network. |
82 | 84 |
/// |
83 | 85 |
/// \ref ResidualDijkstra is a special implementation of the |
84 | 86 |
/// \ref Dijkstra algorithm for finding shortest paths in the |
85 | 87 |
/// residual network of the digraph with respect to the reduced arc |
86 | 88 |
/// lengths and modifying the node potentials according to the |
87 | 89 |
/// distance of the nodes. |
88 | 90 |
class ResidualDijkstra |
89 | 91 |
{ |
90 | 92 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
91 | 93 |
typedef BinHeap<Length, HeapCrossRef> Heap; |
92 | 94 |
|
93 | 95 |
private: |
94 | 96 |
|
95 | 97 |
// The digraph the algorithm runs on |
96 | 98 |
const Digraph &_graph; |
97 | 99 |
|
98 | 100 |
// The main maps |
99 | 101 |
const FlowMap &_flow; |
100 | 102 |
const LengthMap &_length; |
101 | 103 |
PotentialMap &_potential; |
102 | 104 |
|
103 | 105 |
// The distance map |
104 | 106 |
PotentialMap _dist; |
105 | 107 |
// The pred arc map |
106 | 108 |
PredMap &_pred; |
107 | 109 |
// The processed (i.e. permanently labeled) nodes |
108 | 110 |
std::vector<Node> _proc_nodes; |
109 | 111 |
|
110 | 112 |
Node _s; |
111 | 113 |
Node _t; |
112 | 114 |
|
113 | 115 |
public: |
114 | 116 |
|
115 | 117 |
/// Constructor. |
116 | 118 |
ResidualDijkstra( const Digraph &digraph, |
117 | 119 |
const FlowMap &flow, |
118 | 120 |
const LengthMap &length, |
119 | 121 |
PotentialMap &potential, |
120 | 122 |
PredMap &pred, |
121 | 123 |
Node s, Node t ) : |
122 | 124 |
_graph(digraph), _flow(flow), _length(length), _potential(potential), |
123 | 125 |
_dist(digraph), _pred(pred), _s(s), _t(t) {} |
124 | 126 |
|
125 | 127 |
/// \brief Run the algorithm. It returns \c true if a path is found |
126 | 128 |
/// from the source node to the target node. |
127 | 129 |
bool run() { |
128 | 130 |
HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP); |
129 | 131 |
Heap heap(heap_cross_ref); |
130 | 132 |
heap.push(_s, 0); |
131 | 133 |
_pred[_s] = INVALID; |
132 | 134 |
_proc_nodes.clear(); |
133 | 135 |
|
134 | 136 |
// Process nodes |
135 | 137 |
while (!heap.empty() && heap.top() != _t) { |
136 | 138 |
Node u = heap.top(), v; |
137 | 139 |
Length d = heap.prio() + _potential[u], nd; |
138 | 140 |
_dist[u] = heap.prio(); |
139 | 141 |
heap.pop(); |
140 | 142 |
_proc_nodes.push_back(u); |
141 | 143 |
|
142 | 144 |
// Traverse outgoing arcs |
143 | 145 |
for (OutArcIt e(_graph, u); e != INVALID; ++e) { |
144 | 146 |
if (_flow[e] == 0) { |
145 | 147 |
v = _graph.target(e); |
146 | 148 |
switch(heap.state(v)) { |
147 | 149 |
case Heap::PRE_HEAP: |
148 | 150 |
heap.push(v, d + _length[e] - _potential[v]); |
149 | 151 |
_pred[v] = e; |
150 | 152 |
break; |
151 | 153 |
case Heap::IN_HEAP: |
152 | 154 |
nd = d + _length[e] - _potential[v]; |
153 | 155 |
if (nd < heap[v]) { |
154 | 156 |
heap.decrease(v, nd); |
155 | 157 |
_pred[v] = e; |
156 | 158 |
} |
157 | 159 |
break; |
158 | 160 |
case Heap::POST_HEAP: |
159 | 161 |
break; |
160 | 162 |
} |
161 | 163 |
} |
162 | 164 |
} |
163 | 165 |
|
164 | 166 |
// Traverse incoming arcs |
165 | 167 |
for (InArcIt e(_graph, u); e != INVALID; ++e) { |
166 | 168 |
if (_flow[e] == 1) { |
167 | 169 |
v = _graph.source(e); |
168 | 170 |
switch(heap.state(v)) { |
169 | 171 |
case Heap::PRE_HEAP: |
170 | 172 |
heap.push(v, d - _length[e] - _potential[v]); |
171 | 173 |
_pred[v] = e; |
172 | 174 |
break; |
173 | 175 |
case Heap::IN_HEAP: |
174 | 176 |
nd = d - _length[e] - _potential[v]; |
175 | 177 |
if (nd < heap[v]) { |
176 | 178 |
heap.decrease(v, nd); |
177 | 179 |
_pred[v] = e; |
178 | 180 |
} |
179 | 181 |
break; |
180 | 182 |
case Heap::POST_HEAP: |
181 | 183 |
break; |
182 | 184 |
} |
183 | 185 |
} |
184 | 186 |
} |
185 | 187 |
} |
186 | 188 |
if (heap.empty()) return false; |
187 | 189 |
|
188 | 190 |
// Update potentials of processed nodes |
189 | 191 |
Length t_dist = heap.prio(); |
190 | 192 |
for (int i = 0; i < int(_proc_nodes.size()); ++i) |
191 | 193 |
_potential[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist; |
192 | 194 |
return true; |
193 | 195 |
} |
194 | 196 |
|
195 | 197 |
}; //class ResidualDijkstra |
196 | 198 |
|
197 | 199 |
private: |
198 | 200 |
|
199 | 201 |
// The digraph the algorithm runs on |
200 | 202 |
const Digraph &_graph; |
201 | 203 |
// The length map |
202 | 204 |
const LengthMap &_length; |
203 | 205 |
|
204 | 206 |
// Arc map of the current flow |
205 | 207 |
FlowMap *_flow; |
206 | 208 |
bool _local_flow; |
207 | 209 |
// Node map of the current potentials |
208 | 210 |
PotentialMap *_potential; |
209 | 211 |
bool _local_potential; |
210 | 212 |
|
211 | 213 |
// The source node |
212 | 214 |
Node _source; |
213 | 215 |
// The target node |
214 | 216 |
Node _target; |
215 | 217 |
|
216 | 218 |
// Container to store the found paths |
217 | 219 |
std::vector< SimplePath<Digraph> > paths; |
218 | 220 |
int _path_num; |
219 | 221 |
|
220 | 222 |
// The pred arc map |
221 | 223 |
PredMap _pred; |
222 | 224 |
// Implementation of the Dijkstra algorithm for finding augmenting |
223 | 225 |
// shortest paths in the residual network |
224 | 226 |
ResidualDijkstra *_dijkstra; |
225 | 227 |
|
226 | 228 |
public: |
227 | 229 |
|
228 | 230 |
/// \brief Constructor. |
229 | 231 |
/// |
230 | 232 |
/// Constructor. |
231 | 233 |
/// |
232 | 234 |
/// \param digraph The digraph the algorithm runs on. |
233 | 235 |
/// \param length The length (cost) values of the arcs. |
234 | 236 |
/// \param s The source node. |
235 | 237 |
/// \param t The target node. |
236 | 238 |
Suurballe( const Digraph &digraph, |
237 | 239 |
const LengthMap &length, |
238 | 240 |
Node s, Node t ) : |
239 | 241 |
_graph(digraph), _length(length), _flow(0), _local_flow(false), |
240 | 242 |
_potential(0), _local_potential(false), _source(s), _target(t), |
241 | 243 |
_pred(digraph) {} |
242 | 244 |
|
243 | 245 |
/// Destructor. |
244 | 246 |
~Suurballe() { |
245 | 247 |
if (_local_flow) delete _flow; |
246 | 248 |
if (_local_potential) delete _potential; |
247 | 249 |
delete _dijkstra; |
248 | 250 |
} |
249 | 251 |
|
250 | 252 |
/// \brief Set the flow map. |
251 | 253 |
/// |
252 | 254 |
/// This function sets the flow map. |
253 | 255 |
/// |
254 | 256 |
/// The found flow contains only 0 and 1 values. It is the union of |
255 | 257 |
/// the found arc-disjoint paths. |
256 | 258 |
/// |
257 | 259 |
/// \return \c (*this) |
258 | 260 |
Suurballe& flowMap(FlowMap &map) { |
259 | 261 |
if (_local_flow) { |
260 | 262 |
delete _flow; |
261 | 263 |
_local_flow = false; |
262 | 264 |
} |
263 | 265 |
_flow = ↦ |
264 | 266 |
return *this; |
265 | 267 |
} |
266 | 268 |
|
267 | 269 |
/// \brief Set the potential map. |
268 | 270 |
/// |
269 | 271 |
/// This function sets the potential map. |
270 | 272 |
/// |
271 | 273 |
/// The potentials provide the dual solution of the underlying |
272 | 274 |
/// minimum cost flow problem. |
273 | 275 |
/// |
274 | 276 |
/// \return \c (*this) |
275 | 277 |
Suurballe& potentialMap(PotentialMap &map) { |
276 | 278 |
if (_local_potential) { |
277 | 279 |
delete _potential; |
278 | 280 |
_local_potential = false; |
279 | 281 |
} |
280 | 282 |
_potential = ↦ |
281 | 283 |
return *this; |
282 | 284 |
} |
283 | 285 |
|
284 | 286 |
/// \name Execution control |
285 | 287 |
/// The simplest way to execute the algorithm is to call the run() |
0 comments (0 inline)