src/work/marci/bipartite_matching_demo.cc
author alpar
Fri, 15 Apr 2005 20:26:01 +0000
changeset 1360 4338e4280f67
parent 771 ad7dff9ee2fd
permissions -rw-r--r--
Fix a bug that caused corrupt eps file if there are loops or identical
node coordinates in the graph.
marci@771
     1
// -*- c++ -*-
marci@771
     2
#include <iostream>
marci@771
     3
#include <fstream>
marci@771
     4
#include <vector>
marci@771
     5
marci@771
     6
#include <sage_graph.h>
marci@771
     7
//#include <smart_graph.h>
marci@771
     8
//#include <dimacs.h>
alpar@921
     9
#include <lemon/time_measure.h>
marci@771
    10
#include <for_each_macros.h>
marci@771
    11
#include <bfs_dfs.h>
marci@771
    12
#include <bipartite_graph_wrapper.h>
alpar@921
    13
#include <lemon/maps.h>
alpar@921
    14
#include <lemon/max_flow.h>
marci@771
    15
#include <graph_gen.h>
marci@771
    16
#include <max_bipartite_matching.h>
marci@771
    17
alpar@921
    18
using namespace lemon;
marci@771
    19
marci@771
    20
using std::cin;
marci@771
    21
using std::cout;
marci@771
    22
using std::endl;
marci@771
    23
marci@771
    24
int main() {
marci@771
    25
  //typedef UndirListGraph Graph; 
marci@771
    26
  typedef BipartiteGraph<SageGraph> Graph;
marci@771
    27
  
marci@771
    28
  typedef Graph::Node Node;
marci@771
    29
  typedef Graph::NodeIt NodeIt;
marci@771
    30
  typedef Graph::Edge Edge;
marci@771
    31
  typedef Graph::EdgeIt EdgeIt;
marci@771
    32
  typedef Graph::OutEdgeIt OutEdgeIt;
marci@771
    33
marci@771
    34
  Graph g;
marci@771
    35
marci@771
    36
  int a;
marci@771
    37
  cout << "number of nodes in the first color class=";
marci@771
    38
  cin >> a; 
marci@771
    39
  int b;
marci@771
    40
  cout << "number of nodes in the second color class=";
marci@771
    41
  cin >> b; 
marci@771
    42
  int m;
marci@771
    43
  cout << "number of edges=";
marci@771
    44
  cin >> m; 
marci@771
    45
  
marci@771
    46
  cout << "Generatig a random bipartite graph..." << endl;
marci@771
    47
  random_init();
marci@771
    48
  randomBipartiteGraph(g, a, b, m);
marci@771
    49
marci@771
    50
//   cout << "Edges of the bipartite graph:" << endl;
marci@771
    51
//   FOR_EACH_LOC(EdgeIt, e, g) cout << e << " ";
marci@771
    52
//   cout << endl;
marci@771
    53
marci@771
    54
//   cout << "Nodes:" << endl;
marci@771
    55
//   FOR_EACH_LOC(Graph::NodeIt, v, g) cout << v << " ";
marci@771
    56
//   cout << endl;
marci@771
    57
//   cout << "Nodes in T:" << endl;
marci@771
    58
//   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) cout << v << " ";
marci@771
    59
//   cout << endl;
marci@771
    60
//   cout << "Nodes in S:" << endl;
marci@771
    61
//   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) cout << v << " ";
marci@771
    62
//   cout << endl;
marci@771
    63
marci@771
    64
//   cout << "Erasing the first node..." << endl;
marci@771
    65
//   NodeIt n;
marci@771
    66
//   g.first(n);
marci@771
    67
//   g.erase(n);
marci@771
    68
//   cout << "Nodes of the bipartite graph:" << endl;
marci@771
    69
//   FOR_EACH_GLOB(n, g) cout << n << " ";
marci@771
    70
//   cout << endl;
marci@771
    71
marci@771
    72
//   cout << "Nodes in T:" << endl;
marci@771
    73
//   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) cout << v << " ";
marci@771
    74
//   cout << endl;
marci@771
    75
//   cout << "Nodes in S:" << endl;
marci@771
    76
//   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) cout << v << " ";
marci@771
    77
//   cout << endl;
marci@771
    78
marci@771
    79
  typedef stBipartiteGraphWrapper<Graph> stGW;
marci@771
    80
  stGW stgw(g);
marci@771
    81
  ConstMap<stGW::Edge, int> const1map(1);
marci@771
    82
marci@771
    83
  Timer ts;
marci@771
    84
  cout << "max bipartite matching with stGraphWrapper..." << endl;
marci@771
    85
  ts.reset();
marci@771
    86
  stGW::EdgeMap<int> flow(stgw);
marci@771
    87
  MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@771
    88
    max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow);
marci@771
    89
  max_flow_test.run();
marci@771
    90
//  while (max_flow_test.augmentOnShortestPath()) { }
marci@771
    91
//  typedef ListGraph MutableGraph;
marci@771
    92
//  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
marci@771
    93
//  while (max_flow_test.augmentOnBlockingFlow2()) {
marci@771
    94
//   cout << max_flow_test.flowValue() << endl;
marci@771
    95
//  }
marci@771
    96
  cout << "matching value: " << max_flow_test.flowValue() << endl;
marci@771
    97
  cout << "elapsed time: " << ts << endl;
marci@771
    98
//   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
marci@771
    99
//     if (flow[e]) cout << e << endl; 
marci@771
   100
//   }
marci@771
   101
  cout << endl;
marci@771
   102
marci@771
   103
  typedef ConstMap<Graph::Edge, int> EdgeCap; 
marci@771
   104
  EdgeCap ge1(1);
marci@771
   105
  typedef ConstMap<Graph::Node, int> NodeCap;
marci@771
   106
  NodeCap gn1(1);
marci@771
   107
  typedef Graph::EdgeMap<int> EdgeFlow;
marci@771
   108
  EdgeFlow gef(g); //0
marci@771
   109
  typedef Graph::NodeMap<int> NodeFlow; 
marci@771
   110
  NodeFlow gnf(g); //0 
marci@771
   111
marci@771
   112
  typedef stGW::EdgeMapWrapper<EdgeCap, NodeCap> CapMap; 
marci@771
   113
  typedef stGW::EdgeMapWrapper<EdgeFlow, NodeFlow> FlowMap; 
marci@771
   114
  CapMap cm(ge1, gn1);
marci@771
   115
  FlowMap fm(gef, gnf);
marci@771
   116
marci@771
   117
  //Timer ts;
marci@771
   118
  cout << "max bipartite matching with stGraphWrapper..." << endl;
marci@771
   119
  ts.reset();
marci@771
   120
  //stGW::EdgeMap<int> flow(stgw);
marci@771
   121
  MaxFlow<stGW, int, CapMap, FlowMap> 
marci@771
   122
    max_flow_test1(stgw, stgw.S_NODE, stgw.T_NODE, cm, fm);
marci@771
   123
  max_flow_test1.run();
marci@771
   124
//  while (max_flow_test.augmentOnShortestPath()) { }
marci@771
   125
//  typedef ListGraph MutableGraph;
marci@771
   126
//  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
marci@771
   127
//  while (max_flow_test.augmentOnBlockingFlow2()) {
marci@771
   128
//   cout << max_flow_test.flowValue() << endl;
marci@771
   129
//  }
marci@771
   130
  cout << "matching value: " << max_flow_test1.flowValue() << endl;
marci@771
   131
  cout << "elapsed time: " << ts << endl;
marci@771
   132
//   FOR_EACH_LOC(Graph::EdgeIt, e, g) { 
marci@771
   133
//     if (gef[e]) cout << e << endl; 
marci@771
   134
//   }
marci@771
   135
  cout << endl;
marci@771
   136
marci@771
   137
  cout << "max bipartite matching with stGraphWrapper..." << endl;
marci@771
   138
  ts.reset();
marci@771
   139
  FOR_EACH_LOC(Graph::EdgeIt, e, g) gef.set(e, 0); 
marci@771
   140
  FOR_EACH_LOC(Graph::NodeIt, n, g) gnf.set(n, 0); 
marci@771
   141
  MaxBipartiteMatching<Graph, ConstMap<Graph::Edge, int>, ConstMap<Graph::Node, int>, 
marci@771
   142
    Graph::EdgeMap<int>, Graph::NodeMap<int> > 
marci@771
   143
    matching_test(g, ge1, gn1, gef, gnf);
marci@771
   144
  matching_test.run();
marci@771
   145
marci@771
   146
  cout << "matching value: " << matching_test.matchingValue() << endl;
marci@771
   147
  cout << "elapsed time: " << ts << endl;
marci@771
   148
//   FOR_EACH_LOC(Graph::EdgeIt, e, g) { 
marci@771
   149
//     if (gef[e]) cout << e << endl; 
marci@771
   150
//   }
marci@771
   151
  cout << endl;
marci@771
   152
marci@771
   153
  cout << "max bipartite matching with MaxBipartiteMatching..." << endl;
marci@771
   154
  ts.reset();
marci@771
   155
  FOR_EACH_LOC(Graph::EdgeIt, e, g) gef.set(e, 0); 
marci@771
   156
  //FOR_EACH_LOC(Graph::NodeIt, n, g) gnf.set(n, 0); 
marci@771
   157
  typedef MaxBipartiteMatching<Graph, ConstMap<Graph::Edge, int>, 
marci@771
   158
    ConstMap<Graph::Node, int>, 
marci@771
   159
    Graph::EdgeMap<int>, Graph::NodeMap<int> > MaxBipartiteMatching;
marci@771
   160
  MaxBipartiteMatching matching_test_1(g, ge1, gn1, gef/*, gnf*/);
marci@771
   161
  matching_test_1.run();
marci@771
   162
marci@771
   163
  cout << "matching value: " << matching_test_1.matchingValue() << endl;
marci@771
   164
  cout << "elapsed time: " << ts << endl;
marci@771
   165
//   FOR_EACH_LOC(Graph::EdgeIt, e, g) { 
marci@771
   166
//     if (gef[e]) cout << e << endl; 
marci@771
   167
//   }
marci@771
   168
  cout << endl;
marci@771
   169
marci@771
   170
  cout << "testing optimality with MaxBipartiteMatching..." << endl;
marci@771
   171
  ts.reset();
marci@771
   172
  matching_test_1.run(MaxBipartiteMatching::GEN_MATCHING);
marci@771
   173
  cout << "matching value: " << matching_test_1.matchingValue() << endl;
marci@771
   174
  cout << "elapsed time: " << ts << endl;
marci@771
   175
marci@771
   176
  cout << "testing optimality with MaxBipartiteMatching..." << endl;
marci@771
   177
  ts.reset();
marci@771
   178
  matching_test_1.run(MaxBipartiteMatching::GEN_MATCHING_WITH_GOOD_NODE_FLOW);
marci@771
   179
  cout << "matching value: " << matching_test_1.matchingValue() << endl;
marci@771
   180
  cout << "elapsed time: " << ts << endl;
marci@771
   181
marci@771
   182
  return 0;
marci@771
   183
}