test/preflow_test.cc
author alpar
Tue, 17 Oct 2006 10:32:12 +0000
changeset 2244 a28b4e0aa787
parent 2108 f2c532541730
child 2260 4274224f8a7d
permissions -rwxr-xr-x
Benchmark the running time of lemon::Random
alpar@906
     1
/* -*- C++ -*-
alpar@906
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     8
 *
alpar@906
     9
 * Permission to use, modify and distribute this software is granted
alpar@906
    10
 * provided that this copyright notice appears in all copies. For
alpar@906
    11
 * precise terms see the accompanying LICENSE file.
alpar@906
    12
 *
alpar@906
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    14
 * express or implied, and with no claim as to its suitability for any
alpar@906
    15
 * purpose.
alpar@906
    16
 *
alpar@906
    17
 */
alpar@906
    18
jacint@833
    19
#include <fstream>
klao@858
    20
#include <string>
klao@858
    21
jacint@833
    22
#include "test_tools.h"
alpar@921
    23
#include <lemon/smart_graph.h>
alpar@921
    24
#include <lemon/dimacs.h>
alpar@921
    25
#include <lemon/preflow.h>
klao@959
    26
#include <lemon/concept/graph.h>
klao@959
    27
#include <lemon/concept/maps.h>
alpar@855
    28
alpar@921
    29
using namespace lemon;
jacint@833
    30
jacint@833
    31
void check_Preflow() 
jacint@833
    32
{
jacint@833
    33
  typedef int VType;
deba@2111
    34
  typedef concept::Graph Graph;
jacint@833
    35
jacint@833
    36
  typedef Graph::Node Node;
jacint@833
    37
  typedef Graph::Edge Edge;
klao@959
    38
  typedef concept::ReadMap<Edge,VType> CapMap;
klao@959
    39
  typedef concept::ReadWriteMap<Edge,VType> FlowMap;
klao@959
    40
  typedef concept::ReadWriteMap<Node,bool> CutMap;
jacint@833
    41
 
jacint@833
    42
  typedef Preflow<Graph, int, CapMap, FlowMap> PType;
jacint@833
    43
marci@940
    44
  Graph g;
jacint@833
    45
  Node n;
jacint@833
    46
  CapMap cap;
jacint@833
    47
  FlowMap flow;
jacint@833
    48
  CutMap cut;
jacint@833
    49
marci@940
    50
  PType preflow_test(g,n,n,cap,flow);
jacint@833
    51
jacint@833
    52
  preflow_test.run();
jacint@833
    53
  preflow_test.flowValue();
alpar@1222
    54
  preflow_test.source(n);
alpar@1222
    55
  preflow_test.flowMap(flow);
jacint@833
    56
jacint@833
    57
  preflow_test.phase1(PType::NO_FLOW);
jacint@833
    58
  preflow_test.minCut(cut);
jacint@833
    59
jacint@833
    60
  preflow_test.phase2();
alpar@1222
    61
  preflow_test.target(n);
alpar@1222
    62
  preflow_test.capacityMap(cap);
jacint@833
    63
  preflow_test.minMinCut(cut);
jacint@833
    64
  preflow_test.maxMinCut(cut);
jacint@833
    65
}
jacint@833
    66
marci@940
    67
int cut_value ( SmartGraph& g, SmartGraph::NodeMap<bool>& cut, 
jacint@833
    68
		SmartGraph::EdgeMap<int>& cap) {
jacint@833
    69
  
jacint@833
    70
  int c=0;
marci@940
    71
  for(SmartGraph::EdgeIt e(g); e!=INVALID; ++e) {
alpar@986
    72
    if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e];
jacint@833
    73
  }
jacint@833
    74
  return c;
jacint@833
    75
}
jacint@833
    76
jacint@833
    77
int main() {
jacint@833
    78
jacint@833
    79
  typedef SmartGraph Graph;
jacint@833
    80
  
alpar@842
    81
  typedef Graph::Node Node;
jacint@833
    82
  typedef Graph::NodeIt NodeIt;
jacint@833
    83
  typedef Graph::EdgeIt EdgeIt;
jacint@833
    84
  typedef Graph::EdgeMap<int> CapMap;
jacint@833
    85
  typedef Graph::EdgeMap<int> FlowMap;
jacint@833
    86
  typedef Graph::NodeMap<bool> CutMap;
jacint@833
    87
jacint@833
    88
  typedef Preflow<Graph, int> PType;
jacint@833
    89
klao@859
    90
  std::string f_name;
alpar@1215
    91
  if( getenv("srcdir") )
alpar@1215
    92
    f_name = std::string(getenv("srcdir"));
alpar@1215
    93
  else f_name = ".";
ladanyi@2108
    94
  f_name += "/test/preflow_graph.dim";
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]; 
alpar@1222
   134
  preflow_test.capacityMap(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
alpar@1222
   173
  preflow_test.flowMap(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
alpar@1222
   183
  preflow_test.source(s);
alpar@1222
   184
  preflow_test.target(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
}