test/gomory_hu_test.cc
changeset 594 d59dcc933e59
parent 592 e72bacfea6b7
child 643 293551ad254f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/gomory_hu_test.cc	Wed Mar 04 14:09:45 2009 +0000
     1.3 @@ -0,0 +1,92 @@
     1.4 +#include <iostream>
     1.5 +
     1.6 +#include "test_tools.h"
     1.7 +#include <lemon/smart_graph.h>
     1.8 +#include <lemon/lgf_reader.h>
     1.9 +#include <lemon/gomory_hu.h>
    1.10 +#include <cstdlib>
    1.11 +
    1.12 +using namespace std;
    1.13 +using namespace lemon;
    1.14 +
    1.15 +typedef SmartGraph Graph;
    1.16 +
    1.17 +char test_lgf[] =
    1.18 +  "@nodes\n"
    1.19 +  "label\n"
    1.20 +  "0\n"
    1.21 +  "1\n"
    1.22 +  "2\n"
    1.23 +  "3\n"
    1.24 +  "4\n"
    1.25 +  "@arcs\n"
    1.26 +  "     label capacity\n"
    1.27 +  "0 1  0     1\n"
    1.28 +  "1 2  1     1\n"
    1.29 +  "2 3  2     1\n"
    1.30 +  "0 3  4     5\n"
    1.31 +  "0 3  5     10\n"
    1.32 +  "0 3  6     7\n"
    1.33 +  "4 2  7     1\n"
    1.34 +  "@attributes\n"
    1.35 +  "source 0\n"
    1.36 +  "target 3\n";
    1.37 +  
    1.38 +GRAPH_TYPEDEFS(Graph);
    1.39 +typedef Graph::EdgeMap<int> IntEdgeMap;
    1.40 +typedef Graph::NodeMap<bool> BoolNodeMap;
    1.41 +
    1.42 +int cutValue(const Graph& graph, const BoolNodeMap& cut,
    1.43 +	     const IntEdgeMap& capacity) {
    1.44 +
    1.45 +  int sum = 0;
    1.46 +  for (EdgeIt e(graph); e != INVALID; ++e) {
    1.47 +    Node s = graph.u(e);
    1.48 +    Node t = graph.v(e);
    1.49 +
    1.50 +    if (cut[s] != cut[t]) {
    1.51 +      sum += capacity[e];
    1.52 +    }
    1.53 +  }
    1.54 +  return sum;
    1.55 +}
    1.56 +
    1.57 +
    1.58 +int main() {
    1.59 +  Graph graph;
    1.60 +  IntEdgeMap capacity(graph);
    1.61 +
    1.62 +  std::istringstream input(test_lgf);
    1.63 +  GraphReader<Graph>(graph, input).
    1.64 +    edgeMap("capacity", capacity).run();
    1.65 +
    1.66 +  GomoryHu<Graph> ght(graph, capacity);
    1.67 +  ght.run();
    1.68 +
    1.69 +  for (NodeIt u(graph); u != INVALID; ++u) {
    1.70 +    for (NodeIt v(graph); v != u; ++v) {
    1.71 +      Preflow<Graph, IntEdgeMap> pf(graph, capacity, u, v);
    1.72 +      pf.runMinCut();
    1.73 +      BoolNodeMap cm(graph);
    1.74 +      ght.minCutMap(u, v, cm);
    1.75 +      check(pf.flowValue() == ght.minCutValue(u, v), "Wrong cut 1");
    1.76 +      check(cm[u] != cm[v], "Wrong cut 3");
    1.77 +      check(pf.flowValue() == cutValue(graph, cm, capacity), "Wrong cut 2");
    1.78 +
    1.79 +      int sum=0;
    1.80 +      for(GomoryHu<Graph>::MinCutEdgeIt a(ght, u, v);a!=INVALID;++a)
    1.81 +        sum+=capacity[a]; 
    1.82 +      check(sum == ght.minCutValue(u, v), "Problem with MinCutEdgeIt");
    1.83 +
    1.84 +      sum=0;
    1.85 +      for(GomoryHu<Graph>::MinCutNodeIt n(ght, u, v,true);n!=INVALID;++n)
    1.86 +        sum++;
    1.87 +      for(GomoryHu<Graph>::MinCutNodeIt n(ght, u, v,false);n!=INVALID;++n)
    1.88 +        sum++;
    1.89 +      check(sum == countNodes(graph), "Problem with MinCutNodeIt");
    1.90 +      
    1.91 +    }
    1.92 +  }
    1.93 +  
    1.94 +  return 0;
    1.95 +}