src/hugo/suurballe.h
author deba
Tue, 28 Sep 2004 16:40:55 +0000
changeset 916 c0734a8c282c
parent 901 69a8e672acb1
permissions -rw-r--r--
Fix InEdgeIt and OutEdgeIt in the symmetric graphs.
alpar@906
     1
/* -*- C++ -*-
alpar@906
     2
 * src/hugo/suurballe.h - Part of HUGOlib, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@906
    16
marci@901
    17
#ifndef HUGO_SUURBALLE_H
marci@901
    18
#define HUGO_SUURBALLE_H
alpar@899
    19
alpar@899
    20
///\ingroup flowalgs
alpar@899
    21
///\file
alpar@899
    22
///\brief An algorithm for finding k paths of minimal total length.
alpar@899
    23
alpar@899
    24
alpar@899
    25
#include <hugo/maps.h>
alpar@899
    26
#include <vector>
alpar@899
    27
#include <hugo/min_cost_flow.h>
alpar@899
    28
alpar@899
    29
namespace hugo {
alpar@899
    30
alpar@899
    31
/// \addtogroup flowalgs
alpar@899
    32
/// @{
alpar@899
    33
alpar@899
    34
  ///\brief Implementation of an algorithm for finding k edge-disjoint paths between 2 nodes 
alpar@899
    35
  /// of minimal total length 
alpar@899
    36
  ///
alpar@899
    37
  /// The class \ref hugo::Suurballe implements
alpar@899
    38
  /// an algorithm for finding k edge-disjoint paths
alpar@899
    39
  /// from a given source node to a given target node in an
alpar@899
    40
  /// edge-weighted directed graph having minimal total weight (length).
alpar@899
    41
  ///
alpar@899
    42
  ///\warning Length values should be nonnegative.
alpar@899
    43
  /// 
alpar@899
    44
  ///\param Graph The directed graph type the algorithm runs on.
alpar@899
    45
  ///\param LengthMap The type of the length map (values should be nonnegative).
alpar@899
    46
  ///
alpar@899
    47
  ///\note It it questionable if it is correct to call this method after
alpar@899
    48
  ///%Suurballe for it is just a special case of Edmond's and Karp's algorithm
alpar@899
    49
  ///for finding minimum cost flows. In fact, this implementation is just
alpar@899
    50
  ///wraps the MinCostFlow algorithms. The paper of both %Suurballe and
alpar@899
    51
  ///Edmonds-Karp published in 1972, therefore it is possibly right to
alpar@899
    52
  ///state that they are
alpar@899
    53
  ///independent results. Most frequently this special case is referred as
alpar@899
    54
  ///%Suurballe method in the literature, especially in communication
alpar@899
    55
  ///network context.
alpar@899
    56
  ///\author Attila Bernath
alpar@899
    57
  template <typename Graph, typename LengthMap>
alpar@899
    58
  class Suurballe{
alpar@899
    59
alpar@899
    60
alpar@899
    61
    typedef typename LengthMap::ValueType Length;
alpar@899
    62
    
alpar@899
    63
    typedef typename Graph::Node Node;
alpar@899
    64
    typedef typename Graph::NodeIt NodeIt;
alpar@899
    65
    typedef typename Graph::Edge Edge;
alpar@899
    66
    typedef typename Graph::OutEdgeIt OutEdgeIt;
alpar@899
    67
    typedef typename Graph::template EdgeMap<int> EdgeIntMap;
alpar@899
    68
alpar@899
    69
    typedef ConstMap<Edge,int> ConstMap;
alpar@899
    70
alpar@899
    71
    //Input
alpar@899
    72
    const Graph& G;
alpar@899
    73
alpar@899
    74
    //Auxiliary variables
alpar@899
    75
    //This is the capacity map for the mincostflow problem
alpar@899
    76
    ConstMap const1map;
alpar@899
    77
    //This MinCostFlow instance will actually solve the problem
alpar@899
    78
    MinCostFlow<Graph, LengthMap, ConstMap> mincost_flow;
alpar@899
    79
alpar@899
    80
    //Container to store found paths
alpar@899
    81
    std::vector< std::vector<Edge> > paths;
alpar@899
    82
alpar@899
    83
  public :
alpar@899
    84
alpar@899
    85
alpar@899
    86
    /// The constructor of the class.
alpar@899
    87
    
alpar@899
    88
    ///\param _G The directed graph the algorithm runs on. 
alpar@899
    89
    ///\param _length The length (weight or cost) of the edges. 
alpar@899
    90
    Suurballe(Graph& _G, LengthMap& _length) : G(_G),
alpar@899
    91
      const1map(1), mincost_flow(_G, _length, const1map){}
alpar@899
    92
alpar@899
    93
    ///Runs the algorithm.
alpar@899
    94
alpar@899
    95
    ///Runs the algorithm.
alpar@899
    96
    ///Returns k if there are at least k edge-disjoint paths from s to t.
alpar@899
    97
    ///Otherwise it returns the number of found edge-disjoint paths from s to t.
alpar@899
    98
    ///
alpar@899
    99
    ///\param s The source node.
alpar@899
   100
    ///\param t The target node.
alpar@899
   101
    ///\param k How many paths are we looking for?
alpar@899
   102
    ///
alpar@899
   103
    int run(Node s, Node t, int k) {
alpar@899
   104
alpar@899
   105
      int i = mincost_flow.run(s,t,k);
alpar@899
   106
    
alpar@899
   107
alpar@899
   108
      //Let's find the paths
alpar@899
   109
      //We put the paths into stl vectors (as an inner representation). 
alpar@899
   110
      //In the meantime we lose the information stored in 'reversed'.
alpar@899
   111
      //We suppose the lengths to be positive now.
alpar@899
   112
alpar@899
   113
      //We don't want to change the flow of mincost_flow, so we make a copy
alpar@899
   114
      //The name here suggests that the flow has only 0/1 values.
alpar@899
   115
      EdgeIntMap reversed(G); 
alpar@899
   116
alpar@899
   117
      for(typename Graph::EdgeIt e(G); e!=INVALID; ++e) 
alpar@899
   118
	reversed[e] = mincost_flow.getFlow()[e];
alpar@899
   119
      
alpar@899
   120
      paths.clear();
alpar@899
   121
      //total_length=0;
alpar@899
   122
      paths.resize(k);
alpar@899
   123
      for (int j=0; j<i; ++j){
alpar@899
   124
	Node n=s;
alpar@899
   125
	OutEdgeIt e;
alpar@899
   126
alpar@899
   127
	while (n!=t){
alpar@899
   128
alpar@899
   129
alpar@899
   130
	  G.first(e,n);
alpar@899
   131
	  
alpar@899
   132
	  while (!reversed[e]){
alpar@899
   133
	    ++e;
alpar@899
   134
	  }
alpar@899
   135
	  n = G.head(e);
alpar@899
   136
	  paths[j].push_back(e);
alpar@899
   137
	  //total_length += length[e];
alpar@899
   138
	  reversed[e] = 1-reversed[e];
alpar@899
   139
	}
alpar@899
   140
	
alpar@899
   141
      }
alpar@899
   142
      return i;
alpar@899
   143
    }
alpar@899
   144
alpar@899
   145
    
alpar@899
   146
    ///Returns the total length of the paths
alpar@899
   147
    
alpar@899
   148
    ///This function gives back the total length of the found paths.
alpar@899
   149
    ///\pre \ref run() must
alpar@899
   150
    ///be called before using this function.
alpar@899
   151
    Length totalLength(){
alpar@899
   152
      return mincost_flow.totalLength();
alpar@899
   153
    }
alpar@899
   154
alpar@899
   155
    ///Returns the found flow.
alpar@899
   156
alpar@899
   157
    ///This function returns a const reference to the EdgeMap \c flow.
alpar@899
   158
    ///\pre \ref run() must
alpar@899
   159
    ///be called before using this function.
alpar@899
   160
    const EdgeIntMap &getFlow() const { return mincost_flow.flow;}
alpar@899
   161
alpar@899
   162
    /// Returns the optimal dual solution
alpar@899
   163
    
alpar@899
   164
    ///This function returns a const reference to the NodeMap
alpar@899
   165
    ///\c potential (the dual solution).
alpar@899
   166
    /// \pre \ref run() must be called before using this function.
alpar@899
   167
    const EdgeIntMap &getPotential() const { return mincost_flow.potential;}
alpar@899
   168
alpar@899
   169
    ///Checks whether the complementary slackness holds.
alpar@899
   170
alpar@899
   171
    ///This function checks, whether the given solution is optimal.
alpar@899
   172
    ///It should return true after calling \ref run() 
alpar@899
   173
    ///Currently this function only checks optimality,
alpar@899
   174
    ///doesn't bother with feasibility
alpar@899
   175
    ///It is meant for testing purposes.
alpar@899
   176
    ///
alpar@899
   177
    bool checkComplementarySlackness(){
alpar@899
   178
      return mincost_flow.checkComplementarySlackness();
alpar@899
   179
    }
alpar@899
   180
alpar@899
   181
    ///Read the found paths.
alpar@899
   182
    
alpar@899
   183
    ///This function gives back the \c j-th path in argument p.
alpar@899
   184
    ///Assumes that \c run() has been run and nothing changed since then.
alpar@899
   185
    /// \warning It is assumed that \c p is constructed to
alpar@899
   186
    ///be a path of graph \c G.
alpar@899
   187
    ///If \c j is not less than the result of previous \c run,
alpar@899
   188
    ///then the result here will be an empty path (\c j can be 0 as well).
alpar@899
   189
    ///
alpar@899
   190
    ///\param Path The type of the path structure to put the result to (must meet hugo path concept).
alpar@899
   191
    ///\param p The path to put the result to 
alpar@899
   192
    ///\param j Which path you want to get from the found paths (in a real application you would get the found paths iteratively)
alpar@899
   193
    template<typename Path>
alpar@899
   194
    void getPath(Path& p, size_t j){
alpar@899
   195
alpar@899
   196
      p.clear();
alpar@899
   197
      if (j>paths.size()-1){
alpar@899
   198
	return;
alpar@899
   199
      }
alpar@899
   200
      typename Path::Builder B(p);
alpar@899
   201
      for(typename std::vector<Edge>::iterator i=paths[j].begin(); 
alpar@899
   202
	  i!=paths[j].end(); ++i ){
alpar@899
   203
	B.pushBack(*i);
alpar@899
   204
      }
alpar@899
   205
alpar@899
   206
      B.commit();
alpar@899
   207
    }
alpar@899
   208
alpar@899
   209
  }; //class Suurballe
alpar@899
   210
alpar@899
   211
  ///@}
alpar@899
   212
alpar@899
   213
} //namespace hugo
alpar@899
   214
marci@901
   215
#endif //HUGO_SUURBALLE_H