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