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