Index: lemon/suurballe.h
===================================================================
--- lemon/suurballe.h	(revision 931)
+++ lemon/suurballe.h	(revision 670)
@@ -30,51 +30,7 @@
 #include <lemon/path.h>
 #include <lemon/list_graph.h>
-#include <lemon/dijkstra.h>
 #include <lemon/maps.h>
 
 namespace lemon {
-
-  /// \brief Default traits class of Suurballe algorithm.
-  ///
-  /// Default traits class of Suurballe algorithm.
-  /// \tparam GR The digraph type the algorithm runs on.
-  /// \tparam LEN The type of the length map.
-  /// The default value is <tt>GR::ArcMap<int></tt>.
-#ifdef DOXYGEN
-  template <typename GR, typename LEN>
-#else
-  template < typename GR,
-             typename LEN = typename GR::template ArcMap<int> >
-#endif
-  struct SuurballeDefaultTraits
-  {
-    /// The type of the digraph.
-    typedef GR Digraph;
-    /// The type of the length map.
-    typedef LEN LengthMap;
-    /// The type of the lengths.
-    typedef typename LEN::Value Length;
-    /// The type of the flow map.
-    typedef typename GR::template ArcMap<int> FlowMap;
-    /// The type of the potential map.
-    typedef typename GR::template NodeMap<Length> PotentialMap;
-
-    /// \brief The path type
-    ///
-    /// The type used for storing the found arc-disjoint paths.
-    /// It must conform to the \ref lemon::concepts::Path "Path" concept
-    /// and it must have an \c addBack() function.
-    typedef lemon::Path<Digraph> Path;
-    
-    /// The cross reference type used for the heap.
-    typedef typename GR::template NodeMap<int> HeapCrossRef;
-
-    /// \brief The heap type used for internal Dijkstra computations.
-    ///
-    /// The type of the heap used for internal Dijkstra computations.
-    /// It must conform to the \ref lemon::concepts::Heap "Heap" concept
-    /// and its priority type must be \c Length.
-    typedef BinHeap<Length, HeapCrossRef> Heap;
-  };
 
   /// \addtogroup shortest_path
@@ -91,5 +47,5 @@
   /// "minimum cost flow problem". This implementation is actually an
   /// efficient specialized version of the \ref CapacityScaling
-  /// "successive shortest path" algorithm directly for this problem.
+  /// "Successive Shortest Path" algorithm directly for this problem.
   /// Therefore this class provides query functions for flow values and
   /// node potentials (the dual solution) just like the minimum cost flow
@@ -100,14 +56,13 @@
   /// The default value is <tt>GR::ArcMap<int></tt>.
   ///
-  /// \warning Length values should be \e non-negative.
+  /// \warning Length values should be \e non-negative \e integers.
   ///
-  /// \note For finding \e node-disjoint paths, this algorithm can be used
+  /// \note For finding node-disjoint paths this algorithm can be used
   /// along with the \ref SplitNodes adaptor.
 #ifdef DOXYGEN
-  template <typename GR, typename LEN, typename TR>
+  template <typename GR, typename LEN>
 #else
   template < typename GR,
-             typename LEN = typename GR::template ArcMap<int>,
-             typename TR = SuurballeDefaultTraits<GR, LEN> >
+             typename LEN = typename GR::template ArcMap<int> >
 #endif
   class Suurballe
@@ -120,24 +75,24 @@
   public:
 
-    /// The type of the digraph.
-    typedef typename TR::Digraph Digraph;
+    /// The type of the digraph the algorithm runs on.
+    typedef GR Digraph;
     /// The type of the length map.
-    typedef typename TR::LengthMap LengthMap;
+    typedef LEN LengthMap;
     /// The type of the lengths.
-    typedef typename TR::Length Length;
-
+    typedef typename LengthMap::Value Length;
+#ifdef DOXYGEN
     /// The type of the flow map.
-    typedef typename TR::FlowMap FlowMap;
+    typedef GR::ArcMap<int> FlowMap;
     /// The type of the potential map.
-    typedef typename TR::PotentialMap PotentialMap;
+    typedef GR::NodeMap<Length> PotentialMap;
+#else
+    /// The type of the flow map.
+    typedef typename Digraph::template ArcMap<int> FlowMap;
+    /// The type of the potential map.
+    typedef typename Digraph::template NodeMap<Length> PotentialMap;
+#endif
+
     /// The type of the path structures.
-    typedef typename TR::Path Path;
-    /// The cross reference type used for the heap.
-    typedef typename TR::HeapCrossRef HeapCrossRef;
-    /// The heap type used for internal Dijkstra computations.
-    typedef typename TR::Heap Heap;
-
-    /// The \ref SuurballeDefaultTraits "traits class" of the algorithm.
-    typedef TR Traits;
+    typedef SimplePath<GR> Path;
 
   private:
@@ -150,36 +105,42 @@
     class ResidualDijkstra
     {
+      typedef typename Digraph::template NodeMap<int> HeapCrossRef;
+      typedef BinHeap<Length, HeapCrossRef> Heap;
+
     private:
 
+      // The digraph the algorithm runs on
       const Digraph &_graph;
+
+      // The main maps
+      const FlowMap &_flow;
       const LengthMap &_length;
-      const FlowMap &_flow;
-      PotentialMap &_pi;
+      PotentialMap &_potential;
+
+      // The distance map
+      PotentialMap _dist;
+      // The pred arc map
       PredMap &_pred;
+      // The processed (i.e. permanently labeled) nodes
+      std::vector<Node> _proc_nodes;
+
       Node _s;
       Node _t;
-      
-      PotentialMap _dist;
-      std::vector<Node> _proc_nodes;
 
     public:
 
-      // Constructor
-      ResidualDijkstra(Suurballe &srb) :
-        _graph(srb._graph), _length(srb._length),
-        _flow(*srb._flow), _pi(*srb._potential), _pred(srb._pred), 
-        _s(srb._s), _t(srb._t), _dist(_graph) {}
-        
-      // Run the algorithm and return true if a path is found
-      // from the source node to the target node.
-      bool run(int cnt) {
-        return cnt == 0 ? startFirst() : start();
-      }
-
-    private:
-    
-      // Execute the algorithm for the first time (the flow and potential
-      // functions have to be identically zero).
-      bool startFirst() {
+      /// Constructor.
+      ResidualDijkstra( const Digraph &graph,
+                        const FlowMap &flow,
+                        const LengthMap &length,
+                        PotentialMap &potential,
+                        PredMap &pred,
+                        Node s, Node t ) :
+        _graph(graph), _flow(flow), _length(length), _potential(potential),
+        _dist(graph), _pred(pred), _s(s), _t(t) {}
+
+      /// \brief Run the algorithm. It returns \c true if a path is found
+      /// from the source node to the target node.
+      bool run() {
         HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
         Heap heap(heap_cross_ref);
@@ -191,21 +152,22 @@
         while (!heap.empty() && heap.top() != _t) {
           Node u = heap.top(), v;
-          Length d = heap.prio(), dn;
+          Length d = heap.prio() + _potential[u], nd;
           _dist[u] = heap.prio();
+          heap.pop();
           _proc_nodes.push_back(u);
-          heap.pop();
 
           // Traverse outgoing arcs
           for (OutArcIt e(_graph, u); e != INVALID; ++e) {
-            v = _graph.target(e);
-            switch(heap.state(v)) {
+            if (_flow[e] == 0) {
+              v = _graph.target(e);
+              switch(heap.state(v)) {
               case Heap::PRE_HEAP:
-                heap.push(v, d + _length[e]);
+                heap.push(v, d + _length[e] - _potential[v]);
                 _pred[v] = e;
                 break;
               case Heap::IN_HEAP:
-                dn = d + _length[e];
-                if (dn < heap[v]) {
-                  heap.decrease(v, dn);
+                nd = d + _length[e] - _potential[v];
+                if (nd < heap[v]) {
+                  heap.decrease(v, nd);
                   _pred[v] = e;
                 }
@@ -213,50 +175,4 @@
               case Heap::POST_HEAP:
                 break;
-            }
-          }
-        }
-        if (heap.empty()) return false;
-
-        // Update potentials of processed nodes
-        Length t_dist = heap.prio();
-        for (int i = 0; i < int(_proc_nodes.size()); ++i)
-          _pi[_proc_nodes[i]] = _dist[_proc_nodes[i]] - t_dist;
-        return true;
-      }
-
-      // Execute the algorithm.
-      bool start() {
-        HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP);
-        Heap heap(heap_cross_ref);
-        heap.push(_s, 0);
-        _pred[_s] = INVALID;
-        _proc_nodes.clear();
-
-        // Process nodes
-        while (!heap.empty() && heap.top() != _t) {
-          Node u = heap.top(), v;
-          Length d = heap.prio() + _pi[u], dn;
-          _dist[u] = heap.prio();
-          _proc_nodes.push_back(u);
-          heap.pop();
-
-          // Traverse outgoing arcs
-          for (OutArcIt e(_graph, u); e != INVALID; ++e) {
-            if (_flow[e] == 0) {
-              v = _graph.target(e);
-              switch(heap.state(v)) {
-                case Heap::PRE_HEAP:
-                  heap.push(v, d + _length[e] - _pi[v]);
-                  _pred[v] = e;
-                  break;
-                case Heap::IN_HEAP:
-                  dn = d + _length[e] - _pi[v];
-                  if (dn < heap[v]) {
-                    heap.decrease(v, dn);
-                    _pred[v] = e;
-                  }
-                  break;
-                case Heap::POST_HEAP:
-                  break;
               }
             }
@@ -268,17 +184,17 @@
               v = _graph.source(e);
               switch(heap.state(v)) {
-                case Heap::PRE_HEAP:
-                  heap.push(v, d - _length[e] - _pi[v]);
+              case Heap::PRE_HEAP:
+                heap.push(v, d - _length[e] - _potential[v]);
+                _pred[v] = e;
+                break;
+              case Heap::IN_HEAP:
+                nd = d - _length[e] - _potential[v];
+                if (nd < heap[v]) {
+                  heap.decrease(v, nd);
                   _pred[v] = e;
-                  break;
-                case Heap::IN_HEAP:
-                  dn = d - _length[e] - _pi[v];
-                  if (dn < heap[v]) {
-                    heap.decrease(v, dn);
-                    _pred[v] = e;
-                  }
-                  break;
-                case Heap::POST_HEAP:
-                  break;
+                }
+                break;
+              case Heap::POST_HEAP:
+                break;
               }
             }
@@ -290,86 +206,9 @@
         Length t_dist = heap.prio();
         for (int i = 0; i < int(_proc_nodes.size()); ++i)
-          _pi[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist;
+          _potential[_proc_nodes[i]] += _dist[_proc_nodes[i]] - t_dist;
         return true;
       }
 
     }; //class ResidualDijkstra
-
-  public:
-
-    /// \name Named Template Parameters
-    /// @{
-
-    template <typename T>
-    struct SetFlowMapTraits : public Traits {
-      typedef T FlowMap;
-    };
-
-    /// \brief \ref named-templ-param "Named parameter" for setting
-    /// \c FlowMap type.
-    ///
-    /// \ref named-templ-param "Named parameter" for setting
-    /// \c FlowMap type.
-    template <typename T>
-    struct SetFlowMap
-      : public Suurballe<GR, LEN, SetFlowMapTraits<T> > {
-      typedef Suurballe<GR, LEN, SetFlowMapTraits<T> > Create;
-    };
-
-    template <typename T>
-    struct SetPotentialMapTraits : public Traits {
-      typedef T PotentialMap;
-    };
-
-    /// \brief \ref named-templ-param "Named parameter" for setting
-    /// \c PotentialMap type.
-    ///
-    /// \ref named-templ-param "Named parameter" for setting
-    /// \c PotentialMap type.
-    template <typename T>
-    struct SetPotentialMap
-      : public Suurballe<GR, LEN, SetPotentialMapTraits<T> > {
-      typedef Suurballe<GR, LEN, SetPotentialMapTraits<T> > Create;
-    };
-
-    template <typename T>
-    struct SetPathTraits : public Traits {
-      typedef T Path;
-    };
-
-    /// \brief \ref named-templ-param "Named parameter" for setting
-    /// \c %Path type.
-    ///
-    /// \ref named-templ-param "Named parameter" for setting \c %Path type.
-    /// It must conform to the \ref lemon::concepts::Path "Path" concept
-    /// and it must have an \c addBack() function.
-    template <typename T>
-    struct SetPath
-      : public Suurballe<GR, LEN, SetPathTraits<T> > {
-      typedef Suurballe<GR, LEN, SetPathTraits<T> > Create;
-    };
-    
-    template <typename H, typename CR>
-    struct SetHeapTraits : public Traits {
-      typedef H Heap;
-      typedef CR HeapCrossRef;
-    };
-
-    /// \brief \ref named-templ-param "Named parameter" for setting
-    /// \c Heap and \c HeapCrossRef types.
-    ///
-    /// \ref named-templ-param "Named parameter" for setting \c Heap
-    /// and \c HeapCrossRef types with automatic allocation. 
-    /// They will be used for internal Dijkstra computations.
-    /// The heap type must conform to the \ref lemon::concepts::Heap "Heap"
-    /// concept and its priority type must be \c Length.
-    template <typename H,
-              typename CR = typename Digraph::template NodeMap<int> >
-    struct SetHeap
-      : public Suurballe<GR, LEN, SetHeapTraits<H, CR> > {
-      typedef Suurballe<GR, LEN, SetHeapTraits<H, CR> > Create;
-    };
-
-    /// @}
 
   private:
@@ -388,19 +227,17 @@
 
     // The source node
-    Node _s;
+    Node _source;
     // The target node
-    Node _t;
+    Node _target;
 
     // Container to store the found paths
-    std::vector<Path> _paths;
+    std::vector< SimplePath<Digraph> > paths;
     int _path_num;
 
     // The pred arc map
     PredMap _pred;
-    
-    // Data for full init
-    PotentialMap *_init_dist;
-    PredMap *_init_pred;
-    bool _full_init;
+    // Implementation of the Dijkstra algorithm for finding augmenting
+    // shortest paths in the residual network
+    ResidualDijkstra *_dijkstra;
 
   public:
@@ -415,7 +252,9 @@
                const LengthMap &length ) :
       _graph(graph), _length(length), _flow(0), _local_flow(false),
-      _potential(0), _local_potential(false), _pred(graph),
-      _init_dist(0), _init_pred(0)
-    {}
+      _potential(0), _local_potential(false), _pred(graph)
+    {
+      LEMON_ASSERT(std::numeric_limits<Length>::is_integer,
+        "The length type of Suurballe must be integer");
+    }
 
     /// Destructor.
@@ -423,6 +262,5 @@
       if (_local_flow) delete _flow;
       if (_local_potential) delete _potential;
-      delete _init_dist;
-      delete _init_pred;
+      delete _dijkstra;
     }
 
@@ -469,11 +307,8 @@
     /// \name Execution Control
     /// The simplest way to execute the algorithm is to call the run()
-    /// function.\n
-    /// If you need to execute the algorithm many times using the same
-    /// source node, then you may call fullInit() once and start()
-    /// for each target node.\n
+    /// function.
+    /// \n
     /// If you only need the flow that is the union of the found
-    /// arc-disjoint paths, then you may call findFlow() instead of
-    /// start().
+    /// arc-disjoint paths, you may call init() and findFlow().
 
     /// @{
@@ -495,9 +330,11 @@
     /// \code
     ///   s.init(s);
-    ///   s.start(t, k);
+    ///   s.findFlow(t, k);
+    ///   s.findPaths();
     /// \endcode
     int run(const Node& s, const Node& t, int k = 2) {
       init(s);
-      start(t, k);
+      findFlow(t, k);
+      findPaths();
       return _path_num;
     }
@@ -505,9 +342,9 @@
     /// \brief Initialize the algorithm.
     ///
-    /// This function initializes the algorithm with the given source node.
+    /// This function initializes the algorithm.
     ///
     /// \param s The source node.
     void init(const Node& s) {
-      _s = s;
+      _source = s;
 
       // Initialize maps
@@ -520,61 +357,6 @@
         _local_potential = true;
       }
-      _full_init = false;
-    }
-
-    /// \brief Initialize the algorithm and perform Dijkstra.
-    ///
-    /// This function initializes the algorithm and performs a full
-    /// Dijkstra search from the given source node. It makes consecutive
-    /// executions of \ref start() "start(t, k)" faster, since they
-    /// have to perform %Dijkstra only k-1 times.
-    ///
-    /// This initialization is usually worth using instead of \ref init()
-    /// if the algorithm is executed many times using the same source node.
-    ///
-    /// \param s The source node.
-    void fullInit(const Node& s) {
-      // Initialize maps
-      init(s);
-      if (!_init_dist) {
-        _init_dist = new PotentialMap(_graph);
-      }
-      if (!_init_pred) {
-        _init_pred = new PredMap(_graph);
-      }
-
-      // Run a full Dijkstra
-      typename Dijkstra<Digraph, LengthMap>
-        ::template SetStandardHeap<Heap>
-        ::template SetDistMap<PotentialMap>
-        ::template SetPredMap<PredMap>
-        ::Create dijk(_graph, _length);
-      dijk.distMap(*_init_dist).predMap(*_init_pred);
-      dijk.run(s);
-      
-      _full_init = true;
-    }
-
-    /// \brief Execute the algorithm.
-    ///
-    /// This function executes the algorithm.
-    ///
-    /// \param t The target node.
-    /// \param k The number of paths to be found.
-    ///
-    /// \return \c k if there are at least \c k arc-disjoint paths from
-    /// \c s to \c t in the digraph. Otherwise it returns the number of
-    /// arc-disjoint paths found.
-    ///
-    /// \note Apart from the return value, <tt>s.start(t, k)</tt> is
-    /// just a shortcut of the following code.
-    /// \code
-    ///   s.findFlow(t, k);
-    ///   s.findPaths();
-    /// \endcode
-    int start(const Node& t, int k = 2) {
-      findFlow(t, k);
-      findPaths();
-      return _path_num;
+      for (ArcIt e(_graph); e != INVALID; ++e) (*_flow)[e] = 0;
+      for (NodeIt n(_graph); n != INVALID; ++n) (*_potential)[n] = 0;
     }
 
@@ -594,37 +376,18 @@
     /// \pre \ref init() must be called before using this function.
     int findFlow(const Node& t, int k = 2) {
-      _t = t;
-      ResidualDijkstra dijkstra(*this);
-      
-      // Initialization
-      for (ArcIt e(_graph); e != INVALID; ++e) {
-        (*_flow)[e] = 0;
-      }
-      if (_full_init) {
-        for (NodeIt n(_graph); n != INVALID; ++n) {
-          (*_potential)[n] = (*_init_dist)[n];
-        }
-        Node u = _t;
-        Arc e;
-        while ((e = (*_init_pred)[u]) != INVALID) {
-          (*_flow)[e] = 1;
-          u = _graph.source(e);
-        }        
-        _path_num = 1;
-      } else {
-        for (NodeIt n(_graph); n != INVALID; ++n) {
-          (*_potential)[n] = 0;
-        }
-        _path_num = 0;
-      }
+      _target = t;
+      _dijkstra =
+        new ResidualDijkstra( _graph, *_flow, _length, *_potential, _pred,
+                              _source, _target );
 
       // Find shortest paths
+      _path_num = 0;
       while (_path_num < k) {
         // Run Dijkstra
-        if (!dijkstra.run(_path_num)) break;
+        if (!_dijkstra->run()) break;
         ++_path_num;
 
         // Set the flow along the found shortest path
-        Node u = _t;
+        Node u = _target;
         Arc e;
         while ((e = _pred[u]) != INVALID) {
@@ -643,6 +406,6 @@
     /// \brief Compute the paths from the flow.
     ///
-    /// This function computes arc-disjoint paths from the found minimum
-    /// cost flow, which is the union of them.
+    /// This function computes the paths from the found minimum cost flow,
+    /// which is the union of some arc-disjoint paths.
     ///
     /// \pre \ref init() and \ref findFlow() must be called before using
@@ -652,13 +415,13 @@
       for(ArcIt a(_graph); a != INVALID; ++a) res_flow[a] = (*_flow)[a];
 
-      _paths.clear();
-      _paths.resize(_path_num);
+      paths.clear();
+      paths.resize(_path_num);
       for (int i = 0; i < _path_num; ++i) {
-        Node n = _s;
-        while (n != _t) {
+        Node n = _source;
+        while (n != _target) {
           OutArcIt e(_graph, n);
           for ( ; res_flow[e] == 0; ++e) ;
           n = _graph.target(e);
-          _paths[i].addBack(e);
+          paths[i].addBack(e);
           res_flow[e] = 0;
         }
@@ -758,6 +521,6 @@
     /// \pre \ref run() or \ref findPaths() must be called before using
     /// this function.
-    const Path& path(int i) const {
-      return _paths[i];
+    Path path(int i) const {
+      return paths[i];
     }
 
