src/work/flow_test.cc
author marci
Mon, 16 Feb 2004 11:38:19 +0000
changeset 76 d9650659a6ee
child 107 8d62f0072ff0
permissions -rw-r--r--
.
jacint@33
     1
#include <iostream>
jacint@33
     2
#include <vector>
jacint@33
     3
#include <string>
jacint@33
     4
jacint@33
     5
#include <marci_list_graph.hh>
jacint@33
     6
#include <marci_graph_traits.hh>
jacint@33
     7
#include <marci_property_vector.hh>
jacint@33
     8
#include <preflow_push_hl.hh>
jacint@33
     9
#include <preflow_push_max_flow.hh>
jacint@33
    10
#include <reverse_bfs.hh>
jacint@33
    11
#include <dijkstra.hh>
jacint@33
    12
jacint@33
    13
using namespace marci;
jacint@33
    14
jacint@33
    15
jacint@33
    16
int main (int, char*[])
jacint@33
    17
{
jacint@33
    18
  typedef graph_traits<list_graph>::node_iterator node_iterator;
jacint@33
    19
  typedef graph_traits<list_graph>::edge_iterator edge_iterator;
jacint@33
    20
  typedef graph_traits<list_graph>::each_node_iterator each_node_iterator;
jacint@33
    21
  typedef graph_traits<list_graph>::each_edge_iterator each_edge_iterator;
jacint@33
    22
  typedef graph_traits<list_graph>::out_edge_iterator out_edge_iterator;
jacint@33
    23
  typedef graph_traits<list_graph>::in_edge_iterator in_edge_iterator;
jacint@33
    24
  typedef graph_traits<list_graph>::sym_edge_iterator sym_edge_iterator;
jacint@33
    25
jacint@33
    26
  list_graph flow_test;
jacint@33
    27
 
jacint@33
    28
    //Ahuja könyv példája, maxflowvalue=13
jacint@33
    29
  node_iterator s=flow_test.add_node();
jacint@33
    30
  node_iterator v1=flow_test.add_node();
jacint@33
    31
  node_iterator v2=flow_test.add_node();
jacint@33
    32
  node_iterator v3=flow_test.add_node();
jacint@33
    33
  node_iterator v4=flow_test.add_node();
jacint@33
    34
  node_iterator v5=flow_test.add_node();
jacint@33
    35
  node_iterator t=flow_test.add_node();
jacint@33
    36
  
jacint@33
    37
  node_property_vector<list_graph, std::string> node_name(flow_test);
jacint@33
    38
  node_name.put(s, "s");
jacint@33
    39
  node_name.put(v1, "v1");
jacint@33
    40
  node_name.put(v2, "v2");
jacint@33
    41
  node_name.put(v3, "v3");
jacint@33
    42
  node_name.put(v4, "v4");
jacint@33
    43
  node_name.put(v5, "v5");
jacint@33
    44
  node_name.put(t, "t");
jacint@33
    45
jacint@33
    46
  edge_iterator s_v1=flow_test.add_edge(s, v1);
jacint@33
    47
  edge_iterator s_v2=flow_test.add_edge(s, v2);
jacint@33
    48
  edge_iterator s_v3=flow_test.add_edge(s, v3);
jacint@33
    49
  edge_iterator v2_v4=flow_test.add_edge(v2, v4);
jacint@33
    50
  edge_iterator v2_v5=flow_test.add_edge(v2, v5);
jacint@33
    51
  edge_iterator v3_v5=flow_test.add_edge(v3, v5);
jacint@33
    52
  edge_iterator v4_t=flow_test.add_edge(v4, t);
jacint@33
    53
  edge_iterator v5_t=flow_test.add_edge(v5, t);
jacint@33
    54
  edge_iterator v2_s=flow_test.add_edge(v2, s);
jacint@33
    55
  
jacint@33
    56
  edge_property_vector<list_graph, int> cap(flow_test);  
jacint@33
    57
  cap.put(s_v1, 0);
jacint@33
    58
  cap.put(s_v2, 10);
jacint@33
    59
  cap.put(s_v3, 10);
jacint@33
    60
  cap.put(v2_v4, 5);
jacint@33
    61
  cap.put(v2_v5, 8);
jacint@33
    62
  cap.put(v3_v5, 5);
jacint@33
    63
  cap.put(v4_t, 8);
jacint@33
    64
  cap.put(v5_t, 8);
jacint@33
    65
  cap.put(v2_s, 0);
jacint@33
    66
jacint@33
    67
jacint@33
    68
  
jacint@33
    69
  //Marci példája, maxflowvalue=23
jacint@33
    70
  /*  node_iterator s=flow_test.add_node();
jacint@33
    71
  node_iterator v1=flow_test.add_node();
jacint@33
    72
  node_iterator v2=flow_test.add_node();
jacint@33
    73
  node_iterator v3=flow_test.add_node();
jacint@33
    74
  node_iterator v4=flow_test.add_node();
jacint@33
    75
  node_iterator t=flow_test.add_node();
jacint@33
    76
  node_iterator w=flow_test.add_node();
jacint@33
    77
jacint@33
    78
  
jacint@33
    79
  node_property_vector<list_graph, std::string> node_name(flow_test);
jacint@33
    80
  node_name.put(s, "s");
jacint@33
    81
  node_name.put(v1, "v1");
jacint@33
    82
  node_name.put(v2, "v2");
jacint@33
    83
  node_name.put(v3, "v3");
jacint@33
    84
  node_name.put(v4, "v4");
jacint@33
    85
  node_name.put(t, "t");
jacint@33
    86
  node_name.put(w, "w");
jacint@33
    87
jacint@33
    88
  edge_iterator s_v1=flow_test.add_edge(s, v1);
jacint@33
    89
  edge_iterator s_v2=flow_test.add_edge(s, v2);
jacint@33
    90
  edge_iterator v1_v2=flow_test.add_edge(v1, v2);
jacint@33
    91
  edge_iterator v2_v1=flow_test.add_edge(v2, v1);
jacint@33
    92
  edge_iterator v1_v3=flow_test.add_edge(v1, v3);
jacint@33
    93
  edge_iterator v3_v2=flow_test.add_edge(v3, v2);
jacint@33
    94
  edge_iterator v2_v4=flow_test.add_edge(v2, v4);
jacint@33
    95
  edge_iterator v4_v3=flow_test.add_edge(v4, v3);
jacint@33
    96
  edge_iterator v3_t=flow_test.add_edge(v3, t);
jacint@33
    97
  edge_iterator v4_t=flow_test.add_edge(v4, t);
jacint@33
    98
  edge_iterator v3_v3=flow_test.add_edge(v3, v3);
jacint@33
    99
  edge_iterator s_w=flow_test.add_edge(s, w);
jacint@33
   100
  //  edge_iterator v2_s=flow_test.add_edge(v2, s);
jacint@33
   101
  
jacint@33
   102
jacint@33
   103
jacint@33
   104
  edge_property_vector<list_graph, int> cap(flow_test);  //serves as length in dijkstra
jacint@33
   105
  cap.put(s_v1, 16);
jacint@33
   106
  cap.put(s_v2, 13);
jacint@33
   107
  cap.put(v1_v2, 10);
jacint@33
   108
  cap.put(v2_v1, 4);
jacint@33
   109
  cap.put(v1_v3, 12);
jacint@33
   110
  cap.put(v3_v2, 9);
jacint@33
   111
  cap.put(v2_v4, 14);
jacint@33
   112
  cap.put(v4_v3, 7);
jacint@33
   113
  cap.put(v3_t, 20);
jacint@33
   114
  cap.put(v4_t, 4);
jacint@33
   115
  cap.put(v3_v3, 4);
jacint@33
   116
  cap.put(s_w, 4);
jacint@33
   117
  //  cap.put(v2_s, 0);
jacint@33
   118
jacint@33
   119
*/
jacint@33
   120
jacint@33
   121
  //pelda 3, maxflowvalue=4
jacint@33
   122
  /*      node_iterator s=flow_test.add_node();
jacint@33
   123
  node_iterator v1=flow_test.add_node();
jacint@33
   124
  node_iterator v2=flow_test.add_node();
jacint@33
   125
  node_iterator t=flow_test.add_node();
jacint@33
   126
  node_iterator w=flow_test.add_node();
jacint@33
   127
  
jacint@33
   128
  node_property_vector<list_graph, std::string> node_name(flow_test);
jacint@33
   129
  node_name.put(s, "s");
jacint@33
   130
  node_name.put(v1, "v1");
jacint@33
   131
  node_name.put(v2, "v2");
jacint@33
   132
  node_name.put(t, "t");
jacint@33
   133
  node_name.put(w, "w");
jacint@33
   134
jacint@33
   135
  edge_iterator s_v1=flow_test.add_edge(s, v1);
jacint@33
   136
  edge_iterator v1_v2=flow_test.add_edge(v1, v2);
jacint@33
   137
  edge_iterator v2_t=flow_test.add_edge(v2, t);
jacint@33
   138
  edge_iterator v1_v1=flow_test.add_edge(v1, v1);
jacint@33
   139
  edge_iterator s_w=flow_test.add_edge(s, w);
jacint@33
   140
jacint@33
   141
jacint@33
   142
  edge_property_vector<list_graph, int> cap(flow_test); 
jacint@33
   143
    
jacint@33
   144
  cap.put(s_v1, 16);
jacint@33
   145
  cap.put(v1_v2, 10);
jacint@33
   146
  cap.put(v2_t, 4);
jacint@33
   147
  cap.put(v1_v1, 3);
jacint@33
   148
  cap.put(s_w, 5);
jacint@33
   149
  */
jacint@33
   150
  
jacint@33
   151
jacint@33
   152
jacint@33
   153
jacint@33
   154
  std::cout << "Testing reverse_bfs..." << std::endl;
jacint@33
   155
  
jacint@33
   156
  reverse_bfs<list_graph> bfs_test(flow_test, t);
jacint@33
   157
jacint@33
   158
  bfs_test.run();
jacint@33
   159
jacint@33
   160
  for (each_node_iterator w=flow_test.first_node(); w.valid(); ++w) {
jacint@33
   161
    std::cout <<"The distance of " << w << " is " << bfs_test.dist(w) <<std::endl;
jacint@33
   162
    }
jacint@33
   163
jacint@33
   164
jacint@33
   165
jacint@33
   166
jacint@33
   167
jacint@33
   168
  std::cout << "Testing preflow_push_hl..." << std::endl;
jacint@33
   169
  
jacint@33
   170
  preflow_push_hl<list_graph, int> preflow_push_test(flow_test, s, t, cap);
jacint@33
   171
jacint@33
   172
  preflow_push_test.run();
jacint@33
   173
jacint@33
   174
  std::cout << "Maximum flow value is: " << preflow_push_test.maxflow() << "."<<std::endl;
jacint@33
   175
jacint@33
   176
  std::cout<< "The flow on edge s-v1 is "<< preflow_push_test.flowonedge(s_v1) << "."<<std::endl;
jacint@33
   177
jacint@33
   178
  edge_property_vector<list_graph, int> flow=preflow_push_test.allflow();  
jacint@33
   179
  for (each_edge_iterator e=flow_test.first_edge(); e.valid(); ++e) {
jacint@33
   180
    std::cout <<"Flow on edge " << flow_test.tail(e) <<"-" << flow_test.head(e)<< " is " <<flow.get(e) <<std::endl;
jacint@33
   181
    }
jacint@33
   182
jacint@33
   183
  std::cout << "A minimum cut: " <<std::endl;  
jacint@33
   184
  node_property_vector<list_graph, bool> mincut=preflow_push_test.mincut();
jacint@33
   185
jacint@33
   186
  for (each_node_iterator v=flow_test.first_node(); v.valid(); ++v) {
jacint@33
   187
      if (mincut.get(v)) std::cout <<node_name.get(v)<< " ";
jacint@33
   188
    }
jacint@33
   189
  
jacint@33
   190
  std::cout<<"\n\n"<<std::endl;
jacint@33
   191
jacint@33
   192
jacint@33
   193
jacint@33
   194
jacint@33
   195
  std::cout << "Testing preflow_push_max_flow..." << std::endl;
jacint@33
   196
 
jacint@33
   197
  preflow_push_max_flow<list_graph, int> max_flow_test(flow_test, s, t, cap);
jacint@33
   198
jacint@33
   199
  max_flow_test.run();
jacint@33
   200
jacint@33
   201
  std::cout << "Maximum flow value is: " << max_flow_test.maxflow() << "."<< std::endl;
jacint@33
   202
jacint@33
   203
  std::cout << "A minimum cut: " <<std::endl;  
jacint@33
   204
  node_property_vector<list_graph, bool> mincut2=max_flow_test.mincut();
jacint@33
   205
jacint@33
   206
  for (each_node_iterator v=flow_test.first_node(); v.valid(); ++v) {
jacint@33
   207
    if (mincut2.get(v)) std::cout <<node_name.get(v)<< " ";
jacint@33
   208
  }
jacint@33
   209
  
jacint@33
   210
  std::cout << std::endl <<std::endl;
jacint@33
   211
jacint@33
   212
jacint@33
   213
jacint@33
   214
    std::cout << "Testing dijkstra..." << std::endl;
jacint@33
   215
  
jacint@33
   216
    node_iterator root=v2;
jacint@33
   217
jacint@33
   218
    dijkstra<list_graph, int> dijkstra_test(flow_test, root, cap);
jacint@33
   219
jacint@33
   220
    dijkstra_test.run();
jacint@33
   221
jacint@33
   222
    for (each_node_iterator w=flow_test.first_node(); w.valid(); ++w) {
jacint@33
   223
      if (dijkstra_test.reach(w)) {
jacint@33
   224
      std::cout <<"The distance of " << w << " is " << dijkstra_test.dist(w);
jacint@33
   225
      if (dijkstra_test.pred(w).valid()) {
jacint@33
   226
      std::cout <<", a shortest path from the root ends with edge " << dijkstra_test.pred(w) <<std::endl; 
jacint@33
   227
      } else {
jacint@33
   228
       std::cout <<", this is the root."<<std::endl; }
jacint@33
   229
      
jacint@33
   230
      } else {
jacint@33
   231
	cout << w << " is not reachable from " << root <<std::endl;
jacint@33
   232
      }
jacint@33
   233
    }
jacint@33
   234
jacint@33
   235
jacint@33
   236
jacint@33
   237
  return 0;
jacint@33
   238
}
jacint@33
   239
jacint@33
   240
jacint@33
   241
jacint@33
   242
jacint@33
   243
jacint@33
   244
jacint@33
   245
jacint@33
   246
jacint@33
   247