COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/athos/minlengthpaths.h @ 310:76c005b15354

Last change on this file since 310:76c005b15354 was 310:76c005b15354, checked in by Mihaly Barasz, 16 years ago

Converted the "minlengthpaths" alg. to the new style graph_wrappers.

File size: 3.4 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    //auxiliary variable
68    //The value is 1 iff the edge is reversed
69    EdgeIntMap reversed;
70
71   
72  public :
73
74
75    MinLengthPaths(Graph& _G, LengthMap& _length) : G(_G),
76      length(_length), reversed(_G)/*, dijkstra_dist(_G)*/{ }
77
78    ///Runs the algorithm
79   
80    ///Runs the algorithm
81    ///Returns k if there are at least k edge-disjoint paths from s to t.
82    ///Otherwise it returns the number of edge-disjoint paths from s to t.
83    int run(Node s, Node t, int k) {
84      ConstMap const1map(1);
85
86      ResGraphType res_graph(G, reversed, const1map);
87
88      //Initialize the copy of the Dijkstra potential to zero
89      typename ResGraphType::NodeMap<Length> dijkstra_dist(res_graph);
90      ModLengthMap mod_length(res_graph, reversed, length, dijkstra_dist);
91
92      Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
93     
94      for (int i=0; i<k; ++i){
95        dijkstra.run(s);
96        if (!dijkstra.reached(t)){
97          //There is no k path from s to t
98          /// \TODO mit keresett itt ez a ++?
99          return i;
100        };
101       
102        {
103          //We have to copy the potential
104          typename ResGraphType::NodeIt n;
105          for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
106              dijkstra_dist[n] += dijkstra.distMap()[n];
107          }
108        }
109
110
111        //Reversing the sortest path
112        Node n=t;
113        Edge e;
114        while (n!=s){
115          e = dijkstra.pred(n);
116          n = dijkstra.predNode(n);
117          reversed[e] = 1-reversed[e];
118        }
119
120         
121      }
122      return k;
123    }
124
125
126
127
128
129  }; //class MinLengthPaths
130
131
132} //namespace hugo
133
134#endif //HUGO_MINLENGTHPATHS_H
Note: See TracBrowser for help on using the repository browser.