src/work/athos/minlengthpaths.h
author marci
Thu, 08 Apr 2004 12:03:49 +0000
changeset 315 7b97540cd743
parent 310 76c005b15354
child 322 a42dacfd0e3e
permissions -rw-r--r--
ansi pedantic bug in gcc
athos@276
     1
// -*- c++ -*-
athos@306
     2
#ifndef HUGO_MINLENGTHPATHS_H
athos@306
     3
#define HUGO_MINLENGTHPATHS_H
athos@276
     4
alpar@294
     5
///\file
athos@306
     6
///\brief An algorithm for finding k paths of minimal total length.
alpar@294
     7
athos@276
     8
#include <iostream>
athos@276
     9
#include <dijkstra.h>
athos@276
    10
#include <graph_wrapper.h>
athos@306
    11
#include <maps.h>
athos@306
    12
athos@276
    13
namespace hugo {
athos@276
    14
athos@276
    15
klao@310
    16
  ///\brief Implementation of an algorithm for finding k paths between 2 nodes 
athos@306
    17
  /// of minimal total length 
klao@310
    18
  ///
klao@310
    19
  /// The class \ref hugo::MinLengthPaths "MinLengthPaths" implements
klao@310
    20
  /// an algorithm which finds k edge-disjoint paths
klao@310
    21
  /// from a given source node to a given target node in an
klao@310
    22
  /// edge-weighted directed graph having minimal total weigth (length).
athos@276
    23
klao@310
    24
  template <typename Graph, typename LengthMap>
athos@306
    25
  class MinLengthPaths {
athos@276
    26
klao@310
    27
    typedef typename LengthMap::ValueType Length;
athos@276
    28
athos@276
    29
    typedef typename Graph::Node Node;
athos@276
    30
    typedef typename Graph::NodeIt NodeIt;
athos@276
    31
    typedef typename Graph::Edge Edge;
athos@276
    32
    typedef typename Graph::OutEdgeIt OutEdgeIt;
athos@306
    33
    typedef typename Graph::EdgeMap<int> EdgeIntMap;
athos@306
    34
athos@306
    35
    typedef ConstMap<Edge,int> ConstMap;
athos@306
    36
klao@310
    37
    typedef ResGraphWrapper<const Graph,int,EdgeIntMap,ConstMap> ResGraphType;
athos@276
    38
athos@306
    39
athos@306
    40
    class ModLengthMap {   
klao@310
    41
      typedef typename ResGraphType::NodeMap<Length> NodeMap;
athos@306
    42
      const ResGraphType& G;
klao@310
    43
      const EdgeIntMap& rev;
klao@310
    44
      const LengthMap &ol;
klao@310
    45
      const NodeMap &pot;
athos@306
    46
    public :
athos@306
    47
      typedef typename LengthMap::KeyType KeyType;
athos@306
    48
      typedef typename LengthMap::ValueType ValueType;
athos@306
    49
athos@306
    50
      ValueType operator[](typename ResGraphType::Edge e) const {     
athos@306
    51
	if ( (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)] ) <0 ){
athos@306
    52
	  ///\TODO This has to be removed
athos@306
    53
	  std::cout<<"Negative length!!"<<std::endl;
athos@306
    54
	}
athos@306
    55
	return (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
athos@306
    56
      }     
athos@306
    57
klao@310
    58
      ModLengthMap(const ResGraphType& _G, const EdgeIntMap& _rev, 
klao@310
    59
		   const LengthMap &o,  const NodeMap &p) : 
athos@306
    60
	G(_G), rev(_rev), ol(o), pot(p){}; 
athos@306
    61
    };
athos@306
    62
    
athos@306
    63
athos@276
    64
    const Graph& G;
athos@276
    65
    const LengthMap& length;
athos@276
    66
athos@314
    67
    //auxiliry variable
athos@314
    68
    //The value is 1 iff the edge is reversed. 
athos@314
    69
    //If the algorithm has finished, the edges of the seeked paths are 
athos@314
    70
    //exactly those that are reversed 
athos@306
    71
    EdgeIntMap reversed; 
athos@276
    72
    
athos@276
    73
  public :
klao@310
    74
athos@276
    75
athos@306
    76
    MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G), 
athos@306
    77
      length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ }
athos@276
    78
athos@306
    79
    ///Runs the algorithm
alpar@294
    80
    
athos@306
    81
    ///Runs the algorithm
athos@306
    82
    ///Returns k if there are at least k edge-disjoint paths from s to t.
athos@306
    83
    ///Otherwise it returns the number of edge-disjoint paths from s to t.
athos@306
    84
    int run(Node s, Node t, int k) {
athos@306
    85
      ConstMap const1map(1);
athos@276
    86
athos@314
    87
      //We need a residual graph, in which some of the edges are reversed
athos@306
    88
      ResGraphType res_graph(G, reversed, const1map);
athos@306
    89
athos@306
    90
      //Initialize the copy of the Dijkstra potential to zero
klao@310
    91
      typename ResGraphType::NodeMap<Length> dijkstra_dist(res_graph);
klao@310
    92
      ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist);
athos@306
    93
athos@306
    94
      Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
athos@276
    95
      
athos@276
    96
      for (int i=0; i<k; ++i){
athos@276
    97
	dijkstra.run(s);
athos@276
    98
	if (!dijkstra.reached(t)){
athos@314
    99
	  //There are no k paths from s to t
klao@310
   100
	  return i;
athos@276
   101
	};
athos@306
   102
	
athos@306
   103
	{
athos@306
   104
	  //We have to copy the potential
athos@306
   105
	  typename ResGraphType::NodeIt n;
athos@306
   106
	  for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
athos@306
   107
	      dijkstra_dist[n] += dijkstra.distMap()[n];
athos@306
   108
	  }
athos@306
   109
	}
athos@306
   110
athos@306
   111
athos@276
   112
	//Reversing the sortest path
athos@276
   113
	Node n=t;
athos@276
   114
	Edge e;
athos@276
   115
	while (n!=s){
athos@291
   116
	  e = dijkstra.pred(n);
athos@291
   117
	  n = dijkstra.predNode(n);
athos@276
   118
	  reversed[e] = 1-reversed[e];
athos@276
   119
	}
athos@276
   120
athos@276
   121
	  
athos@276
   122
      }
athos@306
   123
      return k;
athos@276
   124
    }
athos@276
   125
athos@276
   126
athos@276
   127
athos@276
   128
athos@276
   129
klao@310
   130
  }; //class MinLengthPaths
athos@276
   131
athos@276
   132
athos@276
   133
} //namespace hugo
athos@276
   134
athos@306
   135
#endif //HUGO_MINLENGTHPATHS_H