src/test/preflow_test.cc
author deba
Sat, 14 May 2005 17:34:31 +0000
changeset 1419 c3244a26adb1
parent 1222 a3fb216a267d
permissions -rwxr-xr-x
Added backward and forward map.
Converting UndirEdge -> Edge
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/test/preflow_test.cc - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, 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();
alpar@1222
    52
  preflow_test.source(n);
alpar@1222
    53
  preflow_test.flowMap(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();
alpar@1222
    59
  preflow_test.target(n);
alpar@1222
    60
  preflow_test.capacityMap(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;
alpar@1215
    89
  if( getenv("srcdir") )
alpar@1215
    90
    f_name = std::string(getenv("srcdir"));
alpar@1215
    91
  else f_name = ".";
alpar@1215
    92
  f_name += "/preflow_graph.dim";
alpar@855
    93
  
klao@858
    94
  std::ifstream file(f_name.c_str());
alpar@855
    95
  
klao@858
    96
  check(file, "Input file '" << f_name << "' not found.");
jacint@833
    97
  
marci@940
    98
  Graph g;
alpar@842
    99
  Node s, t;
marci@940
   100
  CapMap cap(g);
marci@940
   101
  readDimacs(file, g, cap, s, t);
jacint@833
   102
marci@940
   103
  FlowMap flow(g,0);
jacint@887
   104
jacint@833
   105
 
jacint@887
   106
marci@940
   107
  PType preflow_test(g, s, t, cap, flow);
jacint@833
   108
  preflow_test.run(PType::ZERO_FLOW);
jacint@887
   109
    
marci@940
   110
  CutMap min_cut(g,false);
marci@940
   111
  preflow_test.minCut(min_cut); 
marci@940
   112
  int min_cut_value=cut_value(g,min_cut,cap);
jacint@833
   113
   
marci@940
   114
  CutMap min_min_cut(g,false);
marci@940
   115
  preflow_test.minMinCut(min_min_cut); 
marci@940
   116
  int min_min_cut_value=cut_value(g,min_min_cut,cap);
jacint@833
   117
   
marci@940
   118
  CutMap max_min_cut(g,false);
marci@940
   119
  preflow_test.maxMinCut(max_min_cut); 
marci@940
   120
  int max_min_cut_value=cut_value(g,max_min_cut,cap);
jacint@833
   121
jacint@833
   122
  check(preflow_test.flowValue() == min_cut_value &&
jacint@833
   123
	min_cut_value == min_min_cut_value &&
jacint@833
   124
	min_min_cut_value == max_min_cut_value,
jacint@833
   125
	"The max flow value is not equal to the three min cut values.");
jacint@833
   126
jacint@833
   127
  int flow_value=preflow_test.flowValue();
jacint@833
   128
jacint@833
   129
jacint@887
   130
marci@940
   131
  for(EdgeIt e(g); e!=INVALID; ++e) cap[e]=2*cap[e]; 
alpar@1222
   132
  preflow_test.capacityMap(cap);  
alpar@842
   133
jacint@833
   134
  preflow_test.phase1(PType::PRE_FLOW);
jacint@833
   135
marci@940
   136
  CutMap min_cut1(g,false);
marci@940
   137
  preflow_test.minCut(min_cut1); 
marci@940
   138
  min_cut_value=cut_value(g,min_cut1,cap);
jacint@833
   139
   
jacint@833
   140
  check(preflow_test.flowValue() == min_cut_value &&
jacint@833
   141
	min_cut_value == 2*flow_value,
jacint@833
   142
	"The max flow value or the min cut value is wrong.");
jacint@833
   143
jacint@833
   144
  preflow_test.phase2();
jacint@833
   145
marci@940
   146
  CutMap min_cut2(g,false);
marci@940
   147
  preflow_test.minCut(min_cut2); 
marci@940
   148
  min_cut_value=cut_value(g,min_cut2,cap);
jacint@833
   149
   
marci@940
   150
  CutMap min_min_cut2(g,false);
marci@940
   151
  preflow_test.minMinCut(min_min_cut2); 
marci@940
   152
  min_min_cut_value=cut_value(g,min_min_cut2,cap);
jacint@833
   153
 
marci@940
   154
  preflow_test.maxMinCut(max_min_cut); 
marci@940
   155
  max_min_cut_value=cut_value(g,max_min_cut,cap);
jacint@833
   156
jacint@833
   157
  check(preflow_test.flowValue() == min_cut_value &&
jacint@833
   158
	min_cut_value == min_min_cut_value &&
jacint@833
   159
	min_min_cut_value == max_min_cut_value &&
jacint@833
   160
	min_cut_value == 2*flow_value,
jacint@833
   161
	"The max flow value or the three min cut values were not doubled");
jacint@833
   162
jacint@887
   163
jacint@887
   164
marci@940
   165
  EdgeIt e(g);
jacint@887
   166
  for( int i=1; i==10; ++i ) {
jacint@887
   167
    flow.set(e,0);
jacint@833
   168
    ++e;
jacint@833
   169
  }
jacint@833
   170
alpar@1222
   171
  preflow_test.flowMap(flow); 
jacint@887
   172
marci@940
   173
  NodeIt tmp1(g,s);
jacint@887
   174
  ++tmp1;
jacint@887
   175
  if ( tmp1 != INVALID ) s=tmp1;
jacint@887
   176
marci@940
   177
  NodeIt tmp2(g,t);
jacint@887
   178
  ++tmp2;
jacint@887
   179
  if ( tmp2 != INVALID ) t=tmp2;
jacint@887
   180
alpar@1222
   181
  preflow_test.source(s);
alpar@1222
   182
  preflow_test.target(t); 
jacint@887
   183
  
jacint@833
   184
  preflow_test.run();
jacint@833
   185
marci@940
   186
  CutMap min_cut3(g,false);
marci@940
   187
  preflow_test.minCut(min_cut3); 
marci@940
   188
  min_cut_value=cut_value(g,min_cut3,cap);
jacint@833
   189
   
marci@940
   190
  CutMap min_min_cut3(g,false);
marci@940
   191
  preflow_test.minMinCut(min_min_cut3); 
marci@940
   192
  min_min_cut_value=cut_value(g,min_min_cut3,cap);
jacint@833
   193
   
marci@940
   194
  preflow_test.maxMinCut(max_min_cut); 
marci@940
   195
  max_min_cut_value=cut_value(g,max_min_cut,cap);
jacint@833
   196
jacint@833
   197
  check(preflow_test.flowValue() == min_cut_value &&
jacint@833
   198
	min_cut_value == min_min_cut_value &&
jacint@833
   199
	min_min_cut_value == max_min_cut_value,
jacint@833
   200
	"The max flow value or the three min cut values are incorrect.");
jacint@833
   201
}