lemon/concepts/path.h
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 13 Nov 2009 00:10:33 +0100
changeset 815 aef153f430e1
parent 559 c5fd2d996909
child 976 426a704d7483
permissions -rw-r--r--
Entirely rework cycle canceling algorithms (#180)

- Move the cycle canceling algorithms (CycleCanceling, CancelAndTighten)
into one class (CycleCanceling).
- Add a Method parameter to the run() function to be able to select
the used cycle canceling method.
- Use the new interface similarly to NetworkSimplex.
- Rework the implementations using an efficient internal structure
for handling the residual network.
This improvement made the codes much faster.
- Handle GEQ supply type (LEQ is not supported).
- Handle infinite upper bounds.
- Handle negative costs (for arcs of finite upper bound).
- Extend the documentation.
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@96
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@96
     4
 *
alpar@440
     5
 * Copyright (C) 2003-2009
alpar@96
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@96
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@96
     8
 *
alpar@96
     9
 * Permission to use, modify and distribute this software is granted
alpar@96
    10
 * provided that this copyright notice appears in all copies. For
alpar@96
    11
 * precise terms see the accompanying LICENSE file.
alpar@96
    12
 *
alpar@96
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@96
    14
 * express or implied, and with no claim as to its suitability for any
alpar@96
    15
 * purpose.
alpar@96
    16
 *
alpar@96
    17
 */
alpar@96
    18
alpar@96
    19
///\ingroup concept
alpar@96
    20
///\file
kpeter@785
    21
///\brief The concept of paths
alpar@96
    22
///
alpar@96
    23
deba@529
    24
#ifndef LEMON_CONCEPTS_PATH_H
deba@529
    25
#define LEMON_CONCEPTS_PATH_H
alpar@96
    26
deba@220
    27
#include <lemon/core.h>
alpar@96
    28
#include <lemon/concept_check.h>
alpar@96
    29
alpar@96
    30
namespace lemon {
alpar@96
    31
  namespace concepts {
alpar@96
    32
alpar@96
    33
    /// \addtogroup concept
alpar@96
    34
    /// @{
alpar@96
    35
alpar@96
    36
    /// \brief A skeleton structure for representing directed paths in
alpar@96
    37
    /// a digraph.
alpar@96
    38
    ///
alpar@96
    39
    /// A skeleton structure for representing directed paths in a
alpar@209
    40
    /// digraph.
kpeter@785
    41
    /// In a sense, a path can be treated as a list of arcs.
kpeter@785
    42
    /// LEMON path types just store this list. As a consequence, they cannot
kpeter@785
    43
    /// enumerate the nodes on the path directly and a zero length path
kpeter@785
    44
    /// cannot store its source node.
kpeter@785
    45
    ///
kpeter@785
    46
    /// The arcs of a path should be stored in the order of their directions,
kpeter@785
    47
    /// i.e. the target node of each arc should be the same as the source
kpeter@785
    48
    /// node of the next arc. This consistency could be checked using
kpeter@785
    49
    /// \ref checkPath().
kpeter@785
    50
    /// The source and target nodes of a (consistent) path can be obtained
kpeter@785
    51
    /// using \ref pathSource() and \ref pathTarget().
kpeter@785
    52
    ///
kpeter@785
    53
    /// A path can be constructed from another path of any type using the
kpeter@785
    54
    /// copy constructor or the assignment operator.
kpeter@785
    55
    ///
kpeter@559
    56
    /// \tparam GR The digraph type in which the path is.
kpeter@559
    57
    template <typename GR>
alpar@96
    58
    class Path {
alpar@96
    59
    public:
alpar@96
    60
alpar@96
    61
      /// Type of the underlying digraph.
kpeter@559
    62
      typedef GR Digraph;
alpar@96
    63
      /// Arc type of the underlying digraph.
alpar@96
    64
      typedef typename Digraph::Arc Arc;
alpar@96
    65
alpar@96
    66
      class ArcIt;
alpar@96
    67
alpar@96
    68
      /// \brief Default constructor
alpar@96
    69
      Path() {}
alpar@96
    70
kpeter@785
    71
      /// \brief Template copy constructor
alpar@96
    72
      template <typename CPath>
alpar@96
    73
      Path(const CPath& cpath) {}
alpar@96
    74
kpeter@785
    75
      /// \brief Template assigment operator
alpar@96
    76
      template <typename CPath>
kpeter@278
    77
      Path& operator=(const CPath& cpath) {
kpeter@278
    78
        ignore_unused_variable_warning(cpath);
kpeter@278
    79
        return *this;
kpeter@278
    80
      }
alpar@96
    81
kpeter@785
    82
      /// Length of the path, i.e. the number of arcs on the path.
alpar@96
    83
      int length() const { return 0;}
alpar@96
    84
alpar@96
    85
      /// Returns whether the path is empty.
alpar@96
    86
      bool empty() const { return true;}
alpar@96
    87
alpar@96
    88
      /// Resets the path to an empty path.
alpar@96
    89
      void clear() {}
alpar@96
    90
kpeter@785
    91
      /// \brief LEMON style iterator for enumerating the arcs of a path.
alpar@96
    92
      ///
kpeter@785
    93
      /// LEMON style iterator class for enumerating the arcs of a path.
alpar@96
    94
      class ArcIt {
alpar@96
    95
      public:
alpar@209
    96
        /// Default constructor
alpar@209
    97
        ArcIt() {}
alpar@209
    98
        /// Invalid constructor
alpar@209
    99
        ArcIt(Invalid) {}
kpeter@785
   100
        /// Sets the iterator to the first arc of the given path
alpar@209
   101
        ArcIt(const Path &) {}
alpar@96
   102
kpeter@785
   103
        /// Conversion to \c Arc
alpar@209
   104
        operator Arc() const { return INVALID; }
alpar@96
   105
alpar@209
   106
        /// Next arc
alpar@209
   107
        ArcIt& operator++() {return *this;}
alpar@96
   108
alpar@209
   109
        /// Comparison operator
alpar@209
   110
        bool operator==(const ArcIt&) const {return true;}
alpar@209
   111
        /// Comparison operator
alpar@209
   112
        bool operator!=(const ArcIt&) const {return true;}
kpeter@212
   113
        /// Comparison operator
kpeter@212
   114
        bool operator<(const ArcIt&) const {return false;}
alpar@96
   115
alpar@96
   116
      };
alpar@96
   117
alpar@96
   118
      template <typename _Path>
alpar@96
   119
      struct Constraints {
alpar@96
   120
        void constraints() {
alpar@96
   121
          Path<Digraph> pc;
alpar@96
   122
          _Path p, pp(pc);
alpar@96
   123
          int l = p.length();
alpar@96
   124
          int e = p.empty();
alpar@96
   125
          p.clear();
alpar@96
   126
alpar@96
   127
          p = pc;
alpar@96
   128
alpar@96
   129
          typename _Path::ArcIt id, ii(INVALID), i(p);
alpar@96
   130
alpar@96
   131
          ++i;
alpar@96
   132
          typename Digraph::Arc ed = i;
alpar@96
   133
alpar@96
   134
          e = (i == ii);
alpar@96
   135
          e = (i != ii);
alpar@96
   136
          e = (i < ii);
alpar@96
   137
alpar@96
   138
          ignore_unused_variable_warning(l);
alpar@96
   139
          ignore_unused_variable_warning(pp);
alpar@96
   140
          ignore_unused_variable_warning(e);
alpar@96
   141
          ignore_unused_variable_warning(id);
alpar@96
   142
          ignore_unused_variable_warning(ii);
alpar@96
   143
          ignore_unused_variable_warning(ed);
alpar@96
   144
        }
alpar@96
   145
      };
alpar@96
   146
alpar@96
   147
    };
alpar@96
   148
alpar@96
   149
    namespace _path_bits {
alpar@209
   150
alpar@96
   151
      template <typename _Digraph, typename _Path, typename RevPathTag = void>
alpar@96
   152
      struct PathDumperConstraints {
alpar@96
   153
        void constraints() {
alpar@96
   154
          int l = p.length();
alpar@96
   155
          int e = p.empty();
alpar@96
   156
alpar@96
   157
          typename _Path::ArcIt id, i(p);
alpar@96
   158
alpar@96
   159
          ++i;
alpar@96
   160
          typename _Digraph::Arc ed = i;
alpar@96
   161
alpar@96
   162
          e = (i == INVALID);
alpar@96
   163
          e = (i != INVALID);
alpar@96
   164
alpar@96
   165
          ignore_unused_variable_warning(l);
alpar@96
   166
          ignore_unused_variable_warning(e);
alpar@96
   167
          ignore_unused_variable_warning(id);
alpar@96
   168
          ignore_unused_variable_warning(ed);
alpar@96
   169
        }
alpar@96
   170
        _Path& p;
alpar@96
   171
      };
alpar@96
   172
alpar@96
   173
      template <typename _Digraph, typename _Path>
alpar@96
   174
      struct PathDumperConstraints<
alpar@209
   175
        _Digraph, _Path,
alpar@96
   176
        typename enable_if<typename _Path::RevPathTag, void>::type
alpar@96
   177
      > {
alpar@96
   178
        void constraints() {
alpar@96
   179
          int l = p.length();
alpar@96
   180
          int e = p.empty();
alpar@96
   181
alpar@96
   182
          typename _Path::RevArcIt id, i(p);
alpar@96
   183
alpar@96
   184
          ++i;
alpar@96
   185
          typename _Digraph::Arc ed = i;
alpar@96
   186
alpar@96
   187
          e = (i == INVALID);
alpar@96
   188
          e = (i != INVALID);
alpar@96
   189
alpar@96
   190
          ignore_unused_variable_warning(l);
alpar@96
   191
          ignore_unused_variable_warning(e);
alpar@96
   192
          ignore_unused_variable_warning(id);
alpar@96
   193
          ignore_unused_variable_warning(ed);
alpar@96
   194
        }
alpar@96
   195
        _Path& p;
alpar@96
   196
      };
alpar@209
   197
alpar@96
   198
    }
alpar@96
   199
alpar@96
   200
alpar@96
   201
    /// \brief A skeleton structure for path dumpers.
alpar@96
   202
    ///
alpar@96
   203
    /// A skeleton structure for path dumpers. The path dumpers are
kpeter@785
   204
    /// the generalization of the paths, they can enumerate the arcs
kpeter@785
   205
    /// of the path either in forward or in backward order.
kpeter@785
   206
    /// These classes are typically not used directly, they are rather
kpeter@785
   207
    /// used to be assigned to a real path type.
alpar@96
   208
    ///
alpar@96
   209
    /// The main purpose of this concept is that the shortest path
kpeter@785
   210
    /// algorithms can enumerate the arcs easily in reverse order.
kpeter@785
   211
    /// In LEMON, such algorithms give back a (reverse) path dumper that
kpeter@785
   212
    /// can be assigned to a real path. The dumpers can be implemented as
alpar@96
   213
    /// an adaptor class to the predecessor map.
kpeter@559
   214
    ///
kpeter@559
   215
    /// \tparam GR The digraph type in which the path is.
kpeter@559
   216
    template <typename GR>
alpar@96
   217
    class PathDumper {
alpar@96
   218
    public:
alpar@96
   219
alpar@96
   220
      /// Type of the underlying digraph.
kpeter@559
   221
      typedef GR Digraph;
alpar@96
   222
      /// Arc type of the underlying digraph.
alpar@96
   223
      typedef typename Digraph::Arc Arc;
alpar@96
   224
kpeter@785
   225
      /// Length of the path, i.e. the number of arcs on the path.
alpar@96
   226
      int length() const { return 0;}
alpar@96
   227
alpar@96
   228
      /// Returns whether the path is empty.
alpar@96
   229
      bool empty() const { return true;}
alpar@96
   230
alpar@96
   231
      /// \brief Forward or reverse dumping
alpar@96
   232
      ///
kpeter@785
   233
      /// If this tag is defined to be \c True, then reverse dumping
kpeter@785
   234
      /// is provided in the path dumper. In this case, \c RevArcIt
kpeter@785
   235
      /// iterator should be implemented instead of \c ArcIt iterator.
alpar@96
   236
      typedef False RevPathTag;
alpar@96
   237
kpeter@785
   238
      /// \brief LEMON style iterator for enumerating the arcs of a path.
alpar@96
   239
      ///
kpeter@785
   240
      /// LEMON style iterator class for enumerating the arcs of a path.
alpar@96
   241
      class ArcIt {
alpar@96
   242
      public:
alpar@209
   243
        /// Default constructor
alpar@209
   244
        ArcIt() {}
alpar@209
   245
        /// Invalid constructor
alpar@209
   246
        ArcIt(Invalid) {}
kpeter@785
   247
        /// Sets the iterator to the first arc of the given path
alpar@209
   248
        ArcIt(const PathDumper&) {}
alpar@96
   249
kpeter@785
   250
        /// Conversion to \c Arc
alpar@209
   251
        operator Arc() const { return INVALID; }
alpar@96
   252
alpar@209
   253
        /// Next arc
alpar@209
   254
        ArcIt& operator++() {return *this;}
alpar@96
   255
alpar@209
   256
        /// Comparison operator
alpar@209
   257
        bool operator==(const ArcIt&) const {return true;}
alpar@209
   258
        /// Comparison operator
alpar@209
   259
        bool operator!=(const ArcIt&) const {return true;}
kpeter@212
   260
        /// Comparison operator
kpeter@212
   261
        bool operator<(const ArcIt&) const {return false;}
alpar@96
   262
alpar@96
   263
      };
alpar@96
   264
kpeter@785
   265
      /// \brief LEMON style iterator for enumerating the arcs of a path
kpeter@785
   266
      /// in reverse direction.
alpar@96
   267
      ///
kpeter@785
   268
      /// LEMON style iterator class for enumerating the arcs of a path
kpeter@785
   269
      /// in reverse direction.
alpar@96
   270
      class RevArcIt {
alpar@96
   271
      public:
alpar@209
   272
        /// Default constructor
alpar@209
   273
        RevArcIt() {}
alpar@209
   274
        /// Invalid constructor
alpar@209
   275
        RevArcIt(Invalid) {}
kpeter@785
   276
        /// Sets the iterator to the last arc of the given path
alpar@209
   277
        RevArcIt(const PathDumper &) {}
alpar@96
   278
kpeter@785
   279
        /// Conversion to \c Arc
alpar@209
   280
        operator Arc() const { return INVALID; }
alpar@96
   281
alpar@209
   282
        /// Next arc
alpar@209
   283
        RevArcIt& operator++() {return *this;}
alpar@96
   284
alpar@209
   285
        /// Comparison operator
alpar@209
   286
        bool operator==(const RevArcIt&) const {return true;}
alpar@209
   287
        /// Comparison operator
alpar@209
   288
        bool operator!=(const RevArcIt&) const {return true;}
kpeter@212
   289
        /// Comparison operator
kpeter@212
   290
        bool operator<(const RevArcIt&) const {return false;}
alpar@96
   291
alpar@96
   292
      };
alpar@96
   293
alpar@96
   294
      template <typename _Path>
alpar@96
   295
      struct Constraints {
alpar@96
   296
        void constraints() {
alpar@96
   297
          function_requires<_path_bits::
alpar@96
   298
            PathDumperConstraints<Digraph, _Path> >();
alpar@96
   299
        }
alpar@96
   300
      };
alpar@96
   301
alpar@96
   302
    };
alpar@96
   303
alpar@96
   304
alpar@96
   305
    ///@}
alpar@96
   306
  }
alpar@96
   307
alpar@96
   308
} // namespace lemon
alpar@96
   309
deba@529
   310
#endif