0
4
0
| ... | ... |
@@ -10,663 +10,697 @@ |
| 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 |
@defgroup datas Data Structures |
| 23 | 23 |
This group contains the several data structures implemented in LEMON. |
| 24 | 24 |
*/ |
| 25 | 25 |
|
| 26 | 26 |
/** |
| 27 | 27 |
@defgroup graphs Graph Structures |
| 28 | 28 |
@ingroup datas |
| 29 | 29 |
\brief Graph structures implemented in LEMON. |
| 30 | 30 |
|
| 31 | 31 |
The implementation of combinatorial algorithms heavily relies on |
| 32 | 32 |
efficient graph implementations. LEMON offers data structures which are |
| 33 | 33 |
planned to be easily used in an experimental phase of implementation studies, |
| 34 | 34 |
and thereafter the program code can be made efficient by small modifications. |
| 35 | 35 |
|
| 36 | 36 |
The most efficient implementation of diverse applications require the |
| 37 | 37 |
usage of different physical graph implementations. These differences |
| 38 | 38 |
appear in the size of graph we require to handle, memory or time usage |
| 39 | 39 |
limitations or in the set of operations through which the graph can be |
| 40 | 40 |
accessed. LEMON provides several physical graph structures to meet |
| 41 | 41 |
the diverging requirements of the possible users. In order to save on |
| 42 | 42 |
running time or on memory usage, some structures may fail to provide |
| 43 | 43 |
some graph features like arc/edge or node deletion. |
| 44 | 44 |
|
| 45 | 45 |
Alteration of standard containers need a very limited number of |
| 46 | 46 |
operations, these together satisfy the everyday requirements. |
| 47 | 47 |
In the case of graph structures, different operations are needed which do |
| 48 | 48 |
not alter the physical graph, but gives another view. If some nodes or |
| 49 | 49 |
arcs have to be hidden or the reverse oriented graph have to be used, then |
| 50 | 50 |
this is the case. It also may happen that in a flow implementation |
| 51 | 51 |
the residual graph can be accessed by another algorithm, or a node-set |
| 52 | 52 |
is to be shrunk for another algorithm. |
| 53 | 53 |
LEMON also provides a variety of graphs for these requirements called |
| 54 | 54 |
\ref graph_adaptors "graph adaptors". Adaptors cannot be used alone but only |
| 55 | 55 |
in conjunction with other graph representations. |
| 56 | 56 |
|
| 57 | 57 |
You are free to use the graph structure that fit your requirements |
| 58 | 58 |
the best, most graph algorithms and auxiliary data structures can be used |
| 59 | 59 |
with any graph structure. |
| 60 | 60 |
|
| 61 | 61 |
<b>See also:</b> \ref graph_concepts "Graph Structure Concepts". |
| 62 | 62 |
*/ |
| 63 | 63 |
|
| 64 | 64 |
/** |
| 65 | 65 |
@defgroup graph_adaptors Adaptor Classes for Graphs |
| 66 | 66 |
@ingroup graphs |
| 67 | 67 |
\brief Adaptor classes for digraphs and graphs |
| 68 | 68 |
|
| 69 | 69 |
This group contains several useful adaptor classes for digraphs and graphs. |
| 70 | 70 |
|
| 71 | 71 |
The main parts of LEMON are the different graph structures, generic |
| 72 | 72 |
graph algorithms, graph concepts, which couple them, and graph |
| 73 | 73 |
adaptors. While the previous notions are more or less clear, the |
| 74 | 74 |
latter one needs further explanation. Graph adaptors are graph classes |
| 75 | 75 |
which serve for considering graph structures in different ways. |
| 76 | 76 |
|
| 77 | 77 |
A short example makes this much clearer. Suppose that we have an |
| 78 | 78 |
instance \c g of a directed graph type, say ListDigraph and an algorithm |
| 79 | 79 |
\code |
| 80 | 80 |
template <typename Digraph> |
| 81 | 81 |
int algorithm(const Digraph&); |
| 82 | 82 |
\endcode |
| 83 | 83 |
is needed to run on the reverse oriented graph. It may be expensive |
| 84 | 84 |
(in time or in memory usage) to copy \c g with the reversed |
| 85 | 85 |
arcs. In this case, an adaptor class is used, which (according |
| 86 | 86 |
to LEMON \ref concepts::Digraph "digraph concepts") works as a digraph. |
| 87 | 87 |
The adaptor uses the original digraph structure and digraph operations when |
| 88 | 88 |
methods of the reversed oriented graph are called. This means that the adaptor |
| 89 | 89 |
have minor memory usage, and do not perform sophisticated algorithmic |
| 90 | 90 |
actions. The purpose of it is to give a tool for the cases when a |
| 91 | 91 |
graph have to be used in a specific alteration. If this alteration is |
| 92 | 92 |
obtained by a usual construction like filtering the node or the arc set or |
| 93 | 93 |
considering a new orientation, then an adaptor is worthwhile to use. |
| 94 | 94 |
To come back to the reverse oriented graph, in this situation |
| 95 | 95 |
\code |
| 96 | 96 |
template<typename Digraph> class ReverseDigraph; |
| 97 | 97 |
\endcode |
| 98 | 98 |
template class can be used. The code looks as follows |
| 99 | 99 |
\code |
| 100 | 100 |
ListDigraph g; |
| 101 | 101 |
ReverseDigraph<ListDigraph> rg(g); |
| 102 | 102 |
int result = algorithm(rg); |
| 103 | 103 |
\endcode |
| 104 | 104 |
During running the algorithm, the original digraph \c g is untouched. |
| 105 | 105 |
This techniques give rise to an elegant code, and based on stable |
| 106 | 106 |
graph adaptors, complex algorithms can be implemented easily. |
| 107 | 107 |
|
| 108 | 108 |
In flow, circulation and matching problems, the residual |
| 109 | 109 |
graph is of particular importance. Combining an adaptor implementing |
| 110 | 110 |
this with shortest path algorithms or minimum mean cycle algorithms, |
| 111 | 111 |
a range of weighted and cardinality optimization algorithms can be |
| 112 | 112 |
obtained. For other examples, the interested user is referred to the |
| 113 | 113 |
detailed documentation of particular adaptors. |
| 114 | 114 |
|
| 115 | 115 |
The behavior of graph adaptors can be very different. Some of them keep |
| 116 | 116 |
capabilities of the original graph while in other cases this would be |
| 117 | 117 |
meaningless. This means that the concepts that they meet depend |
| 118 | 118 |
on the graph adaptor, and the wrapped graph. |
| 119 | 119 |
For example, if an arc of a reversed digraph is deleted, this is carried |
| 120 | 120 |
out by deleting the corresponding arc of the original digraph, thus the |
| 121 | 121 |
adaptor modifies the original digraph. |
| 122 | 122 |
However in case of a residual digraph, this operation has no sense. |
| 123 | 123 |
|
| 124 | 124 |
Let us stand one more example here to simplify your work. |
| 125 | 125 |
ReverseDigraph has constructor |
| 126 | 126 |
\code |
| 127 | 127 |
ReverseDigraph(Digraph& digraph); |
| 128 | 128 |
\endcode |
| 129 | 129 |
This means that in a situation, when a <tt>const %ListDigraph&</tt> |
| 130 | 130 |
reference to a graph is given, then it have to be instantiated with |
| 131 | 131 |
<tt>Digraph=const %ListDigraph</tt>. |
| 132 | 132 |
\code |
| 133 | 133 |
int algorithm1(const ListDigraph& g) {
|
| 134 | 134 |
ReverseDigraph<const ListDigraph> rg(g); |
| 135 | 135 |
return algorithm2(rg); |
| 136 | 136 |
} |
| 137 | 137 |
\endcode |
| 138 | 138 |
*/ |
| 139 | 139 |
|
| 140 | 140 |
/** |
| 141 | 141 |
@defgroup maps Maps |
| 142 | 142 |
@ingroup datas |
| 143 | 143 |
\brief Map structures implemented in LEMON. |
| 144 | 144 |
|
| 145 | 145 |
This group contains the map structures implemented in LEMON. |
| 146 | 146 |
|
| 147 | 147 |
LEMON provides several special purpose maps and map adaptors that e.g. combine |
| 148 | 148 |
new maps from existing ones. |
| 149 | 149 |
|
| 150 | 150 |
<b>See also:</b> \ref map_concepts "Map Concepts". |
| 151 | 151 |
*/ |
| 152 | 152 |
|
| 153 | 153 |
/** |
| 154 | 154 |
@defgroup graph_maps Graph Maps |
| 155 | 155 |
@ingroup maps |
| 156 | 156 |
\brief Special graph-related maps. |
| 157 | 157 |
|
| 158 | 158 |
This group contains maps that are specifically designed to assign |
| 159 | 159 |
values to the nodes and arcs/edges of graphs. |
| 160 | 160 |
|
| 161 | 161 |
If you are looking for the standard graph maps (\c NodeMap, \c ArcMap, |
| 162 | 162 |
\c EdgeMap), see the \ref graph_concepts "Graph Structure Concepts". |
| 163 | 163 |
*/ |
| 164 | 164 |
|
| 165 | 165 |
/** |
| 166 | 166 |
\defgroup map_adaptors Map Adaptors |
| 167 | 167 |
\ingroup maps |
| 168 | 168 |
\brief Tools to create new maps from existing ones |
| 169 | 169 |
|
| 170 | 170 |
This group contains map adaptors that are used to create "implicit" |
| 171 | 171 |
maps from other maps. |
| 172 | 172 |
|
| 173 | 173 |
Most of them are \ref concepts::ReadMap "read-only maps". |
| 174 | 174 |
They can make arithmetic and logical operations between one or two maps |
| 175 | 175 |
(negation, shifting, addition, multiplication, logical 'and', 'or', |
| 176 | 176 |
'not' etc.) or e.g. convert a map to another one of different Value type. |
| 177 | 177 |
|
| 178 | 178 |
The typical usage of this classes is passing implicit maps to |
| 179 | 179 |
algorithms. If a function type algorithm is called then the function |
| 180 | 180 |
type map adaptors can be used comfortable. For example let's see the |
| 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 | 229 |
@defgroup matrices Matrices |
| 230 | 230 |
@ingroup datas |
| 231 | 231 |
\brief Two dimensional data storages implemented in LEMON. |
| 232 | 232 |
|
| 233 | 233 |
This group contains two dimensional data storages implemented in LEMON. |
| 234 | 234 |
*/ |
| 235 | 235 |
|
| 236 | 236 |
/** |
| 237 | 237 |
@defgroup paths Path Structures |
| 238 | 238 |
@ingroup datas |
| 239 | 239 |
\brief %Path structures implemented in LEMON. |
| 240 | 240 |
|
| 241 | 241 |
This group contains the path structures implemented in LEMON. |
| 242 | 242 |
|
| 243 | 243 |
LEMON provides flexible data structures to work with paths. |
| 244 | 244 |
All of them have similar interfaces and they can be copied easily with |
| 245 | 245 |
assignment operators and copy constructors. This makes it easy and |
| 246 | 246 |
efficient to have e.g. the Dijkstra algorithm to store its result in |
| 247 | 247 |
any kind of path structure. |
| 248 | 248 |
|
| 249 | 249 |
\sa lemon::concepts::Path |
| 250 | 250 |
*/ |
| 251 | 251 |
|
| 252 | 252 |
/** |
| 253 | 253 |
@defgroup auxdat Auxiliary Data Structures |
| 254 | 254 |
@ingroup datas |
| 255 | 255 |
\brief Auxiliary data structures implemented in LEMON. |
| 256 | 256 |
|
| 257 | 257 |
This group contains some data structures implemented in LEMON in |
| 258 | 258 |
order to make it easier to implement combinatorial algorithms. |
| 259 | 259 |
*/ |
| 260 | 260 |
|
| 261 | 261 |
/** |
| 262 | 262 |
@defgroup algs Algorithms |
| 263 | 263 |
\brief This group contains the several algorithms |
| 264 | 264 |
implemented in LEMON. |
| 265 | 265 |
|
| 266 | 266 |
This group contains the several algorithms |
| 267 | 267 |
implemented in LEMON. |
| 268 | 268 |
*/ |
| 269 | 269 |
|
| 270 | 270 |
/** |
| 271 | 271 |
@defgroup search Graph Search |
| 272 | 272 |
@ingroup algs |
| 273 | 273 |
\brief Common graph search algorithms. |
| 274 | 274 |
|
| 275 | 275 |
This group contains the common graph search algorithms, namely |
| 276 | 276 |
\e breadth-first \e search (BFS) and \e depth-first \e search (DFS). |
| 277 | 277 |
*/ |
| 278 | 278 |
|
| 279 | 279 |
/** |
| 280 | 280 |
@defgroup shortest_path Shortest Path Algorithms |
| 281 | 281 |
@ingroup algs |
| 282 | 282 |
\brief Algorithms for finding shortest paths. |
| 283 | 283 |
|
| 284 | 284 |
This group contains the algorithms for finding shortest paths in digraphs. |
| 285 | 285 |
|
| 286 | 286 |
- \ref Dijkstra algorithm for finding shortest paths from a source node |
| 287 | 287 |
when all arc lengths are non-negative. |
| 288 | 288 |
- \ref BellmanFord "Bellman-Ford" algorithm for finding shortest paths |
| 289 | 289 |
from a source node when arc lenghts can be either positive or negative, |
| 290 | 290 |
but the digraph should not contain directed cycles with negative total |
| 291 | 291 |
length. |
| 292 | 292 |
- \ref FloydWarshall "Floyd-Warshall" and \ref Johnson "Johnson" algorithms |
| 293 | 293 |
for solving the \e all-pairs \e shortest \e paths \e problem when arc |
| 294 | 294 |
lenghts can be either positive or negative, but the digraph should |
| 295 | 295 |
not contain directed cycles with negative total length. |
| 296 | 296 |
- \ref Suurballe A successive shortest path algorithm for finding |
| 297 | 297 |
arc-disjoint paths between two nodes having minimum total length. |
| 298 | 298 |
*/ |
| 299 | 299 |
|
| 300 | 300 |
/** |
| 301 | 301 |
@defgroup max_flow Maximum Flow Algorithms |
| 302 | 302 |
@ingroup algs |
| 303 | 303 |
\brief Algorithms for finding maximum flows. |
| 304 | 304 |
|
| 305 | 305 |
This group contains the algorithms for finding maximum flows and |
| 306 | 306 |
feasible circulations. |
| 307 | 307 |
|
| 308 | 308 |
The \e maximum \e flow \e problem is to find a flow of maximum value between |
| 309 | 309 |
a single source and a single target. Formally, there is a \f$G=(V,A)\f$ |
| 310 | 310 |
digraph, a \f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function and
|
| 311 | 311 |
\f$s, t \in V\f$ source and target nodes. |
| 312 | 312 |
A maximum flow is an \f$f: A\rightarrow\mathbf{R}^+_0\f$ solution of the
|
| 313 | 313 |
following optimization problem. |
| 314 | 314 |
|
| 315 | 315 |
\f[ \max\sum_{sv\in A} f(sv) - \sum_{vs\in A} f(vs) \f]
|
| 316 | 316 |
\f[ \sum_{uv\in A} f(uv) = \sum_{vu\in A} f(vu)
|
| 317 | 317 |
\quad \forall u\in V\setminus\{s,t\} \f]
|
| 318 | 318 |
\f[ 0 \leq f(uv) \leq cap(uv) \quad \forall uv\in A \f] |
| 319 | 319 |
|
| 320 | 320 |
LEMON contains several algorithms for solving maximum flow problems: |
| 321 | 321 |
- \ref EdmondsKarp Edmonds-Karp algorithm. |
| 322 | 322 |
- \ref Preflow Goldberg-Tarjan's preflow push-relabel algorithm. |
| 323 | 323 |
- \ref DinitzSleatorTarjan Dinitz's blocking flow algorithm with dynamic trees. |
| 324 | 324 |
- \ref GoldbergTarjan Preflow push-relabel algorithm with dynamic trees. |
| 325 | 325 |
|
| 326 | 326 |
In most cases the \ref Preflow "Preflow" algorithm provides the |
| 327 | 327 |
fastest method for computing a maximum flow. All implementations |
| 328 | 328 |
also provide functions to query the minimum cut, which is the dual |
| 329 | 329 |
problem of maximum flow. |
| 330 | 330 |
|
| 331 | 331 |
\ref Circulation is a preflow push-relabel algorithm implemented directly |
| 332 | 332 |
for finding feasible circulations, which is a somewhat different problem, |
| 333 | 333 |
but it is strongly related to maximum flow. |
| 334 | 334 |
For more information, see \ref Circulation. |
| 335 | 335 |
*/ |
| 336 | 336 |
|
| 337 | 337 |
/** |
| 338 | 338 |
@defgroup min_cost_flow_algs Minimum Cost Flow Algorithms |
| 339 | 339 |
@ingroup algs |
| 340 | 340 |
|
| 341 | 341 |
\brief Algorithms for finding minimum cost flows and circulations. |
| 342 | 342 |
|
| 343 | 343 |
This group contains the algorithms for finding minimum cost flows and |
| 344 | 344 |
circulations. For more information about this problem and its dual |
| 345 | 345 |
solution see \ref min_cost_flow "Minimum Cost Flow Problem". |
| 346 | 346 |
|
| 347 | 347 |
LEMON contains several algorithms for this problem. |
| 348 | 348 |
- \ref NetworkSimplex Primal Network Simplex algorithm with various |
| 349 | 349 |
pivot strategies. |
| 350 | 350 |
- \ref CostScaling Push-Relabel and Augment-Relabel algorithms based on |
| 351 | 351 |
cost scaling. |
| 352 | 352 |
- \ref CapacityScaling Successive Shortest %Path algorithm with optional |
| 353 | 353 |
capacity scaling. |
| 354 | 354 |
- \ref CancelAndTighten The Cancel and Tighten algorithm. |
| 355 | 355 |
- \ref CycleCanceling Cycle-Canceling algorithms. |
| 356 | 356 |
|
| 357 | 357 |
In general NetworkSimplex is the most efficient implementation, |
| 358 | 358 |
but in special cases other algorithms could be faster. |
| 359 | 359 |
For example, if the total supply and/or capacities are rather small, |
| 360 | 360 |
CapacityScaling is usually the fastest algorithm (without effective scaling). |
| 361 | 361 |
*/ |
| 362 | 362 |
|
| 363 | 363 |
/** |
| 364 | 364 |
@defgroup min_cut Minimum Cut Algorithms |
| 365 | 365 |
@ingroup algs |
| 366 | 366 |
|
| 367 | 367 |
\brief Algorithms for finding minimum cut in graphs. |
| 368 | 368 |
|
| 369 | 369 |
This group contains the algorithms for finding minimum cut in graphs. |
| 370 | 370 |
|
| 371 | 371 |
The \e minimum \e cut \e problem is to find a non-empty and non-complete |
| 372 | 372 |
\f$X\f$ subset of the nodes with minimum overall capacity on |
| 373 | 373 |
outgoing arcs. Formally, there is a \f$G=(V,A)\f$ digraph, a |
| 374 | 374 |
\f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function. The minimum
|
| 375 | 375 |
cut is the \f$X\f$ solution of the next optimization problem: |
| 376 | 376 |
|
| 377 | 377 |
\f[ \min_{X \subset V, X\not\in \{\emptyset, V\}}
|
| 378 | 378 |
\sum_{uv\in A, u\in X, v\not\in X}cap(uv) \f]
|
| 379 | 379 |
|
| 380 | 380 |
LEMON contains several algorithms related to minimum cut problems: |
| 381 | 381 |
|
| 382 | 382 |
- \ref HaoOrlin "Hao-Orlin algorithm" for calculating minimum cut |
| 383 | 383 |
in directed graphs. |
| 384 | 384 |
- \ref NagamochiIbaraki "Nagamochi-Ibaraki algorithm" for |
| 385 | 385 |
calculating minimum cut in undirected graphs. |
| 386 | 386 |
- \ref GomoryHu "Gomory-Hu tree computation" for calculating |
| 387 | 387 |
all-pairs minimum cut in undirected graphs. |
| 388 | 388 |
|
| 389 | 389 |
If you want to find minimum cut just between two distinict nodes, |
| 390 | 390 |
see the \ref max_flow "maximum flow problem". |
| 391 | 391 |
*/ |
| 392 | 392 |
|
| 393 | 393 |
/** |
| 394 |
@defgroup min_mean_cycle Minimum Mean Cycle Algorithms |
|
| 395 |
@ingroup algs |
|
| 396 |
\brief Algorithms for finding minimum mean cycles. |
|
| 397 |
|
|
| 398 |
This group contains the algorithms for finding minimum mean cycles. |
|
| 399 |
|
|
| 400 |
The \e minimum \e mean \e cycle \e problem is to find a directed cycle |
|
| 401 |
of minimum mean length (cost) in a digraph. |
|
| 402 |
The mean length of a cycle is the average length of its arcs, i.e. the |
|
| 403 |
ratio between the total length of the cycle and the number of arcs on it. |
|
| 404 |
|
|
| 405 |
This problem has an important connection to \e conservative \e length |
|
| 406 |
\e functions, too. A length function on the arcs of a digraph is called |
|
| 407 |
conservative if and only if there is no directed cycle of negative total |
|
| 408 |
length. For an arbitrary length function, the negative of the minimum |
|
| 409 |
cycle mean is the smallest \f$\epsilon\f$ value so that increasing the |
|
| 410 |
arc lengths uniformly by \f$\epsilon\f$ results in a conservative length |
|
| 411 |
function. |
|
| 412 |
|
|
| 413 |
LEMON contains three algorithms for solving the minimum mean cycle problem: |
|
| 414 |
- \ref Karp "Karp"'s original algorithm. |
|
| 415 |
- \ref HartmannOrlin "Hartmann-Orlin"'s algorithm, which is an improved |
|
| 416 |
version of Karp's algorithm. |
|
| 417 |
- \ref Howard "Howard"'s policy iteration algorithm. |
|
| 418 |
|
|
| 419 |
In practice, the Howard algorithm proved to be by far the most efficient |
|
| 420 |
one, though the best known theoretical bound on its running time is |
|
| 421 |
exponential. |
|
| 422 |
Both Karp and HartmannOrlin algorithms run in time O(ne) and use space |
|
| 423 |
O(n<sup>2</sup>+e), but the latter one is typically faster due to the |
|
| 424 |
applied early termination scheme. |
|
| 425 |
*/ |
|
| 426 |
|
|
| 427 |
/** |
|
| 394 | 428 |
@defgroup graph_properties Connectivity and Other Graph Properties |
| 395 | 429 |
@ingroup algs |
| 396 | 430 |
\brief Algorithms for discovering the graph properties |
| 397 | 431 |
|
| 398 | 432 |
This group contains the algorithms for discovering the graph properties |
| 399 | 433 |
like connectivity, bipartiteness, euler property, simplicity etc. |
| 400 | 434 |
|
| 401 | 435 |
\image html edge_biconnected_components.png |
| 402 | 436 |
\image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth |
| 403 | 437 |
*/ |
| 404 | 438 |
|
| 405 | 439 |
/** |
| 406 | 440 |
@defgroup planar Planarity Embedding and Drawing |
| 407 | 441 |
@ingroup algs |
| 408 | 442 |
\brief Algorithms for planarity checking, embedding and drawing |
| 409 | 443 |
|
| 410 | 444 |
This group contains the algorithms for planarity checking, |
| 411 | 445 |
embedding and drawing. |
| 412 | 446 |
|
| 413 | 447 |
\image html planar.png |
| 414 | 448 |
\image latex planar.eps "Plane graph" width=\textwidth |
| 415 | 449 |
*/ |
| 416 | 450 |
|
| 417 | 451 |
/** |
| 418 | 452 |
@defgroup matching Matching Algorithms |
| 419 | 453 |
@ingroup algs |
| 420 | 454 |
\brief Algorithms for finding matchings in graphs and bipartite graphs. |
| 421 | 455 |
|
| 422 | 456 |
This group contains the algorithms for calculating |
| 423 | 457 |
matchings in graphs and bipartite graphs. The general matching problem is |
| 424 | 458 |
finding a subset of the edges for which each node has at most one incident |
| 425 | 459 |
edge. |
| 426 | 460 |
|
| 427 | 461 |
There are several different algorithms for calculate matchings in |
| 428 | 462 |
graphs. The matching problems in bipartite graphs are generally |
| 429 | 463 |
easier than in general graphs. The goal of the matching optimization |
| 430 | 464 |
can be finding maximum cardinality, maximum weight or minimum cost |
| 431 | 465 |
matching. The search can be constrained to find perfect or |
| 432 | 466 |
maximum cardinality matching. |
| 433 | 467 |
|
| 434 | 468 |
The matching algorithms implemented in LEMON: |
| 435 | 469 |
- \ref MaxBipartiteMatching Hopcroft-Karp augmenting path algorithm |
| 436 | 470 |
for calculating maximum cardinality matching in bipartite graphs. |
| 437 | 471 |
- \ref PrBipartiteMatching Push-relabel algorithm |
| 438 | 472 |
for calculating maximum cardinality matching in bipartite graphs. |
| 439 | 473 |
- \ref MaxWeightedBipartiteMatching |
| 440 | 474 |
Successive shortest path algorithm for calculating maximum weighted |
| 441 | 475 |
matching and maximum weighted bipartite matching in bipartite graphs. |
| 442 | 476 |
- \ref MinCostMaxBipartiteMatching |
| 443 | 477 |
Successive shortest path algorithm for calculating minimum cost maximum |
| 444 | 478 |
matching in bipartite graphs. |
| 445 | 479 |
- \ref MaxMatching Edmond's blossom shrinking algorithm for calculating |
| 446 | 480 |
maximum cardinality matching in general graphs. |
| 447 | 481 |
- \ref MaxWeightedMatching Edmond's blossom shrinking algorithm for calculating |
| 448 | 482 |
maximum weighted matching in general graphs. |
| 449 | 483 |
- \ref MaxWeightedPerfectMatching |
| 450 | 484 |
Edmond's blossom shrinking algorithm for calculating maximum weighted |
| 451 | 485 |
perfect matching in general graphs. |
| 452 | 486 |
|
| 453 | 487 |
\image html bipartite_matching.png |
| 454 | 488 |
\image latex bipartite_matching.eps "Bipartite Matching" width=\textwidth |
| 455 | 489 |
*/ |
| 456 | 490 |
|
| 457 | 491 |
/** |
| 458 | 492 |
@defgroup spantree Minimum Spanning Tree Algorithms |
| 459 | 493 |
@ingroup algs |
| 460 | 494 |
\brief Algorithms for finding minimum cost spanning trees and arborescences. |
| 461 | 495 |
|
| 462 | 496 |
This group contains the algorithms for finding minimum cost spanning |
| 463 | 497 |
trees and arborescences. |
| 464 | 498 |
*/ |
| 465 | 499 |
|
| 466 | 500 |
/** |
| 467 | 501 |
@defgroup auxalg Auxiliary Algorithms |
| 468 | 502 |
@ingroup algs |
| 469 | 503 |
\brief Auxiliary algorithms implemented in LEMON. |
| 470 | 504 |
|
| 471 | 505 |
This group contains some algorithms implemented in LEMON |
| 472 | 506 |
in order to make it easier to implement complex algorithms. |
| 473 | 507 |
*/ |
| 474 | 508 |
|
| 475 | 509 |
/** |
| 476 | 510 |
@defgroup approx Approximation Algorithms |
| 477 | 511 |
@ingroup algs |
| 478 | 512 |
\brief Approximation algorithms. |
| 479 | 513 |
|
| 480 | 514 |
This group contains the approximation and heuristic algorithms |
| 481 | 515 |
implemented in LEMON. |
| 482 | 516 |
*/ |
| 483 | 517 |
|
| 484 | 518 |
/** |
| 485 | 519 |
@defgroup gen_opt_group General Optimization Tools |
| 486 | 520 |
\brief This group contains some general optimization frameworks |
| 487 | 521 |
implemented in LEMON. |
| 488 | 522 |
|
| 489 | 523 |
This group contains some general optimization frameworks |
| 490 | 524 |
implemented in LEMON. |
| 491 | 525 |
*/ |
| 492 | 526 |
|
| 493 | 527 |
/** |
| 494 | 528 |
@defgroup lp_group Lp and Mip Solvers |
| 495 | 529 |
@ingroup gen_opt_group |
| 496 | 530 |
\brief Lp and Mip solver interfaces for LEMON. |
| 497 | 531 |
|
| 498 | 532 |
This group contains Lp and Mip solver interfaces for LEMON. The |
| 499 | 533 |
various LP solvers could be used in the same manner with this |
| 500 | 534 |
interface. |
| 501 | 535 |
*/ |
| 502 | 536 |
|
| 503 | 537 |
/** |
| 504 | 538 |
@defgroup lp_utils Tools for Lp and Mip Solvers |
| 505 | 539 |
@ingroup lp_group |
| 506 | 540 |
\brief Helper tools to the Lp and Mip solvers. |
| 507 | 541 |
|
| 508 | 542 |
This group adds some helper tools to general optimization framework |
| 509 | 543 |
implemented in LEMON. |
| 510 | 544 |
*/ |
| 511 | 545 |
|
| 512 | 546 |
/** |
| 513 | 547 |
@defgroup metah Metaheuristics |
| 514 | 548 |
@ingroup gen_opt_group |
| 515 | 549 |
\brief Metaheuristics for LEMON library. |
| 516 | 550 |
|
| 517 | 551 |
This group contains some metaheuristic optimization tools. |
| 518 | 552 |
*/ |
| 519 | 553 |
|
| 520 | 554 |
/** |
| 521 | 555 |
@defgroup utils Tools and Utilities |
| 522 | 556 |
\brief Tools and utilities for programming in LEMON |
| 523 | 557 |
|
| 524 | 558 |
Tools and utilities for programming in LEMON. |
| 525 | 559 |
*/ |
| 526 | 560 |
|
| 527 | 561 |
/** |
| 528 | 562 |
@defgroup gutils Basic Graph Utilities |
| 529 | 563 |
@ingroup utils |
| 530 | 564 |
\brief Simple basic graph utilities. |
| 531 | 565 |
|
| 532 | 566 |
This group contains some simple basic graph utilities. |
| 533 | 567 |
*/ |
| 534 | 568 |
|
| 535 | 569 |
/** |
| 536 | 570 |
@defgroup misc Miscellaneous Tools |
| 537 | 571 |
@ingroup utils |
| 538 | 572 |
\brief Tools for development, debugging and testing. |
| 539 | 573 |
|
| 540 | 574 |
This group contains several useful tools for development, |
| 541 | 575 |
debugging and testing. |
| 542 | 576 |
*/ |
| 543 | 577 |
|
| 544 | 578 |
/** |
| 545 | 579 |
@defgroup timecount Time Measuring and Counting |
| 546 | 580 |
@ingroup misc |
| 547 | 581 |
\brief Simple tools for measuring the performance of algorithms. |
| 548 | 582 |
|
| 549 | 583 |
This group contains simple tools for measuring the performance |
| 550 | 584 |
of algorithms. |
| 551 | 585 |
*/ |
| 552 | 586 |
|
| 553 | 587 |
/** |
| 554 | 588 |
@defgroup exceptions Exceptions |
| 555 | 589 |
@ingroup utils |
| 556 | 590 |
\brief Exceptions defined in LEMON. |
| 557 | 591 |
|
| 558 | 592 |
This group contains the exceptions defined in LEMON. |
| 559 | 593 |
*/ |
| 560 | 594 |
|
| 561 | 595 |
/** |
| 562 | 596 |
@defgroup io_group Input-Output |
| 563 | 597 |
\brief Graph Input-Output methods |
| 564 | 598 |
|
| 565 | 599 |
This group contains the tools for importing and exporting graphs |
| 566 | 600 |
and graph related data. Now it supports the \ref lgf-format |
| 567 | 601 |
"LEMON Graph Format", the \c DIMACS format and the encapsulated |
| 568 | 602 |
postscript (EPS) format. |
| 569 | 603 |
*/ |
| 570 | 604 |
|
| 571 | 605 |
/** |
| 572 | 606 |
@defgroup lemon_io LEMON Graph Format |
| 573 | 607 |
@ingroup io_group |
| 574 | 608 |
\brief Reading and writing LEMON Graph Format. |
| 575 | 609 |
|
| 576 | 610 |
This group contains methods for reading and writing |
| 577 | 611 |
\ref lgf-format "LEMON Graph Format". |
| 578 | 612 |
*/ |
| 579 | 613 |
|
| 580 | 614 |
/** |
| 581 | 615 |
@defgroup eps_io Postscript Exporting |
| 582 | 616 |
@ingroup io_group |
| 583 | 617 |
\brief General \c EPS drawer and graph exporter |
| 584 | 618 |
|
| 585 | 619 |
This group contains general \c EPS drawing methods and special |
| 586 | 620 |
graph exporting tools. |
| 587 | 621 |
*/ |
| 588 | 622 |
|
| 589 | 623 |
/** |
| 590 | 624 |
@defgroup dimacs_group DIMACS format |
| 591 | 625 |
@ingroup io_group |
| 592 | 626 |
\brief Read and write files in DIMACS format |
| 593 | 627 |
|
| 594 | 628 |
Tools to read a digraph from or write it to a file in DIMACS format data. |
| 595 | 629 |
*/ |
| 596 | 630 |
|
| 597 | 631 |
/** |
| 598 | 632 |
@defgroup nauty_group NAUTY Format |
| 599 | 633 |
@ingroup io_group |
| 600 | 634 |
\brief Read \e Nauty format |
| 601 | 635 |
|
| 602 | 636 |
Tool to read graphs from \e Nauty format data. |
| 603 | 637 |
*/ |
| 604 | 638 |
|
| 605 | 639 |
/** |
| 606 | 640 |
@defgroup concept Concepts |
| 607 | 641 |
\brief Skeleton classes and concept checking classes |
| 608 | 642 |
|
| 609 | 643 |
This group contains the data/algorithm skeletons and concept checking |
| 610 | 644 |
classes implemented in LEMON. |
| 611 | 645 |
|
| 612 | 646 |
The purpose of the classes in this group is fourfold. |
| 613 | 647 |
|
| 614 | 648 |
- These classes contain the documentations of the %concepts. In order |
| 615 | 649 |
to avoid document multiplications, an implementation of a concept |
| 616 | 650 |
simply refers to the corresponding concept class. |
| 617 | 651 |
|
| 618 | 652 |
- These classes declare every functions, <tt>typedef</tt>s etc. an |
| 619 | 653 |
implementation of the %concepts should provide, however completely |
| 620 | 654 |
without implementations and real data structures behind the |
| 621 | 655 |
interface. On the other hand they should provide nothing else. All |
| 622 | 656 |
the algorithms working on a data structure meeting a certain concept |
| 623 | 657 |
should compile with these classes. (Though it will not run properly, |
| 624 | 658 |
of course.) In this way it is easily to check if an algorithm |
| 625 | 659 |
doesn't use any extra feature of a certain implementation. |
| 626 | 660 |
|
| 627 | 661 |
- The concept descriptor classes also provide a <em>checker class</em> |
| 628 | 662 |
that makes it possible to check whether a certain implementation of a |
| 629 | 663 |
concept indeed provides all the required features. |
| 630 | 664 |
|
| 631 | 665 |
- Finally, They can serve as a skeleton of a new implementation of a concept. |
| 632 | 666 |
*/ |
| 633 | 667 |
|
| 634 | 668 |
/** |
| 635 | 669 |
@defgroup graph_concepts Graph Structure Concepts |
| 636 | 670 |
@ingroup concept |
| 637 | 671 |
\brief Skeleton and concept checking classes for graph structures |
| 638 | 672 |
|
| 639 | 673 |
This group contains the skeletons and concept checking classes of LEMON's |
| 640 | 674 |
graph structures and helper classes used to implement these. |
| 641 | 675 |
*/ |
| 642 | 676 |
|
| 643 | 677 |
/** |
| 644 | 678 |
@defgroup map_concepts Map Concepts |
| 645 | 679 |
@ingroup concept |
| 646 | 680 |
\brief Skeleton and concept checking classes for maps |
| 647 | 681 |
|
| 648 | 682 |
This group contains the skeletons and concept checking classes of maps. |
| 649 | 683 |
*/ |
| 650 | 684 |
|
| 651 | 685 |
/** |
| 652 | 686 |
\anchor demoprograms |
| 653 | 687 |
|
| 654 | 688 |
@defgroup demos Demo Programs |
| 655 | 689 |
|
| 656 | 690 |
Some demo programs are listed here. Their full source codes can be found in |
| 657 | 691 |
the \c demo subdirectory of the source tree. |
| 658 | 692 |
|
| 659 | 693 |
In order to compile them, use the <tt>make demo</tt> or the |
| 660 | 694 |
<tt>make check</tt> commands. |
| 661 | 695 |
*/ |
| 662 | 696 |
|
| 663 | 697 |
/** |
| 664 | 698 |
@defgroup tools Standalone Utility Applications |
| 665 | 699 |
|
| 666 | 700 |
Some utility applications are listed here. |
| 667 | 701 |
|
| 668 | 702 |
The standard compilation procedure (<tt>./configure;make</tt>) will compile |
| 669 | 703 |
them, as well. |
| 670 | 704 |
*/ |
| 671 | 705 |
|
| 672 | 706 |
} |
| 1 | 1 |
/* -*- C++ -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_HARTMANN_ORLIN_H |
| 20 | 20 |
#define LEMON_HARTMANN_ORLIN_H |
| 21 | 21 |
|
| 22 |
/// \ingroup |
|
| 22 |
/// \ingroup min_mean_cycle |
|
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief Hartmann-Orlin's algorithm for finding a minimum mean cycle. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <lemon/core.h> |
| 30 | 30 |
#include <lemon/path.h> |
| 31 | 31 |
#include <lemon/tolerance.h> |
| 32 | 32 |
#include <lemon/connectivity.h> |
| 33 | 33 |
|
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 | 36 |
/// \brief Default traits class of HartmannOrlin algorithm. |
| 37 | 37 |
/// |
| 38 | 38 |
/// Default traits class of HartmannOrlin algorithm. |
| 39 | 39 |
/// \tparam GR The type of the digraph. |
| 40 | 40 |
/// \tparam LEN The type of the length map. |
| 41 | 41 |
/// It must conform to the \ref concepts::Rea_data "Rea_data" concept. |
| 42 | 42 |
#ifdef DOXYGEN |
| 43 | 43 |
template <typename GR, typename LEN> |
| 44 | 44 |
#else |
| 45 | 45 |
template <typename GR, typename LEN, |
| 46 | 46 |
bool integer = std::numeric_limits<typename LEN::Value>::is_integer> |
| 47 | 47 |
#endif |
| 48 | 48 |
struct HartmannOrlinDefaultTraits |
| 49 | 49 |
{
|
| 50 | 50 |
/// The type of the digraph |
| 51 | 51 |
typedef GR Digraph; |
| 52 | 52 |
/// The type of the length map |
| 53 | 53 |
typedef LEN LengthMap; |
| 54 | 54 |
/// The type of the arc lengths |
| 55 | 55 |
typedef typename LengthMap::Value Value; |
| 56 | 56 |
|
| 57 | 57 |
/// \brief The large value type used for internal computations |
| 58 | 58 |
/// |
| 59 | 59 |
/// The large value type used for internal computations. |
| 60 | 60 |
/// It is \c long \c long if the \c Value type is integer, |
| 61 | 61 |
/// otherwise it is \c double. |
| 62 | 62 |
/// \c Value must be convertible to \c LargeValue. |
| 63 | 63 |
typedef double LargeValue; |
| 64 | 64 |
|
| 65 | 65 |
/// The tolerance type used for internal computations |
| 66 | 66 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
| 67 | 67 |
|
| 68 | 68 |
/// \brief The path type of the found cycles |
| 69 | 69 |
/// |
| 70 | 70 |
/// The path type of the found cycles. |
| 71 | 71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 72 | 72 |
/// and it must have an \c addBack() function. |
| 73 | 73 |
typedef lemon::Path<Digraph> Path; |
| 74 | 74 |
}; |
| 75 | 75 |
|
| 76 | 76 |
// Default traits class for integer value types |
| 77 | 77 |
template <typename GR, typename LEN> |
| 78 | 78 |
struct HartmannOrlinDefaultTraits<GR, LEN, true> |
| 79 | 79 |
{
|
| 80 | 80 |
typedef GR Digraph; |
| 81 | 81 |
typedef LEN LengthMap; |
| 82 | 82 |
typedef typename LengthMap::Value Value; |
| 83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
| 84 | 84 |
typedef long long LargeValue; |
| 85 | 85 |
#else |
| 86 | 86 |
typedef long LargeValue; |
| 87 | 87 |
#endif |
| 88 | 88 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
| 89 | 89 |
typedef lemon::Path<Digraph> Path; |
| 90 | 90 |
}; |
| 91 | 91 |
|
| 92 | 92 |
|
| 93 |
/// \addtogroup |
|
| 93 |
/// \addtogroup min_mean_cycle |
|
| 94 | 94 |
/// @{
|
| 95 | 95 |
|
| 96 | 96 |
/// \brief Implementation of the Hartmann-Orlin algorithm for finding |
| 97 | 97 |
/// a minimum mean cycle. |
| 98 | 98 |
/// |
| 99 | 99 |
/// This class implements the Hartmann-Orlin algorithm for finding |
| 100 | 100 |
/// a directed cycle of minimum mean length (cost) in a digraph. |
| 101 |
/// It is an improved version of \ref Karp "Karp's original algorithm |
|
| 101 |
/// It is an improved version of \ref Karp "Karp"'s original algorithm, |
|
| 102 | 102 |
/// it applies an efficient early termination scheme. |
| 103 |
/// It runs in time O(ne) and uses space O(n<sup>2</sup>+e). |
|
| 103 | 104 |
/// |
| 104 | 105 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 105 | 106 |
/// \tparam LEN The type of the length map. The default |
| 106 | 107 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 107 | 108 |
#ifdef DOXYGEN |
| 108 | 109 |
template <typename GR, typename LEN, typename TR> |
| 109 | 110 |
#else |
| 110 | 111 |
template < typename GR, |
| 111 | 112 |
typename LEN = typename GR::template ArcMap<int>, |
| 112 | 113 |
typename TR = HartmannOrlinDefaultTraits<GR, LEN> > |
| 113 | 114 |
#endif |
| 114 | 115 |
class HartmannOrlin |
| 115 | 116 |
{
|
| 116 | 117 |
public: |
| 117 | 118 |
|
| 118 | 119 |
/// The type of the digraph |
| 119 | 120 |
typedef typename TR::Digraph Digraph; |
| 120 | 121 |
/// The type of the length map |
| 121 | 122 |
typedef typename TR::LengthMap LengthMap; |
| 122 | 123 |
/// The type of the arc lengths |
| 123 | 124 |
typedef typename TR::Value Value; |
| 124 | 125 |
|
| 125 | 126 |
/// \brief The large value type |
| 126 | 127 |
/// |
| 127 | 128 |
/// The large value type used for internal computations. |
| 128 | 129 |
/// Using the \ref HartmannOrlinDefaultTraits "default traits class", |
| 129 | 130 |
/// it is \c long \c long if the \c Value type is integer, |
| 130 | 131 |
/// otherwise it is \c double. |
| 131 | 132 |
typedef typename TR::LargeValue LargeValue; |
| 132 | 133 |
|
| 133 | 134 |
/// The tolerance type |
| 134 | 135 |
typedef typename TR::Tolerance Tolerance; |
| 135 | 136 |
|
| 136 | 137 |
/// \brief The path type of the found cycles |
| 137 | 138 |
/// |
| 138 | 139 |
/// The path type of the found cycles. |
| 139 | 140 |
/// Using the \ref HartmannOrlinDefaultTraits "default traits class", |
| 140 | 141 |
/// it is \ref lemon::Path "Path<Digraph>". |
| 141 | 142 |
typedef typename TR::Path Path; |
| 142 | 143 |
|
| 143 | 144 |
/// The \ref HartmannOrlinDefaultTraits "traits class" of the algorithm |
| 144 | 145 |
typedef TR Traits; |
| 145 | 146 |
|
| 146 | 147 |
private: |
| 147 | 148 |
|
| 148 | 149 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 149 | 150 |
|
| 150 | 151 |
// Data sturcture for path data |
| 151 | 152 |
struct PathData |
| 152 | 153 |
{
|
| 153 | 154 |
LargeValue dist; |
| 154 | 155 |
Arc pred; |
| 155 | 156 |
PathData(LargeValue d, Arc p = INVALID) : |
| 156 | 157 |
dist(d), pred(p) {}
|
| 157 | 158 |
}; |
| 158 | 159 |
|
| 159 | 160 |
typedef typename Digraph::template NodeMap<std::vector<PathData> > |
| 160 | 161 |
PathDataNodeMap; |
| 161 | 162 |
|
| 162 | 163 |
private: |
| 163 | 164 |
|
| 164 | 165 |
// The digraph the algorithm runs on |
| 165 | 166 |
const Digraph &_gr; |
| 166 | 167 |
// The length of the arcs |
| 167 | 168 |
const LengthMap &_length; |
| 168 | 169 |
|
| 169 | 170 |
// Data for storing the strongly connected components |
| 170 | 171 |
int _comp_num; |
| 171 | 172 |
typename Digraph::template NodeMap<int> _comp; |
| 172 | 173 |
std::vector<std::vector<Node> > _comp_nodes; |
| 173 | 174 |
std::vector<Node>* _nodes; |
| 174 | 175 |
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs; |
| 175 | 176 |
|
| 176 | 177 |
// Data for the found cycles |
| 177 | 178 |
bool _curr_found, _best_found; |
| 178 | 179 |
LargeValue _curr_length, _best_length; |
| 179 | 180 |
int _curr_size, _best_size; |
| 180 | 181 |
Node _curr_node, _best_node; |
| 181 | 182 |
int _curr_level, _best_level; |
| 182 | 183 |
|
| 183 | 184 |
Path *_cycle_path; |
| 184 | 185 |
bool _local_path; |
| 185 | 186 |
|
| 186 | 187 |
// Node map for storing path data |
| 187 | 188 |
PathDataNodeMap _data; |
| 188 | 189 |
// The processed nodes in the last round |
| 189 | 190 |
std::vector<Node> _process; |
| 190 | 191 |
|
| 191 | 192 |
Tolerance _tolerance; |
| 192 | 193 |
|
| 193 | 194 |
// Infinite constant |
| 194 | 195 |
const LargeValue INF; |
| 195 | 196 |
|
| 196 | 197 |
public: |
| 197 | 198 |
|
| 198 | 199 |
/// \name Named Template Parameters |
| 199 | 200 |
/// @{
|
| 200 | 201 |
|
| 201 | 202 |
template <typename T> |
| 202 | 203 |
struct SetLargeValueTraits : public Traits {
|
| 203 | 204 |
typedef T LargeValue; |
| 204 | 205 |
typedef lemon::Tolerance<T> Tolerance; |
| 205 | 206 |
}; |
| 206 | 207 |
|
| 207 | 208 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 208 | 209 |
/// \c LargeValue type. |
| 209 | 210 |
/// |
| 210 | 211 |
/// \ref named-templ-param "Named parameter" for setting \c LargeValue |
| 211 | 212 |
/// type. It is used for internal computations in the algorithm. |
| 212 | 213 |
template <typename T> |
| 213 | 214 |
struct SetLargeValue |
| 214 | 215 |
: public HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > {
|
| 215 | 216 |
typedef HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > Create; |
| 216 | 217 |
}; |
| 217 | 218 |
|
| 218 | 219 |
template <typename T> |
| 219 | 220 |
struct SetPathTraits : public Traits {
|
| 220 | 221 |
typedef T Path; |
| 221 | 222 |
}; |
| 222 | 223 |
|
| 223 | 224 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 224 | 225 |
/// \c %Path type. |
| 225 | 226 |
/// |
| 226 | 227 |
/// \ref named-templ-param "Named parameter" for setting the \c %Path |
| 227 | 228 |
/// type of the found cycles. |
| 228 | 229 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 229 | 230 |
/// and it must have an \c addFront() function. |
| 230 | 231 |
template <typename T> |
| 231 | 232 |
struct SetPath |
| 232 | 233 |
: public HartmannOrlin<GR, LEN, SetPathTraits<T> > {
|
| 233 | 234 |
typedef HartmannOrlin<GR, LEN, SetPathTraits<T> > Create; |
| 234 | 235 |
}; |
| 235 | 236 |
|
| 236 | 237 |
/// @} |
| 237 | 238 |
|
| 238 | 239 |
public: |
| 239 | 240 |
|
| 240 | 241 |
/// \brief Constructor. |
| 241 | 242 |
/// |
| 242 | 243 |
/// The constructor of the class. |
| 243 | 244 |
/// |
| 244 | 245 |
/// \param digraph The digraph the algorithm runs on. |
| 245 | 246 |
/// \param length The lengths (costs) of the arcs. |
| 246 | 247 |
HartmannOrlin( const Digraph &digraph, |
| 247 | 248 |
const LengthMap &length ) : |
| 248 | 249 |
_gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph), |
| 249 | 250 |
_best_found(false), _best_length(0), _best_size(1), |
| 250 | 251 |
_cycle_path(NULL), _local_path(false), _data(digraph), |
| 251 | 252 |
INF(std::numeric_limits<LargeValue>::has_infinity ? |
| 252 | 253 |
std::numeric_limits<LargeValue>::infinity() : |
| 253 | 254 |
std::numeric_limits<LargeValue>::max()) |
| 254 | 255 |
{}
|
| 255 | 256 |
|
| 256 | 257 |
/// Destructor. |
| 257 | 258 |
~HartmannOrlin() {
|
| 258 | 259 |
if (_local_path) delete _cycle_path; |
| 259 | 260 |
} |
| 260 | 261 |
|
| 261 | 262 |
/// \brief Set the path structure for storing the found cycle. |
| 262 | 263 |
/// |
| 263 | 264 |
/// This function sets an external path structure for storing the |
| 264 | 265 |
/// found cycle. |
| 265 | 266 |
/// |
| 266 | 267 |
/// If you don't call this function before calling \ref run() or |
| 267 | 268 |
/// \ref findMinMean(), it will allocate a local \ref Path "path" |
| 268 | 269 |
/// structure. The destuctor deallocates this automatically |
| 269 | 270 |
/// allocated object, of course. |
| 270 | 271 |
/// |
| 271 | 272 |
/// \note The algorithm calls only the \ref lemon::Path::addFront() |
| 272 | 273 |
/// "addFront()" function of the given path structure. |
| 273 | 274 |
/// |
| 274 | 275 |
/// \return <tt>(*this)</tt> |
| 275 | 276 |
HartmannOrlin& cycle(Path &path) {
|
| 276 | 277 |
if (_local_path) {
|
| 277 | 278 |
delete _cycle_path; |
| 278 | 279 |
_local_path = false; |
| 279 | 280 |
} |
| 280 | 281 |
_cycle_path = &path; |
| 281 | 282 |
return *this; |
| 282 | 283 |
} |
| 283 | 284 |
|
| 284 | 285 |
/// \name Execution control |
| 285 | 286 |
/// The simplest way to execute the algorithm is to call the \ref run() |
| 286 | 287 |
/// function.\n |
| 287 | 288 |
/// If you only need the minimum mean length, you may call |
| 288 | 289 |
/// \ref findMinMean(). |
| 289 | 290 |
|
| 290 | 291 |
/// @{
|
| 291 | 292 |
|
| 292 | 293 |
/// \brief Run the algorithm. |
| 293 | 294 |
/// |
| 294 | 295 |
/// This function runs the algorithm. |
| 295 | 296 |
/// It can be called more than once (e.g. if the underlying digraph |
| 296 | 297 |
/// and/or the arc lengths have been modified). |
| 297 | 298 |
/// |
| 298 | 299 |
/// \return \c true if a directed cycle exists in the digraph. |
| 299 | 300 |
/// |
| 300 | 301 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
| 301 | 302 |
/// \code |
| 302 | 303 |
/// return mmc.findMinMean() && mmc.findCycle(); |
| 303 | 304 |
/// \endcode |
| 304 | 305 |
bool run() {
|
| 305 | 306 |
return findMinMean() && findCycle(); |
| 306 | 307 |
} |
| 307 | 308 |
|
| 308 | 309 |
/// \brief Find the minimum cycle mean. |
| 309 | 310 |
/// |
| 310 | 311 |
/// This function finds the minimum mean length of the directed |
| 311 | 312 |
/// cycles in the digraph. |
| 312 | 313 |
/// |
| 313 | 314 |
/// \return \c true if a directed cycle exists in the digraph. |
| 314 | 315 |
bool findMinMean() {
|
| 315 | 316 |
// Initialization and find strongly connected components |
| 316 | 317 |
init(); |
| 317 | 318 |
findComponents(); |
| 318 | 319 |
|
| 319 | 320 |
// Find the minimum cycle mean in the components |
| 320 | 321 |
for (int comp = 0; comp < _comp_num; ++comp) {
|
| 321 | 322 |
if (!initComponent(comp)) continue; |
| 322 | 323 |
processRounds(); |
| 323 | 324 |
|
| 324 | 325 |
// Update the best cycle (global minimum mean cycle) |
| 325 | 326 |
if ( _curr_found && (!_best_found || |
| 326 | 327 |
_curr_length * _best_size < _best_length * _curr_size) ) {
|
| 327 | 328 |
_best_found = true; |
| 328 | 329 |
_best_length = _curr_length; |
| 329 | 330 |
_best_size = _curr_size; |
| 330 | 331 |
_best_node = _curr_node; |
| 331 | 332 |
_best_level = _curr_level; |
| 332 | 333 |
} |
| 333 | 334 |
} |
| 334 | 335 |
return _best_found; |
| 335 | 336 |
} |
| 336 | 337 |
|
| 337 | 338 |
/// \brief Find a minimum mean directed cycle. |
| 338 | 339 |
/// |
| 339 | 340 |
/// This function finds a directed cycle of minimum mean length |
| 340 | 341 |
/// in the digraph using the data computed by findMinMean(). |
| 341 | 342 |
/// |
| 342 | 343 |
/// \return \c true if a directed cycle exists in the digraph. |
| 343 | 344 |
/// |
| 344 | 345 |
/// \pre \ref findMinMean() must be called before using this function. |
| 345 | 346 |
bool findCycle() {
|
| 346 | 347 |
if (!_best_found) return false; |
| 347 | 348 |
IntNodeMap reached(_gr, -1); |
| 348 | 349 |
int r = _best_level + 1; |
| 349 | 350 |
Node u = _best_node; |
| 350 | 351 |
while (reached[u] < 0) {
|
| 351 | 352 |
reached[u] = --r; |
| 352 | 353 |
u = _gr.source(_data[u][r].pred); |
| 353 | 354 |
} |
| 354 | 355 |
r = reached[u]; |
| 355 | 356 |
Arc e = _data[u][r].pred; |
| 356 | 357 |
_cycle_path->addFront(e); |
| 357 | 358 |
_best_length = _length[e]; |
| 358 | 359 |
_best_size = 1; |
| 359 | 360 |
Node v; |
| 360 | 361 |
while ((v = _gr.source(e)) != u) {
|
| 361 | 362 |
e = _data[v][--r].pred; |
| 362 | 363 |
_cycle_path->addFront(e); |
| 363 | 364 |
_best_length += _length[e]; |
| 364 | 365 |
++_best_size; |
| 365 | 366 |
} |
| 366 | 367 |
return true; |
| 367 | 368 |
} |
| 368 | 369 |
|
| 369 | 370 |
/// @} |
| 370 | 371 |
|
| 371 | 372 |
/// \name Query Functions |
| 372 | 373 |
/// The results of the algorithm can be obtained using these |
| 373 | 374 |
/// functions.\n |
| 374 | 375 |
/// The algorithm should be executed before using them. |
| 375 | 376 |
|
| 376 | 377 |
/// @{
|
| 377 | 378 |
|
| 378 | 379 |
/// \brief Return the total length of the found cycle. |
| 379 | 380 |
/// |
| 380 | 381 |
/// This function returns the total length of the found cycle. |
| 381 | 382 |
/// |
| 382 | 383 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 383 | 384 |
/// using this function. |
| 384 | 385 |
LargeValue cycleLength() const {
|
| 385 | 386 |
return _best_length; |
| 386 | 387 |
} |
| 387 | 388 |
|
| 388 | 389 |
/// \brief Return the number of arcs on the found cycle. |
| 389 | 390 |
/// |
| 390 | 391 |
/// This function returns the number of arcs on the found cycle. |
| 391 | 392 |
/// |
| 392 | 393 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 393 | 394 |
/// using this function. |
| 394 | 395 |
int cycleArcNum() const {
|
| 395 | 396 |
return _best_size; |
| 396 | 397 |
} |
| 397 | 398 |
|
| 398 | 399 |
/// \brief Return the mean length of the found cycle. |
| 399 | 400 |
/// |
| 400 | 401 |
/// This function returns the mean length of the found cycle. |
| 401 | 402 |
/// |
| 402 | 403 |
/// \note <tt>alg.cycleMean()</tt> is just a shortcut of the |
| 403 | 404 |
/// following code. |
| 404 | 405 |
/// \code |
| 405 | 406 |
/// return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum(); |
| 406 | 407 |
/// \endcode |
| 407 | 408 |
/// |
| 408 | 409 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 409 | 410 |
/// using this function. |
| 410 | 411 |
double cycleMean() const {
|
| 411 | 412 |
return static_cast<double>(_best_length) / _best_size; |
| 412 | 413 |
} |
| 413 | 414 |
|
| 414 | 415 |
/// \brief Return the found cycle. |
| 415 | 416 |
/// |
| 416 | 417 |
/// This function returns a const reference to the path structure |
| 417 | 418 |
/// storing the found cycle. |
| 418 | 419 |
/// |
| 419 | 420 |
/// \pre \ref run() or \ref findCycle() must be called before using |
| 420 | 421 |
/// this function. |
| 421 | 422 |
const Path& cycle() const {
|
| 422 | 423 |
return *_cycle_path; |
| 423 | 424 |
} |
| 424 | 425 |
|
| 425 | 426 |
///@} |
| 426 | 427 |
|
| 427 | 428 |
private: |
| 428 | 429 |
|
| 429 | 430 |
// Initialization |
| 430 | 431 |
void init() {
|
| 431 | 432 |
if (!_cycle_path) {
|
| 432 | 433 |
_local_path = true; |
| 433 | 434 |
_cycle_path = new Path; |
| 434 | 435 |
} |
| 435 | 436 |
_cycle_path->clear(); |
| 436 | 437 |
_best_found = false; |
| 437 | 438 |
_best_length = 0; |
| 438 | 439 |
_best_size = 1; |
| 439 | 440 |
_cycle_path->clear(); |
| 440 | 441 |
for (NodeIt u(_gr); u != INVALID; ++u) |
| 441 | 442 |
_data[u].clear(); |
| 442 | 443 |
} |
| 443 | 444 |
|
| 444 | 445 |
// Find strongly connected components and initialize _comp_nodes |
| 445 | 446 |
// and _out_arcs |
| 446 | 447 |
void findComponents() {
|
| 447 | 448 |
_comp_num = stronglyConnectedComponents(_gr, _comp); |
| 448 | 449 |
_comp_nodes.resize(_comp_num); |
| 449 | 450 |
if (_comp_num == 1) {
|
| 450 | 451 |
_comp_nodes[0].clear(); |
| 451 | 452 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
| 452 | 453 |
_comp_nodes[0].push_back(n); |
| 453 | 454 |
_out_arcs[n].clear(); |
| 454 | 455 |
for (OutArcIt a(_gr, n); a != INVALID; ++a) {
|
| 455 | 456 |
_out_arcs[n].push_back(a); |
| 456 | 457 |
} |
| 457 | 458 |
} |
| 458 | 459 |
} else {
|
| 459 | 460 |
for (int i = 0; i < _comp_num; ++i) |
| 460 | 461 |
_comp_nodes[i].clear(); |
| 461 | 462 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
| 462 | 463 |
int k = _comp[n]; |
| 463 | 464 |
_comp_nodes[k].push_back(n); |
| 464 | 465 |
_out_arcs[n].clear(); |
| 465 | 466 |
for (OutArcIt a(_gr, n); a != INVALID; ++a) {
|
| 466 | 467 |
if (_comp[_gr.target(a)] == k) _out_arcs[n].push_back(a); |
| 467 | 468 |
} |
| 468 | 469 |
} |
| 469 | 470 |
} |
| 470 | 471 |
} |
| 471 | 472 |
|
| 472 | 473 |
// Initialize path data for the current component |
| 473 | 474 |
bool initComponent(int comp) {
|
| 474 | 475 |
_nodes = &(_comp_nodes[comp]); |
| 475 | 476 |
int n = _nodes->size(); |
| 476 | 477 |
if (n < 1 || (n == 1 && _out_arcs[(*_nodes)[0]].size() == 0)) {
|
| 477 | 478 |
return false; |
| 478 | 479 |
} |
| 479 | 480 |
for (int i = 0; i < n; ++i) {
|
| 480 | 481 |
_data[(*_nodes)[i]].resize(n + 1, PathData(INF)); |
| 481 | 482 |
} |
| 482 | 483 |
return true; |
| 483 | 484 |
} |
| 484 | 485 |
|
| 485 | 486 |
// Process all rounds of computing path data for the current component. |
| 486 | 487 |
// _data[v][k] is the length of a shortest directed walk from the root |
| 1 | 1 |
/* -*- C++ -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_HOWARD_H |
| 20 | 20 |
#define LEMON_HOWARD_H |
| 21 | 21 |
|
| 22 |
/// \ingroup |
|
| 22 |
/// \ingroup min_mean_cycle |
|
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief Howard's algorithm for finding a minimum mean cycle. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <lemon/core.h> |
| 30 | 30 |
#include <lemon/path.h> |
| 31 | 31 |
#include <lemon/tolerance.h> |
| 32 | 32 |
#include <lemon/connectivity.h> |
| 33 | 33 |
|
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 | 36 |
/// \brief Default traits class of Howard class. |
| 37 | 37 |
/// |
| 38 | 38 |
/// Default traits class of Howard class. |
| 39 | 39 |
/// \tparam GR The type of the digraph. |
| 40 | 40 |
/// \tparam LEN The type of the length map. |
| 41 | 41 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
| 42 | 42 |
#ifdef DOXYGEN |
| 43 | 43 |
template <typename GR, typename LEN> |
| 44 | 44 |
#else |
| 45 | 45 |
template <typename GR, typename LEN, |
| 46 | 46 |
bool integer = std::numeric_limits<typename LEN::Value>::is_integer> |
| 47 | 47 |
#endif |
| 48 | 48 |
struct HowardDefaultTraits |
| 49 | 49 |
{
|
| 50 | 50 |
/// The type of the digraph |
| 51 | 51 |
typedef GR Digraph; |
| 52 | 52 |
/// The type of the length map |
| 53 | 53 |
typedef LEN LengthMap; |
| 54 | 54 |
/// The type of the arc lengths |
| 55 | 55 |
typedef typename LengthMap::Value Value; |
| 56 | 56 |
|
| 57 | 57 |
/// \brief The large value type used for internal computations |
| 58 | 58 |
/// |
| 59 | 59 |
/// The large value type used for internal computations. |
| 60 | 60 |
/// It is \c long \c long if the \c Value type is integer, |
| 61 | 61 |
/// otherwise it is \c double. |
| 62 | 62 |
/// \c Value must be convertible to \c LargeValue. |
| 63 | 63 |
typedef double LargeValue; |
| 64 | 64 |
|
| 65 | 65 |
/// The tolerance type used for internal computations |
| 66 | 66 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
| 67 | 67 |
|
| 68 | 68 |
/// \brief The path type of the found cycles |
| 69 | 69 |
/// |
| 70 | 70 |
/// The path type of the found cycles. |
| 71 | 71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 72 | 72 |
/// and it must have an \c addBack() function. |
| 73 | 73 |
typedef lemon::Path<Digraph> Path; |
| 74 | 74 |
}; |
| 75 | 75 |
|
| 76 | 76 |
// Default traits class for integer value types |
| 77 | 77 |
template <typename GR, typename LEN> |
| 78 | 78 |
struct HowardDefaultTraits<GR, LEN, true> |
| 79 | 79 |
{
|
| 80 | 80 |
typedef GR Digraph; |
| 81 | 81 |
typedef LEN LengthMap; |
| 82 | 82 |
typedef typename LengthMap::Value Value; |
| 83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
| 84 | 84 |
typedef long long LargeValue; |
| 85 | 85 |
#else |
| 86 | 86 |
typedef long LargeValue; |
| 87 | 87 |
#endif |
| 88 | 88 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
| 89 | 89 |
typedef lemon::Path<Digraph> Path; |
| 90 | 90 |
}; |
| 91 | 91 |
|
| 92 | 92 |
|
| 93 |
/// \addtogroup |
|
| 93 |
/// \addtogroup min_mean_cycle |
|
| 94 | 94 |
/// @{
|
| 95 | 95 |
|
| 96 | 96 |
/// \brief Implementation of Howard's algorithm for finding a minimum |
| 97 | 97 |
/// mean cycle. |
| 98 | 98 |
/// |
| 99 | 99 |
/// This class implements Howard's policy iteration algorithm for finding |
| 100 | 100 |
/// a directed cycle of minimum mean length (cost) in a digraph. |
| 101 |
/// This class provides the most efficient algorithm for the |
|
| 102 |
/// minimum mean cycle problem, though the best known theoretical |
|
| 103 |
/// bound on its running time is exponential. |
|
| 101 | 104 |
/// |
| 102 | 105 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 103 | 106 |
/// \tparam LEN The type of the length map. The default |
| 104 | 107 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 105 | 108 |
#ifdef DOXYGEN |
| 106 | 109 |
template <typename GR, typename LEN, typename TR> |
| 107 | 110 |
#else |
| 108 | 111 |
template < typename GR, |
| 109 | 112 |
typename LEN = typename GR::template ArcMap<int>, |
| 110 | 113 |
typename TR = HowardDefaultTraits<GR, LEN> > |
| 111 | 114 |
#endif |
| 112 | 115 |
class Howard |
| 113 | 116 |
{
|
| 114 | 117 |
public: |
| 115 | 118 |
|
| 116 | 119 |
/// The type of the digraph |
| 117 | 120 |
typedef typename TR::Digraph Digraph; |
| 118 | 121 |
/// The type of the length map |
| 119 | 122 |
typedef typename TR::LengthMap LengthMap; |
| 120 | 123 |
/// The type of the arc lengths |
| 121 | 124 |
typedef typename TR::Value Value; |
| 122 | 125 |
|
| 123 | 126 |
/// \brief The large value type |
| 124 | 127 |
/// |
| 125 | 128 |
/// The large value type used for internal computations. |
| 126 | 129 |
/// Using the \ref HowardDefaultTraits "default traits class", |
| 127 | 130 |
/// it is \c long \c long if the \c Value type is integer, |
| 128 | 131 |
/// otherwise it is \c double. |
| 129 | 132 |
typedef typename TR::LargeValue LargeValue; |
| 130 | 133 |
|
| 131 | 134 |
/// The tolerance type |
| 132 | 135 |
typedef typename TR::Tolerance Tolerance; |
| 133 | 136 |
|
| 134 | 137 |
/// \brief The path type of the found cycles |
| 135 | 138 |
/// |
| 136 | 139 |
/// The path type of the found cycles. |
| 137 | 140 |
/// Using the \ref HowardDefaultTraits "default traits class", |
| 138 | 141 |
/// it is \ref lemon::Path "Path<Digraph>". |
| 139 | 142 |
typedef typename TR::Path Path; |
| 140 | 143 |
|
| 141 | 144 |
/// The \ref HowardDefaultTraits "traits class" of the algorithm |
| 142 | 145 |
typedef TR Traits; |
| 143 | 146 |
|
| 144 | 147 |
private: |
| 145 | 148 |
|
| 146 | 149 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 147 | 150 |
|
| 148 | 151 |
// The digraph the algorithm runs on |
| 149 | 152 |
const Digraph &_gr; |
| 150 | 153 |
// The length of the arcs |
| 151 | 154 |
const LengthMap &_length; |
| 152 | 155 |
|
| 153 | 156 |
// Data for the found cycles |
| 154 | 157 |
bool _curr_found, _best_found; |
| 155 | 158 |
LargeValue _curr_length, _best_length; |
| 156 | 159 |
int _curr_size, _best_size; |
| 157 | 160 |
Node _curr_node, _best_node; |
| 158 | 161 |
|
| 159 | 162 |
Path *_cycle_path; |
| 160 | 163 |
bool _local_path; |
| 161 | 164 |
|
| 162 | 165 |
// Internal data used by the algorithm |
| 163 | 166 |
typename Digraph::template NodeMap<Arc> _policy; |
| 164 | 167 |
typename Digraph::template NodeMap<bool> _reached; |
| 165 | 168 |
typename Digraph::template NodeMap<int> _level; |
| 166 | 169 |
typename Digraph::template NodeMap<LargeValue> _dist; |
| 167 | 170 |
|
| 168 | 171 |
// Data for storing the strongly connected components |
| 169 | 172 |
int _comp_num; |
| 170 | 173 |
typename Digraph::template NodeMap<int> _comp; |
| 171 | 174 |
std::vector<std::vector<Node> > _comp_nodes; |
| 172 | 175 |
std::vector<Node>* _nodes; |
| 173 | 176 |
typename Digraph::template NodeMap<std::vector<Arc> > _in_arcs; |
| 174 | 177 |
|
| 175 | 178 |
// Queue used for BFS search |
| 176 | 179 |
std::vector<Node> _queue; |
| 177 | 180 |
int _qfront, _qback; |
| 178 | 181 |
|
| 179 | 182 |
Tolerance _tolerance; |
| 180 | 183 |
|
| 181 | 184 |
// Infinite constant |
| 182 | 185 |
const LargeValue INF; |
| 183 | 186 |
|
| 184 | 187 |
public: |
| 185 | 188 |
|
| 186 | 189 |
/// \name Named Template Parameters |
| 187 | 190 |
/// @{
|
| 188 | 191 |
|
| 189 | 192 |
template <typename T> |
| 190 | 193 |
struct SetLargeValueTraits : public Traits {
|
| 191 | 194 |
typedef T LargeValue; |
| 192 | 195 |
typedef lemon::Tolerance<T> Tolerance; |
| 193 | 196 |
}; |
| 194 | 197 |
|
| 195 | 198 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 196 | 199 |
/// \c LargeValue type. |
| 197 | 200 |
/// |
| 198 | 201 |
/// \ref named-templ-param "Named parameter" for setting \c LargeValue |
| 199 | 202 |
/// type. It is used for internal computations in the algorithm. |
| 200 | 203 |
template <typename T> |
| 201 | 204 |
struct SetLargeValue |
| 202 | 205 |
: public Howard<GR, LEN, SetLargeValueTraits<T> > {
|
| 203 | 206 |
typedef Howard<GR, LEN, SetLargeValueTraits<T> > Create; |
| 204 | 207 |
}; |
| 205 | 208 |
|
| 206 | 209 |
template <typename T> |
| 207 | 210 |
struct SetPathTraits : public Traits {
|
| 208 | 211 |
typedef T Path; |
| 209 | 212 |
}; |
| 210 | 213 |
|
| 211 | 214 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 212 | 215 |
/// \c %Path type. |
| 213 | 216 |
/// |
| 214 | 217 |
/// \ref named-templ-param "Named parameter" for setting the \c %Path |
| 215 | 218 |
/// type of the found cycles. |
| 216 | 219 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 217 | 220 |
/// and it must have an \c addBack() function. |
| 218 | 221 |
template <typename T> |
| 219 | 222 |
struct SetPath |
| 220 | 223 |
: public Howard<GR, LEN, SetPathTraits<T> > {
|
| 221 | 224 |
typedef Howard<GR, LEN, SetPathTraits<T> > Create; |
| 222 | 225 |
}; |
| 223 | 226 |
|
| 224 | 227 |
/// @} |
| 225 | 228 |
|
| 226 | 229 |
public: |
| 227 | 230 |
|
| 228 | 231 |
/// \brief Constructor. |
| 229 | 232 |
/// |
| 230 | 233 |
/// The constructor of the class. |
| 231 | 234 |
/// |
| 232 | 235 |
/// \param digraph The digraph the algorithm runs on. |
| 233 | 236 |
/// \param length The lengths (costs) of the arcs. |
| 234 | 237 |
Howard( const Digraph &digraph, |
| 235 | 238 |
const LengthMap &length ) : |
| 236 | 239 |
_gr(digraph), _length(length), _best_found(false), |
| 237 | 240 |
_best_length(0), _best_size(1), _cycle_path(NULL), _local_path(false), |
| 238 | 241 |
_policy(digraph), _reached(digraph), _level(digraph), _dist(digraph), |
| 239 | 242 |
_comp(digraph), _in_arcs(digraph), |
| 240 | 243 |
INF(std::numeric_limits<LargeValue>::has_infinity ? |
| 241 | 244 |
std::numeric_limits<LargeValue>::infinity() : |
| 242 | 245 |
std::numeric_limits<LargeValue>::max()) |
| 243 | 246 |
{}
|
| 244 | 247 |
|
| 245 | 248 |
/// Destructor. |
| 246 | 249 |
~Howard() {
|
| 247 | 250 |
if (_local_path) delete _cycle_path; |
| 248 | 251 |
} |
| 249 | 252 |
|
| 250 | 253 |
/// \brief Set the path structure for storing the found cycle. |
| 251 | 254 |
/// |
| 252 | 255 |
/// This function sets an external path structure for storing the |
| 253 | 256 |
/// found cycle. |
| 254 | 257 |
/// |
| 255 | 258 |
/// If you don't call this function before calling \ref run() or |
| 256 | 259 |
/// \ref findMinMean(), it will allocate a local \ref Path "path" |
| 257 | 260 |
/// structure. The destuctor deallocates this automatically |
| 258 | 261 |
/// allocated object, of course. |
| 259 | 262 |
/// |
| 260 | 263 |
/// \note The algorithm calls only the \ref lemon::Path::addBack() |
| 261 | 264 |
/// "addBack()" function of the given path structure. |
| 262 | 265 |
/// |
| 263 | 266 |
/// \return <tt>(*this)</tt> |
| 264 | 267 |
Howard& cycle(Path &path) {
|
| 265 | 268 |
if (_local_path) {
|
| 266 | 269 |
delete _cycle_path; |
| 267 | 270 |
_local_path = false; |
| 268 | 271 |
} |
| 269 | 272 |
_cycle_path = &path; |
| 270 | 273 |
return *this; |
| 271 | 274 |
} |
| 272 | 275 |
|
| 273 | 276 |
/// \name Execution control |
| 274 | 277 |
/// The simplest way to execute the algorithm is to call the \ref run() |
| 275 | 278 |
/// function.\n |
| 276 | 279 |
/// If you only need the minimum mean length, you may call |
| 277 | 280 |
/// \ref findMinMean(). |
| 278 | 281 |
|
| 279 | 282 |
/// @{
|
| 280 | 283 |
|
| 281 | 284 |
/// \brief Run the algorithm. |
| 282 | 285 |
/// |
| 283 | 286 |
/// This function runs the algorithm. |
| 284 | 287 |
/// It can be called more than once (e.g. if the underlying digraph |
| 285 | 288 |
/// and/or the arc lengths have been modified). |
| 286 | 289 |
/// |
| 287 | 290 |
/// \return \c true if a directed cycle exists in the digraph. |
| 288 | 291 |
/// |
| 289 | 292 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
| 290 | 293 |
/// \code |
| 291 | 294 |
/// return mmc.findMinMean() && mmc.findCycle(); |
| 292 | 295 |
/// \endcode |
| 293 | 296 |
bool run() {
|
| 294 | 297 |
return findMinMean() && findCycle(); |
| 295 | 298 |
} |
| 296 | 299 |
|
| 297 | 300 |
/// \brief Find the minimum cycle mean. |
| 298 | 301 |
/// |
| 299 | 302 |
/// This function finds the minimum mean length of the directed |
| 300 | 303 |
/// cycles in the digraph. |
| 301 | 304 |
/// |
| 302 | 305 |
/// \return \c true if a directed cycle exists in the digraph. |
| 303 | 306 |
bool findMinMean() {
|
| 304 | 307 |
// Initialize and find strongly connected components |
| 305 | 308 |
init(); |
| 306 | 309 |
findComponents(); |
| 307 | 310 |
|
| 308 | 311 |
// Find the minimum cycle mean in the components |
| 309 | 312 |
for (int comp = 0; comp < _comp_num; ++comp) {
|
| 310 | 313 |
// Find the minimum mean cycle in the current component |
| 311 | 314 |
if (!buildPolicyGraph(comp)) continue; |
| 312 | 315 |
while (true) {
|
| 313 | 316 |
findPolicyCycle(); |
| 314 | 317 |
if (!computeNodeDistances()) break; |
| 315 | 318 |
} |
| 316 | 319 |
// Update the best cycle (global minimum mean cycle) |
| 317 | 320 |
if ( _curr_found && (!_best_found || |
| 318 | 321 |
_curr_length * _best_size < _best_length * _curr_size) ) {
|
| 319 | 322 |
_best_found = true; |
| 320 | 323 |
_best_length = _curr_length; |
| 321 | 324 |
_best_size = _curr_size; |
| 322 | 325 |
_best_node = _curr_node; |
| 323 | 326 |
} |
| 324 | 327 |
} |
| 325 | 328 |
return _best_found; |
| 326 | 329 |
} |
| 327 | 330 |
|
| 328 | 331 |
/// \brief Find a minimum mean directed cycle. |
| 329 | 332 |
/// |
| 330 | 333 |
/// This function finds a directed cycle of minimum mean length |
| 331 | 334 |
/// in the digraph using the data computed by findMinMean(). |
| 332 | 335 |
/// |
| 333 | 336 |
/// \return \c true if a directed cycle exists in the digraph. |
| 334 | 337 |
/// |
| 335 | 338 |
/// \pre \ref findMinMean() must be called before using this function. |
| 336 | 339 |
bool findCycle() {
|
| 337 | 340 |
if (!_best_found) return false; |
| 338 | 341 |
_cycle_path->addBack(_policy[_best_node]); |
| 339 | 342 |
for ( Node v = _best_node; |
| 340 | 343 |
(v = _gr.target(_policy[v])) != _best_node; ) {
|
| 341 | 344 |
_cycle_path->addBack(_policy[v]); |
| 342 | 345 |
} |
| 343 | 346 |
return true; |
| 344 | 347 |
} |
| 345 | 348 |
|
| 346 | 349 |
/// @} |
| 347 | 350 |
|
| 348 | 351 |
/// \name Query Functions |
| 349 | 352 |
/// The results of the algorithm can be obtained using these |
| 350 | 353 |
/// functions.\n |
| 351 | 354 |
/// The algorithm should be executed before using them. |
| 352 | 355 |
|
| 353 | 356 |
/// @{
|
| 354 | 357 |
|
| 355 | 358 |
/// \brief Return the total length of the found cycle. |
| 356 | 359 |
/// |
| 357 | 360 |
/// This function returns the total length of the found cycle. |
| 358 | 361 |
/// |
| 359 | 362 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 360 | 363 |
/// using this function. |
| 361 | 364 |
LargeValue cycleLength() const {
|
| 362 | 365 |
return _best_length; |
| 363 | 366 |
} |
| 364 | 367 |
|
| 365 | 368 |
/// \brief Return the number of arcs on the found cycle. |
| 366 | 369 |
/// |
| 367 | 370 |
/// This function returns the number of arcs on the found cycle. |
| 368 | 371 |
/// |
| 369 | 372 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 370 | 373 |
/// using this function. |
| 371 | 374 |
int cycleArcNum() const {
|
| 372 | 375 |
return _best_size; |
| 373 | 376 |
} |
| 374 | 377 |
|
| 375 | 378 |
/// \brief Return the mean length of the found cycle. |
| 376 | 379 |
/// |
| 377 | 380 |
/// This function returns the mean length of the found cycle. |
| 378 | 381 |
/// |
| 379 | 382 |
/// \note <tt>alg.cycleMean()</tt> is just a shortcut of the |
| 380 | 383 |
/// following code. |
| 381 | 384 |
/// \code |
| 382 | 385 |
/// return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum(); |
| 383 | 386 |
/// \endcode |
| 384 | 387 |
/// |
| 385 | 388 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 386 | 389 |
/// using this function. |
| 387 | 390 |
double cycleMean() const {
|
| 388 | 391 |
return static_cast<double>(_best_length) / _best_size; |
| 389 | 392 |
} |
| 390 | 393 |
|
| 391 | 394 |
/// \brief Return the found cycle. |
| 392 | 395 |
/// |
| 393 | 396 |
/// This function returns a const reference to the path structure |
| 394 | 397 |
/// storing the found cycle. |
| 395 | 398 |
/// |
| 396 | 399 |
/// \pre \ref run() or \ref findCycle() must be called before using |
| 397 | 400 |
/// this function. |
| 398 | 401 |
const Path& cycle() const {
|
| 399 | 402 |
return *_cycle_path; |
| 400 | 403 |
} |
| 401 | 404 |
|
| 402 | 405 |
///@} |
| 403 | 406 |
|
| 404 | 407 |
private: |
| 405 | 408 |
|
| 406 | 409 |
// Initialize |
| 407 | 410 |
void init() {
|
| 408 | 411 |
if (!_cycle_path) {
|
| 409 | 412 |
_local_path = true; |
| 410 | 413 |
_cycle_path = new Path; |
| 411 | 414 |
} |
| 412 | 415 |
_queue.resize(countNodes(_gr)); |
| 413 | 416 |
_best_found = false; |
| 414 | 417 |
_best_length = 0; |
| 415 | 418 |
_best_size = 1; |
| 416 | 419 |
_cycle_path->clear(); |
| 417 | 420 |
} |
| 418 | 421 |
|
| 419 | 422 |
// Find strongly connected components and initialize _comp_nodes |
| 420 | 423 |
// and _in_arcs |
| 421 | 424 |
void findComponents() {
|
| 422 | 425 |
_comp_num = stronglyConnectedComponents(_gr, _comp); |
| 423 | 426 |
_comp_nodes.resize(_comp_num); |
| 424 | 427 |
if (_comp_num == 1) {
|
| 425 | 428 |
_comp_nodes[0].clear(); |
| 426 | 429 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
| 427 | 430 |
_comp_nodes[0].push_back(n); |
| 428 | 431 |
_in_arcs[n].clear(); |
| 429 | 432 |
for (InArcIt a(_gr, n); a != INVALID; ++a) {
|
| 430 | 433 |
_in_arcs[n].push_back(a); |
| 431 | 434 |
} |
| 432 | 435 |
} |
| 433 | 436 |
} else {
|
| 434 | 437 |
for (int i = 0; i < _comp_num; ++i) |
| 435 | 438 |
_comp_nodes[i].clear(); |
| 436 | 439 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
| 437 | 440 |
int k = _comp[n]; |
| 438 | 441 |
_comp_nodes[k].push_back(n); |
| 439 | 442 |
_in_arcs[n].clear(); |
| 440 | 443 |
for (InArcIt a(_gr, n); a != INVALID; ++a) {
|
| 441 | 444 |
if (_comp[_gr.source(a)] == k) _in_arcs[n].push_back(a); |
| 442 | 445 |
} |
| 443 | 446 |
} |
| 444 | 447 |
} |
| 445 | 448 |
} |
| 446 | 449 |
|
| 447 | 450 |
// Build the policy graph in the given strongly connected component |
| 448 | 451 |
// (the out-degree of every node is 1) |
| 449 | 452 |
bool buildPolicyGraph(int comp) {
|
| 450 | 453 |
_nodes = &(_comp_nodes[comp]); |
| 451 | 454 |
if (_nodes->size() < 1 || |
| 452 | 455 |
(_nodes->size() == 1 && _in_arcs[(*_nodes)[0]].size() == 0)) {
|
| 453 | 456 |
return false; |
| 454 | 457 |
} |
| 455 | 458 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
| 456 | 459 |
_dist[(*_nodes)[i]] = INF; |
| 457 | 460 |
} |
| 458 | 461 |
Node u, v; |
| 459 | 462 |
Arc e; |
| 460 | 463 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
| 461 | 464 |
v = (*_nodes)[i]; |
| 462 | 465 |
for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
|
| 463 | 466 |
e = _in_arcs[v][j]; |
| 464 | 467 |
u = _gr.source(e); |
| 465 | 468 |
if (_length[e] < _dist[u]) {
|
| 466 | 469 |
_dist[u] = _length[e]; |
| 467 | 470 |
_policy[u] = e; |
| 468 | 471 |
} |
| 469 | 472 |
} |
| 470 | 473 |
} |
| 471 | 474 |
return true; |
| 472 | 475 |
} |
| 473 | 476 |
|
| 474 | 477 |
// Find the minimum mean cycle in the policy graph |
| 475 | 478 |
void findPolicyCycle() {
|
| 476 | 479 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
| 477 | 480 |
_level[(*_nodes)[i]] = -1; |
| 478 | 481 |
} |
| 479 | 482 |
LargeValue clength; |
| 480 | 483 |
int csize; |
| 481 | 484 |
Node u, v; |
| 482 | 485 |
_curr_found = false; |
| 483 | 486 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
| 484 | 487 |
u = (*_nodes)[i]; |
| 1 | 1 |
/* -*- C++ -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_KARP_H |
| 20 | 20 |
#define LEMON_KARP_H |
| 21 | 21 |
|
| 22 |
/// \ingroup |
|
| 22 |
/// \ingroup min_mean_cycle |
|
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief Karp's algorithm for finding a minimum mean cycle. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <lemon/core.h> |
| 30 | 30 |
#include <lemon/path.h> |
| 31 | 31 |
#include <lemon/tolerance.h> |
| 32 | 32 |
#include <lemon/connectivity.h> |
| 33 | 33 |
|
| 34 | 34 |
namespace lemon {
|
| 35 | 35 |
|
| 36 | 36 |
/// \brief Default traits class of Karp algorithm. |
| 37 | 37 |
/// |
| 38 | 38 |
/// Default traits class of Karp algorithm. |
| 39 | 39 |
/// \tparam GR The type of the digraph. |
| 40 | 40 |
/// \tparam LEN The type of the length map. |
| 41 | 41 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
| 42 | 42 |
#ifdef DOXYGEN |
| 43 | 43 |
template <typename GR, typename LEN> |
| 44 | 44 |
#else |
| 45 | 45 |
template <typename GR, typename LEN, |
| 46 | 46 |
bool integer = std::numeric_limits<typename LEN::Value>::is_integer> |
| 47 | 47 |
#endif |
| 48 | 48 |
struct KarpDefaultTraits |
| 49 | 49 |
{
|
| 50 | 50 |
/// The type of the digraph |
| 51 | 51 |
typedef GR Digraph; |
| 52 | 52 |
/// The type of the length map |
| 53 | 53 |
typedef LEN LengthMap; |
| 54 | 54 |
/// The type of the arc lengths |
| 55 | 55 |
typedef typename LengthMap::Value Value; |
| 56 | 56 |
|
| 57 | 57 |
/// \brief The large value type used for internal computations |
| 58 | 58 |
/// |
| 59 | 59 |
/// The large value type used for internal computations. |
| 60 | 60 |
/// It is \c long \c long if the \c Value type is integer, |
| 61 | 61 |
/// otherwise it is \c double. |
| 62 | 62 |
/// \c Value must be convertible to \c LargeValue. |
| 63 | 63 |
typedef double LargeValue; |
| 64 | 64 |
|
| 65 | 65 |
/// The tolerance type used for internal computations |
| 66 | 66 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
| 67 | 67 |
|
| 68 | 68 |
/// \brief The path type of the found cycles |
| 69 | 69 |
/// |
| 70 | 70 |
/// The path type of the found cycles. |
| 71 | 71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 72 | 72 |
/// and it must have an \c addBack() function. |
| 73 | 73 |
typedef lemon::Path<Digraph> Path; |
| 74 | 74 |
}; |
| 75 | 75 |
|
| 76 | 76 |
// Default traits class for integer value types |
| 77 | 77 |
template <typename GR, typename LEN> |
| 78 | 78 |
struct KarpDefaultTraits<GR, LEN, true> |
| 79 | 79 |
{
|
| 80 | 80 |
typedef GR Digraph; |
| 81 | 81 |
typedef LEN LengthMap; |
| 82 | 82 |
typedef typename LengthMap::Value Value; |
| 83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
| 84 | 84 |
typedef long long LargeValue; |
| 85 | 85 |
#else |
| 86 | 86 |
typedef long LargeValue; |
| 87 | 87 |
#endif |
| 88 | 88 |
typedef lemon::Tolerance<LargeValue> Tolerance; |
| 89 | 89 |
typedef lemon::Path<Digraph> Path; |
| 90 | 90 |
}; |
| 91 | 91 |
|
| 92 | 92 |
|
| 93 |
/// \addtogroup |
|
| 93 |
/// \addtogroup min_mean_cycle |
|
| 94 | 94 |
/// @{
|
| 95 | 95 |
|
| 96 | 96 |
/// \brief Implementation of Karp's algorithm for finding a minimum |
| 97 | 97 |
/// mean cycle. |
| 98 | 98 |
/// |
| 99 | 99 |
/// This class implements Karp's algorithm for finding a directed |
| 100 | 100 |
/// cycle of minimum mean length (cost) in a digraph. |
| 101 |
/// It runs in time O(ne) and uses space O(n<sup>2</sup>+e). |
|
| 101 | 102 |
/// |
| 102 | 103 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 103 | 104 |
/// \tparam LEN The type of the length map. The default |
| 104 | 105 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 105 | 106 |
#ifdef DOXYGEN |
| 106 | 107 |
template <typename GR, typename LEN, typename TR> |
| 107 | 108 |
#else |
| 108 | 109 |
template < typename GR, |
| 109 | 110 |
typename LEN = typename GR::template ArcMap<int>, |
| 110 | 111 |
typename TR = KarpDefaultTraits<GR, LEN> > |
| 111 | 112 |
#endif |
| 112 | 113 |
class Karp |
| 113 | 114 |
{
|
| 114 | 115 |
public: |
| 115 | 116 |
|
| 116 | 117 |
/// The type of the digraph |
| 117 | 118 |
typedef typename TR::Digraph Digraph; |
| 118 | 119 |
/// The type of the length map |
| 119 | 120 |
typedef typename TR::LengthMap LengthMap; |
| 120 | 121 |
/// The type of the arc lengths |
| 121 | 122 |
typedef typename TR::Value Value; |
| 122 | 123 |
|
| 123 | 124 |
/// \brief The large value type |
| 124 | 125 |
/// |
| 125 | 126 |
/// The large value type used for internal computations. |
| 126 | 127 |
/// Using the \ref KarpDefaultTraits "default traits class", |
| 127 | 128 |
/// it is \c long \c long if the \c Value type is integer, |
| 128 | 129 |
/// otherwise it is \c double. |
| 129 | 130 |
typedef typename TR::LargeValue LargeValue; |
| 130 | 131 |
|
| 131 | 132 |
/// The tolerance type |
| 132 | 133 |
typedef typename TR::Tolerance Tolerance; |
| 133 | 134 |
|
| 134 | 135 |
/// \brief The path type of the found cycles |
| 135 | 136 |
/// |
| 136 | 137 |
/// The path type of the found cycles. |
| 137 | 138 |
/// Using the \ref KarpDefaultTraits "default traits class", |
| 138 | 139 |
/// it is \ref lemon::Path "Path<Digraph>". |
| 139 | 140 |
typedef typename TR::Path Path; |
| 140 | 141 |
|
| 141 | 142 |
/// The \ref KarpDefaultTraits "traits class" of the algorithm |
| 142 | 143 |
typedef TR Traits; |
| 143 | 144 |
|
| 144 | 145 |
private: |
| 145 | 146 |
|
| 146 | 147 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 147 | 148 |
|
| 148 | 149 |
// Data sturcture for path data |
| 149 | 150 |
struct PathData |
| 150 | 151 |
{
|
| 151 | 152 |
LargeValue dist; |
| 152 | 153 |
Arc pred; |
| 153 | 154 |
PathData(LargeValue d, Arc p = INVALID) : |
| 154 | 155 |
dist(d), pred(p) {}
|
| 155 | 156 |
}; |
| 156 | 157 |
|
| 157 | 158 |
typedef typename Digraph::template NodeMap<std::vector<PathData> > |
| 158 | 159 |
PathDataNodeMap; |
| 159 | 160 |
|
| 160 | 161 |
private: |
| 161 | 162 |
|
| 162 | 163 |
// The digraph the algorithm runs on |
| 163 | 164 |
const Digraph &_gr; |
| 164 | 165 |
// The length of the arcs |
| 165 | 166 |
const LengthMap &_length; |
| 166 | 167 |
|
| 167 | 168 |
// Data for storing the strongly connected components |
| 168 | 169 |
int _comp_num; |
| 169 | 170 |
typename Digraph::template NodeMap<int> _comp; |
| 170 | 171 |
std::vector<std::vector<Node> > _comp_nodes; |
| 171 | 172 |
std::vector<Node>* _nodes; |
| 172 | 173 |
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs; |
| 173 | 174 |
|
| 174 | 175 |
// Data for the found cycle |
| 175 | 176 |
LargeValue _cycle_length; |
| 176 | 177 |
int _cycle_size; |
| 177 | 178 |
Node _cycle_node; |
| 178 | 179 |
|
| 179 | 180 |
Path *_cycle_path; |
| 180 | 181 |
bool _local_path; |
| 181 | 182 |
|
| 182 | 183 |
// Node map for storing path data |
| 183 | 184 |
PathDataNodeMap _data; |
| 184 | 185 |
// The processed nodes in the last round |
| 185 | 186 |
std::vector<Node> _process; |
| 186 | 187 |
|
| 187 | 188 |
Tolerance _tolerance; |
| 188 | 189 |
|
| 189 | 190 |
// Infinite constant |
| 190 | 191 |
const LargeValue INF; |
| 191 | 192 |
|
| 192 | 193 |
public: |
| 193 | 194 |
|
| 194 | 195 |
/// \name Named Template Parameters |
| 195 | 196 |
/// @{
|
| 196 | 197 |
|
| 197 | 198 |
template <typename T> |
| 198 | 199 |
struct SetLargeValueTraits : public Traits {
|
| 199 | 200 |
typedef T LargeValue; |
| 200 | 201 |
typedef lemon::Tolerance<T> Tolerance; |
| 201 | 202 |
}; |
| 202 | 203 |
|
| 203 | 204 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 204 | 205 |
/// \c LargeValue type. |
| 205 | 206 |
/// |
| 206 | 207 |
/// \ref named-templ-param "Named parameter" for setting \c LargeValue |
| 207 | 208 |
/// type. It is used for internal computations in the algorithm. |
| 208 | 209 |
template <typename T> |
| 209 | 210 |
struct SetLargeValue |
| 210 | 211 |
: public Karp<GR, LEN, SetLargeValueTraits<T> > {
|
| 211 | 212 |
typedef Karp<GR, LEN, SetLargeValueTraits<T> > Create; |
| 212 | 213 |
}; |
| 213 | 214 |
|
| 214 | 215 |
template <typename T> |
| 215 | 216 |
struct SetPathTraits : public Traits {
|
| 216 | 217 |
typedef T Path; |
| 217 | 218 |
}; |
| 218 | 219 |
|
| 219 | 220 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 220 | 221 |
/// \c %Path type. |
| 221 | 222 |
/// |
| 222 | 223 |
/// \ref named-templ-param "Named parameter" for setting the \c %Path |
| 223 | 224 |
/// type of the found cycles. |
| 224 | 225 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 225 | 226 |
/// and it must have an \c addFront() function. |
| 226 | 227 |
template <typename T> |
| 227 | 228 |
struct SetPath |
| 228 | 229 |
: public Karp<GR, LEN, SetPathTraits<T> > {
|
| 229 | 230 |
typedef Karp<GR, LEN, SetPathTraits<T> > Create; |
| 230 | 231 |
}; |
| 231 | 232 |
|
| 232 | 233 |
/// @} |
| 233 | 234 |
|
| 234 | 235 |
public: |
| 235 | 236 |
|
| 236 | 237 |
/// \brief Constructor. |
| 237 | 238 |
/// |
| 238 | 239 |
/// The constructor of the class. |
| 239 | 240 |
/// |
| 240 | 241 |
/// \param digraph The digraph the algorithm runs on. |
| 241 | 242 |
/// \param length The lengths (costs) of the arcs. |
| 242 | 243 |
Karp( const Digraph &digraph, |
| 243 | 244 |
const LengthMap &length ) : |
| 244 | 245 |
_gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph), |
| 245 | 246 |
_cycle_length(0), _cycle_size(1), _cycle_node(INVALID), |
| 246 | 247 |
_cycle_path(NULL), _local_path(false), _data(digraph), |
| 247 | 248 |
INF(std::numeric_limits<LargeValue>::has_infinity ? |
| 248 | 249 |
std::numeric_limits<LargeValue>::infinity() : |
| 249 | 250 |
std::numeric_limits<LargeValue>::max()) |
| 250 | 251 |
{}
|
| 251 | 252 |
|
| 252 | 253 |
/// Destructor. |
| 253 | 254 |
~Karp() {
|
| 254 | 255 |
if (_local_path) delete _cycle_path; |
| 255 | 256 |
} |
| 256 | 257 |
|
| 257 | 258 |
/// \brief Set the path structure for storing the found cycle. |
| 258 | 259 |
/// |
| 259 | 260 |
/// This function sets an external path structure for storing the |
| 260 | 261 |
/// found cycle. |
| 261 | 262 |
/// |
| 262 | 263 |
/// If you don't call this function before calling \ref run() or |
| 263 | 264 |
/// \ref findMinMean(), it will allocate a local \ref Path "path" |
| 264 | 265 |
/// structure. The destuctor deallocates this automatically |
| 265 | 266 |
/// allocated object, of course. |
| 266 | 267 |
/// |
| 267 | 268 |
/// \note The algorithm calls only the \ref lemon::Path::addFront() |
| 268 | 269 |
/// "addFront()" function of the given path structure. |
| 269 | 270 |
/// |
| 270 | 271 |
/// \return <tt>(*this)</tt> |
| 271 | 272 |
Karp& cycle(Path &path) {
|
| 272 | 273 |
if (_local_path) {
|
| 273 | 274 |
delete _cycle_path; |
| 274 | 275 |
_local_path = false; |
| 275 | 276 |
} |
| 276 | 277 |
_cycle_path = &path; |
| 277 | 278 |
return *this; |
| 278 | 279 |
} |
| 279 | 280 |
|
| 280 | 281 |
/// \name Execution control |
| 281 | 282 |
/// The simplest way to execute the algorithm is to call the \ref run() |
| 282 | 283 |
/// function.\n |
| 283 | 284 |
/// If you only need the minimum mean length, you may call |
| 284 | 285 |
/// \ref findMinMean(). |
| 285 | 286 |
|
| 286 | 287 |
/// @{
|
| 287 | 288 |
|
| 288 | 289 |
/// \brief Run the algorithm. |
| 289 | 290 |
/// |
| 290 | 291 |
/// This function runs the algorithm. |
| 291 | 292 |
/// It can be called more than once (e.g. if the underlying digraph |
| 292 | 293 |
/// and/or the arc lengths have been modified). |
| 293 | 294 |
/// |
| 294 | 295 |
/// \return \c true if a directed cycle exists in the digraph. |
| 295 | 296 |
/// |
| 296 | 297 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
| 297 | 298 |
/// \code |
| 298 | 299 |
/// return mmc.findMinMean() && mmc.findCycle(); |
| 299 | 300 |
/// \endcode |
| 300 | 301 |
bool run() {
|
| 301 | 302 |
return findMinMean() && findCycle(); |
| 302 | 303 |
} |
| 303 | 304 |
|
| 304 | 305 |
/// \brief Find the minimum cycle mean. |
| 305 | 306 |
/// |
| 306 | 307 |
/// This function finds the minimum mean length of the directed |
| 307 | 308 |
/// cycles in the digraph. |
| 308 | 309 |
/// |
| 309 | 310 |
/// \return \c true if a directed cycle exists in the digraph. |
| 310 | 311 |
bool findMinMean() {
|
| 311 | 312 |
// Initialization and find strongly connected components |
| 312 | 313 |
init(); |
| 313 | 314 |
findComponents(); |
| 314 | 315 |
|
| 315 | 316 |
// Find the minimum cycle mean in the components |
| 316 | 317 |
for (int comp = 0; comp < _comp_num; ++comp) {
|
| 317 | 318 |
if (!initComponent(comp)) continue; |
| 318 | 319 |
processRounds(); |
| 319 | 320 |
updateMinMean(); |
| 320 | 321 |
} |
| 321 | 322 |
return (_cycle_node != INVALID); |
| 322 | 323 |
} |
| 323 | 324 |
|
| 324 | 325 |
/// \brief Find a minimum mean directed cycle. |
| 325 | 326 |
/// |
| 326 | 327 |
/// This function finds a directed cycle of minimum mean length |
| 327 | 328 |
/// in the digraph using the data computed by findMinMean(). |
| 328 | 329 |
/// |
| 329 | 330 |
/// \return \c true if a directed cycle exists in the digraph. |
| 330 | 331 |
/// |
| 331 | 332 |
/// \pre \ref findMinMean() must be called before using this function. |
| 332 | 333 |
bool findCycle() {
|
| 333 | 334 |
if (_cycle_node == INVALID) return false; |
| 334 | 335 |
IntNodeMap reached(_gr, -1); |
| 335 | 336 |
int r = _data[_cycle_node].size(); |
| 336 | 337 |
Node u = _cycle_node; |
| 337 | 338 |
while (reached[u] < 0) {
|
| 338 | 339 |
reached[u] = --r; |
| 339 | 340 |
u = _gr.source(_data[u][r].pred); |
| 340 | 341 |
} |
| 341 | 342 |
r = reached[u]; |
| 342 | 343 |
Arc e = _data[u][r].pred; |
| 343 | 344 |
_cycle_path->addFront(e); |
| 344 | 345 |
_cycle_length = _length[e]; |
| 345 | 346 |
_cycle_size = 1; |
| 346 | 347 |
Node v; |
| 347 | 348 |
while ((v = _gr.source(e)) != u) {
|
| 348 | 349 |
e = _data[v][--r].pred; |
| 349 | 350 |
_cycle_path->addFront(e); |
| 350 | 351 |
_cycle_length += _length[e]; |
| 351 | 352 |
++_cycle_size; |
| 352 | 353 |
} |
| 353 | 354 |
return true; |
| 354 | 355 |
} |
| 355 | 356 |
|
| 356 | 357 |
/// @} |
| 357 | 358 |
|
| 358 | 359 |
/// \name Query Functions |
| 359 | 360 |
/// The results of the algorithm can be obtained using these |
| 360 | 361 |
/// functions.\n |
| 361 | 362 |
/// The algorithm should be executed before using them. |
| 362 | 363 |
|
| 363 | 364 |
/// @{
|
| 364 | 365 |
|
| 365 | 366 |
/// \brief Return the total length of the found cycle. |
| 366 | 367 |
/// |
| 367 | 368 |
/// This function returns the total length of the found cycle. |
| 368 | 369 |
/// |
| 369 | 370 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 370 | 371 |
/// using this function. |
| 371 | 372 |
LargeValue cycleLength() const {
|
| 372 | 373 |
return _cycle_length; |
| 373 | 374 |
} |
| 374 | 375 |
|
| 375 | 376 |
/// \brief Return the number of arcs on the found cycle. |
| 376 | 377 |
/// |
| 377 | 378 |
/// This function returns the number of arcs on the found cycle. |
| 378 | 379 |
/// |
| 379 | 380 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 380 | 381 |
/// using this function. |
| 381 | 382 |
int cycleArcNum() const {
|
| 382 | 383 |
return _cycle_size; |
| 383 | 384 |
} |
| 384 | 385 |
|
| 385 | 386 |
/// \brief Return the mean length of the found cycle. |
| 386 | 387 |
/// |
| 387 | 388 |
/// This function returns the mean length of the found cycle. |
| 388 | 389 |
/// |
| 389 | 390 |
/// \note <tt>alg.cycleMean()</tt> is just a shortcut of the |
| 390 | 391 |
/// following code. |
| 391 | 392 |
/// \code |
| 392 | 393 |
/// return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum(); |
| 393 | 394 |
/// \endcode |
| 394 | 395 |
/// |
| 395 | 396 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 396 | 397 |
/// using this function. |
| 397 | 398 |
double cycleMean() const {
|
| 398 | 399 |
return static_cast<double>(_cycle_length) / _cycle_size; |
| 399 | 400 |
} |
| 400 | 401 |
|
| 401 | 402 |
/// \brief Return the found cycle. |
| 402 | 403 |
/// |
| 403 | 404 |
/// This function returns a const reference to the path structure |
| 404 | 405 |
/// storing the found cycle. |
| 405 | 406 |
/// |
| 406 | 407 |
/// \pre \ref run() or \ref findCycle() must be called before using |
| 407 | 408 |
/// this function. |
| 408 | 409 |
const Path& cycle() const {
|
| 409 | 410 |
return *_cycle_path; |
| 410 | 411 |
} |
| 411 | 412 |
|
| 412 | 413 |
///@} |
| 413 | 414 |
|
| 414 | 415 |
private: |
| 415 | 416 |
|
| 416 | 417 |
// Initialization |
| 417 | 418 |
void init() {
|
| 418 | 419 |
if (!_cycle_path) {
|
| 419 | 420 |
_local_path = true; |
| 420 | 421 |
_cycle_path = new Path; |
| 421 | 422 |
} |
| 422 | 423 |
_cycle_path->clear(); |
| 423 | 424 |
_cycle_length = 0; |
| 424 | 425 |
_cycle_size = 1; |
| 425 | 426 |
_cycle_node = INVALID; |
| 426 | 427 |
for (NodeIt u(_gr); u != INVALID; ++u) |
| 427 | 428 |
_data[u].clear(); |
| 428 | 429 |
} |
| 429 | 430 |
|
| 430 | 431 |
// Find strongly connected components and initialize _comp_nodes |
| 431 | 432 |
// and _out_arcs |
| 432 | 433 |
void findComponents() {
|
| 433 | 434 |
_comp_num = stronglyConnectedComponents(_gr, _comp); |
| 434 | 435 |
_comp_nodes.resize(_comp_num); |
| 435 | 436 |
if (_comp_num == 1) {
|
| 436 | 437 |
_comp_nodes[0].clear(); |
| 437 | 438 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
| 438 | 439 |
_comp_nodes[0].push_back(n); |
| 439 | 440 |
_out_arcs[n].clear(); |
| 440 | 441 |
for (OutArcIt a(_gr, n); a != INVALID; ++a) {
|
| 441 | 442 |
_out_arcs[n].push_back(a); |
| 442 | 443 |
} |
| 443 | 444 |
} |
| 444 | 445 |
} else {
|
| 445 | 446 |
for (int i = 0; i < _comp_num; ++i) |
| 446 | 447 |
_comp_nodes[i].clear(); |
| 447 | 448 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
| 448 | 449 |
int k = _comp[n]; |
| 449 | 450 |
_comp_nodes[k].push_back(n); |
| 450 | 451 |
_out_arcs[n].clear(); |
| 451 | 452 |
for (OutArcIt a(_gr, n); a != INVALID; ++a) {
|
| 452 | 453 |
if (_comp[_gr.target(a)] == k) _out_arcs[n].push_back(a); |
| 453 | 454 |
} |
| 454 | 455 |
} |
| 455 | 456 |
} |
| 456 | 457 |
} |
| 457 | 458 |
|
| 458 | 459 |
// Initialize path data for the current component |
| 459 | 460 |
bool initComponent(int comp) {
|
| 460 | 461 |
_nodes = &(_comp_nodes[comp]); |
| 461 | 462 |
int n = _nodes->size(); |
| 462 | 463 |
if (n < 1 || (n == 1 && _out_arcs[(*_nodes)[0]].size() == 0)) {
|
| 463 | 464 |
return false; |
| 464 | 465 |
} |
| 465 | 466 |
for (int i = 0; i < n; ++i) {
|
| 466 | 467 |
_data[(*_nodes)[i]].resize(n + 1, PathData(INF)); |
| 467 | 468 |
} |
| 468 | 469 |
return true; |
| 469 | 470 |
} |
| 470 | 471 |
|
| 471 | 472 |
// Process all rounds of computing path data for the current component. |
| 472 | 473 |
// _data[v][k] is the length of a shortest directed walk from the root |
| 473 | 474 |
// node to node v containing exactly k arcs. |
| 474 | 475 |
void processRounds() {
|
| 475 | 476 |
Node start = (*_nodes)[0]; |
| 476 | 477 |
_data[start][0] = PathData(0); |
| 477 | 478 |
_process.clear(); |
| 478 | 479 |
_process.push_back(start); |
| 479 | 480 |
|
| 480 | 481 |
int k, n = _nodes->size(); |
| 481 | 482 |
for (k = 1; k <= n && int(_process.size()) < n; ++k) {
|
| 482 | 483 |
processNextBuildRound(k); |
| 483 | 484 |
} |
| 484 | 485 |
for ( ; k <= n; ++k) {
|
0 comments (0 inline)