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