lemon/suurballe.h
changeset 934 fe283caf6414
parent 863 a93f1a27d831
child 1076 97d978243703
equal deleted inserted replaced
13:0df619f614bf 14:ad0d5409c63c
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library.
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     4  *
     5  * Copyright (C) 2003-2009
     5  * Copyright (C) 2003-2010
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     8  *
     9  * Permission to use, modify and distribute this software is granted
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    10  * provided that this copyright notice appears in all copies. For
    63     ///
    63     ///
    64     /// The type used for storing the found arc-disjoint paths.
    64     /// The type used for storing the found arc-disjoint paths.
    65     /// It must conform to the \ref lemon::concepts::Path "Path" concept
    65     /// It must conform to the \ref lemon::concepts::Path "Path" concept
    66     /// and it must have an \c addBack() function.
    66     /// and it must have an \c addBack() function.
    67     typedef lemon::Path<Digraph> Path;
    67     typedef lemon::Path<Digraph> Path;
    68     
    68 
    69     /// The cross reference type used for the heap.
    69     /// The cross reference type used for the heap.
    70     typedef typename GR::template NodeMap<int> HeapCrossRef;
    70     typedef typename GR::template NodeMap<int> HeapCrossRef;
    71 
    71 
    72     /// \brief The heap type used for internal Dijkstra computations.
    72     /// \brief The heap type used for internal Dijkstra computations.
    73     ///
    73     ///
   156       const FlowMap &_flow;
   156       const FlowMap &_flow;
   157       PotentialMap &_pi;
   157       PotentialMap &_pi;
   158       PredMap &_pred;
   158       PredMap &_pred;
   159       Node _s;
   159       Node _s;
   160       Node _t;
   160       Node _t;
   161       
   161 
   162       PotentialMap _dist;
   162       PotentialMap _dist;
   163       std::vector<Node> _proc_nodes;
   163       std::vector<Node> _proc_nodes;
   164 
   164 
   165     public:
   165     public:
   166 
   166 
   167       // Constructor
   167       // Constructor
   168       ResidualDijkstra(Suurballe &srb) :
   168       ResidualDijkstra(Suurballe &srb) :
   169         _graph(srb._graph), _length(srb._length),
   169         _graph(srb._graph), _length(srb._length),
   170         _flow(*srb._flow), _pi(*srb._potential), _pred(srb._pred), 
   170         _flow(*srb._flow), _pi(*srb._potential), _pred(srb._pred),
   171         _s(srb._s), _t(srb._t), _dist(_graph) {}
   171         _s(srb._s), _t(srb._t), _dist(_graph) {}
   172         
   172 
   173       // Run the algorithm and return true if a path is found
   173       // Run the algorithm and return true if a path is found
   174       // from the source node to the target node.
   174       // from the source node to the target node.
   175       bool run(int cnt) {
   175       bool run(int cnt) {
   176         return cnt == 0 ? startFirst() : start();
   176         return cnt == 0 ? startFirst() : start();
   177       }
   177       }
   178 
   178 
   179     private:
   179     private:
   180     
   180 
   181       // Execute the algorithm for the first time (the flow and potential
   181       // Execute the algorithm for the first time (the flow and potential
   182       // functions have to be identically zero).
   182       // functions have to be identically zero).
   183       bool startFirst() {
   183       bool startFirst() {
   184         HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
   184         HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
   185         Heap heap(heap_cross_ref);
   185         Heap heap(heap_cross_ref);
   346     template <typename T>
   346     template <typename T>
   347     struct SetPath
   347     struct SetPath
   348       : public Suurballe<GR, LEN, SetPathTraits<T> > {
   348       : public Suurballe<GR, LEN, SetPathTraits<T> > {
   349       typedef Suurballe<GR, LEN, SetPathTraits<T> > Create;
   349       typedef Suurballe<GR, LEN, SetPathTraits<T> > Create;
   350     };
   350     };
   351     
   351 
   352     template <typename H, typename CR>
   352     template <typename H, typename CR>
   353     struct SetHeapTraits : public Traits {
   353     struct SetHeapTraits : public Traits {
   354       typedef H Heap;
   354       typedef H Heap;
   355       typedef CR HeapCrossRef;
   355       typedef CR HeapCrossRef;
   356     };
   356     };
   357 
   357 
   358     /// \brief \ref named-templ-param "Named parameter" for setting
   358     /// \brief \ref named-templ-param "Named parameter" for setting
   359     /// \c Heap and \c HeapCrossRef types.
   359     /// \c Heap and \c HeapCrossRef types.
   360     ///
   360     ///
   361     /// \ref named-templ-param "Named parameter" for setting \c Heap
   361     /// \ref named-templ-param "Named parameter" for setting \c Heap
   362     /// and \c HeapCrossRef types with automatic allocation. 
   362     /// and \c HeapCrossRef types with automatic allocation.
   363     /// They will be used for internal Dijkstra computations.
   363     /// They will be used for internal Dijkstra computations.
   364     /// The heap type must conform to the \ref lemon::concepts::Heap "Heap"
   364     /// The heap type must conform to the \ref lemon::concepts::Heap "Heap"
   365     /// concept and its priority type must be \c Length.
   365     /// concept and its priority type must be \c Length.
   366     template <typename H,
   366     template <typename H,
   367               typename CR = typename Digraph::template NodeMap<int> >
   367               typename CR = typename Digraph::template NodeMap<int> >
   395     std::vector<Path> _paths;
   395     std::vector<Path> _paths;
   396     int _path_num;
   396     int _path_num;
   397 
   397 
   398     // The pred arc map
   398     // The pred arc map
   399     PredMap _pred;
   399     PredMap _pred;
   400     
   400 
   401     // Data for full init
   401     // Data for full init
   402     PotentialMap *_init_dist;
   402     PotentialMap *_init_dist;
   403     PredMap *_init_pred;
   403     PredMap *_init_pred;
   404     bool _full_init;
   404     bool _full_init;
   405 
   405 
   553         ::template SetDistMap<PotentialMap>
   553         ::template SetDistMap<PotentialMap>
   554         ::template SetPredMap<PredMap>
   554         ::template SetPredMap<PredMap>
   555         ::Create dijk(_graph, _length);
   555         ::Create dijk(_graph, _length);
   556       dijk.distMap(*_init_dist).predMap(*_init_pred);
   556       dijk.distMap(*_init_dist).predMap(*_init_pred);
   557       dijk.run(s);
   557       dijk.run(s);
   558       
   558 
   559       _full_init = true;
   559       _full_init = true;
   560     }
   560     }
   561 
   561 
   562     /// \brief Execute the algorithm.
   562     /// \brief Execute the algorithm.
   563     ///
   563     ///
   597     ///
   597     ///
   598     /// \pre \ref init() must be called before using this function.
   598     /// \pre \ref init() must be called before using this function.
   599     int findFlow(const Node& t, int k = 2) {
   599     int findFlow(const Node& t, int k = 2) {
   600       _t = t;
   600       _t = t;
   601       ResidualDijkstra dijkstra(*this);
   601       ResidualDijkstra dijkstra(*this);
   602       
   602 
   603       // Initialization
   603       // Initialization
   604       for (ArcIt e(_graph); e != INVALID; ++e) {
   604       for (ArcIt e(_graph); e != INVALID; ++e) {
   605         (*_flow)[e] = 0;
   605         (*_flow)[e] = 0;
   606       }
   606       }
   607       if (_full_init) {
   607       if (_full_init) {
   611         Node u = _t;
   611         Node u = _t;
   612         Arc e;
   612         Arc e;
   613         while ((e = (*_init_pred)[u]) != INVALID) {
   613         while ((e = (*_init_pred)[u]) != INVALID) {
   614           (*_flow)[e] = 1;
   614           (*_flow)[e] = 1;
   615           u = _graph.source(e);
   615           u = _graph.source(e);
   616         }        
   616         }
   617         _path_num = 1;
   617         _path_num = 1;
   618       } else {
   618       } else {
   619         for (NodeIt n(_graph); n != INVALID; ++n) {
   619         for (NodeIt n(_graph); n != INVALID; ++n) {
   620           (*_potential)[n] = 0;
   620           (*_potential)[n] = 0;
   621         }
   621         }