COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/athos/minlengthpaths.h @ 451:6b36be4cffa4

Last change on this file since 451:6b36be4cffa4 was 430:60e4627e8c74, checked in by Alpar Juttner, 21 years ago

Many new modules (groups) in the documentation.

File size: 4.1 KB
RevLine 
[276]1// -*- c++ -*-
[306]2#ifndef HUGO_MINLENGTHPATHS_H
3#define HUGO_MINLENGTHPATHS_H
[276]4
[430]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>
[322]13#include <vector>
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).
[276]28
[310]29  template <typename Graph, typename LengthMap>
[306]30  class MinLengthPaths {
[276]31
[310]32    typedef typename LengthMap::ValueType Length;
[276]33
34    typedef typename Graph::Node Node;
35    typedef typename Graph::NodeIt NodeIt;
36    typedef typename Graph::Edge Edge;
37    typedef typename Graph::OutEdgeIt OutEdgeIt;
[306]38    typedef typename Graph::EdgeMap<int> EdgeIntMap;
39
40    typedef ConstMap<Edge,int> ConstMap;
41
[330]42    typedef ResGraphWrapper<const Graph,int,ConstMap,EdgeIntMap> ResGraphType;
[276]43
[306]44
45    class ModLengthMap {   
[310]46      typedef typename ResGraphType::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;
54
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      }     
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){};
65    };
66   
67
[276]68    const Graph& G;
69    const LengthMap& length;
70
[328]71    //auxiliary variables
[322]72
[314]73    //The value is 1 iff the edge is reversed.
74    //If the algorithm has finished, the edges of the seeked paths are
75    //exactly those that are reversed
[306]76    EdgeIntMap reversed;
[276]77   
[322]78    //Container to store found paths
79    std::vector< std::vector<Edge> > paths;
80
[276]81  public :
[310]82
[276]83
[306]84    MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G),
85      length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ }
[276]86
[294]87   
[329]88    ///Runs the algorithm.
89
90    ///Runs the algorithm.
[306]91    ///Returns k if there are at least k edge-disjoint paths from s to t.
[329]92    ///Otherwise it returns the number of found edge-disjoint paths from s to t.
[306]93    int run(Node s, Node t, int k) {
94      ConstMap const1map(1);
[276]95
[314]96      //We need a residual graph, in which some of the edges are reversed
[330]97      ResGraphType res_graph(G, const1map, reversed);
[306]98
99      //Initialize the copy of the Dijkstra potential to zero
[310]100      typename ResGraphType::NodeMap<Length> dijkstra_dist(res_graph);
101      ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist);
[306]102
103      Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
[322]104
105      int i;
106      for (i=0; i<k; ++i){
[276]107        dijkstra.run(s);
108        if (!dijkstra.reached(t)){
[314]109          //There are no k paths from s to t
[322]110          break;
[276]111        };
[306]112       
113        {
114          //We have to copy the potential
115          typename ResGraphType::NodeIt n;
116          for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
117              dijkstra_dist[n] += dijkstra.distMap()[n];
118          }
119        }
120
121
[276]122        //Reversing the sortest path
123        Node n=t;
124        Edge e;
125        while (n!=s){
[291]126          e = dijkstra.pred(n);
127          n = dijkstra.predNode(n);
[276]128          reversed[e] = 1-reversed[e];
129        }
130
131         
132      }
[322]133     
134      //Let's find the paths
135      //We put the paths into vectors (just for now). In the meantime we lose
136      //the information stored in 'reversed'
137      //We suppose the lengths to be positive now.
138      paths.clear();
139      paths.resize(k);
140      for (int j=0; j<i; ++j){
141        Node n=s;
142        OutEdgeIt e;
143
144        while (n!=t){
145
146
147          G.first(e,n);
148         
149          while (!reversed[e]){
150            G.next(e);
151          }
152          n = G.head(e);
153          paths[j].push_back(e);
154          reversed[e] = 1-reversed[e];
155        }
156       
157      }
158
159      return i;
[276]160    }
161
162
[310]163  }; //class MinLengthPaths
[276]164
[430]165  ///@}
[276]166
167} //namespace hugo
168
[306]169#endif //HUGO_MINLENGTHPATHS_H
Note: See TracBrowser for help on using the repository browser.