COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/athos/minlengthpaths.h @ 306:4d15193e3a5d

Last change on this file since 306:4d15193e3a5d was 306:4d15193e3a5d, checked in by athos, 20 years ago

Compiles and segfaults again. Renamed from Suurballe.

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