src/hugo/minlengthpaths.h
author marci
Mon, 20 Sep 2004 09:05:19 +0000
changeset 888 cc3590763f7f
parent 853 4cb8f31c1ff8
permissions -rw-r--r--
(none)
athos@610
     1
// -*- c++ -*-
athos@610
     2
#ifndef HUGO_MINLENGTHPATHS_H
athos@610
     3
#define HUGO_MINLENGTHPATHS_H
athos@610
     4
alpar@759
     5
///\ingroup flowalgs
athos@610
     6
///\file
athos@610
     7
///\brief An algorithm for finding k paths of minimal total length.
athos@610
     8
athos@611
     9
athos@610
    10
#include <hugo/maps.h>
athos@610
    11
#include <vector>
athos@610
    12
#include <hugo/mincostflows.h>
athos@610
    13
athos@610
    14
namespace hugo {
athos@610
    15
alpar@759
    16
/// \addtogroup flowalgs
athos@610
    17
/// @{
athos@610
    18
athos@860
    19
  ///\brief Implementation of an algorithm for finding k edge-disjoint paths between 2 nodes 
athos@610
    20
  /// of minimal total length 
athos@610
    21
  ///
athos@860
    22
  /// The class \ref hugo::MinLengthPaths implements
athos@610
    23
  /// an algorithm for finding k edge-disjoint paths
athos@610
    24
  /// from a given source node to a given target node in an
athos@860
    25
  /// edge-weighted directed graph having minimal total weight (length).
athos@610
    26
  ///
athos@860
    27
  ///\warning Length values should be nonnegative.
athos@860
    28
  /// 
athos@860
    29
  ///\param Graph The directed graph type the algorithm runs on.
athos@860
    30
  ///\param LengthMap The type of the length map (values should be nonnegative).
athos@610
    31
  ///
athos@610
    32
  ///\author Attila Bernath
athos@610
    33
  template <typename Graph, typename LengthMap>
athos@610
    34
  class MinLengthPaths{
athos@610
    35
athos@610
    36
athos@610
    37
    typedef typename LengthMap::ValueType Length;
athos@610
    38
    
athos@610
    39
    typedef typename Graph::Node Node;
athos@610
    40
    typedef typename Graph::NodeIt NodeIt;
athos@610
    41
    typedef typename Graph::Edge Edge;
athos@610
    42
    typedef typename Graph::OutEdgeIt OutEdgeIt;
athos@610
    43
    typedef typename Graph::template EdgeMap<int> EdgeIntMap;
athos@610
    44
athos@610
    45
    typedef ConstMap<Edge,int> ConstMap;
athos@610
    46
athos@610
    47
    //Input
athos@610
    48
    const Graph& G;
athos@610
    49
athos@610
    50
    //Auxiliary variables
athos@610
    51
    //This is the capacity map for the mincostflow problem
athos@610
    52
    ConstMap const1map;
athos@610
    53
    //This MinCostFlows instance will actually solve the problem
athos@610
    54
    MinCostFlows<Graph, LengthMap, ConstMap> mincost_flow;
athos@610
    55
athos@610
    56
    //Container to store found paths
athos@610
    57
    std::vector< std::vector<Edge> > paths;
athos@610
    58
athos@610
    59
  public :
athos@610
    60
athos@610
    61
athos@860
    62
    /// The constructor of the class.
athos@860
    63
    
athos@860
    64
    ///\param _G The directed graph the algorithm runs on. 
athos@860
    65
    ///\param _length The length (weight or cost) of the edges. 
athos@610
    66
    MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G),
athos@610
    67
      const1map(1), mincost_flow(_G, _length, const1map){}
athos@610
    68
athos@610
    69
    ///Runs the algorithm.
athos@610
    70
athos@610
    71
    ///Runs the algorithm.
athos@610
    72
    ///Returns k if there are at least k edge-disjoint paths from s to t.
alpar@851
    73
    ///Otherwise it returns the number of found edge-disjoint paths from s to t.
athos@860
    74
    ///
athos@860
    75
    ///\param s The source node.
athos@860
    76
    ///\param t The target node.
athos@860
    77
    ///\param k How many paths are we looking for?
athos@860
    78
    ///
athos@610
    79
    int run(Node s, Node t, int k) {
athos@610
    80
athos@610
    81
      int i = mincost_flow.run(s,t,k);
athos@860
    82
    
athos@610
    83
athos@610
    84
      //Let's find the paths
athos@610
    85
      //We put the paths into stl vectors (as an inner representation). 
athos@610
    86
      //In the meantime we lose the information stored in 'reversed'.
athos@610
    87
      //We suppose the lengths to be positive now.
athos@610
    88
athos@610
    89
      //We don't want to change the flow of mincost_flow, so we make a copy
athos@610
    90
      //The name here suggests that the flow has only 0/1 values.
athos@610
    91
      EdgeIntMap reversed(G); 
athos@610
    92
marci@788
    93
      for(typename Graph::EdgeIt e(G); e!=INVALID; ++e) 
athos@610
    94
	reversed[e] = mincost_flow.getFlow()[e];
athos@610
    95
      
athos@610
    96
      paths.clear();
athos@610
    97
      //total_length=0;
athos@610
    98
      paths.resize(k);
athos@610
    99
      for (int j=0; j<i; ++j){
athos@610
   100
	Node n=s;
athos@610
   101
	OutEdgeIt e;
athos@610
   102
athos@610
   103
	while (n!=t){
athos@610
   104
athos@610
   105
athos@610
   106
	  G.first(e,n);
athos@610
   107
	  
athos@610
   108
	  while (!reversed[e]){
hegyi@776
   109
	    ++e;
athos@610
   110
	  }
athos@610
   111
	  n = G.head(e);
athos@610
   112
	  paths[j].push_back(e);
athos@610
   113
	  //total_length += length[e];
athos@610
   114
	  reversed[e] = 1-reversed[e];
athos@610
   115
	}
athos@610
   116
	
athos@610
   117
      }
athos@610
   118
      return i;
athos@610
   119
    }
athos@610
   120
athos@610
   121
    
athos@860
   122
    ///Returns the total length of the paths
alpar@851
   123
    
athos@610
   124
    ///This function gives back the total length of the found paths.
alpar@851
   125
    ///\pre \ref run() must
alpar@851
   126
    ///be called before using this function.
athos@610
   127
    Length totalLength(){
athos@610
   128
      return mincost_flow.totalLength();
athos@610
   129
    }
athos@610
   130
athos@860
   131
    ///Returns the found flow.
alpar@851
   132
alpar@851
   133
    ///This function returns a const reference to the EdgeMap \c flow.
alpar@851
   134
    ///\pre \ref run() must
athos@610
   135
    ///be called before using this function.
athos@610
   136
    const EdgeIntMap &getFlow() const { return mincost_flow.flow;}
athos@610
   137
athos@860
   138
    /// Returns the optimal dual solution
alpar@851
   139
    
alpar@851
   140
    ///This function returns a const reference to the NodeMap
alpar@851
   141
    ///\c potential (the dual solution).
athos@610
   142
    /// \pre \ref run() must be called before using this function.
athos@610
   143
    const EdgeIntMap &getPotential() const { return mincost_flow.potential;}
athos@610
   144
alpar@851
   145
    ///Checks whether the complementary slackness holds.
alpar@851
   146
athos@860
   147
    ///This function checks, whether the given solution is optimal.
athos@860
   148
    ///It should return true after calling \ref run() 
alpar@851
   149
    ///Currently this function only checks optimality,
alpar@851
   150
    ///doesn't bother with feasibility
athos@860
   151
    ///It is meant for testing purposes.
athos@610
   152
    ///
athos@610
   153
    bool checkComplementarySlackness(){
athos@610
   154
      return mincost_flow.checkComplementarySlackness();
athos@610
   155
    }
athos@610
   156
alpar@851
   157
    ///Read the found paths.
alpar@851
   158
    
athos@610
   159
    ///This function gives back the \c j-th path in argument p.
athos@610
   160
    ///Assumes that \c run() has been run and nothing changed since then.
alpar@851
   161
    /// \warning It is assumed that \c p is constructed to
alpar@851
   162
    ///be a path of graph \c G.
alpar@851
   163
    ///If \c j is not less than the result of previous \c run,
alpar@851
   164
    ///then the result here will be an empty path (\c j can be 0 as well).
athos@860
   165
    ///
athos@860
   166
    ///\param Path The type of the path structure to put the result to (must meet hugo path concept).
athos@860
   167
    ///\param p The path to put the result to 
athos@860
   168
    ///\param j Which path you want to get from the found paths (in a real application you would get the found paths iteratively)
alpar@851
   169
    template<typename Path>
alpar@851
   170
    void getPath(Path& p, size_t j){
athos@860
   171
athos@610
   172
      p.clear();
athos@610
   173
      if (j>paths.size()-1){
athos@610
   174
	return;
athos@610
   175
      }
alpar@853
   176
      typename Path::Builder B(p);
athos@610
   177
      for(typename std::vector<Edge>::iterator i=paths[j].begin(); 
athos@610
   178
	  i!=paths[j].end(); ++i ){
athos@610
   179
	B.pushBack(*i);
athos@610
   180
      }
athos@610
   181
athos@610
   182
      B.commit();
athos@610
   183
    }
athos@610
   184
athos@610
   185
  }; //class MinLengthPaths
athos@610
   186
athos@610
   187
  ///@}
athos@610
   188
athos@610
   189
} //namespace hugo
athos@610
   190
athos@610
   191
#endif //HUGO_MINLENGTHPATHS_H