[Lemon-commits] [lemon_svn] athos: r824 - in hugo/trunk/src: hugo work/athos

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:41:41 CET 2006


Author: athos
Date: Thu May 13 18:00:18 2004
New Revision: 824

Added:
   hugo/trunk/src/work/athos/mincostflow.h
      - copied, changed from r823, /hugo/trunk/src/hugo/mincostflows.h
Modified:
   hugo/trunk/src/hugo/mincostflows.h

Log:
Slight modifications.

Modified: hugo/trunk/src/hugo/mincostflows.h
==============================================================================
--- hugo/trunk/src/hugo/mincostflows.h	(original)
+++ hugo/trunk/src/hugo/mincostflows.h	Thu May 13 18:00:18 2004
@@ -92,11 +92,6 @@
     //To store the potentila (dual variables)
     typename Graph::template NodeMap<Length> potential;
     
-    //Container to store found paths
-    //std::vector< std::vector<Edge> > paths;
-    //typedef DirPath<Graph> DPath;
-    //DPath paths;
-
 
     Length total_length;
 
@@ -151,6 +146,11 @@
 	  break;
 	};
 	
+	//We have to copy the potential
+	FOR_EACH_LOC(typename ResGraphType::NodeIt, n, res_graph){
+	  potential[n] += dijkstra.distMap()[n];
+	}
+	/*
 	{
 	  //We have to copy the potential
 	  typename ResGraphType::NodeIt n;
@@ -158,7 +158,7 @@
 	      potential[n] += dijkstra.distMap()[n];
 	  }
 	}
-
+	*/
 
 	//Augmenting on the sortest path
 	Node n=t;
@@ -225,25 +225,6 @@
       return true;
     }
     
-    /*
-      ///\todo To be implemented later
-
-    ///This function gives back the \c j-th path in argument p.
-    ///Assumes that \c run() has been run and nothing changed since then.
-    /// \warning It is assumed that \c p is constructed to be a path of graph \c G. If \c j is greater than the result of previous \c run, then the result here will be an empty path.
-    template<typename DirPath>
-    void getPath(DirPath& p, int j){
-      p.clear();
-      typename DirPath::Builder B(p);
-      for(typename std::vector<Edge>::iterator i=paths[j].begin(); 
-	  i!=paths[j].end(); ++i ){
-	B.pushBack(*i);
-      }
-
-      B.commit();
-    }
-
-    */
 
   }; //class MinCostFlows
 
@@ -251,4 +232,4 @@
 
 } //namespace hugo
 
-#endif //HUGO_MINCOSTFLOW_H
+#endif //HUGO_MINCOSTFLOWS_H

Copied: hugo/trunk/src/work/athos/mincostflow.h (from r823, /hugo/trunk/src/hugo/mincostflows.h)
==============================================================================
--- /hugo/trunk/src/hugo/mincostflows.h	(original)
+++ hugo/trunk/src/work/athos/mincostflow.h	Thu May 13 18:00:18 2004
@@ -1,6 +1,6 @@
 // -*- c++ -*-
-#ifndef HUGO_MINCOSTFLOWS_H
-#define HUGO_MINCOSTFLOWS_H
+#ifndef HUGO_MINCOSTFLOW_H
+#define HUGO_MINCOSTFLOW_H
 
 ///\ingroup galgs
 ///\file
@@ -22,11 +22,13 @@
   ///(for small values of \c k) having minimal total cost between 2 nodes 
   /// 
   ///
-  /// The class \ref hugo::MinCostFlows "MinCostFlows" implements
-  /// an algorithm for finding a flow of value \c k 
-  ///(for small values of \c k) having minimal total cost  
-  /// from a given source node to a given target node in an
-  /// edge-weighted directed graph having nonnegative integer capacities.
+  /// The class \ref hugo::MinCostFlow "MinCostFlow" implements
+  /// an algorithm for solving the following general minimum cost flow problem>
+  /// 
+  ///
+  ///
+  /// \warning It is assumed here that the problem has a feasible solution
+  ///
   /// The range of the length (weight) function is nonnegative reals but 
   /// the range of capacity function is the set of nonnegative integers. 
   /// It is not a polinomial time algorithm for counting the minimum cost
@@ -34,13 +36,13 @@
   /// where \c M is the value of the maximal flow.
   ///
   ///\author Attila Bernath
-  template <typename Graph, typename LengthMap, typename CapacityMap>
-  class MinCostFlows {
+  template <typename Graph, typename LengthMap, typename SupplyMap>
+  class MinCostFlow {
 
     typedef typename LengthMap::ValueType Length;
 
-    //Warning: this should be integer type
-    typedef typename CapacityMap::ValueType Capacity;
+
+    typedef typename SupplyMap::ValueType Supply;
     
     typedef typename Graph::Node Node;
     typedef typename Graph::NodeIt NodeIt;
@@ -82,7 +84,7 @@
     //Input
     const Graph& G;
     const LengthMap& length;
-    const CapacityMap& capacity;
+    const SupplyMap& supply;//supply or demand of nodes
 
 
     //auxiliary variables
@@ -91,12 +93,9 @@
     EdgeIntMap flow; 
     //To store the potentila (dual variables)
     typename Graph::template NodeMap<Length> potential;
+    //To store excess-deficit values
+    SupplyMap excess;
     
-    //Container to store found paths
-    //std::vector< std::vector<Edge> > paths;
-    //typedef DirPath<Graph> DPath;
-    //DPath paths;
-
 
     Length total_length;
 
@@ -104,8 +103,8 @@
   public :
 
 
-    MinCostFlows(Graph& _G, LengthMap& _length, CapacityMap& _cap) : G(_G), 
-      length(_length), capacity(_cap), flow(_G), potential(_G){ }
+    MinCostFlow(Graph& _G, LengthMap& _length, SupplyMap& _supply) : G(_G), 
+      length(_length), supply(_supply), flow(_G), potential(_G){ }
 
     
     ///Runs the algorithm.
@@ -115,7 +114,7 @@
     ///Otherwise it returns the number of found edge-disjoint paths from s to t.
     ///\todo May be it does make sense to be able to start with a nonzero 
     /// feasible primal-dual solution pair as well.
-    int run(Node s, Node t, int k) {
+    int run() {
 
       //Resetting variables from previous runs
       total_length = 0;
@@ -123,26 +122,31 @@
       FOR_EACH_LOC(typename Graph::EdgeIt, e, G){
 	flow.set(e,0);
       }
+
+      //Initial value for delta
+      Supply delta = 0;
       
       FOR_EACH_LOC(typename Graph::NodeIt, n, G){
-	//cout << potential[n]<<endl;
+	if (delta < abs(supply[e])){
+	  delta = abs(supply[e]);
+	}
+	excess.set(n,supply[e]);
+	//Initialize the copy of the Dijkstra potential to zero
 	potential.set(n,0);
       }
       
 
       
-      //We need a residual graph
-      ResGraphType res_graph(G, capacity, flow);
-
-      //Initialize the copy of the Dijkstra potential to zero
-      
-      //typename ResGraphType::template NodeMap<Length> potential(res_graph);
+      //We need a residual graph which is uncapacitated
+      ResGraphType res_graph(G, flow);
 
 
+      
       ModLengthMap mod_length(res_graph, length, potential);
 
       Dijkstra<ResGraphType, ModLengthMap> dijkstra(res_graph, mod_length);
 
+
       int i;
       for (i=0; i<k; ++i){
 	dijkstra.run(s);
@@ -151,14 +155,20 @@
 	  break;
 	};
 	
+	//We have to copy the potential
+	FOR_EACH_LOC(typename ResGraphType::NodeIt, n, res_graph){
+	  potential[n] += dijkstra.distMap()[n];
+	}
+
+	/*
 	{
-	  //We have to copy the potential
+
 	  typename ResGraphType::NodeIt n;
 	  for ( res_graph.first(n) ; res_graph.valid(n) ; res_graph.next(n) ) {
 	      potential[n] += dijkstra.distMap()[n];
 	  }
 	}
-
+	*/
 
 	//Augmenting on the sortest path
 	Node n=t;
@@ -166,7 +176,7 @@
 	while (n!=s){
 	  e = dijkstra.pred(n);
 	  n = dijkstra.predNode(n);
-	  res_graph.augment(e,1);
+	  res_graph.augment(e,delta);
 	  //Let's update the total length
 	  if (res_graph.forward(e))
 	    total_length += length[e];
@@ -225,27 +235,8 @@
       return true;
     }
     
-    /*
-      ///\todo To be implemented later
-
-    ///This function gives back the \c j-th path in argument p.
-    ///Assumes that \c run() has been run and nothing changed since then.
-    /// \warning It is assumed that \c p is constructed to be a path of graph \c G. If \c j is greater than the result of previous \c run, then the result here will be an empty path.
-    template<typename DirPath>
-    void getPath(DirPath& p, int j){
-      p.clear();
-      typename DirPath::Builder B(p);
-      for(typename std::vector<Edge>::iterator i=paths[j].begin(); 
-	  i!=paths[j].end(); ++i ){
-	B.pushBack(*i);
-      }
-
-      B.commit();
-    }
-
-    */
 
-  }; //class MinCostFlows
+  }; //class MinCostFlow
 
   ///@}
 



More information about the Lemon-commits mailing list