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