# source:lemon-0.x/src/work/athos/minlengthpaths.h@330:7ac0d4e8a31c

Last change on this file since 330:7ac0d4e8a31c was 330:7ac0d4e8a31c, checked in by marci, 20 years ago

In the resgraphwrapper interface, and in the constructor,
the order of FlowMap? and CapacityMap? is changed.

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