COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/hugo/minlengthpaths.h @ 851:209c9d53e195

Last change on this file since 851:209c9d53e195 was 851:209c9d53e195, checked in by Alpar Juttner, 20 years ago

Changes in doc.

File size: 4.8 KB
RevLine 
[610]1// -*- c++ -*-
2#ifndef HUGO_MINLENGTHPATHS_H
3#define HUGO_MINLENGTHPATHS_H
4
[759]5///\ingroup flowalgs
[610]6///\file
7///\brief An algorithm for finding k paths of minimal total length.
8
[611]9
[610]10//#include <hugo/dijkstra.h>
11//#include <hugo/graph_wrapper.h>
12#include <hugo/maps.h>
13#include <vector>
14#include <hugo/mincostflows.h>
15
16namespace hugo {
17
[759]18/// \addtogroup flowalgs
[610]19/// @{
20
21  ///\brief Implementation of an algorithm for finding k paths between 2 nodes
22  /// of minimal total length
23  ///
24  /// The class \ref hugo::MinLengthPaths "MinLengthPaths" implements
25  /// an algorithm for finding 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).
28  ///
29  ///\warning It is assumed that the lengths are positive, since the
30  /// general flow-decomposition is not implemented yet.
31  ///
32  ///\author Attila Bernath
33  template <typename Graph, typename LengthMap>
34  class MinLengthPaths{
35
36
37    typedef typename LengthMap::ValueType Length;
38   
39    typedef typename Graph::Node Node;
40    typedef typename Graph::NodeIt NodeIt;
41    typedef typename Graph::Edge Edge;
42    typedef typename Graph::OutEdgeIt OutEdgeIt;
43    typedef typename Graph::template EdgeMap<int> EdgeIntMap;
44
45    typedef ConstMap<Edge,int> ConstMap;
46
47    //Input
48    const Graph& G;
49
50    //Auxiliary variables
51    //This is the capacity map for the mincostflow problem
52    ConstMap const1map;
53    //This MinCostFlows instance will actually solve the problem
54    MinCostFlows<Graph, LengthMap, ConstMap> mincost_flow;
55
56    //Container to store found paths
57    std::vector< std::vector<Edge> > paths;
58
59  public :
60
61
62    MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G),
63      const1map(1), mincost_flow(_G, _length, const1map){}
64
65    ///Runs the algorithm.
66
67    ///Runs the algorithm.
68    ///Returns k if there are at least k edge-disjoint paths from s to t.
[851]69    ///Otherwise it returns the number of found edge-disjoint paths from s to t.
[610]70    int run(Node s, Node t, int k) {
71
72      int i = mincost_flow.run(s,t,k);
73     
74
75
76      //Let's find the paths
77      //We put the paths into stl vectors (as an inner representation).
78      //In the meantime we lose the information stored in 'reversed'.
79      //We suppose the lengths to be positive now.
80
81      //We don't want to change the flow of mincost_flow, so we make a copy
82      //The name here suggests that the flow has only 0/1 values.
83      EdgeIntMap reversed(G);
84
[788]85      for(typename Graph::EdgeIt e(G); e!=INVALID; ++e)
[610]86        reversed[e] = mincost_flow.getFlow()[e];
87     
88      paths.clear();
89      //total_length=0;
90      paths.resize(k);
91      for (int j=0; j<i; ++j){
92        Node n=s;
93        OutEdgeIt e;
94
95        while (n!=t){
96
97
98          G.first(e,n);
99         
100          while (!reversed[e]){
[776]101            ++e;
[610]102          }
103          n = G.head(e);
104          paths[j].push_back(e);
105          //total_length += length[e];
106          reversed[e] = 1-reversed[e];
107        }
108       
109      }
110      return i;
111    }
112
113   
[851]114    ///Total length of the paths
115   
[610]116    ///This function gives back the total length of the found paths.
[851]117    ///\pre \ref run() must
118    ///be called before using this function.
[610]119    Length totalLength(){
120      return mincost_flow.totalLength();
121    }
122
[851]123    ///Return the found flow.
124
125    ///This function returns a const reference to the EdgeMap \c flow.
126    ///\pre \ref run() must
[610]127    ///be called before using this function.
128    const EdgeIntMap &getFlow() const { return mincost_flow.flow;}
129
[851]130    /// Return the optimal dual solution
131   
132    ///This function returns a const reference to the NodeMap
133    ///\c potential (the dual solution).
[610]134    /// \pre \ref run() must be called before using this function.
135    const EdgeIntMap &getPotential() const { return mincost_flow.potential;}
136
[851]137    ///Checks whether the complementary slackness holds.
138
[610]139    ///This function checks, whether the given solution is optimal
140    ///Running after a \c run() should return with true
[851]141    ///Currently this function only checks optimality,
142    ///doesn't bother with feasibility
[610]143    ///
144    ///\todo Is this OK here?
145    bool checkComplementarySlackness(){
146      return mincost_flow.checkComplementarySlackness();
147    }
148
[851]149    ///Read the found paths.
150   
[610]151    ///This function gives back the \c j-th path in argument p.
152    ///Assumes that \c run() has been run and nothing changed since then.
[851]153    /// \warning It is assumed that \c p is constructed to
154    ///be a path of graph \c G.
155    ///If \c j is not less than the result of previous \c run,
156    ///then the result here will be an empty path (\c j can be 0 as well).
157    template<typename Path>
158    void getPath(Path& p, size_t j){
[610]159     
160      p.clear();
161      if (j>paths.size()-1){
162        return;
163      }
164      typename DirPath::Builder B(p);
165      for(typename std::vector<Edge>::iterator i=paths[j].begin();
166          i!=paths[j].end(); ++i ){
167        B.pushBack(*i);
168      }
169
170      B.commit();
171    }
172
173  }; //class MinLengthPaths
174
175  ///@}
176
177} //namespace hugo
178
179#endif //HUGO_MINLENGTHPATHS_H
Note: See TracBrowser for help on using the repository browser.