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