alpar@463
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
alpar@357
|
2 |
*
|
alpar@463
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
alpar@357
|
4 |
*
|
alpar@463
|
5 |
* Copyright (C) 2003-2009
|
alpar@357
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@357
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@357
|
8 |
*
|
alpar@357
|
9 |
* Permission to use, modify and distribute this software is granted
|
alpar@357
|
10 |
* provided that this copyright notice appears in all copies. For
|
alpar@357
|
11 |
* precise terms see the accompanying LICENSE file.
|
alpar@357
|
12 |
*
|
alpar@357
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@357
|
14 |
* express or implied, and with no claim as to its suitability for any
|
alpar@357
|
15 |
* purpose.
|
alpar@357
|
16 |
*
|
alpar@357
|
17 |
*/
|
alpar@357
|
18 |
|
alpar@357
|
19 |
#ifndef LEMON_SUURBALLE_H
|
alpar@357
|
20 |
#define LEMON_SUURBALLE_H
|
alpar@357
|
21 |
|
alpar@357
|
22 |
///\ingroup shortest_path
|
alpar@357
|
23 |
///\file
|
alpar@357
|
24 |
///\brief An algorithm for finding arc-disjoint paths between two
|
alpar@357
|
25 |
/// nodes having minimum total length.
|
alpar@357
|
26 |
|
alpar@357
|
27 |
#include <vector>
|
kpeter@670
|
28 |
#include <limits>
|
alpar@357
|
29 |
#include <lemon/bin_heap.h>
|
alpar@357
|
30 |
#include <lemon/path.h>
|
deba@566
|
31 |
#include <lemon/list_graph.h>
|
deba@566
|
32 |
#include <lemon/maps.h>
|
alpar@357
|
33 |
|
alpar@357
|
34 |
namespace lemon {
|
alpar@357
|
35 |
|
alpar@357
|
36 |
/// \addtogroup shortest_path
|
alpar@357
|
37 |
/// @{
|
alpar@357
|
38 |
|
kpeter@358
|
39 |
/// \brief Algorithm for finding arc-disjoint paths between two nodes
|
kpeter@358
|
40 |
/// having minimum total length.
|
alpar@357
|
41 |
///
|
alpar@357
|
42 |
/// \ref lemon::Suurballe "Suurballe" implements an algorithm for
|
alpar@357
|
43 |
/// finding arc-disjoint paths having minimum total length (cost)
|
kpeter@358
|
44 |
/// from a given source node to a given target node in a digraph.
|
alpar@357
|
45 |
///
|
kpeter@670
|
46 |
/// Note that this problem is a special case of the \ref min_cost_flow
|
kpeter@670
|
47 |
/// "minimum cost flow problem". This implementation is actually an
|
kpeter@670
|
48 |
/// efficient specialized version of the \ref CapacityScaling
|
kpeter@670
|
49 |
/// "Successive Shortest Path" algorithm directly for this problem.
|
kpeter@670
|
50 |
/// Therefore this class provides query functions for flow values and
|
kpeter@670
|
51 |
/// node potentials (the dual solution) just like the minimum cost flow
|
kpeter@670
|
52 |
/// algorithms.
|
alpar@357
|
53 |
///
|
kpeter@606
|
54 |
/// \tparam GR The digraph type the algorithm runs on.
|
kpeter@670
|
55 |
/// \tparam LEN The type of the length map.
|
kpeter@670
|
56 |
/// The default value is <tt>GR::ArcMap<int></tt>.
|
alpar@357
|
57 |
///
|
alpar@357
|
58 |
/// \warning Length values should be \e non-negative \e integers.
|
alpar@357
|
59 |
///
|
alpar@357
|
60 |
/// \note For finding node-disjoint paths this algorithm can be used
|
kpeter@670
|
61 |
/// along with the \ref SplitNodes adaptor.
|
kpeter@358
|
62 |
#ifdef DOXYGEN
|
kpeter@606
|
63 |
template <typename GR, typename LEN>
|
kpeter@358
|
64 |
#else
|
kpeter@670
|
65 |
template < typename GR,
|
kpeter@606
|
66 |
typename LEN = typename GR::template ArcMap<int> >
|
kpeter@358
|
67 |
#endif
|
alpar@357
|
68 |
class Suurballe
|
alpar@357
|
69 |
{
|
kpeter@606
|
70 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR);
|
alpar@357
|
71 |
|
alpar@357
|
72 |
typedef ConstMap<Arc, int> ConstArcMap;
|
kpeter@606
|
73 |
typedef typename GR::template NodeMap<Arc> PredMap;
|
alpar@357
|
74 |
|
alpar@357
|
75 |
public:
|
alpar@357
|
76 |
|
kpeter@606
|
77 |
/// The type of the digraph the algorithm runs on.
|
kpeter@606
|
78 |
typedef GR Digraph;
|
kpeter@606
|
79 |
/// The type of the length map.
|
kpeter@606
|
80 |
typedef LEN LengthMap;
|
kpeter@606
|
81 |
/// The type of the lengths.
|
kpeter@606
|
82 |
typedef typename LengthMap::Value Length;
|
kpeter@670
|
83 |
#ifdef DOXYGEN
|
kpeter@670
|
84 |
/// The type of the flow map.
|
kpeter@670
|
85 |
typedef GR::ArcMap<int> FlowMap;
|
kpeter@670
|
86 |
/// The type of the potential map.
|
kpeter@670
|
87 |
typedef GR::NodeMap<Length> PotentialMap;
|
kpeter@670
|
88 |
#else
|
alpar@357
|
89 |
/// The type of the flow map.
|
alpar@357
|
90 |
typedef typename Digraph::template ArcMap<int> FlowMap;
|
alpar@357
|
91 |
/// The type of the potential map.
|
alpar@357
|
92 |
typedef typename Digraph::template NodeMap<Length> PotentialMap;
|
kpeter@670
|
93 |
#endif
|
kpeter@670
|
94 |
|
alpar@357
|
95 |
/// The type of the path structures.
|
kpeter@670
|
96 |
typedef SimplePath<GR> Path;
|
alpar@357
|
97 |
|
alpar@357
|
98 |
private:
|
alpar@463
|
99 |
|
kpeter@670
|
100 |
// ResidualDijkstra is a special implementation of the
|
kpeter@670
|
101 |
// Dijkstra algorithm for finding shortest paths in the
|
kpeter@670
|
102 |
// residual network with respect to the reduced arc lengths
|
kpeter@670
|
103 |
// and modifying the node potentials according to the
|
kpeter@670
|
104 |
// distance of the nodes.
|
alpar@357
|
105 |
class ResidualDijkstra
|
alpar@357
|
106 |
{
|
alpar@357
|
107 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef;
|
alpar@357
|
108 |
typedef BinHeap<Length, HeapCrossRef> Heap;
|
alpar@357
|
109 |
|
alpar@357
|
110 |
private:
|
alpar@357
|
111 |
|
kpeter@358
|
112 |
// The digraph the algorithm runs on
|
alpar@357
|
113 |
const Digraph &_graph;
|
alpar@357
|
114 |
|
alpar@357
|
115 |
// The main maps
|
alpar@357
|
116 |
const FlowMap &_flow;
|
alpar@357
|
117 |
const LengthMap &_length;
|
alpar@357
|
118 |
PotentialMap &_potential;
|
alpar@357
|
119 |
|
alpar@357
|
120 |
// The distance map
|
alpar@357
|
121 |
PotentialMap _dist;
|
alpar@357
|
122 |
// The pred arc map
|
alpar@357
|
123 |
PredMap &_pred;
|
alpar@357
|
124 |
// The processed (i.e. permanently labeled) nodes
|
alpar@357
|
125 |
std::vector<Node> _proc_nodes;
|
alpar@463
|
126 |
|
alpar@357
|
127 |
Node _s;
|
alpar@357
|
128 |
Node _t;
|
alpar@357
|
129 |
|
alpar@357
|
130 |
public:
|
alpar@357
|
131 |
|
alpar@357
|
132 |
/// Constructor.
|
kpeter@670
|
133 |
ResidualDijkstra( const Digraph &graph,
|
alpar@357
|
134 |
const FlowMap &flow,
|
alpar@357
|
135 |
const LengthMap &length,
|
alpar@357
|
136 |
PotentialMap &potential,
|
alpar@357
|
137 |
PredMap &pred,
|
alpar@357
|
138 |
Node s, Node t ) :
|
kpeter@670
|
139 |
_graph(graph), _flow(flow), _length(length), _potential(potential),
|
kpeter@670
|
140 |
_dist(graph), _pred(pred), _s(s), _t(t) {}
|
alpar@357
|
141 |
|
kpeter@358
|
142 |
/// \brief Run the algorithm. It returns \c true if a path is found
|
alpar@357
|
143 |
/// from the source node to the target node.
|
alpar@357
|
144 |
bool run() {
|
alpar@357
|
145 |
HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
|
alpar@357
|
146 |
Heap heap(heap_cross_ref);
|
alpar@357
|
147 |
heap.push(_s, 0);
|
alpar@357
|
148 |
_pred[_s] = INVALID;
|
alpar@357
|
149 |
_proc_nodes.clear();
|
alpar@357
|
150 |
|
kpeter@358
|
151 |
// Process nodes
|
alpar@357
|
152 |
while (!heap.empty() && heap.top() != _t) {
|
alpar@357
|
153 |
Node u = heap.top(), v;
|
alpar@357
|
154 |
Length d = heap.prio() + _potential[u], nd;
|
alpar@357
|
155 |
_dist[u] = heap.prio();
|
alpar@357
|
156 |
heap.pop();
|
alpar@357
|
157 |
_proc_nodes.push_back(u);
|
alpar@357
|
158 |
|
kpeter@358
|
159 |
// Traverse outgoing arcs
|
alpar@357
|
160 |
for (OutArcIt e(_graph, u); e != INVALID; ++e) {
|
alpar@357
|
161 |
if (_flow[e] == 0) {
|
alpar@357
|
162 |
v = _graph.target(e);
|
alpar@357
|
163 |
switch(heap.state(v)) {
|
alpar@357
|
164 |
case Heap::PRE_HEAP:
|
alpar@357
|
165 |
heap.push(v, d + _length[e] - _potential[v]);
|
alpar@357
|
166 |
_pred[v] = e;
|
alpar@357
|
167 |
break;
|
alpar@357
|
168 |
case Heap::IN_HEAP:
|
alpar@357
|
169 |
nd = d + _length[e] - _potential[v];
|
alpar@357
|
170 |
if (nd < heap[v]) {
|
alpar@357
|
171 |
heap.decrease(v, nd);
|
alpar@357
|
172 |
_pred[v] = e;
|
alpar@357
|
173 |
}
|
alpar@357
|
174 |
break;
|
alpar@357
|
175 |
case Heap::POST_HEAP:
|
alpar@357
|
176 |
break;
|
alpar@357
|
177 |
}
|
alpar@357
|
178 |
}
|
alpar@357
|
179 |
}
|
alpar@357
|
180 |
|
kpeter@358
|
181 |
// Traverse incoming arcs
|
alpar@357
|
182 |
for (InArcIt e(_graph, u); e != INVALID; ++e) {
|
alpar@357
|
183 |
if (_flow[e] == 1) {
|
alpar@357
|
184 |
v = _graph.source(e);
|
alpar@357
|
185 |
switch(heap.state(v)) {
|
alpar@357
|
186 |
case Heap::PRE_HEAP:
|
alpar@357
|
187 |
heap.push(v, d - _length[e] - _potential[v]);
|
alpar@357
|
188 |
_pred[v] = e;
|
alpar@357
|
189 |
break;
|
alpar@357
|
190 |
case Heap::IN_HEAP:
|
alpar@357
|
191 |
nd = d - _length[e] - _potential[v];
|
alpar@357
|
192 |
if (nd < heap[v]) {
|
alpar@357
|
193 |
heap.decrease(v, nd);
|
alpar@357
|
194 |
_pred[v] = e;
|
alpar@357
|
195 |
}
|
alpar@357
|
196 |
break;
|
alpar@357
|
197 |
case Heap::POST_HEAP:
|
alpar@357
|
198 |
break;
|
alpar@357
|
199 |
}
|
alpar@357
|
200 |
}
|
alpar@357
|
201 |
}
|
alpar@357
|
202 |
}
|
alpar@357
|
203 |
if (heap.empty()) return false;
|
alpar@357
|
204 |
|
kpeter@358
|
205 |
// Update potentials of processed nodes
|
alpar@357
|
206 |
Length t_dist = heap.prio();
|
alpar@357
|
207 |
for (int i = 0; i < int(_proc_nodes.size()); ++i)
|
alpar@357
|
208 |
_potential[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist;
|
alpar@357
|
209 |
return true;
|
alpar@357
|
210 |
}
|
alpar@357
|
211 |
|
alpar@357
|
212 |
}; //class ResidualDijkstra
|
alpar@357
|
213 |
|
alpar@357
|
214 |
private:
|
alpar@357
|
215 |
|
kpeter@358
|
216 |
// The digraph the algorithm runs on
|
alpar@357
|
217 |
const Digraph &_graph;
|
alpar@357
|
218 |
// The length map
|
alpar@357
|
219 |
const LengthMap &_length;
|
alpar@463
|
220 |
|
alpar@357
|
221 |
// Arc map of the current flow
|
alpar@357
|
222 |
FlowMap *_flow;
|
alpar@357
|
223 |
bool _local_flow;
|
alpar@357
|
224 |
// Node map of the current potentials
|
alpar@357
|
225 |
PotentialMap *_potential;
|
alpar@357
|
226 |
bool _local_potential;
|
alpar@357
|
227 |
|
alpar@357
|
228 |
// The source node
|
alpar@357
|
229 |
Node _source;
|
alpar@357
|
230 |
// The target node
|
alpar@357
|
231 |
Node _target;
|
alpar@357
|
232 |
|
alpar@357
|
233 |
// Container to store the found paths
|
alpar@357
|
234 |
std::vector< SimplePath<Digraph> > paths;
|
alpar@357
|
235 |
int _path_num;
|
alpar@357
|
236 |
|
alpar@357
|
237 |
// The pred arc map
|
alpar@357
|
238 |
PredMap _pred;
|
alpar@357
|
239 |
// Implementation of the Dijkstra algorithm for finding augmenting
|
alpar@357
|
240 |
// shortest paths in the residual network
|
alpar@357
|
241 |
ResidualDijkstra *_dijkstra;
|
alpar@357
|
242 |
|
alpar@357
|
243 |
public:
|
alpar@357
|
244 |
|
alpar@357
|
245 |
/// \brief Constructor.
|
alpar@357
|
246 |
///
|
alpar@357
|
247 |
/// Constructor.
|
alpar@357
|
248 |
///
|
kpeter@670
|
249 |
/// \param graph The digraph the algorithm runs on.
|
alpar@357
|
250 |
/// \param length The length (cost) values of the arcs.
|
kpeter@670
|
251 |
Suurballe( const Digraph &graph,
|
kpeter@670
|
252 |
const LengthMap &length ) :
|
kpeter@670
|
253 |
_graph(graph), _length(length), _flow(0), _local_flow(false),
|
kpeter@670
|
254 |
_potential(0), _local_potential(false), _pred(graph)
|
kpeter@670
|
255 |
{
|
kpeter@670
|
256 |
LEMON_ASSERT(std::numeric_limits<Length>::is_integer,
|
kpeter@670
|
257 |
"The length type of Suurballe must be integer");
|
kpeter@670
|
258 |
}
|
alpar@357
|
259 |
|
alpar@357
|
260 |
/// Destructor.
|
alpar@357
|
261 |
~Suurballe() {
|
alpar@357
|
262 |
if (_local_flow) delete _flow;
|
alpar@357
|
263 |
if (_local_potential) delete _potential;
|
alpar@357
|
264 |
delete _dijkstra;
|
alpar@357
|
265 |
}
|
alpar@357
|
266 |
|
kpeter@358
|
267 |
/// \brief Set the flow map.
|
alpar@357
|
268 |
///
|
kpeter@358
|
269 |
/// This function sets the flow map.
|
kpeter@670
|
270 |
/// If it is not used before calling \ref run() or \ref init(),
|
kpeter@670
|
271 |
/// an instance will be allocated automatically. The destructor
|
kpeter@670
|
272 |
/// deallocates this automatically allocated map, of course.
|
alpar@357
|
273 |
///
|
kpeter@670
|
274 |
/// The found flow contains only 0 and 1 values, since it is the
|
kpeter@670
|
275 |
/// union of the found arc-disjoint paths.
|
alpar@357
|
276 |
///
|
kpeter@606
|
277 |
/// \return <tt>(*this)</tt>
|
alpar@357
|
278 |
Suurballe& flowMap(FlowMap &map) {
|
alpar@357
|
279 |
if (_local_flow) {
|
alpar@357
|
280 |
delete _flow;
|
alpar@357
|
281 |
_local_flow = false;
|
alpar@357
|
282 |
}
|
alpar@357
|
283 |
_flow = ↦
|
alpar@357
|
284 |
return *this;
|
alpar@357
|
285 |
}
|
alpar@357
|
286 |
|
kpeter@358
|
287 |
/// \brief Set the potential map.
|
alpar@357
|
288 |
///
|
kpeter@358
|
289 |
/// This function sets the potential map.
|
kpeter@670
|
290 |
/// If it is not used before calling \ref run() or \ref init(),
|
kpeter@670
|
291 |
/// an instance will be allocated automatically. The destructor
|
kpeter@670
|
292 |
/// deallocates this automatically allocated map, of course.
|
alpar@357
|
293 |
///
|
kpeter@670
|
294 |
/// The node potentials provide the dual solution of the underlying
|
kpeter@670
|
295 |
/// \ref min_cost_flow "minimum cost flow problem".
|
alpar@357
|
296 |
///
|
kpeter@606
|
297 |
/// \return <tt>(*this)</tt>
|
alpar@357
|
298 |
Suurballe& potentialMap(PotentialMap &map) {
|
alpar@357
|
299 |
if (_local_potential) {
|
alpar@357
|
300 |
delete _potential;
|
alpar@357
|
301 |
_local_potential = false;
|
alpar@357
|
302 |
}
|
alpar@357
|
303 |
_potential = ↦
|
alpar@357
|
304 |
return *this;
|
alpar@357
|
305 |
}
|
alpar@357
|
306 |
|
kpeter@631
|
307 |
/// \name Execution Control
|
alpar@357
|
308 |
/// The simplest way to execute the algorithm is to call the run()
|
alpar@357
|
309 |
/// function.
|
alpar@357
|
310 |
/// \n
|
alpar@357
|
311 |
/// If you only need the flow that is the union of the found
|
alpar@357
|
312 |
/// arc-disjoint paths, you may call init() and findFlow().
|
alpar@357
|
313 |
|
alpar@357
|
314 |
/// @{
|
alpar@357
|
315 |
|
kpeter@358
|
316 |
/// \brief Run the algorithm.
|
alpar@357
|
317 |
///
|
kpeter@358
|
318 |
/// This function runs the algorithm.
|
alpar@357
|
319 |
///
|
kpeter@670
|
320 |
/// \param s The source node.
|
kpeter@670
|
321 |
/// \param t The target node.
|
alpar@357
|
322 |
/// \param k The number of paths to be found.
|
alpar@357
|
323 |
///
|
kpeter@358
|
324 |
/// \return \c k if there are at least \c k arc-disjoint paths from
|
kpeter@358
|
325 |
/// \c s to \c t in the digraph. Otherwise it returns the number of
|
alpar@357
|
326 |
/// arc-disjoint paths found.
|
alpar@357
|
327 |
///
|
kpeter@670
|
328 |
/// \note Apart from the return value, <tt>s.run(s, t, k)</tt> is
|
kpeter@670
|
329 |
/// just a shortcut of the following code.
|
alpar@357
|
330 |
/// \code
|
kpeter@670
|
331 |
/// s.init(s);
|
kpeter@670
|
332 |
/// s.findFlow(t, k);
|
alpar@357
|
333 |
/// s.findPaths();
|
alpar@357
|
334 |
/// \endcode
|
kpeter@670
|
335 |
int run(const Node& s, const Node& t, int k = 2) {
|
kpeter@670
|
336 |
init(s);
|
kpeter@670
|
337 |
findFlow(t, k);
|
alpar@357
|
338 |
findPaths();
|
alpar@357
|
339 |
return _path_num;
|
alpar@357
|
340 |
}
|
alpar@357
|
341 |
|
kpeter@358
|
342 |
/// \brief Initialize the algorithm.
|
alpar@357
|
343 |
///
|
kpeter@358
|
344 |
/// This function initializes the algorithm.
|
kpeter@670
|
345 |
///
|
kpeter@670
|
346 |
/// \param s The source node.
|
kpeter@670
|
347 |
void init(const Node& s) {
|
kpeter@670
|
348 |
_source = s;
|
kpeter@670
|
349 |
|
kpeter@358
|
350 |
// Initialize maps
|
alpar@357
|
351 |
if (!_flow) {
|
alpar@357
|
352 |
_flow = new FlowMap(_graph);
|
alpar@357
|
353 |
_local_flow = true;
|
alpar@357
|
354 |
}
|
alpar@357
|
355 |
if (!_potential) {
|
alpar@357
|
356 |
_potential = new PotentialMap(_graph);
|
alpar@357
|
357 |
_local_potential = true;
|
alpar@357
|
358 |
}
|
alpar@357
|
359 |
for (ArcIt e(_graph); e != INVALID; ++e) (*_flow)[e] = 0;
|
alpar@357
|
360 |
for (NodeIt n(_graph); n != INVALID; ++n) (*_potential)[n] = 0;
|
alpar@357
|
361 |
}
|
alpar@357
|
362 |
|
kpeter@670
|
363 |
/// \brief Execute the algorithm to find an optimal flow.
|
alpar@357
|
364 |
///
|
kpeter@358
|
365 |
/// This function executes the successive shortest path algorithm to
|
kpeter@670
|
366 |
/// find a minimum cost flow, which is the union of \c k (or less)
|
alpar@357
|
367 |
/// arc-disjoint paths.
|
alpar@357
|
368 |
///
|
kpeter@670
|
369 |
/// \param t The target node.
|
kpeter@670
|
370 |
/// \param k The number of paths to be found.
|
kpeter@670
|
371 |
///
|
kpeter@358
|
372 |
/// \return \c k if there are at least \c k arc-disjoint paths from
|
kpeter@670
|
373 |
/// the source node to the given node \c t in the digraph.
|
kpeter@670
|
374 |
/// Otherwise it returns the number of arc-disjoint paths found.
|
alpar@357
|
375 |
///
|
alpar@357
|
376 |
/// \pre \ref init() must be called before using this function.
|
kpeter@670
|
377 |
int findFlow(const Node& t, int k = 2) {
|
kpeter@670
|
378 |
_target = t;
|
kpeter@670
|
379 |
_dijkstra =
|
kpeter@670
|
380 |
new ResidualDijkstra( _graph, *_flow, _length, *_potential, _pred,
|
kpeter@670
|
381 |
_source, _target );
|
kpeter@670
|
382 |
|
kpeter@358
|
383 |
// Find shortest paths
|
alpar@357
|
384 |
_path_num = 0;
|
alpar@357
|
385 |
while (_path_num < k) {
|
kpeter@358
|
386 |
// Run Dijkstra
|
alpar@357
|
387 |
if (!_dijkstra->run()) break;
|
alpar@357
|
388 |
++_path_num;
|
alpar@357
|
389 |
|
kpeter@358
|
390 |
// Set the flow along the found shortest path
|
alpar@357
|
391 |
Node u = _target;
|
alpar@357
|
392 |
Arc e;
|
alpar@357
|
393 |
while ((e = _pred[u]) != INVALID) {
|
alpar@357
|
394 |
if (u == _graph.target(e)) {
|
alpar@357
|
395 |
(*_flow)[e] = 1;
|
alpar@357
|
396 |
u = _graph.source(e);
|
alpar@357
|
397 |
} else {
|
alpar@357
|
398 |
(*_flow)[e] = 0;
|
alpar@357
|
399 |
u = _graph.target(e);
|
alpar@357
|
400 |
}
|
alpar@357
|
401 |
}
|
alpar@357
|
402 |
}
|
alpar@357
|
403 |
return _path_num;
|
alpar@357
|
404 |
}
|
alpar@463
|
405 |
|
kpeter@358
|
406 |
/// \brief Compute the paths from the flow.
|
alpar@357
|
407 |
///
|
kpeter@670
|
408 |
/// This function computes the paths from the found minimum cost flow,
|
kpeter@670
|
409 |
/// which is the union of some arc-disjoint paths.
|
alpar@357
|
410 |
///
|
alpar@357
|
411 |
/// \pre \ref init() and \ref findFlow() must be called before using
|
alpar@357
|
412 |
/// this function.
|
alpar@357
|
413 |
void findPaths() {
|
alpar@357
|
414 |
FlowMap res_flow(_graph);
|
kpeter@358
|
415 |
for(ArcIt a(_graph); a != INVALID; ++a) res_flow[a] = (*_flow)[a];
|
alpar@357
|
416 |
|
alpar@357
|
417 |
paths.clear();
|
alpar@357
|
418 |
paths.resize(_path_num);
|
alpar@357
|
419 |
for (int i = 0; i < _path_num; ++i) {
|
alpar@357
|
420 |
Node n = _source;
|
alpar@357
|
421 |
while (n != _target) {
|
alpar@357
|
422 |
OutArcIt e(_graph, n);
|
alpar@357
|
423 |
for ( ; res_flow[e] == 0; ++e) ;
|
alpar@357
|
424 |
n = _graph.target(e);
|
alpar@357
|
425 |
paths[i].addBack(e);
|
alpar@357
|
426 |
res_flow[e] = 0;
|
alpar@357
|
427 |
}
|
alpar@357
|
428 |
}
|
alpar@357
|
429 |
}
|
alpar@357
|
430 |
|
alpar@357
|
431 |
/// @}
|
alpar@357
|
432 |
|
alpar@357
|
433 |
/// \name Query Functions
|
kpeter@358
|
434 |
/// The results of the algorithm can be obtained using these
|
alpar@357
|
435 |
/// functions.
|
alpar@357
|
436 |
/// \n The algorithm should be executed before using them.
|
alpar@357
|
437 |
|
alpar@357
|
438 |
/// @{
|
alpar@357
|
439 |
|
kpeter@670
|
440 |
/// \brief Return the total length of the found paths.
|
kpeter@670
|
441 |
///
|
kpeter@670
|
442 |
/// This function returns the total length of the found paths, i.e.
|
kpeter@670
|
443 |
/// the total cost of the found flow.
|
kpeter@670
|
444 |
/// The complexity of the function is O(e).
|
kpeter@670
|
445 |
///
|
kpeter@670
|
446 |
/// \pre \ref run() or \ref findFlow() must be called before using
|
kpeter@670
|
447 |
/// this function.
|
kpeter@670
|
448 |
Length totalLength() const {
|
kpeter@670
|
449 |
Length c = 0;
|
kpeter@670
|
450 |
for (ArcIt e(_graph); e != INVALID; ++e)
|
kpeter@670
|
451 |
c += (*_flow)[e] * _length[e];
|
kpeter@670
|
452 |
return c;
|
kpeter@670
|
453 |
}
|
kpeter@670
|
454 |
|
kpeter@670
|
455 |
/// \brief Return the flow value on the given arc.
|
kpeter@670
|
456 |
///
|
kpeter@670
|
457 |
/// This function returns the flow value on the given arc.
|
kpeter@670
|
458 |
/// It is \c 1 if the arc is involved in one of the found arc-disjoint
|
kpeter@670
|
459 |
/// paths, otherwise it is \c 0.
|
kpeter@670
|
460 |
///
|
kpeter@670
|
461 |
/// \pre \ref run() or \ref findFlow() must be called before using
|
kpeter@670
|
462 |
/// this function.
|
kpeter@670
|
463 |
int flow(const Arc& arc) const {
|
kpeter@670
|
464 |
return (*_flow)[arc];
|
kpeter@670
|
465 |
}
|
kpeter@670
|
466 |
|
kpeter@670
|
467 |
/// \brief Return a const reference to an arc map storing the
|
alpar@357
|
468 |
/// found flow.
|
alpar@357
|
469 |
///
|
kpeter@670
|
470 |
/// This function returns a const reference to an arc map storing
|
kpeter@358
|
471 |
/// the flow that is the union of the found arc-disjoint paths.
|
alpar@357
|
472 |
///
|
kpeter@358
|
473 |
/// \pre \ref run() or \ref findFlow() must be called before using
|
kpeter@358
|
474 |
/// this function.
|
alpar@357
|
475 |
const FlowMap& flowMap() const {
|
alpar@357
|
476 |
return *_flow;
|
alpar@357
|
477 |
}
|
alpar@357
|
478 |
|
kpeter@358
|
479 |
/// \brief Return the potential of the given node.
|
alpar@357
|
480 |
///
|
kpeter@358
|
481 |
/// This function returns the potential of the given node.
|
kpeter@670
|
482 |
/// The node potentials provide the dual solution of the
|
kpeter@670
|
483 |
/// underlying \ref min_cost_flow "minimum cost flow problem".
|
alpar@357
|
484 |
///
|
kpeter@358
|
485 |
/// \pre \ref run() or \ref findFlow() must be called before using
|
kpeter@358
|
486 |
/// this function.
|
alpar@357
|
487 |
Length potential(const Node& node) const {
|
alpar@357
|
488 |
return (*_potential)[node];
|
alpar@357
|
489 |
}
|
alpar@357
|
490 |
|
kpeter@670
|
491 |
/// \brief Return a const reference to a node map storing the
|
kpeter@670
|
492 |
/// found potentials (the dual solution).
|
alpar@357
|
493 |
///
|
kpeter@670
|
494 |
/// This function returns a const reference to a node map storing
|
kpeter@670
|
495 |
/// the found potentials that provide the dual solution of the
|
kpeter@670
|
496 |
/// underlying \ref min_cost_flow "minimum cost flow problem".
|
alpar@357
|
497 |
///
|
kpeter@358
|
498 |
/// \pre \ref run() or \ref findFlow() must be called before using
|
kpeter@358
|
499 |
/// this function.
|
kpeter@670
|
500 |
const PotentialMap& potentialMap() const {
|
kpeter@670
|
501 |
return *_potential;
|
alpar@357
|
502 |
}
|
alpar@357
|
503 |
|
kpeter@358
|
504 |
/// \brief Return the number of the found paths.
|
alpar@357
|
505 |
///
|
kpeter@358
|
506 |
/// This function returns the number of the found paths.
|
alpar@357
|
507 |
///
|
kpeter@358
|
508 |
/// \pre \ref run() or \ref findFlow() must be called before using
|
kpeter@358
|
509 |
/// this function.
|
alpar@357
|
510 |
int pathNum() const {
|
alpar@357
|
511 |
return _path_num;
|
alpar@357
|
512 |
}
|
alpar@357
|
513 |
|
kpeter@358
|
514 |
/// \brief Return a const reference to the specified path.
|
alpar@357
|
515 |
///
|
kpeter@358
|
516 |
/// This function returns a const reference to the specified path.
|
alpar@357
|
517 |
///
|
kpeter@670
|
518 |
/// \param i The function returns the <tt>i</tt>-th path.
|
alpar@357
|
519 |
/// \c i must be between \c 0 and <tt>%pathNum()-1</tt>.
|
alpar@357
|
520 |
///
|
kpeter@358
|
521 |
/// \pre \ref run() or \ref findPaths() must be called before using
|
kpeter@358
|
522 |
/// this function.
|
alpar@357
|
523 |
Path path(int i) const {
|
alpar@357
|
524 |
return paths[i];
|
alpar@357
|
525 |
}
|
alpar@357
|
526 |
|
alpar@357
|
527 |
/// @}
|
alpar@357
|
528 |
|
alpar@357
|
529 |
}; //class Suurballe
|
alpar@357
|
530 |
|
alpar@357
|
531 |
///@}
|
alpar@357
|
532 |
|
alpar@357
|
533 |
} //namespace lemon
|
alpar@357
|
534 |
|
alpar@357
|
535 |
#endif //LEMON_SUURBALLE_H
|