src/hugo/minlengthpaths.h
changeset 610 4ce8c695e748
child 611 83530dad618a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/hugo/minlengthpaths.h	Tue May 11 16:15:18 2004 +0000
     1.3 @@ -0,0 +1,164 @@
     1.4 +// -*- c++ -*-
     1.5 +#ifndef HUGO_MINLENGTHPATHS_H
     1.6 +#define HUGO_MINLENGTHPATHS_H
     1.7 +
     1.8 +///\ingroup galgs
     1.9 +///\file
    1.10 +///\brief An algorithm for finding k paths of minimal total length.
    1.11 +
    1.12 +#include <iostream>
    1.13 +//#include <hugo/dijkstra.h>
    1.14 +//#include <hugo/graph_wrapper.h>
    1.15 +#include <hugo/maps.h>
    1.16 +#include <vector>
    1.17 +#include <hugo/mincostflows.h>
    1.18 +#include <for_each_macros.h>
    1.19 +
    1.20 +namespace hugo {
    1.21 +
    1.22 +/// \addtogroup galgs
    1.23 +/// @{
    1.24 +
    1.25 +  ///\brief Implementation of an algorithm for finding k paths between 2 nodes 
    1.26 +  /// of minimal total length 
    1.27 +  ///
    1.28 +  /// The class \ref hugo::MinLengthPaths "MinLengthPaths" implements
    1.29 +  /// an algorithm for finding k edge-disjoint paths
    1.30 +  /// from a given source node to a given target node in an
    1.31 +  /// edge-weighted directed graph having minimal total weigth (length).
    1.32 +  ///
    1.33 +  ///\warning It is assumed that the lengths are positive, since the
    1.34 +  /// general flow-decomposition is not implemented yet.
    1.35 +  ///
    1.36 +  ///\author Attila Bernath
    1.37 +  template <typename Graph, typename LengthMap>
    1.38 +  class MinLengthPaths{
    1.39 +
    1.40 +
    1.41 +    typedef typename LengthMap::ValueType Length;
    1.42 +    
    1.43 +    typedef typename Graph::Node Node;
    1.44 +    typedef typename Graph::NodeIt NodeIt;
    1.45 +    typedef typename Graph::Edge Edge;
    1.46 +    typedef typename Graph::OutEdgeIt OutEdgeIt;
    1.47 +    typedef typename Graph::template EdgeMap<int> EdgeIntMap;
    1.48 +
    1.49 +    typedef ConstMap<Edge,int> ConstMap;
    1.50 +
    1.51 +    //Input
    1.52 +    const Graph& G;
    1.53 +
    1.54 +    //Auxiliary variables
    1.55 +    //This is the capacity map for the mincostflow problem
    1.56 +    ConstMap const1map;
    1.57 +    //This MinCostFlows instance will actually solve the problem
    1.58 +    MinCostFlows<Graph, LengthMap, ConstMap> mincost_flow;
    1.59 +
    1.60 +    //Container to store found paths
    1.61 +    std::vector< std::vector<Edge> > paths;
    1.62 +
    1.63 +  public :
    1.64 +
    1.65 +
    1.66 +    MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G),
    1.67 +      const1map(1), mincost_flow(_G, _length, const1map){}
    1.68 +
    1.69 +    ///Runs the algorithm.
    1.70 +
    1.71 +    ///Runs the algorithm.
    1.72 +    ///Returns k if there are at least k edge-disjoint paths from s to t.
    1.73 +   ///Otherwise it returns the number of found edge-disjoint paths from s to t.
    1.74 +    int run(Node s, Node t, int k) {
    1.75 +
    1.76 +      int i = mincost_flow.run(s,t,k);
    1.77 +      
    1.78 +
    1.79 +
    1.80 +      //Let's find the paths
    1.81 +      //We put the paths into stl vectors (as an inner representation). 
    1.82 +      //In the meantime we lose the information stored in 'reversed'.
    1.83 +      //We suppose the lengths to be positive now.
    1.84 +
    1.85 +      //We don't want to change the flow of mincost_flow, so we make a copy
    1.86 +      //The name here suggests that the flow has only 0/1 values.
    1.87 +      EdgeIntMap reversed(G); 
    1.88 +
    1.89 +      FOR_EACH_LOC(typename Graph::EdgeIt, e, G){
    1.90 +	reversed[e] = mincost_flow.getFlow()[e];
    1.91 +      }
    1.92 +      
    1.93 +      paths.clear();
    1.94 +      //total_length=0;
    1.95 +      paths.resize(k);
    1.96 +      for (int j=0; j<i; ++j){
    1.97 +	Node n=s;
    1.98 +	OutEdgeIt e;
    1.99 +
   1.100 +	while (n!=t){
   1.101 +
   1.102 +
   1.103 +	  G.first(e,n);
   1.104 +	  
   1.105 +	  while (!reversed[e]){
   1.106 +	    G.next(e);
   1.107 +	  }
   1.108 +	  n = G.head(e);
   1.109 +	  paths[j].push_back(e);
   1.110 +	  //total_length += length[e];
   1.111 +	  reversed[e] = 1-reversed[e];
   1.112 +	}
   1.113 +	
   1.114 +      }
   1.115 +      return i;
   1.116 +    }
   1.117 +
   1.118 +    
   1.119 +    ///This function gives back the total length of the found paths.
   1.120 +    ///Assumes that \c run() has been run and nothing changed since then.
   1.121 +    Length totalLength(){
   1.122 +      return mincost_flow.totalLength();
   1.123 +    }
   1.124 +
   1.125 +    ///Returns a const reference to the EdgeMap \c flow. \pre \ref run() must
   1.126 +    ///be called before using this function.
   1.127 +    const EdgeIntMap &getFlow() const { return mincost_flow.flow;}
   1.128 +
   1.129 +  ///Returns a const reference to the NodeMap \c potential (the dual solution).
   1.130 +    /// \pre \ref run() must be called before using this function.
   1.131 +    const EdgeIntMap &getPotential() const { return mincost_flow.potential;}
   1.132 +
   1.133 +    ///This function checks, whether the given solution is optimal
   1.134 +    ///Running after a \c run() should return with true
   1.135 +    ///In this "state of the art" this only checks optimality, doesn't bother with feasibility
   1.136 +    ///
   1.137 +    ///\todo Is this OK here?
   1.138 +    bool checkComplementarySlackness(){
   1.139 +      return mincost_flow.checkComplementarySlackness();
   1.140 +    }
   1.141 +
   1.142 +    ///This function gives back the \c j-th path in argument p.
   1.143 +    ///Assumes that \c run() has been run and nothing changed since then.
   1.144 +    /// \warning It is assumed that \c p is constructed to be a path of graph \c G. If \c j is not less than the result of previous \c run, then the result here will be an empty path (\c j can be 0 as well).
   1.145 +    template<typename DirPath>
   1.146 +    void getPath(DirPath& p, size_t j){
   1.147 +      
   1.148 +      p.clear();
   1.149 +      if (j>paths.size()-1){
   1.150 +	return;
   1.151 +      }
   1.152 +      typename DirPath::Builder B(p);
   1.153 +      for(typename std::vector<Edge>::iterator i=paths[j].begin(); 
   1.154 +	  i!=paths[j].end(); ++i ){
   1.155 +	B.pushBack(*i);
   1.156 +      }
   1.157 +
   1.158 +      B.commit();
   1.159 +    }
   1.160 +
   1.161 +  }; //class MinLengthPaths
   1.162 +
   1.163 +  ///@}
   1.164 +
   1.165 +} //namespace hugo
   1.166 +
   1.167 +#endif //HUGO_MINLENGTHPATHS_H