src/work/klao/minlengthpaths.h
author alpar
Fri, 16 Apr 2004 21:18:40 +0000
changeset 342 c98125b3f47c
parent 308 379e1d50089d
permissions -rw-r--r--
An example for a readable/writeable but non-referrable map
without get() and set().
klao@308
     1
// -*- c++ -*-
klao@308
     2
#ifndef HUGO_MINLENGTHPATHS_H
klao@308
     3
#define HUGO_MINLENGTHPATHS_H
klao@308
     4
klao@308
     5
///\file
klao@308
     6
///\brief An algorithm for finding k paths of minimal total length.
klao@308
     7
klao@308
     8
#include <iostream>
klao@308
     9
#include <dijkstra.h>
klao@308
    10
#include <graph_wrapper.h>
klao@308
    11
#include <maps.h>
klao@308
    12
klao@308
    13
namespace hugo {
klao@308
    14
klao@308
    15
klao@310
    16
  ///\brief Implementation of an algorithm for finding k paths between 2 nodes 
klao@308
    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).
klao@308
    23
klao@310
    24
  template <typename Graph, typename LengthMap>
klao@308
    25
  class MinLengthPaths {
klao@308
    26
klao@310
    27
    typedef typename LengthMap::ValueType Length;
klao@308
    28
klao@308
    29
    typedef typename Graph::Node Node;
klao@308
    30
    typedef typename Graph::NodeIt NodeIt;
klao@308
    31
    typedef typename Graph::Edge Edge;
klao@308
    32
    typedef typename Graph::OutEdgeIt OutEdgeIt;
klao@308
    33
    typedef typename Graph::EdgeMap<int> EdgeIntMap;
klao@308
    34
klao@308
    35
    typedef ConstMap<Edge,int> ConstMap;
klao@308
    36
klao@310
    37
    typedef ResGraphWrapper<const Graph,int,EdgeIntMap,ConstMap> ResGraphType;
klao@308
    38
klao@308
    39
klao@308
    40
    class ModLengthMap {   
klao@310
    41
      typedef typename ResGraphType::NodeMap<Length> NodeMap;
klao@308
    42
      const ResGraphType& G;
klao@310
    43
      const EdgeIntMap& rev;
klao@310
    44
      const LengthMap &ol;
klao@310
    45
      const NodeMap &pot;
klao@308
    46
    public :
klao@308
    47
      typedef typename LengthMap::KeyType KeyType;
klao@308
    48
      typedef typename LengthMap::ValueType ValueType;
klao@308
    49
klao@308
    50
      ValueType operator[](typename ResGraphType::Edge e) const {     
klao@308
    51
	if ( (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)] ) <0 ){
klao@308
    52
	  ///\TODO This has to be removed
klao@308
    53
	  std::cout<<"Negative length!!"<<std::endl;
klao@308
    54
	}
klao@308
    55
	return (1-2*rev[e])*ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);   
klao@308
    56
      }     
klao@308
    57
klao@310
    58
      ModLengthMap(const ResGraphType& _G, const EdgeIntMap& _rev, 
klao@310
    59
		   const LengthMap &o,  const NodeMap &p) : 
klao@308
    60
	G(_G), rev(_rev), ol(o), pot(p){}; 
klao@308
    61
    };
klao@308
    62
    
klao@308
    63
klao@308
    64
    const Graph& G;
klao@308
    65
    const LengthMap& length;
klao@308
    66
klao@308
    67
    //auxiliary variable
klao@308
    68
    //The value is 1 iff the edge is reversed
klao@308
    69
    EdgeIntMap reversed; 
klao@308
    70
klao@308
    71
    
klao@308
    72
  public :
klao@310
    73
klao@308
    74
klao@308
    75
    MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G), 
klao@308
    76
      length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ }
klao@308
    77
klao@308
    78
    ///Runs the algorithm
klao@308
    79
    
klao@308
    80
    ///Runs the algorithm
klao@308
    81
    ///Returns k if there are at least k edge-disjoint paths from s to t.
klao@308
    82
    ///Otherwise it returns the number of edge-disjoint paths from s to t.
klao@308
    83
    int run(Node s, Node t, int k) {
klao@308
    84
      ConstMap const1map(1);
klao@308
    85
klao@308
    86
      ResGraphType res_graph(G, reversed, const1map);
klao@308
    87
klao@308
    88
      //Initialize the copy of the Dijkstra potential to zero
klao@310
    89
      typename ResGraphType::NodeMap<Length> dijkstra_dist(res_graph);
klao@310
    90
      ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist);
klao@308
    91
klao@308
    92
      Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
klao@308
    93
      
klao@308
    94
      for (int i=0; i<k; ++i){
klao@308
    95
	dijkstra.run(s);
klao@308
    96
	if (!dijkstra.reached(t)){
klao@308
    97
	  //There is no k path from s to t
klao@310
    98
	  /// \TODO mit keresett itt ez a ++?
klao@310
    99
	  return i;
klao@308
   100
	};
klao@308
   101
	
klao@308
   102
	{
klao@308
   103
	  //We have to copy the potential
klao@308
   104
	  typename ResGraphType::NodeIt n;
klao@308
   105
	  for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
klao@308
   106
	      dijkstra_dist[n] += dijkstra.distMap()[n];
klao@308
   107
	  }
klao@308
   108
	}
klao@308
   109
klao@308
   110
klao@308
   111
	//Reversing the sortest path
klao@308
   112
	Node n=t;
klao@308
   113
	Edge e;
klao@308
   114
	while (n!=s){
klao@308
   115
	  e = dijkstra.pred(n);
klao@308
   116
	  n = dijkstra.predNode(n);
klao@308
   117
	  reversed[e] = 1-reversed[e];
klao@308
   118
	}
klao@308
   119
klao@308
   120
	  
klao@308
   121
      }
klao@308
   122
      return k;
klao@308
   123
    }
klao@308
   124
klao@308
   125
klao@308
   126
klao@308
   127
klao@308
   128
klao@310
   129
  }; //class MinLengthPaths
klao@308
   130
klao@308
   131
klao@308
   132
} //namespace hugo
klao@308
   133
klao@308
   134
#endif //HUGO_MINLENGTHPATHS_H