src/test/preflow_test.cc
author marci
Mon, 15 Nov 2004 16:39:55 +0000
changeset 997 665ffade9aca
parent 959 c80ef5912903
child 1164 80bb73097736
permissions -rwxr-xr-x
RevGraphWrapper modified according to the factory
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/test/preflow_test.cc - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@906
    16
jacint@833
    17
#include <fstream>
klao@858
    18
#include <string>
klao@858
    19
jacint@833
    20
#include "test_tools.h"
alpar@921
    21
#include <lemon/smart_graph.h>
alpar@921
    22
#include <lemon/dimacs.h>
alpar@921
    23
#include <lemon/preflow.h>
klao@959
    24
#include <lemon/concept/graph.h>
klao@959
    25
#include <lemon/concept/maps.h>
alpar@855
    26
alpar@921
    27
using namespace lemon;
jacint@833
    28
jacint@833
    29
void check_Preflow() 
jacint@833
    30
{
jacint@833
    31
  typedef int VType;
klao@959
    32
  typedef concept::StaticGraph Graph;
jacint@833
    33
jacint@833
    34
  typedef Graph::Node Node;
jacint@833
    35
  typedef Graph::Edge Edge;
klao@959
    36
  typedef concept::ReadMap<Edge,VType> CapMap;
klao@959
    37
  typedef concept::ReadWriteMap<Edge,VType> FlowMap;
klao@959
    38
  typedef concept::ReadWriteMap<Node,bool> CutMap;
jacint@833
    39
 
jacint@833
    40
  typedef Preflow<Graph, int, CapMap, FlowMap> PType;
jacint@833
    41
marci@940
    42
  Graph g;
jacint@833
    43
  Node n;
jacint@833
    44
  CapMap cap;
jacint@833
    45
  FlowMap flow;
jacint@833
    46
  CutMap cut;
jacint@833
    47
marci@940
    48
  PType preflow_test(g,n,n,cap,flow);
jacint@833
    49
jacint@833
    50
  preflow_test.run();
jacint@833
    51
  preflow_test.flowValue();
jacint@833
    52
  preflow_test.setSource(n);
jacint@833
    53
  preflow_test.setFlow(flow);
jacint@833
    54
jacint@833
    55
  preflow_test.phase1(PType::NO_FLOW);
jacint@833
    56
  preflow_test.minCut(cut);
jacint@833
    57
jacint@833
    58
  preflow_test.phase2();
jacint@833
    59
  preflow_test.setTarget(n);
jacint@833
    60
  preflow_test.setCap(cap);
jacint@833
    61
  preflow_test.minMinCut(cut);
jacint@833
    62
  preflow_test.maxMinCut(cut);
jacint@833
    63
}
jacint@833
    64
marci@940
    65
int cut_value ( SmartGraph& g, SmartGraph::NodeMap<bool>& cut, 
jacint@833
    66
		SmartGraph::EdgeMap<int>& cap) {
jacint@833
    67
  
jacint@833
    68
  int c=0;
marci@940
    69
  for(SmartGraph::EdgeIt e(g); e!=INVALID; ++e) {
alpar@986
    70
    if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e];
jacint@833
    71
  }
jacint@833
    72
  return c;
jacint@833
    73
}
jacint@833
    74
jacint@833
    75
int main() {
jacint@833
    76
jacint@833
    77
  typedef SmartGraph Graph;
jacint@833
    78
  
alpar@842
    79
  typedef Graph::Node Node;
jacint@833
    80
  typedef Graph::NodeIt NodeIt;
jacint@833
    81
  typedef Graph::EdgeIt EdgeIt;
jacint@833
    82
  typedef Graph::EdgeMap<int> CapMap;
jacint@833
    83
  typedef Graph::EdgeMap<int> FlowMap;
jacint@833
    84
  typedef Graph::NodeMap<bool> CutMap;
jacint@833
    85
jacint@833
    86
  typedef Preflow<Graph, int> PType;
jacint@833
    87
klao@859
    88
  std::string f_name;
klao@858
    89
  if( getenv("srcdir") ) {
jacint@887
    90
    f_name = std::string(getenv("srcdir")) + "/preflow_graph.dim";
klao@858
    91
  }
klao@858
    92
  else {
jacint@887
    93
    f_name = "preflow_graph.dim";
klao@858
    94
  }
alpar@855
    95
  
klao@858
    96
  std::ifstream file(f_name.c_str());
alpar@855
    97
  
klao@858
    98
  check(file, "Input file '" << f_name << "' not found.");
jacint@833
    99
  
marci@940
   100
  Graph g;
alpar@842
   101
  Node s, t;
marci@940
   102
  CapMap cap(g);
marci@940
   103
  readDimacs(file, g, cap, s, t);
jacint@833
   104
marci@940
   105
  FlowMap flow(g,0);
jacint@887
   106
jacint@833
   107
 
jacint@887
   108
marci@940
   109
  PType preflow_test(g, s, t, cap, flow);
jacint@833
   110
  preflow_test.run(PType::ZERO_FLOW);
jacint@887
   111
    
marci@940
   112
  CutMap min_cut(g,false);
marci@940
   113
  preflow_test.minCut(min_cut); 
marci@940
   114
  int min_cut_value=cut_value(g,min_cut,cap);
jacint@833
   115
   
marci@940
   116
  CutMap min_min_cut(g,false);
marci@940
   117
  preflow_test.minMinCut(min_min_cut); 
marci@940
   118
  int min_min_cut_value=cut_value(g,min_min_cut,cap);
jacint@833
   119
   
marci@940
   120
  CutMap max_min_cut(g,false);
marci@940
   121
  preflow_test.maxMinCut(max_min_cut); 
marci@940
   122
  int max_min_cut_value=cut_value(g,max_min_cut,cap);
jacint@833
   123
jacint@833
   124
  check(preflow_test.flowValue() == min_cut_value &&
jacint@833
   125
	min_cut_value == min_min_cut_value &&
jacint@833
   126
	min_min_cut_value == max_min_cut_value,
jacint@833
   127
	"The max flow value is not equal to the three min cut values.");
jacint@833
   128
jacint@833
   129
  int flow_value=preflow_test.flowValue();
jacint@833
   130
jacint@833
   131
jacint@887
   132
marci@940
   133
  for(EdgeIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e]; 
jacint@833
   134
  preflow_test.setCap(cap);  
alpar@842
   135
jacint@833
   136
  preflow_test.phase1(PType::PRE_FLOW);
jacint@833
   137
marci@940
   138
  CutMap min_cut1(g,false);
marci@940
   139
  preflow_test.minCut(min_cut1); 
marci@940
   140
  min_cut_value=cut_value(g,min_cut1,cap);
jacint@833
   141
   
jacint@833
   142
  check(preflow_test.flowValue() == min_cut_value &&
jacint@833
   143
	min_cut_value == 2*flow_value,
jacint@833
   144
	"The max flow value or the min cut value is wrong.");
jacint@833
   145
jacint@833
   146
  preflow_test.phase2();
jacint@833
   147
marci@940
   148
  CutMap min_cut2(g,false);
marci@940
   149
  preflow_test.minCut(min_cut2); 
marci@940
   150
  min_cut_value=cut_value(g,min_cut2,cap);
jacint@833
   151
   
marci@940
   152
  CutMap min_min_cut2(g,false);
marci@940
   153
  preflow_test.minMinCut(min_min_cut2); 
marci@940
   154
  min_min_cut_value=cut_value(g,min_min_cut2,cap);
jacint@833
   155
 
marci@940
   156
  preflow_test.maxMinCut(max_min_cut); 
marci@940
   157
  max_min_cut_value=cut_value(g,max_min_cut,cap);
jacint@833
   158
jacint@833
   159
  check(preflow_test.flowValue() == min_cut_value &&
jacint@833
   160
	min_cut_value == min_min_cut_value &&
jacint@833
   161
	min_min_cut_value == max_min_cut_value &&
jacint@833
   162
	min_cut_value == 2*flow_value,
jacint@833
   163
	"The max flow value or the three min cut values were not doubled");
jacint@833
   164
jacint@887
   165
jacint@887
   166
marci@940
   167
  EdgeIt e(g);
jacint@887
   168
  for( int i=1; i==10; ++i ) {
jacint@887
   169
    flow.set(e,0);
jacint@833
   170
    ++e;
jacint@833
   171
  }
jacint@833
   172
jacint@833
   173
  preflow_test.setFlow(flow); 
jacint@887
   174
marci@940
   175
  NodeIt tmp1(g,s);
jacint@887
   176
  ++tmp1;
jacint@887
   177
  if ( tmp1 != INVALID ) s=tmp1;
jacint@887
   178
marci@940
   179
  NodeIt tmp2(g,t);
jacint@887
   180
  ++tmp2;
jacint@887
   181
  if ( tmp2 != INVALID ) t=tmp2;
jacint@887
   182
jacint@833
   183
  preflow_test.setSource(s);
jacint@887
   184
  preflow_test.setTarget(t); 
jacint@887
   185
  
jacint@833
   186
  preflow_test.run();
jacint@833
   187
marci@940
   188
  CutMap min_cut3(g,false);
marci@940
   189
  preflow_test.minCut(min_cut3); 
marci@940
   190
  min_cut_value=cut_value(g,min_cut3,cap);
jacint@833
   191
   
marci@940
   192
  CutMap min_min_cut3(g,false);
marci@940
   193
  preflow_test.minMinCut(min_min_cut3); 
marci@940
   194
  min_min_cut_value=cut_value(g,min_min_cut3,cap);
jacint@833
   195
   
marci@940
   196
  preflow_test.maxMinCut(max_min_cut); 
marci@940
   197
  max_min_cut_value=cut_value(g,max_min_cut,cap);
jacint@833
   198
jacint@833
   199
  check(preflow_test.flowValue() == min_cut_value &&
jacint@833
   200
	min_cut_value == min_min_cut_value &&
jacint@833
   201
	min_min_cut_value == max_min_cut_value,
jacint@833
   202
	"The max flow value or the three min cut values are incorrect.");
jacint@833
   203
}