src/work/athos/old/minlengthpaths.h
author athos
Wed, 23 Mar 2005 12:12:42 +0000
changeset 1248 ca613a9e3567
parent 986 e997802b855c
permissions -rw-r--r--
Bug fix in lp_base.h.
athos@601
     1
// -*- c++ -*-
alpar@921
     2
#ifndef LEMON_MINLENGTHPATHS_H
alpar@921
     3
#define LEMON_MINLENGTHPATHS_H
athos@601
     4
athos@601
     5
///\ingroup galgs
athos@601
     6
///\file
athos@601
     7
///\brief An algorithm for finding k paths of minimal total length.
athos@601
     8
athos@601
     9
#include <iostream>
alpar@921
    10
#include <lemon/dijkstra.h>
alpar@921
    11
#include <lemon/graph_wrapper.h>
alpar@921
    12
#include <lemon/maps.h>
athos@607
    13
#include <vector>
athos@601
    14
athos@601
    15
alpar@921
    16
namespace lemon {
athos@601
    17
athos@601
    18
/// \addtogroup galgs
athos@601
    19
/// @{
athos@601
    20
athos@601
    21
  ///\brief Implementation of an algorithm for finding k paths between 2 nodes 
athos@601
    22
  /// of minimal total length 
athos@601
    23
  ///
alpar@921
    24
  /// The class \ref lemon::MinLengthPaths "MinLengthPaths" implements
athos@601
    25
  /// an algorithm for finding k edge-disjoint paths
athos@601
    26
  /// from a given source node to a given target node in an
athos@601
    27
  /// edge-weighted directed graph having minimal total weigth (length).
athos@601
    28
  ///
athos@601
    29
  ///\author Attila Bernath
athos@601
    30
  template <typename Graph, typename LengthMap>
athos@601
    31
  class MinLengthPaths {
athos@601
    32
alpar@987
    33
    typedef typename LengthMap::Value Length;
athos@601
    34
    
athos@601
    35
    typedef typename Graph::Node Node;
athos@601
    36
    typedef typename Graph::NodeIt NodeIt;
athos@601
    37
    typedef typename Graph::Edge Edge;
athos@601
    38
    typedef typename Graph::OutEdgeIt OutEdgeIt;
athos@601
    39
    typedef typename Graph::template EdgeMap<int> EdgeIntMap;
athos@601
    40
athos@601
    41
    typedef ConstMap<Edge,int> ConstMap;
athos@601
    42
athos@601
    43
    typedef ResGraphWrapper<const Graph,int,ConstMap,EdgeIntMap> ResGraphType;
athos@601
    44
athos@601
    45
    class ModLengthMap {   
athos@601
    46
      typedef typename ResGraphType::template NodeMap<Length> NodeMap;
athos@601
    47
      const ResGraphType& G;
athos@601
    48
      const EdgeIntMap& rev;
athos@601
    49
      const LengthMap &ol;
athos@601
    50
      const NodeMap &pot;
athos@601
    51
    public :
alpar@987
    52
      typedef typename LengthMap::Key Key;
alpar@987
    53
      typedef typename LengthMap::Value Value;
athos@601
    54
	
alpar@987
    55
      Value operator[](typename ResGraphType::Edge e) const {     
alpar@986
    56
	//if ( (1-2*rev[e])*ol[e]-(pot[G.target(e)]-pot[G.source(e)] ) <0 ){
athos@601
    57
	//  std::cout<<"Negative length!!"<<std::endl;
athos@601
    58
	//}
alpar@986
    59
	return (1-2*rev[e])*ol[e]-(pot[G.target(e)]-pot[G.source(e)]);   
athos@601
    60
      }     
athos@601
    61
	
athos@601
    62
      ModLengthMap(const ResGraphType& _G, const EdgeIntMap& _rev, 
athos@601
    63
		   const LengthMap &o,  const NodeMap &p) : 
athos@601
    64
	G(_G), rev(_rev), ol(o), pot(p){}; 
athos@601
    65
    };//ModLengthMap
athos@601
    66
athos@601
    67
athos@601
    68
    
athos@601
    69
athos@601
    70
    const Graph& G;
athos@601
    71
    const LengthMap& length;
athos@601
    72
athos@601
    73
    //auxiliary variables
athos@601
    74
athos@601
    75
    //The value is 1 iff the edge is reversed. 
athos@601
    76
    //If the algorithm has finished, the edges of the seeked paths are 
athos@601
    77
    //exactly those that are reversed 
athos@601
    78
    EdgeIntMap reversed; 
athos@601
    79
    
athos@601
    80
    //Container to store found paths
athos@601
    81
    std::vector< std::vector<Edge> > paths;
athos@601
    82
    //typedef DirPath<Graph> DPath;
athos@601
    83
    //DPath paths;
athos@601
    84
athos@601
    85
athos@601
    86
    Length total_length;
athos@601
    87
athos@601
    88
  public :
athos@601
    89
athos@601
    90
athos@601
    91
    MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G), 
athos@601
    92
      length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ }
athos@601
    93
athos@601
    94
    
athos@601
    95
    ///Runs the algorithm.
athos@601
    96
athos@601
    97
    ///Runs the algorithm.
athos@601
    98
    ///Returns k if there are at least k edge-disjoint paths from s to t.
athos@601
    99
    ///Otherwise it returns the number of found edge-disjoint paths from s to t.
athos@601
   100
    int run(Node s, Node t, int k) {
athos@601
   101
      ConstMap const1map(1);
athos@601
   102
athos@601
   103
athos@601
   104
      //We need a residual graph, in which some of the edges are reversed
athos@601
   105
      ResGraphType res_graph(G, const1map, reversed);
athos@601
   106
athos@601
   107
      //Initialize the copy of the Dijkstra potential to zero
athos@601
   108
      typename ResGraphType::template NodeMap<Length> dijkstra_dist(res_graph);
athos@601
   109
      ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist);
athos@601
   110
athos@601
   111
      Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
athos@601
   112
athos@601
   113
      int i;
athos@601
   114
      for (i=0; i<k; ++i){
athos@601
   115
	dijkstra.run(s);
athos@601
   116
	if (!dijkstra.reached(t)){
athos@601
   117
	  //There are no k paths from s to t
athos@601
   118
	  break;
athos@601
   119
	};
athos@601
   120
	
athos@601
   121
	{
athos@601
   122
	  //We have to copy the potential
athos@601
   123
	  typename ResGraphType::NodeIt n;
athos@601
   124
	  for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
athos@601
   125
	      dijkstra_dist[n] += dijkstra.distMap()[n];
athos@601
   126
	  }
athos@601
   127
	}
athos@601
   128
athos@601
   129
athos@601
   130
	//Reversing the sortest path
athos@601
   131
	Node n=t;
athos@601
   132
	Edge e;
athos@601
   133
	while (n!=s){
athos@601
   134
	  e = dijkstra.pred(n);
athos@601
   135
	  n = dijkstra.predNode(n);
athos@601
   136
	  reversed[e] = 1-reversed[e];
athos@601
   137
	}
athos@601
   138
athos@601
   139
	  
athos@601
   140
      }
athos@601
   141
      
athos@601
   142
      //Let's find the paths
athos@601
   143
      //We put the paths into stl vectors (as an inner representation). 
athos@601
   144
      //In the meantime we lose the information stored in 'reversed'.
athos@601
   145
      //We suppose the lengths to be positive now.
athos@601
   146
athos@601
   147
      //Meanwhile we put the total length of the found paths 
athos@601
   148
      //in the member variable total_length
athos@601
   149
      paths.clear();
athos@601
   150
      total_length=0;
athos@601
   151
      paths.resize(k);
athos@601
   152
      for (int j=0; j<i; ++j){
athos@601
   153
	Node n=s;
athos@601
   154
	OutEdgeIt e;
athos@601
   155
athos@601
   156
	while (n!=t){
athos@601
   157
athos@601
   158
athos@601
   159
	  G.first(e,n);
athos@601
   160
	  
athos@601
   161
	  while (!reversed[e]){
athos@601
   162
	    G.next(e);
athos@601
   163
	  }
alpar@986
   164
	  n = G.target(e);
athos@601
   165
	  paths[j].push_back(e);
athos@601
   166
	  total_length += length[e];
athos@601
   167
	  reversed[e] = 1-reversed[e];
athos@601
   168
	}
athos@601
   169
	
athos@601
   170
      }
athos@601
   171
athos@601
   172
      return i;
athos@601
   173
    }
athos@601
   174
athos@601
   175
    ///This function gives back the total length of the found paths.
athos@601
   176
    ///Assumes that \c run() has been run and nothing changed since then.
athos@601
   177
    Length totalLength(){
athos@601
   178
      return total_length;
athos@601
   179
    }
athos@601
   180
athos@601
   181
    ///This function gives back the \c j-th path in argument p.
athos@601
   182
    ///Assumes that \c run() has been run and nothing changed since then.
athos@601
   183
    /// \warning It is assumed that \c p is constructed to be a path of graph \c G. If \c j is greater than the result of previous \c run, then the result here will be an empty path.
athos@601
   184
    template<typename DirPath>
athos@601
   185
    void getPath(DirPath& p, int j){
athos@601
   186
      p.clear();
athos@601
   187
      typename DirPath::Builder B(p);
athos@601
   188
      for(typename std::vector<Edge>::iterator i=paths[j].begin(); 
athos@601
   189
	  i!=paths[j].end(); ++i ){
athos@601
   190
	B.pushBack(*i);
athos@601
   191
      }
athos@601
   192
athos@601
   193
      B.commit();
athos@601
   194
    }
athos@601
   195
athos@601
   196
  }; //class MinLengthPaths
athos@601
   197
athos@601
   198
  ///@}
athos@601
   199
alpar@921
   200
} //namespace lemon
athos@601
   201
alpar@921
   202
#endif //LEMON_MINLENGTHPATHS_H