COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/athos/minlengthpaths.h @ 314:eabbe162e32e

Last change on this file since 314:eabbe162e32e was 314:eabbe162e32e, checked in by athos, 20 years ago

minlengthpaths is ready, but the paths are not yet determined: needs to canonize a flow

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