# source:lemon-0.x/src/work/athos/minlengthpaths.h@322:a42dacfd0e3e

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

The paths are stored in vectors, assumed there is no circle of length 0

File size: 4.0 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#include <vector>
13
14
15namespace hugo {
16
17
18
19  ///\brief Implementation of an algorithm for finding k paths between 2 nodes
20  /// of minimal total length
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).
26
27  template <typename Graph, typename LengthMap>
28  class MinLengthPaths {
29
30    typedef typename LengthMap::ValueType Length;
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;
36    typedef typename Graph::EdgeMap<int> EdgeIntMap;
37
38    typedef ConstMap<Edge,int> ConstMap;
39
40    typedef ResGraphWrapper<const Graph,int,EdgeIntMap,ConstMap> ResGraphType;
41
42
43    class ModLengthMap {
44      typedef typename ResGraphType::NodeMap<Length> NodeMap;
45      const ResGraphType& G;
46      const EdgeIntMap& rev;
47      const LengthMap &ol;
48      const NodeMap &pot;
49    public :
50      typedef typename LengthMap::KeyType KeyType;
51      typedef typename LengthMap::ValueType ValueType;
52
53      ValueType operator[](typename ResGraphType::Edge e) const {
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
60      ModLengthMap(const ResGraphType& _G, const EdgeIntMap& _rev,
61                   const LengthMap &o,  const NodeMap &p) :
62        G(_G), rev(_rev), ol(o), pot(p){};
63    };
64
65
66    const Graph& G;
67    const LengthMap& length;
68
69    //auxiliry variables
70
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
74    EdgeIntMap reversed;
75
76    //Container to store found paths
77    std::vector< std::vector<Edge> > paths;
78
79  public :
80
81
82    MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G),
83      length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ }
84
85
86    ///Runs the algorithm
87    ///Returns k if there are at least k edge-disjoint paths from s to t.
88    ///Otherwise it returns the number of edge-disjoint paths from s to t.
89    int run(Node s, Node t, int k) {
90      ConstMap const1map(1);
91
92      //We need a residual graph, in which some of the edges are reversed
93      ResGraphType res_graph(G, reversed, const1map);
94
95      //Initialize the copy of the Dijkstra potential to zero
96      typename ResGraphType::NodeMap<Length> dijkstra_dist(res_graph);
97      ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist);
98
99      Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
100
101      int i;
102      for (i=0; i<k; ++i){
103        dijkstra.run(s);
104        if (!dijkstra.reached(t)){
105          //There are no k paths from s to t
106          break;
107        };
108
109        {
110          //We have to copy the potential
111          typename ResGraphType::NodeIt n;
112          for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
113              dijkstra_dist[n] += dijkstra.distMap()[n];
114          }
115        }
116
117
118        //Reversing the sortest path
119        Node n=t;
120        Edge e;
121        while (n!=s){
122          e = dijkstra.pred(n);
123          n = dijkstra.predNode(n);
124          reversed[e] = 1-reversed[e];
125        }
126
127
128      }
129
130      //Let's find the paths
131      //We put the paths into vectors (just for now). In the meantime we lose
132      //the information stored in 'reversed'
133      //We suppose the lengths to be positive now.
134      paths.clear();
135      paths.resize(k);
136      for (int j=0; j<i; ++j){
137        Node n=s;
138        OutEdgeIt e;
139
140        while (n!=t){
141
142
143          G.first(e,n);
144
145          while (!reversed[e]){
146            G.next(e);
147          }
149          paths[j].push_back(e);
150          reversed[e] = 1-reversed[e];
151        }
152
153      }
154
155      return i;
156    }
157
158
159  }; //class MinLengthPaths
160
161
162} //namespace hugo
163
164#endif //HUGO_MINLENGTHPATHS_H
Note: See TracBrowser for help on using the repository browser.