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