tapolcai@543: #include <iostream>
tapolcai@543: 
tapolcai@543: #include "test_tools.h"
tapolcai@543: #include <lemon/smart_graph.h>
kpeter@596: #include <lemon/concepts/graph.h>
kpeter@596: #include <lemon/concepts/maps.h>
tapolcai@543: #include <lemon/lgf_reader.h>
alpar@545: #include <lemon/gomory_hu.h>
tapolcai@543: #include <cstdlib>
tapolcai@543: 
tapolcai@543: using namespace std;
tapolcai@543: using namespace lemon;
tapolcai@543: 
tapolcai@543: typedef SmartGraph Graph;
tapolcai@543: 
tapolcai@543: char test_lgf[] =
tapolcai@543:   "@nodes\n"
tapolcai@543:   "label\n"
tapolcai@543:   "0\n"
tapolcai@543:   "1\n"
tapolcai@543:   "2\n"
tapolcai@543:   "3\n"
tapolcai@543:   "4\n"
tapolcai@543:   "@arcs\n"
tapolcai@543:   "     label capacity\n"
tapolcai@543:   "0 1  0     1\n"
tapolcai@543:   "1 2  1     1\n"
tapolcai@543:   "2 3  2     1\n"
tapolcai@543:   "0 3  4     5\n"
tapolcai@543:   "0 3  5     10\n"
tapolcai@543:   "0 3  6     7\n"
tapolcai@543:   "4 2  7     1\n"
tapolcai@543:   "@attributes\n"
tapolcai@543:   "source 0\n"
tapolcai@543:   "target 3\n";
tapolcai@543:   
kpeter@596: void checkGomoryHuCompile()
kpeter@596: {
kpeter@596:   typedef int Value;
kpeter@596:   typedef concepts::Graph Graph;
kpeter@596: 
kpeter@596:   typedef Graph::Node Node;
kpeter@596:   typedef Graph::Edge Edge;
kpeter@596:   typedef concepts::ReadMap<Edge, Value> CapMap;
kpeter@596:   typedef concepts::ReadWriteMap<Node, bool> CutMap;
kpeter@596: 
kpeter@596:   Graph g;
kpeter@596:   Node n;
kpeter@596:   CapMap cap;
kpeter@596:   CutMap cut;
kpeter@596:   Value v;
kpeter@596:   int d;
kpeter@596: 
kpeter@596:   GomoryHu<Graph, CapMap> gh_test(g, cap);
kpeter@596:   const GomoryHu<Graph, CapMap>&
kpeter@596:     const_gh_test = gh_test;
kpeter@596: 
kpeter@596:   gh_test.run();
kpeter@596: 
kpeter@596:   n = const_gh_test.predNode(n);
kpeter@596:   v = const_gh_test.predValue(n);
kpeter@596:   d = const_gh_test.rootDist(n);
kpeter@596:   v = const_gh_test.minCutValue(n, n);
kpeter@596:   v = const_gh_test.minCutMap(n, n, cut);
kpeter@596: }
kpeter@596: 
tapolcai@543: GRAPH_TYPEDEFS(Graph);
tapolcai@543: typedef Graph::EdgeMap<int> IntEdgeMap;
tapolcai@543: typedef Graph::NodeMap<bool> BoolNodeMap;
tapolcai@543: 
tapolcai@543: int cutValue(const Graph& graph, const BoolNodeMap& cut,
tapolcai@543: 	     const IntEdgeMap& capacity) {
tapolcai@543: 
tapolcai@543:   int sum = 0;
tapolcai@543:   for (EdgeIt e(graph); e != INVALID; ++e) {
tapolcai@543:     Node s = graph.u(e);
tapolcai@543:     Node t = graph.v(e);
tapolcai@543: 
tapolcai@543:     if (cut[s] != cut[t]) {
tapolcai@543:       sum += capacity[e];
tapolcai@543:     }
tapolcai@543:   }
tapolcai@543:   return sum;
tapolcai@543: }
tapolcai@543: 
tapolcai@543: 
tapolcai@543: int main() {
tapolcai@543:   Graph graph;
tapolcai@543:   IntEdgeMap capacity(graph);
tapolcai@543: 
tapolcai@543:   std::istringstream input(test_lgf);
tapolcai@543:   GraphReader<Graph>(graph, input).
tapolcai@543:     edgeMap("capacity", capacity).run();
tapolcai@543: 
alpar@545:   GomoryHu<Graph> ght(graph, capacity);
tapolcai@543:   ght.run();
tapolcai@543: 
tapolcai@543:   for (NodeIt u(graph); u != INVALID; ++u) {
tapolcai@543:     for (NodeIt v(graph); v != u; ++v) {
tapolcai@543:       Preflow<Graph, IntEdgeMap> pf(graph, capacity, u, v);
tapolcai@543:       pf.runMinCut();
tapolcai@543:       BoolNodeMap cm(graph);
tapolcai@543:       ght.minCutMap(u, v, cm);
tapolcai@543:       check(pf.flowValue() == ght.minCutValue(u, v), "Wrong cut 1");
kpeter@596:       check(cm[u] != cm[v], "Wrong cut 2");
kpeter@596:       check(pf.flowValue() == cutValue(graph, cm, capacity), "Wrong cut 3");
alpar@544: 
alpar@544:       int sum=0;
alpar@545:       for(GomoryHu<Graph>::MinCutEdgeIt a(ght, u, v);a!=INVALID;++a)
alpar@544:         sum+=capacity[a]; 
alpar@544:       check(sum == ght.minCutValue(u, v), "Problem with MinCutEdgeIt");
alpar@544: 
alpar@544:       sum=0;
alpar@545:       for(GomoryHu<Graph>::MinCutNodeIt n(ght, u, v,true);n!=INVALID;++n)
alpar@544:         sum++;
alpar@545:       for(GomoryHu<Graph>::MinCutNodeIt n(ght, u, v,false);n!=INVALID;++n)
alpar@544:         sum++;
alpar@544:       check(sum == countNodes(graph), "Problem with MinCutNodeIt");
tapolcai@543:     }
tapolcai@543:   }
tapolcai@543:   
tapolcai@543:   return 0;
tapolcai@543: }