[Lemon-commits] Peter Kovacs: Improvements related to BFS/DFS/Di...

Lemon HG hg at lemon.cs.elte.hu
Sun Sep 28 11:15:12 CEST 2008


details:   http://lemon.cs.elte.hu/hg/lemon/rev/da414906fe21
changeset: 286:da414906fe21
user:      Peter Kovacs <kpeter [at] inf.elte.hu>
date:      Fri Sep 26 12:40:11 2008 +0200
description:
	Improvements related to BFS/DFS/Dijkstra (ticket #96)
	 - Add run(s,t) function to BfsVisit.
	 - Modify run(s,t) functions in the class interfaces to return bool
	value.
	 - Bug fix in Dijkstra::start(t) function.
	 - Improve Dijkstra::currentDist().
	 - Extend test files to check named class template parameters.
	 - Doc improvements.

diffstat:

6 files changed, 195 insertions(+), 89 deletions(-)
lemon/bfs.h           |   56 ++++++++++++++++++++++++++++++++++---------------
lemon/dfs.h           |   44 +++++++++++++++++++-------------------
lemon/dijkstra.h      |   41 +++++++++++++++++++++--------------
test/bfs_test.cc      |   47 +++++++++++++++++++++++++++++++----------
test/dfs_test.cc      |   47 +++++++++++++++++++++++++++++++----------
test/dijkstra_test.cc |   49 +++++++++++++++++++++++++++++++++---------

diffs (truncated from 565 to 300 lines):

diff -r 6307bbbf285b -r da414906fe21 lemon/bfs.h
--- a/lemon/bfs.h	Tue Sep 23 18:42:49 2008 +0200
+++ b/lemon/bfs.h	Fri Sep 26 12:40:11 2008 +0200
@@ -607,11 +607,11 @@
     ///Executes the algorithm until the given target node is reached.
     ///
     ///This method runs the %BFS algorithm from the root node(s)
-    ///in order to compute the shortest path to \c dest.
+    ///in order to compute the shortest path to \c t.
     ///
     ///The algorithm computes
-    ///- the shortest path to \c dest,
-    ///- the distance of \c dest from the root(s).
+    ///- the shortest path to \c t,
+    ///- the distance of \c t from the root(s).
     ///
     ///\pre init() must be called and at least one root node should be
     ///added with addSource() before using this function.
@@ -623,10 +623,10 @@
     ///    b.processNextNode(t, reach);
     ///  }
     ///\endcode
-    void start(Node dest)
+    void start(Node t)
     {
       bool reach = false;
-      while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
+      while ( !emptyQueue() && !reach ) processNextNode(t, reach);
     }
 
     ///Executes the algorithm until a condition is met.
@@ -664,7 +664,7 @@
       return rnode;
     }
 
-    ///Runs the algorithm from the given node.
+    ///Runs the algorithm from the given source node.
 
     ///This method runs the %BFS algorithm from node \c s
     ///in order to compute the shortest path to each node.
@@ -688,10 +688,10 @@
     ///Finds the shortest path between \c s and \c t.
 
     ///This method runs the %BFS algorithm from node \c s
-    ///in order to compute the shortest path to \c t.
+    ///in order to compute the shortest path to node \c t
+    ///(it stops searching when \c t is processed).
     ///
-    ///\return The length of the shortest <tt>s</tt>--<tt>t</tt> path,
-    ///if \c t is reachable form \c s, \c 0 otherwise.
+    ///\return \c true if \c t is reachable form \c s.
     ///
     ///\note Apart from the return value, <tt>b.run(s,t)</tt> is just a
     ///shortcut of the following code.
@@ -700,11 +700,11 @@
     ///  b.addSource(s);
     ///  b.start(t);
     ///\endcode
-    int run(Node s,Node t) {
+    bool run(Node s,Node t) {
       init();
       addSource(s);
       start(t);
-      return reached(t) ? _curr_dist : 0;
+      return reached(t);
     }
 
     ///Runs the algorithm to visit all nodes in the digraph.
@@ -1621,11 +1621,11 @@
     /// Executes the algorithm until the given target node is reached.
     ///
     /// This method runs the %BFS algorithm from the root node(s)
-    /// in order to compute the shortest path to \c dest.
+    /// in order to compute the shortest path to \c t.
     ///
     /// The algorithm computes
-    /// - the shortest path to \c dest,
-    /// - the distance of \c dest from the root(s).
+    /// - the shortest path to \c t,
+    /// - the distance of \c t from the root(s).
     ///
     /// \pre init() must be called and at least one root node should be
     /// added with addSource() before using this function.
@@ -1637,9 +1637,9 @@
     ///     b.processNextNode(t, reach);
     ///   }
     /// \endcode
-    void start(Node dest) {
+    void start(Node t) {
       bool reach = false;
-      while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
+      while ( !emptyQueue() && !reach ) processNextNode(t, reach);
     }
 
     /// \brief Executes the algorithm until a condition is met.
@@ -1677,7 +1677,7 @@
       return rnode;
     }
 
-    /// \brief Runs the algorithm from the given node.
+    /// \brief Runs the algorithm from the given source node.
     ///
     /// This method runs the %BFS algorithm from node \c s
     /// in order to compute the shortest path to each node.
@@ -1696,6 +1696,28 @@
       init();
       addSource(s);
       start();
+    }
+
+    /// \brief Finds the shortest path between \c s and \c t.
+    ///
+    /// This method runs the %BFS algorithm from node \c s
+    /// in order to compute the shortest path to node \c t
+    /// (it stops searching when \c t is processed).
+    ///
+    /// \return \c true if \c t is reachable form \c s.
+    ///
+    /// \note Apart from the return value, <tt>b.run(s,t)</tt> is just a
+    /// shortcut of the following code.
+    ///\code
+    ///   b.init();
+    ///   b.addSource(s);
+    ///   b.start(t);
+    ///\endcode
+    bool run(Node s,Node t) {
+      init();
+      addSource(s);
+      start(t);
+      return reached(t);
     }
 
     /// \brief Runs the algorithm to visit all nodes in the digraph.
diff -r 6307bbbf285b -r da414906fe21 lemon/dfs.h
--- a/lemon/dfs.h	Tue Sep 23 18:42:49 2008 +0200
+++ b/lemon/dfs.h	Fri Sep 26 12:40:11 2008 +0200
@@ -558,17 +558,17 @@
     ///Executes the algorithm until the given target node is reached.
     ///
     ///This method runs the %DFS algorithm from the root node
-    ///in order to compute the DFS path to \c dest.
+    ///in order to compute the DFS path to \c t.
     ///
     ///The algorithm computes
-    ///- the %DFS path to \c dest,
-    ///- the distance of \c dest from the root in the %DFS tree.
+    ///- the %DFS path to \c t,
+    ///- the distance of \c t from the root in the %DFS tree.
     ///
     ///\pre init() must be called and a root node should be
     ///added with addSource() before using this function.
-    void start(Node dest)
+    void start(Node t)
     {
-      while ( !emptyQueue() && G->target(_stack[_stack_head])!=dest )
+      while ( !emptyQueue() && G->target(_stack[_stack_head])!=t )
         processNextArc();
     }
 
@@ -598,7 +598,7 @@
       return emptyQueue() ? INVALID : _stack[_stack_head];
     }
 
-    ///Runs the algorithm from the given node.
+    ///Runs the algorithm from the given source node.
 
     ///This method runs the %DFS algorithm from node \c s
     ///in order to compute the DFS path to each node.
@@ -622,10 +622,10 @@
     ///Finds the %DFS path between \c s and \c t.
 
     ///This method runs the %DFS algorithm from node \c s
-    ///in order to compute the DFS path to \c t.
+    ///in order to compute the DFS path to node \c t
+    ///(it stops searching when \c t is processed)
     ///
-    ///\return The length of the <tt>s</tt>--<tt>t</tt> DFS path,
-    ///if \c t is reachable form \c s, \c 0 otherwise.
+    ///\return \c true if \c t is reachable form \c s.
     ///
     ///\note Apart from the return value, <tt>d.run(s,t)</tt> is
     ///just a shortcut of the following code.
@@ -634,11 +634,11 @@
     ///  d.addSource(s);
     ///  d.start(t);
     ///\endcode
-    int run(Node s,Node t) {
+    bool run(Node s,Node t) {
       init();
       addSource(s);
       start(t);
-      return reached(t)?_stack_head+1:0;
+      return reached(t);
     }
 
     ///Runs the algorithm to visit all nodes in the digraph.
@@ -1526,16 +1526,16 @@
     /// Executes the algorithm until the given target node is reached.
     ///
     /// This method runs the %DFS algorithm from the root node
-    /// in order to compute the DFS path to \c dest.
+    /// in order to compute the DFS path to \c t.
     ///
     /// The algorithm computes
-    /// - the %DFS path to \c dest,
-    /// - the distance of \c dest from the root in the %DFS tree.
+    /// - the %DFS path to \c t,
+    /// - the distance of \c t from the root in the %DFS tree.
     ///
     /// \pre init() must be called and a root node should be added
     /// with addSource() before using this function.
-    void start(Node dest) {
-      while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != dest )
+    void start(Node t) {
+      while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != t )
         processNextArc();
     }
 
@@ -1564,7 +1564,7 @@
       return emptyQueue() ? INVALID : _stack[_stack_head];
     }
 
-    /// \brief Runs the algorithm from the given node.
+    /// \brief Runs the algorithm from the given source node.
     ///
     /// This method runs the %DFS algorithm from node \c s.
     /// in order to compute the DFS path to each node.
@@ -1588,10 +1588,10 @@
     /// \brief Finds the %DFS path between \c s and \c t.
 
     /// This method runs the %DFS algorithm from node \c s
-    /// in order to compute the DFS path to \c t.
+    /// in order to compute the DFS path to node \c t
+    /// (it stops searching when \c t is processed).
     ///
-    /// \return The length of the <tt>s</tt>--<tt>t</tt> DFS path,
-    /// if \c t is reachable form \c s, \c 0 otherwise.
+    /// \return \c true if \c t is reachable form \c s.
     ///
     /// \note Apart from the return value, <tt>d.run(s,t)</tt> is
     /// just a shortcut of the following code.
@@ -1600,11 +1600,11 @@
     ///   d.addSource(s);
     ///   d.start(t);
     ///\endcode
-    int run(Node s,Node t) {
+    bool run(Node s,Node t) {
       init();
       addSource(s);
       start(t);
-      return reached(t)?_stack_head+1:0;
+      return reached(t);
     }
 
     /// \brief Runs the algorithm to visit all nodes in the digraph.
diff -r 6307bbbf285b -r da414906fe21 lemon/dijkstra.h
--- a/lemon/dijkstra.h	Tue Sep 23 18:42:49 2008 +0200
+++ b/lemon/dijkstra.h	Fri Sep 26 12:40:11 2008 +0200
@@ -728,23 +728,26 @@
       while ( !emptyQueue() ) processNextNode();
     }
 
-    ///Executes the algorithm until the given target node is reached.
+    ///Executes the algorithm until the given target node is processed.
 
-    ///Executes the algorithm until the given target node is reached.
+    ///Executes the algorithm until the given target node is processed.
     ///
     ///This method runs the %Dijkstra algorithm from the root node(s)
-    ///in order to compute the shortest path to \c dest.
+    ///in order to compute the shortest path to \c t.
     ///
     ///The algorithm computes
-    ///- the shortest path to \c dest,
-    ///- the distance of \c dest from the root(s).
+    ///- the shortest path to \c t,
+    ///- the distance of \c t from the root(s).
     ///
     ///\pre init() must be called and at least one root node should be
     ///added with addSource() before using this function.
-    void start(Node dest)
+    void start(Node t)
     {
-      while ( !_heap->empty() && _heap->top()!=dest ) processNextNode();
-      if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio());
+      while ( !_heap->empty() && _heap->top()!=t ) processNextNode();
+      if ( !_heap->empty() ) {
+        finalizeNodeData(_heap->top(),_heap->prio());
+        _heap->pop();
+      }
     }
 
     ///Executes the algorithm until a condition is met.
@@ -772,7 +775,7 @@
       return _heap->top();
     }
 
-    ///Runs the algorithm from the given node.
+    ///Runs the algorithm from the given source node.
 



More information about the Lemon-commits mailing list