COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/athos/minlengthpaths.h @ 511:325c9430723e

Last change on this file since 511:325c9430723e was 511:325c9430723e, checked in by athos, 17 years ago

getPath() function implemented.

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