gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Modify the interface of Suurballe (#266, #181) - Move the parameters s and t from the constructor to the run() function. It makes the interface capable for multiple run(s,t,k) calls (possible improvement in the future) and it is more similar to Dijkstra. - Simliarly init() and findFlow(k) were replaced by init(s) and findFlow(t,k). The separation of parameters s and t is for the future plans of supporting multiple targets with one source node. For more information see #181. - LEMON_ASSERT for the Length type (check if it is integer). - Doc improvements. - Rearrange query functions. - Extend test file.
0 3 0
default
3 files changed with 222 insertions and 149 deletions:
↑ Collapse diff ↑
Ignore white space 48 line context
... ...
@@ -4,151 +4,161 @@
4 4
 *
5 5
 * Copyright (C) 2003-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_SUURBALLE_H
20 20
#define LEMON_SUURBALLE_H
21 21

	
22 22
///\ingroup shortest_path
23 23
///\file
24 24
///\brief An algorithm for finding arc-disjoint paths between two
25 25
/// nodes having minimum total length.
26 26

	
27 27
#include <vector>
28
#include <limits>
28 29
#include <lemon/bin_heap.h>
29 30
#include <lemon/path.h>
30 31
#include <lemon/list_graph.h>
31 32
#include <lemon/maps.h>
32 33

	
33 34
namespace lemon {
34 35

	
35 36
  /// \addtogroup shortest_path
36 37
  /// @{
37 38

	
38 39
  /// \brief Algorithm for finding arc-disjoint paths between two nodes
39 40
  /// having minimum total length.
40 41
  ///
41 42
  /// \ref lemon::Suurballe "Suurballe" implements an algorithm for
42 43
  /// finding arc-disjoint paths having minimum total length (cost)
43 44
  /// from a given source node to a given target node in a digraph.
44 45
  ///
45
  /// In fact, this implementation is the specialization of the
46
  /// \ref CapacityScaling "successive shortest path" algorithm.
46
  /// Note that this problem is a special case of the \ref min_cost_flow
47
  /// "minimum cost flow problem". This implementation is actually an
48
  /// efficient specialized version of the \ref CapacityScaling
49
  /// "Successive Shortest Path" algorithm directly for this problem.
50
  /// Therefore this class provides query functions for flow values and
51
  /// node potentials (the dual solution) just like the minimum cost flow
52
  /// algorithms.
47 53
  ///
48 54
  /// \tparam GR The digraph type the algorithm runs on.
49
  /// The default value is \c ListDigraph.
50
  /// \tparam LEN The type of the length (cost) map.
51
  /// The default value is <tt>Digraph::ArcMap<int></tt>.
55
  /// \tparam LEN The type of the length map.
56
  /// The default value is <tt>GR::ArcMap<int></tt>.
52 57
  ///
53 58
  /// \warning Length values should be \e non-negative \e integers.
54 59
  ///
55 60
  /// \note For finding node-disjoint paths this algorithm can be used
56
  /// with \ref SplitNodes.
61
  /// along with the \ref SplitNodes adaptor.
57 62
#ifdef DOXYGEN
58 63
  template <typename GR, typename LEN>
59 64
#else
60
  template < typename GR = ListDigraph,
65
  template < typename GR,
61 66
             typename LEN = typename GR::template ArcMap<int> >
62 67
#endif
63 68
  class Suurballe
64 69
  {
65 70
    TEMPLATE_DIGRAPH_TYPEDEFS(GR);
66 71

	
67 72
    typedef ConstMap<Arc, int> ConstArcMap;
68 73
    typedef typename GR::template NodeMap<Arc> PredMap;
69 74

	
70 75
  public:
71 76

	
72 77
    /// The type of the digraph the algorithm runs on.
73 78
    typedef GR Digraph;
74 79
    /// The type of the length map.
75 80
    typedef LEN LengthMap;
76 81
    /// The type of the lengths.
77 82
    typedef typename LengthMap::Value Length;
83
#ifdef DOXYGEN
84
    /// The type of the flow map.
85
    typedef GR::ArcMap<int> FlowMap;
86
    /// The type of the potential map.
87
    typedef GR::NodeMap<Length> PotentialMap;
88
#else
78 89
    /// The type of the flow map.
79 90
    typedef typename Digraph::template ArcMap<int> FlowMap;
80 91
    /// The type of the potential map.
81 92
    typedef typename Digraph::template NodeMap<Length> PotentialMap;
93
#endif
94

	
82 95
    /// The type of the path structures.
83
    typedef SimplePath<Digraph> Path;
96
    typedef SimplePath<GR> Path;
84 97

	
85 98
  private:
86 99

	
87
    /// \brief Special implementation of the Dijkstra algorithm
88
    /// for finding shortest paths in the residual network.
89
    ///
90
    /// \ref ResidualDijkstra is a special implementation of the
91
    /// \ref Dijkstra algorithm for finding shortest paths in the
92
    /// residual network of the digraph with respect to the reduced arc
93
    /// lengths and modifying the node potentials according to the
94
    /// distance of the nodes.
100
    // ResidualDijkstra is a special implementation of the
101
    // Dijkstra algorithm for finding shortest paths in the
102
    // residual network with respect to the reduced arc lengths
103
    // and modifying the node potentials according to the
104
    // distance of the nodes.
95 105
    class ResidualDijkstra
96 106
    {
97 107
      typedef typename Digraph::template NodeMap<int> HeapCrossRef;
98 108
      typedef BinHeap<Length, HeapCrossRef> Heap;
99 109

	
100 110
    private:
101 111

	
102 112
      // The digraph the algorithm runs on
103 113
      const Digraph &_graph;
104 114

	
105 115
      // The main maps
106 116
      const FlowMap &_flow;
107 117
      const LengthMap &_length;
108 118
      PotentialMap &_potential;
109 119

	
110 120
      // The distance map
111 121
      PotentialMap _dist;
112 122
      // The pred arc map
113 123
      PredMap &_pred;
114 124
      // The processed (i.e. permanently labeled) nodes
115 125
      std::vector<Node> _proc_nodes;
116 126

	
117 127
      Node _s;
118 128
      Node _t;
119 129

	
120 130
    public:
121 131

	
122 132
      /// Constructor.
123
      ResidualDijkstra( const Digraph &digraph,
133
      ResidualDijkstra( const Digraph &graph,
124 134
                        const FlowMap &flow,
125 135
                        const LengthMap &length,
126 136
                        PotentialMap &potential,
127 137
                        PredMap &pred,
128 138
                        Node s, Node t ) :
129
        _graph(digraph), _flow(flow), _length(length), _potential(potential),
130
        _dist(digraph), _pred(pred), _s(s), _t(t) {}
139
        _graph(graph), _flow(flow), _length(length), _potential(potential),
140
        _dist(graph), _pred(pred), _s(s), _t(t) {}
131 141

	
132 142
      /// \brief Run the algorithm. It returns \c true if a path is found
133 143
      /// from the source node to the target node.
134 144
      bool run() {
135 145
        HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
136 146
        Heap heap(heap_cross_ref);
137 147
        heap.push(_s, 0);
138 148
        _pred[_s] = INVALID;
139 149
        _proc_nodes.clear();
140 150

	
141 151
        // Process nodes
142 152
        while (!heap.empty() && heap.top() != _t) {
143 153
          Node u = heap.top(), v;
144 154
          Length d = heap.prio() + _potential[u], nd;
145 155
          _dist[u] = heap.prio();
146 156
          heap.pop();
147 157
          _proc_nodes.push_back(u);
148 158

	
149 159
          // Traverse outgoing arcs
150 160
          for (OutArcIt e(_graph, u); e != INVALID; ++e) {
151 161
            if (_flow[e] == 0) {
152 162
              v = _graph.target(e);
153 163
              switch(heap.state(v)) {
154 164
              case Heap::PRE_HEAP:
... ...
@@ -215,294 +225,311 @@
215 225
    PotentialMap *_potential;
216 226
    bool _local_potential;
217 227

	
218 228
    // The source node
219 229
    Node _source;
220 230
    // The target node
221 231
    Node _target;
222 232

	
223 233
    // Container to store the found paths
224 234
    std::vector< SimplePath<Digraph> > paths;
225 235
    int _path_num;
226 236

	
227 237
    // The pred arc map
228 238
    PredMap _pred;
229 239
    // Implementation of the Dijkstra algorithm for finding augmenting
230 240
    // shortest paths in the residual network
231 241
    ResidualDijkstra *_dijkstra;
232 242

	
233 243
  public:
234 244

	
235 245
    /// \brief Constructor.
236 246
    ///
237 247
    /// Constructor.
238 248
    ///
239
    /// \param digraph The digraph the algorithm runs on.
249
    /// \param graph The digraph the algorithm runs on.
240 250
    /// \param length The length (cost) values of the arcs.
241
    /// \param s The source node.
242
    /// \param t The target node.
243
    Suurballe( const Digraph &digraph,
244
               const LengthMap &length,
245
               Node s, Node t ) :
246
      _graph(digraph), _length(length), _flow(0), _local_flow(false),
247
      _potential(0), _local_potential(false), _source(s), _target(t),
248
      _pred(digraph) {}
251
    Suurballe( const Digraph &graph,
252
               const LengthMap &length ) :
253
      _graph(graph), _length(length), _flow(0), _local_flow(false),
254
      _potential(0), _local_potential(false), _pred(graph)
255
    {
256
      LEMON_ASSERT(std::numeric_limits<Length>::is_integer,
257
        "The length type of Suurballe must be integer");
258
    }
249 259

	
250 260
    /// Destructor.
251 261
    ~Suurballe() {
252 262
      if (_local_flow) delete _flow;
253 263
      if (_local_potential) delete _potential;
254 264
      delete _dijkstra;
255 265
    }
256 266

	
257 267
    /// \brief Set the flow map.
258 268
    ///
259 269
    /// This function sets the flow map.
270
    /// If it is not used before calling \ref run() or \ref init(),
271
    /// an instance will be allocated automatically. The destructor
272
    /// deallocates this automatically allocated map, of course.
260 273
    ///
261
    /// The found flow contains only 0 and 1 values. It is the union of
262
    /// the found arc-disjoint paths.
274
    /// The found flow contains only 0 and 1 values, since it is the
275
    /// union of the found arc-disjoint paths.
263 276
    ///
264 277
    /// \return <tt>(*this)</tt>
265 278
    Suurballe& flowMap(FlowMap &map) {
266 279
      if (_local_flow) {
267 280
        delete _flow;
268 281
        _local_flow = false;
269 282
      }
270 283
      _flow = &map;
271 284
      return *this;
272 285
    }
273 286

	
274 287
    /// \brief Set the potential map.
275 288
    ///
276 289
    /// This function sets the potential map.
290
    /// If it is not used before calling \ref run() or \ref init(),
291
    /// an instance will be allocated automatically. The destructor
292
    /// deallocates this automatically allocated map, of course.
277 293
    ///
278
    /// The potentials provide the dual solution of the underlying
279
    /// minimum cost flow problem.
294
    /// The node potentials provide the dual solution of the underlying
295
    /// \ref min_cost_flow "minimum cost flow problem".
280 296
    ///
281 297
    /// \return <tt>(*this)</tt>
282 298
    Suurballe& potentialMap(PotentialMap &map) {
283 299
      if (_local_potential) {
284 300
        delete _potential;
285 301
        _local_potential = false;
286 302
      }
287 303
      _potential = &map;
288 304
      return *this;
289 305
    }
290 306

	
291 307
    /// \name Execution Control
292 308
    /// The simplest way to execute the algorithm is to call the run()
293 309
    /// function.
294 310
    /// \n
295 311
    /// If you only need the flow that is the union of the found
296 312
    /// arc-disjoint paths, you may call init() and findFlow().
297 313

	
298 314
    /// @{
299 315

	
300 316
    /// \brief Run the algorithm.
301 317
    ///
302 318
    /// This function runs the algorithm.
303 319
    ///
320
    /// \param s The source node.
321
    /// \param t The target node.
304 322
    /// \param k The number of paths to be found.
305 323
    ///
306 324
    /// \return \c k if there are at least \c k arc-disjoint paths from
307 325
    /// \c s to \c t in the digraph. Otherwise it returns the number of
308 326
    /// arc-disjoint paths found.
309 327
    ///
310
    /// \note Apart from the return value, <tt>s.run(k)</tt> is just a
311
    /// shortcut of the following code.
328
    /// \note Apart from the return value, <tt>s.run(s, t, k)</tt> is
329
    /// just a shortcut of the following code.
312 330
    /// \code
313
    ///   s.init();
314
    ///   s.findFlow(k);
331
    ///   s.init(s);
332
    ///   s.findFlow(t, k);
315 333
    ///   s.findPaths();
316 334
    /// \endcode
317
    int run(int k = 2) {
318
      init();
319
      findFlow(k);
335
    int run(const Node& s, const Node& t, int k = 2) {
336
      init(s);
337
      findFlow(t, k);
320 338
      findPaths();
321 339
      return _path_num;
322 340
    }
323 341

	
324 342
    /// \brief Initialize the algorithm.
325 343
    ///
326 344
    /// This function initializes the algorithm.
327
    void init() {
345
    ///
346
    /// \param s The source node.
347
    void init(const Node& s) {
348
      _source = s;
349

	
328 350
      // Initialize maps
329 351
      if (!_flow) {
330 352
        _flow = new FlowMap(_graph);
331 353
        _local_flow = true;
332 354
      }
333 355
      if (!_potential) {
334 356
        _potential = new PotentialMap(_graph);
335 357
        _local_potential = true;
336 358
      }
337 359
      for (ArcIt e(_graph); e != INVALID; ++e) (*_flow)[e] = 0;
338 360
      for (NodeIt n(_graph); n != INVALID; ++n) (*_potential)[n] = 0;
339

	
340
      _dijkstra = new ResidualDijkstra( _graph, *_flow, _length,
341
                                        *_potential, _pred,
342
                                        _source, _target );
343 361
    }
344 362

	
345
    /// \brief Execute the successive shortest path algorithm to find
346
    /// an optimal flow.
363
    /// \brief Execute the algorithm to find an optimal flow.
347 364
    ///
348 365
    /// This function executes the successive shortest path algorithm to
349
    /// find a minimum cost flow, which is the union of \c k or less
366
    /// find a minimum cost flow, which is the union of \c k (or less)
350 367
    /// arc-disjoint paths.
351 368
    ///
369
    /// \param t The target node.
370
    /// \param k The number of paths to be found.
371
    ///
352 372
    /// \return \c k if there are at least \c k arc-disjoint paths from
353
    /// \c s to \c t in the digraph. Otherwise it returns the number of
354
    /// arc-disjoint paths found.
373
    /// the source node to the given node \c t in the digraph.
374
    /// Otherwise it returns the number of arc-disjoint paths found.
355 375
    ///
356 376
    /// \pre \ref init() must be called before using this function.
357
    int findFlow(int k = 2) {
377
    int findFlow(const Node& t, int k = 2) {
378
      _target = t;
379
      _dijkstra =
380
        new ResidualDijkstra( _graph, *_flow, _length, *_potential, _pred,
381
                              _source, _target );
382

	
358 383
      // Find shortest paths
359 384
      _path_num = 0;
360 385
      while (_path_num < k) {
361 386
        // Run Dijkstra
362 387
        if (!_dijkstra->run()) break;
363 388
        ++_path_num;
364 389

	
365 390
        // Set the flow along the found shortest path
366 391
        Node u = _target;
367 392
        Arc e;
368 393
        while ((e = _pred[u]) != INVALID) {
369 394
          if (u == _graph.target(e)) {
370 395
            (*_flow)[e] = 1;
371 396
            u = _graph.source(e);
372 397
          } else {
373 398
            (*_flow)[e] = 0;
374 399
            u = _graph.target(e);
375 400
          }
376 401
        }
377 402
      }
378 403
      return _path_num;
379 404
    }
380 405

	
381 406
    /// \brief Compute the paths from the flow.
382 407
    ///
383
    /// This function computes the paths from the flow.
408
    /// This function computes the paths from the found minimum cost flow,
409
    /// which is the union of some arc-disjoint paths.
384 410
    ///
385 411
    /// \pre \ref init() and \ref findFlow() must be called before using
386 412
    /// this function.
387 413
    void findPaths() {
388
      // Create the residual flow map (the union of the paths not found
389
      // so far)
390 414
      FlowMap res_flow(_graph);
391 415
      for(ArcIt a(_graph); a != INVALID; ++a) res_flow[a] = (*_flow)[a];
392 416

	
393 417
      paths.clear();
394 418
      paths.resize(_path_num);
395 419
      for (int i = 0; i < _path_num; ++i) {
396 420
        Node n = _source;
397 421
        while (n != _target) {
398 422
          OutArcIt e(_graph, n);
399 423
          for ( ; res_flow[e] == 0; ++e) ;
400 424
          n = _graph.target(e);
401 425
          paths[i].addBack(e);
402 426
          res_flow[e] = 0;
403 427
        }
404 428
      }
405 429
    }
406 430

	
407 431
    /// @}
408 432

	
409 433
    /// \name Query Functions
410 434
    /// The results of the algorithm can be obtained using these
411 435
    /// functions.
412 436
    /// \n The algorithm should be executed before using them.
413 437

	
414 438
    /// @{
415 439

	
416
    /// \brief Return a const reference to the arc map storing the
440
    /// \brief Return the total length of the found paths.
441
    ///
442
    /// This function returns the total length of the found paths, i.e.
443
    /// the total cost of the found flow.
444
    /// The complexity of the function is O(e).
445
    ///
446
    /// \pre \ref run() or \ref findFlow() must be called before using
447
    /// this function.
448
    Length totalLength() const {
449
      Length c = 0;
450
      for (ArcIt e(_graph); e != INVALID; ++e)
451
        c += (*_flow)[e] * _length[e];
452
      return c;
453
    }
454

	
455
    /// \brief Return the flow value on the given arc.
456
    ///
457
    /// This function returns the flow value on the given arc.
458
    /// It is \c 1 if the arc is involved in one of the found arc-disjoint
459
    /// paths, otherwise it is \c 0.
460
    ///
461
    /// \pre \ref run() or \ref findFlow() must be called before using
462
    /// this function.
463
    int flow(const Arc& arc) const {
464
      return (*_flow)[arc];
465
    }
466

	
467
    /// \brief Return a const reference to an arc map storing the
417 468
    /// found flow.
418 469
    ///
419
    /// This function returns a const reference to the arc map storing
470
    /// This function returns a const reference to an arc map storing
420 471
    /// the flow that is the union of the found arc-disjoint paths.
421 472
    ///
422 473
    /// \pre \ref run() or \ref findFlow() must be called before using
423 474
    /// this function.
424 475
    const FlowMap& flowMap() const {
425 476
      return *_flow;
426 477
    }
427 478

	
428
    /// \brief Return a const reference to the node map storing the
429
    /// found potentials (the dual solution).
430
    ///
431
    /// This function returns a const reference to the node map storing
432
    /// the found potentials that provide the dual solution of the
433
    /// underlying minimum cost flow problem.
434
    ///
435
    /// \pre \ref run() or \ref findFlow() must be called before using
436
    /// this function.
437
    const PotentialMap& potentialMap() const {
438
      return *_potential;
439
    }
440

	
441
    /// \brief Return the flow on the given arc.
442
    ///
443
    /// This function returns the flow on the given arc.
444
    /// It is \c 1 if the arc is involved in one of the found paths,
445
    /// otherwise it is \c 0.
446
    ///
447
    /// \pre \ref run() or \ref findFlow() must be called before using
448
    /// this function.
449
    int flow(const Arc& arc) const {
450
      return (*_flow)[arc];
451
    }
452

	
453 479
    /// \brief Return the potential of the given node.
454 480
    ///
455 481
    /// This function returns the potential of the given node.
482
    /// The node potentials provide the dual solution of the
483
    /// underlying \ref min_cost_flow "minimum cost flow problem".
456 484
    ///
457 485
    /// \pre \ref run() or \ref findFlow() must be called before using
458 486
    /// this function.
459 487
    Length potential(const Node& node) const {
460 488
      return (*_potential)[node];
461 489
    }
462 490

	
463
    /// \brief Return the total length (cost) of the found paths (flow).
491
    /// \brief Return a const reference to a node map storing the
492
    /// found potentials (the dual solution).
464 493
    ///
465
    /// This function returns the total length (cost) of the found paths
466
    /// (flow). The complexity of the function is O(e).
494
    /// This function returns a const reference to a node map storing
495
    /// the found potentials that provide the dual solution of the
496
    /// underlying \ref min_cost_flow "minimum cost flow problem".
467 497
    ///
468 498
    /// \pre \ref run() or \ref findFlow() must be called before using
469 499
    /// this function.
470
    Length totalLength() const {
471
      Length c = 0;
472
      for (ArcIt e(_graph); e != INVALID; ++e)
473
        c += (*_flow)[e] * _length[e];
474
      return c;
500
    const PotentialMap& potentialMap() const {
501
      return *_potential;
475 502
    }
476 503

	
477 504
    /// \brief Return the number of the found paths.
478 505
    ///
479 506
    /// This function returns the number of the found paths.
480 507
    ///
481 508
    /// \pre \ref run() or \ref findFlow() must be called before using
482 509
    /// this function.
483 510
    int pathNum() const {
484 511
      return _path_num;
485 512
    }
486 513

	
487 514
    /// \brief Return a const reference to the specified path.
488 515
    ///
489 516
    /// This function returns a const reference to the specified path.
490 517
    ///
491
    /// \param i The function returns the \c i-th path.
518
    /// \param i The function returns the <tt>i</tt>-th path.
492 519
    /// \c i must be between \c 0 and <tt>%pathNum()-1</tt>.
493 520
    ///
494 521
    /// \pre \ref run() or \ref findPaths() must be called before using
495 522
    /// this function.
496 523
    Path path(int i) const {
497 524
      return paths[i];
498 525
    }
499 526

	
500 527
    /// @}
501 528

	
502 529
  }; //class Suurballe
503 530

	
504 531
  ///@}
505 532

	
506 533
} //namespace lemon
507 534

	
508 535
#endif //LEMON_SUURBALLE_H
Ignore white space 48 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#include <iostream>
20 20

	
21 21
#include <lemon/list_graph.h>
22 22
#include <lemon/lgf_reader.h>
23 23
#include <lemon/path.h>
24 24
#include <lemon/suurballe.h>
25
#include <lemon/concepts/digraph.h>
25 26

	
26 27
#include "test_tools.h"
27 28

	
28 29
using namespace lemon;
29 30

	
30 31
char test_lgf[] =
31 32
  "@nodes\n"
32
  "label supply1 supply2 supply3\n"
33
  "1     0        20      27\n"
34
  "2     0       -4        0\n"
35
  "3     0        0        0\n"
36
  "4     0        0        0\n"
37
  "5     0        9        0\n"
38
  "6     0       -6        0\n"
39
  "7     0        0        0\n"
40
  "8     0        0        0\n"
41
  "9     0        3        0\n"
42
  "10    0       -2        0\n"
43
  "11    0        0        0\n"
44
  "12    0       -20     -27\n"
33
  "label\n"
34
  "1\n"
35
  "2\n"
36
  "3\n"
37
  "4\n"
38
  "5\n"
39
  "6\n"
40
  "7\n"
41
  "8\n"
42
  "9\n"
43
  "10\n"
44
  "11\n"
45
  "12\n"
45 46
  "@arcs\n"
46
  "      cost capacity lower1 lower2\n"
47
  " 1  2  70  11       0      8\n"
48
  " 1  3 150   3       0      1\n"
49
  " 1  4  80  15       0      2\n"
50
  " 2  8  80  12       0      0\n"
51
  " 3  5 140   5       0      3\n"
52
  " 4  6  60  10       0      1\n"
53
  " 4  7  80   2       0      0\n"
54
  " 4  8 110   3       0      0\n"
55
  " 5  7  60  14       0      0\n"
56
  " 5 11 120  12       0      0\n"
57
  " 6  3   0   3       0      0\n"
58
  " 6  9 140   4       0      0\n"
59
  " 6 10  90   8       0      0\n"
60
  " 7  1  30   5       0      0\n"
61
  " 8 12  60  16       0      4\n"
62
  " 9 12  50   6       0      0\n"
63
  "10 12  70  13       0      5\n"
64
  "10  2 100   7       0      0\n"
65
  "10  7  60  10       0      0\n"
66
  "11 10  20  14       0      6\n"
67
  "12 11  30  10       0      0\n"
47
  "      length\n"
48
  " 1  2  70\n"
49
  " 1  3 150\n"
50
  " 1  4  80\n"
51
  " 2  8  80\n"
52
  " 3  5 140\n"
53
  " 4  6  60\n"
54
  " 4  7  80\n"
55
  " 4  8 110\n"
56
  " 5  7  60\n"
57
  " 5 11 120\n"
58
  " 6  3   0\n"
59
  " 6  9 140\n"
60
  " 6 10  90\n"
61
  " 7  1  30\n"
62
  " 8 12  60\n"
63
  " 9 12  50\n"
64
  "10 12  70\n"
65
  "10  2 100\n"
66
  "10  7  60\n"
67
  "11 10  20\n"
68
  "12 11  30\n"
68 69
  "@attributes\n"
69 70
  "source  1\n"
70 71
  "target 12\n"
71 72
  "@end\n";
72 73

	
74
// Check the interface of Suurballe
75
void checkSuurballeCompile()
76
{
77
  typedef int VType;
78
  typedef concepts::Digraph Digraph;
79

	
80
  typedef Digraph::Node Node;
81
  typedef Digraph::Arc Arc;
82
  typedef concepts::ReadMap<Arc, VType> LengthMap;
83
  
84
  typedef Suurballe<Digraph, LengthMap> SuurballeType;
85

	
86
  Digraph g;
87
  Node n;
88
  Arc e;
89
  LengthMap len;
90
  SuurballeType::FlowMap flow(g);
91
  SuurballeType::PotentialMap pi(g);
92

	
93
  SuurballeType suurb_test(g, len);
94
  const SuurballeType& const_suurb_test = suurb_test;
95

	
96
  suurb_test
97
    .flowMap(flow)
98
    .potentialMap(pi);
99

	
100
  int k;
101
  k = suurb_test.run(n, n);
102
  k = suurb_test.run(n, n, k);
103
  suurb_test.init(n);
104
  k = suurb_test.findFlow(n);
105
  k = suurb_test.findFlow(n, k);
106
  suurb_test.findPaths();
107
  
108
  int f;
109
  VType c;
110
  c = const_suurb_test.totalLength();
111
  f = const_suurb_test.flow(e);
112
  const SuurballeType::FlowMap& fm =
113
    const_suurb_test.flowMap();
114
  c = const_suurb_test.potential(n);
115
  const SuurballeType::PotentialMap& pm =
116
    const_suurb_test.potentialMap();
117
  k = const_suurb_test.pathNum();
118
  Path<Digraph> p = const_suurb_test.path(k);
119
  
120
  ignore_unused_variable_warning(fm);
121
  ignore_unused_variable_warning(pm);
122
}
123

	
73 124
// Check the feasibility of the flow
74 125
template <typename Digraph, typename FlowMap>
75 126
bool checkFlow( const Digraph& gr, const FlowMap& flow,
76 127
                typename Digraph::Node s, typename Digraph::Node t,
77 128
                int value )
78 129
{
79 130
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
80 131
  for (ArcIt e(gr); e != INVALID; ++e)
81 132
    if (!(flow[e] == 0 || flow[e] == 1)) return false;
82 133

	
83 134
  for (NodeIt n(gr); n != INVALID; ++n) {
84 135
    int sum = 0;
85 136
    for (OutArcIt e(gr, n); e != INVALID; ++e)
86 137
      sum += flow[e];
87 138
    for (InArcIt e(gr, n); e != INVALID; ++e)
88 139
      sum -= flow[e];
89 140
    if (n == s && sum != value) return false;
90 141
    if (n == t && sum != -value) return false;
91 142
    if (n != s && n != t && sum != 0) return false;
92 143
  }
93 144

	
94 145
  return true;
95 146
}
96 147

	
97 148
// Check the optimalitiy of the flow
98 149
template < typename Digraph, typename CostMap,
99 150
           typename FlowMap, typename PotentialMap >
100 151
bool checkOptimality( const Digraph& gr, const CostMap& cost,
101 152
                      const FlowMap& flow, const PotentialMap& pi )
102 153
{
103 154
  // Check the "Complementary Slackness" optimality condition
104 155
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
105 156
  bool opt = true;
106 157
  for (ArcIt e(gr); e != INVALID; ++e) {
107 158
    typename CostMap::Value red_cost =
108 159
      cost[e] + pi[gr.source(e)] - pi[gr.target(e)];
109 160
    opt = (flow[e] == 0 && red_cost >= 0) ||
110 161
          (flow[e] == 1 && red_cost <= 0);
111 162
    if (!opt) break;
112 163
  }
113 164
  return opt;
114 165
}
115 166

	
116 167
// Check a path
117 168
template <typename Digraph, typename Path>
118 169
bool checkPath( const Digraph& gr, const Path& path,
119 170
                typename Digraph::Node s, typename Digraph::Node t)
120 171
{
121
  // Check the "Complementary Slackness" optimality condition
122 172
  TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
123 173
  Node n = s;
124 174
  for (int i = 0; i < path.length(); ++i) {
125 175
    if (gr.source(path.nth(i)) != n) return false;
126 176
    n = gr.target(path.nth(i));
127 177
  }
128 178
  return n == t;
129 179
}
130 180

	
131 181

	
132 182
int main()
133 183
{
134 184
  DIGRAPH_TYPEDEFS(ListDigraph);
135 185

	
136 186
  // Read the test digraph
137 187
  ListDigraph digraph;
138 188
  ListDigraph::ArcMap<int> length(digraph);
139
  Node source, target;
189
  Node s, t;
140 190

	
141 191
  std::istringstream input(test_lgf);
142 192
  DigraphReader<ListDigraph>(digraph, input).
143
    arcMap("cost", length).
144
    node("source", source).
145
    node("target", target).
193
    arcMap("length", length).
194
    node("source", s).
195
    node("target", t).
146 196
    run();
147 197

	
148 198
  // Find 2 paths
149 199
  {
150
    Suurballe<ListDigraph> suurballe(digraph, length, source, target);
151
    check(suurballe.run(2) == 2, "Wrong number of paths");
152
    check(checkFlow(digraph, suurballe.flowMap(), source, target, 2),
200
    Suurballe<ListDigraph> suurballe(digraph, length);
201
    check(suurballe.run(s, t) == 2, "Wrong number of paths");
202
    check(checkFlow(digraph, suurballe.flowMap(), s, t, 2),
153 203
          "The flow is not feasible");
154 204
    check(suurballe.totalLength() == 510, "The flow is not optimal");
155 205
    check(checkOptimality(digraph, length, suurballe.flowMap(),
156 206
                          suurballe.potentialMap()),
157 207
          "Wrong potentials");
158 208
    for (int i = 0; i < suurballe.pathNum(); ++i)
159
      check(checkPath(digraph, suurballe.path(i), source, target),
160
            "Wrong path");
209
      check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
161 210
  }
162 211

	
163 212
  // Find 3 paths
164 213
  {
165
    Suurballe<ListDigraph> suurballe(digraph, length, source, target);
166
    check(suurballe.run(3) == 3, "Wrong number of paths");
167
    check(checkFlow(digraph, suurballe.flowMap(), source, target, 3),
214
    Suurballe<ListDigraph> suurballe(digraph, length);
215
    check(suurballe.run(s, t, 3) == 3, "Wrong number of paths");
216
    check(checkFlow(digraph, suurballe.flowMap(), s, t, 3),
168 217
          "The flow is not feasible");
169 218
    check(suurballe.totalLength() == 1040, "The flow is not optimal");
170 219
    check(checkOptimality(digraph, length, suurballe.flowMap(),
171 220
                          suurballe.potentialMap()),
172 221
          "Wrong potentials");
173 222
    for (int i = 0; i < suurballe.pathNum(); ++i)
174
      check(checkPath(digraph, suurballe.path(i), source, target),
175
            "Wrong path");
223
      check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
176 224
  }
177 225

	
178 226
  // Find 5 paths (only 3 can be found)
179 227
  {
180
    Suurballe<ListDigraph> suurballe(digraph, length, source, target);
181
    check(suurballe.run(5) == 3, "Wrong number of paths");
182
    check(checkFlow(digraph, suurballe.flowMap(), source, target, 3),
228
    Suurballe<ListDigraph> suurballe(digraph, length);
229
    check(suurballe.run(s, t, 5) == 3, "Wrong number of paths");
230
    check(checkFlow(digraph, suurballe.flowMap(), s, t, 3),
183 231
          "The flow is not feasible");
184 232
    check(suurballe.totalLength() == 1040, "The flow is not optimal");
185 233
    check(checkOptimality(digraph, length, suurballe.flowMap(),
186 234
                          suurballe.potentialMap()),
187 235
          "Wrong potentials");
188 236
    for (int i = 0; i < suurballe.pathNum(); ++i)
189
      check(checkPath(digraph, suurballe.path(i), source, target),
190
            "Wrong path");
237
      check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path");
191 238
  }
192 239

	
193 240
  return 0;
194 241
}
Ignore white space 48 line context
... ...
@@ -459,82 +459,81 @@
459 459
  Bfs<ListGraph> bfs(g);
460 460
  for(std::vector<Edge>::reverse_iterator ei=arcs.rbegin();
461 461
      ei!=arcs.rend();++ei)
462 462
    {
463 463
      Node a=g.u(*ei);
464 464
      Node b=g.v(*ei);
465 465
      g.erase(*ei);
466 466
      bfs.run(a,b);
467 467
      if(bfs.predArc(b)==INVALID || bfs.dist(b)>d)
468 468
        g.addEdge(a,b);
469 469
      else cnt++;
470 470
    }
471 471
}
472 472

	
473 473
void sparse2(int d)
474 474
{
475 475
  Counter cnt("Number of arcs removed: ");
476 476
  for(std::vector<Edge>::reverse_iterator ei=arcs.rbegin();
477 477
      ei!=arcs.rend();++ei)
478 478
    {
479 479
      Node a=g.u(*ei);
480 480
      Node b=g.v(*ei);
481 481
      g.erase(*ei);
482 482
      ConstMap<Arc,int> cegy(1);
483
      Suurballe<ListGraph,ConstMap<Arc,int> > sur(g,cegy,a,b);
484
      int k=sur.run(2);
483
      Suurballe<ListGraph,ConstMap<Arc,int> > sur(g,cegy);
484
      int k=sur.run(a,b,2);
485 485
      if(k<2 || sur.totalLength()>d)
486 486
        g.addEdge(a,b);
487 487
      else cnt++;
488 488
//       else std::cout << "Remove arc " << g.id(a) << "-" << g.id(b) << '\n';
489 489
    }
490 490
}
491 491

	
492 492
void sparseTriangle(int d)
493 493
{
494 494
  Counter cnt("Number of arcs added: ");
495 495
  std::vector<Parc> pedges;
496 496
  for(NodeIt n(g);n!=INVALID;++n)
497 497
    for(NodeIt m=++(NodeIt(n));m!=INVALID;++m)
498 498
      {
499 499
        Parc p;
500 500
        p.a=n;
501 501
        p.b=m;
502 502
        p.len=(coords[m]-coords[n]).normSquare();
503 503
        pedges.push_back(p);
504 504
      }
505 505
  std::sort(pedges.begin(),pedges.end(),pedgeLess);
506 506
  for(std::vector<Parc>::iterator pi=pedges.begin();pi!=pedges.end();++pi)
507 507
    {
508 508
      Line li(pi->a,pi->b);
509 509
      EdgeIt e(g);
510 510
      for(;e!=INVALID && !cross(e,li);++e) ;
511 511
      Edge ne;
512 512
      if(e==INVALID) {
513 513
        ConstMap<Arc,int> cegy(1);
514
        Suurballe<ListGraph,ConstMap<Arc,int> >
515
          sur(g,cegy,pi->a,pi->b);
516
        int k=sur.run(2);
514
        Suurballe<ListGraph,ConstMap<Arc,int> > sur(g,cegy);
515
        int k=sur.run(pi->a,pi->b,2);
517 516
        if(k<2 || sur.totalLength()>d)
518 517
          {
519 518
            ne=g.addEdge(pi->a,pi->b);
520 519
            arcs.push_back(ne);
521 520
            cnt++;
522 521
          }
523 522
      }
524 523
    }
525 524
}
526 525

	
527 526
template <typename Graph, typename CoordMap>
528 527
class LengthSquareMap {
529 528
public:
530 529
  typedef typename Graph::Edge Key;
531 530
  typedef typename CoordMap::Value::Value Value;
532 531

	
533 532
  LengthSquareMap(const Graph& graph, const CoordMap& coords)
534 533
    : _graph(graph), _coords(coords) {}
535 534

	
536 535
  Value operator[](const Key& key) const {
537 536
    return (_coords[_graph.v(key)] -
538 537
            _coords[_graph.u(key)]).normSquare();
539 538
  }
540 539

	
0 comments (0 inline)