31 namespace lemon { | 
    31 namespace lemon { | 
    32   | 
    32   | 
    33   /// \addtogroup shortest_path  | 
    33   /// \addtogroup shortest_path  | 
    34   /// @{ | 
    34   /// @{ | 
    35   | 
    35   | 
    36   /// \brief Implementation of an algorithm for finding arc-disjoint  | 
    36   /// \brief Algorithm for finding arc-disjoint paths between two nodes  | 
    37   /// paths between two nodes having minimum total length.  | 
    37   /// having minimum total length.  | 
    38   ///  | 
    38   ///  | 
    39   /// \ref lemon::Suurballe "Suurballe" implements an algorithm for  | 
    39   /// \ref lemon::Suurballe "Suurballe" implements an algorithm for  | 
    40   /// finding arc-disjoint paths having minimum total length (cost)  | 
    40   /// finding arc-disjoint paths having minimum total length (cost)  | 
    41   /// from a given source node to a given target node in a directed  | 
    41   /// from a given source node to a given target node in a digraph.  | 
    42   /// digraph.  | 
         | 
    43   ///  | 
    42   ///  | 
    44   /// In fact, this implementation is the specialization of the  | 
    43   /// In fact, this implementation is the specialization of the  | 
    45   /// \ref CapacityScaling "successive shortest path" algorithm.  | 
    44   /// \ref CapacityScaling "successive shortest path" algorithm.  | 
    46   ///  | 
    45   ///  | 
    47   /// \tparam Digraph The directed digraph type the algorithm runs on.  | 
    46   /// \tparam Digraph The digraph type the algorithm runs on.  | 
         | 
    47   /// The default value is \c ListDigraph.  | 
    48   /// \tparam LengthMap The type of the length (cost) map.  | 
    48   /// \tparam LengthMap The type of the length (cost) map.  | 
         | 
    49   /// The default value is <tt>Digraph::ArcMap<int></tt>.  | 
    49   ///  | 
    50   ///  | 
    50   /// \warning Length values should be \e non-negative \e integers.  | 
    51   /// \warning Length values should be \e non-negative \e integers.  | 
    51   ///  | 
    52   ///  | 
    52   /// \note For finding node-disjoint paths this algorithm can be used  | 
    53   /// \note For finding node-disjoint paths this algorithm can be used  | 
    53   /// with \ref SplitDigraphAdaptor.  | 
    54   /// with \ref SplitDigraphAdaptor.  | 
    54   ///  | 
    55 #ifdef DOXYGEN  | 
    55   /// \author Attila Bernath and Peter Kovacs  | 
    56   template <typename Digraph, typename LengthMap>  | 
    56     | 
    57 #else  | 
    57   template < typename Digraph,   | 
    58   template < typename Digraph = ListDigraph,  | 
    58              typename LengthMap = typename Digraph::template ArcMap<int> >  | 
    59              typename LengthMap = typename Digraph::template ArcMap<int> >  | 
         | 
    60 #endif  | 
    59   class Suurballe  | 
    61   class Suurballe  | 
    60   { | 
    62   { | 
    61     TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);  | 
    63     TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);  | 
    62   | 
    64   | 
    63     typedef typename LengthMap::Value Length;  | 
    65     typedef typename LengthMap::Value Length;  | 
   118                         PredMap &pred,  | 
   120                         PredMap &pred,  | 
   119                         Node s, Node t ) :  | 
   121                         Node s, Node t ) :  | 
   120         _graph(digraph), _flow(flow), _length(length), _potential(potential),  | 
   122         _graph(digraph), _flow(flow), _length(length), _potential(potential),  | 
   121         _dist(digraph), _pred(pred), _s(s), _t(t) {} | 
   123         _dist(digraph), _pred(pred), _s(s), _t(t) {} | 
   122   | 
   124   | 
   123       /// \brief Runs the algorithm. Returns \c true if a path is found  | 
   125       /// \brief Run the algorithm. It returns \c true if a path is found  | 
   124       /// from the source node to the target node.  | 
   126       /// from the source node to the target node.  | 
   125       bool run() { | 
   127       bool run() { | 
   126         HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);  | 
   128         HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);  | 
   127         Heap heap(heap_cross_ref);  | 
   129         Heap heap(heap_cross_ref);  | 
   128         heap.push(_s, 0);  | 
   130         heap.push(_s, 0);  | 
   129         _pred[_s] = INVALID;  | 
   131         _pred[_s] = INVALID;  | 
   130         _proc_nodes.clear();  | 
   132         _proc_nodes.clear();  | 
   131   | 
   133   | 
   132         // Processing nodes  | 
   134         // Process nodes  | 
   133         while (!heap.empty() && heap.top() != _t) { | 
   135         while (!heap.empty() && heap.top() != _t) { | 
   134           Node u = heap.top(), v;  | 
   136           Node u = heap.top(), v;  | 
   135           Length d = heap.prio() + _potential[u], nd;  | 
   137           Length d = heap.prio() + _potential[u], nd;  | 
   136           _dist[u] = heap.prio();  | 
   138           _dist[u] = heap.prio();  | 
   137           heap.pop();  | 
   139           heap.pop();  | 
   138           _proc_nodes.push_back(u);  | 
   140           _proc_nodes.push_back(u);  | 
   139   | 
   141   | 
   140           // Traversing outgoing arcs  | 
   142           // Traverse outgoing arcs  | 
   141           for (OutArcIt e(_graph, u); e != INVALID; ++e) { | 
   143           for (OutArcIt e(_graph, u); e != INVALID; ++e) { | 
   142             if (_flow[e] == 0) { | 
   144             if (_flow[e] == 0) { | 
   143               v = _graph.target(e);  | 
   145               v = _graph.target(e);  | 
   144               switch(heap.state(v)) { | 
   146               switch(heap.state(v)) { | 
   145               case Heap::PRE_HEAP:  | 
   147               case Heap::PRE_HEAP:  | 
   181             }  | 
   183             }  | 
   182           }  | 
   184           }  | 
   183         }  | 
   185         }  | 
   184         if (heap.empty()) return false;  | 
   186         if (heap.empty()) return false;  | 
   185   | 
   187   | 
   186         // Updating potentials of processed nodes  | 
   188         // Update potentials of processed nodes  | 
   187         Length t_dist = heap.prio();  | 
   189         Length t_dist = heap.prio();  | 
   188         for (int i = 0; i < int(_proc_nodes.size()); ++i)  | 
   190         for (int i = 0; i < int(_proc_nodes.size()); ++i)  | 
   189           _potential[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist;  | 
   191           _potential[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist;  | 
   190         return true;  | 
   192         return true;  | 
   191       }  | 
   193       }  | 
   192   | 
   194   | 
   193     }; //class ResidualDijkstra  | 
   195     }; //class ResidualDijkstra  | 
   194   | 
   196   | 
   195   private:  | 
   197   private:  | 
   196   | 
   198   | 
   197     // The directed digraph the algorithm runs on  | 
   199     // The digraph the algorithm runs on  | 
   198     const Digraph &_graph;  | 
   200     const Digraph &_graph;  | 
   199     // The length map  | 
   201     // The length map  | 
   200     const LengthMap &_length;  | 
   202     const LengthMap &_length;  | 
   201       | 
   203       | 
   202     // Arc map of the current flow  | 
   204     // Arc map of the current flow  | 
   286     /// If you only need the flow that is the union of the found  | 
   288     /// If you only need the flow that is the union of the found  | 
   287     /// arc-disjoint paths, you may call init() and findFlow().  | 
   289     /// arc-disjoint paths, you may call init() and findFlow().  | 
   288   | 
   290   | 
   289     /// @{ | 
   291     /// @{ | 
   290   | 
   292   | 
   291     /// \brief Runs the algorithm.  | 
   293     /// \brief Run the algorithm.  | 
   292     ///  | 
   294     ///  | 
   293     /// Runs the algorithm.  | 
   295     /// This function runs the algorithm.  | 
   294     ///  | 
   296     ///  | 
   295     /// \param k The number of paths to be found.  | 
   297     /// \param k The number of paths to be found.  | 
   296     ///  | 
   298     ///  | 
   297     /// \return \c k if there are at least \c k arc-disjoint paths  | 
   299     /// \return \c k if there are at least \c k arc-disjoint paths from  | 
   298     /// from \c s to \c t. Otherwise it returns the number of  | 
   300     /// \c s to \c t in the digraph. Otherwise it returns the number of  | 
   299     /// arc-disjoint paths found.  | 
   301     /// arc-disjoint paths found.  | 
   300     ///  | 
   302     ///  | 
   301     /// \note Apart from the return value, <tt>s.run(k)</tt> is just a  | 
   303     /// \note Apart from the return value, <tt>s.run(k)</tt> is just a  | 
   302     /// shortcut of the following code.  | 
   304     /// shortcut of the following code.  | 
   303     /// \code  | 
   305     /// \code  | 
   331       _dijkstra = new ResidualDijkstra( _graph, *_flow, _length,   | 
   333       _dijkstra = new ResidualDijkstra( _graph, *_flow, _length,   | 
   332                                         *_potential, _pred,  | 
   334                                         *_potential, _pred,  | 
   333                                         _source, _target );  | 
   335                                         _source, _target );  | 
   334     }  | 
   336     }  | 
   335   | 
   337   | 
   336     /// \brief Executes the successive shortest path algorithm to find  | 
   338     /// \brief Execute the successive shortest path algorithm to find  | 
   337     /// an optimal flow.  | 
   339     /// an optimal flow.  | 
   338     ///  | 
   340     ///  | 
   339     /// Executes the successive shortest path algorithm to find a  | 
   341     /// This function executes the successive shortest path algorithm to  | 
   340     /// minimum cost flow, which is the union of \c k or less  | 
   342     /// find a minimum cost flow, which is the union of \c k or less  | 
   341     /// arc-disjoint paths.  | 
   343     /// arc-disjoint paths.  | 
   342     ///  | 
   344     ///  | 
   343     /// \return \c k if there are at least \c k arc-disjoint paths  | 
   345     /// \return \c k if there are at least \c k arc-disjoint paths from  | 
   344     /// from \c s to \c t. Otherwise it returns the number of  | 
   346     /// \c s to \c t in the digraph. Otherwise it returns the number of  | 
   345     /// arc-disjoint paths found.  | 
   347     /// arc-disjoint paths found.  | 
   346     ///  | 
   348     ///  | 
   347     /// \pre \ref init() must be called before using this function.  | 
   349     /// \pre \ref init() must be called before using this function.  | 
   348     int findFlow(int k = 2) { | 
   350     int findFlow(int k = 2) { | 
   349       // Finding shortest paths  | 
   351       // Find shortest paths  | 
   350       _path_num = 0;  | 
   352       _path_num = 0;  | 
   351       while (_path_num < k) { | 
   353       while (_path_num < k) { | 
   352         // Running Dijkstra  | 
   354         // Run Dijkstra  | 
   353         if (!_dijkstra->run()) break;  | 
   355         if (!_dijkstra->run()) break;  | 
   354         ++_path_num;  | 
   356         ++_path_num;  | 
   355   | 
   357   | 
   356         // Setting the flow along the found shortest path  | 
   358         // Set the flow along the found shortest path  | 
   357         Node u = _target;  | 
   359         Node u = _target;  | 
   358         Arc e;  | 
   360         Arc e;  | 
   359         while ((e = _pred[u]) != INVALID) { | 
   361         while ((e = _pred[u]) != INVALID) { | 
   360           if (u == _graph.target(e)) { | 
   362           if (u == _graph.target(e)) { | 
   361             (*_flow)[e] = 1;  | 
   363             (*_flow)[e] = 1;  | 
   396     }  | 
   398     }  | 
   397   | 
   399   | 
   398     /// @}  | 
   400     /// @}  | 
   399   | 
   401   | 
   400     /// \name Query Functions  | 
   402     /// \name Query Functions  | 
   401     /// The result of the algorithm can be obtained using these  | 
   403     /// The results of the algorithm can be obtained using these  | 
   402     /// functions.  | 
   404     /// functions.  | 
   403     /// \n The algorithm should be executed before using them.  | 
   405     /// \n The algorithm should be executed before using them.  | 
   404   | 
   406   | 
   405     /// @{ | 
   407     /// @{ | 
   406   | 
   408   | 
   407     /// \brief Returns a const reference to the arc map storing the  | 
   409     /// \brief Return a const reference to the arc map storing the  | 
   408     /// found flow.  | 
   410     /// found flow.  | 
   409     ///  | 
   411     ///  | 
   410     /// Returns a const reference to the arc map storing the flow that  | 
   412     /// This function returns a const reference to the arc map storing  | 
   411     /// is the union of the found arc-disjoint paths.  | 
   413     /// the flow that is the union of the found arc-disjoint paths.  | 
   412     ///  | 
   414     ///  | 
   413     /// \pre \ref run() or findFlow() must be called before using this  | 
   415     /// \pre \ref run() or \ref findFlow() must be called before using  | 
   414     /// function.  | 
   416     /// this function.  | 
   415     const FlowMap& flowMap() const { | 
   417     const FlowMap& flowMap() const { | 
   416       return *_flow;  | 
   418       return *_flow;  | 
   417     }  | 
   419     }  | 
   418   | 
   420   | 
   419     /// \brief Returns a const reference to the node map storing the  | 
   421     /// \brief Return a const reference to the node map storing the  | 
   420     /// found potentials (the dual solution).  | 
   422     /// found potentials (the dual solution).  | 
   421     ///  | 
   423     ///  | 
   422     /// Returns a const reference to the node map storing the found  | 
   424     /// This function returns a const reference to the node map storing  | 
   423     /// potentials that provide the dual solution of the underlying   | 
   425     /// the found potentials that provide the dual solution of the  | 
   424     /// minimum cost flow problem.  | 
   426     /// underlying minimum cost flow problem.  | 
   425     ///  | 
   427     ///  | 
   426     /// \pre \ref run() or findFlow() must be called before using this  | 
   428     /// \pre \ref run() or \ref findFlow() must be called before using  | 
   427     /// function.  | 
   429     /// this function.  | 
   428     const PotentialMap& potentialMap() const { | 
   430     const PotentialMap& potentialMap() const { | 
   429       return *_potential;  | 
   431       return *_potential;  | 
   430     }  | 
   432     }  | 
   431   | 
   433   | 
   432     /// \brief Returns the flow on the given arc.  | 
   434     /// \brief Return the flow on the given arc.  | 
   433     ///  | 
   435     ///  | 
   434     /// Returns the flow on the given arc.  | 
   436     /// This function returns the flow on the given arc.  | 
   435     /// It is \c 1 if the arc is involved in one of the found paths,  | 
   437     /// It is \c 1 if the arc is involved in one of the found paths,  | 
   436     /// otherwise it is \c 0.  | 
   438     /// otherwise it is \c 0.  | 
   437     ///  | 
   439     ///  | 
   438     /// \pre \ref run() or findFlow() must be called before using this  | 
   440     /// \pre \ref run() or \ref findFlow() must be called before using  | 
   439     /// function.  | 
   441     /// this function.  | 
   440     int flow(const Arc& arc) const { | 
   442     int flow(const Arc& arc) const { | 
   441       return (*_flow)[arc];  | 
   443       return (*_flow)[arc];  | 
   442     }  | 
   444     }  | 
   443   | 
   445   | 
   444     /// \brief Returns the potential of the given node.  | 
   446     /// \brief Return the potential of the given node.  | 
   445     ///  | 
   447     ///  | 
   446     /// Returns the potential of the given node.  | 
   448     /// This function returns the potential of the given node.  | 
   447     ///  | 
   449     ///  | 
   448     /// \pre \ref run() or findFlow() must be called before using this  | 
   450     /// \pre \ref run() or \ref findFlow() must be called before using  | 
   449     /// function.  | 
   451     /// this function.  | 
   450     Length potential(const Node& node) const { | 
   452     Length potential(const Node& node) const { | 
   451       return (*_potential)[node];  | 
   453       return (*_potential)[node];  | 
   452     }  | 
   454     }  | 
   453   | 
   455   | 
   454     /// \brief Returns the total length (cost) of the found paths (flow).  | 
   456     /// \brief Return the total length (cost) of the found paths (flow).  | 
   455     ///  | 
   457     ///  | 
   456     /// Returns the total length (cost) of the found paths (flow).  | 
   458     /// This function returns the total length (cost) of the found paths  | 
   457     /// The complexity of the function is \f$ O(e) \f$.  | 
   459     /// (flow). The complexity of the function is \f$ O(e) \f$.  | 
   458     ///  | 
   460     ///  | 
   459     /// \pre \ref run() or findFlow() must be called before using this  | 
   461     /// \pre \ref run() or \ref findFlow() must be called before using  | 
   460     /// function.  | 
   462     /// this function.  | 
   461     Length totalLength() const { | 
   463     Length totalLength() const { | 
   462       Length c = 0;  | 
   464       Length c = 0;  | 
   463       for (ArcIt e(_graph); e != INVALID; ++e)  | 
   465       for (ArcIt e(_graph); e != INVALID; ++e)  | 
   464         c += (*_flow)[e] * _length[e];  | 
   466         c += (*_flow)[e] * _length[e];  | 
   465       return c;  | 
   467       return c;  | 
   466     }  | 
   468     }  | 
   467   | 
   469   | 
   468     /// \brief Returns the number of the found paths.  | 
   470     /// \brief Return the number of the found paths.  | 
   469     ///  | 
   471     ///  | 
   470     /// Returns the number of the found paths.  | 
   472     /// This function returns the number of the found paths.  | 
   471     ///  | 
   473     ///  | 
   472     /// \pre \ref run() or findFlow() must be called before using this  | 
   474     /// \pre \ref run() or \ref findFlow() must be called before using  | 
   473     /// function.  | 
   475     /// this function.  | 
   474     int pathNum() const { | 
   476     int pathNum() const { | 
   475       return _path_num;  | 
   477       return _path_num;  | 
   476     }  | 
   478     }  | 
   477   | 
   479   | 
   478     /// \brief Returns a const reference to the specified path.  | 
   480     /// \brief Return a const reference to the specified path.  | 
   479     ///  | 
   481     ///  | 
   480     /// Returns a const reference to the specified path.  | 
   482     /// This function returns a const reference to the specified path.  | 
   481     ///  | 
   483     ///  | 
   482     /// \param i The function returns the \c i-th path.  | 
   484     /// \param i The function returns the \c i-th path.  | 
   483     /// \c i must be between \c 0 and <tt>%pathNum()-1</tt>.  | 
   485     /// \c i must be between \c 0 and <tt>%pathNum()-1</tt>.  | 
   484     ///  | 
   486     ///  | 
   485     /// \pre \ref run() or findPaths() must be called before using this  | 
   487     /// \pre \ref run() or \ref findPaths() must be called before using  | 
   486     /// function.  | 
   488     /// this function.  | 
   487     Path path(int i) const { | 
   489     Path path(int i) const { | 
   488       return paths[i];  | 
   490       return paths[i];  | 
   489     }  | 
   491     }  | 
   490   | 
   492   | 
   491     /// @}  | 
   493     /// @}  |