0
73
18
1 |
%%%%% Defining LEMON %%%%% |
|
2 |
|
|
3 |
@misc{lemon, |
|
4 |
key = {LEMON}, |
|
5 |
title = {{LEMON} -- {L}ibrary for {E}fficient {M}odeling and |
|
6 |
{O}ptimization in {N}etworks}, |
|
7 |
howpublished = {\url{http://lemon.cs.elte.hu/}}, |
|
8 |
year = 2009 |
|
9 |
} |
|
10 |
|
|
11 |
@misc{egres, |
|
12 |
key = {EGRES}, |
|
13 |
title = {{EGRES} -- {E}gerv{\'a}ry {R}esearch {G}roup on |
|
14 |
{C}ombinatorial {O}ptimization}, |
|
15 |
url = {http://www.cs.elte.hu/egres/} |
|
16 |
} |
|
17 |
|
|
18 |
@misc{coinor, |
|
19 |
key = {COIN-OR}, |
|
20 |
title = {{COIN-OR} -- {C}omputational {I}nfrastructure for |
|
21 |
{O}perations {R}esearch}, |
|
22 |
url = {http://www.coin-or.org/} |
|
23 |
} |
|
24 |
|
|
25 |
|
|
26 |
%%%%% Other libraries %%%%%% |
|
27 |
|
|
28 |
@misc{boost, |
|
29 |
key = {Boost}, |
|
30 |
title = {{B}oost {C++} {L}ibraries}, |
|
31 |
url = {http://www.boost.org/} |
|
32 |
} |
|
33 |
|
|
34 |
@book{bglbook, |
|
35 |
author = {Jeremy G. Siek and Lee-Quan Lee and Andrew |
|
36 |
Lumsdaine}, |
|
37 |
title = {The Boost Graph Library: User Guide and Reference |
|
38 |
Manual}, |
|
39 |
publisher = {Addison-Wesley}, |
|
40 |
year = 2002 |
|
41 |
} |
|
42 |
|
|
43 |
@misc{leda, |
|
44 |
key = {LEDA}, |
|
45 |
title = {{LEDA} -- {L}ibrary of {E}fficient {D}ata {T}ypes and |
|
46 |
{A}lgorithms}, |
|
47 |
url = {http://www.algorithmic-solutions.com/} |
|
48 |
} |
|
49 |
|
|
50 |
@book{ledabook, |
|
51 |
author = {Kurt Mehlhorn and Stefan N{\"a}her}, |
|
52 |
title = {{LEDA}: {A} platform for combinatorial and geometric |
|
53 |
computing}, |
|
54 |
isbn = {0-521-56329-1}, |
|
55 |
publisher = {Cambridge University Press}, |
|
56 |
address = {New York, NY, USA}, |
|
57 |
year = 1999 |
|
58 |
} |
|
59 |
|
|
60 |
|
|
61 |
%%%%% Tools that LEMON depends on %%%%% |
|
62 |
|
|
63 |
@misc{cmake, |
|
64 |
key = {CMake}, |
|
65 |
title = {{CMake} -- {C}ross {P}latform {M}ake}, |
|
66 |
url = {http://www.cmake.org/} |
|
67 |
} |
|
68 |
|
|
69 |
@misc{doxygen, |
|
70 |
key = {Doxygen}, |
|
71 |
title = {{Doxygen} -- {S}ource code documentation generator |
|
72 |
tool}, |
|
73 |
url = {http://www.doxygen.org/} |
|
74 |
} |
|
75 |
|
|
76 |
|
|
77 |
%%%%% LP/MIP libraries %%%%% |
|
78 |
|
|
79 |
@misc{glpk, |
|
80 |
key = {GLPK}, |
|
81 |
title = {{GLPK} -- {GNU} {L}inear {P}rogramming {K}it}, |
|
82 |
url = {http://www.gnu.org/software/glpk/} |
|
83 |
} |
|
84 |
|
|
85 |
@misc{clp, |
|
86 |
key = {Clp}, |
|
87 |
title = {{Clp} -- {Coin-Or} {L}inear {P}rogramming}, |
|
88 |
url = {http://projects.coin-or.org/Clp/} |
|
89 |
} |
|
90 |
|
|
91 |
@misc{cbc, |
|
92 |
key = {Cbc}, |
|
93 |
title = {{Cbc} -- {Coin-Or} {B}ranch and {C}ut}, |
|
94 |
url = {http://projects.coin-or.org/Cbc/} |
|
95 |
} |
|
96 |
|
|
97 |
@misc{cplex, |
|
98 |
key = {CPLEX}, |
|
99 |
title = {{ILOG} {CPLEX}}, |
|
100 |
url = {http://www.ilog.com/} |
|
101 |
} |
|
102 |
|
|
103 |
@misc{soplex, |
|
104 |
key = {SoPlex}, |
|
105 |
title = {{SoPlex} -- {T}he {S}equential {O}bject-{O}riented |
|
106 |
{S}implex}, |
|
107 |
url = {http://soplex.zib.de/} |
|
108 |
} |
|
109 |
|
|
110 |
|
|
111 |
%%%%% General books %%%%% |
|
112 |
|
|
113 |
@book{amo93networkflows, |
|
114 |
author = {Ravindra K. Ahuja and Thomas L. Magnanti and James |
|
115 |
B. Orlin}, |
|
116 |
title = {Network Flows: Theory, Algorithms, and Applications}, |
|
117 |
publisher = {Prentice-Hall, Inc.}, |
|
118 |
year = 1993, |
|
119 |
month = feb, |
|
120 |
isbn = {978-0136175490} |
|
121 |
} |
|
122 |
|
|
123 |
@book{schrijver03combinatorial, |
|
124 |
author = {Alexander Schrijver}, |
|
125 |
title = {Combinatorial Optimization: Polyhedra and Efficiency}, |
|
126 |
publisher = {Springer-Verlag}, |
|
127 |
year = 2003, |
|
128 |
isbn = {978-3540443896} |
|
129 |
} |
|
130 |
|
|
131 |
@book{clrs01algorithms, |
|
132 |
author = {Thomas H. Cormen and Charles E. Leiserson and Ronald |
|
133 |
L. Rivest and Clifford Stein}, |
|
134 |
title = {Introduction to Algorithms}, |
|
135 |
publisher = {The MIT Press}, |
|
136 |
year = 2001, |
|
137 |
edition = {2nd} |
|
138 |
} |
|
139 |
|
|
140 |
@book{stroustrup00cpp, |
|
141 |
author = {Bjarne Stroustrup}, |
|
142 |
title = {The C++ Programming Language}, |
|
143 |
edition = {3rd}, |
|
144 |
publisher = {Addison-Wesley Professional}, |
|
145 |
isbn = 0201700735, |
|
146 |
month = {February}, |
|
147 |
year = 2000 |
|
148 |
} |
|
149 |
|
|
150 |
|
|
151 |
%%%%% Maximum flow algorithms %%%%% |
|
152 |
|
|
153 |
@article{edmondskarp72theoretical, |
|
154 |
author = {Jack Edmonds and Richard M. Karp}, |
|
155 |
title = {Theoretical improvements in algorithmic efficiency |
|
156 |
for network flow problems}, |
|
157 |
journal = {Journal of the ACM}, |
|
158 |
year = 1972, |
|
159 |
volume = 19, |
|
160 |
number = 2, |
|
161 |
pages = {248-264} |
|
162 |
} |
|
163 |
|
|
164 |
@article{goldberg88newapproach, |
|
165 |
author = {Andrew V. Goldberg and Robert E. Tarjan}, |
|
166 |
title = {A new approach to the maximum flow problem}, |
|
167 |
journal = {Journal of the ACM}, |
|
168 |
year = 1988, |
|
169 |
volume = 35, |
|
170 |
number = 4, |
|
171 |
pages = {921-940} |
|
172 |
} |
|
173 |
|
|
174 |
@article{dinic70algorithm, |
|
175 |
author = {E. A. Dinic}, |
|
176 |
title = {Algorithm for solution of a problem of maximum flow |
|
177 |
in a network with power estimation}, |
|
178 |
journal = {Soviet Math. Doklady}, |
|
179 |
year = 1970, |
|
180 |
volume = 11, |
|
181 |
pages = {1277-1280} |
|
182 |
} |
|
183 |
|
|
184 |
@article{goldberg08partial, |
|
185 |
author = {Andrew V. Goldberg}, |
|
186 |
title = {The Partial Augment-Relabel Algorithm for the |
|
187 |
Maximum Flow Problem}, |
|
188 |
journal = {16th Annual European Symposium on Algorithms}, |
|
189 |
year = 2008, |
|
190 |
pages = {466-477} |
|
191 |
} |
|
192 |
|
|
193 |
@article{sleator83dynamic, |
|
194 |
author = {Daniel D. Sleator and Robert E. Tarjan}, |
|
195 |
title = {A data structure for dynamic trees}, |
|
196 |
journal = {Journal of Computer and System Sciences}, |
|
197 |
year = 1983, |
|
198 |
volume = 26, |
|
199 |
number = 3, |
|
200 |
pages = {362-391} |
|
201 |
} |
|
202 |
|
|
203 |
|
|
204 |
%%%%% Minimum mean cycle algorithms %%%%% |
|
205 |
|
|
206 |
@article{karp78characterization, |
|
207 |
author = {Richard M. Karp}, |
|
208 |
title = {A characterization of the minimum cycle mean in a |
|
209 |
digraph}, |
|
210 |
journal = {Discrete Math.}, |
|
211 |
year = 1978, |
|
212 |
volume = 23, |
|
213 |
pages = {309-311} |
|
214 |
} |
|
215 |
|
|
216 |
@article{dasdan98minmeancycle, |
|
217 |
author = {Ali Dasdan and Rajesh K. Gupta}, |
|
218 |
title = {Faster Maximum and Minimum Mean Cycle Alogrithms for |
|
219 |
System Performance Analysis}, |
|
220 |
journal = {IEEE Transactions on Computer-Aided Design of |
|
221 |
Integrated Circuits and Systems}, |
|
222 |
year = 1998, |
|
223 |
volume = 17, |
|
224 |
number = 10, |
|
225 |
pages = {889-899} |
|
226 |
} |
|
227 |
|
|
228 |
|
|
229 |
%%%%% Minimum cost flow algorithms %%%%% |
|
230 |
|
|
231 |
@article{klein67primal, |
|
232 |
author = {Morton Klein}, |
|
233 |
title = {A primal method for minimal cost flows with |
|
234 |
applications to the assignment and transportation |
|
235 |
problems}, |
|
236 |
journal = {Management Science}, |
|
237 |
year = 1967, |
|
238 |
volume = 14, |
|
239 |
pages = {205-220} |
|
240 |
} |
|
241 |
|
|
242 |
@article{goldberg89cyclecanceling, |
|
243 |
author = {Andrew V. Goldberg and Robert E. Tarjan}, |
|
244 |
title = {Finding minimum-cost circulations by canceling |
|
245 |
negative cycles}, |
|
246 |
journal = {Journal of the ACM}, |
|
247 |
year = 1989, |
|
248 |
volume = 36, |
|
249 |
number = 4, |
|
250 |
pages = {873-886} |
|
251 |
} |
|
252 |
|
|
253 |
@article{goldberg90approximation, |
|
254 |
author = {Andrew V. Goldberg and Robert E. Tarjan}, |
|
255 |
title = {Finding Minimum-Cost Circulations by Successive |
|
256 |
Approximation}, |
|
257 |
journal = {Mathematics of Operations Research}, |
|
258 |
year = 1990, |
|
259 |
volume = 15, |
|
260 |
number = 3, |
|
261 |
pages = {430-466} |
|
262 |
} |
|
263 |
|
|
264 |
@article{goldberg97efficient, |
|
265 |
author = {Andrew V. Goldberg}, |
|
266 |
title = {An Efficient Implementation of a Scaling |
|
267 |
Minimum-Cost Flow Algorithm}, |
|
268 |
journal = {Journal of Algorithms}, |
|
269 |
year = 1997, |
|
270 |
volume = 22, |
|
271 |
number = 1, |
|
272 |
pages = {1-29} |
|
273 |
} |
|
274 |
|
|
275 |
@article{bunnagel98efficient, |
|
276 |
author = {Ursula B{\"u}nnagel and Bernhard Korte and Jens |
|
277 |
Vygen}, |
|
278 |
title = {Efficient implementation of the {G}oldberg-{T}arjan |
|
279 |
minimum-cost flow algorithm}, |
|
280 |
journal = {Optimization Methods and Software}, |
|
281 |
year = 1998, |
|
282 |
volume = 10, |
|
283 |
pages = {157-174} |
|
284 |
} |
|
285 |
|
|
286 |
@book{dantzig63linearprog, |
|
287 |
author = {George B. Dantzig}, |
|
288 |
title = {Linear Programming and Extensions}, |
|
289 |
publisher = {Princeton University Press}, |
|
290 |
year = 1963 |
|
291 |
} |
|
292 |
|
|
293 |
@mastersthesis{kellyoneill91netsimplex, |
|
294 |
author = {Damian J. Kelly and Garrett M. O'Neill}, |
|
295 |
title = {The Minimum Cost Flow Problem and The Network |
|
296 |
Simplex Method}, |
|
297 |
school = {University College}, |
|
298 |
address = {Dublin, Ireland}, |
|
299 |
year = 1991, |
|
300 |
month = sep, |
|
301 |
} |
1 |
/* -*- C++ -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2008 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_BELLMAN_FORD_H |
|
20 |
#define LEMON_BELLMAN_FORD_H |
|
21 |
|
|
22 |
/// \ingroup shortest_path |
|
23 |
/// \file |
|
24 |
/// \brief Bellman-Ford algorithm. |
|
25 |
|
|
26 |
#include <lemon/list_graph.h> |
|
27 |
#include <lemon/bits/path_dump.h> |
|
28 |
#include <lemon/core.h> |
|
29 |
#include <lemon/error.h> |
|
30 |
#include <lemon/maps.h> |
|
31 |
#include <lemon/path.h> |
|
32 |
|
|
33 |
#include <limits> |
|
34 |
|
|
35 |
namespace lemon { |
|
36 |
|
|
37 |
/// \brief Default OperationTraits for the BellmanFord algorithm class. |
|
38 |
/// |
|
39 |
/// This operation traits class defines all computational operations |
|
40 |
/// and constants that are used in the Bellman-Ford algorithm. |
|
41 |
/// The default implementation is based on the \c numeric_limits class. |
|
42 |
/// If the numeric type does not have infinity value, then the maximum |
|
43 |
/// value is used as extremal infinity value. |
|
44 |
template < |
|
45 |
typename V, |
|
46 |
bool has_inf = std::numeric_limits<V>::has_infinity> |
|
47 |
struct BellmanFordDefaultOperationTraits { |
|
48 |
/// \e |
|
49 |
typedef V Value; |
|
50 |
/// \brief Gives back the zero value of the type. |
|
51 |
static Value zero() { |
|
52 |
return static_cast<Value>(0); |
|
53 |
} |
|
54 |
/// \brief Gives back the positive infinity value of the type. |
|
55 |
static Value infinity() { |
|
56 |
return std::numeric_limits<Value>::infinity(); |
|
57 |
} |
|
58 |
/// \brief Gives back the sum of the given two elements. |
|
59 |
static Value plus(const Value& left, const Value& right) { |
|
60 |
return left + right; |
|
61 |
} |
|
62 |
/// \brief Gives back \c true only if the first value is less than |
|
63 |
/// the second. |
|
64 |
static bool less(const Value& left, const Value& right) { |
|
65 |
return left < right; |
|
66 |
} |
|
67 |
}; |
|
68 |
|
|
69 |
template <typename V> |
|
70 |
struct BellmanFordDefaultOperationTraits<V, false> { |
|
71 |
typedef V Value; |
|
72 |
static Value zero() { |
|
73 |
return static_cast<Value>(0); |
|
74 |
} |
|
75 |
static Value infinity() { |
|
76 |
return std::numeric_limits<Value>::max(); |
|
77 |
} |
|
78 |
static Value plus(const Value& left, const Value& right) { |
|
79 |
if (left == infinity() || right == infinity()) return infinity(); |
|
80 |
return left + right; |
|
81 |
} |
|
82 |
static bool less(const Value& left, const Value& right) { |
|
83 |
return left < right; |
|
84 |
} |
|
85 |
}; |
|
86 |
|
|
87 |
/// \brief Default traits class of BellmanFord class. |
|
88 |
/// |
|
89 |
/// Default traits class of BellmanFord class. |
|
90 |
/// \param GR The type of the digraph. |
|
91 |
/// \param LEN The type of the length map. |
|
92 |
template<typename GR, typename LEN> |
|
93 |
struct BellmanFordDefaultTraits { |
|
94 |
/// The type of the digraph the algorithm runs on. |
|
95 |
typedef GR Digraph; |
|
96 |
|
|
97 |
/// \brief The type of the map that stores the arc lengths. |
|
98 |
/// |
|
99 |
/// The type of the map that stores the arc lengths. |
|
100 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
|
101 |
typedef LEN LengthMap; |
|
102 |
|
|
103 |
/// The type of the arc lengths. |
|
104 |
typedef typename LEN::Value Value; |
|
105 |
|
|
106 |
/// \brief Operation traits for Bellman-Ford algorithm. |
|
107 |
/// |
|
108 |
/// It defines the used operations and the infinity value for the |
|
109 |
/// given \c Value type. |
|
110 |
/// \see BellmanFordDefaultOperationTraits |
|
111 |
typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
|
112 |
|
|
113 |
/// \brief The type of the map that stores the last arcs of the |
|
114 |
/// shortest paths. |
|
115 |
/// |
|
116 |
/// The type of the map that stores the last |
|
117 |
/// arcs of the shortest paths. |
|
118 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
119 |
typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
|
120 |
|
|
121 |
/// \brief Instantiates a \c PredMap. |
|
122 |
/// |
|
123 |
/// This function instantiates a \ref PredMap. |
|
124 |
/// \param g is the digraph to which we would like to define the |
|
125 |
/// \ref PredMap. |
|
126 |
static PredMap *createPredMap(const GR& g) { |
|
127 |
return new PredMap(g); |
|
128 |
} |
|
129 |
|
|
130 |
/// \brief The type of the map that stores the distances of the nodes. |
|
131 |
/// |
|
132 |
/// The type of the map that stores the distances of the nodes. |
|
133 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
134 |
typedef typename GR::template NodeMap<typename LEN::Value> DistMap; |
|
135 |
|
|
136 |
/// \brief Instantiates a \c DistMap. |
|
137 |
/// |
|
138 |
/// This function instantiates a \ref DistMap. |
|
139 |
/// \param g is the digraph to which we would like to define the |
|
140 |
/// \ref DistMap. |
|
141 |
static DistMap *createDistMap(const GR& g) { |
|
142 |
return new DistMap(g); |
|
143 |
} |
|
144 |
|
|
145 |
}; |
|
146 |
|
|
147 |
/// \brief %BellmanFord algorithm class. |
|
148 |
/// |
|
149 |
/// \ingroup shortest_path |
|
150 |
/// This class provides an efficient implementation of the Bellman-Ford |
|
151 |
/// algorithm. The maximum time complexity of the algorithm is |
|
152 |
/// <tt>O(ne)</tt>. |
|
153 |
/// |
|
154 |
/// The Bellman-Ford algorithm solves the single-source shortest path |
|
155 |
/// problem when the arcs can have negative lengths, but the digraph |
|
156 |
/// should not contain directed cycles with negative total length. |
|
157 |
/// If all arc costs are non-negative, consider to use the Dijkstra |
|
158 |
/// algorithm instead, since it is more efficient. |
|
159 |
/// |
|
160 |
/// The arc lengths are passed to the algorithm using a |
|
161 |
/// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any |
|
162 |
/// kind of length. The type of the length values is determined by the |
|
163 |
/// \ref concepts::ReadMap::Value "Value" type of the length map. |
|
164 |
/// |
|
165 |
/// There is also a \ref bellmanFord() "function-type interface" for the |
|
166 |
/// Bellman-Ford algorithm, which is convenient in the simplier cases and |
|
167 |
/// it can be used easier. |
|
168 |
/// |
|
169 |
/// \tparam GR The type of the digraph the algorithm runs on. |
|
170 |
/// The default type is \ref ListDigraph. |
|
171 |
/// \tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies |
|
172 |
/// the lengths of the arcs. The default map type is |
|
173 |
/// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
|
174 |
#ifdef DOXYGEN |
|
175 |
template <typename GR, typename LEN, typename TR> |
|
176 |
#else |
|
177 |
template <typename GR=ListDigraph, |
|
178 |
typename LEN=typename GR::template ArcMap<int>, |
|
179 |
typename TR=BellmanFordDefaultTraits<GR,LEN> > |
|
180 |
#endif |
|
181 |
class BellmanFord { |
|
182 |
public: |
|
183 |
|
|
184 |
///The type of the underlying digraph. |
|
185 |
typedef typename TR::Digraph Digraph; |
|
186 |
|
|
187 |
/// \brief The type of the arc lengths. |
|
188 |
typedef typename TR::LengthMap::Value Value; |
|
189 |
/// \brief The type of the map that stores the arc lengths. |
|
190 |
typedef typename TR::LengthMap LengthMap; |
|
191 |
/// \brief The type of the map that stores the last |
|
192 |
/// arcs of the shortest paths. |
|
193 |
typedef typename TR::PredMap PredMap; |
|
194 |
/// \brief The type of the map that stores the distances of the nodes. |
|
195 |
typedef typename TR::DistMap DistMap; |
|
196 |
/// The type of the paths. |
|
197 |
typedef PredMapPath<Digraph, PredMap> Path; |
|
198 |
///\brief The \ref BellmanFordDefaultOperationTraits |
|
199 |
/// "operation traits class" of the algorithm. |
|
200 |
typedef typename TR::OperationTraits OperationTraits; |
|
201 |
|
|
202 |
///The \ref BellmanFordDefaultTraits "traits class" of the algorithm. |
|
203 |
typedef TR Traits; |
|
204 |
|
|
205 |
private: |
|
206 |
|
|
207 |
typedef typename Digraph::Node Node; |
|
208 |
typedef typename Digraph::NodeIt NodeIt; |
|
209 |
typedef typename Digraph::Arc Arc; |
|
210 |
typedef typename Digraph::OutArcIt OutArcIt; |
|
211 |
|
|
212 |
// Pointer to the underlying digraph. |
|
213 |
const Digraph *_gr; |
|
214 |
// Pointer to the length map |
|
215 |
const LengthMap *_length; |
|
216 |
// Pointer to the map of predecessors arcs. |
|
217 |
PredMap *_pred; |
|
218 |
// Indicates if _pred is locally allocated (true) or not. |
|
219 |
bool _local_pred; |
|
220 |
// Pointer to the map of distances. |
|
221 |
DistMap *_dist; |
|
222 |
// Indicates if _dist is locally allocated (true) or not. |
|
223 |
bool _local_dist; |
|
224 |
|
|
225 |
typedef typename Digraph::template NodeMap<bool> MaskMap; |
|
226 |
MaskMap *_mask; |
|
227 |
|
|
228 |
std::vector<Node> _process; |
|
229 |
|
|
230 |
// Creates the maps if necessary. |
|
231 |
void create_maps() { |
|
232 |
if(!_pred) { |
|
233 |
_local_pred = true; |
|
234 |
_pred = Traits::createPredMap(*_gr); |
|
235 |
} |
|
236 |
if(!_dist) { |
|
237 |
_local_dist = true; |
|
238 |
_dist = Traits::createDistMap(*_gr); |
|
239 |
} |
|
240 |
_mask = new MaskMap(*_gr, false); |
|
241 |
} |
|
242 |
|
|
243 |
public : |
|
244 |
|
|
245 |
typedef BellmanFord Create; |
|
246 |
|
|
247 |
/// \name Named Template Parameters |
|
248 |
|
|
249 |
///@{ |
|
250 |
|
|
251 |
template <class T> |
|
252 |
struct SetPredMapTraits : public Traits { |
|
253 |
typedef T PredMap; |
|
254 |
static PredMap *createPredMap(const Digraph&) { |
|
255 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
|
256 |
return 0; // ignore warnings |
|
257 |
} |
|
258 |
}; |
|
259 |
|
|
260 |
/// \brief \ref named-templ-param "Named parameter" for setting |
|
261 |
/// \c PredMap type. |
|
262 |
/// |
|
263 |
/// \ref named-templ-param "Named parameter" for setting |
|
264 |
/// \c PredMap type. |
|
265 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
266 |
template <class T> |
|
267 |
struct SetPredMap |
|
268 |
: public BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > { |
|
269 |
typedef BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > Create; |
|
270 |
}; |
|
271 |
|
|
272 |
template <class T> |
|
273 |
struct SetDistMapTraits : public Traits { |
|
274 |
typedef T DistMap; |
|
275 |
static DistMap *createDistMap(const Digraph&) { |
|
276 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
|
277 |
return 0; // ignore warnings |
|
278 |
} |
|
279 |
}; |
|
280 |
|
|
281 |
/// \brief \ref named-templ-param "Named parameter" for setting |
|
282 |
/// \c DistMap type. |
|
283 |
/// |
|
284 |
/// \ref named-templ-param "Named parameter" for setting |
|
285 |
/// \c DistMap type. |
|
286 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
287 |
template <class T> |
|
288 |
struct SetDistMap |
|
289 |
: public BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > { |
|
290 |
typedef BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > Create; |
|
291 |
}; |
|
292 |
|
|
293 |
template <class T> |
|
294 |
struct SetOperationTraitsTraits : public Traits { |
|
295 |
typedef T OperationTraits; |
|
296 |
}; |
|
297 |
|
|
298 |
/// \brief \ref named-templ-param "Named parameter" for setting |
|
299 |
/// \c OperationTraits type. |
|
300 |
/// |
|
301 |
/// \ref named-templ-param "Named parameter" for setting |
|
302 |
/// \c OperationTraits type. |
|
303 |
/// For more information, see \ref BellmanFordDefaultOperationTraits. |
|
304 |
template <class T> |
|
305 |
struct SetOperationTraits |
|
306 |
: public BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> > { |
|
307 |
typedef BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> > |
|
308 |
Create; |
|
309 |
}; |
|
310 |
|
|
311 |
///@} |
|
312 |
|
|
313 |
protected: |
|
314 |
|
|
315 |
BellmanFord() {} |
|
316 |
|
|
317 |
public: |
|
318 |
|
|
319 |
/// \brief Constructor. |
|
320 |
/// |
|
321 |
/// Constructor. |
|
322 |
/// \param g The digraph the algorithm runs on. |
|
323 |
/// \param length The length map used by the algorithm. |
|
324 |
BellmanFord(const Digraph& g, const LengthMap& length) : |
|
325 |
_gr(&g), _length(&length), |
|
326 |
_pred(0), _local_pred(false), |
|
327 |
_dist(0), _local_dist(false), _mask(0) {} |
|
328 |
|
|
329 |
///Destructor. |
|
330 |
~BellmanFord() { |
|
331 |
if(_local_pred) delete _pred; |
|
332 |
if(_local_dist) delete _dist; |
|
333 |
if(_mask) delete _mask; |
|
334 |
} |
|
335 |
|
|
336 |
/// \brief Sets the length map. |
|
337 |
/// |
|
338 |
/// Sets the length map. |
|
339 |
/// \return <tt>(*this)</tt> |
|
340 |
BellmanFord &lengthMap(const LengthMap &map) { |
|
341 |
_length = ↦ |
|
342 |
return *this; |
|
343 |
} |
|
344 |
|
|
345 |
/// \brief Sets the map that stores the predecessor arcs. |
|
346 |
/// |
|
347 |
/// Sets the map that stores the predecessor arcs. |
|
348 |
/// If you don't use this function before calling \ref run() |
|
349 |
/// or \ref init(), an instance will be allocated automatically. |
|
350 |
/// The destructor deallocates this automatically allocated map, |
|
351 |
/// of course. |
|
352 |
/// \return <tt>(*this)</tt> |
|
353 |
BellmanFord &predMap(PredMap &map) { |
|
354 |
if(_local_pred) { |
|
355 |
delete _pred; |
|
356 |
_local_pred=false; |
|
357 |
} |
|
358 |
_pred = ↦ |
|
359 |
return *this; |
|
360 |
} |
|
361 |
|
|
362 |
/// \brief Sets the map that stores the distances of the nodes. |
|
363 |
/// |
|
364 |
/// Sets the map that stores the distances of the nodes calculated |
|
365 |
/// by the algorithm. |
|
366 |
/// If you don't use this function before calling \ref run() |
|
367 |
/// or \ref init(), an instance will be allocated automatically. |
|
368 |
/// The destructor deallocates this automatically allocated map, |
|
369 |
/// of course. |
|
370 |
/// \return <tt>(*this)</tt> |
|
371 |
BellmanFord &distMap(DistMap &map) { |
|
372 |
if(_local_dist) { |
|
373 |
delete _dist; |
|
374 |
_local_dist=false; |
|
375 |
} |
|
376 |
_dist = ↦ |
|
377 |
return *this; |
|
378 |
} |
|
379 |
|
|
380 |
/// \name Execution Control |
|
381 |
/// The simplest way to execute the Bellman-Ford algorithm is to use |
|
382 |
/// one of the member functions called \ref run().\n |
|
383 |
/// If you need better control on the execution, you have to call |
|
384 |
/// \ref init() first, then you can add several source nodes |
|
385 |
/// with \ref addSource(). Finally the actual path computation can be |
|
386 |
/// performed with \ref start(), \ref checkedStart() or |
|
387 |
/// \ref limitedStart(). |
|
388 |
|
|
389 |
///@{ |
|
390 |
|
|
391 |
/// \brief Initializes the internal data structures. |
|
392 |
/// |
|
393 |
/// Initializes the internal data structures. The optional parameter |
|
394 |
/// is the initial distance of each node. |
|
395 |
void init(const Value value = OperationTraits::infinity()) { |
|
396 |
create_maps(); |
|
397 |
for (NodeIt it(*_gr); it != INVALID; ++it) { |
|
398 |
_pred->set(it, INVALID); |
|
399 |
_dist->set(it, value); |
|
400 |
} |
|
401 |
_process.clear(); |
|
402 |
if (OperationTraits::less(value, OperationTraits::infinity())) { |
|
403 |
for (NodeIt it(*_gr); it != INVALID; ++it) { |
|
404 |
_process.push_back(it); |
|
405 |
_mask->set(it, true); |
|
406 |
} |
|
407 |
} |
|
408 |
} |
|
409 |
|
|
410 |
/// \brief Adds a new source node. |
|
411 |
/// |
|
412 |
/// This function adds a new source node. The optional second parameter |
|
413 |
/// is the initial distance of the node. |
|
414 |
void addSource(Node source, Value dst = OperationTraits::zero()) { |
|
415 |
_dist->set(source, dst); |
|
416 |
if (!(*_mask)[source]) { |
|
417 |
_process.push_back(source); |
|
418 |
_mask->set(source, true); |
|
419 |
} |
|
420 |
} |
|
421 |
|
|
422 |
/// \brief Executes one round from the Bellman-Ford algorithm. |
|
423 |
/// |
|
424 |
/// If the algoritm calculated the distances in the previous round |
|
425 |
/// exactly for the paths of at most \c k arcs, then this function |
|
426 |
/// will calculate the distances exactly for the paths of at most |
|
427 |
/// <tt>k+1</tt> arcs. Performing \c k iterations using this function |
|
428 |
/// calculates the shortest path distances exactly for the paths |
|
429 |
/// consisting of at most \c k arcs. |
|
430 |
/// |
|
431 |
/// \warning The paths with limited arc number cannot be retrieved |
|
432 |
/// easily with \ref path() or \ref predArc() functions. If you also |
|
433 |
/// need the shortest paths and not only the distances, you should |
|
434 |
/// store the \ref predMap() "predecessor map" after each iteration |
|
435 |
/// and build the path manually. |
|
436 |
/// |
|
437 |
/// \return \c true when the algorithm have not found more shorter |
|
438 |
/// paths. |
|
439 |
/// |
|
440 |
/// \see ActiveIt |
|
441 |
bool processNextRound() { |
|
442 |
for (int i = 0; i < int(_process.size()); ++i) { |
|
443 |
_mask->set(_process[i], false); |
|
444 |
} |
|
445 |
std::vector<Node> nextProcess; |
|
446 |
std::vector<Value> values(_process.size()); |
|
447 |
for (int i = 0; i < int(_process.size()); ++i) { |
|
448 |
values[i] = (*_dist)[_process[i]]; |
|
449 |
} |
|
450 |
for (int i = 0; i < int(_process.size()); ++i) { |
|
451 |
for (OutArcIt it(*_gr, _process[i]); it != INVALID; ++it) { |
|
452 |
Node target = _gr->target(it); |
|
453 |
Value relaxed = OperationTraits::plus(values[i], (*_length)[it]); |
|
454 |
if (OperationTraits::less(relaxed, (*_dist)[target])) { |
|
455 |
_pred->set(target, it); |
|
456 |
_dist->set(target, relaxed); |
|
457 |
if (!(*_mask)[target]) { |
|
458 |
_mask->set(target, true); |
|
459 |
nextProcess.push_back(target); |
|
460 |
} |
|
461 |
} |
|
462 |
} |
|
463 |
} |
|
464 |
_process.swap(nextProcess); |
|
465 |
return _process.empty(); |
|
466 |
} |
|
467 |
|
|
468 |
/// \brief Executes one weak round from the Bellman-Ford algorithm. |
|
469 |
/// |
|
470 |
/// If the algorithm calculated the distances in the previous round |
|
471 |
/// at least for the paths of at most \c k arcs, then this function |
|
472 |
/// will calculate the distances at least for the paths of at most |
|
473 |
/// <tt>k+1</tt> arcs. |
|
474 |
/// This function does not make it possible to calculate the shortest |
|
475 |
/// path distances exactly for paths consisting of at most \c k arcs, |
|
476 |
/// this is why it is called weak round. |
|
477 |
/// |
|
478 |
/// \return \c true when the algorithm have not found more shorter |
|
479 |
/// paths. |
|
480 |
/// |
|
481 |
/// \see ActiveIt |
|
482 |
bool processNextWeakRound() { |
|
483 |
for (int i = 0; i < int(_process.size()); ++i) { |
|
484 |
_mask->set(_process[i], false); |
|
485 |
} |
|
486 |
std::vector<Node> nextProcess; |
|
487 |
for (int i = 0; i < int(_process.size()); ++i) { |
|
488 |
for (OutArcIt it(*_gr, _process[i]); it != INVALID; ++it) { |
|
489 |
Node target = _gr->target(it); |
|
490 |
Value relaxed = |
|
491 |
OperationTraits::plus((*_dist)[_process[i]], (*_length)[it]); |
|
492 |
if (OperationTraits::less(relaxed, (*_dist)[target])) { |
|
493 |
_pred->set(target, it); |
|
494 |
_dist->set(target, relaxed); |
|
495 |
if (!(*_mask)[target]) { |
|
496 |
_mask->set(target, true); |
|
497 |
nextProcess.push_back(target); |
|
498 |
} |
|
499 |
} |
|
500 |
} |
|
501 |
} |
|
502 |
_process.swap(nextProcess); |
|
503 |
return _process.empty(); |
|
504 |
} |
|
505 |
|
|
506 |
/// \brief Executes the algorithm. |
|
507 |
/// |
|
508 |
/// Executes the algorithm. |
|
509 |
/// |
|
510 |
/// This method runs the Bellman-Ford algorithm from the root node(s) |
|
511 |
/// in order to compute the shortest path to each node. |
|
512 |
/// |
|
513 |
/// The algorithm computes |
|
514 |
/// - the shortest path tree (forest), |
|
515 |
/// - the distance of each node from the root(s). |
|
516 |
/// |
|
517 |
/// \pre init() must be called and at least one root node should be |
|
518 |
/// added with addSource() before using this function. |
|
519 |
void start() { |
|
520 |
int num = countNodes(*_gr) - 1; |
|
521 |
for (int i = 0; i < num; ++i) { |
|
522 |
if (processNextWeakRound()) break; |
|
523 |
} |
|
524 |
} |
|
525 |
|
|
526 |
/// \brief Executes the algorithm and checks the negative cycles. |
|
527 |
/// |
|
528 |
/// Executes the algorithm and checks the negative cycles. |
|
529 |
/// |
|
530 |
/// This method runs the Bellman-Ford algorithm from the root node(s) |
|
531 |
/// in order to compute the shortest path to each node and also checks |
|
532 |
/// if the digraph contains cycles with negative total length. |
|
533 |
/// |
|
534 |
/// The algorithm computes |
|
535 |
/// - the shortest path tree (forest), |
|
536 |
/// - the distance of each node from the root(s). |
|
537 |
/// |
|
538 |
/// \return \c false if there is a negative cycle in the digraph. |
|
539 |
/// |
|
540 |
/// \pre init() must be called and at least one root node should be |
|
541 |
/// added with addSource() before using this function. |
|
542 |
bool checkedStart() { |
|
543 |
int num = countNodes(*_gr); |
|
544 |
for (int i = 0; i < num; ++i) { |
|
545 |
if (processNextWeakRound()) return true; |
|
546 |
} |
|
547 |
return _process.empty(); |
|
548 |
} |
|
549 |
|
|
550 |
/// \brief Executes the algorithm with arc number limit. |
|
551 |
/// |
|
552 |
/// Executes the algorithm with arc number limit. |
|
553 |
/// |
|
554 |
/// This method runs the Bellman-Ford algorithm from the root node(s) |
|
555 |
/// in order to compute the shortest path distance for each node |
|
556 |
/// using only the paths consisting of at most \c num arcs. |
|
557 |
/// |
|
558 |
/// The algorithm computes |
|
559 |
/// - the limited distance of each node from the root(s), |
|
560 |
/// - the predecessor arc for each node. |
|
561 |
/// |
|
562 |
/// \warning The paths with limited arc number cannot be retrieved |
|
563 |
/// easily with \ref path() or \ref predArc() functions. If you also |
|
564 |
/// need the shortest paths and not only the distances, you should |
|
565 |
/// store the \ref predMap() "predecessor map" after each iteration |
|
566 |
/// and build the path manually. |
|
567 |
/// |
|
568 |
/// \pre init() must be called and at least one root node should be |
|
569 |
/// added with addSource() before using this function. |
|
570 |
void limitedStart(int num) { |
|
571 |
for (int i = 0; i < num; ++i) { |
|
572 |
if (processNextRound()) break; |
|
573 |
} |
|
574 |
} |
|
575 |
|
|
576 |
/// \brief Runs the algorithm from the given root node. |
|
577 |
/// |
|
578 |
/// This method runs the Bellman-Ford algorithm from the given root |
|
579 |
/// node \c s in order to compute the shortest path to each node. |
|
580 |
/// |
|
581 |
/// The algorithm computes |
|
582 |
/// - the shortest path tree (forest), |
|
583 |
/// - the distance of each node from the root(s). |
|
584 |
/// |
|
585 |
/// \note bf.run(s) is just a shortcut of the following code. |
|
586 |
/// \code |
|
587 |
/// bf.init(); |
|
588 |
/// bf.addSource(s); |
|
589 |
/// bf.start(); |
|
590 |
/// \endcode |
|
591 |
void run(Node s) { |
|
592 |
init(); |
|
593 |
addSource(s); |
|
594 |
start(); |
|
595 |
} |
|
596 |
|
|
597 |
/// \brief Runs the algorithm from the given root node with arc |
|
598 |
/// number limit. |
|
599 |
/// |
|
600 |
/// This method runs the Bellman-Ford algorithm from the given root |
|
601 |
/// node \c s in order to compute the shortest path distance for each |
|
602 |
/// node using only the paths consisting of at most \c num arcs. |
|
603 |
/// |
|
604 |
/// The algorithm computes |
|
605 |
/// - the limited distance of each node from the root(s), |
|
606 |
/// - the predecessor arc for each node. |
|
607 |
/// |
|
608 |
/// \warning The paths with limited arc number cannot be retrieved |
|
609 |
/// easily with \ref path() or \ref predArc() functions. If you also |
|
610 |
/// need the shortest paths and not only the distances, you should |
|
611 |
/// store the \ref predMap() "predecessor map" after each iteration |
|
612 |
/// and build the path manually. |
|
613 |
/// |
|
614 |
/// \note bf.run(s, num) is just a shortcut of the following code. |
|
615 |
/// \code |
|
616 |
/// bf.init(); |
|
617 |
/// bf.addSource(s); |
|
618 |
/// bf.limitedStart(num); |
|
619 |
/// \endcode |
|
620 |
void run(Node s, int num) { |
|
621 |
init(); |
|
622 |
addSource(s); |
|
623 |
limitedStart(num); |
|
624 |
} |
|
625 |
|
|
626 |
///@} |
|
627 |
|
|
628 |
/// \brief LEMON iterator for getting the active nodes. |
|
629 |
/// |
|
630 |
/// This class provides a common style LEMON iterator that traverses |
|
631 |
/// the active nodes of the Bellman-Ford algorithm after the last |
|
632 |
/// phase. These nodes should be checked in the next phase to |
|
633 |
/// find augmenting arcs outgoing from them. |
|
634 |
class ActiveIt { |
|
635 |
public: |
|
636 |
|
|
637 |
/// \brief Constructor. |
|
638 |
/// |
|
639 |
/// Constructor for getting the active nodes of the given BellmanFord |
|
640 |
/// instance. |
|
641 |
ActiveIt(const BellmanFord& algorithm) : _algorithm(&algorithm) |
|
642 |
{ |
|
643 |
_index = _algorithm->_process.size() - 1; |
|
644 |
} |
|
645 |
|
|
646 |
/// \brief Invalid constructor. |
|
647 |
/// |
|
648 |
/// Invalid constructor. |
|
649 |
ActiveIt(Invalid) : _algorithm(0), _index(-1) {} |
|
650 |
|
|
651 |
/// \brief Conversion to \c Node. |
|
652 |
/// |
|
653 |
/// Conversion to \c Node. |
|
654 |
operator Node() const { |
|
655 |
return _index >= 0 ? _algorithm->_process[_index] : INVALID; |
|
656 |
} |
|
657 |
|
|
658 |
/// \brief Increment operator. |
|
659 |
/// |
|
660 |
/// Increment operator. |
|
661 |
ActiveIt& operator++() { |
|
662 |
--_index; |
|
663 |
return *this; |
|
664 |
} |
|
665 |
|
|
666 |
bool operator==(const ActiveIt& it) const { |
|
667 |
return static_cast<Node>(*this) == static_cast<Node>(it); |
|
668 |
} |
|
669 |
bool operator!=(const ActiveIt& it) const { |
|
670 |
return static_cast<Node>(*this) != static_cast<Node>(it); |
|
671 |
} |
|
672 |
bool operator<(const ActiveIt& it) const { |
|
673 |
return static_cast<Node>(*this) < static_cast<Node>(it); |
|
674 |
} |
|
675 |
|
|
676 |
private: |
|
677 |
const BellmanFord* _algorithm; |
|
678 |
int _index; |
|
679 |
}; |
|
680 |
|
|
681 |
/// \name Query Functions |
|
682 |
/// The result of the Bellman-Ford algorithm can be obtained using these |
|
683 |
/// functions.\n |
|
684 |
/// Either \ref run() or \ref init() should be called before using them. |
|
685 |
|
|
686 |
///@{ |
|
687 |
|
|
688 |
/// \brief The shortest path to the given node. |
|
689 |
/// |
|
690 |
/// Gives back the shortest path to the given node from the root(s). |
|
691 |
/// |
|
692 |
/// \warning \c t should be reached from the root(s). |
|
693 |
/// |
|
694 |
/// \pre Either \ref run() or \ref init() must be called before |
|
695 |
/// using this function. |
|
696 |
Path path(Node t) const |
|
697 |
{ |
|
698 |
return Path(*_gr, *_pred, t); |
|
699 |
} |
|
700 |
|
|
701 |
/// \brief The distance of the given node from the root(s). |
|
702 |
/// |
|
703 |
/// Returns the distance of the given node from the root(s). |
|
704 |
/// |
|
705 |
/// \warning If node \c v is not reached from the root(s), then |
|
706 |
/// the return value of this function is undefined. |
|
707 |
/// |
|
708 |
/// \pre Either \ref run() or \ref init() must be called before |
|
709 |
/// using this function. |
|
710 |
Value dist(Node v) const { return (*_dist)[v]; } |
|
711 |
|
|
712 |
/// \brief Returns the 'previous arc' of the shortest path tree for |
|
713 |
/// the given node. |
|
714 |
/// |
|
715 |
/// This function returns the 'previous arc' of the shortest path |
|
716 |
/// tree for node \c v, i.e. it returns the last arc of a |
|
717 |
/// shortest path from a root to \c v. It is \c INVALID if \c v |
|
718 |
/// is not reached from the root(s) or if \c v is a root. |
|
719 |
/// |
|
720 |
/// The shortest path tree used here is equal to the shortest path |
|
721 |
/// tree used in \ref predNode() and \ref predMap(). |
|
722 |
/// |
|
723 |
/// \pre Either \ref run() or \ref init() must be called before |
|
724 |
/// using this function. |
|
725 |
Arc predArc(Node v) const { return (*_pred)[v]; } |
|
726 |
|
|
727 |
/// \brief Returns the 'previous node' of the shortest path tree for |
|
728 |
/// the given node. |
|
729 |
/// |
|
730 |
/// This function returns the 'previous node' of the shortest path |
|
731 |
/// tree for node \c v, i.e. it returns the last but one node of |
|
732 |
/// a shortest path from a root to \c v. It is \c INVALID if \c v |
|
733 |
/// is not reached from the root(s) or if \c v is a root. |
|
734 |
/// |
|
735 |
/// The shortest path tree used here is equal to the shortest path |
|
736 |
/// tree used in \ref predArc() and \ref predMap(). |
|
737 |
/// |
|
738 |
/// \pre Either \ref run() or \ref init() must be called before |
|
739 |
/// using this function. |
|
740 |
Node predNode(Node v) const { |
|
741 |
return (*_pred)[v] == INVALID ? INVALID : _gr->source((*_pred)[v]); |
|
742 |
} |
|
743 |
|
|
744 |
/// \brief Returns a const reference to the node map that stores the |
|
745 |
/// distances of the nodes. |
|
746 |
/// |
|
747 |
/// Returns a const reference to the node map that stores the distances |
|
748 |
/// of the nodes calculated by the algorithm. |
|
749 |
/// |
|
750 |
/// \pre Either \ref run() or \ref init() must be called before |
|
751 |
/// using this function. |
|
752 |
const DistMap &distMap() const { return *_dist;} |
|
753 |
|
|
754 |
/// \brief Returns a const reference to the node map that stores the |
|
755 |
/// predecessor arcs. |
|
756 |
/// |
|
757 |
/// Returns a const reference to the node map that stores the predecessor |
|
758 |
/// arcs, which form the shortest path tree (forest). |
|
759 |
/// |
|
760 |
/// \pre Either \ref run() or \ref init() must be called before |
|
761 |
/// using this function. |
|
762 |
const PredMap &predMap() const { return *_pred; } |
|
763 |
|
|
764 |
/// \brief Checks if a node is reached from the root(s). |
|
765 |
/// |
|
766 |
/// Returns \c true if \c v is reached from the root(s). |
|
767 |
/// |
|
768 |
/// \pre Either \ref run() or \ref init() must be called before |
|
769 |
/// using this function. |
|
770 |
bool reached(Node v) const { |
|
771 |
return (*_dist)[v] != OperationTraits::infinity(); |
|
772 |
} |
|
773 |
|
|
774 |
/// \brief Gives back a negative cycle. |
|
775 |
/// |
|
776 |
/// This function gives back a directed cycle with negative total |
|
777 |
/// length if the algorithm has already found one. |
|
778 |
/// Otherwise it gives back an empty path. |
|
779 |
lemon::Path<Digraph> negativeCycle() const { |
|
780 |
typename Digraph::template NodeMap<int> state(*_gr, -1); |
|
781 |
lemon::Path<Digraph> cycle; |
|
782 |
for (int i = 0; i < int(_process.size()); ++i) { |
|
783 |
if (state[_process[i]] != -1) continue; |
|
784 |
for (Node v = _process[i]; (*_pred)[v] != INVALID; |
|
785 |
v = _gr->source((*_pred)[v])) { |
|
786 |
if (state[v] == i) { |
|
787 |
cycle.addFront((*_pred)[v]); |
|
788 |
for (Node u = _gr->source((*_pred)[v]); u != v; |
|
789 |
u = _gr->source((*_pred)[u])) { |
|
790 |
cycle.addFront((*_pred)[u]); |
|
791 |
} |
|
792 |
return cycle; |
|
793 |
} |
|
794 |
else if (state[v] >= 0) { |
|
795 |
break; |
|
796 |
} |
|
797 |
state[v] = i; |
|
798 |
} |
|
799 |
} |
|
800 |
return cycle; |
|
801 |
} |
|
802 |
|
|
803 |
///@} |
|
804 |
}; |
|
805 |
|
|
806 |
/// \brief Default traits class of bellmanFord() function. |
|
807 |
/// |
|
808 |
/// Default traits class of bellmanFord() function. |
|
809 |
/// \tparam GR The type of the digraph. |
|
810 |
/// \tparam LEN The type of the length map. |
|
811 |
template <typename GR, typename LEN> |
|
812 |
struct BellmanFordWizardDefaultTraits { |
|
813 |
/// The type of the digraph the algorithm runs on. |
|
814 |
typedef GR Digraph; |
|
815 |
|
|
816 |
/// \brief The type of the map that stores the arc lengths. |
|
817 |
/// |
|
818 |
/// The type of the map that stores the arc lengths. |
|
819 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
|
820 |
typedef LEN LengthMap; |
|
821 |
|
|
822 |
/// The type of the arc lengths. |
|
823 |
typedef typename LEN::Value Value; |
|
824 |
|
|
825 |
/// \brief Operation traits for Bellman-Ford algorithm. |
|
826 |
/// |
|
827 |
/// It defines the used operations and the infinity value for the |
|
828 |
/// given \c Value type. |
|
829 |
/// \see BellmanFordDefaultOperationTraits |
|
830 |
typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
|
831 |
|
|
832 |
/// \brief The type of the map that stores the last |
|
833 |
/// arcs of the shortest paths. |
|
834 |
/// |
|
835 |
/// The type of the map that stores the last arcs of the shortest paths. |
|
836 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
837 |
typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
|
838 |
|
|
839 |
/// \brief Instantiates a \c PredMap. |
|
840 |
/// |
|
841 |
/// This function instantiates a \ref PredMap. |
|
842 |
/// \param g is the digraph to which we would like to define the |
|
843 |
/// \ref PredMap. |
|
844 |
static PredMap *createPredMap(const GR &g) { |
|
845 |
return new PredMap(g); |
|
846 |
} |
|
847 |
|
|
848 |
/// \brief The type of the map that stores the distances of the nodes. |
|
849 |
/// |
|
850 |
/// The type of the map that stores the distances of the nodes. |
|
851 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
852 |
typedef typename GR::template NodeMap<Value> DistMap; |
|
853 |
|
|
854 |
/// \brief Instantiates a \c DistMap. |
|
855 |
/// |
|
856 |
/// This function instantiates a \ref DistMap. |
|
857 |
/// \param g is the digraph to which we would like to define the |
|
858 |
/// \ref DistMap. |
|
859 |
static DistMap *createDistMap(const GR &g) { |
|
860 |
return new DistMap(g); |
|
861 |
} |
|
862 |
|
|
863 |
///The type of the shortest paths. |
|
864 |
|
|
865 |
///The type of the shortest paths. |
|
866 |
///It must meet the \ref concepts::Path "Path" concept. |
|
867 |
typedef lemon::Path<Digraph> Path; |
|
868 |
}; |
|
869 |
|
|
870 |
/// \brief Default traits class used by BellmanFordWizard. |
|
871 |
/// |
|
872 |
/// Default traits class used by BellmanFordWizard. |
|
873 |
/// \tparam GR The type of the digraph. |
|
874 |
/// \tparam LEN The type of the length map. |
|
875 |
template <typename GR, typename LEN> |
|
876 |
class BellmanFordWizardBase |
|
877 |
: public BellmanFordWizardDefaultTraits<GR, LEN> { |
|
878 |
|
|
879 |
typedef BellmanFordWizardDefaultTraits<GR, LEN> Base; |
|
880 |
protected: |
|
881 |
// Type of the nodes in the digraph. |
|
882 |
typedef typename Base::Digraph::Node Node; |
|
883 |
|
|
884 |
// Pointer to the underlying digraph. |
|
885 |
void *_graph; |
|
886 |
// Pointer to the length map |
|
887 |
void *_length; |
|
888 |
// Pointer to the map of predecessors arcs. |
|
889 |
void *_pred; |
|
890 |
// Pointer to the map of distances. |
|
891 |
void *_dist; |
|
892 |
//Pointer to the shortest path to the target node. |
|
893 |
void *_path; |
|
894 |
//Pointer to the distance of the target node. |
|
895 |
void *_di; |
|
896 |
|
|
897 |
public: |
|
898 |
/// Constructor. |
|
899 |
|
|
900 |
/// This constructor does not require parameters, it initiates |
|
901 |
/// all of the attributes to default values \c 0. |
|
902 |
BellmanFordWizardBase() : |
|
903 |
_graph(0), _length(0), _pred(0), _dist(0), _path(0), _di(0) {} |
|
904 |
|
|
905 |
/// Constructor. |
|
906 |
|
|
907 |
/// This constructor requires two parameters, |
|
908 |
/// others are initiated to \c 0. |
|
909 |
/// \param gr The digraph the algorithm runs on. |
|
910 |
/// \param len The length map. |
|
911 |
BellmanFordWizardBase(const GR& gr, |
|
912 |
const LEN& len) : |
|
913 |
_graph(reinterpret_cast<void*>(const_cast<GR*>(&gr))), |
|
914 |
_length(reinterpret_cast<void*>(const_cast<LEN*>(&len))), |
|
915 |
_pred(0), _dist(0), _path(0), _di(0) {} |
|
916 |
|
|
917 |
}; |
|
918 |
|
|
919 |
/// \brief Auxiliary class for the function-type interface of the |
|
920 |
/// \ref BellmanFord "Bellman-Ford" algorithm. |
|
921 |
/// |
|
922 |
/// This auxiliary class is created to implement the |
|
923 |
/// \ref bellmanFord() "function-type interface" of the |
|
924 |
/// \ref BellmanFord "Bellman-Ford" algorithm. |
|
925 |
/// It does not have own \ref run() method, it uses the |
|
926 |
/// functions and features of the plain \ref BellmanFord. |
|
927 |
/// |
|
928 |
/// This class should only be used through the \ref bellmanFord() |
|
929 |
/// function, which makes it easier to use the algorithm. |
|
930 |
template<class TR> |
|
931 |
class BellmanFordWizard : public TR { |
|
932 |
typedef TR Base; |
|
933 |
|
|
934 |
typedef typename TR::Digraph Digraph; |
|
935 |
|
|
936 |
typedef typename Digraph::Node Node; |
|
937 |
typedef typename Digraph::NodeIt NodeIt; |
|
938 |
typedef typename Digraph::Arc Arc; |
|
939 |
typedef typename Digraph::OutArcIt ArcIt; |
|
940 |
|
|
941 |
typedef typename TR::LengthMap LengthMap; |
|
942 |
typedef typename LengthMap::Value Value; |
|
943 |
typedef typename TR::PredMap PredMap; |
|
944 |
typedef typename TR::DistMap DistMap; |
|
945 |
typedef typename TR::Path Path; |
|
946 |
|
|
947 |
public: |
|
948 |
/// Constructor. |
|
949 |
BellmanFordWizard() : TR() {} |
|
950 |
|
|
951 |
/// \brief Constructor that requires parameters. |
|
952 |
/// |
|
953 |
/// Constructor that requires parameters. |
|
954 |
/// These parameters will be the default values for the traits class. |
|
955 |
/// \param gr The digraph the algorithm runs on. |
|
956 |
/// \param len The length map. |
|
957 |
BellmanFordWizard(const Digraph& gr, const LengthMap& len) |
|
958 |
: TR(gr, len) {} |
|
959 |
|
|
960 |
/// \brief Copy constructor |
|
961 |
BellmanFordWizard(const TR &b) : TR(b) {} |
|
962 |
|
|
963 |
~BellmanFordWizard() {} |
|
964 |
|
|
965 |
/// \brief Runs the Bellman-Ford algorithm from the given source node. |
|
966 |
/// |
|
967 |
/// This method runs the Bellman-Ford algorithm from the given source |
|
968 |
/// node in order to compute the shortest path to each node. |
|
969 |
void run(Node s) { |
|
970 |
BellmanFord<Digraph,LengthMap,TR> |
|
971 |
bf(*reinterpret_cast<const Digraph*>(Base::_graph), |
|
972 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
|
973 |
if (Base::_pred) bf.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
|
974 |
if (Base::_dist) bf.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
|
975 |
bf.run(s); |
|
976 |
} |
|
977 |
|
|
978 |
/// \brief Runs the Bellman-Ford algorithm to find the shortest path |
|
979 |
/// between \c s and \c t. |
|
980 |
/// |
|
981 |
/// This method runs the Bellman-Ford algorithm from node \c s |
|
982 |
/// in order to compute the shortest path to node \c t. |
|
983 |
/// Actually, it computes the shortest path to each node, but using |
|
984 |
/// this function you can retrieve the distance and the shortest path |
|
985 |
/// for a single target node easier. |
|
986 |
/// |
|
987 |
/// \return \c true if \c t is reachable form \c s. |
|
988 |
bool run(Node s, Node t) { |
|
989 |
BellmanFord<Digraph,LengthMap,TR> |
|
990 |
bf(*reinterpret_cast<const Digraph*>(Base::_graph), |
|
991 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
|
992 |
if (Base::_pred) bf.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
|
993 |
if (Base::_dist) bf.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
|
994 |
bf.run(s); |
|
995 |
if (Base::_path) *reinterpret_cast<Path*>(Base::_path) = bf.path(t); |
|
996 |
if (Base::_di) *reinterpret_cast<Value*>(Base::_di) = bf.dist(t); |
|
997 |
return bf.reached(t); |
|
998 |
} |
|
999 |
|
|
1000 |
template<class T> |
|
1001 |
struct SetPredMapBase : public Base { |
|
1002 |
typedef T PredMap; |
|
1003 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
|
1004 |
SetPredMapBase(const TR &b) : TR(b) {} |
|
1005 |
}; |
|
1006 |
|
|
1007 |
/// \brief \ref named-templ-param "Named parameter" for setting |
|
1008 |
/// the predecessor map. |
|
1009 |
/// |
|
1010 |
/// \ref named-templ-param "Named parameter" for setting |
|
1011 |
/// the map that stores the predecessor arcs of the nodes. |
|
1012 |
template<class T> |
|
1013 |
BellmanFordWizard<SetPredMapBase<T> > predMap(const T &t) { |
|
1014 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1015 |
return BellmanFordWizard<SetPredMapBase<T> >(*this); |
|
1016 |
} |
|
1017 |
|
|
1018 |
template<class T> |
|
1019 |
struct SetDistMapBase : public Base { |
|
1020 |
typedef T DistMap; |
|
1021 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
|
1022 |
SetDistMapBase(const TR &b) : TR(b) {} |
|
1023 |
}; |
|
1024 |
|
|
1025 |
/// \brief \ref named-templ-param "Named parameter" for setting |
|
1026 |
/// the distance map. |
|
1027 |
/// |
|
1028 |
/// \ref named-templ-param "Named parameter" for setting |
|
1029 |
/// the map that stores the distances of the nodes calculated |
|
1030 |
/// by the algorithm. |
|
1031 |
template<class T> |
|
1032 |
BellmanFordWizard<SetDistMapBase<T> > distMap(const T &t) { |
|
1033 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1034 |
return BellmanFordWizard<SetDistMapBase<T> >(*this); |
|
1035 |
} |
|
1036 |
|
|
1037 |
template<class T> |
|
1038 |
struct SetPathBase : public Base { |
|
1039 |
typedef T Path; |
|
1040 |
SetPathBase(const TR &b) : TR(b) {} |
|
1041 |
}; |
|
1042 |
|
|
1043 |
/// \brief \ref named-func-param "Named parameter" for getting |
|
1044 |
/// the shortest path to the target node. |
|
1045 |
/// |
|
1046 |
/// \ref named-func-param "Named parameter" for getting |
|
1047 |
/// the shortest path to the target node. |
|
1048 |
template<class T> |
|
1049 |
BellmanFordWizard<SetPathBase<T> > path(const T &t) |
|
1050 |
{ |
|
1051 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1052 |
return BellmanFordWizard<SetPathBase<T> >(*this); |
|
1053 |
} |
|
1054 |
|
|
1055 |
/// \brief \ref named-func-param "Named parameter" for getting |
|
1056 |
/// the distance of the target node. |
|
1057 |
/// |
|
1058 |
/// \ref named-func-param "Named parameter" for getting |
|
1059 |
/// the distance of the target node. |
|
1060 |
BellmanFordWizard dist(const Value &d) |
|
1061 |
{ |
|
1062 |
Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d)); |
|
1063 |
return *this; |
|
1064 |
} |
|
1065 |
|
|
1066 |
}; |
|
1067 |
|
|
1068 |
/// \brief Function type interface for the \ref BellmanFord "Bellman-Ford" |
|
1069 |
/// algorithm. |
|
1070 |
/// |
|
1071 |
/// \ingroup shortest_path |
|
1072 |
/// Function type interface for the \ref BellmanFord "Bellman-Ford" |
|
1073 |
/// algorithm. |
|
1074 |
/// |
|
1075 |
/// This function also has several \ref named-templ-func-param |
|
1076 |
/// "named parameters", they are declared as the members of class |
|
1077 |
/// \ref BellmanFordWizard. |
|
1078 |
/// The following examples show how to use these parameters. |
|
1079 |
/// \code |
|
1080 |
/// // Compute shortest path from node s to each node |
|
1081 |
/// bellmanFord(g,length).predMap(preds).distMap(dists).run(s); |
|
1082 |
/// |
|
1083 |
/// // Compute shortest path from s to t |
|
1084 |
/// bool reached = bellmanFord(g,length).path(p).dist(d).run(s,t); |
|
1085 |
/// \endcode |
|
1086 |
/// \warning Don't forget to put the \ref BellmanFordWizard::run() "run()" |
|
1087 |
/// to the end of the parameter list. |
|
1088 |
/// \sa BellmanFordWizard |
|
1089 |
/// \sa BellmanFord |
|
1090 |
template<typename GR, typename LEN> |
|
1091 |
BellmanFordWizard<BellmanFordWizardBase<GR,LEN> > |
|
1092 |
bellmanFord(const GR& digraph, |
|
1093 |
const LEN& length) |
|
1094 |
{ |
|
1095 |
return BellmanFordWizard<BellmanFordWizardBase<GR,LEN> >(digraph, length); |
|
1096 |
} |
|
1097 |
|
|
1098 |
} //END OF NAMESPACE LEMON |
|
1099 |
|
|
1100 |
#endif |
|
1101 |
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 |
* |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 |
* |
|
5 |
* Copyright (C) 2003-2009 |
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
8 |
* |
|
9 |
* Permission to use, modify and distribute this software is granted |
|
10 |
* provided that this copyright notice appears in all copies. For |
|
11 |
* precise terms see the accompanying LICENSE file. |
|
12 |
* |
|
13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
14 |
* express or implied, and with no claim as to its suitability for any |
|
15 |
* purpose. |
|
16 |
* |
|
17 |
*/ |
|
18 |
|
|
19 |
#ifndef LEMON_BINOM_HEAP_H |
|
20 |
#define LEMON_BINOM_HEAP_H |
|
21 |
|
|
22 |
///\file |
|
23 |
///\ingroup heaps |
|
24 |
///\brief Binomial Heap implementation. |
|
25 |
|
|
26 |
#include <vector> |
|
27 |
#include <utility> |
|
28 |
#include <functional> |
|
29 |
#include <lemon/math.h> |
|
30 |
#include <lemon/counter.h> |
|
31 |
|
|
32 |
namespace lemon { |
|
33 |
|
|
34 |
/// \ingroup heaps |
|
35 |
/// |
|
36 |
///\brief Binomial heap data structure. |
|
37 |
/// |
|
38 |
/// This class implements the \e binomial \e heap data structure. |
|
39 |
/// It fully conforms to the \ref concepts::Heap "heap concept". |
|
40 |
/// |
|
41 |
/// The methods \ref increase() and \ref erase() are not efficient |
|
42 |
/// in a binomial heap. In case of many calls of these operations, |
|
43 |
/// it is better to use other heap structure, e.g. \ref BinHeap |
|
44 |
/// "binary heap". |
|
45 |
/// |
|
46 |
/// \tparam PR Type of the priorities of the items. |
|
47 |
/// \tparam IM A read-writable item map with \c int values, used |
|
48 |
/// internally to handle the cross references. |
|
49 |
/// \tparam CMP A functor class for comparing the priorities. |
|
50 |
/// The default is \c std::less<PR>. |
|
51 |
#ifdef DOXYGEN |
|
52 |
template <typename PR, typename IM, typename CMP> |
|
53 |
#else |
|
54 |
template <typename PR, typename IM, typename CMP = std::less<PR> > |
|
55 |
#endif |
|
56 |
class BinomHeap { |
|
57 |
public: |
|
58 |
/// Type of the item-int map. |
|
59 |
typedef IM ItemIntMap; |
|
60 |
/// Type of the priorities. |
|
61 |
typedef PR Prio; |
|
62 |
/// Type of the items stored in the heap. |
|
63 |
typedef typename ItemIntMap::Key Item; |
|
64 |
/// Functor type for comparing the priorities. |
|
65 |
typedef CMP Compare; |
|
66 |
|
|
67 |
/// \brief Type to represent the states of the items. |
|
68 |
/// |
|
69 |
/// Each item has a state associated to it. It can be "in heap", |
|
70 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the |
|
71 |
/// heap's point of view, but may be useful to the user. |
|
72 |
/// |
|
73 |
/// The item-int map must be initialized in such way that it assigns |
|
74 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
|
75 |
enum State { |
|
76 |
IN_HEAP = 0, ///< = 0. |
|
77 |
PRE_HEAP = -1, ///< = -1. |
|
78 |
POST_HEAP = -2 ///< = -2. |
|
79 |
}; |
|
80 |
|
|
81 |
private: |
|
82 |
class Store; |
|
83 |
|
|
84 |
std::vector<Store> _data; |
|
85 |
int _min, _head; |
|
86 |
ItemIntMap &_iim; |
|
87 |
Compare _comp; |
|
88 |
int _num_items; |
|
89 |
|
|
90 |
public: |
|
91 |
/// \brief Constructor. |
|
92 |
/// |
|
93 |
/// Constructor. |
|
94 |
/// \param map A map that assigns \c int values to the items. |
|
95 |
/// It is used internally to handle the cross references. |
|
96 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
|
97 |
explicit BinomHeap(ItemIntMap &map) |
|
98 |
: _min(0), _head(-1), _iim(map), _num_items(0) {} |
|
99 |
|
|
100 |
/// \brief Constructor. |
|
101 |
/// |
|
102 |
/// Constructor. |
|
103 |
/// \param map A map that assigns \c int values to the items. |
|
104 |
/// It is used internally to handle the cross references. |
|
105 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
|
106 |
/// \param comp The function object used for comparing the priorities. |
|
107 |
BinomHeap(ItemIntMap &map, const Compare &comp) |
|
108 |
: _min(0), _head(-1), _iim(map), _comp(comp), _num_items(0) {} |
|
109 |
|
|
110 |
/// \brief The number of items stored in the heap. |
|
111 |
/// |
|
112 |
/// This function returns the number of items stored in the heap. |
|
113 |
int size() const { return _num_items; } |
|
114 |
|
|
115 |
/// \brief Check if the heap is empty. |
|
116 |
/// |
|
117 |
/// This function returns \c true if the heap is empty. |
|
118 |
bool empty() const { return _num_items==0; } |
|
119 |
|
|
120 |
/// \brief Make the heap empty. |
|
121 |
/// |
|
122 |
/// This functon makes the heap empty. |
|
123 |
/// It does not change the cross reference map. If you want to reuse |
|
124 |
/// a heap that is not surely empty, you should first clear it and |
|
125 |
/// then you should set the cross reference map to \c PRE_HEAP |
|
126 |
/// for each item. |
|
127 |
void clear() { |
|
128 |
_data.clear(); _min=0; _num_items=0; _head=-1; |
|
129 |
} |
|
130 |
|
|
131 |
/// \brief Set the priority of an item or insert it, if it is |
|
132 |
/// not stored in the heap. |
|
133 |
/// |
|
134 |
/// This method sets the priority of the given item if it is |
|
135 |
/// already stored in the heap. Otherwise it inserts the given |
|
136 |
/// item into the heap with the given priority. |
|
137 |
/// \param item The item. |
|
138 |
/// \param value The priority. |
|
139 |
void set (const Item& item, const Prio& value) { |
|
140 |
int i=_iim[item]; |
|
141 |
if ( i >= 0 && _data[i].in ) { |
|
142 |
if ( _comp(value, _data[i].prio) ) decrease(item, value); |
|
143 |
if ( _comp(_data[i].prio, value) ) increase(item, value); |
|
144 |
} else push(item, value); |
|
145 |
} |
|
146 |
|
|
147 |
/// \brief Insert an item into the heap with the given priority. |
|
148 |
/// |
|
149 |
/// This function inserts the given item into the heap with the |
|
150 |
/// given priority. |
|
151 |
/// \param item The item to insert. |
|
152 |
/// \param value The priority of the item. |
|
153 |
/// \pre \e item must not be stored in the heap. |
|
154 |
void push (const Item& item, const Prio& value) { |
|
155 |
int i=_iim[item]; |
|
156 |
if ( i<0 ) { |
|
157 |
int s=_data.size(); |
|
158 |
_iim.set( item,s ); |
|
159 |
Store st; |
|
160 |
st.name=item; |
|
161 |
st.prio=value; |
|
162 |
_data.push_back(st); |
|
163 |
i=s; |
|
164 |
} |
|
165 |
else { |
|
166 |
_data[i].parent=_data[i].right_neighbor=_data[i].child=-1; |
|
167 |
_data[i].degree=0; |
|
168 |
_data[i].in=true; |
|
169 |
_data[i].prio=value; |
|
170 |
} |
|
171 |
|
|
172 |
if( 0==_num_items ) { |
|
173 |
_head=i; |
|
174 |
_min=i; |
|
175 |
} else { |
|
176 |
merge(i); |
|
177 |
if( _comp(_data[i].prio, _data[_min].prio) ) _min=i; |
|
178 |
} |
|
179 |
++_num_items; |
|
180 |
} |
|
181 |
|
|
182 |
/// \brief Return the item having minimum priority. |
|
183 |
/// |
|
184 |
/// This function returns the item having minimum priority. |
|
185 |
/// \pre The heap must be non-empty. |
|
186 |
Item top() const { return _data[_min].name; } |
|
187 |
|
|
188 |
/// \brief The minimum priority. |
|
189 |
/// |
|
190 |
/// This function returns the minimum priority. |
|
191 |
/// \pre The heap must be non-empty. |
|
192 |
Prio prio() const { return _data[_min].prio; } |
|
193 |
|
|
194 |
/// \brief The priority of the given item. |
|
195 |
/// |
|
196 |
/// This function returns the priority of the given item. |
|
197 |
/// \param item The item. |
|
198 |
/// \pre \e item must be in the heap. |
|
199 |
const Prio& operator[](const Item& item) const { |
|
200 |
return _data[_iim[item]].prio; |
|
201 |
} |
|
202 |
|
|
203 |
/// \brief Remove the item having minimum priority. |
|
204 |
/// |
|
205 |
/// This function removes the item having minimum priority. |
|
206 |
/// \pre The heap must be non-empty. |
|
207 |
void pop() { |
|
208 |
_data[_min].in=false; |
|
209 |
|
|
210 |
int head_child=-1; |
|
211 |
if ( _data[_min].child!=-1 ) { |
|
212 |
int child=_data[_min].child; |
|
213 |
int neighb; |
|
214 |
while( child!=-1 ) { |
|
215 |
neighb=_data[child].right_neighbor; |
|
216 |
_data[child].parent=-1; |
|
217 |
_data[child].right_neighbor=head_child; |
|
218 |
head_child=child; |
|
219 |
child=neighb; |
|
220 |
} |
|
221 |
} |
|
222 |
|
|
223 |
if ( _data[_head].right_neighbor==-1 ) { |
|
224 |
// there was only one root |
|
225 |
_head=head_child; |
|
226 |
} |
|
227 |
else { |
|
228 |
// there were more roots |
|
229 |
if( _head!=_min ) { unlace(_min); } |
|
230 |
else { _head=_data[_head].right_neighbor; } |
|
231 |
merge(head_child); |
|
232 |
} |
|
233 |
_min=findMin(); |
|
234 |
--_num_items; |
|
235 |
} |
|
236 |
|
|
237 |
/// \brief Remove the given item from the heap. |
|
238 |
/// |
|
239 |
/// This function removes the given item from the heap if it is |
|
240 |
/// already stored. |
|
241 |
/// \param item The item to delete. |
|
242 |
/// \pre \e item must be in the heap. |
|
243 |
void erase (const Item& item) { |
|
244 |
int i=_iim[item]; |
|
245 |
if ( i >= 0 && _data[i].in ) { |
|
246 |
decrease( item, _data[_min].prio-1 ); |
|
247 |
pop(); |
|
248 |
} |
|
249 |
} |
|
250 |
|
|
251 |
/// \brief Decrease the priority of an item to the given value. |
|
252 |
/// |
|
253 |
/// This function decreases the priority of an item to the given value. |
|
254 |
/// \param item The item. |
|
255 |
/// \param value The priority. |
|
256 |
/// \pre \e item must be stored in the heap with priority at least \e value. |
|
257 |
void decrease (Item item, const Prio& value) { |
|
258 |
int i=_iim[item]; |
|
259 |
int p=_data[i].parent; |
|
260 |
_data[i].prio=value; |
|
261 |
|
|
262 |
while( p!=-1 && _comp(value, _data[p].prio) ) { |
|
263 |
_data[i].name=_data[p].name; |
|
264 |
_data[i].prio=_data[p].prio; |
|
265 |
_data[p].name=item; |
|
266 |
_data[p].prio=value; |
|
267 |
_iim[_data[i].name]=i; |
|
268 |
i=p; |
|
269 |
p=_data[p].parent; |
|
270 |
} |
|
271 |
_iim[item]=i; |
|
272 |
if ( _comp(value, _data[_min].prio) ) _min=i; |
|
273 |
} |
|
274 |
|
|
275 |
/// \brief Increase the priority of an item to the given value. |
|
276 |
/// |
|
277 |
/// This function increases the priority of an item to the given value. |
|
278 |
/// \param item The item. |
|
279 |
/// \param value The priority. |
|
280 |
/// \pre \e item must be stored in the heap with priority at most \e value. |
|
281 |
void increase (Item item, const Prio& value) { |
|
282 |
erase(item); |
|
283 |
push(item, value); |
|
284 |
} |
|
285 |
|
|
286 |
/// \brief Return the state of an item. |
|
287 |
/// |
|
288 |
/// This method returns \c PRE_HEAP if the given item has never |
|
289 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
|
290 |
/// and \c POST_HEAP otherwise. |
|
291 |
/// In the latter case it is possible that the item will get back |
|
292 |
/// to the heap again. |
|
293 |
/// \param item The item. |
|
294 |
State state(const Item &item) const { |
|
295 |
int i=_iim[item]; |
|
296 |
if( i>=0 ) { |
|
297 |
if ( _data[i].in ) i=0; |
|
298 |
else i=-2; |
|
299 |
} |
|
300 |
return State(i); |
|
301 |
} |
|
302 |
|
|
303 |
/// \brief Set the state of an item in the heap. |
|
304 |
/// |
|
305 |
/// This function sets the state of the given item in the heap. |
|
306 |
/// It can be used to manually clear the heap when it is important |
|
307 |
/// to achive better time complexity. |
|
308 |
/// \param i The item. |
|
309 |
/// \param st The state. It should not be \c IN_HEAP. |
|
310 |
void state(const Item& i, State st) { |
|
311 |
switch (st) { |
|
312 |
case POST_HEAP: |
|
313 |
case PRE_HEAP: |
|
314 |
if (state(i) == IN_HEAP) { |
|
315 |
erase(i); |
|
316 |
} |
|
317 |
_iim[i] = st; |
|
318 |
break; |
|
319 |
case IN_HEAP: |
|
320 |
break; |
|
321 |
} |
|
322 |
} |
|
323 |
|
|
324 |
private: |
|
325 |
|
|
326 |
// Find the minimum of the roots |
|
327 |
int findMin() { |
|
328 |
if( _head!=-1 ) { |
|
329 |
int min_loc=_head, min_val=_data[_head].prio; |
|
330 |
for( int x=_data[_head].right_neighbor; x!=-1; |
|
331 |
x=_data[x].right_neighbor ) { |
|
332 |
if( _comp( _data[x].prio,min_val ) ) { |
|
333 |
min_val=_data[x].prio; |
|
334 |
min_loc=x; |
|
335 |
} |
|
336 |
} |
|
337 |
return min_loc; |
|
338 |
} |
|
339 |
else return -1; |
|
340 |
} |
|
341 |
|
|
342 |
// Merge the heap with another heap starting at the given position |
|
343 |
void merge(int a) { |
|
344 |
if( _head==-1 || a==-1 ) return; |
|
345 |
if( _data[a].right_neighbor==-1 && |
|
346 |
_data[a].degree<=_data[_head].degree ) { |
|
347 |
_data[a].right_neighbor=_head; |
|
348 |
_head=a; |
|
349 |
} else { |
|
350 |
interleave(a); |
|
351 |
} |
|
352 |
if( _data[_head].right_neighbor==-1 ) return; |
|
353 |
|
|
354 |
int x=_head; |
|
355 |
int x_prev=-1, x_next=_data[x].right_neighbor; |
|
356 |
while( x_next!=-1 ) { |
|
357 |
if( _data[x].degree!=_data[x_next].degree || |
|
358 |
( _data[x_next].right_neighbor!=-1 && |
|
359 |
_data[_data[x_next].right_neighbor].degree==_data[x].degree ) ) { |
|
360 |
x_prev=x; |
|
361 |
x=x_next; |
|
362 |
} |
|
363 |
else { |
|
364 |
if( _comp(_data[x_next].prio,_data[x].prio) ) { |
|
365 |
if( x_prev==-1 ) { |
|
366 |
_head=x_next; |
|
367 |
} else { |
|
368 |
_data[x_prev].right_neighbor=x_next; |
|
369 |
} |
|
370 |
fuse(x,x_next); |
|
371 |
x=x_next; |
|
372 |
} |
|
373 |
else { |
|
374 |
_data[x].right_neighbor=_data[x_next].right_neighbor; |
|
375 |
fuse(x_next,x); |
|
376 |
} |
|
377 |
} |
|
378 |
x_next=_data[x].right_neighbor; |
|
379 |
} |
|
380 |
} |
|
381 |
|
|
382 |
// Interleave the elements of the given list into the list of the roots |
|
383 |
void interleave(int a) { |
|
384 |
int p=_head, q=a; |
|
385 |
int curr=_data.size(); |
|
386 |
_data.push_back(Store()); |
|
387 |
|
|
388 |
while( p!=-1 || q!=-1 ) { |
|
389 |
if( q==-1 || ( p!=-1 && _data[p].degree<_data[q].degree ) ) { |
|
390 |
_data[curr].right_neighbor=p; |
|
391 |
curr=p; |
|
392 |
p=_data[p].right_neighbor; |
|
393 |
} |
|
394 |
else { |
|
395 |
_data[curr].right_neighbor=q; |
|
396 |
curr=q; |
|
397 |
q=_data[q].right_neighbor; |
|
398 |
} |
|
399 |
} |
|
400 |
|
|
401 |
_head=_data.back().right_neighbor; |
|
402 |
_data.pop_back(); |
|
403 |
} |
|
404 |
|
|
405 |
// Lace node a under node b |
|
406 |
void fuse(int a, int b) { |
|
407 |
_data[a].parent=b; |
|
408 |
_data[a].right_neighbor=_data[b].child; |
|
409 |
_data[b].child=a; |
|
410 |
|
|
411 |
++_data[b].degree; |
|
412 |
} |
|
413 |
|
|
414 |
// Unlace node a (if it has siblings) |
|
415 |
void unlace(int a) { |
|
416 |
int neighb=_data[a].right_neighbor; |
|
417 |
int other=_head; |
|
418 |
|
|
419 |
while( _data[other].right_neighbor!=a ) |
|
420 |
other=_data[other].right_neighbor; |
|
421 |
_data[other].right_neighbor=neighb; |
|
422 |
} |
|
423 |
|
|
424 |
private: |
|
425 |
|
|
426 |
class Store { |
|
427 |
friend class BinomHeap; |
|
428 |
|
|
429 |
Item name; |
|
430 |
int parent; |
|
431 |
int right_neighbor; |
|
432 |
int child; |
|
433 |
int degree; |
|
434 |
bool in; |
|
435 |
Prio prio; |
|
436 |
|
|
437 |
Store() : parent(-1), right_neighbor(-1), child(-1), degree(0), |
|
438 |
in(true) {} |
|
439 |
}; |
|
440 |
}; |
|
441 |
|
|
442 |
} //namespace lemon |
|
443 |
|
|
444 |
#endif //LEMON_BINOM_HEAP_H |
|
445 |
1 | 1 |
CMAKE_MINIMUM_REQUIRED(VERSION 2.6) |
2 | 2 |
|
3 | 3 |
SET(PROJECT_NAME "LEMON") |
4 | 4 |
PROJECT(${PROJECT_NAME}) |
5 | 5 |
|
6 | 6 |
IF(EXISTS ${PROJECT_SOURCE_DIR}/cmake/version.cmake) |
7 | 7 |
INCLUDE(${PROJECT_SOURCE_DIR}/cmake/version.cmake) |
8 | 8 |
ELSEIF(DEFINED ENV{LEMON_VERSION}) |
9 | 9 |
SET(LEMON_VERSION $ENV{LEMON_VERSION} CACHE STRING "LEMON version string.") |
10 | 10 |
ELSE() |
11 | 11 |
EXECUTE_PROCESS( |
12 | 12 |
COMMAND hg id -i |
13 | 13 |
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} |
14 | 14 |
OUTPUT_VARIABLE HG_REVISION |
15 | 15 |
ERROR_QUIET |
16 | 16 |
OUTPUT_STRIP_TRAILING_WHITESPACE |
17 | 17 |
) |
18 | 18 |
IF(HG_REVISION STREQUAL "") |
19 | 19 |
SET(HG_REVISION "hg-tip") |
20 | 20 |
ENDIF() |
21 | 21 |
SET(LEMON_VERSION ${HG_REVISION} CACHE STRING "LEMON version string.") |
22 | 22 |
ENDIF() |
23 | 23 |
|
24 | 24 |
SET(PROJECT_VERSION ${LEMON_VERSION}) |
25 | 25 |
|
26 | 26 |
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) |
27 | 27 |
|
28 | 28 |
FIND_PACKAGE(Doxygen) |
29 | 29 |
FIND_PACKAGE(Ghostscript) |
30 | 30 |
FIND_PACKAGE(GLPK 4.33) |
31 | 31 |
FIND_PACKAGE(CPLEX) |
32 | 32 |
FIND_PACKAGE(COIN) |
33 | 33 |
|
34 | 34 |
INCLUDE(CheckTypeSize) |
35 | 35 |
CHECK_TYPE_SIZE("long long" LONG_LONG) |
36 | 36 |
SET(LEMON_HAVE_LONG_LONG ${HAVE_LONG_LONG}) |
37 | 37 |
|
38 |
INCLUDE(FindPythonInterp) |
|
39 |
|
|
38 | 40 |
ENABLE_TESTING() |
39 | 41 |
|
40 | 42 |
ADD_SUBDIRECTORY(lemon) |
41 | 43 |
IF(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR}) |
42 | 44 |
ADD_SUBDIRECTORY(demo) |
43 | 45 |
ADD_SUBDIRECTORY(tools) |
44 | 46 |
ADD_SUBDIRECTORY(doc) |
45 | 47 |
ADD_SUBDIRECTORY(test) |
46 | 48 |
ENDIF() |
47 | 49 |
|
48 | 50 |
CONFIGURE_FILE( |
49 | 51 |
${PROJECT_SOURCE_DIR}/cmake/LEMONConfig.cmake.in |
50 | 52 |
${PROJECT_BINARY_DIR}/cmake/LEMONConfig.cmake |
51 | 53 |
@ONLY |
52 | 54 |
) |
53 | 55 |
IF(UNIX) |
54 | 56 |
INSTALL( |
55 | 57 |
FILES ${PROJECT_BINARY_DIR}/cmake/LEMONConfig.cmake |
56 | 58 |
DESTINATION share/lemon/cmake |
57 | 59 |
) |
58 | 60 |
ELSEIF(WIN32) |
59 | 61 |
INSTALL( |
60 | 62 |
FILES ${PROJECT_BINARY_DIR}/cmake/LEMONConfig.cmake |
61 | 63 |
DESTINATION cmake |
62 | 64 |
) |
63 | 65 |
ENDIF() |
64 | 66 |
|
65 | 67 |
IF(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR} AND WIN32) |
66 | 68 |
SET(CPACK_PACKAGE_NAME ${PROJECT_NAME}) |
67 | 69 |
SET(CPACK_PACKAGE_VENDOR "EGRES") |
68 | 70 |
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY |
69 | 71 |
"LEMON - Library for Efficient Modeling and Optimization in Networks") |
70 | 72 |
SET(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE") |
71 | 73 |
|
72 | 74 |
SET(CPACK_PACKAGE_VERSION ${PROJECT_VERSION}) |
73 | 75 |
|
74 | 76 |
SET(CPACK_PACKAGE_INSTALL_DIRECTORY |
75 | 77 |
"${PROJECT_NAME} ${PROJECT_VERSION}") |
76 | 78 |
SET(CPACK_PACKAGE_INSTALL_REGISTRY_KEY |
77 | 79 |
"${PROJECT_NAME} ${PROJECT_VERSION}") |
78 | 80 |
|
79 | 81 |
SET(CPACK_COMPONENTS_ALL headers library html_documentation bin) |
80 | 82 |
|
81 | 83 |
SET(CPACK_COMPONENT_HEADERS_DISPLAY_NAME "C++ headers") |
82 | 84 |
SET(CPACK_COMPONENT_LIBRARY_DISPLAY_NAME "Dynamic-link library") |
83 | 85 |
SET(CPACK_COMPONENT_BIN_DISPLAY_NAME "Command line utilities") |
84 | 86 |
SET(CPACK_COMPONENT_HTML_DOCUMENTATION_DISPLAY_NAME "HTML documentation") |
85 | 87 |
1 | 1 |
ACLOCAL_AMFLAGS = -I m4 |
2 | 2 |
|
3 | 3 |
AM_CXXFLAGS = $(WARNINGCXXFLAGS) |
4 | 4 |
|
5 | 5 |
AM_CPPFLAGS = -I$(top_srcdir) -I$(top_builddir) |
6 | 6 |
LDADD = $(top_builddir)/lemon/libemon.la |
7 | 7 |
|
8 | 8 |
EXTRA_DIST = \ |
9 | 9 |
AUTHORS \ |
10 | 10 |
LICENSE \ |
11 | 11 |
m4/lx_check_cplex.m4 \ |
12 | 12 |
m4/lx_check_glpk.m4 \ |
13 | 13 |
m4/lx_check_soplex.m4 \ |
14 | 14 |
m4/lx_check_coin.m4 \ |
15 | 15 |
CMakeLists.txt \ |
16 | 16 |
cmake/FindGhostscript.cmake \ |
17 | 17 |
cmake/FindCPLEX.cmake \ |
18 | 18 |
cmake/FindGLPK.cmake \ |
19 | 19 |
cmake/FindCOIN.cmake \ |
20 |
cmake/LEMONConfig.cmake.in \ |
|
20 | 21 |
cmake/version.cmake.in \ |
21 | 22 |
cmake/version.cmake \ |
22 | 23 |
cmake/nsis/lemon.ico \ |
23 | 24 |
cmake/nsis/uninstall.ico |
24 | 25 |
|
25 | 26 |
pkgconfigdir = $(libdir)/pkgconfig |
26 | 27 |
lemondir = $(pkgincludedir) |
27 | 28 |
bitsdir = $(lemondir)/bits |
28 | 29 |
conceptdir = $(lemondir)/concepts |
29 | 30 |
pkgconfig_DATA = |
30 | 31 |
lib_LTLIBRARIES = |
31 | 32 |
lemon_HEADERS = |
32 | 33 |
bits_HEADERS = |
33 | 34 |
concept_HEADERS = |
34 | 35 |
noinst_HEADERS = |
35 | 36 |
noinst_PROGRAMS = |
36 | 37 |
bin_PROGRAMS = |
37 | 38 |
check_PROGRAMS = |
38 | 39 |
dist_bin_SCRIPTS = |
39 | 40 |
TESTS = |
40 | 41 |
XFAIL_TESTS = |
41 | 42 |
|
42 | 43 |
include lemon/Makefile.am |
43 | 44 |
include test/Makefile.am |
44 | 45 |
include doc/Makefile.am |
45 | 46 |
include tools/Makefile.am |
47 |
include scripts/Makefile.am |
|
46 | 48 |
|
47 | 49 |
DIST_SUBDIRS = demo |
48 | 50 |
|
49 | 51 |
demo: |
50 | 52 |
$(MAKE) $(AM_MAKEFLAGS) -C demo |
51 | 53 |
|
52 | 54 |
MRPROPERFILES = \ |
53 | 55 |
aclocal.m4 \ |
54 | 56 |
config.h.in \ |
55 | 57 |
config.h.in~ \ |
56 | 58 |
configure \ |
57 | 59 |
Makefile.in \ |
58 | 60 |
build-aux/config.guess \ |
59 | 61 |
build-aux/config.sub \ |
60 | 62 |
build-aux/depcomp \ |
61 | 63 |
build-aux/install-sh \ |
62 | 64 |
build-aux/ltmain.sh \ |
63 | 65 |
build-aux/missing \ |
64 | 66 |
doc/doxygen.log |
65 | 67 |
|
66 | 68 |
mrproper: |
67 | 69 |
$(MAKE) $(AM_MAKEFLAGS) maintainer-clean |
68 | 70 |
-rm -f $(MRPROPERFILES) |
69 | 71 |
|
70 | 72 |
dist-bz2: dist |
71 | 73 |
zcat $(PACKAGE)-$(VERSION).tar.gz | \ |
72 | 74 |
bzip2 --best -c > $(PACKAGE)-$(VERSION).tar.bz2 |
73 | 75 |
|
74 | 76 |
distcheck-bz2: distcheck |
75 | 77 |
zcat $(PACKAGE)-$(VERSION).tar.gz | \ |
76 | 78 |
bzip2 --best -c > $(PACKAGE)-$(VERSION).tar.bz2 |
77 | 79 |
|
78 | 80 |
.PHONY: demo mrproper dist-bz2 distcheck-bz2 |
1 | 1 |
dnl Process this file with autoconf to produce a configure script. |
2 | 2 |
|
3 | 3 |
dnl Version information. |
4 | 4 |
m4_define([lemon_version_number], |
5 | 5 |
[m4_normalize(esyscmd([echo ${LEMON_VERSION}]))]) |
6 | 6 |
dnl m4_define([lemon_version_number], []) |
7 | 7 |
m4_define([lemon_hg_path], [m4_normalize(esyscmd([./scripts/chg-len.py]))]) |
8 | 8 |
m4_define([lemon_hg_revision], [m4_normalize(esyscmd([hg id -i 2> /dev/null]))]) |
9 | 9 |
m4_define([lemon_version], [ifelse(lemon_version_number(), |
10 | 10 |
[], |
11 | 11 |
[ifelse(lemon_hg_revision(), |
12 | 12 |
[], |
13 | 13 |
[hg-tip], |
14 | 14 |
[lemon_hg_path().lemon_hg_revision()])], |
15 | 15 |
[lemon_version_number()])]) |
16 | 16 |
|
17 | 17 |
AC_PREREQ([2.59]) |
18 | 18 |
AC_INIT([LEMON], [lemon_version()], [lemon-user@lemon.cs.elte.hu], [lemon]) |
19 | 19 |
AC_CONFIG_AUX_DIR([build-aux]) |
20 | 20 |
AC_CONFIG_MACRO_DIR([m4]) |
21 | 21 |
AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects nostdinc]) |
22 | 22 |
AC_CONFIG_SRCDIR([lemon/list_graph.h]) |
23 | 23 |
AC_CONFIG_HEADERS([config.h lemon/config.h]) |
24 | 24 |
|
25 | 25 |
AC_DEFINE([LEMON_VERSION], [lemon_version()], [The version string]) |
26 | 26 |
|
27 | 27 |
dnl Do compilation tests using the C++ compiler. |
28 | 28 |
AC_LANG([C++]) |
29 | 29 |
|
30 | 30 |
dnl Check the existence of long long type. |
31 | 31 |
AC_CHECK_TYPE(long long, [long_long_found=yes], [long_long_found=no]) |
32 | 32 |
if test x"$long_long_found" = x"yes"; then |
33 | 33 |
AC_DEFINE([LEMON_HAVE_LONG_LONG], [1], [Define to 1 if you have long long.]) |
34 | 34 |
fi |
35 | 35 |
|
36 | 36 |
dnl Checks for programs. |
37 | 37 |
AC_PROG_CXX |
38 | 38 |
AC_PROG_CXXCPP |
39 | 39 |
AC_PROG_INSTALL |
40 | 40 |
AC_DISABLE_SHARED |
41 | 41 |
AC_PROG_LIBTOOL |
42 | 42 |
|
43 | 43 |
AC_CHECK_PROG([doxygen_found],[doxygen],[yes],[no]) |
44 |
AC_CHECK_PROG([python_found],[python],[yes],[no]) |
|
44 | 45 |
AC_CHECK_PROG([gs_found],[gs],[yes],[no]) |
45 | 46 |
|
46 | 47 |
dnl Detect Intel compiler. |
47 | 48 |
AC_MSG_CHECKING([whether we are using the Intel C++ compiler]) |
48 | 49 |
AC_COMPILE_IFELSE([#ifndef __INTEL_COMPILER |
49 | 50 |
choke me |
50 | 51 |
#endif], [ICC=[yes]], [ICC=[no]]) |
51 | 52 |
if test x"$ICC" = x"yes"; then |
52 | 53 |
AC_MSG_RESULT([yes]) |
53 | 54 |
else |
54 | 55 |
AC_MSG_RESULT([no]) |
55 | 56 |
fi |
56 | 57 |
|
57 | 58 |
dnl Set custom compiler flags when using g++. |
58 | 59 |
if test "$GXX" = yes -a "$ICC" = no; then |
59 | 60 |
WARNINGCXXFLAGS="-Wall -W -Wall -W -Wunused -Wformat=2 -Wctor-dtor-privacy -Wnon-virtual-dtor -Wno-char-subscripts -Wwrite-strings -Wno-char-subscripts -Wreturn-type -Wcast-qual -Wcast-align -Wsign-promo -Woverloaded-virtual -ansi -fno-strict-aliasing -Wold-style-cast -Wno-unknown-pragmas" |
60 | 61 |
fi |
61 | 62 |
AC_SUBST([WARNINGCXXFLAGS]) |
62 | 63 |
|
63 | 64 |
dnl Checks for libraries. |
64 | 65 |
LX_CHECK_GLPK |
65 | 66 |
LX_CHECK_CPLEX |
66 | 67 |
LX_CHECK_SOPLEX |
67 | 68 |
LX_CHECK_COIN |
68 | 69 |
|
69 | 70 |
AM_CONDITIONAL([HAVE_LP], [test x"$lx_lp_found" = x"yes"]) |
70 | 71 |
AM_CONDITIONAL([HAVE_MIP], [test x"$lx_mip_found" = x"yes"]) |
71 | 72 |
|
72 | 73 |
dnl Disable/enable building the binary tools. |
73 | 74 |
AC_ARG_ENABLE([tools], |
74 | 75 |
AS_HELP_STRING([--enable-tools], [build additional tools @<:@default@:>@]) |
75 | 76 |
AS_HELP_STRING([--disable-tools], [do not build additional tools]), |
76 | 77 |
[], [enable_tools=yes]) |
77 | 78 |
AC_MSG_CHECKING([whether to build the additional tools]) |
78 | 79 |
if test x"$enable_tools" != x"no"; then |
79 | 80 |
AC_MSG_RESULT([yes]) |
80 | 81 |
else |
81 | 82 |
AC_MSG_RESULT([no]) |
82 | 83 |
fi |
83 | 84 |
AM_CONDITIONAL([WANT_TOOLS], [test x"$enable_tools" != x"no"]) |
84 | 85 |
|
86 |
dnl Support for running test cases using valgrind. |
|
87 |
use_valgrind=no |
|
88 |
AC_ARG_ENABLE([valgrind], |
|
89 |
AS_HELP_STRING([--enable-valgrind], [use valgrind when running tests]), |
|
90 |
[use_valgrind=yes]) |
|
91 |
|
|
92 |
if [[ "$use_valgrind" = "yes" ]]; then |
|
93 |
AC_CHECK_PROG(HAVE_VALGRIND, valgrind, yes, no) |
|
94 |
|
|
95 |
if [[ "$HAVE_VALGRIND" = "no" ]]; then |
|
96 |
AC_MSG_ERROR([Valgrind not found in PATH.]) |
|
97 |
fi |
|
98 |
fi |
|
99 |
AM_CONDITIONAL(USE_VALGRIND, [test "$use_valgrind" = "yes"]) |
|
100 |
|
|
85 | 101 |
dnl Checks for header files. |
86 | 102 |
AC_CHECK_HEADERS(limits.h sys/time.h sys/times.h unistd.h) |
87 | 103 |
|
88 | 104 |
dnl Checks for typedefs, structures, and compiler characteristics. |
89 | 105 |
AC_C_CONST |
90 | 106 |
AC_C_INLINE |
91 | 107 |
AC_TYPE_SIZE_T |
92 | 108 |
AC_HEADER_TIME |
93 | 109 |
AC_STRUCT_TM |
94 | 110 |
|
95 | 111 |
dnl Checks for library functions. |
96 | 112 |
AC_HEADER_STDC |
97 | 113 |
AC_CHECK_FUNCS(gettimeofday times ctime_r) |
98 | 114 |
|
99 | 115 |
dnl Add dependencies on files generated by configure. |
100 | 116 |
AC_SUBST([CONFIG_STATUS_DEPENDENCIES], |
101 | 117 |
['$(top_srcdir)/doc/Doxyfile.in $(top_srcdir)/lemon/lemon.pc.in $(top_srcdir)/cmake/version.cmake.in']) |
102 | 118 |
|
103 | 119 |
AC_CONFIG_FILES([ |
104 | 120 |
Makefile |
105 | 121 |
demo/Makefile |
106 | 122 |
cmake/version.cmake |
107 | 123 |
doc/Doxyfile |
108 | 124 |
lemon/lemon.pc |
109 | 125 |
]) |
110 | 126 |
|
111 | 127 |
AC_OUTPUT |
112 | 128 |
|
113 | 129 |
echo |
114 | 130 |
echo '****************************** SUMMARY ******************************' |
115 | 131 |
echo |
116 | 132 |
echo Package version............... : $PACKAGE-$VERSION |
117 | 133 |
echo |
118 | 134 |
echo C++ compiler.................. : $CXX |
119 | 135 |
echo C++ compiles flags............ : $WARNINGCXXFLAGS $CXXFLAGS |
120 | 136 |
echo |
121 | 137 |
echo Compiler supports long long... : $long_long_found |
122 | 138 |
echo |
123 | 139 |
echo GLPK support.................. : $lx_glpk_found |
124 | 140 |
echo CPLEX support................. : $lx_cplex_found |
125 | 141 |
echo SOPLEX support................ : $lx_soplex_found |
126 | 142 |
echo CLP support................... : $lx_clp_found |
127 | 143 |
echo CBC support................... : $lx_cbc_found |
128 | 144 |
echo |
129 | 145 |
echo Build additional tools........ : $enable_tools |
146 |
echo Use valgrind for tests........ : $use_valgrind |
|
130 | 147 |
echo |
131 | 148 |
echo The packace will be installed in |
132 | 149 |
echo -n ' ' |
133 | 150 |
echo $prefix. |
134 | 151 |
echo |
135 | 152 |
echo '*********************************************************************' |
136 | 153 |
|
137 | 154 |
echo |
138 | 155 |
echo Configure complete, now type \'make\' and then \'make install\'. |
139 | 156 |
echo |
1 | 1 |
SET(PACKAGE_NAME ${PROJECT_NAME}) |
2 | 2 |
SET(PACKAGE_VERSION ${PROJECT_VERSION}) |
3 | 3 |
SET(abs_top_srcdir ${PROJECT_SOURCE_DIR}) |
4 | 4 |
SET(abs_top_builddir ${PROJECT_BINARY_DIR}) |
5 | 5 |
|
6 | 6 |
CONFIGURE_FILE( |
7 | 7 |
${PROJECT_SOURCE_DIR}/doc/Doxyfile.in |
8 | 8 |
${PROJECT_BINARY_DIR}/doc/Doxyfile |
9 | 9 |
@ONLY |
10 | 10 |
) |
11 | 11 |
|
12 |
IF(DOXYGEN_EXECUTABLE AND GHOSTSCRIPT_EXECUTABLE) |
|
12 |
IF(DOXYGEN_EXECUTABLE AND PYTHONINTERP_FOUND AND GHOSTSCRIPT_EXECUTABLE) |
|
13 | 13 |
FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html/) |
14 | 14 |
SET(GHOSTSCRIPT_OPTIONS -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sDEVICE=pngalpha) |
15 | 15 |
ADD_CUSTOM_TARGET(html |
16 | 16 |
COMMAND ${CMAKE_COMMAND} -E remove_directory gen-images |
17 | 17 |
COMMAND ${CMAKE_COMMAND} -E make_directory gen-images |
18 | 18 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/bipartite_matching.png ${CMAKE_CURRENT_SOURCE_DIR}/images/bipartite_matching.eps |
19 | 19 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/bipartite_partitions.png ${CMAKE_CURRENT_SOURCE_DIR}/images/bipartite_partitions.eps |
20 | 20 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/connected_components.png ${CMAKE_CURRENT_SOURCE_DIR}/images/connected_components.eps |
21 | 21 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/edge_biconnected_components.png ${CMAKE_CURRENT_SOURCE_DIR}/images/edge_biconnected_components.eps |
22 | 22 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/grid_graph.png ${CMAKE_CURRENT_SOURCE_DIR}/images/grid_graph.eps |
23 | 23 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/node_biconnected_components.png ${CMAKE_CURRENT_SOURCE_DIR}/images/node_biconnected_components.eps |
24 | 24 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/nodeshape_0.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_0.eps |
25 | 25 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/nodeshape_1.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_1.eps |
26 | 26 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/nodeshape_2.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_2.eps |
27 | 27 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/nodeshape_3.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_3.eps |
28 | 28 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/nodeshape_4.png ${CMAKE_CURRENT_SOURCE_DIR}/images/nodeshape_4.eps |
29 | 29 |
COMMAND ${GHOSTSCRIPT_EXECUTABLE} ${GHOSTSCRIPT_OPTIONS} -r18 -sOutputFile=gen-images/strongly_connected_components.png ${CMAKE_CURRENT_SOURCE_DIR}/images/strongly_connected_components.eps |
30 | 30 |
COMMAND ${CMAKE_COMMAND} -E remove_directory html |
31 |
COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/scripts/bib2dox.py ${CMAKE_CURRENT_SOURCE_DIR}/references.bib >references.dox |
|
31 | 32 |
COMMAND ${DOXYGEN_EXECUTABLE} Doxyfile |
32 | 33 |
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} |
33 | 34 |
) |
34 | 35 |
|
35 | 36 |
SET_TARGET_PROPERTIES(html PROPERTIES PROJECT_LABEL BUILD_DOC) |
36 | 37 |
|
37 | 38 |
IF(UNIX) |
38 | 39 |
INSTALL( |
39 | 40 |
DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html/ |
40 | 41 |
DESTINATION share/doc/lemon/html |
41 | 42 |
COMPONENT html_documentation |
42 | 43 |
) |
43 | 44 |
ELSEIF(WIN32) |
44 | 45 |
INSTALL( |
45 | 46 |
DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html/ |
46 | 47 |
DESTINATION doc |
47 | 48 |
COMPONENT html_documentation |
48 | 49 |
) |
49 | 50 |
ENDIF() |
50 | 51 |
|
51 | 52 |
ENDIF() |
1 |
# Doxyfile 1.5. |
|
1 |
# Doxyfile 1.5.9 |
|
2 | 2 |
|
3 | 3 |
#--------------------------------------------------------------------------- |
4 | 4 |
# Project related configuration options |
5 | 5 |
#--------------------------------------------------------------------------- |
6 | 6 |
DOXYFILE_ENCODING = UTF-8 |
7 | 7 |
PROJECT_NAME = @PACKAGE_NAME@ |
8 | 8 |
PROJECT_NUMBER = @PACKAGE_VERSION@ |
9 | 9 |
OUTPUT_DIRECTORY = |
10 | 10 |
CREATE_SUBDIRS = NO |
11 | 11 |
OUTPUT_LANGUAGE = English |
12 | 12 |
BRIEF_MEMBER_DESC = YES |
13 | 13 |
REPEAT_BRIEF = NO |
14 | 14 |
ABBREVIATE_BRIEF = |
15 | 15 |
ALWAYS_DETAILED_SEC = NO |
16 | 16 |
INLINE_INHERITED_MEMB = NO |
17 | 17 |
FULL_PATH_NAMES = YES |
18 | 18 |
STRIP_FROM_PATH = "@abs_top_srcdir@" |
19 | 19 |
STRIP_FROM_INC_PATH = "@abs_top_srcdir@" |
20 | 20 |
SHORT_NAMES = YES |
21 | 21 |
JAVADOC_AUTOBRIEF = NO |
22 | 22 |
QT_AUTOBRIEF = NO |
23 | 23 |
MULTILINE_CPP_IS_BRIEF = NO |
24 |
DETAILS_AT_TOP = YES |
|
25 | 24 |
INHERIT_DOCS = NO |
26 | 25 |
SEPARATE_MEMBER_PAGES = NO |
27 | 26 |
TAB_SIZE = 8 |
28 | 27 |
ALIASES = |
29 | 28 |
OPTIMIZE_OUTPUT_FOR_C = NO |
30 | 29 |
OPTIMIZE_OUTPUT_JAVA = NO |
31 | 30 |
OPTIMIZE_FOR_FORTRAN = NO |
32 | 31 |
OPTIMIZE_OUTPUT_VHDL = NO |
33 | 32 |
BUILTIN_STL_SUPPORT = YES |
34 | 33 |
CPP_CLI_SUPPORT = NO |
35 | 34 |
SIP_SUPPORT = NO |
36 | 35 |
IDL_PROPERTY_SUPPORT = YES |
37 | 36 |
DISTRIBUTE_GROUP_DOC = NO |
38 | 37 |
SUBGROUPING = YES |
39 | 38 |
TYPEDEF_HIDES_STRUCT = NO |
40 | 39 |
SYMBOL_CACHE_SIZE = 0 |
41 | 40 |
#--------------------------------------------------------------------------- |
42 | 41 |
# Build related configuration options |
43 | 42 |
#--------------------------------------------------------------------------- |
44 | 43 |
EXTRACT_ALL = NO |
45 | 44 |
EXTRACT_PRIVATE = YES |
46 | 45 |
EXTRACT_STATIC = YES |
47 | 46 |
EXTRACT_LOCAL_CLASSES = NO |
48 | 47 |
EXTRACT_LOCAL_METHODS = NO |
49 | 48 |
EXTRACT_ANON_NSPACES = NO |
50 | 49 |
HIDE_UNDOC_MEMBERS = YES |
51 | 50 |
HIDE_UNDOC_CLASSES = YES |
52 | 51 |
HIDE_FRIEND_COMPOUNDS = NO |
53 | 52 |
HIDE_IN_BODY_DOCS = NO |
54 | 53 |
INTERNAL_DOCS = NO |
55 | 54 |
CASE_SENSE_NAMES = YES |
56 | 55 |
HIDE_SCOPE_NAMES = YES |
57 | 56 |
SHOW_INCLUDE_FILES = YES |
58 | 57 |
INLINE_INFO = YES |
59 | 58 |
SORT_MEMBER_DOCS = NO |
60 | 59 |
SORT_BRIEF_DOCS = NO |
61 | 60 |
SORT_GROUP_NAMES = NO |
62 | 61 |
SORT_BY_SCOPE_NAME = NO |
63 | 62 |
GENERATE_TODOLIST = YES |
64 | 63 |
GENERATE_TESTLIST = YES |
65 | 64 |
GENERATE_BUGLIST = YES |
66 | 65 |
GENERATE_DEPRECATEDLIST= YES |
67 | 66 |
ENABLED_SECTIONS = |
68 | 67 |
MAX_INITIALIZER_LINES = 5 |
69 | 68 |
SHOW_USED_FILES = NO |
70 | 69 |
SHOW_DIRECTORIES = YES |
71 | 70 |
SHOW_FILES = YES |
72 | 71 |
SHOW_NAMESPACES = YES |
73 | 72 |
FILE_VERSION_FILTER = |
74 | 73 |
LAYOUT_FILE = DoxygenLayout.xml |
75 | 74 |
#--------------------------------------------------------------------------- |
76 | 75 |
# configuration options related to warning and progress messages |
77 | 76 |
#--------------------------------------------------------------------------- |
78 | 77 |
QUIET = NO |
79 | 78 |
WARNINGS = YES |
80 | 79 |
WARN_IF_UNDOCUMENTED = YES |
81 | 80 |
WARN_IF_DOC_ERROR = YES |
82 | 81 |
WARN_NO_PARAMDOC = NO |
83 | 82 |
WARN_FORMAT = "$file:$line: $text" |
84 | 83 |
WARN_LOGFILE = doxygen.log |
85 | 84 |
#--------------------------------------------------------------------------- |
86 | 85 |
# configuration options related to the input files |
87 | 86 |
#--------------------------------------------------------------------------- |
88 | 87 |
INPUT = "@abs_top_srcdir@/doc" \ |
89 | 88 |
"@abs_top_srcdir@/lemon" \ |
90 | 89 |
"@abs_top_srcdir@/lemon/bits" \ |
91 | 90 |
"@abs_top_srcdir@/lemon/concepts" \ |
92 | 91 |
"@abs_top_srcdir@/demo" \ |
93 | 92 |
"@abs_top_srcdir@/tools" \ |
94 |
"@abs_top_srcdir@/test/test_tools.h" |
|
93 |
"@abs_top_srcdir@/test/test_tools.h" \ |
|
94 |
"@abs_top_builddir@/doc/references.dox" |
|
95 | 95 |
INPUT_ENCODING = UTF-8 |
96 | 96 |
FILE_PATTERNS = *.h \ |
97 | 97 |
*.cc \ |
98 | 98 |
*.dox |
99 | 99 |
RECURSIVE = NO |
100 | 100 |
EXCLUDE = |
101 | 101 |
EXCLUDE_SYMLINKS = NO |
102 | 102 |
EXCLUDE_PATTERNS = |
103 | 103 |
EXCLUDE_SYMBOLS = |
104 | 104 |
EXAMPLE_PATH = "@abs_top_srcdir@/demo" \ |
105 | 105 |
"@abs_top_srcdir@/LICENSE" \ |
106 | 106 |
"@abs_top_srcdir@/doc" |
107 | 107 |
EXAMPLE_PATTERNS = |
108 | 108 |
EXAMPLE_RECURSIVE = NO |
109 | 109 |
IMAGE_PATH = "@abs_top_srcdir@/doc/images" \ |
110 | 110 |
"@abs_top_builddir@/doc/gen-images" |
111 | 111 |
INPUT_FILTER = |
112 | 112 |
FILTER_PATTERNS = |
113 | 113 |
FILTER_SOURCE_FILES = NO |
114 | 114 |
#--------------------------------------------------------------------------- |
115 | 115 |
# configuration options related to source browsing |
116 | 116 |
#--------------------------------------------------------------------------- |
117 | 117 |
SOURCE_BROWSER = NO |
118 | 118 |
INLINE_SOURCES = NO |
119 | 119 |
STRIP_CODE_COMMENTS = YES |
120 | 120 |
REFERENCED_BY_RELATION = NO |
121 | 121 |
REFERENCES_RELATION = NO |
122 | 122 |
REFERENCES_LINK_SOURCE = YES |
123 | 123 |
USE_HTAGS = NO |
124 | 124 |
VERBATIM_HEADERS = NO |
125 | 125 |
#--------------------------------------------------------------------------- |
126 | 126 |
# configuration options related to the alphabetical class index |
127 | 127 |
#--------------------------------------------------------------------------- |
128 | 128 |
ALPHABETICAL_INDEX = YES |
129 | 129 |
COLS_IN_ALPHA_INDEX = 2 |
130 | 130 |
IGNORE_PREFIX = |
131 | 131 |
#--------------------------------------------------------------------------- |
132 | 132 |
# configuration options related to the HTML output |
133 | 133 |
#--------------------------------------------------------------------------- |
134 | 134 |
GENERATE_HTML = YES |
135 | 135 |
HTML_OUTPUT = html |
136 | 136 |
HTML_FILE_EXTENSION = .html |
137 | 137 |
HTML_HEADER = |
138 | 138 |
HTML_FOOTER = |
139 | 139 |
HTML_STYLESHEET = |
140 | 140 |
HTML_ALIGN_MEMBERS = YES |
141 | 141 |
HTML_DYNAMIC_SECTIONS = NO |
142 | 142 |
GENERATE_DOCSET = NO |
... | ... |
@@ -178,89 +178,89 @@ |
178 | 178 |
#--------------------------------------------------------------------------- |
179 | 179 |
# configuration options related to the RTF output |
180 | 180 |
#--------------------------------------------------------------------------- |
181 | 181 |
GENERATE_RTF = NO |
182 | 182 |
RTF_OUTPUT = rtf |
183 | 183 |
COMPACT_RTF = NO |
184 | 184 |
RTF_HYPERLINKS = NO |
185 | 185 |
RTF_STYLESHEET_FILE = |
186 | 186 |
RTF_EXTENSIONS_FILE = |
187 | 187 |
#--------------------------------------------------------------------------- |
188 | 188 |
# configuration options related to the man page output |
189 | 189 |
#--------------------------------------------------------------------------- |
190 | 190 |
GENERATE_MAN = NO |
191 | 191 |
MAN_OUTPUT = man |
192 | 192 |
MAN_EXTENSION = .3 |
193 | 193 |
MAN_LINKS = NO |
194 | 194 |
#--------------------------------------------------------------------------- |
195 | 195 |
# configuration options related to the XML output |
196 | 196 |
#--------------------------------------------------------------------------- |
197 | 197 |
GENERATE_XML = NO |
198 | 198 |
XML_OUTPUT = xml |
199 | 199 |
XML_SCHEMA = |
200 | 200 |
XML_DTD = |
201 | 201 |
XML_PROGRAMLISTING = YES |
202 | 202 |
#--------------------------------------------------------------------------- |
203 | 203 |
# configuration options for the AutoGen Definitions output |
204 | 204 |
#--------------------------------------------------------------------------- |
205 | 205 |
GENERATE_AUTOGEN_DEF = NO |
206 | 206 |
#--------------------------------------------------------------------------- |
207 | 207 |
# configuration options related to the Perl module output |
208 | 208 |
#--------------------------------------------------------------------------- |
209 | 209 |
GENERATE_PERLMOD = NO |
210 | 210 |
PERLMOD_LATEX = NO |
211 | 211 |
PERLMOD_PRETTY = YES |
212 | 212 |
PERLMOD_MAKEVAR_PREFIX = |
213 | 213 |
#--------------------------------------------------------------------------- |
214 | 214 |
# Configuration options related to the preprocessor |
215 | 215 |
#--------------------------------------------------------------------------- |
216 | 216 |
ENABLE_PREPROCESSING = YES |
217 | 217 |
MACRO_EXPANSION = NO |
218 | 218 |
EXPAND_ONLY_PREDEF = NO |
219 | 219 |
SEARCH_INCLUDES = YES |
220 | 220 |
INCLUDE_PATH = |
221 | 221 |
INCLUDE_FILE_PATTERNS = |
222 | 222 |
PREDEFINED = DOXYGEN |
223 | 223 |
EXPAND_AS_DEFINED = |
224 | 224 |
SKIP_FUNCTION_MACROS = YES |
225 | 225 |
#--------------------------------------------------------------------------- |
226 |
# |
|
226 |
# Options related to the search engine |
|
227 | 227 |
#--------------------------------------------------------------------------- |
228 | 228 |
TAGFILES = "@abs_top_srcdir@/doc/libstdc++.tag = http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/ " |
229 | 229 |
GENERATE_TAGFILE = html/lemon.tag |
230 | 230 |
ALLEXTERNALS = NO |
231 | 231 |
EXTERNAL_GROUPS = NO |
232 | 232 |
PERL_PATH = /usr/bin/perl |
233 | 233 |
#--------------------------------------------------------------------------- |
234 | 234 |
# Configuration options related to the dot tool |
235 | 235 |
#--------------------------------------------------------------------------- |
236 | 236 |
CLASS_DIAGRAMS = YES |
237 | 237 |
MSCGEN_PATH = |
238 | 238 |
HIDE_UNDOC_RELATIONS = YES |
239 | 239 |
HAVE_DOT = YES |
240 | 240 |
DOT_FONTNAME = FreeSans |
241 | 241 |
DOT_FONTSIZE = 10 |
242 | 242 |
DOT_FONTPATH = |
243 | 243 |
CLASS_GRAPH = YES |
244 | 244 |
COLLABORATION_GRAPH = NO |
245 | 245 |
GROUP_GRAPHS = NO |
246 | 246 |
UML_LOOK = NO |
247 | 247 |
TEMPLATE_RELATIONS = NO |
248 | 248 |
INCLUDE_GRAPH = NO |
249 | 249 |
INCLUDED_BY_GRAPH = NO |
250 | 250 |
CALL_GRAPH = NO |
251 | 251 |
CALLER_GRAPH = NO |
252 | 252 |
GRAPHICAL_HIERARCHY = NO |
253 | 253 |
DIRECTORY_GRAPH = NO |
254 | 254 |
DOT_IMAGE_FORMAT = png |
255 | 255 |
DOT_PATH = |
256 | 256 |
DOTFILE_DIRS = |
257 | 257 |
DOT_GRAPH_MAX_NODES = 50 |
258 | 258 |
MAX_DOT_GRAPH_DEPTH = 0 |
259 | 259 |
DOT_TRANSPARENT = NO |
260 | 260 |
DOT_MULTI_TARGETS = NO |
261 | 261 |
GENERATE_LEGEND = YES |
262 | 262 |
DOT_CLEANUP = YES |
263 | 263 |
#--------------------------------------------------------------------------- |
264 | 264 |
# Configuration::additions related to the search engine |
265 | 265 |
#--------------------------------------------------------------------------- |
266 | 266 |
SEARCHENGINE = NO |
... | ... |
@@ -21,89 +21,101 @@ |
21 | 21 |
nodeshape_2.eps \ |
22 | 22 |
nodeshape_3.eps \ |
23 | 23 |
nodeshape_4.eps |
24 | 24 |
|
25 | 25 |
DOC_EPS_IMAGES27 = \ |
26 | 26 |
bipartite_matching.eps \ |
27 | 27 |
bipartite_partitions.eps \ |
28 | 28 |
connected_components.eps \ |
29 | 29 |
edge_biconnected_components.eps \ |
30 | 30 |
node_biconnected_components.eps \ |
31 | 31 |
strongly_connected_components.eps |
32 | 32 |
|
33 | 33 |
DOC_EPS_IMAGES = \ |
34 | 34 |
$(DOC_EPS_IMAGES18) \ |
35 | 35 |
$(DOC_EPS_IMAGES27) |
36 | 36 |
|
37 | 37 |
DOC_PNG_IMAGES = \ |
38 | 38 |
$(DOC_EPS_IMAGES:%.eps=doc/gen-images/%.png) |
39 | 39 |
|
40 | 40 |
EXTRA_DIST += $(DOC_EPS_IMAGES:%=doc/images/%) |
41 | 41 |
|
42 | 42 |
doc/html: |
43 | 43 |
$(MAKE) $(AM_MAKEFLAGS) html |
44 | 44 |
|
45 | 45 |
GS_COMMAND=gs -dNOPAUSE -dBATCH -q -dEPSCrop -dTextAlphaBits=4 -dGraphicsAlphaBits=4 |
46 | 46 |
|
47 | 47 |
$(DOC_EPS_IMAGES18:%.eps=doc/gen-images/%.png): doc/gen-images/%.png: doc/images/%.eps |
48 | 48 |
-mkdir doc/gen-images |
49 | 49 |
if test ${gs_found} = yes; then \ |
50 | 50 |
$(GS_COMMAND) -sDEVICE=pngalpha -r18 -sOutputFile=$@ $<; \ |
51 | 51 |
else \ |
52 | 52 |
echo; \ |
53 | 53 |
echo "Ghostscript not found."; \ |
54 | 54 |
echo; \ |
55 | 55 |
exit 1; \ |
56 | 56 |
fi |
57 | 57 |
|
58 | 58 |
$(DOC_EPS_IMAGES27:%.eps=doc/gen-images/%.png): doc/gen-images/%.png: doc/images/%.eps |
59 | 59 |
-mkdir doc/gen-images |
60 | 60 |
if test ${gs_found} = yes; then \ |
61 | 61 |
$(GS_COMMAND) -sDEVICE=pngalpha -r27 -sOutputFile=$@ $<; \ |
62 | 62 |
else \ |
63 | 63 |
echo; \ |
64 | 64 |
echo "Ghostscript not found."; \ |
65 | 65 |
echo; \ |
66 | 66 |
exit 1; \ |
67 | 67 |
fi |
68 | 68 |
|
69 |
|
|
69 |
references.dox: doc/references.bib |
|
70 |
if test ${python_found} = yes; then \ |
|
71 |
cd doc; \ |
|
72 |
python @abs_top_srcdir@/scripts/bib2dox.py @abs_top_builddir@/$< >$@; \ |
|
73 |
cd ..; \ |
|
74 |
else \ |
|
75 |
echo; \ |
|
76 |
echo "Python not found."; \ |
|
77 |
echo; \ |
|
78 |
exit 1; \ |
|
79 |
fi |
|
80 |
|
|
81 |
html-local: $(DOC_PNG_IMAGES) references.dox |
|
70 | 82 |
if test ${doxygen_found} = yes; then \ |
71 | 83 |
cd doc; \ |
72 | 84 |
doxygen Doxyfile; \ |
73 | 85 |
cd ..; \ |
74 | 86 |
else \ |
75 | 87 |
echo; \ |
76 | 88 |
echo "Doxygen not found."; \ |
77 | 89 |
echo; \ |
78 | 90 |
exit 1; \ |
79 | 91 |
fi |
80 | 92 |
|
81 | 93 |
clean-local: |
82 | 94 |
-rm -rf doc/html |
83 | 95 |
-rm -f doc/doxygen.log |
84 | 96 |
-rm -f $(DOC_PNG_IMAGES) |
85 | 97 |
-rm -rf doc/gen-images |
86 | 98 |
|
87 | 99 |
update-external-tags: |
88 | 100 |
wget -O doc/libstdc++.tag.tmp http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/libstdc++.tag && \ |
89 | 101 |
mv doc/libstdc++.tag.tmp doc/libstdc++.tag || \ |
90 | 102 |
rm doc/libstdc++.tag.tmp |
91 | 103 |
|
92 | 104 |
install-html-local: doc/html |
93 | 105 |
@$(NORMAL_INSTALL) |
94 | 106 |
$(mkinstalldirs) $(DESTDIR)$(htmldir)/html |
95 | 107 |
for p in doc/html/*.{html,css,png,map,gif,tag} ; do \ |
96 | 108 |
f="`echo $$p | sed -e 's|^.*/||'`"; \ |
97 | 109 |
echo " $(INSTALL_DATA) $$p $(DESTDIR)$(htmldir)/html/$$f"; \ |
98 | 110 |
$(INSTALL_DATA) $$p $(DESTDIR)$(htmldir)/html/$$f; \ |
99 | 111 |
done |
100 | 112 |
|
101 | 113 |
uninstall-local: |
102 | 114 |
@$(NORMAL_UNINSTALL) |
103 | 115 |
for p in doc/html/*.{html,css,png,map,gif,tag} ; do \ |
104 | 116 |
f="`echo $$p | sed -e 's|^.*/||'`"; \ |
105 | 117 |
echo " rm -f $(DESTDIR)$(htmldir)/html/$$f"; \ |
106 | 118 |
rm -f $(DESTDIR)$(htmldir)/html/$$f; \ |
107 | 119 |
done |
108 | 120 |
|
109 | 121 |
.PHONY: update-external-tags |
... | ... |
@@ -181,492 +181,585 @@ |
181 | 181 |
usage of map adaptors with the \c graphToEps() function. |
182 | 182 |
\code |
183 | 183 |
Color nodeColor(int deg) { |
184 | 184 |
if (deg >= 2) { |
185 | 185 |
return Color(0.5, 0.0, 0.5); |
186 | 186 |
} else if (deg == 1) { |
187 | 187 |
return Color(1.0, 0.5, 1.0); |
188 | 188 |
} else { |
189 | 189 |
return Color(0.0, 0.0, 0.0); |
190 | 190 |
} |
191 | 191 |
} |
192 | 192 |
|
193 | 193 |
Digraph::NodeMap<int> degree_map(graph); |
194 | 194 |
|
195 | 195 |
graphToEps(graph, "graph.eps") |
196 | 196 |
.coords(coords).scaleToA4().undirected() |
197 | 197 |
.nodeColors(composeMap(functorToMap(nodeColor), degree_map)) |
198 | 198 |
.run(); |
199 | 199 |
\endcode |
200 | 200 |
The \c functorToMap() function makes an \c int to \c Color map from the |
201 | 201 |
\c nodeColor() function. The \c composeMap() compose the \c degree_map |
202 | 202 |
and the previously created map. The composed map is a proper function to |
203 | 203 |
get the color of each node. |
204 | 204 |
|
205 | 205 |
The usage with class type algorithms is little bit harder. In this |
206 | 206 |
case the function type map adaptors can not be used, because the |
207 | 207 |
function map adaptors give back temporary objects. |
208 | 208 |
\code |
209 | 209 |
Digraph graph; |
210 | 210 |
|
211 | 211 |
typedef Digraph::ArcMap<double> DoubleArcMap; |
212 | 212 |
DoubleArcMap length(graph); |
213 | 213 |
DoubleArcMap speed(graph); |
214 | 214 |
|
215 | 215 |
typedef DivMap<DoubleArcMap, DoubleArcMap> TimeMap; |
216 | 216 |
TimeMap time(length, speed); |
217 | 217 |
|
218 | 218 |
Dijkstra<Digraph, TimeMap> dijkstra(graph, time); |
219 | 219 |
dijkstra.run(source, target); |
220 | 220 |
\endcode |
221 | 221 |
We have a length map and a maximum speed map on the arcs of a digraph. |
222 | 222 |
The minimum time to pass the arc can be calculated as the division of |
223 | 223 |
the two maps which can be done implicitly with the \c DivMap template |
224 | 224 |
class. We use the implicit minimum time map as the length map of the |
225 | 225 |
\c Dijkstra algorithm. |
226 | 226 |
*/ |
227 | 227 |
|
228 | 228 |
/** |
229 |
@defgroup matrices Matrices |
|
230 |
@ingroup datas |
|
231 |
\brief Two dimensional data storages implemented in LEMON. |
|
232 |
|
|
233 |
This group contains two dimensional data storages implemented in LEMON. |
|
234 |
*/ |
|
235 |
|
|
236 |
/** |
|
237 | 229 |
@defgroup paths Path Structures |
238 | 230 |
@ingroup datas |
239 | 231 |
\brief %Path structures implemented in LEMON. |
240 | 232 |
|
241 | 233 |
This group contains the path structures implemented in LEMON. |
242 | 234 |
|
243 | 235 |
LEMON provides flexible data structures to work with paths. |
244 | 236 |
All of them have similar interfaces and they can be copied easily with |
245 | 237 |
assignment operators and copy constructors. This makes it easy and |
246 | 238 |
efficient to have e.g. the Dijkstra algorithm to store its result in |
247 | 239 |
any kind of path structure. |
248 | 240 |
|
249 |
\sa |
|
241 |
\sa \ref concepts::Path "Path concept" |
|
242 |
*/ |
|
243 |
|
|
244 |
/** |
|
245 |
@defgroup heaps Heap Structures |
|
246 |
@ingroup datas |
|
247 |
\brief %Heap structures implemented in LEMON. |
|
248 |
|
|
249 |
This group contains the heap structures implemented in LEMON. |
|
250 |
|
|
251 |
LEMON provides several heap classes. They are efficient implementations |
|
252 |
of the abstract data type \e priority \e queue. They store items with |
|
253 |
specified values called \e priorities in such a way that finding and |
|
254 |
removing the item with minimum priority are efficient. |
|
255 |
The basic operations are adding and erasing items, changing the priority |
|
256 |
of an item, etc. |
|
257 |
|
|
258 |
Heaps are crucial in several algorithms, such as Dijkstra and Prim. |
|
259 |
The heap implementations have the same interface, thus any of them can be |
|
260 |
used easily in such algorithms. |
|
261 |
|
|
262 |
\sa \ref concepts::Heap "Heap concept" |
|
263 |
*/ |
|
264 |
|
|
265 |
/** |
|
266 |
@defgroup matrices Matrices |
|
267 |
@ingroup datas |
|
268 |
\brief Two dimensional data storages implemented in LEMON. |
|
269 |
|
|
270 |
This group contains two dimensional data storages implemented in LEMON. |
|
250 | 271 |
*/ |
251 | 272 |
|
252 | 273 |
/** |
253 | 274 |
@defgroup auxdat Auxiliary Data Structures |
254 | 275 |
@ingroup datas |
255 | 276 |
\brief Auxiliary data structures implemented in LEMON. |
256 | 277 |
|
257 | 278 |
This group contains some data structures implemented in LEMON in |
258 | 279 |
order to make it easier to implement combinatorial algorithms. |
259 | 280 |
*/ |
260 | 281 |
|
261 | 282 |
/** |
283 |
@defgroup geomdat Geometric Data Structures |
|
284 |
@ingroup auxdat |
|
285 |
\brief Geometric data structures implemented in LEMON. |
|
286 |
|
|
287 |
This group contains geometric data structures implemented in LEMON. |
|
288 |
|
|
289 |
- \ref lemon::dim2::Point "dim2::Point" implements a two dimensional |
|
290 |
vector with the usual operations. |
|
291 |
- \ref lemon::dim2::Box "dim2::Box" can be used to determine the |
|
292 |
rectangular bounding box of a set of \ref lemon::dim2::Point |
|
293 |
"dim2::Point"'s. |
|
294 |
*/ |
|
295 |
|
|
296 |
/** |
|
297 |
@defgroup matrices Matrices |
|
298 |
@ingroup auxdat |
|
299 |
\brief Two dimensional data storages implemented in LEMON. |
|
300 |
|
|
301 |
This group contains two dimensional data storages implemented in LEMON. |
|
302 |
*/ |
|
303 |
|
|
304 |
/** |
|
262 | 305 |
@defgroup algs Algorithms |
263 | 306 |
\brief This group contains the several algorithms |
264 | 307 |
implemented in LEMON. |
265 | 308 |
|
266 | 309 |
This group contains the several algorithms |
267 | 310 |
implemented in LEMON. |
268 | 311 |
*/ |
269 | 312 |
|
270 | 313 |
/** |
271 | 314 |
@defgroup search Graph Search |
272 | 315 |
@ingroup algs |
273 | 316 |
\brief Common graph search algorithms. |
274 | 317 |
|
275 | 318 |
This group contains the common graph search algorithms, namely |
276 |
\e breadth-first \e search (BFS) and \e depth-first \e search (DFS) |
|
319 |
\e breadth-first \e search (BFS) and \e depth-first \e search (DFS) |
|
320 |
\ref clrs01algorithms. |
|
277 | 321 |
*/ |
278 | 322 |
|
279 | 323 |
/** |
280 | 324 |
@defgroup shortest_path Shortest Path Algorithms |
281 | 325 |
@ingroup algs |
282 | 326 |
\brief Algorithms for finding shortest paths. |
283 | 327 |
|
284 |
This group contains the algorithms for finding shortest paths in digraphs |
|
328 |
This group contains the algorithms for finding shortest paths in digraphs |
|
329 |
\ref clrs01algorithms. |
|
285 | 330 |
|
286 | 331 |
- \ref Dijkstra algorithm for finding shortest paths from a source node |
287 | 332 |
when all arc lengths are non-negative. |
288 | 333 |
- \ref BellmanFord "Bellman-Ford" algorithm for finding shortest paths |
289 | 334 |
from a source node when arc lenghts can be either positive or negative, |
290 | 335 |
but the digraph should not contain directed cycles with negative total |
291 | 336 |
length. |
292 | 337 |
- \ref FloydWarshall "Floyd-Warshall" and \ref Johnson "Johnson" algorithms |
293 | 338 |
for solving the \e all-pairs \e shortest \e paths \e problem when arc |
294 | 339 |
lenghts can be either positive or negative, but the digraph should |
295 | 340 |
not contain directed cycles with negative total length. |
296 | 341 |
- \ref Suurballe A successive shortest path algorithm for finding |
297 | 342 |
arc-disjoint paths between two nodes having minimum total length. |
298 | 343 |
*/ |
299 | 344 |
|
300 | 345 |
/** |
346 |
@defgroup spantree Minimum Spanning Tree Algorithms |
|
347 |
@ingroup algs |
|
348 |
\brief Algorithms for finding minimum cost spanning trees and arborescences. |
|
349 |
|
|
350 |
This group contains the algorithms for finding minimum cost spanning |
|
351 |
trees and arborescences \ref clrs01algorithms. |
|
352 |
*/ |
|
353 |
|
|
354 |
/** |
|
301 | 355 |
@defgroup max_flow Maximum Flow Algorithms |
302 | 356 |
@ingroup algs |
303 | 357 |
\brief Algorithms for finding maximum flows. |
304 | 358 |
|
305 | 359 |
This group contains the algorithms for finding maximum flows and |
306 |
feasible circulations. |
|
360 |
feasible circulations \ref clrs01algorithms, \ref amo93networkflows. |
|
307 | 361 |
|
308 | 362 |
The \e maximum \e flow \e problem is to find a flow of maximum value between |
309 | 363 |
a single source and a single target. Formally, there is a \f$G=(V,A)\f$ |
310 | 364 |
digraph, a \f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function and |
311 | 365 |
\f$s, t \in V\f$ source and target nodes. |
312 | 366 |
A maximum flow is an \f$f: A\rightarrow\mathbf{R}^+_0\f$ solution of the |
313 | 367 |
following optimization problem. |
314 | 368 |
|
315 | 369 |
\f[ \max\sum_{sv\in A} f(sv) - \sum_{vs\in A} f(vs) \f] |
316 | 370 |
\f[ \sum_{uv\in A} f(uv) = \sum_{vu\in A} f(vu) |
317 | 371 |
\quad \forall u\in V\setminus\{s,t\} \f] |
318 | 372 |
\f[ 0 \leq f(uv) \leq cap(uv) \quad \forall uv\in A \f] |
319 | 373 |
|
320 | 374 |
LEMON contains several algorithms for solving maximum flow problems: |
321 |
- \ref EdmondsKarp Edmonds-Karp algorithm. |
|
322 |
- \ref Preflow Goldberg-Tarjan's preflow push-relabel algorithm. |
|
323 |
- \ref DinitzSleatorTarjan Dinitz's blocking flow algorithm with dynamic trees. |
|
324 |
- \ref GoldbergTarjan Preflow push-relabel algorithm with dynamic trees. |
|
375 |
- \ref EdmondsKarp Edmonds-Karp algorithm |
|
376 |
\ref edmondskarp72theoretical. |
|
377 |
- \ref Preflow Goldberg-Tarjan's preflow push-relabel algorithm |
|
378 |
\ref goldberg88newapproach. |
|
379 |
- \ref DinitzSleatorTarjan Dinitz's blocking flow algorithm with dynamic trees |
|
380 |
\ref dinic70algorithm, \ref sleator83dynamic. |
|
381 |
- \ref GoldbergTarjan !Preflow push-relabel algorithm with dynamic trees |
|
382 |
\ref goldberg88newapproach, \ref sleator83dynamic. |
|
325 | 383 |
|
326 |
In most cases the \ref Preflow |
|
384 |
In most cases the \ref Preflow algorithm provides the |
|
327 | 385 |
fastest method for computing a maximum flow. All implementations |
328 | 386 |
also provide functions to query the minimum cut, which is the dual |
329 | 387 |
problem of maximum flow. |
330 | 388 |
|
331 | 389 |
\ref Circulation is a preflow push-relabel algorithm implemented directly |
332 | 390 |
for finding feasible circulations, which is a somewhat different problem, |
333 | 391 |
but it is strongly related to maximum flow. |
334 | 392 |
For more information, see \ref Circulation. |
335 | 393 |
*/ |
336 | 394 |
|
337 | 395 |
/** |
338 | 396 |
@defgroup min_cost_flow_algs Minimum Cost Flow Algorithms |
339 | 397 |
@ingroup algs |
340 | 398 |
|
341 | 399 |
\brief Algorithms for finding minimum cost flows and circulations. |
342 | 400 |
|
343 | 401 |
This group contains the algorithms for finding minimum cost flows and |
344 |
circulations. For more information about this problem and its dual |
|
345 |
solution see \ref min_cost_flow "Minimum Cost Flow Problem". |
|
402 |
circulations \ref amo93networkflows. For more information about this |
|
403 |
problem and its dual solution, see \ref min_cost_flow |
|
404 |
"Minimum Cost Flow Problem". |
|
346 | 405 |
|
347 | 406 |
LEMON contains several algorithms for this problem. |
348 | 407 |
- \ref NetworkSimplex Primal Network Simplex algorithm with various |
349 |
pivot strategies. |
|
408 |
pivot strategies \ref dantzig63linearprog, \ref kellyoneill91netsimplex. |
|
350 | 409 |
- \ref CostScaling Push-Relabel and Augment-Relabel algorithms based on |
351 |
cost scaling |
|
410 |
cost scaling \ref goldberg90approximation, \ref goldberg97efficient, |
|
411 |
\ref bunnagel98efficient. |
|
352 | 412 |
- \ref CapacityScaling Successive Shortest %Path algorithm with optional |
353 |
capacity scaling. |
|
354 |
- \ref CancelAndTighten The Cancel and Tighten algorithm. |
|
355 |
|
|
413 |
capacity scaling \ref edmondskarp72theoretical. |
|
414 |
- \ref CancelAndTighten The Cancel and Tighten algorithm |
|
415 |
\ref goldberg89cyclecanceling. |
|
416 |
- \ref CycleCanceling Cycle-Canceling algorithms |
|
417 |
\ref klein67primal, \ref goldberg89cyclecanceling. |
|
356 | 418 |
|
357 | 419 |
In general NetworkSimplex is the most efficient implementation, |
358 | 420 |
but in special cases other algorithms could be faster. |
359 | 421 |
For example, if the total supply and/or capacities are rather small, |
360 | 422 |
CapacityScaling is usually the fastest algorithm (without effective scaling). |
361 | 423 |
*/ |
362 | 424 |
|
363 | 425 |
/** |
364 | 426 |
@defgroup min_cut Minimum Cut Algorithms |
365 | 427 |
@ingroup algs |
366 | 428 |
|
367 | 429 |
\brief Algorithms for finding minimum cut in graphs. |
368 | 430 |
|
369 | 431 |
This group contains the algorithms for finding minimum cut in graphs. |
370 | 432 |
|
371 | 433 |
The \e minimum \e cut \e problem is to find a non-empty and non-complete |
372 | 434 |
\f$X\f$ subset of the nodes with minimum overall capacity on |
373 | 435 |
outgoing arcs. Formally, there is a \f$G=(V,A)\f$ digraph, a |
374 | 436 |
\f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function. The minimum |
375 | 437 |
cut is the \f$X\f$ solution of the next optimization problem: |
376 | 438 |
|
377 | 439 |
\f[ \min_{X \subset V, X\not\in \{\emptyset, V\}} |
378 |
\sum_{uv\in A |
|
440 |
\sum_{uv\in A: u\in X, v\not\in X}cap(uv) \f] |
|
379 | 441 |
|
380 | 442 |
LEMON contains several algorithms related to minimum cut problems: |
381 | 443 |
|
382 | 444 |
- \ref HaoOrlin "Hao-Orlin algorithm" for calculating minimum cut |
383 | 445 |
in directed graphs. |
384 | 446 |
- \ref NagamochiIbaraki "Nagamochi-Ibaraki algorithm" for |
385 | 447 |
calculating minimum cut in undirected graphs. |
386 | 448 |
- \ref GomoryHu "Gomory-Hu tree computation" for calculating |
387 | 449 |
all-pairs minimum cut in undirected graphs. |
388 | 450 |
|
389 | 451 |
If you want to find minimum cut just between two distinict nodes, |
390 | 452 |
see the \ref max_flow "maximum flow problem". |
391 | 453 |
*/ |
392 | 454 |
|
393 | 455 |
/** |
394 |
@defgroup |
|
456 |
@defgroup min_mean_cycle Minimum Mean Cycle Algorithms |
|
395 | 457 |
@ingroup algs |
396 |
\brief Algorithms for |
|
458 |
\brief Algorithms for finding minimum mean cycles. |
|
397 | 459 |
|
398 |
This group contains the algorithms for discovering the graph properties |
|
399 |
like connectivity, bipartiteness, euler property, simplicity etc. |
|
460 |
This group contains the algorithms for finding minimum mean cycles |
|
461 |
\ref clrs01algorithms, \ref amo93networkflows. |
|
400 | 462 |
|
401 |
\image html edge_biconnected_components.png |
|
402 |
\image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth |
|
403 |
|
|
463 |
The \e minimum \e mean \e cycle \e problem is to find a directed cycle |
|
464 |
of minimum mean length (cost) in a digraph. |
|
465 |
The mean length of a cycle is the average length of its arcs, i.e. the |
|
466 |
ratio between the total length of the cycle and the number of arcs on it. |
|
404 | 467 |
|
405 |
/** |
|
406 |
@defgroup planar Planarity Embedding and Drawing |
|
407 |
@ingroup algs |
|
408 |
\brief Algorithms for planarity checking, embedding and drawing |
|
468 |
This problem has an important connection to \e conservative \e length |
|
469 |
\e functions, too. A length function on the arcs of a digraph is called |
|
470 |
conservative if and only if there is no directed cycle of negative total |
|
471 |
length. For an arbitrary length function, the negative of the minimum |
|
472 |
cycle mean is the smallest \f$\epsilon\f$ value so that increasing the |
|
473 |
arc lengths uniformly by \f$\epsilon\f$ results in a conservative length |
|
474 |
function. |
|
409 | 475 |
|
410 |
This group contains the algorithms for planarity checking, |
|
411 |
embedding and drawing. |
|
476 |
LEMON contains three algorithms for solving the minimum mean cycle problem: |
|
477 |
- \ref Karp "Karp"'s original algorithm \ref amo93networkflows, |
|
478 |
\ref dasdan98minmeancycle. |
|
479 |
- \ref HartmannOrlin "Hartmann-Orlin"'s algorithm, which is an improved |
|
480 |
version of Karp's algorithm \ref dasdan98minmeancycle. |
|
481 |
- \ref Howard "Howard"'s policy iteration algorithm |
|
482 |
\ref dasdan98minmeancycle. |
|
412 | 483 |
|
413 |
\image html planar.png |
|
414 |
\image latex planar.eps "Plane graph" width=\textwidth |
|
484 |
In practice, the Howard algorithm proved to be by far the most efficient |
|
485 |
one, though the best known theoretical bound on its running time is |
|
486 |
exponential. |
|
487 |
Both Karp and HartmannOrlin algorithms run in time O(ne) and use space |
|
488 |
O(n<sup>2</sup>+e), but the latter one is typically faster due to the |
|
489 |
applied early termination scheme. |
|
415 | 490 |
*/ |
416 | 491 |
|
417 | 492 |
/** |
418 | 493 |
@defgroup matching Matching Algorithms |
419 | 494 |
@ingroup algs |
420 | 495 |
\brief Algorithms for finding matchings in graphs and bipartite graphs. |
421 | 496 |
|
422 | 497 |
This group contains the algorithms for calculating |
423 | 498 |
matchings in graphs and bipartite graphs. The general matching problem is |
424 | 499 |
finding a subset of the edges for which each node has at most one incident |
425 | 500 |
edge. |
426 | 501 |
|
427 | 502 |
There are several different algorithms for calculate matchings in |
428 | 503 |
graphs. The matching problems in bipartite graphs are generally |
429 | 504 |
easier than in general graphs. The goal of the matching optimization |
430 | 505 |
can be finding maximum cardinality, maximum weight or minimum cost |
431 | 506 |
matching. The search can be constrained to find perfect or |
432 | 507 |
maximum cardinality matching. |
433 | 508 |
|
434 | 509 |
The matching algorithms implemented in LEMON: |
435 | 510 |
- \ref MaxBipartiteMatching Hopcroft-Karp augmenting path algorithm |
436 | 511 |
for calculating maximum cardinality matching in bipartite graphs. |
437 | 512 |
- \ref PrBipartiteMatching Push-relabel algorithm |
438 | 513 |
for calculating maximum cardinality matching in bipartite graphs. |
439 | 514 |
- \ref MaxWeightedBipartiteMatching |
440 | 515 |
Successive shortest path algorithm for calculating maximum weighted |
441 | 516 |
matching and maximum weighted bipartite matching in bipartite graphs. |
442 | 517 |
- \ref MinCostMaxBipartiteMatching |
443 | 518 |
Successive shortest path algorithm for calculating minimum cost maximum |
444 | 519 |
matching in bipartite graphs. |
445 | 520 |
- \ref MaxMatching Edmond's blossom shrinking algorithm for calculating |
446 | 521 |
maximum cardinality matching in general graphs. |
447 | 522 |
- \ref MaxWeightedMatching Edmond's blossom shrinking algorithm for calculating |
448 | 523 |
maximum weighted matching in general graphs. |
449 | 524 |
- \ref MaxWeightedPerfectMatching |
450 | 525 |
Edmond's blossom shrinking algorithm for calculating maximum weighted |
451 | 526 |
perfect matching in general graphs. |
452 | 527 |
|
453 | 528 |
\image html bipartite_matching.png |
454 | 529 |
\image latex bipartite_matching.eps "Bipartite Matching" width=\textwidth |
455 | 530 |
*/ |
456 | 531 |
|
457 | 532 |
/** |
458 |
@defgroup |
|
533 |
@defgroup graph_properties Connectivity and Other Graph Properties |
|
459 | 534 |
@ingroup algs |
460 |
\brief Algorithms for |
|
535 |
\brief Algorithms for discovering the graph properties |
|
461 | 536 |
|
462 |
This group contains the algorithms for finding minimum cost spanning |
|
463 |
trees and arborescences. |
|
537 |
This group contains the algorithms for discovering the graph properties |
|
538 |
like connectivity, bipartiteness, euler property, simplicity etc. |
|
539 |
|
|
540 |
\image html connected_components.png |
|
541 |
\image latex connected_components.eps "Connected components" width=\textwidth |
|
542 |
*/ |
|
543 |
|
|
544 |
/** |
|
545 |
@defgroup planar Planarity Embedding and Drawing |
|
546 |
@ingroup algs |
|
547 |
\brief Algorithms for planarity checking, embedding and drawing |
|
548 |
|
|
549 |
This group contains the algorithms for planarity checking, |
|
550 |
embedding and drawing. |
|
551 |
|
|
552 |
\image html planar.png |
|
553 |
\image latex planar.eps "Plane graph" width=\textwidth |
|
554 |
*/ |
|
555 |
|
|
556 |
/** |
|
557 |
@defgroup approx Approximation Algorithms |
|
558 |
@ingroup algs |
|
559 |
\brief Approximation algorithms. |
|
560 |
|
|
561 |
This group contains the approximation and heuristic algorithms |
|
562 |
implemented in LEMON. |
|
464 | 563 |
*/ |
465 | 564 |
|
466 | 565 |
/** |
467 | 566 |
@defgroup auxalg Auxiliary Algorithms |
468 | 567 |
@ingroup algs |
469 | 568 |
\brief Auxiliary algorithms implemented in LEMON. |
470 | 569 |
|
471 | 570 |
This group contains some algorithms implemented in LEMON |
472 | 571 |
in order to make it easier to implement complex algorithms. |
473 | 572 |
*/ |
474 | 573 |
|
475 | 574 |
/** |
476 |
@defgroup approx Approximation Algorithms |
|
477 |
@ingroup algs |
|
478 |
\brief Approximation algorithms. |
|
479 |
|
|
480 |
This group contains the approximation and heuristic algorithms |
|
481 |
implemented in LEMON. |
|
482 |
*/ |
|
483 |
|
|
484 |
/** |
|
485 | 575 |
@defgroup gen_opt_group General Optimization Tools |
486 | 576 |
\brief This group contains some general optimization frameworks |
487 | 577 |
implemented in LEMON. |
488 | 578 |
|
489 | 579 |
This group contains some general optimization frameworks |
490 | 580 |
implemented in LEMON. |
491 | 581 |
*/ |
492 | 582 |
|
493 | 583 |
/** |
494 |
@defgroup lp_group |
|
584 |
@defgroup lp_group LP and MIP Solvers |
|
495 | 585 |
@ingroup gen_opt_group |
496 |
\brief |
|
586 |
\brief LP and MIP solver interfaces for LEMON. |
|
497 | 587 |
|
498 |
This group contains Lp and Mip solver interfaces for LEMON. The |
|
499 |
various LP solvers could be used in the same manner with this |
|
500 |
|
|
588 |
This group contains LP and MIP solver interfaces for LEMON. |
|
589 |
Various LP solvers could be used in the same manner with this |
|
590 |
high-level interface. |
|
591 |
|
|
592 |
The currently supported solvers are \ref glpk, \ref clp, \ref cbc, |
|
593 |
\ref cplex, \ref soplex. |
|
501 | 594 |
*/ |
502 | 595 |
|
503 | 596 |
/** |
504 | 597 |
@defgroup lp_utils Tools for Lp and Mip Solvers |
505 | 598 |
@ingroup lp_group |
506 | 599 |
\brief Helper tools to the Lp and Mip solvers. |
507 | 600 |
|
508 | 601 |
This group adds some helper tools to general optimization framework |
509 | 602 |
implemented in LEMON. |
510 | 603 |
*/ |
511 | 604 |
|
512 | 605 |
/** |
513 | 606 |
@defgroup metah Metaheuristics |
514 | 607 |
@ingroup gen_opt_group |
515 | 608 |
\brief Metaheuristics for LEMON library. |
516 | 609 |
|
517 | 610 |
This group contains some metaheuristic optimization tools. |
518 | 611 |
*/ |
519 | 612 |
|
520 | 613 |
/** |
521 | 614 |
@defgroup utils Tools and Utilities |
522 | 615 |
\brief Tools and utilities for programming in LEMON |
523 | 616 |
|
524 | 617 |
Tools and utilities for programming in LEMON. |
525 | 618 |
*/ |
526 | 619 |
|
527 | 620 |
/** |
528 | 621 |
@defgroup gutils Basic Graph Utilities |
529 | 622 |
@ingroup utils |
530 | 623 |
\brief Simple basic graph utilities. |
531 | 624 |
|
532 | 625 |
This group contains some simple basic graph utilities. |
533 | 626 |
*/ |
534 | 627 |
|
535 | 628 |
/** |
536 | 629 |
@defgroup misc Miscellaneous Tools |
537 | 630 |
@ingroup utils |
538 | 631 |
\brief Tools for development, debugging and testing. |
539 | 632 |
|
540 | 633 |
This group contains several useful tools for development, |
541 | 634 |
debugging and testing. |
542 | 635 |
*/ |
543 | 636 |
|
544 | 637 |
/** |
545 | 638 |
@defgroup timecount Time Measuring and Counting |
546 | 639 |
@ingroup misc |
547 | 640 |
\brief Simple tools for measuring the performance of algorithms. |
548 | 641 |
|
549 | 642 |
This group contains simple tools for measuring the performance |
550 | 643 |
of algorithms. |
551 | 644 |
*/ |
552 | 645 |
|
553 | 646 |
/** |
554 | 647 |
@defgroup exceptions Exceptions |
555 | 648 |
@ingroup utils |
556 | 649 |
\brief Exceptions defined in LEMON. |
557 | 650 |
|
558 | 651 |
This group contains the exceptions defined in LEMON. |
559 | 652 |
*/ |
560 | 653 |
|
561 | 654 |
/** |
562 | 655 |
@defgroup io_group Input-Output |
563 | 656 |
\brief Graph Input-Output methods |
564 | 657 |
|
565 | 658 |
This group contains the tools for importing and exporting graphs |
566 | 659 |
and graph related data. Now it supports the \ref lgf-format |
567 | 660 |
"LEMON Graph Format", the \c DIMACS format and the encapsulated |
568 | 661 |
postscript (EPS) format. |
569 | 662 |
*/ |
570 | 663 |
|
571 | 664 |
/** |
572 | 665 |
@defgroup lemon_io LEMON Graph Format |
573 | 666 |
@ingroup io_group |
574 | 667 |
\brief Reading and writing LEMON Graph Format. |
575 | 668 |
|
576 | 669 |
This group contains methods for reading and writing |
577 | 670 |
\ref lgf-format "LEMON Graph Format". |
578 | 671 |
*/ |
579 | 672 |
|
580 | 673 |
/** |
581 | 674 |
@defgroup eps_io Postscript Exporting |
582 | 675 |
@ingroup io_group |
583 | 676 |
\brief General \c EPS drawer and graph exporter |
584 | 677 |
|
585 | 678 |
This group contains general \c EPS drawing methods and special |
586 | 679 |
graph exporting tools. |
587 | 680 |
*/ |
588 | 681 |
|
589 | 682 |
/** |
590 |
@defgroup dimacs_group DIMACS |
|
683 |
@defgroup dimacs_group DIMACS Format |
|
591 | 684 |
@ingroup io_group |
592 | 685 |
\brief Read and write files in DIMACS format |
593 | 686 |
|
594 | 687 |
Tools to read a digraph from or write it to a file in DIMACS format data. |
595 | 688 |
*/ |
596 | 689 |
|
597 | 690 |
/** |
598 | 691 |
@defgroup nauty_group NAUTY Format |
599 | 692 |
@ingroup io_group |
600 | 693 |
\brief Read \e Nauty format |
601 | 694 |
|
602 | 695 |
Tool to read graphs from \e Nauty format data. |
603 | 696 |
*/ |
604 | 697 |
|
605 | 698 |
/** |
606 | 699 |
@defgroup concept Concepts |
607 | 700 |
\brief Skeleton classes and concept checking classes |
608 | 701 |
|
609 | 702 |
This group contains the data/algorithm skeletons and concept checking |
610 | 703 |
classes implemented in LEMON. |
611 | 704 |
|
612 | 705 |
The purpose of the classes in this group is fourfold. |
613 | 706 |
|
614 | 707 |
- These classes contain the documentations of the %concepts. In order |
615 | 708 |
to avoid document multiplications, an implementation of a concept |
616 | 709 |
simply refers to the corresponding concept class. |
617 | 710 |
|
618 | 711 |
- These classes declare every functions, <tt>typedef</tt>s etc. an |
619 | 712 |
implementation of the %concepts should provide, however completely |
620 | 713 |
without implementations and real data structures behind the |
621 | 714 |
interface. On the other hand they should provide nothing else. All |
622 | 715 |
the algorithms working on a data structure meeting a certain concept |
623 | 716 |
should compile with these classes. (Though it will not run properly, |
624 | 717 |
of course.) In this way it is easily to check if an algorithm |
625 | 718 |
doesn't use any extra feature of a certain implementation. |
626 | 719 |
|
627 | 720 |
- The concept descriptor classes also provide a <em>checker class</em> |
628 | 721 |
that makes it possible to check whether a certain implementation of a |
629 | 722 |
concept indeed provides all the required features. |
630 | 723 |
|
631 | 724 |
- Finally, They can serve as a skeleton of a new implementation of a concept. |
632 | 725 |
*/ |
633 | 726 |
|
634 | 727 |
/** |
635 | 728 |
@defgroup graph_concepts Graph Structure Concepts |
636 | 729 |
@ingroup concept |
637 | 730 |
\brief Skeleton and concept checking classes for graph structures |
638 | 731 |
|
639 |
This group contains the skeletons and concept checking classes of LEMON's |
|
640 |
graph structures and helper classes used to implement these. |
|
732 |
This group contains the skeletons and concept checking classes of |
|
733 |
graph structures. |
|
641 | 734 |
*/ |
642 | 735 |
|
643 | 736 |
/** |
644 | 737 |
@defgroup map_concepts Map Concepts |
645 | 738 |
@ingroup concept |
646 | 739 |
\brief Skeleton and concept checking classes for maps |
647 | 740 |
|
648 | 741 |
This group contains the skeletons and concept checking classes of maps. |
649 | 742 |
*/ |
650 | 743 |
|
651 | 744 |
/** |
745 |
@defgroup tools Standalone Utility Applications |
|
746 |
|
|
747 |
Some utility applications are listed here. |
|
748 |
|
|
749 |
The standard compilation procedure (<tt>./configure;make</tt>) will compile |
|
750 |
them, as well. |
|
751 |
*/ |
|
752 |
|
|
753 |
/** |
|
652 | 754 |
\anchor demoprograms |
653 | 755 |
|
654 | 756 |
@defgroup demos Demo Programs |
655 | 757 |
|
656 | 758 |
Some demo programs are listed here. Their full source codes can be found in |
657 | 759 |
the \c demo subdirectory of the source tree. |
658 | 760 |
|
659 | 761 |
In order to compile them, use the <tt>make demo</tt> or the |
660 | 762 |
<tt>make check</tt> commands. |
661 | 763 |
*/ |
662 | 764 |
|
663 |
/** |
|
664 |
@defgroup tools Standalone Utility Applications |
|
665 |
|
|
666 |
Some utility applications are listed here. |
|
667 |
|
|
668 |
The standard compilation procedure (<tt>./configure;make</tt>) will compile |
|
669 |
them, as well. |
|
670 |
*/ |
|
671 |
|
|
672 | 765 |
} |
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 |
/** |
20 | 20 |
\mainpage LEMON Documentation |
21 | 21 |
|
22 | 22 |
\section intro Introduction |
23 | 23 |
|
24 |
\subsection whatis What is LEMON |
|
25 |
|
|
26 |
LEMON stands for <b>L</b>ibrary for <b>E</b>fficient <b>M</b>odeling |
|
27 |
and <b>O</b>ptimization in <b>N</b>etworks. |
|
28 |
It is a C++ template |
|
29 |
library aimed at combinatorial optimization tasks which |
|
30 |
often involve in working |
|
31 |
with graphs. |
|
24 |
<b>LEMON</b> stands for <i><b>L</b>ibrary for <b>E</b>fficient <b>M</b>odeling |
|
25 |
and <b>O</b>ptimization in <b>N</b>etworks</i>. |
|
26 |
It is a C++ template library providing efficient implementation of common |
|
27 |
data structures and algorithms with focus on combinatorial optimization |
|
28 |
problems in graphs and networks. |
|
32 | 29 |
|
33 | 30 |
<b> |
34 | 31 |
LEMON is an <a class="el" href="http://opensource.org/">open source</a> |
35 | 32 |
project. |
36 | 33 |
You are free to use it in your commercial or |
37 | 34 |
non-commercial applications under very permissive |
38 | 35 |
\ref license "license terms". |
39 | 36 |
</b> |
40 | 37 |
|
41 |
|
|
38 |
The project is maintained by the |
|
39 |
<a href="http://www.cs.elte.hu/egres/">Egerváry Research Group on |
|
40 |
Combinatorial Optimization</a> \ref egres |
|
41 |
at the Operations Research Department of the |
|
42 |
<a href="http://www.elte.hu/">Eötvös Loránd University, |
|
43 |
Budapest</a>, Hungary. |
|
44 |
LEMON is also a member of the <a href="http://www.coin-or.org/">COIN-OR</a> |
|
45 |
initiative \ref coinor. |
|
46 |
|
|
47 |
\section howtoread How to Read the Documentation |
|
42 | 48 |
|
43 | 49 |
If you would like to get to know the library, see |
44 | 50 |
<a class="el" href="http://lemon.cs.elte.hu/pub/tutorial/">LEMON Tutorial</a>. |
45 | 51 |
|
46 | 52 |
If you know what you are looking for, then try to find it under the |
47 | 53 |
<a class="el" href="modules.html">Modules</a> section. |
48 | 54 |
|
49 | 55 |
If you are a user of the old (0.x) series of LEMON, please check out the |
50 | 56 |
\ref migration "Migration Guide" for the backward incompatibilities. |
51 | 57 |
*/ |
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 |
namespace lemon { |
20 | 20 |
|
21 | 21 |
/** |
22 | 22 |
\page min_cost_flow Minimum Cost Flow Problem |
23 | 23 |
|
24 | 24 |
\section mcf_def Definition (GEQ form) |
25 | 25 |
|
26 | 26 |
The \e minimum \e cost \e flow \e problem is to find a feasible flow of |
27 | 27 |
minimum total cost from a set of supply nodes to a set of demand nodes |
28 | 28 |
in a network with capacity constraints (lower and upper bounds) |
29 |
and arc costs. |
|
29 |
and arc costs \ref amo93networkflows. |
|
30 | 30 |
|
31 | 31 |
Formally, let \f$G=(V,A)\f$ be a digraph, \f$lower: A\rightarrow\mathbf{R}\f$, |
32 | 32 |
\f$upper: A\rightarrow\mathbf{R}\cup\{+\infty\}\f$ denote the lower and |
33 | 33 |
upper bounds for the flow values on the arcs, for which |
34 | 34 |
\f$lower(uv) \leq upper(uv)\f$ must hold for all \f$uv\in A\f$, |
35 | 35 |
\f$cost: A\rightarrow\mathbf{R}\f$ denotes the cost per unit flow |
36 | 36 |
on the arcs and \f$sup: V\rightarrow\mathbf{R}\f$ denotes the |
37 | 37 |
signed supply values of the nodes. |
38 | 38 |
If \f$sup(u)>0\f$, then \f$u\f$ is a supply node with \f$sup(u)\f$ |
39 | 39 |
supply, if \f$sup(u)<0\f$, then \f$u\f$ is a demand node with |
40 | 40 |
\f$-sup(u)\f$ demand. |
41 | 41 |
A minimum cost flow is an \f$f: A\rightarrow\mathbf{R}\f$ solution |
42 | 42 |
of the following optimization problem. |
43 | 43 |
|
44 | 44 |
\f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f] |
45 | 45 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \geq |
46 | 46 |
sup(u) \quad \forall u\in V \f] |
47 | 47 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f] |
48 | 48 |
|
49 | 49 |
The sum of the supply values, i.e. \f$\sum_{u\in V} sup(u)\f$ must be |
50 | 50 |
zero or negative in order to have a feasible solution (since the sum |
51 | 51 |
of the expressions on the left-hand side of the inequalities is zero). |
52 | 52 |
It means that the total demand must be greater or equal to the total |
53 | 53 |
supply and all the supplies have to be carried out from the supply nodes, |
54 | 54 |
but there could be demands that are not satisfied. |
55 | 55 |
If \f$\sum_{u\in V} sup(u)\f$ is zero, then all the supply/demand |
56 | 56 |
constraints have to be satisfied with equality, i.e. all demands |
57 | 57 |
have to be satisfied and all supplies have to be used. |
58 | 58 |
|
59 | 59 |
|
60 | 60 |
\section mcf_algs Algorithms |
61 | 61 |
|
62 | 62 |
LEMON contains several algorithms for solving this problem, for more |
63 | 63 |
information see \ref min_cost_flow_algs "Minimum Cost Flow Algorithms". |
64 | 64 |
|
65 | 65 |
A feasible solution for this problem can be found using \ref Circulation. |
66 | 66 |
|
67 | 67 |
|
68 | 68 |
\section mcf_dual Dual Solution |
69 | 69 |
|
70 | 70 |
The dual solution of the minimum cost flow problem is represented by |
71 | 71 |
node potentials \f$\pi: V\rightarrow\mathbf{R}\f$. |
72 | 72 |
An \f$f: A\rightarrow\mathbf{R}\f$ primal feasible solution is optimal |
73 | 73 |
if and only if for some \f$\pi: V\rightarrow\mathbf{R}\f$ node potentials |
74 | 74 |
the following \e complementary \e slackness optimality conditions hold. |
75 | 75 |
|
76 | 76 |
- For all \f$uv\in A\f$ arcs: |
77 | 77 |
- if \f$cost^\pi(uv)>0\f$, then \f$f(uv)=lower(uv)\f$; |
78 | 78 |
- if \f$lower(uv)<f(uv)<upper(uv)\f$, then \f$cost^\pi(uv)=0\f$; |
79 | 79 |
- if \f$cost^\pi(uv)<0\f$, then \f$f(uv)=upper(uv)\f$. |
80 | 80 |
- For all \f$u\in V\f$ nodes: |
81 |
- \f$\pi(u) |
|
81 |
- \f$\pi(u)\leq 0\f$; |
|
82 | 82 |
- if \f$\sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \neq sup(u)\f$, |
83 | 83 |
then \f$\pi(u)=0\f$. |
84 | 84 |
|
85 | 85 |
Here \f$cost^\pi(uv)\f$ denotes the \e reduced \e cost of the arc |
86 | 86 |
\f$uv\in A\f$ with respect to the potential function \f$\pi\f$, i.e. |
87 | 87 |
\f[ cost^\pi(uv) = cost(uv) + \pi(u) - \pi(v).\f] |
88 | 88 |
|
89 | 89 |
All algorithms provide dual solution (node potentials), as well, |
90 | 90 |
if an optimal flow is found. |
91 | 91 |
|
92 | 92 |
|
93 | 93 |
\section mcf_eq Equality Form |
94 | 94 |
|
95 | 95 |
The above \ref mcf_def "definition" is actually more general than the |
96 | 96 |
usual formulation of the minimum cost flow problem, in which strict |
97 | 97 |
equalities are required in the supply/demand contraints. |
98 | 98 |
|
99 | 99 |
\f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f] |
100 | 100 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) = |
101 | 101 |
sup(u) \quad \forall u\in V \f] |
102 | 102 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f] |
103 | 103 |
|
104 | 104 |
However if the sum of the supply values is zero, then these two problems |
105 | 105 |
are equivalent. |
106 | 106 |
The \ref min_cost_flow_algs "algorithms" in LEMON support the general |
107 | 107 |
form, so if you need the equality form, you have to ensure this additional |
108 | 108 |
contraint manually. |
109 | 109 |
|
110 | 110 |
|
111 | 111 |
\section mcf_leq Opposite Inequalites (LEQ Form) |
112 | 112 |
|
113 | 113 |
Another possible definition of the minimum cost flow problem is |
114 | 114 |
when there are <em>"less or equal"</em> (LEQ) supply/demand constraints, |
115 | 115 |
instead of the <em>"greater or equal"</em> (GEQ) constraints. |
116 | 116 |
|
117 | 117 |
\f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f] |
118 | 118 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \leq |
119 | 119 |
sup(u) \quad \forall u\in V \f] |
120 | 120 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f] |
121 | 121 |
|
122 | 122 |
It means that the total demand must be less or equal to the |
123 | 123 |
total supply (i.e. \f$\sum_{u\in V} sup(u)\f$ must be zero or |
124 | 124 |
positive) and all the demands have to be satisfied, but there |
125 | 125 |
could be supplies that are not carried out from the supply |
126 | 126 |
nodes. |
127 | 127 |
The equality form is also a special case of this form, of course. |
128 | 128 |
|
129 | 129 |
You could easily transform this case to the \ref mcf_def "GEQ form" |
130 | 130 |
of the problem by reversing the direction of the arcs and taking the |
131 | 131 |
negative of the supply values (e.g. using \ref ReverseDigraph and |
132 | 132 |
\ref NegMap adaptors). |
133 | 133 |
However \ref NetworkSimplex algorithm also supports this form directly |
134 | 134 |
for the sake of convenience. |
135 | 135 |
|
136 | 136 |
Note that the optimality conditions for this supply constraint type are |
137 | 137 |
slightly differ from the conditions that are discussed for the GEQ form, |
138 | 138 |
namely the potentials have to be non-negative instead of non-positive. |
139 | 139 |
An \f$f: A\rightarrow\mathbf{R}\f$ feasible solution of this problem |
140 | 140 |
is optimal if and only if for some \f$\pi: V\rightarrow\mathbf{R}\f$ |
141 | 141 |
node potentials the following conditions hold. |
142 | 142 |
|
143 | 143 |
- For all \f$uv\in A\f$ arcs: |
144 | 144 |
- if \f$cost^\pi(uv)>0\f$, then \f$f(uv)=lower(uv)\f$; |
145 | 145 |
- if \f$lower(uv)<f(uv)<upper(uv)\f$, then \f$cost^\pi(uv)=0\f$; |
146 | 146 |
- if \f$cost^\pi(uv)<0\f$, then \f$f(uv)=upper(uv)\f$. |
147 | 147 |
- For all \f$u\in V\f$ nodes: |
148 |
- \f$\pi(u) |
|
148 |
- \f$\pi(u)\geq 0\f$; |
|
149 | 149 |
- if \f$\sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \neq sup(u)\f$, |
150 | 150 |
then \f$\pi(u)=0\f$. |
151 | 151 |
|
152 | 152 |
*/ |
153 | 153 |
} |
... | ... |
@@ -12,126 +12,135 @@ |
12 | 12 |
lemon/base.cc \ |
13 | 13 |
lemon/color.cc \ |
14 | 14 |
lemon/lp_base.cc \ |
15 | 15 |
lemon/lp_skeleton.cc \ |
16 | 16 |
lemon/random.cc \ |
17 | 17 |
lemon/bits/windows.cc |
18 | 18 |
|
19 | 19 |
nodist_lemon_HEADERS = lemon/config.h |
20 | 20 |
|
21 | 21 |
lemon_libemon_la_CXXFLAGS = \ |
22 | 22 |
$(AM_CXXFLAGS) \ |
23 | 23 |
$(GLPK_CFLAGS) \ |
24 | 24 |
$(CPLEX_CFLAGS) \ |
25 | 25 |
$(SOPLEX_CXXFLAGS) \ |
26 | 26 |
$(CLP_CXXFLAGS) \ |
27 | 27 |
$(CBC_CXXFLAGS) |
28 | 28 |
|
29 | 29 |
lemon_libemon_la_LDFLAGS = \ |
30 | 30 |
$(GLPK_LIBS) \ |
31 | 31 |
$(CPLEX_LIBS) \ |
32 | 32 |
$(SOPLEX_LIBS) \ |
33 | 33 |
$(CLP_LIBS) \ |
34 | 34 |
$(CBC_LIBS) |
35 | 35 |
|
36 | 36 |
if HAVE_GLPK |
37 | 37 |
lemon_libemon_la_SOURCES += lemon/glpk.cc |
38 | 38 |
endif |
39 | 39 |
|
40 | 40 |
if HAVE_CPLEX |
41 | 41 |
lemon_libemon_la_SOURCES += lemon/cplex.cc |
42 | 42 |
endif |
43 | 43 |
|
44 | 44 |
if HAVE_SOPLEX |
45 | 45 |
lemon_libemon_la_SOURCES += lemon/soplex.cc |
46 | 46 |
endif |
47 | 47 |
|
48 | 48 |
if HAVE_CLP |
49 | 49 |
lemon_libemon_la_SOURCES += lemon/clp.cc |
50 | 50 |
endif |
51 | 51 |
|
52 | 52 |
if HAVE_CBC |
53 | 53 |
lemon_libemon_la_SOURCES += lemon/cbc.cc |
54 | 54 |
endif |
55 | 55 |
|
56 | 56 |
lemon_HEADERS += \ |
57 | 57 |
lemon/adaptors.h \ |
58 | 58 |
lemon/arg_parser.h \ |
59 | 59 |
lemon/assert.h \ |
60 |
lemon/bellman_ford.h \ |
|
60 | 61 |
lemon/bfs.h \ |
61 | 62 |
lemon/bin_heap.h \ |
63 |
lemon/binom_heap.h \ |
|
62 | 64 |
lemon/bucket_heap.h \ |
63 | 65 |
lemon/cbc.h \ |
64 | 66 |
lemon/circulation.h \ |
65 | 67 |
lemon/clp.h \ |
66 | 68 |
lemon/color.h \ |
67 | 69 |
lemon/concept_check.h \ |
68 | 70 |
lemon/connectivity.h \ |
69 | 71 |
lemon/counter.h \ |
70 | 72 |
lemon/core.h \ |
71 | 73 |
lemon/cplex.h \ |
72 | 74 |
lemon/dfs.h \ |
73 | 75 |
lemon/dijkstra.h \ |
74 | 76 |
lemon/dim2.h \ |
75 | 77 |
lemon/dimacs.h \ |
76 | 78 |
lemon/edge_set.h \ |
77 | 79 |
lemon/elevator.h \ |
78 | 80 |
lemon/error.h \ |
79 | 81 |
lemon/euler.h \ |
80 | 82 |
lemon/fib_heap.h \ |
83 |
lemon/fourary_heap.h \ |
|
81 | 84 |
lemon/full_graph.h \ |
82 | 85 |
lemon/glpk.h \ |
83 | 86 |
lemon/gomory_hu.h \ |
84 | 87 |
lemon/graph_to_eps.h \ |
85 | 88 |
lemon/grid_graph.h \ |
89 |
lemon/hartmann_orlin.h \ |
|
90 |
lemon/howard.h \ |
|
86 | 91 |
lemon/hypercube_graph.h \ |
92 |
lemon/karp.h \ |
|
93 |
lemon/kary_heap.h \ |
|
87 | 94 |
lemon/kruskal.h \ |
88 | 95 |
lemon/hao_orlin.h \ |
89 | 96 |
lemon/lgf_reader.h \ |
90 | 97 |
lemon/lgf_writer.h \ |
91 | 98 |
lemon/list_graph.h \ |
92 | 99 |
lemon/lp.h \ |
93 | 100 |
lemon/lp_base.h \ |
94 | 101 |
lemon/lp_skeleton.h \ |
95 |
lemon/list_graph.h \ |
|
96 | 102 |
lemon/maps.h \ |
97 | 103 |
lemon/matching.h \ |
98 | 104 |
lemon/math.h \ |
99 | 105 |
lemon/min_cost_arborescence.h \ |
100 | 106 |
lemon/nauty_reader.h \ |
101 | 107 |
lemon/network_simplex.h \ |
108 |
lemon/pairing_heap.h \ |
|
102 | 109 |
lemon/path.h \ |
110 |
lemon/planarity.h \ |
|
103 | 111 |
lemon/preflow.h \ |
104 | 112 |
lemon/radix_heap.h \ |
105 | 113 |
lemon/radix_sort.h \ |
106 | 114 |
lemon/random.h \ |
107 | 115 |
lemon/smart_graph.h \ |
108 | 116 |
lemon/soplex.h \ |
117 |
lemon/static_graph.h \ |
|
109 | 118 |
lemon/suurballe.h \ |
110 | 119 |
lemon/time_measure.h \ |
111 | 120 |
lemon/tolerance.h \ |
112 | 121 |
lemon/unionfind.h \ |
113 | 122 |
lemon/bits/windows.h |
114 | 123 |
|
115 | 124 |
bits_HEADERS += \ |
116 | 125 |
lemon/bits/alteration_notifier.h \ |
117 | 126 |
lemon/bits/array_map.h \ |
118 | 127 |
lemon/bits/bezier.h \ |
119 | 128 |
lemon/bits/default_map.h \ |
120 | 129 |
lemon/bits/edge_set_extender.h \ |
121 | 130 |
lemon/bits/enable_if.h \ |
122 | 131 |
lemon/bits/graph_adaptor_extender.h \ |
123 | 132 |
lemon/bits/graph_extender.h \ |
124 | 133 |
lemon/bits/map_extender.h \ |
125 | 134 |
lemon/bits/path_dump.h \ |
126 | 135 |
lemon/bits/solver_bits.h \ |
127 | 136 |
lemon/bits/traits.h \ |
128 | 137 |
lemon/bits/variant.h \ |
129 | 138 |
lemon/bits/vector_map.h |
130 | 139 |
|
131 | 140 |
concept_HEADERS += \ |
132 | 141 |
lemon/concepts/digraph.h \ |
133 | 142 |
lemon/concepts/graph.h \ |
134 | 143 |
lemon/concepts/graph_components.h \ |
135 | 144 |
lemon/concepts/heap.h \ |
136 | 145 |
lemon/concepts/maps.h \ |
137 | 146 |
lemon/concepts/path.h |
... | ... |
@@ -315,96 +315,99 @@ |
315 | 315 |
return *this; |
316 | 316 |
} |
317 | 317 |
}; |
318 | 318 |
|
319 | 319 |
}; |
320 | 320 |
|
321 | 321 |
template <typename DGR> |
322 | 322 |
class ReverseDigraphBase : public DigraphAdaptorBase<DGR> { |
323 | 323 |
typedef DigraphAdaptorBase<DGR> Parent; |
324 | 324 |
public: |
325 | 325 |
typedef DGR Digraph; |
326 | 326 |
protected: |
327 | 327 |
ReverseDigraphBase() : Parent() { } |
328 | 328 |
public: |
329 | 329 |
typedef typename Parent::Node Node; |
330 | 330 |
typedef typename Parent::Arc Arc; |
331 | 331 |
|
332 | 332 |
void firstIn(Arc& a, const Node& n) const { Parent::firstOut(a, n); } |
333 | 333 |
void firstOut(Arc& a, const Node& n ) const { Parent::firstIn(a, n); } |
334 | 334 |
|
335 | 335 |
void nextIn(Arc& a) const { Parent::nextOut(a); } |
336 | 336 |
void nextOut(Arc& a) const { Parent::nextIn(a); } |
337 | 337 |
|
338 | 338 |
Node source(const Arc& a) const { return Parent::target(a); } |
339 | 339 |
Node target(const Arc& a) const { return Parent::source(a); } |
340 | 340 |
|
341 | 341 |
Arc addArc(const Node& u, const Node& v) { return Parent::addArc(v, u); } |
342 | 342 |
|
343 | 343 |
typedef FindArcTagIndicator<DGR> FindArcTag; |
344 | 344 |
Arc findArc(const Node& u, const Node& v, |
345 | 345 |
const Arc& prev = INVALID) const { |
346 | 346 |
return Parent::findArc(v, u, prev); |
347 | 347 |
} |
348 | 348 |
|
349 | 349 |
}; |
350 | 350 |
|
351 | 351 |
/// \ingroup graph_adaptors |
352 | 352 |
/// |
353 | 353 |
/// \brief Adaptor class for reversing the orientation of the arcs in |
354 | 354 |
/// a digraph. |
355 | 355 |
/// |
356 | 356 |
/// ReverseDigraph can be used for reversing the arcs in a digraph. |
357 | 357 |
/// It conforms to the \ref concepts::Digraph "Digraph" concept. |
358 | 358 |
/// |
359 | 359 |
/// The adapted digraph can also be modified through this adaptor |
360 | 360 |
/// by adding or removing nodes or arcs, unless the \c GR template |
361 | 361 |
/// parameter is set to be \c const. |
362 | 362 |
/// |
363 |
/// This class provides item counting in the same time as the adapted |
|
364 |
/// digraph structure. |
|
365 |
/// |
|
363 | 366 |
/// \tparam DGR The type of the adapted digraph. |
364 | 367 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
365 | 368 |
/// It can also be specified to be \c const. |
366 | 369 |
/// |
367 | 370 |
/// \note The \c Node and \c Arc types of this adaptor and the adapted |
368 | 371 |
/// digraph are convertible to each other. |
369 | 372 |
template<typename DGR> |
370 | 373 |
#ifdef DOXYGEN |
371 | 374 |
class ReverseDigraph { |
372 | 375 |
#else |
373 | 376 |
class ReverseDigraph : |
374 | 377 |
public DigraphAdaptorExtender<ReverseDigraphBase<DGR> > { |
375 | 378 |
#endif |
376 | 379 |
typedef DigraphAdaptorExtender<ReverseDigraphBase<DGR> > Parent; |
377 | 380 |
public: |
378 | 381 |
/// The type of the adapted digraph. |
379 | 382 |
typedef DGR Digraph; |
380 | 383 |
protected: |
381 | 384 |
ReverseDigraph() { } |
382 | 385 |
public: |
383 | 386 |
|
384 | 387 |
/// \brief Constructor |
385 | 388 |
/// |
386 | 389 |
/// Creates a reverse digraph adaptor for the given digraph. |
387 | 390 |
explicit ReverseDigraph(DGR& digraph) { |
388 | 391 |
Parent::initialize(digraph); |
389 | 392 |
} |
390 | 393 |
}; |
391 | 394 |
|
392 | 395 |
/// \brief Returns a read-only ReverseDigraph adaptor |
393 | 396 |
/// |
394 | 397 |
/// This function just returns a read-only \ref ReverseDigraph adaptor. |
395 | 398 |
/// \ingroup graph_adaptors |
396 | 399 |
/// \relates ReverseDigraph |
397 | 400 |
template<typename DGR> |
398 | 401 |
ReverseDigraph<const DGR> reverseDigraph(const DGR& digraph) { |
399 | 402 |
return ReverseDigraph<const DGR>(digraph); |
400 | 403 |
} |
401 | 404 |
|
402 | 405 |
|
403 | 406 |
template <typename DGR, typename NF, typename AF, bool ch = true> |
404 | 407 |
class SubDigraphBase : public DigraphAdaptorBase<DGR> { |
405 | 408 |
typedef DigraphAdaptorBase<DGR> Parent; |
406 | 409 |
public: |
407 | 410 |
typedef DGR Digraph; |
408 | 411 |
typedef NF NodeFilterMap; |
409 | 412 |
typedef AF ArcFilterMap; |
410 | 413 |
|
... | ... |
@@ -674,96 +677,98 @@ |
674 | 677 |
} |
675 | 678 |
}; |
676 | 679 |
|
677 | 680 |
template <typename V> |
678 | 681 |
class ArcMap |
679 | 682 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
680 | 683 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> { |
681 | 684 |
typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
682 | 685 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent; |
683 | 686 |
|
684 | 687 |
public: |
685 | 688 |
typedef V Value; |
686 | 689 |
|
687 | 690 |
ArcMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor) |
688 | 691 |
: Parent(adaptor) {} |
689 | 692 |
ArcMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor, const V& value) |
690 | 693 |
: Parent(adaptor, value) {} |
691 | 694 |
|
692 | 695 |
private: |
693 | 696 |
ArcMap& operator=(const ArcMap& cmap) { |
694 | 697 |
return operator=<ArcMap>(cmap); |
695 | 698 |
} |
696 | 699 |
|
697 | 700 |
template <typename CMap> |
698 | 701 |
ArcMap& operator=(const CMap& cmap) { |
699 | 702 |
Parent::operator=(cmap); |
700 | 703 |
return *this; |
701 | 704 |
} |
702 | 705 |
}; |
703 | 706 |
|
704 | 707 |
}; |
705 | 708 |
|
706 | 709 |
/// \ingroup graph_adaptors |
707 | 710 |
/// |
708 | 711 |
/// \brief Adaptor class for hiding nodes and arcs in a digraph |
709 | 712 |
/// |
710 | 713 |
/// SubDigraph can be used for hiding nodes and arcs in a digraph. |
711 | 714 |
/// A \c bool node map and a \c bool arc map must be specified, which |
712 | 715 |
/// define the filters for nodes and arcs. |
713 | 716 |
/// Only the nodes and arcs with \c true filter value are |
714 | 717 |
/// shown in the subdigraph. The arcs that are incident to hidden |
715 | 718 |
/// nodes are also filtered out. |
716 | 719 |
/// This adaptor conforms to the \ref concepts::Digraph "Digraph" concept. |
717 | 720 |
/// |
718 | 721 |
/// The adapted digraph can also be modified through this adaptor |
719 | 722 |
/// by adding or removing nodes or arcs, unless the \c GR template |
720 | 723 |
/// parameter is set to be \c const. |
721 | 724 |
/// |
725 |
/// This class provides only linear time counting for nodes and arcs. |
|
726 |
/// |
|
722 | 727 |
/// \tparam DGR The type of the adapted digraph. |
723 | 728 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
724 | 729 |
/// It can also be specified to be \c const. |
725 | 730 |
/// \tparam NF The type of the node filter map. |
726 | 731 |
/// It must be a \c bool (or convertible) node map of the |
727 | 732 |
/// adapted digraph. The default type is |
728 | 733 |
/// \ref concepts::Digraph::NodeMap "DGR::NodeMap<bool>". |
729 | 734 |
/// \tparam AF The type of the arc filter map. |
730 | 735 |
/// It must be \c bool (or convertible) arc map of the |
731 | 736 |
/// adapted digraph. The default type is |
732 | 737 |
/// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>". |
733 | 738 |
/// |
734 | 739 |
/// \note The \c Node and \c Arc types of this adaptor and the adapted |
735 | 740 |
/// digraph are convertible to each other. |
736 | 741 |
/// |
737 | 742 |
/// \see FilterNodes |
738 | 743 |
/// \see FilterArcs |
739 | 744 |
#ifdef DOXYGEN |
740 | 745 |
template<typename DGR, typename NF, typename AF> |
741 | 746 |
class SubDigraph { |
742 | 747 |
#else |
743 | 748 |
template<typename DGR, |
744 | 749 |
typename NF = typename DGR::template NodeMap<bool>, |
745 | 750 |
typename AF = typename DGR::template ArcMap<bool> > |
746 | 751 |
class SubDigraph : |
747 | 752 |
public DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> > { |
748 | 753 |
#endif |
749 | 754 |
public: |
750 | 755 |
/// The type of the adapted digraph. |
751 | 756 |
typedef DGR Digraph; |
752 | 757 |
/// The type of the node filter map. |
753 | 758 |
typedef NF NodeFilterMap; |
754 | 759 |
/// The type of the arc filter map. |
755 | 760 |
typedef AF ArcFilterMap; |
756 | 761 |
|
757 | 762 |
typedef DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> > |
758 | 763 |
Parent; |
759 | 764 |
|
760 | 765 |
typedef typename Parent::Node Node; |
761 | 766 |
typedef typename Parent::Arc Arc; |
762 | 767 |
|
763 | 768 |
protected: |
764 | 769 |
SubDigraph() { } |
765 | 770 |
public: |
766 | 771 |
|
767 | 772 |
/// \brief Constructor |
768 | 773 |
/// |
769 | 774 |
/// Creates a subdigraph for the given digraph with the |
... | ... |
@@ -1269,96 +1274,98 @@ |
1269 | 1274 |
|
1270 | 1275 |
template <typename V> |
1271 | 1276 |
class EdgeMap |
1272 | 1277 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
1273 | 1278 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> { |
1274 | 1279 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
1275 | 1280 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent; |
1276 | 1281 |
|
1277 | 1282 |
public: |
1278 | 1283 |
typedef V Value; |
1279 | 1284 |
|
1280 | 1285 |
EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
1281 | 1286 |
: Parent(adaptor) {} |
1282 | 1287 |
|
1283 | 1288 |
EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
1284 | 1289 |
: Parent(adaptor, value) {} |
1285 | 1290 |
|
1286 | 1291 |
private: |
1287 | 1292 |
EdgeMap& operator=(const EdgeMap& cmap) { |
1288 | 1293 |
return operator=<EdgeMap>(cmap); |
1289 | 1294 |
} |
1290 | 1295 |
|
1291 | 1296 |
template <typename CMap> |
1292 | 1297 |
EdgeMap& operator=(const CMap& cmap) { |
1293 | 1298 |
Parent::operator=(cmap); |
1294 | 1299 |
return *this; |
1295 | 1300 |
} |
1296 | 1301 |
}; |
1297 | 1302 |
|
1298 | 1303 |
}; |
1299 | 1304 |
|
1300 | 1305 |
/// \ingroup graph_adaptors |
1301 | 1306 |
/// |
1302 | 1307 |
/// \brief Adaptor class for hiding nodes and edges in an undirected |
1303 | 1308 |
/// graph. |
1304 | 1309 |
/// |
1305 | 1310 |
/// SubGraph can be used for hiding nodes and edges in a graph. |
1306 | 1311 |
/// A \c bool node map and a \c bool edge map must be specified, which |
1307 | 1312 |
/// define the filters for nodes and edges. |
1308 | 1313 |
/// Only the nodes and edges with \c true filter value are |
1309 | 1314 |
/// shown in the subgraph. The edges that are incident to hidden |
1310 | 1315 |
/// nodes are also filtered out. |
1311 | 1316 |
/// This adaptor conforms to the \ref concepts::Graph "Graph" concept. |
1312 | 1317 |
/// |
1313 | 1318 |
/// The adapted graph can also be modified through this adaptor |
1314 | 1319 |
/// by adding or removing nodes or edges, unless the \c GR template |
1315 | 1320 |
/// parameter is set to be \c const. |
1316 | 1321 |
/// |
1322 |
/// This class provides only linear time counting for nodes, edges and arcs. |
|
1323 |
/// |
|
1317 | 1324 |
/// \tparam GR The type of the adapted graph. |
1318 | 1325 |
/// It must conform to the \ref concepts::Graph "Graph" concept. |
1319 | 1326 |
/// It can also be specified to be \c const. |
1320 | 1327 |
/// \tparam NF The type of the node filter map. |
1321 | 1328 |
/// It must be a \c bool (or convertible) node map of the |
1322 | 1329 |
/// adapted graph. The default type is |
1323 | 1330 |
/// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>". |
1324 | 1331 |
/// \tparam EF The type of the edge filter map. |
1325 | 1332 |
/// It must be a \c bool (or convertible) edge map of the |
1326 | 1333 |
/// adapted graph. The default type is |
1327 | 1334 |
/// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
1328 | 1335 |
/// |
1329 | 1336 |
/// \note The \c Node, \c Edge and \c Arc types of this adaptor and the |
1330 | 1337 |
/// adapted graph are convertible to each other. |
1331 | 1338 |
/// |
1332 | 1339 |
/// \see FilterNodes |
1333 | 1340 |
/// \see FilterEdges |
1334 | 1341 |
#ifdef DOXYGEN |
1335 | 1342 |
template<typename GR, typename NF, typename EF> |
1336 | 1343 |
class SubGraph { |
1337 | 1344 |
#else |
1338 | 1345 |
template<typename GR, |
1339 | 1346 |
typename NF = typename GR::template NodeMap<bool>, |
1340 | 1347 |
typename EF = typename GR::template EdgeMap<bool> > |
1341 | 1348 |
class SubGraph : |
1342 | 1349 |
public GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> > { |
1343 | 1350 |
#endif |
1344 | 1351 |
public: |
1345 | 1352 |
/// The type of the adapted graph. |
1346 | 1353 |
typedef GR Graph; |
1347 | 1354 |
/// The type of the node filter map. |
1348 | 1355 |
typedef NF NodeFilterMap; |
1349 | 1356 |
/// The type of the edge filter map. |
1350 | 1357 |
typedef EF EdgeFilterMap; |
1351 | 1358 |
|
1352 | 1359 |
typedef GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> > |
1353 | 1360 |
Parent; |
1354 | 1361 |
|
1355 | 1362 |
typedef typename Parent::Node Node; |
1356 | 1363 |
typedef typename Parent::Edge Edge; |
1357 | 1364 |
|
1358 | 1365 |
protected: |
1359 | 1366 |
SubGraph() { } |
1360 | 1367 |
public: |
1361 | 1368 |
|
1362 | 1369 |
/// \brief Constructor |
1363 | 1370 |
/// |
1364 | 1371 |
/// Creates a subgraph for the given graph with the given node |
... | ... |
@@ -1426,96 +1433,98 @@ |
1426 | 1433 |
/// This function just returns a read-only \ref SubGraph adaptor. |
1427 | 1434 |
/// \ingroup graph_adaptors |
1428 | 1435 |
/// \relates SubGraph |
1429 | 1436 |
template<typename GR, typename NF, typename EF> |
1430 | 1437 |
SubGraph<const GR, NF, EF> |
1431 | 1438 |
subGraph(const GR& graph, NF& node_filter, EF& edge_filter) { |
1432 | 1439 |
return SubGraph<const GR, NF, EF> |
1433 | 1440 |
(graph, node_filter, edge_filter); |
1434 | 1441 |
} |
1435 | 1442 |
|
1436 | 1443 |
template<typename GR, typename NF, typename EF> |
1437 | 1444 |
SubGraph<const GR, const NF, EF> |
1438 | 1445 |
subGraph(const GR& graph, const NF& node_filter, EF& edge_filter) { |
1439 | 1446 |
return SubGraph<const GR, const NF, EF> |
1440 | 1447 |
(graph, node_filter, edge_filter); |
1441 | 1448 |
} |
1442 | 1449 |
|
1443 | 1450 |
template<typename GR, typename NF, typename EF> |
1444 | 1451 |
SubGraph<const GR, NF, const EF> |
1445 | 1452 |
subGraph(const GR& graph, NF& node_filter, const EF& edge_filter) { |
1446 | 1453 |
return SubGraph<const GR, NF, const EF> |
1447 | 1454 |
(graph, node_filter, edge_filter); |
1448 | 1455 |
} |
1449 | 1456 |
|
1450 | 1457 |
template<typename GR, typename NF, typename EF> |
1451 | 1458 |
SubGraph<const GR, const NF, const EF> |
1452 | 1459 |
subGraph(const GR& graph, const NF& node_filter, const EF& edge_filter) { |
1453 | 1460 |
return SubGraph<const GR, const NF, const EF> |
1454 | 1461 |
(graph, node_filter, edge_filter); |
1455 | 1462 |
} |
1456 | 1463 |
|
1457 | 1464 |
|
1458 | 1465 |
/// \ingroup graph_adaptors |
1459 | 1466 |
/// |
1460 | 1467 |
/// \brief Adaptor class for hiding nodes in a digraph or a graph. |
1461 | 1468 |
/// |
1462 | 1469 |
/// FilterNodes adaptor can be used for hiding nodes in a digraph or a |
1463 | 1470 |
/// graph. A \c bool node map must be specified, which defines the filter |
1464 | 1471 |
/// for the nodes. Only the nodes with \c true filter value and the |
1465 | 1472 |
/// arcs/edges incident to nodes both with \c true filter value are shown |
1466 | 1473 |
/// in the subgraph. This adaptor conforms to the \ref concepts::Digraph |
1467 | 1474 |
/// "Digraph" concept or the \ref concepts::Graph "Graph" concept |
1468 | 1475 |
/// depending on the \c GR template parameter. |
1469 | 1476 |
/// |
1470 | 1477 |
/// The adapted (di)graph can also be modified through this adaptor |
1471 | 1478 |
/// by adding or removing nodes or arcs/edges, unless the \c GR template |
1472 | 1479 |
/// parameter is set to be \c const. |
1473 | 1480 |
/// |
1481 |
/// This class provides only linear time item counting. |
|
1482 |
/// |
|
1474 | 1483 |
/// \tparam GR The type of the adapted digraph or graph. |
1475 | 1484 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept |
1476 | 1485 |
/// or the \ref concepts::Graph "Graph" concept. |
1477 | 1486 |
/// It can also be specified to be \c const. |
1478 | 1487 |
/// \tparam NF The type of the node filter map. |
1479 | 1488 |
/// It must be a \c bool (or convertible) node map of the |
1480 | 1489 |
/// adapted (di)graph. The default type is |
1481 | 1490 |
/// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>". |
1482 | 1491 |
/// |
1483 | 1492 |
/// \note The \c Node and <tt>Arc/Edge</tt> types of this adaptor and the |
1484 | 1493 |
/// adapted (di)graph are convertible to each other. |
1485 | 1494 |
#ifdef DOXYGEN |
1486 | 1495 |
template<typename GR, typename NF> |
1487 | 1496 |
class FilterNodes { |
1488 | 1497 |
#else |
1489 | 1498 |
template<typename GR, |
1490 | 1499 |
typename NF = typename GR::template NodeMap<bool>, |
1491 | 1500 |
typename Enable = void> |
1492 | 1501 |
class FilterNodes : |
1493 | 1502 |
public DigraphAdaptorExtender< |
1494 | 1503 |
SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >, |
1495 | 1504 |
true> > { |
1496 | 1505 |
#endif |
1497 | 1506 |
typedef DigraphAdaptorExtender< |
1498 | 1507 |
SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >, |
1499 | 1508 |
true> > Parent; |
1500 | 1509 |
|
1501 | 1510 |
public: |
1502 | 1511 |
|
1503 | 1512 |
typedef GR Digraph; |
1504 | 1513 |
typedef NF NodeFilterMap; |
1505 | 1514 |
|
1506 | 1515 |
typedef typename Parent::Node Node; |
1507 | 1516 |
|
1508 | 1517 |
protected: |
1509 | 1518 |
ConstMap<typename Digraph::Arc, Const<bool, true> > const_true_map; |
1510 | 1519 |
|
1511 | 1520 |
FilterNodes() : const_true_map() {} |
1512 | 1521 |
|
1513 | 1522 |
public: |
1514 | 1523 |
|
1515 | 1524 |
/// \brief Constructor |
1516 | 1525 |
/// |
1517 | 1526 |
/// Creates a subgraph for the given digraph or graph with the |
1518 | 1527 |
/// given node filter map. |
1519 | 1528 |
FilterNodes(GR& graph, NF& node_filter) |
1520 | 1529 |
: Parent(), const_true_map() |
1521 | 1530 |
{ |
... | ... |
@@ -1574,96 +1583,98 @@ |
1574 | 1583 |
FilterNodes() : const_true_map() {} |
1575 | 1584 |
|
1576 | 1585 |
public: |
1577 | 1586 |
|
1578 | 1587 |
FilterNodes(GR& graph, NodeFilterMap& node_filter) : |
1579 | 1588 |
Parent(), const_true_map() { |
1580 | 1589 |
Parent::initialize(graph, node_filter, const_true_map); |
1581 | 1590 |
} |
1582 | 1591 |
|
1583 | 1592 |
void status(const Node& n, bool v) const { Parent::status(n, v); } |
1584 | 1593 |
bool status(const Node& n) const { return Parent::status(n); } |
1585 | 1594 |
void disable(const Node& n) const { Parent::status(n, false); } |
1586 | 1595 |
void enable(const Node& n) const { Parent::status(n, true); } |
1587 | 1596 |
|
1588 | 1597 |
}; |
1589 | 1598 |
|
1590 | 1599 |
|
1591 | 1600 |
/// \brief Returns a read-only FilterNodes adaptor |
1592 | 1601 |
/// |
1593 | 1602 |
/// This function just returns a read-only \ref FilterNodes adaptor. |
1594 | 1603 |
/// \ingroup graph_adaptors |
1595 | 1604 |
/// \relates FilterNodes |
1596 | 1605 |
template<typename GR, typename NF> |
1597 | 1606 |
FilterNodes<const GR, NF> |
1598 | 1607 |
filterNodes(const GR& graph, NF& node_filter) { |
1599 | 1608 |
return FilterNodes<const GR, NF>(graph, node_filter); |
1600 | 1609 |
} |
1601 | 1610 |
|
1602 | 1611 |
template<typename GR, typename NF> |
1603 | 1612 |
FilterNodes<const GR, const NF> |
1604 | 1613 |
filterNodes(const GR& graph, const NF& node_filter) { |
1605 | 1614 |
return FilterNodes<const GR, const NF>(graph, node_filter); |
1606 | 1615 |
} |
1607 | 1616 |
|
1608 | 1617 |
/// \ingroup graph_adaptors |
1609 | 1618 |
/// |
1610 | 1619 |
/// \brief Adaptor class for hiding arcs in a digraph. |
1611 | 1620 |
/// |
1612 | 1621 |
/// FilterArcs adaptor can be used for hiding arcs in a digraph. |
1613 | 1622 |
/// A \c bool arc map must be specified, which defines the filter for |
1614 | 1623 |
/// the arcs. Only the arcs with \c true filter value are shown in the |
1615 | 1624 |
/// subdigraph. This adaptor conforms to the \ref concepts::Digraph |
1616 | 1625 |
/// "Digraph" concept. |
1617 | 1626 |
/// |
1618 | 1627 |
/// The adapted digraph can also be modified through this adaptor |
1619 | 1628 |
/// by adding or removing nodes or arcs, unless the \c GR template |
1620 | 1629 |
/// parameter is set to be \c const. |
1621 | 1630 |
/// |
1631 |
/// This class provides only linear time counting for nodes and arcs. |
|
1632 |
/// |
|
1622 | 1633 |
/// \tparam DGR The type of the adapted digraph. |
1623 | 1634 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
1624 | 1635 |
/// It can also be specified to be \c const. |
1625 | 1636 |
/// \tparam AF The type of the arc filter map. |
1626 | 1637 |
/// It must be a \c bool (or convertible) arc map of the |
1627 | 1638 |
/// adapted digraph. The default type is |
1628 | 1639 |
/// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>". |
1629 | 1640 |
/// |
1630 | 1641 |
/// \note The \c Node and \c Arc types of this adaptor and the adapted |
1631 | 1642 |
/// digraph are convertible to each other. |
1632 | 1643 |
#ifdef DOXYGEN |
1633 | 1644 |
template<typename DGR, |
1634 | 1645 |
typename AF> |
1635 | 1646 |
class FilterArcs { |
1636 | 1647 |
#else |
1637 | 1648 |
template<typename DGR, |
1638 | 1649 |
typename AF = typename DGR::template ArcMap<bool> > |
1639 | 1650 |
class FilterArcs : |
1640 | 1651 |
public DigraphAdaptorExtender< |
1641 | 1652 |
SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >, |
1642 | 1653 |
AF, false> > { |
1643 | 1654 |
#endif |
1644 | 1655 |
typedef DigraphAdaptorExtender< |
1645 | 1656 |
SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >, |
1646 | 1657 |
AF, false> > Parent; |
1647 | 1658 |
|
1648 | 1659 |
public: |
1649 | 1660 |
|
1650 | 1661 |
/// The type of the adapted digraph. |
1651 | 1662 |
typedef DGR Digraph; |
1652 | 1663 |
/// The type of the arc filter map. |
1653 | 1664 |
typedef AF ArcFilterMap; |
1654 | 1665 |
|
1655 | 1666 |
typedef typename Parent::Arc Arc; |
1656 | 1667 |
|
1657 | 1668 |
protected: |
1658 | 1669 |
ConstMap<typename DGR::Node, Const<bool, true> > const_true_map; |
1659 | 1670 |
|
1660 | 1671 |
FilterArcs() : const_true_map() {} |
1661 | 1672 |
|
1662 | 1673 |
public: |
1663 | 1674 |
|
1664 | 1675 |
/// \brief Constructor |
1665 | 1676 |
/// |
1666 | 1677 |
/// Creates a subdigraph for the given digraph with the given arc |
1667 | 1678 |
/// filter map. |
1668 | 1679 |
FilterArcs(DGR& digraph, ArcFilterMap& arc_filter) |
1669 | 1680 |
: Parent(), const_true_map() { |
... | ... |
@@ -1684,96 +1695,98 @@ |
1684 | 1695 |
bool status(const Arc& a) const { return Parent::status(a); } |
1685 | 1696 |
|
1686 | 1697 |
/// \brief Disables the given arc |
1687 | 1698 |
/// |
1688 | 1699 |
/// This function disables the given arc in the subdigraph, |
1689 | 1700 |
/// so the iteration jumps over it. |
1690 | 1701 |
/// It is the same as \ref status() "status(a, false)". |
1691 | 1702 |
void disable(const Arc& a) const { Parent::status(a, false); } |
1692 | 1703 |
|
1693 | 1704 |
/// \brief Enables the given arc |
1694 | 1705 |
/// |
1695 | 1706 |
/// This function enables the given arc in the subdigraph. |
1696 | 1707 |
/// It is the same as \ref status() "status(a, true)". |
1697 | 1708 |
void enable(const Arc& a) const { Parent::status(a, true); } |
1698 | 1709 |
|
1699 | 1710 |
}; |
1700 | 1711 |
|
1701 | 1712 |
/// \brief Returns a read-only FilterArcs adaptor |
1702 | 1713 |
/// |
1703 | 1714 |
/// This function just returns a read-only \ref FilterArcs adaptor. |
1704 | 1715 |
/// \ingroup graph_adaptors |
1705 | 1716 |
/// \relates FilterArcs |
1706 | 1717 |
template<typename DGR, typename AF> |
1707 | 1718 |
FilterArcs<const DGR, AF> |
1708 | 1719 |
filterArcs(const DGR& digraph, AF& arc_filter) { |
1709 | 1720 |
return FilterArcs<const DGR, AF>(digraph, arc_filter); |
1710 | 1721 |
} |
1711 | 1722 |
|
1712 | 1723 |
template<typename DGR, typename AF> |
1713 | 1724 |
FilterArcs<const DGR, const AF> |
1714 | 1725 |
filterArcs(const DGR& digraph, const AF& arc_filter) { |
1715 | 1726 |
return FilterArcs<const DGR, const AF>(digraph, arc_filter); |
1716 | 1727 |
} |
1717 | 1728 |
|
1718 | 1729 |
/// \ingroup graph_adaptors |
1719 | 1730 |
/// |
1720 | 1731 |
/// \brief Adaptor class for hiding edges in a graph. |
1721 | 1732 |
/// |
1722 | 1733 |
/// FilterEdges adaptor can be used for hiding edges in a graph. |
1723 | 1734 |
/// A \c bool edge map must be specified, which defines the filter for |
1724 | 1735 |
/// the edges. Only the edges with \c true filter value are shown in the |
1725 | 1736 |
/// subgraph. This adaptor conforms to the \ref concepts::Graph |
1726 | 1737 |
/// "Graph" concept. |
1727 | 1738 |
/// |
1728 | 1739 |
/// The adapted graph can also be modified through this adaptor |
1729 | 1740 |
/// by adding or removing nodes or edges, unless the \c GR template |
1730 | 1741 |
/// parameter is set to be \c const. |
1731 | 1742 |
/// |
1743 |
/// This class provides only linear time counting for nodes, edges and arcs. |
|
1744 |
/// |
|
1732 | 1745 |
/// \tparam GR The type of the adapted graph. |
1733 | 1746 |
/// It must conform to the \ref concepts::Graph "Graph" concept. |
1734 | 1747 |
/// It can also be specified to be \c const. |
1735 | 1748 |
/// \tparam EF The type of the edge filter map. |
1736 | 1749 |
/// It must be a \c bool (or convertible) edge map of the |
1737 | 1750 |
/// adapted graph. The default type is |
1738 | 1751 |
/// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
1739 | 1752 |
/// |
1740 | 1753 |
/// \note The \c Node, \c Edge and \c Arc types of this adaptor and the |
1741 | 1754 |
/// adapted graph are convertible to each other. |
1742 | 1755 |
#ifdef DOXYGEN |
1743 | 1756 |
template<typename GR, |
1744 | 1757 |
typename EF> |
1745 | 1758 |
class FilterEdges { |
1746 | 1759 |
#else |
1747 | 1760 |
template<typename GR, |
1748 | 1761 |
typename EF = typename GR::template EdgeMap<bool> > |
1749 | 1762 |
class FilterEdges : |
1750 | 1763 |
public GraphAdaptorExtender< |
1751 | 1764 |
SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true> >, |
1752 | 1765 |
EF, false> > { |
1753 | 1766 |
#endif |
1754 | 1767 |
typedef GraphAdaptorExtender< |
1755 | 1768 |
SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true > >, |
1756 | 1769 |
EF, false> > Parent; |
1757 | 1770 |
|
1758 | 1771 |
public: |
1759 | 1772 |
|
1760 | 1773 |
/// The type of the adapted graph. |
1761 | 1774 |
typedef GR Graph; |
1762 | 1775 |
/// The type of the edge filter map. |
1763 | 1776 |
typedef EF EdgeFilterMap; |
1764 | 1777 |
|
1765 | 1778 |
typedef typename Parent::Edge Edge; |
1766 | 1779 |
|
1767 | 1780 |
protected: |
1768 | 1781 |
ConstMap<typename GR::Node, Const<bool, true> > const_true_map; |
1769 | 1782 |
|
1770 | 1783 |
FilterEdges() : const_true_map(true) { |
1771 | 1784 |
Parent::setNodeFilterMap(const_true_map); |
1772 | 1785 |
} |
1773 | 1786 |
|
1774 | 1787 |
public: |
1775 | 1788 |
|
1776 | 1789 |
/// \brief Constructor |
1777 | 1790 |
/// |
1778 | 1791 |
/// Creates a subgraph for the given graph with the given edge |
1779 | 1792 |
/// filter map. |
... | ... |
@@ -2187,96 +2200,99 @@ |
2187 | 2200 |
|
2188 | 2201 |
private: |
2189 | 2202 |
EdgeMap& operator=(const EdgeMap& cmap) { |
2190 | 2203 |
return operator=<EdgeMap>(cmap); |
2191 | 2204 |
} |
2192 | 2205 |
|
2193 | 2206 |
template <typename CMap> |
2194 | 2207 |
EdgeMap& operator=(const CMap& cmap) { |
2195 | 2208 |
Parent::operator=(cmap); |
2196 | 2209 |
return *this; |
2197 | 2210 |
} |
2198 | 2211 |
|
2199 | 2212 |
}; |
2200 | 2213 |
|
2201 | 2214 |
typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier; |
2202 | 2215 |
NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } |
2203 | 2216 |
|
2204 | 2217 |
typedef typename ItemSetTraits<DGR, Edge>::ItemNotifier EdgeNotifier; |
2205 | 2218 |
EdgeNotifier& notifier(Edge) const { return _digraph->notifier(Edge()); } |
2206 | 2219 |
|
2207 | 2220 |
typedef EdgeNotifier ArcNotifier; |
2208 | 2221 |
ArcNotifier& notifier(Arc) const { return _digraph->notifier(Edge()); } |
2209 | 2222 |
|
2210 | 2223 |
protected: |
2211 | 2224 |
|
2212 | 2225 |
UndirectorBase() : _digraph(0) {} |
2213 | 2226 |
|
2214 | 2227 |
DGR* _digraph; |
2215 | 2228 |
|
2216 | 2229 |
void initialize(DGR& digraph) { |
2217 | 2230 |
_digraph = &digraph; |
2218 | 2231 |
} |
2219 | 2232 |
|
2220 | 2233 |
}; |
2221 | 2234 |
|
2222 | 2235 |
/// \ingroup graph_adaptors |
2223 | 2236 |
/// |
2224 | 2237 |
/// \brief Adaptor class for viewing a digraph as an undirected graph. |
2225 | 2238 |
/// |
2226 | 2239 |
/// Undirector adaptor can be used for viewing a digraph as an undirected |
2227 | 2240 |
/// graph. All arcs of the underlying digraph are showed in the |
2228 | 2241 |
/// adaptor as an edge (and also as a pair of arcs, of course). |
2229 | 2242 |
/// This adaptor conforms to the \ref concepts::Graph "Graph" concept. |
2230 | 2243 |
/// |
2231 | 2244 |
/// The adapted digraph can also be modified through this adaptor |
2232 | 2245 |
/// by adding or removing nodes or edges, unless the \c GR template |
2233 | 2246 |
/// parameter is set to be \c const. |
2234 | 2247 |
/// |
2248 |
/// This class provides item counting in the same time as the adapted |
|
2249 |
/// digraph structure. |
|
2250 |
/// |
|
2235 | 2251 |
/// \tparam DGR The type of the adapted digraph. |
2236 | 2252 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
2237 | 2253 |
/// It can also be specified to be \c const. |
2238 | 2254 |
/// |
2239 | 2255 |
/// \note The \c Node type of this adaptor and the adapted digraph are |
2240 | 2256 |
/// convertible to each other, moreover the \c Edge type of the adaptor |
2241 | 2257 |
/// and the \c Arc type of the adapted digraph are also convertible to |
2242 | 2258 |
/// each other. |
2243 | 2259 |
/// (Thus the \c Arc type of the adaptor is convertible to the \c Arc type |
2244 | 2260 |
/// of the adapted digraph.) |
2245 | 2261 |
template<typename DGR> |
2246 | 2262 |
#ifdef DOXYGEN |
2247 | 2263 |
class Undirector { |
2248 | 2264 |
#else |
2249 | 2265 |
class Undirector : |
2250 | 2266 |
public GraphAdaptorExtender<UndirectorBase<DGR> > { |
2251 | 2267 |
#endif |
2252 | 2268 |
typedef GraphAdaptorExtender<UndirectorBase<DGR> > Parent; |
2253 | 2269 |
public: |
2254 | 2270 |
/// The type of the adapted digraph. |
2255 | 2271 |
typedef DGR Digraph; |
2256 | 2272 |
protected: |
2257 | 2273 |
Undirector() { } |
2258 | 2274 |
public: |
2259 | 2275 |
|
2260 | 2276 |
/// \brief Constructor |
2261 | 2277 |
/// |
2262 | 2278 |
/// Creates an undirected graph from the given digraph. |
2263 | 2279 |
Undirector(DGR& digraph) { |
2264 | 2280 |
initialize(digraph); |
2265 | 2281 |
} |
2266 | 2282 |
|
2267 | 2283 |
/// \brief Arc map combined from two original arc maps |
2268 | 2284 |
/// |
2269 | 2285 |
/// This map adaptor class adapts two arc maps of the underlying |
2270 | 2286 |
/// digraph to get an arc map of the undirected graph. |
2271 | 2287 |
/// Its value type is inherited from the first arc map type (\c FW). |
2272 | 2288 |
/// \tparam FW The type of the "foward" arc map. |
2273 | 2289 |
/// \tparam BK The type of the "backward" arc map. |
2274 | 2290 |
template <typename FW, typename BK> |
2275 | 2291 |
class CombinedArcMap { |
2276 | 2292 |
public: |
2277 | 2293 |
|
2278 | 2294 |
/// The key type of the map |
2279 | 2295 |
typedef typename Parent::Arc Key; |
2280 | 2296 |
/// The value type of the map |
2281 | 2297 |
typedef typename FW::Value Value; |
2282 | 2298 |
|
... | ... |
@@ -2490,96 +2506,99 @@ |
2490 | 2506 |
public: |
2491 | 2507 |
|
2492 | 2508 |
explicit ArcMap(const OrienterBase<GR, DM>& adapter) |
2493 | 2509 |
: Parent(*adapter._graph) { } |
2494 | 2510 |
|
2495 | 2511 |
ArcMap(const OrienterBase<GR, DM>& adapter, const V& value) |
2496 | 2512 |
: Parent(*adapter._graph, value) { } |
2497 | 2513 |
|
2498 | 2514 |
private: |
2499 | 2515 |
ArcMap& operator=(const ArcMap& cmap) { |
2500 | 2516 |
return operator=<ArcMap>(cmap); |
2501 | 2517 |
} |
2502 | 2518 |
|
2503 | 2519 |
template <typename CMap> |
2504 | 2520 |
ArcMap& operator=(const CMap& cmap) { |
2505 | 2521 |
Parent::operator=(cmap); |
2506 | 2522 |
return *this; |
2507 | 2523 |
} |
2508 | 2524 |
}; |
2509 | 2525 |
|
2510 | 2526 |
|
2511 | 2527 |
|
2512 | 2528 |
protected: |
2513 | 2529 |
Graph* _graph; |
2514 | 2530 |
DM* _direction; |
2515 | 2531 |
|
2516 | 2532 |
void initialize(GR& graph, DM& direction) { |
2517 | 2533 |
_graph = &graph; |
2518 | 2534 |
_direction = &direction; |
2519 | 2535 |
} |
2520 | 2536 |
|
2521 | 2537 |
}; |
2522 | 2538 |
|
2523 | 2539 |
/// \ingroup graph_adaptors |
2524 | 2540 |
/// |
2525 | 2541 |
/// \brief Adaptor class for orienting the edges of a graph to get a digraph |
2526 | 2542 |
/// |
2527 | 2543 |
/// Orienter adaptor can be used for orienting the edges of a graph to |
2528 | 2544 |
/// get a digraph. A \c bool edge map of the underlying graph must be |
2529 | 2545 |
/// specified, which define the direction of the arcs in the adaptor. |
2530 | 2546 |
/// The arcs can be easily reversed by the \c reverseArc() member function |
2531 | 2547 |
/// of the adaptor. |
2532 | 2548 |
/// This class conforms to the \ref concepts::Digraph "Digraph" concept. |
2533 | 2549 |
/// |
2534 | 2550 |
/// The adapted graph can also be modified through this adaptor |
2535 | 2551 |
/// by adding or removing nodes or arcs, unless the \c GR template |
2536 | 2552 |
/// parameter is set to be \c const. |
2537 | 2553 |
/// |
2554 |
/// This class provides item counting in the same time as the adapted |
|
2555 |
/// graph structure. |
|
2556 |
/// |
|
2538 | 2557 |
/// \tparam GR The type of the adapted graph. |
2539 | 2558 |
/// It must conform to the \ref concepts::Graph "Graph" concept. |
2540 | 2559 |
/// It can also be specified to be \c const. |
2541 | 2560 |
/// \tparam DM The type of the direction map. |
2542 | 2561 |
/// It must be a \c bool (or convertible) edge map of the |
2543 | 2562 |
/// adapted graph. The default type is |
2544 | 2563 |
/// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
2545 | 2564 |
/// |
2546 | 2565 |
/// \note The \c Node type of this adaptor and the adapted graph are |
2547 | 2566 |
/// convertible to each other, moreover the \c Arc type of the adaptor |
2548 | 2567 |
/// and the \c Edge type of the adapted graph are also convertible to |
2549 | 2568 |
/// each other. |
2550 | 2569 |
#ifdef DOXYGEN |
2551 | 2570 |
template<typename GR, |
2552 | 2571 |
typename DM> |
2553 | 2572 |
class Orienter { |
2554 | 2573 |
#else |
2555 | 2574 |
template<typename GR, |
2556 | 2575 |
typename DM = typename GR::template EdgeMap<bool> > |
2557 | 2576 |
class Orienter : |
2558 | 2577 |
public DigraphAdaptorExtender<OrienterBase<GR, DM> > { |
2559 | 2578 |
#endif |
2560 | 2579 |
typedef DigraphAdaptorExtender<OrienterBase<GR, DM> > Parent; |
2561 | 2580 |
public: |
2562 | 2581 |
|
2563 | 2582 |
/// The type of the adapted graph. |
2564 | 2583 |
typedef GR Graph; |
2565 | 2584 |
/// The type of the direction edge map. |
2566 | 2585 |
typedef DM DirectionMap; |
2567 | 2586 |
|
2568 | 2587 |
typedef typename Parent::Arc Arc; |
2569 | 2588 |
|
2570 | 2589 |
protected: |
2571 | 2590 |
Orienter() { } |
2572 | 2591 |
|
2573 | 2592 |
public: |
2574 | 2593 |
|
2575 | 2594 |
/// \brief Constructor |
2576 | 2595 |
/// |
2577 | 2596 |
/// Constructor of the adaptor. |
2578 | 2597 |
Orienter(GR& graph, DM& direction) { |
2579 | 2598 |
Parent::initialize(graph, direction); |
2580 | 2599 |
} |
2581 | 2600 |
|
2582 | 2601 |
/// \brief Reverses the given arc |
2583 | 2602 |
/// |
2584 | 2603 |
/// This function reverses the given arc. |
2585 | 2604 |
/// It is done by simply negate the assigned value of \c a |
... | ... |
@@ -2633,96 +2652,98 @@ |
2633 | 2652 |
}; |
2634 | 2653 |
|
2635 | 2654 |
template<typename DGR,typename CM, typename FM, typename TL> |
2636 | 2655 |
class ResBackwardFilter { |
2637 | 2656 |
public: |
2638 | 2657 |
|
2639 | 2658 |
typedef typename DGR::Arc Key; |
2640 | 2659 |
typedef bool Value; |
2641 | 2660 |
|
2642 | 2661 |
private: |
2643 | 2662 |
|
2644 | 2663 |
const CM* _capacity; |
2645 | 2664 |
const FM* _flow; |
2646 | 2665 |
TL _tolerance; |
2647 | 2666 |
|
2648 | 2667 |
public: |
2649 | 2668 |
|
2650 | 2669 |
ResBackwardFilter(const CM& capacity, const FM& flow, |
2651 | 2670 |
const TL& tolerance = TL()) |
2652 | 2671 |
: _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } |
2653 | 2672 |
|
2654 | 2673 |
bool operator[](const typename DGR::Arc& a) const { |
2655 | 2674 |
return _tolerance.positive((*_flow)[a]); |
2656 | 2675 |
} |
2657 | 2676 |
}; |
2658 | 2677 |
|
2659 | 2678 |
} |
2660 | 2679 |
|
2661 | 2680 |
/// \ingroup graph_adaptors |
2662 | 2681 |
/// |
2663 | 2682 |
/// \brief Adaptor class for composing the residual digraph for directed |
2664 | 2683 |
/// flow and circulation problems. |
2665 | 2684 |
/// |
2666 | 2685 |
/// ResidualDigraph can be used for composing the \e residual digraph |
2667 | 2686 |
/// for directed flow and circulation problems. Let \f$ G=(V, A) \f$ |
2668 | 2687 |
/// be a directed graph and let \f$ F \f$ be a number type. |
2669 | 2688 |
/// Let \f$ flow, cap: A\to F \f$ be functions on the arcs. |
2670 | 2689 |
/// This adaptor implements a digraph structure with node set \f$ V \f$ |
2671 | 2690 |
/// and arc set \f$ A_{forward}\cup A_{backward} \f$, |
2672 | 2691 |
/// where \f$ A_{forward}=\{uv : uv\in A, flow(uv)<cap(uv)\} \f$ and |
2673 | 2692 |
/// \f$ A_{backward}=\{vu : uv\in A, flow(uv)>0\} \f$, i.e. the so |
2674 | 2693 |
/// called residual digraph. |
2675 | 2694 |
/// When the union \f$ A_{forward}\cup A_{backward} \f$ is taken, |
2676 | 2695 |
/// multiplicities are counted, i.e. the adaptor has exactly |
2677 | 2696 |
/// \f$ |A_{forward}| + |A_{backward}|\f$ arcs (it may have parallel |
2678 | 2697 |
/// arcs). |
2679 | 2698 |
/// This class conforms to the \ref concepts::Digraph "Digraph" concept. |
2680 | 2699 |
/// |
2700 |
/// This class provides only linear time counting for nodes and arcs. |
|
2701 |
/// |
|
2681 | 2702 |
/// \tparam DGR The type of the adapted digraph. |
2682 | 2703 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
2683 | 2704 |
/// It is implicitly \c const. |
2684 | 2705 |
/// \tparam CM The type of the capacity map. |
2685 | 2706 |
/// It must be an arc map of some numerical type, which defines |
2686 | 2707 |
/// the capacities in the flow problem. It is implicitly \c const. |
2687 | 2708 |
/// The default type is |
2688 | 2709 |
/// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
2689 | 2710 |
/// \tparam FM The type of the flow map. |
2690 | 2711 |
/// It must be an arc map of some numerical type, which defines |
2691 | 2712 |
/// the flow values in the flow problem. The default type is \c CM. |
2692 | 2713 |
/// \tparam TL The tolerance type for handling inexact computation. |
2693 | 2714 |
/// The default tolerance type depends on the value type of the |
2694 | 2715 |
/// capacity map. |
2695 | 2716 |
/// |
2696 | 2717 |
/// \note This adaptor is implemented using Undirector and FilterArcs |
2697 | 2718 |
/// adaptors. |
2698 | 2719 |
/// |
2699 | 2720 |
/// \note The \c Node type of this adaptor and the adapted digraph are |
2700 | 2721 |
/// convertible to each other, moreover the \c Arc type of the adaptor |
2701 | 2722 |
/// is convertible to the \c Arc type of the adapted digraph. |
2702 | 2723 |
#ifdef DOXYGEN |
2703 | 2724 |
template<typename DGR, typename CM, typename FM, typename TL> |
2704 | 2725 |
class ResidualDigraph |
2705 | 2726 |
#else |
2706 | 2727 |
template<typename DGR, |
2707 | 2728 |
typename CM = typename DGR::template ArcMap<int>, |
2708 | 2729 |
typename FM = CM, |
2709 | 2730 |
typename TL = Tolerance<typename CM::Value> > |
2710 | 2731 |
class ResidualDigraph |
2711 | 2732 |
: public SubDigraph< |
2712 | 2733 |
Undirector<const DGR>, |
2713 | 2734 |
ConstMap<typename DGR::Node, Const<bool, true> >, |
2714 | 2735 |
typename Undirector<const DGR>::template CombinedArcMap< |
2715 | 2736 |
_adaptor_bits::ResForwardFilter<const DGR, CM, FM, TL>, |
2716 | 2737 |
_adaptor_bits::ResBackwardFilter<const DGR, CM, FM, TL> > > |
2717 | 2738 |
#endif |
2718 | 2739 |
{ |
2719 | 2740 |
public: |
2720 | 2741 |
|
2721 | 2742 |
/// The type of the underlying digraph. |
2722 | 2743 |
typedef DGR Digraph; |
2723 | 2744 |
/// The type of the capacity map. |
2724 | 2745 |
typedef CM CapacityMap; |
2725 | 2746 |
/// The type of the flow map. |
2726 | 2747 |
typedef FM FlowMap; |
2727 | 2748 |
/// The tolerance type. |
2728 | 2749 |
typedef TL Tolerance; |
... | ... |
@@ -3280,96 +3301,99 @@ |
3280 | 3301 |
ArcMap(const SplitNodesBase<DGR>& adaptor, const V& value) |
3281 | 3302 |
: Parent(adaptor, value) {} |
3282 | 3303 |
|
3283 | 3304 |
private: |
3284 | 3305 |
ArcMap& operator=(const ArcMap& cmap) { |
3285 | 3306 |
return operator=<ArcMap>(cmap); |
3286 | 3307 |
} |
3287 | 3308 |
|
3288 | 3309 |
template <typename CMap> |
3289 | 3310 |
ArcMap& operator=(const CMap& cmap) { |
3290 | 3311 |
Parent::operator=(cmap); |
3291 | 3312 |
return *this; |
3292 | 3313 |
} |
3293 | 3314 |
}; |
3294 | 3315 |
|
3295 | 3316 |
protected: |
3296 | 3317 |
|
3297 | 3318 |
SplitNodesBase() : _digraph(0) {} |
3298 | 3319 |
|
3299 | 3320 |
DGR* _digraph; |
3300 | 3321 |
|
3301 | 3322 |
void initialize(Digraph& digraph) { |
3302 | 3323 |
_digraph = &digraph; |
3303 | 3324 |
} |
3304 | 3325 |
|
3305 | 3326 |
}; |
3306 | 3327 |
|
3307 | 3328 |
/// \ingroup graph_adaptors |
3308 | 3329 |
/// |
3309 | 3330 |
/// \brief Adaptor class for splitting the nodes of a digraph. |
3310 | 3331 |
/// |
3311 | 3332 |
/// SplitNodes adaptor can be used for splitting each node into an |
3312 | 3333 |
/// \e in-node and an \e out-node in a digraph. Formaly, the adaptor |
3313 | 3334 |
/// replaces each node \f$ u \f$ in the digraph with two nodes, |
3314 | 3335 |
/// namely node \f$ u_{in} \f$ and node \f$ u_{out} \f$. |
3315 | 3336 |
/// If there is a \f$ (v, u) \f$ arc in the original digraph, then the |
3316 | 3337 |
/// new target of the arc will be \f$ u_{in} \f$ and similarly the |
3317 | 3338 |
/// source of each original \f$ (u, v) \f$ arc will be \f$ u_{out} \f$. |
3318 | 3339 |
/// The adaptor adds an additional \e bind \e arc from \f$ u_{in} \f$ |
3319 | 3340 |
/// to \f$ u_{out} \f$ for each node \f$ u \f$ of the original digraph. |
3320 | 3341 |
/// |
3321 | 3342 |
/// The aim of this class is running an algorithm with respect to node |
3322 | 3343 |
/// costs or capacities if the algorithm considers only arc costs or |
3323 | 3344 |
/// capacities directly. |
3324 | 3345 |
/// In this case you can use \c SplitNodes adaptor, and set the node |
3325 | 3346 |
/// costs/capacities of the original digraph to the \e bind \e arcs |
3326 | 3347 |
/// in the adaptor. |
3327 | 3348 |
/// |
3349 |
/// This class provides item counting in the same time as the adapted |
|
3350 |
/// digraph structure. |
|
3351 |
/// |
|
3328 | 3352 |
/// \tparam DGR The type of the adapted digraph. |
3329 | 3353 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
3330 | 3354 |
/// It is implicitly \c const. |
3331 | 3355 |
/// |
3332 | 3356 |
/// \note The \c Node type of this adaptor is converible to the \c Node |
3333 | 3357 |
/// type of the adapted digraph. |
3334 | 3358 |
template <typename DGR> |
3335 | 3359 |
#ifdef DOXYGEN |
3336 | 3360 |
class SplitNodes { |
3337 | 3361 |
#else |
3338 | 3362 |
class SplitNodes |
3339 | 3363 |
: public DigraphAdaptorExtender<SplitNodesBase<const DGR> > { |
3340 | 3364 |
#endif |
3341 | 3365 |
typedef DigraphAdaptorExtender<SplitNodesBase<const DGR> > Parent; |
3342 | 3366 |
|
3343 | 3367 |
public: |
3344 | 3368 |
typedef DGR Digraph; |
3345 | 3369 |
|
3346 | 3370 |
typedef typename DGR::Node DigraphNode; |
3347 | 3371 |
typedef typename DGR::Arc DigraphArc; |
3348 | 3372 |
|
3349 | 3373 |
typedef typename Parent::Node Node; |
3350 | 3374 |
typedef typename Parent::Arc Arc; |
3351 | 3375 |
|
3352 | 3376 |
/// \brief Constructor |
3353 | 3377 |
/// |
3354 | 3378 |
/// Constructor of the adaptor. |
3355 | 3379 |
SplitNodes(const DGR& g) { |
3356 | 3380 |
Parent::initialize(g); |
3357 | 3381 |
} |
3358 | 3382 |
|
3359 | 3383 |
/// \brief Returns \c true if the given node is an in-node. |
3360 | 3384 |
/// |
3361 | 3385 |
/// Returns \c true if the given node is an in-node. |
3362 | 3386 |
static bool inNode(const Node& n) { |
3363 | 3387 |
return Parent::inNode(n); |
3364 | 3388 |
} |
3365 | 3389 |
|
3366 | 3390 |
/// \brief Returns \c true if the given node is an out-node. |
3367 | 3391 |
/// |
3368 | 3392 |
/// Returns \c true if the given node is an out-node. |
3369 | 3393 |
static bool outNode(const Node& n) { |
3370 | 3394 |
return Parent::outNode(n); |
3371 | 3395 |
} |
3372 | 3396 |
|
3373 | 3397 |
/// \brief Returns \c true if the given arc is an original arc. |
3374 | 3398 |
/// |
3375 | 3399 |
/// Returns \c true if the given arc is one of the arcs in the |
... | ... |
@@ -2,146 +2,147 @@ |
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_BFS_H |
20 | 20 |
#define LEMON_BFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 | 24 |
///\brief BFS algorithm. |
25 | 25 |
|
26 | 26 |
#include <lemon/list_graph.h> |
27 | 27 |
#include <lemon/bits/path_dump.h> |
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/error.h> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/path.h> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
///Default traits class of Bfs class. |
36 | 36 |
|
37 | 37 |
///Default traits class of Bfs class. |
38 | 38 |
///\tparam GR Digraph type. |
39 | 39 |
template<class GR> |
40 | 40 |
struct BfsDefaultTraits |
41 | 41 |
{ |
42 | 42 |
///The type of the digraph the algorithm runs on. |
43 | 43 |
typedef GR Digraph; |
44 | 44 |
|
45 | 45 |
///\brief The type of the map that stores the predecessor |
46 | 46 |
///arcs of the shortest paths. |
47 | 47 |
/// |
48 | 48 |
///The type of the map that stores the predecessor |
49 | 49 |
///arcs of the shortest paths. |
50 |
///It must |
|
50 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
52 | 52 |
///Instantiates a \c PredMap. |
53 | 53 |
|
54 | 54 |
///This function instantiates a \ref PredMap. |
55 | 55 |
///\param g is the digraph, to which we would like to define the |
56 | 56 |
///\ref PredMap. |
57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
58 | 58 |
{ |
59 | 59 |
return new PredMap(g); |
60 | 60 |
} |
61 | 61 |
|
62 | 62 |
///The type of the map that indicates which nodes are processed. |
63 | 63 |
|
64 | 64 |
///The type of the map that indicates which nodes are processed. |
65 |
///It must |
|
65 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
66 |
///By default, it is a NullMap. |
|
66 | 67 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
67 | 68 |
///Instantiates a \c ProcessedMap. |
68 | 69 |
|
69 | 70 |
///This function instantiates a \ref ProcessedMap. |
70 | 71 |
///\param g is the digraph, to which |
71 | 72 |
///we would like to define the \ref ProcessedMap |
72 | 73 |
#ifdef DOXYGEN |
73 | 74 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
74 | 75 |
#else |
75 | 76 |
static ProcessedMap *createProcessedMap(const Digraph &) |
76 | 77 |
#endif |
77 | 78 |
{ |
78 | 79 |
return new ProcessedMap(); |
79 | 80 |
} |
80 | 81 |
|
81 | 82 |
///The type of the map that indicates which nodes are reached. |
82 | 83 |
|
83 | 84 |
///The type of the map that indicates which nodes are reached. |
84 |
///It must |
|
85 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
85 | 86 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
86 | 87 |
///Instantiates a \c ReachedMap. |
87 | 88 |
|
88 | 89 |
///This function instantiates a \ref ReachedMap. |
89 | 90 |
///\param g is the digraph, to which |
90 | 91 |
///we would like to define the \ref ReachedMap. |
91 | 92 |
static ReachedMap *createReachedMap(const Digraph &g) |
92 | 93 |
{ |
93 | 94 |
return new ReachedMap(g); |
94 | 95 |
} |
95 | 96 |
|
96 | 97 |
///The type of the map that stores the distances of the nodes. |
97 | 98 |
|
98 | 99 |
///The type of the map that stores the distances of the nodes. |
99 |
///It must |
|
100 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
100 | 101 |
typedef typename Digraph::template NodeMap<int> DistMap; |
101 | 102 |
///Instantiates a \c DistMap. |
102 | 103 |
|
103 | 104 |
///This function instantiates a \ref DistMap. |
104 | 105 |
///\param g is the digraph, to which we would like to define the |
105 | 106 |
///\ref DistMap. |
106 | 107 |
static DistMap *createDistMap(const Digraph &g) |
107 | 108 |
{ |
108 | 109 |
return new DistMap(g); |
109 | 110 |
} |
110 | 111 |
}; |
111 | 112 |
|
112 | 113 |
///%BFS algorithm class. |
113 | 114 |
|
114 | 115 |
///\ingroup search |
115 | 116 |
///This class provides an efficient implementation of the %BFS algorithm. |
116 | 117 |
/// |
117 | 118 |
///There is also a \ref bfs() "function-type interface" for the BFS |
118 | 119 |
///algorithm, which is convenient in the simplier cases and it can be |
119 | 120 |
///used easier. |
120 | 121 |
/// |
121 | 122 |
///\tparam GR The type of the digraph the algorithm runs on. |
122 | 123 |
///The default type is \ref ListDigraph. |
123 | 124 |
#ifdef DOXYGEN |
124 | 125 |
template <typename GR, |
125 | 126 |
typename TR> |
126 | 127 |
#else |
127 | 128 |
template <typename GR=ListDigraph, |
128 | 129 |
typename TR=BfsDefaultTraits<GR> > |
129 | 130 |
#endif |
130 | 131 |
class Bfs { |
131 | 132 |
public: |
132 | 133 |
|
133 | 134 |
///The type of the digraph the algorithm runs on. |
134 | 135 |
typedef typename TR::Digraph Digraph; |
135 | 136 |
|
136 | 137 |
///\brief The type of the map that stores the predecessor arcs of the |
137 | 138 |
///shortest paths. |
138 | 139 |
typedef typename TR::PredMap PredMap; |
139 | 140 |
///The type of the map that stores the distances of the nodes. |
140 | 141 |
typedef typename TR::DistMap DistMap; |
141 | 142 |
///The type of the map that indicates which nodes are reached. |
142 | 143 |
typedef typename TR::ReachedMap ReachedMap; |
143 | 144 |
///The type of the map that indicates which nodes are processed. |
144 | 145 |
typedef typename TR::ProcessedMap ProcessedMap; |
145 | 146 |
///The type of the paths. |
146 | 147 |
typedef PredMapPath<Digraph, PredMap> Path; |
147 | 148 |
|
... | ... |
@@ -180,157 +181,157 @@ |
180 | 181 |
|
181 | 182 |
//Creates the maps if necessary. |
182 | 183 |
void create_maps() |
183 | 184 |
{ |
184 | 185 |
if(!_pred) { |
185 | 186 |
local_pred = true; |
186 | 187 |
_pred = Traits::createPredMap(*G); |
187 | 188 |
} |
188 | 189 |
if(!_dist) { |
189 | 190 |
local_dist = true; |
190 | 191 |
_dist = Traits::createDistMap(*G); |
191 | 192 |
} |
192 | 193 |
if(!_reached) { |
193 | 194 |
local_reached = true; |
194 | 195 |
_reached = Traits::createReachedMap(*G); |
195 | 196 |
} |
196 | 197 |
if(!_processed) { |
197 | 198 |
local_processed = true; |
198 | 199 |
_processed = Traits::createProcessedMap(*G); |
199 | 200 |
} |
200 | 201 |
} |
201 | 202 |
|
202 | 203 |
protected: |
203 | 204 |
|
204 | 205 |
Bfs() {} |
205 | 206 |
|
206 | 207 |
public: |
207 | 208 |
|
208 | 209 |
typedef Bfs Create; |
209 | 210 |
|
210 | 211 |
///\name Named Template Parameters |
211 | 212 |
|
212 | 213 |
///@{ |
213 | 214 |
|
214 | 215 |
template <class T> |
215 | 216 |
struct SetPredMapTraits : public Traits { |
216 | 217 |
typedef T PredMap; |
217 | 218 |
static PredMap *createPredMap(const Digraph &) |
218 | 219 |
{ |
219 | 220 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
220 | 221 |
return 0; // ignore warnings |
221 | 222 |
} |
222 | 223 |
}; |
223 | 224 |
///\brief \ref named-templ-param "Named parameter" for setting |
224 | 225 |
///\c PredMap type. |
225 | 226 |
/// |
226 | 227 |
///\ref named-templ-param "Named parameter" for setting |
227 | 228 |
///\c PredMap type. |
228 |
///It must |
|
229 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
229 | 230 |
template <class T> |
230 | 231 |
struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > { |
231 | 232 |
typedef Bfs< Digraph, SetPredMapTraits<T> > Create; |
232 | 233 |
}; |
233 | 234 |
|
234 | 235 |
template <class T> |
235 | 236 |
struct SetDistMapTraits : public Traits { |
236 | 237 |
typedef T DistMap; |
237 | 238 |
static DistMap *createDistMap(const Digraph &) |
238 | 239 |
{ |
239 | 240 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
240 | 241 |
return 0; // ignore warnings |
241 | 242 |
} |
242 | 243 |
}; |
243 | 244 |
///\brief \ref named-templ-param "Named parameter" for setting |
244 | 245 |
///\c DistMap type. |
245 | 246 |
/// |
246 | 247 |
///\ref named-templ-param "Named parameter" for setting |
247 | 248 |
///\c DistMap type. |
248 |
///It must |
|
249 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
249 | 250 |
template <class T> |
250 | 251 |
struct SetDistMap : public Bfs< Digraph, SetDistMapTraits<T> > { |
251 | 252 |
typedef Bfs< Digraph, SetDistMapTraits<T> > Create; |
252 | 253 |
}; |
253 | 254 |
|
254 | 255 |
template <class T> |
255 | 256 |
struct SetReachedMapTraits : public Traits { |
256 | 257 |
typedef T ReachedMap; |
257 | 258 |
static ReachedMap *createReachedMap(const Digraph &) |
258 | 259 |
{ |
259 | 260 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
260 | 261 |
return 0; // ignore warnings |
261 | 262 |
} |
262 | 263 |
}; |
263 | 264 |
///\brief \ref named-templ-param "Named parameter" for setting |
264 | 265 |
///\c ReachedMap type. |
265 | 266 |
/// |
266 | 267 |
///\ref named-templ-param "Named parameter" for setting |
267 | 268 |
///\c ReachedMap type. |
268 |
///It must |
|
269 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
269 | 270 |
template <class T> |
270 | 271 |
struct SetReachedMap : public Bfs< Digraph, SetReachedMapTraits<T> > { |
271 | 272 |
typedef Bfs< Digraph, SetReachedMapTraits<T> > Create; |
272 | 273 |
}; |
273 | 274 |
|
274 | 275 |
template <class T> |
275 | 276 |
struct SetProcessedMapTraits : public Traits { |
276 | 277 |
typedef T ProcessedMap; |
277 | 278 |
static ProcessedMap *createProcessedMap(const Digraph &) |
278 | 279 |
{ |
279 | 280 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
280 | 281 |
return 0; // ignore warnings |
281 | 282 |
} |
282 | 283 |
}; |
283 | 284 |
///\brief \ref named-templ-param "Named parameter" for setting |
284 | 285 |
///\c ProcessedMap type. |
285 | 286 |
/// |
286 | 287 |
///\ref named-templ-param "Named parameter" for setting |
287 | 288 |
///\c ProcessedMap type. |
288 |
///It must |
|
289 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
289 | 290 |
template <class T> |
290 | 291 |
struct SetProcessedMap : public Bfs< Digraph, SetProcessedMapTraits<T> > { |
291 | 292 |
typedef Bfs< Digraph, SetProcessedMapTraits<T> > Create; |
292 | 293 |
}; |
293 | 294 |
|
294 | 295 |
struct SetStandardProcessedMapTraits : public Traits { |
295 | 296 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
296 | 297 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
297 | 298 |
{ |
298 | 299 |
return new ProcessedMap(g); |
299 | 300 |
return 0; // ignore warnings |
300 | 301 |
} |
301 | 302 |
}; |
302 | 303 |
///\brief \ref named-templ-param "Named parameter" for setting |
303 | 304 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
304 | 305 |
/// |
305 | 306 |
///\ref named-templ-param "Named parameter" for setting |
306 | 307 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
307 | 308 |
///If you don't set it explicitly, it will be automatically allocated. |
308 | 309 |
struct SetStandardProcessedMap : |
309 | 310 |
public Bfs< Digraph, SetStandardProcessedMapTraits > { |
310 | 311 |
typedef Bfs< Digraph, SetStandardProcessedMapTraits > Create; |
311 | 312 |
}; |
312 | 313 |
|
313 | 314 |
///@} |
314 | 315 |
|
315 | 316 |
public: |
316 | 317 |
|
317 | 318 |
///Constructor. |
318 | 319 |
|
319 | 320 |
///Constructor. |
320 | 321 |
///\param g The digraph the algorithm runs on. |
321 | 322 |
Bfs(const Digraph &g) : |
322 | 323 |
G(&g), |
323 | 324 |
_pred(NULL), local_pred(false), |
324 | 325 |
_dist(NULL), local_dist(false), |
325 | 326 |
_reached(NULL), local_reached(false), |
326 | 327 |
_processed(NULL), local_processed(false) |
327 | 328 |
{ } |
328 | 329 |
|
329 | 330 |
///Destructor. |
330 | 331 |
~Bfs() |
331 | 332 |
{ |
332 | 333 |
if(local_pred) delete _pred; |
333 | 334 |
if(local_dist) delete _dist; |
334 | 335 |
if(local_reached) delete _reached; |
335 | 336 |
if(local_processed) delete _processed; |
336 | 337 |
} |
... | ... |
@@ -368,98 +369,98 @@ |
368 | 369 |
local_reached=false; |
369 | 370 |
} |
370 | 371 |
_reached = &m; |
371 | 372 |
return *this; |
372 | 373 |
} |
373 | 374 |
|
374 | 375 |
///Sets the map that indicates which nodes are processed. |
375 | 376 |
|
376 | 377 |
///Sets the map that indicates which nodes are processed. |
377 | 378 |
///If you don't use this function before calling \ref run(Node) "run()" |
378 | 379 |
///or \ref init(), an instance will be allocated automatically. |
379 | 380 |
///The destructor deallocates this automatically allocated map, |
380 | 381 |
///of course. |
381 | 382 |
///\return <tt> (*this) </tt> |
382 | 383 |
Bfs &processedMap(ProcessedMap &m) |
383 | 384 |
{ |
384 | 385 |
if(local_processed) { |
385 | 386 |
delete _processed; |
386 | 387 |
local_processed=false; |
387 | 388 |
} |
388 | 389 |
_processed = &m; |
389 | 390 |
return *this; |
390 | 391 |
} |
391 | 392 |
|
392 | 393 |
///Sets the map that stores the distances of the nodes. |
393 | 394 |
|
394 | 395 |
///Sets the map that stores the distances of the nodes calculated by |
395 | 396 |
///the algorithm. |
396 | 397 |
///If you don't use this function before calling \ref run(Node) "run()" |
397 | 398 |
///or \ref init(), an instance will be allocated automatically. |
398 | 399 |
///The destructor deallocates this automatically allocated map, |
399 | 400 |
///of course. |
400 | 401 |
///\return <tt> (*this) </tt> |
401 | 402 |
Bfs &distMap(DistMap &m) |
402 | 403 |
{ |
403 | 404 |
if(local_dist) { |
404 | 405 |
delete _dist; |
405 | 406 |
local_dist=false; |
406 | 407 |
} |
407 | 408 |
_dist = &m; |
408 | 409 |
return *this; |
409 | 410 |
} |
410 | 411 |
|
411 | 412 |
public: |
412 | 413 |
|
413 | 414 |
///\name Execution Control |
414 | 415 |
///The simplest way to execute the BFS algorithm is to use one of the |
415 | 416 |
///member functions called \ref run(Node) "run()".\n |
416 |
///If you need more control on the execution, first you have to call |
|
417 |
///\ref init(), then you can add several source nodes with |
|
417 |
///If you need better control on the execution, you have to call |
|
418 |
///\ref init() first, then you can add several source nodes with |
|
418 | 419 |
///\ref addSource(). Finally the actual path computation can be |
419 | 420 |
///performed with one of the \ref start() functions. |
420 | 421 |
|
421 | 422 |
///@{ |
422 | 423 |
|
423 | 424 |
///\brief Initializes the internal data structures. |
424 | 425 |
/// |
425 | 426 |
///Initializes the internal data structures. |
426 | 427 |
void init() |
427 | 428 |
{ |
428 | 429 |
create_maps(); |
429 | 430 |
_queue.resize(countNodes(*G)); |
430 | 431 |
_queue_head=_queue_tail=0; |
431 | 432 |
_curr_dist=1; |
432 | 433 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
433 | 434 |
_pred->set(u,INVALID); |
434 | 435 |
_reached->set(u,false); |
435 | 436 |
_processed->set(u,false); |
436 | 437 |
} |
437 | 438 |
} |
438 | 439 |
|
439 | 440 |
///Adds a new source node. |
440 | 441 |
|
441 | 442 |
///Adds a new source node to the set of nodes to be processed. |
442 | 443 |
/// |
443 | 444 |
void addSource(Node s) |
444 | 445 |
{ |
445 | 446 |
if(!(*_reached)[s]) |
446 | 447 |
{ |
447 | 448 |
_reached->set(s,true); |
448 | 449 |
_pred->set(s,INVALID); |
449 | 450 |
_dist->set(s,0); |
450 | 451 |
_queue[_queue_head++]=s; |
451 | 452 |
_queue_next_dist=_queue_head; |
452 | 453 |
} |
453 | 454 |
} |
454 | 455 |
|
455 | 456 |
///Processes the next node. |
456 | 457 |
|
457 | 458 |
///Processes the next node. |
458 | 459 |
/// |
459 | 460 |
///\return The processed node. |
460 | 461 |
/// |
461 | 462 |
///\pre The queue must not be empty. |
462 | 463 |
Node processNextNode() |
463 | 464 |
{ |
464 | 465 |
if(_queue_tail==_queue_next_dist) { |
465 | 466 |
_curr_dist++; |
... | ... |
@@ -655,522 +656,514 @@ |
655 | 656 |
return rnode; |
656 | 657 |
} |
657 | 658 |
|
658 | 659 |
///Runs the algorithm from the given source node. |
659 | 660 |
|
660 | 661 |
///This method runs the %BFS algorithm from node \c s |
661 | 662 |
///in order to compute the shortest path to each node. |
662 | 663 |
/// |
663 | 664 |
///The algorithm computes |
664 | 665 |
///- the shortest path tree, |
665 | 666 |
///- the distance of each node from the root. |
666 | 667 |
/// |
667 | 668 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
668 | 669 |
///\code |
669 | 670 |
/// b.init(); |
670 | 671 |
/// b.addSource(s); |
671 | 672 |
/// b.start(); |
672 | 673 |
///\endcode |
673 | 674 |
void run(Node s) { |
674 | 675 |
init(); |
675 | 676 |
addSource(s); |
676 | 677 |
start(); |
677 | 678 |
} |
678 | 679 |
|
679 | 680 |
///Finds the shortest path between \c s and \c t. |
680 | 681 |
|
681 | 682 |
///This method runs the %BFS algorithm from node \c s |
682 | 683 |
///in order to compute the shortest path to node \c t |
683 | 684 |
///(it stops searching when \c t is processed). |
684 | 685 |
/// |
685 | 686 |
///\return \c true if \c t is reachable form \c s. |
686 | 687 |
/// |
687 | 688 |
///\note Apart from the return value, <tt>b.run(s,t)</tt> is just a |
688 | 689 |
///shortcut of the following code. |
689 | 690 |
///\code |
690 | 691 |
/// b.init(); |
691 | 692 |
/// b.addSource(s); |
692 | 693 |
/// b.start(t); |
693 | 694 |
///\endcode |
694 | 695 |
bool run(Node s,Node t) { |
695 | 696 |
init(); |
696 | 697 |
addSource(s); |
697 | 698 |
start(t); |
698 | 699 |
return reached(t); |
699 | 700 |
} |
700 | 701 |
|
701 | 702 |
///Runs the algorithm to visit all nodes in the digraph. |
702 | 703 |
|
703 |
///This method runs the %BFS algorithm in order to |
|
704 |
///compute the shortest path to each node. |
|
705 |
/// |
|
706 |
///The algorithm computes |
|
707 |
///- the shortest path tree (forest), |
|
708 |
///- the distance of each node from the root(s). |
|
704 |
///This method runs the %BFS algorithm in order to visit all nodes |
|
705 |
///in the digraph. |
|
709 | 706 |
/// |
710 | 707 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
711 | 708 |
///\code |
712 | 709 |
/// b.init(); |
713 | 710 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
714 | 711 |
/// if (!b.reached(n)) { |
715 | 712 |
/// b.addSource(n); |
716 | 713 |
/// b.start(); |
717 | 714 |
/// } |
718 | 715 |
/// } |
719 | 716 |
///\endcode |
720 | 717 |
void run() { |
721 | 718 |
init(); |
722 | 719 |
for (NodeIt n(*G); n != INVALID; ++n) { |
723 | 720 |
if (!reached(n)) { |
724 | 721 |
addSource(n); |
725 | 722 |
start(); |
726 | 723 |
} |
727 | 724 |
} |
728 | 725 |
} |
729 | 726 |
|
730 | 727 |
///@} |
731 | 728 |
|
732 | 729 |
///\name Query Functions |
733 | 730 |
///The results of the BFS algorithm can be obtained using these |
734 | 731 |
///functions.\n |
735 | 732 |
///Either \ref run(Node) "run()" or \ref start() should be called |
736 | 733 |
///before using them. |
737 | 734 |
|
738 | 735 |
///@{ |
739 | 736 |
|
740 |
///The shortest path to |
|
737 |
///The shortest path to the given node. |
|
741 | 738 |
|
742 |
///Returns the shortest path to |
|
739 |
///Returns the shortest path to the given node from the root(s). |
|
743 | 740 |
/// |
744 | 741 |
///\warning \c t should be reached from the root(s). |
745 | 742 |
/// |
746 | 743 |
///\pre Either \ref run(Node) "run()" or \ref init() |
747 | 744 |
///must be called before using this function. |
748 | 745 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
749 | 746 |
|
750 |
///The distance of |
|
747 |
///The distance of the given node from the root(s). |
|
751 | 748 |
|
752 |
///Returns the distance of |
|
749 |
///Returns the distance of the given node from the root(s). |
|
753 | 750 |
/// |
754 | 751 |
///\warning If node \c v is not reached from the root(s), then |
755 | 752 |
///the return value of this function is undefined. |
756 | 753 |
/// |
757 | 754 |
///\pre Either \ref run(Node) "run()" or \ref init() |
758 | 755 |
///must be called before using this function. |
759 | 756 |
int dist(Node v) const { return (*_dist)[v]; } |
760 | 757 |
|
761 |
///Returns the 'previous arc' of the shortest path tree for a node. |
|
762 |
|
|
758 |
///\brief Returns the 'previous arc' of the shortest path tree for |
|
759 |
///the given node. |
|
760 |
/// |
|
763 | 761 |
///This function returns the 'previous arc' of the shortest path |
764 | 762 |
///tree for the node \c v, i.e. it returns the last arc of a |
765 | 763 |
///shortest path from a root to \c v. It is \c INVALID if \c v |
766 | 764 |
///is not reached from the root(s) or if \c v is a root. |
767 | 765 |
/// |
768 | 766 |
///The shortest path tree used here is equal to the shortest path |
769 |
///tree used in \ref predNode(). |
|
767 |
///tree used in \ref predNode() and \ref predMap(). |
|
770 | 768 |
/// |
771 | 769 |
///\pre Either \ref run(Node) "run()" or \ref init() |
772 | 770 |
///must be called before using this function. |
773 | 771 |
Arc predArc(Node v) const { return (*_pred)[v];} |
774 | 772 |
|
775 |
///Returns the 'previous node' of the shortest path tree for a node. |
|
776 |
|
|
773 |
///\brief Returns the 'previous node' of the shortest path tree for |
|
774 |
///the given node. |
|
775 |
/// |
|
777 | 776 |
///This function returns the 'previous node' of the shortest path |
778 | 777 |
///tree for the node \c v, i.e. it returns the last but one node |
779 |
/// |
|
778 |
///of a shortest path from a root to \c v. It is \c INVALID |
|
780 | 779 |
///if \c v is not reached from the root(s) or if \c v is a root. |
781 | 780 |
/// |
782 | 781 |
///The shortest path tree used here is equal to the shortest path |
783 |
///tree used in \ref predArc(). |
|
782 |
///tree used in \ref predArc() and \ref predMap(). |
|
784 | 783 |
/// |
785 | 784 |
///\pre Either \ref run(Node) "run()" or \ref init() |
786 | 785 |
///must be called before using this function. |
787 | 786 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
788 | 787 |
G->source((*_pred)[v]); } |
789 | 788 |
|
790 | 789 |
///\brief Returns a const reference to the node map that stores the |
791 | 790 |
/// distances of the nodes. |
792 | 791 |
/// |
793 | 792 |
///Returns a const reference to the node map that stores the distances |
794 | 793 |
///of the nodes calculated by the algorithm. |
795 | 794 |
/// |
796 | 795 |
///\pre Either \ref run(Node) "run()" or \ref init() |
797 | 796 |
///must be called before using this function. |
798 | 797 |
const DistMap &distMap() const { return *_dist;} |
799 | 798 |
|
800 | 799 |
///\brief Returns a const reference to the node map that stores the |
801 | 800 |
///predecessor arcs. |
802 | 801 |
/// |
803 | 802 |
///Returns a const reference to the node map that stores the predecessor |
804 |
///arcs, which form the shortest path tree. |
|
803 |
///arcs, which form the shortest path tree (forest). |
|
805 | 804 |
/// |
806 | 805 |
///\pre Either \ref run(Node) "run()" or \ref init() |
807 | 806 |
///must be called before using this function. |
808 | 807 |
const PredMap &predMap() const { return *_pred;} |
809 | 808 |
|
810 |
///Checks if |
|
809 |
///Checks if the given node is reached from the root(s). |
|
811 | 810 |
|
812 | 811 |
///Returns \c true if \c v is reached from the root(s). |
813 | 812 |
/// |
814 | 813 |
///\pre Either \ref run(Node) "run()" or \ref init() |
815 | 814 |
///must be called before using this function. |
816 | 815 |
bool reached(Node v) const { return (*_reached)[v]; } |
817 | 816 |
|
818 | 817 |
///@} |
819 | 818 |
}; |
820 | 819 |
|
821 | 820 |
///Default traits class of bfs() function. |
822 | 821 |
|
823 | 822 |
///Default traits class of bfs() function. |
824 | 823 |
///\tparam GR Digraph type. |
825 | 824 |
template<class GR> |
826 | 825 |
struct BfsWizardDefaultTraits |
827 | 826 |
{ |
828 | 827 |
///The type of the digraph the algorithm runs on. |
829 | 828 |
typedef GR Digraph; |
830 | 829 |
|
831 | 830 |
///\brief The type of the map that stores the predecessor |
832 | 831 |
///arcs of the shortest paths. |
833 | 832 |
/// |
834 | 833 |
///The type of the map that stores the predecessor |
835 | 834 |
///arcs of the shortest paths. |
836 |
///It must |
|
835 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
837 | 836 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
838 | 837 |
///Instantiates a PredMap. |
839 | 838 |
|
840 | 839 |
///This function instantiates a PredMap. |
841 | 840 |
///\param g is the digraph, to which we would like to define the |
842 | 841 |
///PredMap. |
843 | 842 |
static PredMap *createPredMap(const Digraph &g) |
844 | 843 |
{ |
845 | 844 |
return new PredMap(g); |
846 | 845 |
} |
847 | 846 |
|
848 | 847 |
///The type of the map that indicates which nodes are processed. |
849 | 848 |
|
850 | 849 |
///The type of the map that indicates which nodes are processed. |
851 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
|
852 |
///By default it is a NullMap. |
|
850 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
851 |
///By default, it is a NullMap. |
|
853 | 852 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
854 | 853 |
///Instantiates a ProcessedMap. |
855 | 854 |
|
856 | 855 |
///This function instantiates a ProcessedMap. |
857 | 856 |
///\param g is the digraph, to which |
858 | 857 |
///we would like to define the ProcessedMap. |
859 | 858 |
#ifdef DOXYGEN |
860 | 859 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
861 | 860 |
#else |
862 | 861 |
static ProcessedMap *createProcessedMap(const Digraph &) |
863 | 862 |
#endif |
864 | 863 |
{ |
865 | 864 |
return new ProcessedMap(); |
866 | 865 |
} |
867 | 866 |
|
868 | 867 |
///The type of the map that indicates which nodes are reached. |
869 | 868 |
|
870 | 869 |
///The type of the map that indicates which nodes are reached. |
871 |
///It must |
|
870 |
///It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
872 | 871 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
873 | 872 |
///Instantiates a ReachedMap. |
874 | 873 |
|
875 | 874 |
///This function instantiates a ReachedMap. |
876 | 875 |
///\param g is the digraph, to which |
877 | 876 |
///we would like to define the ReachedMap. |
878 | 877 |
static ReachedMap *createReachedMap(const Digraph &g) |
879 | 878 |
{ |
880 | 879 |
return new ReachedMap(g); |
881 | 880 |
} |
882 | 881 |
|
883 | 882 |
///The type of the map that stores the distances of the nodes. |
884 | 883 |
|
885 | 884 |
///The type of the map that stores the distances of the nodes. |
886 |
///It must |
|
885 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
|
887 | 886 |
typedef typename Digraph::template NodeMap<int> DistMap; |
888 | 887 |
///Instantiates a DistMap. |
889 | 888 |
|
890 | 889 |
///This function instantiates a DistMap. |
891 | 890 |
///\param g is the digraph, to which we would like to define |
892 | 891 |
///the DistMap |
893 | 892 |
static DistMap *createDistMap(const Digraph &g) |
894 | 893 |
{ |
895 | 894 |
return new DistMap(g); |
896 | 895 |
} |
897 | 896 |
|
898 | 897 |
///The type of the shortest paths. |
899 | 898 |
|
900 | 899 |
///The type of the shortest paths. |
901 |
///It must |
|
900 |
///It must conform to the \ref concepts::Path "Path" concept. |
|
902 | 901 |
typedef lemon::Path<Digraph> Path; |
903 | 902 |
}; |
904 | 903 |
|
905 | 904 |
/// Default traits class used by BfsWizard |
906 | 905 |
|
907 |
/// To make it easier to use Bfs algorithm |
|
908 |
/// we have created a wizard class. |
|
909 |
/// This \ref BfsWizard class needs default traits, |
|
910 |
/// as well as the \ref Bfs class. |
|
911 |
/// The \ref BfsWizardBase is a class to be the default traits of the |
|
912 |
/// \ref BfsWizard class. |
|
906 |
/// Default traits class used by BfsWizard. |
|
907 |
/// \tparam GR The type of the digraph. |
|
913 | 908 |
template<class GR> |
914 | 909 |
class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
915 | 910 |
{ |
916 | 911 |
|
917 | 912 |
typedef BfsWizardDefaultTraits<GR> Base; |
918 | 913 |
protected: |
919 | 914 |
//The type of the nodes in the digraph. |
920 | 915 |
typedef typename Base::Digraph::Node Node; |
921 | 916 |
|
922 | 917 |
//Pointer to the digraph the algorithm runs on. |
923 | 918 |
void *_g; |
924 | 919 |
//Pointer to the map of reached nodes. |
925 | 920 |
void *_reached; |
926 | 921 |
//Pointer to the map of processed nodes. |
927 | 922 |
void *_processed; |
928 | 923 |
//Pointer to the map of predecessors arcs. |
929 | 924 |
void *_pred; |
930 | 925 |
//Pointer to the map of distances. |
931 | 926 |
void *_dist; |
932 | 927 |
//Pointer to the shortest path to the target node. |
933 | 928 |
void *_path; |
934 | 929 |
//Pointer to the distance of the target node. |
935 | 930 |
int *_di; |
936 | 931 |
|
937 | 932 |
public: |
938 | 933 |
/// Constructor. |
939 | 934 |
|
940 |
/// This constructor does not require parameters, |
|
935 |
/// This constructor does not require parameters, it initiates |
|
941 | 936 |
/// all of the attributes to \c 0. |
942 | 937 |
BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
943 | 938 |
_dist(0), _path(0), _di(0) {} |
944 | 939 |
|
945 | 940 |
/// Constructor. |
946 | 941 |
|
947 | 942 |
/// This constructor requires one parameter, |
948 | 943 |
/// others are initiated to \c 0. |
949 | 944 |
/// \param g The digraph the algorithm runs on. |
950 | 945 |
BfsWizardBase(const GR &g) : |
951 | 946 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
952 | 947 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
953 | 948 |
|
954 | 949 |
}; |
955 | 950 |
|
956 | 951 |
/// Auxiliary class for the function-type interface of BFS algorithm. |
957 | 952 |
|
958 | 953 |
/// This auxiliary class is created to implement the |
959 | 954 |
/// \ref bfs() "function-type interface" of \ref Bfs algorithm. |
960 | 955 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
961 | 956 |
/// functions and features of the plain \ref Bfs. |
962 | 957 |
/// |
963 | 958 |
/// This class should only be used through the \ref bfs() function, |
964 | 959 |
/// which makes it easier to use the algorithm. |
965 | 960 |
template<class TR> |
966 | 961 |
class BfsWizard : public TR |
967 | 962 |
{ |
968 | 963 |
typedef TR Base; |
969 | 964 |
|
970 |
///The type of the digraph the algorithm runs on. |
|
971 | 965 |
typedef typename TR::Digraph Digraph; |
972 | 966 |
|
973 | 967 |
typedef typename Digraph::Node Node; |
974 | 968 |
typedef typename Digraph::NodeIt NodeIt; |
975 | 969 |
typedef typename Digraph::Arc Arc; |
976 | 970 |
typedef typename Digraph::OutArcIt OutArcIt; |
977 | 971 |
|
978 |
///\brief The type of the map that stores the predecessor |
|
979 |
///arcs of the shortest paths. |
|
980 | 972 |
typedef typename TR::PredMap PredMap; |
981 |
///\brief The type of the map that stores the distances of the nodes. |
|
982 | 973 |
typedef typename TR::DistMap DistMap; |
983 |
///\brief The type of the map that indicates which nodes are reached. |
|
984 | 974 |
typedef typename TR::ReachedMap ReachedMap; |
985 |
///\brief The type of the map that indicates which nodes are processed. |
|
986 | 975 |
typedef typename TR::ProcessedMap ProcessedMap; |
987 |
///The type of the shortest paths |
|
988 | 976 |
typedef typename TR::Path Path; |
989 | 977 |
|
990 | 978 |
public: |
991 | 979 |
|
992 | 980 |
/// Constructor. |
993 | 981 |
BfsWizard() : TR() {} |
994 | 982 |
|
995 | 983 |
/// Constructor that requires parameters. |
996 | 984 |
|
997 | 985 |
/// Constructor that requires parameters. |
998 | 986 |
/// These parameters will be the default values for the traits class. |
999 | 987 |
/// \param g The digraph the algorithm runs on. |
1000 | 988 |
BfsWizard(const Digraph &g) : |
1001 | 989 |
TR(g) {} |
1002 | 990 |
|
1003 | 991 |
///Copy constructor |
1004 | 992 |
BfsWizard(const TR &b) : TR(b) {} |
1005 | 993 |
|
1006 | 994 |
~BfsWizard() {} |
1007 | 995 |
|
1008 | 996 |
///Runs BFS algorithm from the given source node. |
1009 | 997 |
|
1010 | 998 |
///This method runs BFS algorithm from node \c s |
1011 | 999 |
///in order to compute the shortest path to each node. |
1012 | 1000 |
void run(Node s) |
1013 | 1001 |
{ |
1014 | 1002 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
1015 | 1003 |
if (Base::_pred) |
1016 | 1004 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1017 | 1005 |
if (Base::_dist) |
1018 | 1006 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1019 | 1007 |
if (Base::_reached) |
1020 | 1008 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
1021 | 1009 |
if (Base::_processed) |
1022 | 1010 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1023 | 1011 |
if (s!=INVALID) |
1024 | 1012 |
alg.run(s); |
1025 | 1013 |
else |
1026 | 1014 |
alg.run(); |
1027 | 1015 |
} |
1028 | 1016 |
|
1029 | 1017 |
///Finds the shortest path between \c s and \c t. |
1030 | 1018 |
|
1031 | 1019 |
///This method runs BFS algorithm from node \c s |
1032 | 1020 |
///in order to compute the shortest path to node \c t |
1033 | 1021 |
///(it stops searching when \c t is processed). |
1034 | 1022 |
/// |
1035 | 1023 |
///\return \c true if \c t is reachable form \c s. |
1036 | 1024 |
bool run(Node s, Node t) |
1037 | 1025 |
{ |
1038 | 1026 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
1039 | 1027 |
if (Base::_pred) |
1040 | 1028 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1041 | 1029 |
if (Base::_dist) |
1042 | 1030 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1043 | 1031 |
if (Base::_reached) |
1044 | 1032 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
1045 | 1033 |
if (Base::_processed) |
1046 | 1034 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1047 | 1035 |
alg.run(s,t); |
1048 | 1036 |
if (Base::_path) |
1049 | 1037 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
1050 | 1038 |
if (Base::_di) |
1051 | 1039 |
*Base::_di = alg.dist(t); |
1052 | 1040 |
return alg.reached(t); |
1053 | 1041 |
} |
1054 | 1042 |
|
1055 | 1043 |
///Runs BFS algorithm to visit all nodes in the digraph. |
1056 | 1044 |
|
1057 |
///This method runs BFS algorithm in order to compute |
|
1058 |
///the shortest path to each node. |
|
1045 |
///This method runs BFS algorithm in order to visit all nodes |
|
1046 |
///in the digraph. |
|
1059 | 1047 |
void run() |
1060 | 1048 |
{ |
1061 | 1049 |
run(INVALID); |
1062 | 1050 |
} |
1063 | 1051 |
|
1064 | 1052 |
template<class T> |
1065 | 1053 |
struct SetPredMapBase : public Base { |
1066 | 1054 |
typedef T PredMap; |
1067 | 1055 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1068 | 1056 |
SetPredMapBase(const TR &b) : TR(b) {} |
1069 | 1057 |
}; |
1070 |
///\brief \ref named-func-param "Named parameter" |
|
1071 |
///for setting PredMap object. |
|
1058 |
|
|
1059 |
///\brief \ref named-templ-param "Named parameter" for setting |
|
1060 |
///the predecessor map. |
|
1072 | 1061 |
/// |
1073 |
///\ref named-func-param "Named parameter" |
|
1074 |
///for setting PredMap object. |
|
1062 |
///\ref named-templ-param "Named parameter" function for setting |
|
1063 |
///the map that stores the predecessor arcs of the nodes. |
|
1075 | 1064 |
template<class T> |
1076 | 1065 |
BfsWizard<SetPredMapBase<T> > predMap(const T &t) |
1077 | 1066 |
{ |
1078 | 1067 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1079 | 1068 |
return BfsWizard<SetPredMapBase<T> >(*this); |
1080 | 1069 |
} |
1081 | 1070 |
|
1082 | 1071 |
template<class T> |
1083 | 1072 |
struct SetReachedMapBase : public Base { |
1084 | 1073 |
typedef T ReachedMap; |
1085 | 1074 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
1086 | 1075 |
SetReachedMapBase(const TR &b) : TR(b) {} |
1087 | 1076 |
}; |
1088 |
///\brief \ref named-func-param "Named parameter" |
|
1089 |
///for setting ReachedMap object. |
|
1077 |
|
|
1078 |
///\brief \ref named-templ-param "Named parameter" for setting |
|
1079 |
///the reached map. |
|
1090 | 1080 |
/// |
1091 |
/// \ref named-func-param "Named parameter" |
|
1092 |
///for setting ReachedMap object. |
|
1081 |
///\ref named-templ-param "Named parameter" function for setting |
|
1082 |
///the map that indicates which nodes are reached. |
|
1093 | 1083 |
template<class T> |
1094 | 1084 |
BfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
1095 | 1085 |
{ |
1096 | 1086 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1097 | 1087 |
return BfsWizard<SetReachedMapBase<T> >(*this); |
1098 | 1088 |
} |
1099 | 1089 |
|
1100 | 1090 |
template<class T> |
1101 | 1091 |
struct SetDistMapBase : public Base { |
1102 | 1092 |
typedef T DistMap; |
1103 | 1093 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1104 | 1094 |
SetDistMapBase(const TR &b) : TR(b) {} |
1105 | 1095 |
}; |
1106 |
///\brief \ref named-func-param "Named parameter" |
|
1107 |
///for setting DistMap object. |
|
1096 |
|
|
1097 |
///\brief \ref named-templ-param "Named parameter" for setting |
|
1098 |
///the distance map. |
|
1108 | 1099 |
/// |
1109 |
/// \ref named-func-param "Named parameter" |
|
1110 |
///for setting DistMap object. |
|
1100 |
///\ref named-templ-param "Named parameter" function for setting |
|
1101 |
///the map that stores the distances of the nodes calculated |
|
1102 |
///by the algorithm. |
|
1111 | 1103 |
template<class T> |
1112 | 1104 |
BfsWizard<SetDistMapBase<T> > distMap(const T &t) |
1113 | 1105 |
{ |
1114 | 1106 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1115 | 1107 |
return BfsWizard<SetDistMapBase<T> >(*this); |
1116 | 1108 |
} |
1117 | 1109 |
|
1118 | 1110 |
template<class T> |
1119 | 1111 |
struct SetProcessedMapBase : public Base { |
1120 | 1112 |
typedef T ProcessedMap; |
1121 | 1113 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1122 | 1114 |
SetProcessedMapBase(const TR &b) : TR(b) {} |
1123 | 1115 |
}; |
1124 |
///\brief \ref named-func-param "Named parameter" |
|
1125 |
///for setting ProcessedMap object. |
|
1116 |
|
|
1117 |
///\brief \ref named-func-param "Named parameter" for setting |
|
1118 |
///the processed map. |
|
1126 | 1119 |
/// |
1127 |
/// \ref named-func-param "Named parameter" |
|
1128 |
///for setting ProcessedMap object. |
|
1120 |
///\ref named-templ-param "Named parameter" function for setting |
|
1121 |
///the map that indicates which nodes are processed. |
|
1129 | 1122 |
template<class T> |
1130 | 1123 |
BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
1131 | 1124 |
{ |
1132 | 1125 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1133 | 1126 |
return BfsWizard<SetProcessedMapBase<T> >(*this); |
1134 | 1127 |
} |
1135 | 1128 |
|
1136 | 1129 |
template<class T> |
1137 | 1130 |
struct SetPathBase : public Base { |
1138 | 1131 |
typedef T Path; |
1139 | 1132 |
SetPathBase(const TR &b) : TR(b) {} |
1140 | 1133 |
}; |
1141 | 1134 |
///\brief \ref named-func-param "Named parameter" |
1142 | 1135 |
///for getting the shortest path to the target node. |
1143 | 1136 |
/// |
1144 | 1137 |
///\ref named-func-param "Named parameter" |
1145 | 1138 |
///for getting the shortest path to the target node. |
1146 | 1139 |
template<class T> |
1147 | 1140 |
BfsWizard<SetPathBase<T> > path(const T &t) |
1148 | 1141 |
{ |
1149 | 1142 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1150 | 1143 |
return BfsWizard<SetPathBase<T> >(*this); |
1151 | 1144 |
} |
1152 | 1145 |
|
1153 | 1146 |
///\brief \ref named-func-param "Named parameter" |
1154 | 1147 |
///for getting the distance of the target node. |
1155 | 1148 |
/// |
1156 | 1149 |
///\ref named-func-param "Named parameter" |
1157 | 1150 |
///for getting the distance of the target node. |
1158 | 1151 |
BfsWizard dist(const int &d) |
1159 | 1152 |
{ |
1160 | 1153 |
Base::_di=const_cast<int*>(&d); |
1161 | 1154 |
return *this; |
1162 | 1155 |
} |
1163 | 1156 |
|
1164 | 1157 |
}; |
1165 | 1158 |
|
1166 | 1159 |
///Function-type interface for BFS algorithm. |
1167 | 1160 |
|
1168 | 1161 |
/// \ingroup search |
1169 | 1162 |
///Function-type interface for BFS algorithm. |
1170 | 1163 |
/// |
1171 | 1164 |
///This function also has several \ref named-func-param "named parameters", |
1172 | 1165 |
///they are declared as the members of class \ref BfsWizard. |
1173 | 1166 |
///The following examples show how to use these parameters. |
1174 | 1167 |
///\code |
1175 | 1168 |
/// // Compute shortest path from node s to each node |
1176 | 1169 |
/// bfs(g).predMap(preds).distMap(dists).run(s); |
... | ... |
@@ -1219,97 +1212,97 @@ |
1219 | 1212 |
/// \brief Called when an arc is examined but its target node is |
1220 | 1213 |
/// already discovered. |
1221 | 1214 |
/// |
1222 | 1215 |
/// This function is called when an arc is examined but its target node is |
1223 | 1216 |
/// already discovered. |
1224 | 1217 |
void examine(const Arc& arc) {} |
1225 | 1218 |
}; |
1226 | 1219 |
#else |
1227 | 1220 |
template <typename GR> |
1228 | 1221 |
struct BfsVisitor { |
1229 | 1222 |
typedef GR Digraph; |
1230 | 1223 |
typedef typename Digraph::Arc Arc; |
1231 | 1224 |
typedef typename Digraph::Node Node; |
1232 | 1225 |
void start(const Node&) {} |
1233 | 1226 |
void reach(const Node&) {} |
1234 | 1227 |
void process(const Node&) {} |
1235 | 1228 |
void discover(const Arc&) {} |
1236 | 1229 |
void examine(const Arc&) {} |
1237 | 1230 |
|
1238 | 1231 |
template <typename _Visitor> |
1239 | 1232 |
struct Constraints { |
1240 | 1233 |
void constraints() { |
1241 | 1234 |
Arc arc; |
1242 | 1235 |
Node node; |
1243 | 1236 |
visitor.start(node); |
1244 | 1237 |
visitor.reach(node); |
1245 | 1238 |
visitor.process(node); |
1246 | 1239 |
visitor.discover(arc); |
1247 | 1240 |
visitor.examine(arc); |
1248 | 1241 |
} |
1249 | 1242 |
_Visitor& visitor; |
1250 | 1243 |
}; |
1251 | 1244 |
}; |
1252 | 1245 |
#endif |
1253 | 1246 |
|
1254 | 1247 |
/// \brief Default traits class of BfsVisit class. |
1255 | 1248 |
/// |
1256 | 1249 |
/// Default traits class of BfsVisit class. |
1257 | 1250 |
/// \tparam GR The type of the digraph the algorithm runs on. |
1258 | 1251 |
template<class GR> |
1259 | 1252 |
struct BfsVisitDefaultTraits { |
1260 | 1253 |
|
1261 | 1254 |
/// \brief The type of the digraph the algorithm runs on. |
1262 | 1255 |
typedef GR Digraph; |
1263 | 1256 |
|
1264 | 1257 |
/// \brief The type of the map that indicates which nodes are reached. |
1265 | 1258 |
/// |
1266 | 1259 |
/// The type of the map that indicates which nodes are reached. |
1267 |
/// It must |
|
1260 |
/// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
1268 | 1261 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1269 | 1262 |
|
1270 | 1263 |
/// \brief Instantiates a ReachedMap. |
1271 | 1264 |
/// |
1272 | 1265 |
/// This function instantiates a ReachedMap. |
1273 | 1266 |
/// \param digraph is the digraph, to which |
1274 | 1267 |
/// we would like to define the ReachedMap. |
1275 | 1268 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1276 | 1269 |
return new ReachedMap(digraph); |
1277 | 1270 |
} |
1278 | 1271 |
|
1279 | 1272 |
}; |
1280 | 1273 |
|
1281 | 1274 |
/// \ingroup search |
1282 | 1275 |
/// |
1283 | 1276 |
/// \brief BFS algorithm class with visitor interface. |
1284 | 1277 |
/// |
1285 | 1278 |
/// This class provides an efficient implementation of the BFS algorithm |
1286 | 1279 |
/// with visitor interface. |
1287 | 1280 |
/// |
1288 | 1281 |
/// The BfsVisit class provides an alternative interface to the Bfs |
1289 | 1282 |
/// class. It works with callback mechanism, the BfsVisit object calls |
1290 | 1283 |
/// the member functions of the \c Visitor class on every BFS event. |
1291 | 1284 |
/// |
1292 | 1285 |
/// This interface of the BFS algorithm should be used in special cases |
1293 | 1286 |
/// when extra actions have to be performed in connection with certain |
1294 | 1287 |
/// events of the BFS algorithm. Otherwise consider to use Bfs or bfs() |
1295 | 1288 |
/// instead. |
1296 | 1289 |
/// |
1297 | 1290 |
/// \tparam GR The type of the digraph the algorithm runs on. |
1298 | 1291 |
/// The default type is \ref ListDigraph. |
1299 | 1292 |
/// The value of GR is not used directly by \ref BfsVisit, |
1300 | 1293 |
/// it is only passed to \ref BfsVisitDefaultTraits. |
1301 | 1294 |
/// \tparam VS The Visitor type that is used by the algorithm. |
1302 | 1295 |
/// \ref BfsVisitor "BfsVisitor<GR>" is an empty visitor, which |
1303 | 1296 |
/// does not observe the BFS events. If you want to observe the BFS |
1304 | 1297 |
/// events, you should implement your own visitor class. |
1305 | 1298 |
/// \tparam TR Traits class to set various data types used by the |
1306 | 1299 |
/// algorithm. The default traits class is |
1307 | 1300 |
/// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<GR>". |
1308 | 1301 |
/// See \ref BfsVisitDefaultTraits for the documentation of |
1309 | 1302 |
/// a BFS visit traits class. |
1310 | 1303 |
#ifdef DOXYGEN |
1311 | 1304 |
template <typename GR, typename VS, typename TR> |
1312 | 1305 |
#else |
1313 | 1306 |
template <typename GR = ListDigraph, |
1314 | 1307 |
typename VS = BfsVisitor<GR>, |
1315 | 1308 |
typename TR = BfsVisitDefaultTraits<GR> > |
... | ... |
@@ -1380,98 +1373,98 @@ |
1380 | 1373 |
/// |
1381 | 1374 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
1382 | 1375 |
template <class T> |
1383 | 1376 |
struct SetReachedMap : public BfsVisit< Digraph, Visitor, |
1384 | 1377 |
SetReachedMapTraits<T> > { |
1385 | 1378 |
typedef BfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
1386 | 1379 |
}; |
1387 | 1380 |
///@} |
1388 | 1381 |
|
1389 | 1382 |
public: |
1390 | 1383 |
|
1391 | 1384 |
/// \brief Constructor. |
1392 | 1385 |
/// |
1393 | 1386 |
/// Constructor. |
1394 | 1387 |
/// |
1395 | 1388 |
/// \param digraph The digraph the algorithm runs on. |
1396 | 1389 |
/// \param visitor The visitor object of the algorithm. |
1397 | 1390 |
BfsVisit(const Digraph& digraph, Visitor& visitor) |
1398 | 1391 |
: _digraph(&digraph), _visitor(&visitor), |
1399 | 1392 |
_reached(0), local_reached(false) {} |
1400 | 1393 |
|
1401 | 1394 |
/// \brief Destructor. |
1402 | 1395 |
~BfsVisit() { |
1403 | 1396 |
if(local_reached) delete _reached; |
1404 | 1397 |
} |
1405 | 1398 |
|
1406 | 1399 |
/// \brief Sets the map that indicates which nodes are reached. |
1407 | 1400 |
/// |
1408 | 1401 |
/// Sets the map that indicates which nodes are reached. |
1409 | 1402 |
/// If you don't use this function before calling \ref run(Node) "run()" |
1410 | 1403 |
/// or \ref init(), an instance will be allocated automatically. |
1411 | 1404 |
/// The destructor deallocates this automatically allocated map, |
1412 | 1405 |
/// of course. |
1413 | 1406 |
/// \return <tt> (*this) </tt> |
1414 | 1407 |
BfsVisit &reachedMap(ReachedMap &m) { |
1415 | 1408 |
if(local_reached) { |
1416 | 1409 |
delete _reached; |
1417 | 1410 |
local_reached = false; |
1418 | 1411 |
} |
1419 | 1412 |
_reached = &m; |
1420 | 1413 |
return *this; |
1421 | 1414 |
} |
1422 | 1415 |
|
1423 | 1416 |
public: |
1424 | 1417 |
|
1425 | 1418 |
/// \name Execution Control |
1426 | 1419 |
/// The simplest way to execute the BFS algorithm is to use one of the |
1427 | 1420 |
/// member functions called \ref run(Node) "run()".\n |
1428 |
/// If you need more control on the execution, first you have to call |
|
1429 |
/// \ref init(), then you can add several source nodes with |
|
1421 |
/// If you need better control on the execution, you have to call |
|
1422 |
/// \ref init() first, then you can add several source nodes with |
|
1430 | 1423 |
/// \ref addSource(). Finally the actual path computation can be |
1431 | 1424 |
/// performed with one of the \ref start() functions. |
1432 | 1425 |
|
1433 | 1426 |
/// @{ |
1434 | 1427 |
|
1435 | 1428 |
/// \brief Initializes the internal data structures. |
1436 | 1429 |
/// |
1437 | 1430 |
/// Initializes the internal data structures. |
1438 | 1431 |
void init() { |
1439 | 1432 |
create_maps(); |
1440 | 1433 |
_list.resize(countNodes(*_digraph)); |
1441 | 1434 |
_list_front = _list_back = -1; |
1442 | 1435 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1443 | 1436 |
_reached->set(u, false); |
1444 | 1437 |
} |
1445 | 1438 |
} |
1446 | 1439 |
|
1447 | 1440 |
/// \brief Adds a new source node. |
1448 | 1441 |
/// |
1449 | 1442 |
/// Adds a new source node to the set of nodes to be processed. |
1450 | 1443 |
void addSource(Node s) { |
1451 | 1444 |
if(!(*_reached)[s]) { |
1452 | 1445 |
_reached->set(s,true); |
1453 | 1446 |
_visitor->start(s); |
1454 | 1447 |
_visitor->reach(s); |
1455 | 1448 |
_list[++_list_back] = s; |
1456 | 1449 |
} |
1457 | 1450 |
} |
1458 | 1451 |
|
1459 | 1452 |
/// \brief Processes the next node. |
1460 | 1453 |
/// |
1461 | 1454 |
/// Processes the next node. |
1462 | 1455 |
/// |
1463 | 1456 |
/// \return The processed node. |
1464 | 1457 |
/// |
1465 | 1458 |
/// \pre The queue must not be empty. |
1466 | 1459 |
Node processNextNode() { |
1467 | 1460 |
Node n = _list[++_list_front]; |
1468 | 1461 |
_visitor->process(n); |
1469 | 1462 |
Arc e; |
1470 | 1463 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1471 | 1464 |
Node m = _digraph->target(e); |
1472 | 1465 |
if (!(*_reached)[m]) { |
1473 | 1466 |
_visitor->discover(e); |
1474 | 1467 |
_visitor->reach(m); |
1475 | 1468 |
_reached->set(m, true); |
1476 | 1469 |
_list[++_list_back] = m; |
1477 | 1470 |
} else { |
... | ... |
@@ -1653,100 +1646,96 @@ |
1653 | 1646 |
return rnode; |
1654 | 1647 |
} |
1655 | 1648 |
|
1656 | 1649 |
/// \brief Runs the algorithm from the given source node. |
1657 | 1650 |
/// |
1658 | 1651 |
/// This method runs the %BFS algorithm from node \c s |
1659 | 1652 |
/// in order to compute the shortest path to each node. |
1660 | 1653 |
/// |
1661 | 1654 |
/// The algorithm computes |
1662 | 1655 |
/// - the shortest path tree, |
1663 | 1656 |
/// - the distance of each node from the root. |
1664 | 1657 |
/// |
1665 | 1658 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
1666 | 1659 |
///\code |
1667 | 1660 |
/// b.init(); |
1668 | 1661 |
/// b.addSource(s); |
1669 | 1662 |
/// b.start(); |
1670 | 1663 |
///\endcode |
1671 | 1664 |
void run(Node s) { |
1672 | 1665 |
init(); |
1673 | 1666 |
addSource(s); |
1674 | 1667 |
start(); |
1675 | 1668 |
} |
1676 | 1669 |
|
1677 | 1670 |
/// \brief Finds the shortest path between \c s and \c t. |
1678 | 1671 |
/// |
1679 | 1672 |
/// This method runs the %BFS algorithm from node \c s |
1680 | 1673 |
/// in order to compute the shortest path to node \c t |
1681 | 1674 |
/// (it stops searching when \c t is processed). |
1682 | 1675 |
/// |
1683 | 1676 |
/// \return \c true if \c t is reachable form \c s. |
1684 | 1677 |
/// |
1685 | 1678 |
/// \note Apart from the return value, <tt>b.run(s,t)</tt> is just a |
1686 | 1679 |
/// shortcut of the following code. |
1687 | 1680 |
///\code |
1688 | 1681 |
/// b.init(); |
1689 | 1682 |
/// b.addSource(s); |
1690 | 1683 |
/// b.start(t); |
1691 | 1684 |
///\endcode |
1692 | 1685 |
bool run(Node s,Node t) { |
1693 | 1686 |
init(); |
1694 | 1687 |
addSource(s); |
1695 | 1688 |
start(t); |
1696 | 1689 |
return reached(t); |
1697 | 1690 |
} |
1698 | 1691 |
|
1699 | 1692 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
1700 | 1693 |
/// |
1701 |
/// This method runs the %BFS algorithm in order to |
|
1702 |
/// compute the shortest path to each node. |
|
1703 |
/// |
|
1704 |
/// The algorithm computes |
|
1705 |
/// - the shortest path tree (forest), |
|
1706 |
/// - the distance of each node from the root(s). |
|
1694 |
/// This method runs the %BFS algorithm in order to visit all nodes |
|
1695 |
/// in the digraph. |
|
1707 | 1696 |
/// |
1708 | 1697 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
1709 | 1698 |
///\code |
1710 | 1699 |
/// b.init(); |
1711 | 1700 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
1712 | 1701 |
/// if (!b.reached(n)) { |
1713 | 1702 |
/// b.addSource(n); |
1714 | 1703 |
/// b.start(); |
1715 | 1704 |
/// } |
1716 | 1705 |
/// } |
1717 | 1706 |
///\endcode |
1718 | 1707 |
void run() { |
1719 | 1708 |
init(); |
1720 | 1709 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1721 | 1710 |
if (!reached(it)) { |
1722 | 1711 |
addSource(it); |
1723 | 1712 |
start(); |
1724 | 1713 |
} |
1725 | 1714 |
} |
1726 | 1715 |
} |
1727 | 1716 |
|
1728 | 1717 |
///@} |
1729 | 1718 |
|
1730 | 1719 |
/// \name Query Functions |
1731 | 1720 |
/// The results of the BFS algorithm can be obtained using these |
1732 | 1721 |
/// functions.\n |
1733 | 1722 |
/// Either \ref run(Node) "run()" or \ref start() should be called |
1734 | 1723 |
/// before using them. |
1735 | 1724 |
|
1736 | 1725 |
///@{ |
1737 | 1726 |
|
1738 |
/// \brief Checks if |
|
1727 |
/// \brief Checks if the given node is reached from the root(s). |
|
1739 | 1728 |
/// |
1740 | 1729 |
/// Returns \c true if \c v is reached from the root(s). |
1741 | 1730 |
/// |
1742 | 1731 |
/// \pre Either \ref run(Node) "run()" or \ref init() |
1743 | 1732 |
/// must be called before using this function. |
1744 | 1733 |
bool reached(Node v) const { return (*_reached)[v]; } |
1745 | 1734 |
|
1746 | 1735 |
///@} |
1747 | 1736 |
|
1748 | 1737 |
}; |
1749 | 1738 |
|
1750 | 1739 |
} //END OF NAMESPACE LEMON |
1751 | 1740 |
|
1752 | 1741 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BIN_HEAP_H |
20 | 20 |
#define LEMON_BIN_HEAP_H |
21 | 21 |
|
22 |
///\ingroup |
|
22 |
///\ingroup heaps |
|
23 | 23 |
///\file |
24 |
///\brief Binary |
|
24 |
///\brief Binary heap implementation. |
|
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
#include <utility> |
28 | 28 |
#include <functional> |
29 | 29 |
|
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 |
///\ingroup |
|
32 |
/// \ingroup heaps |
|
33 | 33 |
/// |
34 |
///\brief |
|
34 |
/// \brief Binary heap data structure. |
|
35 | 35 |
/// |
36 |
///This class implements the \e binary \e heap data structure. |
|
36 |
/// This class implements the \e binary \e heap data structure. |
|
37 |
/// It fully conforms to the \ref concepts::Heap "heap concept". |
|
37 | 38 |
/// |
38 |
///A \e heap is a data structure for storing items with specified values |
|
39 |
///called \e priorities in such a way that finding the item with minimum |
|
40 |
///priority is efficient. \c CMP specifies the ordering of the priorities. |
|
41 |
///In a heap one can change the priority of an item, add or erase an |
|
42 |
///item, etc. |
|
43 |
/// |
|
44 |
///\tparam PR Type of the priority of the items. |
|
45 |
///\tparam IM A read and writable item map with int values, used internally |
|
46 |
///to handle the cross references. |
|
47 |
///\tparam CMP A functor class for the ordering of the priorities. |
|
48 |
///The default is \c std::less<PR>. |
|
49 |
/// |
|
50 |
///\sa FibHeap |
|
51 |
///\sa Dijkstra |
|
39 |
/// \tparam PR Type of the priorities of the items. |
|
40 |
/// \tparam IM A read-writable item map with \c int values, used |
|
41 |
/// internally to handle the cross references. |
|
42 |
/// \tparam CMP A functor class for comparing the priorities. |
|
43 |
/// The default is \c std::less<PR>. |
|
44 |
#ifdef DOXYGEN |
|
45 |
template <typename PR, typename IM, typename CMP> |
|
46 |
#else |
|
52 | 47 |
template <typename PR, typename IM, typename CMP = std::less<PR> > |
48 |
#endif |
|
53 | 49 |
class BinHeap { |
50 |
public: |
|
54 | 51 |
|
55 |
public: |
|
56 |
///\e |
|
52 |
/// Type of the item-int map. |
|
57 | 53 |
typedef IM ItemIntMap; |
58 |
/// |
|
54 |
/// Type of the priorities. |
|
59 | 55 |
typedef PR Prio; |
60 |
/// |
|
56 |
/// Type of the items stored in the heap. |
|
61 | 57 |
typedef typename ItemIntMap::Key Item; |
62 |
/// |
|
58 |
/// Type of the item-priority pairs. |
|
63 | 59 |
typedef std::pair<Item,Prio> Pair; |
64 |
/// |
|
60 |
/// Functor type for comparing the priorities. |
|
65 | 61 |
typedef CMP Compare; |
66 | 62 |
|
67 |
/// \brief Type to represent the |
|
63 |
/// \brief Type to represent the states of the items. |
|
68 | 64 |
/// |
69 |
/// Each Item element have a state associated to it. It may be "in heap", |
|
70 |
/// "pre heap" or "post heap". The latter two are indifferent from the |
|
65 |
/// Each item has a state associated to it. It can be "in heap", |
|
66 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the |
|
71 | 67 |
/// heap's point of view, but may be useful to the user. |
72 | 68 |
/// |
73 | 69 |
/// The item-int map must be initialized in such way that it assigns |
74 | 70 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
75 | 71 |
enum State { |
76 | 72 |
IN_HEAP = 0, ///< = 0. |
77 | 73 |
PRE_HEAP = -1, ///< = -1. |
78 | 74 |
POST_HEAP = -2 ///< = -2. |
79 | 75 |
}; |
80 | 76 |
|
81 | 77 |
private: |
82 | 78 |
std::vector<Pair> _data; |
83 | 79 |
Compare _comp; |
84 | 80 |
ItemIntMap &_iim; |
85 | 81 |
|
86 | 82 |
public: |
87 |
|
|
83 |
|
|
84 |
/// \brief Constructor. |
|
88 | 85 |
/// |
89 |
/// The constructor. |
|
90 |
/// \param map should be given to the constructor, since it is used |
|
91 |
/// internally to handle the cross references. The value of the map |
|
92 |
/// must be \c PRE_HEAP (<tt>-1</tt>) for every item. |
|
86 |
/// Constructor. |
|
87 |
/// \param map A map that assigns \c int values to the items. |
|
88 |
/// It is used internally to handle the cross references. |
|
89 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
|
93 | 90 |
explicit BinHeap(ItemIntMap &map) : _iim(map) {} |
94 | 91 |
|
95 |
/// \brief |
|
92 |
/// \brief Constructor. |
|
96 | 93 |
/// |
97 |
/// The constructor. |
|
98 |
/// \param map should be given to the constructor, since it is used |
|
99 |
/// internally to handle the cross references. The value of the map |
|
100 |
/// should be PRE_HEAP (-1) for each element. |
|
101 |
/// |
|
102 |
/// \param comp The comparator function object. |
|
94 |
/// Constructor. |
|
95 |
/// \param map A map that assigns \c int values to the items. |
|
96 |
/// It is used internally to handle the cross references. |
|
97 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
|
98 |
/// \param comp The function object used for comparing the priorities. |
|
103 | 99 |
BinHeap(ItemIntMap &map, const Compare &comp) |
104 | 100 |
: _iim(map), _comp(comp) {} |
105 | 101 |
|
106 | 102 |
|
107 |
/// The number of items stored in the heap. |
|
103 |
/// \brief The number of items stored in the heap. |
|
108 | 104 |
/// |
109 |
/// |
|
105 |
/// This function returns the number of items stored in the heap. |
|
110 | 106 |
int size() const { return _data.size(); } |
111 | 107 |
|
112 |
/// \brief |
|
108 |
/// \brief Check if the heap is empty. |
|
113 | 109 |
/// |
114 |
/// |
|
110 |
/// This function returns \c true if the heap is empty. |
|
115 | 111 |
bool empty() const { return _data.empty(); } |
116 | 112 |
|
117 |
/// \brief Make |
|
113 |
/// \brief Make the heap empty. |
|
118 | 114 |
/// |
119 |
/// Make empty this heap. It does not change the cross reference map. |
|
120 |
/// If you want to reuse what is not surely empty you should first clear |
|
121 |
/// the heap and after that you should set the cross reference map for |
|
122 |
/// each item to \c PRE_HEAP. |
|
115 |
/// This functon makes the heap empty. |
|
116 |
/// It does not change the cross reference map. If you want to reuse |
|
117 |
/// a heap that is not surely empty, you should first clear it and |
|
118 |
/// then you should set the cross reference map to \c PRE_HEAP |
|
119 |
/// for each item. |
|
123 | 120 |
void clear() { |
124 | 121 |
_data.clear(); |
125 | 122 |
} |
126 | 123 |
|
127 | 124 |
private: |
128 | 125 |
static int parent(int i) { return (i-1)/2; } |
129 | 126 |
|
130 |
static int |
|
127 |
static int secondChild(int i) { return 2*i+2; } |
|
131 | 128 |
bool less(const Pair &p1, const Pair &p2) const { |
132 | 129 |
return _comp(p1.second, p2.second); |
133 | 130 |
} |
134 | 131 |
|
135 |
int |
|
132 |
int bubbleUp(int hole, Pair p) { |
|
136 | 133 |
int par = parent(hole); |
137 | 134 |
while( hole>0 && less(p,_data[par]) ) { |
138 | 135 |
move(_data[par],hole); |
139 | 136 |
hole = par; |
140 | 137 |
par = parent(hole); |
141 | 138 |
} |
142 | 139 |
move(p, hole); |
143 | 140 |
return hole; |
144 | 141 |
} |
145 | 142 |
|
146 |
int bubble_down(int hole, Pair p, int length) { |
|
147 |
int child = second_child(hole); |
|
143 |
int bubbleDown(int hole, Pair p, int length) { |
|
144 |
int child = secondChild(hole); |
|
148 | 145 |
while(child < length) { |
149 | 146 |
if( less(_data[child-1], _data[child]) ) { |
150 | 147 |
--child; |
151 | 148 |
} |
152 | 149 |
if( !less(_data[child], p) ) |
153 | 150 |
goto ok; |
154 | 151 |
move(_data[child], hole); |
155 | 152 |
hole = child; |
156 |
child = |
|
153 |
child = secondChild(hole); |
|
157 | 154 |
} |
158 | 155 |
child--; |
159 | 156 |
if( child<length && less(_data[child], p) ) { |
160 | 157 |
move(_data[child], hole); |
161 | 158 |
hole=child; |
162 | 159 |
} |
163 | 160 |
ok: |
164 | 161 |
move(p, hole); |
165 | 162 |
return hole; |
166 | 163 |
} |
167 | 164 |
|
168 | 165 |
void move(const Pair &p, int i) { |
169 | 166 |
_data[i] = p; |
170 | 167 |
_iim.set(p.first, i); |
171 | 168 |
} |
172 | 169 |
|
173 | 170 |
public: |
171 |
|
|
174 | 172 |
/// \brief Insert a pair of item and priority into the heap. |
175 | 173 |
/// |
176 |
/// |
|
174 |
/// This function inserts \c p.first to the heap with priority |
|
175 |
/// \c p.second. |
|
177 | 176 |
/// \param p The pair to insert. |
177 |
/// \pre \c p.first must not be stored in the heap. |
|
178 | 178 |
void push(const Pair &p) { |
179 | 179 |
int n = _data.size(); |
180 | 180 |
_data.resize(n+1); |
181 |
|
|
181 |
bubbleUp(n, p); |
|
182 | 182 |
} |
183 | 183 |
|
184 |
/// \brief Insert an item into the heap with the given |
|
184 |
/// \brief Insert an item into the heap with the given priority. |
|
185 | 185 |
/// |
186 |
/// |
|
186 |
/// This function inserts the given item into the heap with the |
|
187 |
/// given priority. |
|
187 | 188 |
/// \param i The item to insert. |
188 | 189 |
/// \param p The priority of the item. |
190 |
/// \pre \e i must not be stored in the heap. |
|
189 | 191 |
void push(const Item &i, const Prio &p) { push(Pair(i,p)); } |
190 | 192 |
|
191 |
/// \brief |
|
193 |
/// \brief Return the item having minimum priority. |
|
192 | 194 |
/// |
193 |
/// This method returns the item with minimum priority relative to \c |
|
194 |
/// Compare. |
|
195 |
/// |
|
195 |
/// This function returns the item having minimum priority. |
|
196 |
/// \pre The heap must be non-empty. |
|
196 | 197 |
Item top() const { |
197 | 198 |
return _data[0].first; |
198 | 199 |
} |
199 | 200 |
|
200 |
/// \brief |
|
201 |
/// \brief The minimum priority. |
|
201 | 202 |
/// |
202 |
/// It returns the minimum priority relative to \c Compare. |
|
203 |
/// \pre The heap must be nonempty. |
|
203 |
/// This function returns the minimum priority. |
|
204 |
/// \pre The heap must be non-empty. |
|
204 | 205 |
Prio prio() const { |
205 | 206 |
return _data[0].second; |
206 | 207 |
} |
207 | 208 |
|
208 |
/// \brief |
|
209 |
/// \brief Remove the item having minimum priority. |
|
209 | 210 |
/// |
210 |
/// This method deletes the item with minimum priority relative to \c |
|
211 |
/// Compare from the heap. |
|
211 |
/// This function removes the item having minimum priority. |
|
212 | 212 |
/// \pre The heap must be non-empty. |
213 | 213 |
void pop() { |
214 | 214 |
int n = _data.size()-1; |
215 | 215 |
_iim.set(_data[0].first, POST_HEAP); |
216 | 216 |
if (n > 0) { |
217 |
|
|
217 |
bubbleDown(0, _data[n], n); |
|
218 | 218 |
} |
219 | 219 |
_data.pop_back(); |
220 | 220 |
} |
221 | 221 |
|
222 |
/// \brief |
|
222 |
/// \brief Remove the given item from the heap. |
|
223 | 223 |
/// |
224 |
/// This method deletes item \c i from the heap. |
|
225 |
/// \param i The item to erase. |
|
226 |
/// |
|
224 |
/// This function removes the given item from the heap if it is |
|
225 |
/// already stored. |
|
226 |
/// \param i The item to delete. |
|
227 |
/// \pre \e i must be in the heap. |
|
227 | 228 |
void erase(const Item &i) { |
228 | 229 |
int h = _iim[i]; |
229 | 230 |
int n = _data.size()-1; |
230 | 231 |
_iim.set(_data[h].first, POST_HEAP); |
231 | 232 |
if( h < n ) { |
232 |
if ( bubble_up(h, _data[n]) == h) { |
|
233 |
bubble_down(h, _data[n], n); |
|
233 |
if ( bubbleUp(h, _data[n]) == h) { |
|
234 |
bubbleDown(h, _data[n], n); |
|
234 | 235 |
} |
235 | 236 |
} |
236 | 237 |
_data.pop_back(); |
237 | 238 |
} |
238 | 239 |
|
239 |
|
|
240 |
/// \brief Returns the priority of \c i. |
|
240 |
/// \brief The priority of the given item. |
|
241 | 241 |
/// |
242 |
/// This function returns the priority of |
|
242 |
/// This function returns the priority of the given item. |
|
243 | 243 |
/// \param i The item. |
244 |
/// \pre \ |
|
244 |
/// \pre \e i must be in the heap. |
|
245 | 245 |
Prio operator[](const Item &i) const { |
246 | 246 |
int idx = _iim[i]; |
247 | 247 |
return _data[idx].second; |
248 | 248 |
} |
249 | 249 |
|
250 |
/// \brief \c i gets to the heap with priority \c p independently |
|
251 |
/// if \c i was already there. |
|
250 |
/// \brief Set the priority of an item or insert it, if it is |
|
251 |
/// not stored in the heap. |
|
252 | 252 |
/// |
253 |
/// This method calls \ref push(\c i, \c p) if \c i is not stored |
|
254 |
/// in the heap and sets the priority of \c i to \c p otherwise. |
|
253 |
/// This method sets the priority of the given item if it is |
|
254 |
/// already stored in the heap. Otherwise it inserts the given |
|
255 |
/// item into the heap with the given priority. |
|
255 | 256 |
/// \param i The item. |
256 | 257 |
/// \param p The priority. |
257 | 258 |
void set(const Item &i, const Prio &p) { |
258 | 259 |
int idx = _iim[i]; |
259 | 260 |
if( idx < 0 ) { |
260 | 261 |
push(i,p); |
261 | 262 |
} |
262 | 263 |
else if( _comp(p, _data[idx].second) ) { |
263 |
|
|
264 |
bubbleUp(idx, Pair(i,p)); |
|
264 | 265 |
} |
265 | 266 |
else { |
266 |
|
|
267 |
bubbleDown(idx, Pair(i,p), _data.size()); |
|
267 | 268 |
} |
268 | 269 |
} |
269 | 270 |
|
270 |
/// \brief |
|
271 |
/// \brief Decrease the priority of an item to the given value. |
|
271 | 272 |
/// |
272 |
/// This |
|
273 |
/// This function decreases the priority of an item to the given value. |
|
273 | 274 |
/// \param i The item. |
274 | 275 |
/// \param p The priority. |
275 |
/// \pre \c i must be stored in the heap with priority at least \c |
|
276 |
/// p relative to \c Compare. |
|
276 |
/// \pre \e i must be stored in the heap with priority at least \e p. |
|
277 | 277 |
void decrease(const Item &i, const Prio &p) { |
278 | 278 |
int idx = _iim[i]; |
279 |
|
|
279 |
bubbleUp(idx, Pair(i,p)); |
|
280 | 280 |
} |
281 | 281 |
|
282 |
/// \brief |
|
282 |
/// \brief Increase the priority of an item to the given value. |
|
283 | 283 |
/// |
284 |
/// This |
|
284 |
/// This function increases the priority of an item to the given value. |
|
285 | 285 |
/// \param i The item. |
286 | 286 |
/// \param p The priority. |
287 |
/// \pre \c i must be stored in the heap with priority at most \c |
|
288 |
/// p relative to \c Compare. |
|
287 |
/// \pre \e i must be stored in the heap with priority at most \e p. |
|
289 | 288 |
void increase(const Item &i, const Prio &p) { |
290 | 289 |
int idx = _iim[i]; |
291 |
|
|
290 |
bubbleDown(idx, Pair(i,p), _data.size()); |
|
292 | 291 |
} |
293 | 292 |
|
294 |
/// \brief Returns if \c item is in, has already been in, or has |
|
295 |
/// never been in the heap. |
|
293 |
/// \brief Return the state of an item. |
|
296 | 294 |
/// |
297 |
/// This method returns PRE_HEAP if \c item has never been in the |
|
298 |
/// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP |
|
299 |
/// otherwise. In the latter case it is possible that \c item will |
|
300 |
/// get back to the heap again. |
|
295 |
/// This method returns \c PRE_HEAP if the given item has never |
|
296 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
|
297 |
/// and \c POST_HEAP otherwise. |
|
298 |
/// In the latter case it is possible that the item will get back |
|
299 |
/// to the heap again. |
|
301 | 300 |
/// \param i The item. |
302 | 301 |
State state(const Item &i) const { |
303 | 302 |
int s = _iim[i]; |
304 | 303 |
if( s>=0 ) |
305 | 304 |
s=0; |
306 | 305 |
return State(s); |
307 | 306 |
} |
308 | 307 |
|
309 |
/// \brief |
|
308 |
/// \brief Set the state of an item in the heap. |
|
310 | 309 |
/// |
311 |
/// Sets the state of the \c item in the heap. It can be used to |
|
312 |
/// manually clear the heap when it is important to achive the |
|
313 |
/// |
|
310 |
/// This function sets the state of the given item in the heap. |
|
311 |
/// It can be used to manually clear the heap when it is important |
|
312 |
/// to achive better time complexity. |
|
314 | 313 |
/// \param i The item. |
315 | 314 |
/// \param st The state. It should not be \c IN_HEAP. |
316 | 315 |
void state(const Item& i, State st) { |
317 | 316 |
switch (st) { |
318 | 317 |
case POST_HEAP: |
319 | 318 |
case PRE_HEAP: |
320 | 319 |
if (state(i) == IN_HEAP) { |
321 | 320 |
erase(i); |
322 | 321 |
} |
323 | 322 |
_iim[i] = st; |
324 | 323 |
break; |
325 | 324 |
case IN_HEAP: |
326 | 325 |
break; |
327 | 326 |
} |
328 | 327 |
} |
329 | 328 |
|
330 |
/// \brief |
|
329 |
/// \brief Replace an item in the heap. |
|
331 | 330 |
/// |
332 |
/// The \c i item is replaced with \c j item. The \c i item should |
|
333 |
/// be in the heap, while the \c j should be out of the heap. The |
|
334 |
/// \c i item will out of the heap and \c j will be in the heap |
|
335 |
/// with the same prioriority as prevoiusly the \c i item. |
|
331 |
/// This function replaces item \c i with item \c j. |
|
332 |
/// Item \c i must be in the heap, while \c j must be out of the heap. |
|
333 |
/// After calling this method, item \c i will be out of the |
|
334 |
/// heap and \c j will be in the heap with the same prioriority |
|
335 |
/// as item \c i had before. |
|
336 | 336 |
void replace(const Item& i, const Item& j) { |
337 | 337 |
int idx = _iim[i]; |
338 | 338 |
_iim.set(i, _iim[j]); |
339 | 339 |
_iim.set(j, idx); |
340 | 340 |
_data[idx].first = j; |
341 | 341 |
} |
342 | 342 |
|
343 | 343 |
}; // class BinHeap |
344 | 344 |
|
345 | 345 |
} // namespace lemon |
346 | 346 |
|
347 | 347 |
#endif // LEMON_BIN_HEAP_H |
... | ... |
@@ -492,121 +492,121 @@ |
492 | 492 |
// \brief Base node of the iterator |
493 | 493 |
// |
494 | 494 |
// Returns the base node (ie. the source in this case) of the iterator |
495 | 495 |
Node baseNode(const OutArcIt &e) const { |
496 | 496 |
return Parent::source(static_cast<const Arc&>(e)); |
497 | 497 |
} |
498 | 498 |
// \brief Running node of the iterator |
499 | 499 |
// |
500 | 500 |
// Returns the running node (ie. the target in this case) of the |
501 | 501 |
// iterator |
502 | 502 |
Node runningNode(const OutArcIt &e) const { |
503 | 503 |
return Parent::target(static_cast<const Arc&>(e)); |
504 | 504 |
} |
505 | 505 |
|
506 | 506 |
// \brief Base node of the iterator |
507 | 507 |
// |
508 | 508 |
// Returns the base node (ie. the target in this case) of the iterator |
509 | 509 |
Node baseNode(const InArcIt &e) const { |
510 | 510 |
return Parent::target(static_cast<const Arc&>(e)); |
511 | 511 |
} |
512 | 512 |
// \brief Running node of the iterator |
513 | 513 |
// |
514 | 514 |
// Returns the running node (ie. the source in this case) of the |
515 | 515 |
// iterator |
516 | 516 |
Node runningNode(const InArcIt &e) const { |
517 | 517 |
return Parent::source(static_cast<const Arc&>(e)); |
518 | 518 |
} |
519 | 519 |
|
520 | 520 |
// Base node of the iterator |
521 | 521 |
// |
522 | 522 |
// Returns the base node of the iterator |
523 | 523 |
Node baseNode(const IncEdgeIt &e) const { |
524 | 524 |
return e.direction ? u(e) : v(e); |
525 | 525 |
} |
526 | 526 |
// Running node of the iterator |
527 | 527 |
// |
528 | 528 |
// Returns the running node of the iterator |
529 | 529 |
Node runningNode(const IncEdgeIt &e) const { |
530 | 530 |
return e.direction ? v(e) : u(e); |
531 | 531 |
} |
532 | 532 |
|
533 | 533 |
|
534 | 534 |
template <typename _Value> |
535 | 535 |
class ArcMap |
536 | 536 |
: public MapExtender<DefaultMap<Graph, Arc, _Value> > { |
537 | 537 |
typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent; |
538 | 538 |
|
539 | 539 |
public: |
540 |
ArcMap(const Graph& _g) |
|
540 |
explicit ArcMap(const Graph& _g) |
|
541 | 541 |
: Parent(_g) {} |
542 | 542 |
ArcMap(const Graph& _g, const _Value& _v) |
543 | 543 |
: Parent(_g, _v) {} |
544 | 544 |
|
545 | 545 |
ArcMap& operator=(const ArcMap& cmap) { |
546 | 546 |
return operator=<ArcMap>(cmap); |
547 | 547 |
} |
548 | 548 |
|
549 | 549 |
template <typename CMap> |
550 | 550 |
ArcMap& operator=(const CMap& cmap) { |
551 | 551 |
Parent::operator=(cmap); |
552 | 552 |
return *this; |
553 | 553 |
} |
554 | 554 |
|
555 | 555 |
}; |
556 | 556 |
|
557 | 557 |
|
558 | 558 |
template <typename _Value> |
559 | 559 |
class EdgeMap |
560 | 560 |
: public MapExtender<DefaultMap<Graph, Edge, _Value> > { |
561 | 561 |
typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent; |
562 | 562 |
|
563 | 563 |
public: |
564 |
EdgeMap(const Graph& _g) |
|
564 |
explicit EdgeMap(const Graph& _g) |
|
565 | 565 |
: Parent(_g) {} |
566 | 566 |
|
567 | 567 |
EdgeMap(const Graph& _g, const _Value& _v) |
568 | 568 |
: Parent(_g, _v) {} |
569 | 569 |
|
570 | 570 |
EdgeMap& operator=(const EdgeMap& cmap) { |
571 | 571 |
return operator=<EdgeMap>(cmap); |
572 | 572 |
} |
573 | 573 |
|
574 | 574 |
template <typename CMap> |
575 | 575 |
EdgeMap& operator=(const CMap& cmap) { |
576 | 576 |
Parent::operator=(cmap); |
577 | 577 |
return *this; |
578 | 578 |
} |
579 | 579 |
|
580 | 580 |
}; |
581 | 581 |
|
582 | 582 |
|
583 | 583 |
// Alteration extension |
584 | 584 |
|
585 | 585 |
Edge addEdge(const Node& from, const Node& to) { |
586 | 586 |
Edge edge = Parent::addEdge(from, to); |
587 | 587 |
notifier(Edge()).add(edge); |
588 | 588 |
std::vector<Arc> arcs; |
589 | 589 |
arcs.push_back(Parent::direct(edge, true)); |
590 | 590 |
arcs.push_back(Parent::direct(edge, false)); |
591 | 591 |
notifier(Arc()).add(arcs); |
592 | 592 |
return edge; |
593 | 593 |
} |
594 | 594 |
|
595 | 595 |
void clear() { |
596 | 596 |
notifier(Arc()).clear(); |
597 | 597 |
notifier(Edge()).clear(); |
598 | 598 |
Parent::clear(); |
599 | 599 |
} |
600 | 600 |
|
601 | 601 |
void erase(const Edge& edge) { |
602 | 602 |
std::vector<Arc> arcs; |
603 | 603 |
arcs.push_back(Parent::direct(edge, true)); |
604 | 604 |
arcs.push_back(Parent::direct(edge, false)); |
605 | 605 |
notifier(Arc()).erase(arcs); |
606 | 606 |
notifier(Edge()).erase(edge); |
607 | 607 |
Parent::erase(edge); |
608 | 608 |
} |
609 | 609 |
|
610 | 610 |
|
611 | 611 |
EdgeSetExtender() { |
612 | 612 |
arc_notifier.setContainer(*this); |
... | ... |
@@ -11,101 +11,101 @@ |
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_GRAPH_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_GRAPH_EXTENDER_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
|
24 | 24 |
#include <lemon/bits/map_extender.h> |
25 | 25 |
#include <lemon/bits/default_map.h> |
26 | 26 |
|
27 | 27 |
#include <lemon/concept_check.h> |
28 | 28 |
#include <lemon/concepts/maps.h> |
29 | 29 |
|
30 | 30 |
//\ingroup graphbits |
31 | 31 |
//\file |
32 | 32 |
//\brief Extenders for the graph types |
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
// \ingroup graphbits |
36 | 36 |
// |
37 | 37 |
// \brief Extender for the digraph implementations |
38 | 38 |
template <typename Base> |
39 | 39 |
class DigraphExtender : public Base { |
40 | 40 |
typedef Base Parent; |
41 | 41 |
|
42 | 42 |
public: |
43 | 43 |
|
44 | 44 |
typedef DigraphExtender Digraph; |
45 | 45 |
|
46 | 46 |
// Base extensions |
47 | 47 |
|
48 | 48 |
typedef typename Parent::Node Node; |
49 | 49 |
typedef typename Parent::Arc Arc; |
50 | 50 |
|
51 | 51 |
int maxId(Node) const { |
52 | 52 |
return Parent::maxNodeId(); |
53 | 53 |
} |
54 | 54 |
|
55 | 55 |
int maxId(Arc) const { |
56 | 56 |
return Parent::maxArcId(); |
57 | 57 |
} |
58 | 58 |
|
59 |
Node fromId(int id, Node) |
|
59 |
static Node fromId(int id, Node) { |
|
60 | 60 |
return Parent::nodeFromId(id); |
61 | 61 |
} |
62 | 62 |
|
63 |
Arc fromId(int id, Arc) |
|
63 |
static Arc fromId(int id, Arc) { |
|
64 | 64 |
return Parent::arcFromId(id); |
65 | 65 |
} |
66 | 66 |
|
67 | 67 |
Node oppositeNode(const Node &node, const Arc &arc) const { |
68 | 68 |
if (node == Parent::source(arc)) |
69 | 69 |
return Parent::target(arc); |
70 | 70 |
else if(node == Parent::target(arc)) |
71 | 71 |
return Parent::source(arc); |
72 | 72 |
else |
73 | 73 |
return INVALID; |
74 | 74 |
} |
75 | 75 |
|
76 | 76 |
// Alterable extension |
77 | 77 |
|
78 | 78 |
typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier; |
79 | 79 |
typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier; |
80 | 80 |
|
81 | 81 |
|
82 | 82 |
protected: |
83 | 83 |
|
84 | 84 |
mutable NodeNotifier node_notifier; |
85 | 85 |
mutable ArcNotifier arc_notifier; |
86 | 86 |
|
87 | 87 |
public: |
88 | 88 |
|
89 | 89 |
NodeNotifier& notifier(Node) const { |
90 | 90 |
return node_notifier; |
91 | 91 |
} |
92 | 92 |
|
93 | 93 |
ArcNotifier& notifier(Arc) const { |
94 | 94 |
return arc_notifier; |
95 | 95 |
} |
96 | 96 |
|
97 | 97 |
class NodeIt : public Node { |
98 | 98 |
const Digraph* _digraph; |
99 | 99 |
public: |
100 | 100 |
|
101 | 101 |
NodeIt() {} |
102 | 102 |
|
103 | 103 |
NodeIt(Invalid i) : Node(i) { } |
104 | 104 |
|
105 | 105 |
explicit NodeIt(const Digraph& digraph) : _digraph(&digraph) { |
106 | 106 |
_digraph->first(static_cast<Node&>(*this)); |
107 | 107 |
} |
108 | 108 |
|
109 | 109 |
NodeIt(const Digraph& digraph, const Node& node) |
110 | 110 |
: Node(node), _digraph(&digraph) {} |
111 | 111 |
|
... | ... |
@@ -310,105 +310,105 @@ |
310 | 310 |
void erase(const Arc& arc) { |
311 | 311 |
notifier(Arc()).erase(arc); |
312 | 312 |
Parent::erase(arc); |
313 | 313 |
} |
314 | 314 |
|
315 | 315 |
DigraphExtender() { |
316 | 316 |
node_notifier.setContainer(*this); |
317 | 317 |
arc_notifier.setContainer(*this); |
318 | 318 |
} |
319 | 319 |
|
320 | 320 |
|
321 | 321 |
~DigraphExtender() { |
322 | 322 |
arc_notifier.clear(); |
323 | 323 |
node_notifier.clear(); |
324 | 324 |
} |
325 | 325 |
}; |
326 | 326 |
|
327 | 327 |
// \ingroup _graphbits |
328 | 328 |
// |
329 | 329 |
// \brief Extender for the Graphs |
330 | 330 |
template <typename Base> |
331 | 331 |
class GraphExtender : public Base { |
332 | 332 |
typedef Base Parent; |
333 | 333 |
|
334 | 334 |
public: |
335 | 335 |
|
336 | 336 |
typedef GraphExtender Graph; |
337 | 337 |
|
338 | 338 |
typedef True UndirectedTag; |
339 | 339 |
|
340 | 340 |
typedef typename Parent::Node Node; |
341 | 341 |
typedef typename Parent::Arc Arc; |
342 | 342 |
typedef typename Parent::Edge Edge; |
343 | 343 |
|
344 | 344 |
// Graph extension |
345 | 345 |
|
346 | 346 |
int maxId(Node) const { |
347 | 347 |
return Parent::maxNodeId(); |
348 | 348 |
} |
349 | 349 |
|
350 | 350 |
int maxId(Arc) const { |
351 | 351 |
return Parent::maxArcId(); |
352 | 352 |
} |
353 | 353 |
|
354 | 354 |
int maxId(Edge) const { |
355 | 355 |
return Parent::maxEdgeId(); |
356 | 356 |
} |
357 | 357 |
|
358 |
Node fromId(int id, Node) |
|
358 |
static Node fromId(int id, Node) { |
|
359 | 359 |
return Parent::nodeFromId(id); |
360 | 360 |
} |
361 | 361 |
|
362 |
Arc fromId(int id, Arc) |
|
362 |
static Arc fromId(int id, Arc) { |
|
363 | 363 |
return Parent::arcFromId(id); |
364 | 364 |
} |
365 | 365 |
|
366 |
Edge fromId(int id, Edge) |
|
366 |
static Edge fromId(int id, Edge) { |
|
367 | 367 |
return Parent::edgeFromId(id); |
368 | 368 |
} |
369 | 369 |
|
370 | 370 |
Node oppositeNode(const Node &n, const Edge &e) const { |
371 | 371 |
if( n == Parent::u(e)) |
372 | 372 |
return Parent::v(e); |
373 | 373 |
else if( n == Parent::v(e)) |
374 | 374 |
return Parent::u(e); |
375 | 375 |
else |
376 | 376 |
return INVALID; |
377 | 377 |
} |
378 | 378 |
|
379 | 379 |
Arc oppositeArc(const Arc &arc) const { |
380 | 380 |
return Parent::direct(arc, !Parent::direction(arc)); |
381 | 381 |
} |
382 | 382 |
|
383 | 383 |
using Parent::direct; |
384 | 384 |
Arc direct(const Edge &edge, const Node &node) const { |
385 | 385 |
return Parent::direct(edge, Parent::u(edge) == node); |
386 | 386 |
} |
387 | 387 |
|
388 | 388 |
// Alterable extension |
389 | 389 |
|
390 | 390 |
typedef AlterationNotifier<GraphExtender, Node> NodeNotifier; |
391 | 391 |
typedef AlterationNotifier<GraphExtender, Arc> ArcNotifier; |
392 | 392 |
typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier; |
393 | 393 |
|
394 | 394 |
|
395 | 395 |
protected: |
396 | 396 |
|
397 | 397 |
mutable NodeNotifier node_notifier; |
398 | 398 |
mutable ArcNotifier arc_notifier; |
399 | 399 |
mutable EdgeNotifier edge_notifier; |
400 | 400 |
|
401 | 401 |
public: |
402 | 402 |
|
403 | 403 |
NodeNotifier& notifier(Node) const { |
404 | 404 |
return node_notifier; |
405 | 405 |
} |
406 | 406 |
|
407 | 407 |
ArcNotifier& notifier(Arc) const { |
408 | 408 |
return arc_notifier; |
409 | 409 |
} |
410 | 410 |
|
411 | 411 |
EdgeNotifier& notifier(Edge) const { |
412 | 412 |
return edge_notifier; |
413 | 413 |
} |
414 | 414 |
|
... | ... |
@@ -559,145 +559,145 @@ |
559 | 559 |
// |
560 | 560 |
// Returns the base node (ie. the source in this case) of the iterator |
561 | 561 |
Node baseNode(const OutArcIt &arc) const { |
562 | 562 |
return Parent::source(static_cast<const Arc&>(arc)); |
563 | 563 |
} |
564 | 564 |
// \brief Running node of the iterator |
565 | 565 |
// |
566 | 566 |
// Returns the running node (ie. the target in this case) of the |
567 | 567 |
// iterator |
568 | 568 |
Node runningNode(const OutArcIt &arc) const { |
569 | 569 |
return Parent::target(static_cast<const Arc&>(arc)); |
570 | 570 |
} |
571 | 571 |
|
572 | 572 |
// \brief Base node of the iterator |
573 | 573 |
// |
574 | 574 |
// Returns the base node (ie. the target in this case) of the iterator |
575 | 575 |
Node baseNode(const InArcIt &arc) const { |
576 | 576 |
return Parent::target(static_cast<const Arc&>(arc)); |
577 | 577 |
} |
578 | 578 |
// \brief Running node of the iterator |
579 | 579 |
// |
580 | 580 |
// Returns the running node (ie. the source in this case) of the |
581 | 581 |
// iterator |
582 | 582 |
Node runningNode(const InArcIt &arc) const { |
583 | 583 |
return Parent::source(static_cast<const Arc&>(arc)); |
584 | 584 |
} |
585 | 585 |
|
586 | 586 |
// Base node of the iterator |
587 | 587 |
// |
588 | 588 |
// Returns the base node of the iterator |
589 | 589 |
Node baseNode(const IncEdgeIt &edge) const { |
590 | 590 |
return edge._direction ? u(edge) : v(edge); |
591 | 591 |
} |
592 | 592 |
// Running node of the iterator |
593 | 593 |
// |
594 | 594 |
// Returns the running node of the iterator |
595 | 595 |
Node runningNode(const IncEdgeIt &edge) const { |
596 | 596 |
return edge._direction ? v(edge) : u(edge); |
597 | 597 |
} |
598 | 598 |
|
599 | 599 |
// Mappable extension |
600 | 600 |
|
601 | 601 |
template <typename _Value> |
602 | 602 |
class NodeMap |
603 | 603 |
: public MapExtender<DefaultMap<Graph, Node, _Value> > { |
604 | 604 |
typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent; |
605 | 605 |
|
606 | 606 |
public: |
607 |
NodeMap(const Graph& graph) |
|
607 |
explicit NodeMap(const Graph& graph) |
|
608 | 608 |
: Parent(graph) {} |
609 | 609 |
NodeMap(const Graph& graph, const _Value& value) |
610 | 610 |
: Parent(graph, value) {} |
611 | 611 |
|
612 | 612 |
private: |
613 | 613 |
NodeMap& operator=(const NodeMap& cmap) { |
614 | 614 |
return operator=<NodeMap>(cmap); |
615 | 615 |
} |
616 | 616 |
|
617 | 617 |
template <typename CMap> |
618 | 618 |
NodeMap& operator=(const CMap& cmap) { |
619 | 619 |
Parent::operator=(cmap); |
620 | 620 |
return *this; |
621 | 621 |
} |
622 | 622 |
|
623 | 623 |
}; |
624 | 624 |
|
625 | 625 |
template <typename _Value> |
626 | 626 |
class ArcMap |
627 | 627 |
: public MapExtender<DefaultMap<Graph, Arc, _Value> > { |
628 | 628 |
typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent; |
629 | 629 |
|
630 | 630 |
public: |
631 |
ArcMap(const Graph& graph) |
|
631 |
explicit ArcMap(const Graph& graph) |
|
632 | 632 |
: Parent(graph) {} |
633 | 633 |
ArcMap(const Graph& graph, const _Value& value) |
634 | 634 |
: Parent(graph, value) {} |
635 | 635 |
|
636 | 636 |
private: |
637 | 637 |
ArcMap& operator=(const ArcMap& cmap) { |
638 | 638 |
return operator=<ArcMap>(cmap); |
639 | 639 |
} |
640 | 640 |
|
641 | 641 |
template <typename CMap> |
642 | 642 |
ArcMap& operator=(const CMap& cmap) { |
643 | 643 |
Parent::operator=(cmap); |
644 | 644 |
return *this; |
645 | 645 |
} |
646 | 646 |
}; |
647 | 647 |
|
648 | 648 |
|
649 | 649 |
template <typename _Value> |
650 | 650 |
class EdgeMap |
651 | 651 |
: public MapExtender<DefaultMap<Graph, Edge, _Value> > { |
652 | 652 |
typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent; |
653 | 653 |
|
654 | 654 |
public: |
655 |
EdgeMap(const Graph& graph) |
|
655 |
explicit EdgeMap(const Graph& graph) |
|
656 | 656 |
: Parent(graph) {} |
657 | 657 |
|
658 | 658 |
EdgeMap(const Graph& graph, const _Value& value) |
659 | 659 |
: Parent(graph, value) {} |
660 | 660 |
|
661 | 661 |
private: |
662 | 662 |
EdgeMap& operator=(const EdgeMap& cmap) { |
663 | 663 |
return operator=<EdgeMap>(cmap); |
664 | 664 |
} |
665 | 665 |
|
666 | 666 |
template <typename CMap> |
667 | 667 |
EdgeMap& operator=(const CMap& cmap) { |
668 | 668 |
Parent::operator=(cmap); |
669 | 669 |
return *this; |
670 | 670 |
} |
671 | 671 |
|
672 | 672 |
}; |
673 | 673 |
|
674 | 674 |
// Alteration extension |
675 | 675 |
|
676 | 676 |
Node addNode() { |
677 | 677 |
Node node = Parent::addNode(); |
678 | 678 |
notifier(Node()).add(node); |
679 | 679 |
return node; |
680 | 680 |
} |
681 | 681 |
|
682 | 682 |
Edge addEdge(const Node& from, const Node& to) { |
683 | 683 |
Edge edge = Parent::addEdge(from, to); |
684 | 684 |
notifier(Edge()).add(edge); |
685 | 685 |
std::vector<Arc> ev; |
686 | 686 |
ev.push_back(Parent::direct(edge, true)); |
687 | 687 |
ev.push_back(Parent::direct(edge, false)); |
688 | 688 |
notifier(Arc()).add(ev); |
689 | 689 |
return edge; |
690 | 690 |
} |
691 | 691 |
|
692 | 692 |
void clear() { |
693 | 693 |
notifier(Arc()).clear(); |
694 | 694 |
notifier(Edge()).clear(); |
695 | 695 |
notifier(Node()).clear(); |
696 | 696 |
Parent::clear(); |
697 | 697 |
} |
698 | 698 |
|
699 | 699 |
template <typename Graph, typename NodeRefMap, typename EdgeRefMap> |
700 | 700 |
void build(const Graph& graph, NodeRefMap& nodeRef, |
701 | 701 |
EdgeRefMap& edgeRef) { |
702 | 702 |
Parent::build(graph, nodeRef, edgeRef); |
703 | 703 |
notifier(Node()).build(); |
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_BUCKET_HEAP_H |
20 | 20 |
#define LEMON_BUCKET_HEAP_H |
21 | 21 |
|
22 |
///\ingroup |
|
22 |
///\ingroup heaps |
|
23 | 23 |
///\file |
24 |
///\brief Bucket |
|
24 |
///\brief Bucket heap implementation. |
|
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
#include <utility> |
28 | 28 |
#include <functional> |
29 | 29 |
|
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 | 32 |
namespace _bucket_heap_bits { |
33 | 33 |
|
34 | 34 |
template <bool MIN> |
35 | 35 |
struct DirectionTraits { |
36 | 36 |
static bool less(int left, int right) { |
37 | 37 |
return left < right; |
38 | 38 |
} |
39 | 39 |
static void increase(int& value) { |
40 | 40 |
++value; |
41 | 41 |
} |
42 | 42 |
}; |
43 | 43 |
|
44 | 44 |
template <> |
45 | 45 |
struct DirectionTraits<false> { |
46 | 46 |
static bool less(int left, int right) { |
47 | 47 |
return left > right; |
48 | 48 |
} |
49 | 49 |
static void increase(int& value) { |
50 | 50 |
--value; |
51 | 51 |
} |
52 | 52 |
}; |
53 | 53 |
|
54 | 54 |
} |
55 | 55 |
|
56 |
/// \ingroup |
|
56 |
/// \ingroup heaps |
|
57 | 57 |
/// |
58 |
/// \brief |
|
58 |
/// \brief Bucket heap data structure. |
|
59 | 59 |
/// |
60 |
/// This class implements the \e bucket \e heap data structure. A \e heap |
|
61 |
/// is a data structure for storing items with specified values called \e |
|
62 |
/// priorities in such a way that finding the item with minimum priority is |
|
63 |
/// efficient. The bucket heap is very simple implementation, it can store |
|
64 |
/// only integer priorities and it stores for each priority in the |
|
65 |
/// \f$ [0..C) \f$ range a list of items. So it should be used only when |
|
66 |
/// the |
|
60 |
/// This class implements the \e bucket \e heap data structure. |
|
61 |
/// It practically conforms to the \ref concepts::Heap "heap concept", |
|
62 |
/// but it has some limitations. |
|
67 | 63 |
/// |
68 |
/// \param IM A read and write Item int map, used internally |
|
69 |
/// to handle the cross references. |
|
70 |
/// \param MIN If the given parameter is false then instead of the |
|
71 |
/// minimum value the maximum can be retrivied with the top() and |
|
72 |
/// |
|
64 |
/// The bucket heap is a very simple structure. It can store only |
|
65 |
/// \c int priorities and it maintains a list of items for each priority |
|
66 |
/// in the range <tt>[0..C)</tt>. So it should only be used when the |
|
67 |
/// priorities are small. It is not intended to use as a Dijkstra heap. |
|
68 |
/// |
|
69 |
/// \tparam IM A read-writable item map with \c int values, used |
|
70 |
/// internally to handle the cross references. |
|
71 |
/// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap. |
|
72 |
/// The default is \e min-heap. If this parameter is set to \c false, |
|
73 |
/// then the comparison is reversed, so the top(), prio() and pop() |
|
74 |
/// functions deal with the item having maximum priority instead of the |
|
75 |
/// minimum. |
|
76 |
/// |
|
77 |
/// \sa SimpleBucketHeap |
|
73 | 78 |
template <typename IM, bool MIN = true> |
74 | 79 |
class BucketHeap { |
75 | 80 |
|
76 | 81 |
public: |
77 |
/// \e |
|
78 |
typedef typename IM::Key Item; |
|
79 |
|
|
82 |
|
|
83 |
/// Type of the item-int map. |
|
84 |
typedef IM ItemIntMap; |
|
85 |
/// Type of the priorities. |
|
80 | 86 |
typedef int Prio; |
81 |
/// \e |
|
82 |
typedef std::pair<Item, Prio> Pair; |
|
83 |
/// \e |
|
84 |
typedef IM ItemIntMap; |
|
87 |
/// Type of the items stored in the heap. |
|
88 |
typedef typename ItemIntMap::Key Item; |
|
89 |
/// Type of the item-priority pairs. |
|
90 |
typedef std::pair<Item,Prio> Pair; |
|
85 | 91 |
|
86 | 92 |
private: |
87 | 93 |
|
88 | 94 |
typedef _bucket_heap_bits::DirectionTraits<MIN> Direction; |
89 | 95 |
|
90 | 96 |
public: |
91 | 97 |
|
92 |
/// \brief Type to represent the |
|
98 |
/// \brief Type to represent the states of the items. |
|
93 | 99 |
/// |
94 |
/// Each Item element have a state associated to it. It may be "in heap", |
|
95 |
/// "pre heap" or "post heap". The latter two are indifferent from the |
|
100 |
/// Each item has a state associated to it. It can be "in heap", |
|
101 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the |
|
96 | 102 |
/// heap's point of view, but may be useful to the user. |
97 | 103 |
/// |
98 | 104 |
/// The item-int map must be initialized in such way that it assigns |
99 | 105 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
100 | 106 |
enum State { |
101 | 107 |
IN_HEAP = 0, ///< = 0. |
102 | 108 |
PRE_HEAP = -1, ///< = -1. |
103 | 109 |
POST_HEAP = -2 ///< = -2. |
104 | 110 |
}; |
105 | 111 |
|
106 | 112 |
public: |
107 |
|
|
113 |
|
|
114 |
/// \brief Constructor. |
|
108 | 115 |
/// |
109 |
/// The constructor. |
|
110 |
/// \param map should be given to the constructor, since it is used |
|
111 |
/// internally to handle the cross references. The value of the map |
|
112 |
/// should be PRE_HEAP (-1) for each element. |
|
116 |
/// Constructor. |
|
117 |
/// \param map A map that assigns \c int values to the items. |
|
118 |
/// It is used internally to handle the cross references. |
|
119 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
|
113 | 120 |
explicit BucketHeap(ItemIntMap &map) : _iim(map), _minimum(0) {} |
114 | 121 |
|
115 |
/// The number of items stored in the heap. |
|
122 |
/// \brief The number of items stored in the heap. |
|
116 | 123 |
/// |
117 |
/// |
|
124 |
/// This function returns the number of items stored in the heap. |
|
118 | 125 |
int size() const { return _data.size(); } |
119 | 126 |
|
120 |
/// \brief |
|
127 |
/// \brief Check if the heap is empty. |
|
121 | 128 |
/// |
122 |
/// |
|
129 |
/// This function returns \c true if the heap is empty. |
|
123 | 130 |
bool empty() const { return _data.empty(); } |
124 | 131 |
|
125 |
/// \brief Make |
|
132 |
/// \brief Make the heap empty. |
|
126 | 133 |
/// |
127 |
/// Make empty this heap. It does not change the cross reference |
|
128 |
/// map. If you want to reuse a heap what is not surely empty you |
|
129 |
/// should first clear the heap and after that you should set the |
|
130 |
/// cross reference map for each item to \c PRE_HEAP. |
|
134 |
/// This functon makes the heap empty. |
|
135 |
/// It does not change the cross reference map. If you want to reuse |
|
136 |
/// a heap that is not surely empty, you should first clear it and |
|
137 |
/// then you should set the cross reference map to \c PRE_HEAP |
|
138 |
/// for each item. |
|
131 | 139 |
void clear() { |
132 | 140 |
_data.clear(); _first.clear(); _minimum = 0; |
133 | 141 |
} |
134 | 142 |
|
135 | 143 |
private: |
136 | 144 |
|
137 |
void |
|
145 |
void relocateLast(int idx) { |
|
138 | 146 |
if (idx + 1 < int(_data.size())) { |
139 | 147 |
_data[idx] = _data.back(); |
140 | 148 |
if (_data[idx].prev != -1) { |
141 | 149 |
_data[_data[idx].prev].next = idx; |
142 | 150 |
} else { |
143 | 151 |
_first[_data[idx].value] = idx; |
144 | 152 |
} |
145 | 153 |
if (_data[idx].next != -1) { |
146 | 154 |
_data[_data[idx].next].prev = idx; |
147 | 155 |
} |
148 | 156 |
_iim[_data[idx].item] = idx; |
149 | 157 |
} |
150 | 158 |
_data.pop_back(); |
151 | 159 |
} |
152 | 160 |
|
153 | 161 |
void unlace(int idx) { |
154 | 162 |
if (_data[idx].prev != -1) { |
155 | 163 |
_data[_data[idx].prev].next = _data[idx].next; |
156 | 164 |
} else { |
157 | 165 |
_first[_data[idx].value] = _data[idx].next; |
158 | 166 |
} |
159 | 167 |
if (_data[idx].next != -1) { |
160 | 168 |
_data[_data[idx].next].prev = _data[idx].prev; |
161 | 169 |
} |
162 | 170 |
} |
163 | 171 |
|
164 | 172 |
void lace(int idx) { |
165 | 173 |
if (int(_first.size()) <= _data[idx].value) { |
166 | 174 |
_first.resize(_data[idx].value + 1, -1); |
167 | 175 |
} |
168 | 176 |
_data[idx].next = _first[_data[idx].value]; |
169 | 177 |
if (_data[idx].next != -1) { |
170 | 178 |
_data[_data[idx].next].prev = idx; |
171 | 179 |
} |
172 | 180 |
_first[_data[idx].value] = idx; |
173 | 181 |
_data[idx].prev = -1; |
174 | 182 |
} |
175 | 183 |
|
176 | 184 |
public: |
185 |
|
|
177 | 186 |
/// \brief Insert a pair of item and priority into the heap. |
178 | 187 |
/// |
179 |
/// |
|
188 |
/// This function inserts \c p.first to the heap with priority |
|
189 |
/// \c p.second. |
|
180 | 190 |
/// \param p The pair to insert. |
191 |
/// \pre \c p.first must not be stored in the heap. |
|
181 | 192 |
void push(const Pair& p) { |
182 | 193 |
push(p.first, p.second); |
183 | 194 |
} |
184 | 195 |
|
185 | 196 |
/// \brief Insert an item into the heap with the given priority. |
186 | 197 |
/// |
187 |
/// |
|
198 |
/// This function inserts the given item into the heap with the |
|
199 |
/// given priority. |
|
188 | 200 |
/// \param i The item to insert. |
189 | 201 |
/// \param p The priority of the item. |
202 |
/// \pre \e i must not be stored in the heap. |
|
190 | 203 |
void push(const Item &i, const Prio &p) { |
191 | 204 |
int idx = _data.size(); |
192 | 205 |
_iim[i] = idx; |
193 | 206 |
_data.push_back(BucketItem(i, p)); |
194 | 207 |
lace(idx); |
195 | 208 |
if (Direction::less(p, _minimum)) { |
196 | 209 |
_minimum = p; |
197 | 210 |
} |
198 | 211 |
} |
199 | 212 |
|
200 |
/// \brief |
|
213 |
/// \brief Return the item having minimum priority. |
|
201 | 214 |
/// |
202 |
/// This method returns the item with minimum priority. |
|
203 |
/// \pre The heap must be nonempty. |
|
215 |
/// This function returns the item having minimum priority. |
|
216 |
/// \pre The heap must be non-empty. |
|
204 | 217 |
Item top() const { |
205 | 218 |
while (_first[_minimum] == -1) { |
206 | 219 |
Direction::increase(_minimum); |
207 | 220 |
} |
208 | 221 |
return _data[_first[_minimum]].item; |
209 | 222 |
} |
210 | 223 |
|
211 |
/// \brief |
|
224 |
/// \brief The minimum priority. |
|
212 | 225 |
/// |
213 |
/// It returns the minimum priority. |
|
214 |
/// \pre The heap must be nonempty. |
|
226 |
/// This function returns the minimum priority. |
|
227 |
/// \pre The heap must be non-empty. |
|
215 | 228 |
Prio prio() const { |
216 | 229 |
while (_first[_minimum] == -1) { |
217 | 230 |
Direction::increase(_minimum); |
218 | 231 |
} |
219 | 232 |
return _minimum; |
220 | 233 |
} |
221 | 234 |
|
222 |
/// \brief |
|
235 |
/// \brief Remove the item having minimum priority. |
|
223 | 236 |
/// |
224 |
/// This |
|
237 |
/// This function removes the item having minimum priority. |
|
225 | 238 |
/// \pre The heap must be non-empty. |
226 | 239 |
void pop() { |
227 | 240 |
while (_first[_minimum] == -1) { |
228 | 241 |
Direction::increase(_minimum); |
229 | 242 |
} |
230 | 243 |
int idx = _first[_minimum]; |
231 | 244 |
_iim[_data[idx].item] = -2; |
232 | 245 |
unlace(idx); |
233 |
|
|
246 |
relocateLast(idx); |
|
234 | 247 |
} |
235 | 248 |
|
236 |
/// \brief |
|
249 |
/// \brief Remove the given item from the heap. |
|
237 | 250 |
/// |
238 |
/// This method deletes item \c i from the heap, if \c i was |
|
239 |
/// already stored in the heap. |
|
240 |
/// |
|
251 |
/// This function removes the given item from the heap if it is |
|
252 |
/// already stored. |
|
253 |
/// \param i The item to delete. |
|
254 |
/// \pre \e i must be in the heap. |
|
241 | 255 |
void erase(const Item &i) { |
242 | 256 |
int idx = _iim[i]; |
243 | 257 |
_iim[_data[idx].item] = -2; |
244 | 258 |
unlace(idx); |
245 |
|
|
259 |
relocateLast(idx); |
|
246 | 260 |
} |
247 | 261 |
|
248 |
|
|
249 |
/// \brief Returns the priority of \c i. |
|
262 |
/// \brief The priority of the given item. |
|
250 | 263 |
/// |
251 |
/// This function returns the priority of item \c i. |
|
252 |
/// \pre \c i must be in the heap. |
|
264 |
/// This function returns the priority of the given item. |
|
253 | 265 |
/// \param i The item. |
266 |
/// \pre \e i must be in the heap. |
|
254 | 267 |
Prio operator[](const Item &i) const { |
255 | 268 |
int idx = _iim[i]; |
256 | 269 |
return _data[idx].value; |
257 | 270 |
} |
258 | 271 |
|
259 |
/// \brief \c i gets to the heap with priority \c p independently |
|
260 |
/// if \c i was already there. |
|
272 |
/// \brief Set the priority of an item or insert it, if it is |
|
273 |
/// not stored in the heap. |
|
261 | 274 |
/// |
262 |
/// This method calls \ref push(\c i, \c p) if \c i is not stored |
|
263 |
/// in the heap and sets the priority of \c i to \c p otherwise. |
|
275 |
/// This method sets the priority of the given item if it is |
|
276 |
/// already stored in the heap. Otherwise it inserts the given |
|
277 |
/// item into the heap with the given priority. |
|
264 | 278 |
/// \param i The item. |
265 | 279 |
/// \param p The priority. |
266 | 280 |
void set(const Item &i, const Prio &p) { |
267 | 281 |
int idx = _iim[i]; |
268 | 282 |
if (idx < 0) { |
269 | 283 |
push(i, p); |
270 | 284 |
} else if (Direction::less(p, _data[idx].value)) { |
271 | 285 |
decrease(i, p); |
272 | 286 |
} else { |
273 | 287 |
increase(i, p); |
274 | 288 |
} |
275 | 289 |
} |
276 | 290 |
|
277 |
/// \brief |
|
291 |
/// \brief Decrease the priority of an item to the given value. |
|
278 | 292 |
/// |
279 |
/// This method decreases the priority of item \c i to \c p. |
|
280 |
/// \pre \c i must be stored in the heap with priority at least \c |
|
281 |
/// |
|
293 |
/// This function decreases the priority of an item to the given value. |
|
282 | 294 |
/// \param i The item. |
283 | 295 |
/// \param p The priority. |
296 |
/// \pre \e i must be stored in the heap with priority at least \e p. |
|
284 | 297 |
void decrease(const Item &i, const Prio &p) { |
285 | 298 |
int idx = _iim[i]; |
286 | 299 |
unlace(idx); |
287 | 300 |
_data[idx].value = p; |
288 | 301 |
if (Direction::less(p, _minimum)) { |
289 | 302 |
_minimum = p; |
290 | 303 |
} |
291 | 304 |
lace(idx); |
292 | 305 |
} |
293 | 306 |
|
294 |
/// \brief |
|
307 |
/// \brief Increase the priority of an item to the given value. |
|
295 | 308 |
/// |
296 |
/// This method sets the priority of item \c i to \c p. |
|
297 |
/// \pre \c i must be stored in the heap with priority at most \c |
|
298 |
/// |
|
309 |
/// This function increases the priority of an item to the given value. |
|
299 | 310 |
/// \param i The item. |
300 | 311 |
/// \param p The priority. |
312 |
/// \pre \e i must be stored in the heap with priority at most \e p. |
|
301 | 313 |
void increase(const Item &i, const Prio &p) { |
302 | 314 |
int idx = _iim[i]; |
303 | 315 |
unlace(idx); |
304 | 316 |
_data[idx].value = p; |
305 | 317 |
lace(idx); |
306 | 318 |
} |
307 | 319 |
|
308 |
/// \brief Returns if \c item is in, has already been in, or has |
|
309 |
/// never been in the heap. |
|
320 |
/// \brief Return the state of an item. |
|
310 | 321 |
/// |
311 |
/// This method returns PRE_HEAP if \c item has never been in the |
|
312 |
/// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP |
|
313 |
/// otherwise. In the latter case it is possible that \c item will |
|
314 |
/// get back to the heap again. |
|
322 |
/// This method returns \c PRE_HEAP if the given item has never |
|
323 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
|
324 |
/// and \c POST_HEAP otherwise. |
|
325 |
/// In the latter case it is possible that the item will get back |
|
326 |
/// to the heap again. |
|
315 | 327 |
/// \param i The item. |
316 | 328 |
State state(const Item &i) const { |
317 | 329 |
int idx = _iim[i]; |
318 | 330 |
if (idx >= 0) idx = 0; |
319 | 331 |
return State(idx); |
320 | 332 |
} |
321 | 333 |
|
322 |
/// \brief |
|
334 |
/// \brief Set the state of an item in the heap. |
|
323 | 335 |
/// |
324 |
/// Sets the state of the \c item in the heap. It can be used to |
|
325 |
/// manually clear the heap when it is important to achive the |
|
326 |
/// |
|
336 |
/// This function sets the state of the given item in the heap. |
|
337 |
/// It can be used to manually clear the heap when it is important |
|
338 |
/// to achive better time complexity. |
|
327 | 339 |
/// \param i The item. |
328 | 340 |
/// \param st The state. It should not be \c IN_HEAP. |
329 | 341 |
void state(const Item& i, State st) { |
330 | 342 |
switch (st) { |
331 | 343 |
case POST_HEAP: |
332 | 344 |
case PRE_HEAP: |
333 | 345 |
if (state(i) == IN_HEAP) { |
334 | 346 |
erase(i); |
335 | 347 |
} |
336 | 348 |
_iim[i] = st; |
337 | 349 |
break; |
338 | 350 |
case IN_HEAP: |
339 | 351 |
break; |
340 | 352 |
} |
341 | 353 |
} |
342 | 354 |
|
343 | 355 |
private: |
344 | 356 |
|
345 | 357 |
struct BucketItem { |
346 | 358 |
BucketItem(const Item& _item, int _value) |
347 | 359 |
: item(_item), value(_value) {} |
348 | 360 |
|
349 | 361 |
Item item; |
350 | 362 |
int value; |
351 | 363 |
|
352 | 364 |
int prev, next; |
353 | 365 |
}; |
354 | 366 |
|
355 | 367 |
ItemIntMap& _iim; |
356 | 368 |
std::vector<int> _first; |
357 | 369 |
std::vector<BucketItem> _data; |
358 | 370 |
mutable int _minimum; |
359 | 371 |
|
360 | 372 |
}; // class BucketHeap |
361 | 373 |
|
362 |
/// \ingroup |
|
374 |
/// \ingroup heaps |
|
363 | 375 |
/// |
364 |
/// \brief |
|
376 |
/// \brief Simplified bucket heap data structure. |
|
365 | 377 |
/// |
366 | 378 |
/// This class implements a simplified \e bucket \e heap data |
367 |
/// structure. It does not provide some functionality but it faster |
|
368 |
/// and simplier data structure than the BucketHeap. The main |
|
369 |
/// difference is that the BucketHeap stores for every key a double |
|
370 |
/// linked list while this class stores just simple lists. In the |
|
371 |
/// other way it does not support erasing each elements just the |
|
372 |
/// minimal and it does not supports key increasing, decreasing. |
|
379 |
/// structure. It does not provide some functionality, but it is |
|
380 |
/// faster and simpler than BucketHeap. The main difference is |
|
381 |
/// that BucketHeap stores a doubly-linked list for each key while |
|
382 |
/// this class stores only simply-linked lists. It supports erasing |
|
383 |
/// only for the item having minimum priority and it does not support |
|
384 |
/// key increasing and decreasing. |
|
373 | 385 |
/// |
374 |
/// \param IM A read and write Item int map, used internally |
|
375 |
/// to handle the cross references. |
|
376 |
/// \param MIN If the given parameter is false then instead of the |
|
377 |
/// minimum value the maximum can be retrivied with the top() and |
|
378 |
/// |
|
386 |
/// Note that this implementation does not conform to the |
|
387 |
/// \ref concepts::Heap "heap concept" due to the lack of some |
|
388 |
/// functionality. |
|
389 |
/// |
|
390 |
/// \tparam IM A read-writable item map with \c int values, used |
|
391 |
/// internally to handle the cross references. |
|
392 |
/// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap. |
|
393 |
/// The default is \e min-heap. If this parameter is set to \c false, |
|
394 |
/// then the comparison is reversed, so the top(), prio() and pop() |
|
395 |
/// functions deal with the item having maximum priority instead of the |
|
396 |
/// minimum. |
|
379 | 397 |
/// |
380 | 398 |
/// \sa BucketHeap |
381 | 399 |
template <typename IM, bool MIN = true > |
382 | 400 |
class SimpleBucketHeap { |
383 | 401 |
|
384 | 402 |
public: |
385 |
|
|
403 |
|
|
404 |
/// Type of the item-int map. |
|
405 |
typedef IM ItemIntMap; |
|
406 |
/// Type of the priorities. |
|
386 | 407 |
typedef int Prio; |
387 |
typedef std::pair<Item, Prio> Pair; |
|
388 |
typedef IM ItemIntMap; |
|
408 |
/// Type of the items stored in the heap. |
|
409 |
typedef typename ItemIntMap::Key Item; |
|
410 |
/// Type of the item-priority pairs. |
|
411 |
typedef std::pair<Item,Prio> Pair; |
|
389 | 412 |
|
390 | 413 |
private: |
391 | 414 |
|
392 | 415 |
typedef _bucket_heap_bits::DirectionTraits<MIN> Direction; |
393 | 416 |
|
394 | 417 |
public: |
395 | 418 |
|
396 |
/// \brief Type to represent the |
|
419 |
/// \brief Type to represent the states of the items. |
|
397 | 420 |
/// |
398 |
/// Each Item element have a state associated to it. It may be "in heap", |
|
399 |
/// "pre heap" or "post heap". The latter two are indifferent from the |
|
421 |
/// Each item has a state associated to it. It can be "in heap", |
|
422 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the |
|
400 | 423 |
/// heap's point of view, but may be useful to the user. |
401 | 424 |
/// |
402 | 425 |
/// The item-int map must be initialized in such way that it assigns |
403 | 426 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
404 | 427 |
enum State { |
405 | 428 |
IN_HEAP = 0, ///< = 0. |
406 | 429 |
PRE_HEAP = -1, ///< = -1. |
407 | 430 |
POST_HEAP = -2 ///< = -2. |
408 | 431 |
}; |
409 | 432 |
|
410 | 433 |
public: |
411 | 434 |
|
412 |
/// \brief |
|
435 |
/// \brief Constructor. |
|
413 | 436 |
/// |
414 |
/// The constructor. |
|
415 |
/// \param map should be given to the constructor, since it is used |
|
416 |
/// internally to handle the cross references. The value of the map |
|
417 |
/// should be PRE_HEAP (-1) for each element. |
|
437 |
/// Constructor. |
|
438 |
/// \param map A map that assigns \c int values to the items. |
|
439 |
/// It is used internally to handle the cross references. |
|
440 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
|
418 | 441 |
explicit SimpleBucketHeap(ItemIntMap &map) |
419 | 442 |
: _iim(map), _free(-1), _num(0), _minimum(0) {} |
420 | 443 |
|
421 |
/// \brief |
|
444 |
/// \brief The number of items stored in the heap. |
|
422 | 445 |
/// |
423 |
/// |
|
446 |
/// This function returns the number of items stored in the heap. |
|
424 | 447 |
int size() const { return _num; } |
425 | 448 |
|
426 |
/// \brief |
|
449 |
/// \brief Check if the heap is empty. |
|
427 | 450 |
/// |
428 |
/// |
|
451 |
/// This function returns \c true if the heap is empty. |
|
429 | 452 |
bool empty() const { return _num == 0; } |
430 | 453 |
|
431 |
/// \brief Make |
|
454 |
/// \brief Make the heap empty. |
|
432 | 455 |
/// |
433 |
/// Make empty this heap. It does not change the cross reference |
|
434 |
/// map. If you want to reuse a heap what is not surely empty you |
|
435 |
/// should first clear the heap and after that you should set the |
|
436 |
/// cross reference map for each item to \c PRE_HEAP. |
|
456 |
/// This functon makes the heap empty. |
|
457 |
/// It does not change the cross reference map. If you want to reuse |
|
458 |
/// a heap that is not surely empty, you should first clear it and |
|
459 |
/// then you should set the cross reference map to \c PRE_HEAP |
|
460 |
/// for each item. |
|
437 | 461 |
void clear() { |
438 | 462 |
_data.clear(); _first.clear(); _free = -1; _num = 0; _minimum = 0; |
439 | 463 |
} |
440 | 464 |
|
441 | 465 |
/// \brief Insert a pair of item and priority into the heap. |
442 | 466 |
/// |
443 |
/// |
|
467 |
/// This function inserts \c p.first to the heap with priority |
|
468 |
/// \c p.second. |
|
444 | 469 |
/// \param p The pair to insert. |
470 |
/// \pre \c p.first must not be stored in the heap. |
|
445 | 471 |
void push(const Pair& p) { |
446 | 472 |
push(p.first, p.second); |
447 | 473 |
} |
448 | 474 |
|
449 | 475 |
/// \brief Insert an item into the heap with the given priority. |
450 | 476 |
/// |
451 |
/// |
|
477 |
/// This function inserts the given item into the heap with the |
|
478 |
/// given priority. |
|
452 | 479 |
/// \param i The item to insert. |
453 | 480 |
/// \param p The priority of the item. |
481 |
/// \pre \e i must not be stored in the heap. |
|
454 | 482 |
void push(const Item &i, const Prio &p) { |
455 | 483 |
int idx; |
456 | 484 |
if (_free == -1) { |
457 | 485 |
idx = _data.size(); |
458 | 486 |
_data.push_back(BucketItem(i)); |
459 | 487 |
} else { |
460 | 488 |
idx = _free; |
461 | 489 |
_free = _data[idx].next; |
462 | 490 |
_data[idx].item = i; |
463 | 491 |
} |
464 | 492 |
_iim[i] = idx; |
465 | 493 |
if (p >= int(_first.size())) _first.resize(p + 1, -1); |
466 | 494 |
_data[idx].next = _first[p]; |
467 | 495 |
_first[p] = idx; |
468 | 496 |
if (Direction::less(p, _minimum)) { |
469 | 497 |
_minimum = p; |
470 | 498 |
} |
471 | 499 |
++_num; |
472 | 500 |
} |
473 | 501 |
|
474 |
/// \brief |
|
502 |
/// \brief Return the item having minimum priority. |
|
475 | 503 |
/// |
476 |
/// This method returns the item with minimum priority. |
|
477 |
/// \pre The heap must be nonempty. |
|
504 |
/// This function returns the item having minimum priority. |
|
505 |
/// \pre The heap must be non-empty. |
|
478 | 506 |
Item top() const { |
479 | 507 |
while (_first[_minimum] == -1) { |
480 | 508 |
Direction::increase(_minimum); |
481 | 509 |
} |
482 | 510 |
return _data[_first[_minimum]].item; |
483 | 511 |
} |
484 | 512 |
|
485 |
/// \brief |
|
513 |
/// \brief The minimum priority. |
|
486 | 514 |
/// |
487 |
/// It returns the minimum priority. |
|
488 |
/// \pre The heap must be nonempty. |
|
515 |
/// This function returns the minimum priority. |
|
516 |
/// \pre The heap must be non-empty. |
|
489 | 517 |
Prio prio() const { |
490 | 518 |
while (_first[_minimum] == -1) { |
491 | 519 |
Direction::increase(_minimum); |
492 | 520 |
} |
493 | 521 |
return _minimum; |
494 | 522 |
} |
495 | 523 |
|
496 |
/// \brief |
|
524 |
/// \brief Remove the item having minimum priority. |
|
497 | 525 |
/// |
498 |
/// This |
|
526 |
/// This function removes the item having minimum priority. |
|
499 | 527 |
/// \pre The heap must be non-empty. |
500 | 528 |
void pop() { |
501 | 529 |
while (_first[_minimum] == -1) { |
502 | 530 |
Direction::increase(_minimum); |
503 | 531 |
} |
504 | 532 |
int idx = _first[_minimum]; |
505 | 533 |
_iim[_data[idx].item] = -2; |
506 | 534 |
_first[_minimum] = _data[idx].next; |
507 | 535 |
_data[idx].next = _free; |
508 | 536 |
_free = idx; |
509 | 537 |
--_num; |
510 | 538 |
} |
511 | 539 |
|
512 |
/// \brief |
|
540 |
/// \brief The priority of the given item. |
|
513 | 541 |
/// |
514 |
/// This function returns the priority of item \c i. |
|
515 |
/// \warning This operator is not a constant time function |
|
516 |
/// because it scans the whole data structure to find the proper |
|
517 |
/// value. |
|
518 |
/// |
|
542 |
/// This function returns the priority of the given item. |
|
519 | 543 |
/// \param i The item. |
544 |
/// \pre \e i must be in the heap. |
|
545 |
/// \warning This operator is not a constant time function because |
|
546 |
/// it scans the whole data structure to find the proper value. |
|
520 | 547 |
Prio operator[](const Item &i) const { |
521 |
for (int k = 0; k < _first.size(); ++k) { |
|
548 |
for (int k = 0; k < int(_first.size()); ++k) { |
|
522 | 549 |
int idx = _first[k]; |
523 | 550 |
while (idx != -1) { |
524 | 551 |
if (_data[idx].item == i) { |
525 | 552 |
return k; |
526 | 553 |
} |
527 | 554 |
idx = _data[idx].next; |
528 | 555 |
} |
529 | 556 |
} |
530 | 557 |
return -1; |
531 | 558 |
} |
532 | 559 |
|
533 |
/// \brief Returns if \c item is in, has already been in, or has |
|
534 |
/// never been in the heap. |
|
560 |
/// \brief Return the state of an item. |
|
535 | 561 |
/// |
536 |
/// This method returns PRE_HEAP if \c item has never been in the |
|
537 |
/// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP |
|
538 |
/// otherwise. In the latter case it is possible that \c item will |
|
539 |
/// get back to the heap again. |
|
562 |
/// This method returns \c PRE_HEAP if the given item has never |
|
563 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
|
564 |
/// and \c POST_HEAP otherwise. |
|
565 |
/// In the latter case it is possible that the item will get back |
|
566 |
/// to the heap again. |
|
540 | 567 |
/// \param i The item. |
541 | 568 |
State state(const Item &i) const { |
542 | 569 |
int idx = _iim[i]; |
543 | 570 |
if (idx >= 0) idx = 0; |
544 | 571 |
return State(idx); |
545 | 572 |
} |
546 | 573 |
|
547 | 574 |
private: |
548 | 575 |
|
549 | 576 |
struct BucketItem { |
550 | 577 |
BucketItem(const Item& _item) |
551 | 578 |
: item(_item) {} |
552 | 579 |
|
553 | 580 |
Item item; |
554 | 581 |
int next; |
555 | 582 |
}; |
556 | 583 |
|
557 | 584 |
ItemIntMap& _iim; |
558 | 585 |
std::vector<int> _first; |
559 | 586 |
std::vector<BucketItem> _data; |
560 | 587 |
int _free, _num; |
561 | 588 |
mutable int _minimum; |
562 | 589 |
|
563 | 590 |
}; // class SimpleBucketHeap |
564 | 591 |
|
565 | 592 |
} |
566 | 593 |
|
567 | 594 |
#endif |
... | ... |
@@ -49,96 +49,108 @@ |
49 | 49 |
#include "coin/CbcHeuristic.hpp" |
50 | 50 |
|
51 | 51 |
namespace lemon { |
52 | 52 |
|
53 | 53 |
CbcMip::CbcMip() { |
54 | 54 |
_prob = new CoinModel(); |
55 | 55 |
_prob->setProblemName("LEMON"); |
56 | 56 |
_osi_solver = 0; |
57 | 57 |
_cbc_model = 0; |
58 | 58 |
messageLevel(MESSAGE_NOTHING); |
59 | 59 |
} |
60 | 60 |
|
61 | 61 |
CbcMip::CbcMip(const CbcMip& other) { |
62 | 62 |
_prob = new CoinModel(*other._prob); |
63 | 63 |
_prob->setProblemName("LEMON"); |
64 | 64 |
_osi_solver = 0; |
65 | 65 |
_cbc_model = 0; |
66 | 66 |
messageLevel(MESSAGE_NOTHING); |
67 | 67 |
} |
68 | 68 |
|
69 | 69 |
CbcMip::~CbcMip() { |
70 | 70 |
delete _prob; |
71 | 71 |
if (_osi_solver) delete _osi_solver; |
72 | 72 |
if (_cbc_model) delete _cbc_model; |
73 | 73 |
} |
74 | 74 |
|
75 | 75 |
const char* CbcMip::_solverName() const { return "CbcMip"; } |
76 | 76 |
|
77 | 77 |
int CbcMip::_addCol() { |
78 | 78 |
_prob->addColumn(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX, 0.0, 0, false); |
79 | 79 |
return _prob->numberColumns() - 1; |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
CbcMip* CbcMip::newSolver() const { |
83 | 83 |
CbcMip* newlp = new CbcMip; |
84 | 84 |
return newlp; |
85 | 85 |
} |
86 | 86 |
|
87 | 87 |
CbcMip* CbcMip::cloneSolver() const { |
88 | 88 |
CbcMip* copylp = new CbcMip(*this); |
89 | 89 |
return copylp; |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
int CbcMip::_addRow() { |
93 | 93 |
_prob->addRow(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX); |
94 | 94 |
return _prob->numberRows() - 1; |
95 | 95 |
} |
96 | 96 |
|
97 |
int CbcMip::_addRow(Value l, ExprIterator b, ExprIterator e, Value u) { |
|
98 |
std::vector<int> indexes; |
|
99 |
std::vector<Value> values; |
|
100 |
|
|
101 |
for(ExprIterator it = b; it != e; ++it) { |
|
102 |
indexes.push_back(it->first); |
|
103 |
values.push_back(it->second); |
|
104 |
} |
|
105 |
|
|
106 |
_prob->addRow(values.size(), &indexes.front(), &values.front(), l, u); |
|
107 |
return _prob->numberRows() - 1; |
|
108 |
} |
|
97 | 109 |
|
98 | 110 |
void CbcMip::_eraseCol(int i) { |
99 | 111 |
_prob->deleteColumn(i); |
100 | 112 |
} |
101 | 113 |
|
102 | 114 |
void CbcMip::_eraseRow(int i) { |
103 | 115 |
_prob->deleteRow(i); |
104 | 116 |
} |
105 | 117 |
|
106 | 118 |
void CbcMip::_eraseColId(int i) { |
107 | 119 |
cols.eraseIndex(i); |
108 | 120 |
} |
109 | 121 |
|
110 | 122 |
void CbcMip::_eraseRowId(int i) { |
111 | 123 |
rows.eraseIndex(i); |
112 | 124 |
} |
113 | 125 |
|
114 | 126 |
void CbcMip::_getColName(int c, std::string& name) const { |
115 | 127 |
name = _prob->getColumnName(c); |
116 | 128 |
} |
117 | 129 |
|
118 | 130 |
void CbcMip::_setColName(int c, const std::string& name) { |
119 | 131 |
_prob->setColumnName(c, name.c_str()); |
120 | 132 |
} |
121 | 133 |
|
122 | 134 |
int CbcMip::_colByName(const std::string& name) const { |
123 | 135 |
return _prob->column(name.c_str()); |
124 | 136 |
} |
125 | 137 |
|
126 | 138 |
void CbcMip::_getRowName(int r, std::string& name) const { |
127 | 139 |
name = _prob->getRowName(r); |
128 | 140 |
} |
129 | 141 |
|
130 | 142 |
void CbcMip::_setRowName(int r, const std::string& name) { |
131 | 143 |
_prob->setRowName(r, name.c_str()); |
132 | 144 |
} |
133 | 145 |
|
134 | 146 |
int CbcMip::_rowByName(const std::string& name) const { |
135 | 147 |
return _prob->row(name.c_str()); |
136 | 148 |
} |
137 | 149 |
|
138 | 150 |
void CbcMip::_setRowCoeffs(int i, ExprIterator b, ExprIterator e) { |
139 | 151 |
for (ExprIterator it = b; it != e; ++it) { |
140 | 152 |
_prob->setElement(i, it->first, it->second); |
141 | 153 |
} |
142 | 154 |
} |
143 | 155 |
|
144 | 156 |
void CbcMip::_getRowCoeffs(int ix, InsertIterator b) const { |
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)