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