[Lemon-commits] [lemon_svn] athos: r696 - hugo/trunk/src/work/athos
Lemon SVN
svn at lemon.cs.elte.hu
Mon Nov 6 20:41:00 CET 2006
Author: athos
Date: Tue May 4 18:52:15 2004
New Revision: 696
Modified:
hugo/trunk/src/work/athos/mincostflows.h
hugo/trunk/src/work/athos/mincostflows_test.cc
Log:
Minimum cost flows of small values: algorithm from Andras Frank's lecture notes (approximately)
Modified: hugo/trunk/src/work/athos/mincostflows.h
==============================================================================
--- hugo/trunk/src/work/athos/mincostflows.h (original)
+++ hugo/trunk/src/work/athos/mincostflows.h Tue May 4 18:52:15 2004
@@ -11,7 +11,7 @@
#include <graph_wrapper.h>
#include <maps.h>
#include <vector.h>
-
+#include <for_each_macros.h>
namespace hugo {
@@ -34,12 +34,13 @@
/// where \c M is the value of the maximal flow.
///
///\author Attila Bernath
- template <typename Graph, typename LengthMap>
+ template <typename Graph, typename LengthMap, typename CapacityMap>
class MinCostFlows {
typedef typename LengthMap::ValueType Length;
- typedef typename LengthMap::ValueType Length;
+ //Warning: this should be integer type
+ typedef typename CapacityMap::ValueType Capacity;
typedef typename Graph::Node Node;
typedef typename Graph::NodeIt NodeIt;
@@ -49,8 +50,8 @@
// typedef ConstMap<Edge,int> ConstMap;
- typedef ResGraphWrapper<const Graph,int,EdgeIntMap,EdgeIntMap> ResGraphType;
-
+ typedef ResGraphWrapper<const Graph,int,CapacityMap,EdgeIntMap> ResGraphType;
+ typedef typename ResGraphType::Edge ResGraphEdge;
class ModLengthMap {
typedef typename ResGraphType::template NodeMap<Length> NodeMap;
const ResGraphType& G;
@@ -68,7 +69,7 @@
return -ol[e]-(pot[G.head(e)]-pot[G.tail(e)]);
}
- ModLengthMap(const ResGraphType& _G, const EdgeIntMap& _rev,
+ ModLengthMap(const ResGraphType& _G,
const LengthMap &o, const NodeMap &p) :
G(_G), /*rev(_rev),*/ ol(o), pot(p){};
};//ModLengthMap
@@ -78,7 +79,7 @@
//Input
const Graph& G;
const LengthMap& length;
- const EdgeIntMap& capacity;
+ const CapacityMap& capacity;
//auxiliary variables
@@ -98,7 +99,7 @@
public :
- MinLengthPaths(Graph& _G, LengthMap& _length, EdgeIntMap& _cap) : G(_G),
+ MinCostFlows(Graph& _G, LengthMap& _length, CapacityMap& _cap) : G(_G),
length(_length), capacity(_cap), flow(_G)/*, dijkstra_dist(_G)*/{ }
@@ -109,7 +110,13 @@
///Otherwise it returns the number of found edge-disjoint paths from s to t.
int run(Node s, Node t, int k) {
+ //Resetting variables from previous runs
+ total_length = 0;
+ FOR_EACH_LOC(typename Graph::EdgeIt, e, G){
+ flow.set(e,0);
+ }
+
//We need a residual graph
ResGraphType res_graph(G, capacity, flow);
@@ -138,59 +145,36 @@
//Augmenting on the sortest path
Node n=t;
- Edge e;
+ ResGraphEdge e;
while (n!=s){
e = dijkstra.pred(n);
n = dijkstra.predNode(n);
- G.augment(e,1);
+ res_graph.augment(e,1);
+ //Let's update the total length
+ if (res_graph.forward(e))
+ total_length += length[e];
+ else
+ total_length -= length[e];
}
}
- /*
- ///\TODO To be implemented later
-
- //Let's find the paths
- //We put the paths into stl vectors (as an inner representation).
- //In the meantime we lose the information stored in 'reversed'.
- //We suppose the lengths to be positive now.
-
- //Meanwhile we put the total length of the found paths
- //in the member variable total_length
- paths.clear();
- total_length=0;
- paths.resize(k);
- for (int j=0; j<i; ++j){
- Node n=s;
- OutEdgeIt e;
-
- while (n!=t){
-
-
- G.first(e,n);
-
- while (!reversed[e]){
- G.next(e);
- }
- n = G.head(e);
- paths[j].push_back(e);
- total_length += length[e];
- reversed[e] = 1-reversed[e];
- }
-
- }
- */
return i;
}
+
+
///This function gives back the total length of the found paths.
///Assumes that \c run() has been run and nothing changed since then.
Length totalLength(){
return total_length;
}
+ /*
+ ///\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.
@@ -206,7 +190,9 @@
B.commit();
}
- }; //class MinLengthPaths
+ */
+
+ }; //class MinCostFlows
///@}
Modified: hugo/trunk/src/work/athos/mincostflows_test.cc
==============================================================================
--- hugo/trunk/src/work/athos/mincostflows_test.cc (original)
+++ hugo/trunk/src/work/athos/mincostflows_test.cc Tue May 4 18:52:15 2004
@@ -61,16 +61,21 @@
length.set(v4_t, 8);
length.set(v5_t, 8);
- ConstMap const1map(1);
- std::cout << "Minlengthpaths algorithm test..." << std::endl;
+ ConstMap<Edge, int> const1map(1);
+ std::cout << "Mincostflows algorithm test..." << std::endl;
int k=3;
- MinCostFlows< ListGraph, ListGraph::EdgeMap<int> >
+ MinCostFlows< ListGraph, ListGraph::EdgeMap<int>, ConstMap<Edge, int> >
surb_test(graph, length, const1map);
check( surb_test.run(s,t,k) == 2 && surb_test.totalLength() == 46,"Two paths, total length should be 46");
+ k=1;
+ check( surb_test.run(s,t,k) == 1 && surb_test.totalLength() == 19,"One path, total length should be 19");
+
+ //cout << surb_test.run(s,t,k) << surb_test.totalLength()<<endl;
+ /*
typedef DirPath<ListGraph> DPath;
DPath P(graph);
@@ -85,7 +90,7 @@
surb_test.getPath(P,0);
check(P.length() == 4, "First path should contain 4 edges.");
-
+ */
cout << (passed ? "All tests passed." : "Some of the tests failed!!!")
<< endl;
More information about the Lemon-commits
mailing list