COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/athos/minlengthpaths.h @ 318:7bec4e8fb7dd

Last change on this file since 318:7bec4e8fb7dd was 314:eabbe162e32e, checked in by athos, 21 years ago

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

File size: 3.5 KB
Line 
1// -*- c++ -*-
2#ifndef HUGO_MINLENGTHPATHS_H
3#define HUGO_MINLENGTHPATHS_H
4
5///\file
6///\brief An algorithm for finding k paths of minimal total length.
7
8#include <iostream>
9#include <dijkstra.h>
10#include <graph_wrapper.h>
11#include <maps.h>
12
13namespace hugo {
14
15
16  ///\brief Implementation of an algorithm for finding k paths between 2 nodes
17  /// of minimal total length
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).
23
24  template <typename Graph, typename LengthMap>
25  class MinLengthPaths {
26
27    typedef typename LengthMap::ValueType Length;
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;
33    typedef typename Graph::EdgeMap<int> EdgeIntMap;
34
35    typedef ConstMap<Edge,int> ConstMap;
36
37    typedef ResGraphWrapper<const Graph,int,EdgeIntMap,ConstMap> ResGraphType;
38
39
40    class ModLengthMap {   
41      typedef typename ResGraphType::NodeMap<Length> NodeMap;
42      const ResGraphType& G;
43      const EdgeIntMap& rev;
44      const LengthMap &ol;
45      const NodeMap &pot;
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
58      ModLengthMap(const ResGraphType& _G, const EdgeIntMap& _rev,
59                   const LengthMap &o,  const NodeMap &p) :
60        G(_G), rev(_rev), ol(o), pot(p){};
61    };
62   
63
64    const Graph& G;
65    const LengthMap& length;
66
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
71    EdgeIntMap reversed;
72   
73  public :
74
75
76    MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G),
77      length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ }
78
79    ///Runs the algorithm
80   
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);
86
87      //We need a residual graph, in which some of the edges are reversed
88      ResGraphType res_graph(G, reversed, const1map);
89
90      //Initialize the copy of the Dijkstra potential to zero
91      typename ResGraphType::NodeMap<Length> dijkstra_dist(res_graph);
92      ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist);
93
94      Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
95     
96      for (int i=0; i<k; ++i){
97        dijkstra.run(s);
98        if (!dijkstra.reached(t)){
99          //There are no k paths from s to t
100          return i;
101        };
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
112        //Reversing the sortest path
113        Node n=t;
114        Edge e;
115        while (n!=s){
116          e = dijkstra.pred(n);
117          n = dijkstra.predNode(n);
118          reversed[e] = 1-reversed[e];
119        }
120
121         
122      }
123      return k;
124    }
125
126
127
128
129
130  }; //class MinLengthPaths
131
132
133} //namespace hugo
134
135#endif //HUGO_MINLENGTHPATHS_H
Note: See TracBrowser for help on using the repository browser.