src/work/marci/bipartite_matching_try.cc
author marci
Thu, 06 May 2004 16:54:54 +0000
changeset 555 995bc1f1a3ce
parent 498 eb8bfa683d92
child 557 9c0ce0a1f000
permissions -rw-r--r--
#include <hugo/ > modifications
marci@410
     1
// -*- c++ -*-
marci@410
     2
#include <iostream>
marci@410
     3
#include <fstream>
marci@410
     4
#include <vector>
marci@410
     5
#include <cstdlib>
marci@410
     6
marci@410
     7
#include <list_graph.h>
marci@410
     8
//#include <smart_graph.h>
marci@410
     9
//#include <dimacs.h>
marci@555
    10
#include <hugo/time_measure.h>
marci@410
    11
#include <for_each_macros.h>
marci@410
    12
#include <bfs_iterator.h>
marci@410
    13
#include <graph_wrapper.h>
marci@496
    14
#include <bipartite_graph_wrapper.h>
marci@555
    15
#include <hugo/maps.h>
marci@480
    16
#include <max_flow.h>
marci@410
    17
marci@410
    18
/**
marci@410
    19
 * Inicializalja a veletlenszamgeneratort.
marci@410
    20
 * Figyelem, ez nem jo igazi random szamokhoz,
marci@410
    21
 * erre ne bizzad a titkaidat!
marci@410
    22
 */
marci@410
    23
void random_init()
marci@410
    24
{
marci@410
    25
	unsigned int seed = getpid();
marci@410
    26
	seed |= seed << 15;
marci@410
    27
	seed ^= time(0);
marci@410
    28
marci@410
    29
	srand(seed);
marci@410
    30
}
marci@410
    31
marci@410
    32
/**
marci@410
    33
 * Egy veletlen int-et ad vissza 0 es m-1 kozott.
marci@410
    34
 */
marci@410
    35
int random(int m)
marci@410
    36
{
marci@410
    37
  return int( double(m) * rand() / (RAND_MAX + 1.0) );
marci@410
    38
}
marci@410
    39
marci@410
    40
using namespace hugo;
marci@410
    41
marci@410
    42
int main() {
marci@410
    43
  typedef UndirListGraph Graph; 
marci@410
    44
  typedef Graph::Node Node;
marci@410
    45
  typedef Graph::NodeIt NodeIt;
marci@410
    46
  typedef Graph::Edge Edge;
marci@410
    47
  typedef Graph::EdgeIt EdgeIt;
marci@410
    48
  typedef Graph::OutEdgeIt OutEdgeIt;
marci@410
    49
marci@410
    50
  Graph g;
marci@410
    51
marci@410
    52
  std::vector<Graph::Node> s_nodes;
marci@410
    53
  std::vector<Graph::Node> t_nodes;
marci@410
    54
marci@410
    55
  int a;
marci@410
    56
  std::cout << "number of nodes in the first color class=";
marci@410
    57
  std::cin >> a; 
marci@410
    58
  int b;
marci@410
    59
  std::cout << "number of nodes in the second color class=";
marci@410
    60
  std::cin >> b; 
marci@410
    61
  int m;
marci@410
    62
  std::cout << "number of edges=";
marci@410
    63
  std::cin >> m; 
marci@410
    64
  
marci@410
    65
marci@410
    66
  for (int i=0; i<a; ++i) s_nodes.push_back(g.addNode());
marci@410
    67
  for (int i=0; i<b; ++i) t_nodes.push_back(g.addNode());
marci@410
    68
marci@410
    69
  random_init();
marci@410
    70
  for(int i=0; i<m; ++i) {
marci@410
    71
    g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
marci@410
    72
  }
marci@410
    73
marci@410
    74
  Graph::NodeMap<int> ref_map(g, -1);
marci@410
    75
marci@410
    76
  IterableBoolMap< Graph::NodeMap<int> > bipartite_map(ref_map);
marci@410
    77
  for (int i=0; i<a; ++i) bipartite_map.insert(s_nodes[i], false);
marci@410
    78
  for (int i=0; i<b; ++i) bipartite_map.insert(t_nodes[i], true);
marci@410
    79
marci@410
    80
//   Graph::Node u;
marci@410
    81
//   std::cout << "These nodes will be in S:\n";
marci@410
    82
//   //FIXME azert kellene ++, es invalid vizsgalat u-bol, hogy ezt le lehessen 
marci@410
    83
//   //irni 1etlen FOR_EACH-csel.
marci@410
    84
//   for (bipartite_map.first(u, false); g.valid(u); bipartite_map.next(u)) 
marci@410
    85
//     std::cout << u << " ";
marci@410
    86
//   std::cout << "\n";
marci@410
    87
//   std::cout << "These nodes will be in T:\n";
marci@410
    88
//   for (bipartite_map.first(u, true); g.valid(u); bipartite_map.next(u)) 
marci@410
    89
//     std::cout << u << " ";
marci@410
    90
//   std::cout << "\n";
marci@410
    91
marci@410
    92
  typedef BipartiteGraphWrapper<Graph> BGW;
marci@410
    93
  BGW bgw(g, bipartite_map);
marci@410
    94
marci@410
    95
//   std::cout << "Nodes by NodeIt:\n";
marci@410
    96
//   FOR_EACH_LOC(BGW::NodeIt, n, bgw) {
marci@410
    97
//     std::cout << n << " ";
marci@410
    98
//   }
marci@410
    99
//   std::cout << "\n";
marci@410
   100
//   std::cout << "Nodes in S by ClassNodeIt:\n";
marci@410
   101
//   FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.S_CLASS) {
marci@410
   102
//     std::cout << n << " ";
marci@410
   103
//   }
marci@410
   104
//   std::cout << "\n";
marci@410
   105
//   std::cout << "Nodes in T by ClassNodeIt:\n";
marci@410
   106
//   FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.T_CLASS) {
marci@410
   107
//     std::cout << n << " ";
marci@410
   108
//   }
marci@410
   109
//   std::cout << "\n";
marci@410
   110
//   std::cout << "Edges of the bipartite graph:\n";
marci@410
   111
//   FOR_EACH_LOC(BGW::EdgeIt, e, bgw) {
marci@410
   112
//     std::cout << bgw.tail(e) << "->" << bgw.head(e) << std::endl;
marci@410
   113
//   }
marci@410
   114
marci@498
   115
//  BGW::NodeMap<int> dbyj(bgw);
marci@498
   116
//  BGW::EdgeMap<int> dbyxcj(bgw);
marci@410
   117
marci@410
   118
  typedef stGraphWrapper<BGW> stGW;
marci@410
   119
  stGW stgw(bgw);
marci@410
   120
  ConstMap<stGW::Edge, int> const1map(1);
marci@410
   121
//  stGW::NodeMap<int> ize(stgw);
marci@410
   122
marci@410
   123
//   BfsIterator< BGW, BGW::NodeMap<bool> > bfs(bgw);
marci@410
   124
//   Graph::NodeIt si;
marci@410
   125
//   Graph::Node s; 
marci@410
   126
//   s=g.first(si);
marci@410
   127
//   bfs.pushAndSetReached(BGW::Node(s));
marci@410
   128
//   while (!bfs.finished()) { ++bfs; }
marci@410
   129
marci@410
   130
//   FOR_EACH_LOC(stGW::NodeIt, n, stgw) { 
marci@410
   131
//     std::cout << "out-edges of " << n << ":\n"; 
marci@410
   132
//     FOR_EACH_INC_LOC(stGW::OutEdgeIt, e, stgw, n) { 
marci@410
   133
//       std::cout << " " << e << "\n";
marci@410
   134
//       std::cout << " aNode: " << stgw.aNode(e) << "\n";
marci@410
   135
//       std::cout << " bNode: " << stgw.bNode(e) << "\n";      
marci@410
   136
//     }
marci@410
   137
//     std::cout << "in-edges of " << n << ":\n"; 
marci@410
   138
//     FOR_EACH_INC_LOC(stGW::InEdgeIt, e, stgw, n) { 
marci@410
   139
//       std::cout << " " << e << "\n";
marci@410
   140
//       std::cout << " aNode: " << stgw.aNode(e) << "\n";
marci@410
   141
//       std::cout << " bNode: " << stgw.bNode(e) << "\n";     
marci@410
   142
//     }
marci@410
   143
//   }
marci@410
   144
//   std::cout << "Edges of the stGraphWrapper:\n"; 
marci@410
   145
//   FOR_EACH_LOC(stGW::EdgeIt, n, stgw) { 
marci@410
   146
//     std::cout << " " << n << "\n";
marci@410
   147
//   }
marci@410
   148
marci@410
   149
//   stGW::NodeMap<bool> b(stgw);
marci@410
   150
//   FOR_EACH_LOC(stGW::NodeIt, n, stgw) { 
marci@410
   151
//     std::cout << n << ": " << b[n] <<"\n";
marci@410
   152
//   }
marci@410
   153
marci@410
   154
//   std::cout << "Bfs from s: \n";
marci@410
   155
//   BfsIterator< stGW, stGW::NodeMap<bool> > bfs_stgw(stgw);
marci@410
   156
//   bfs_stgw.pushAndSetReached(stgw.S_NODE);
marci@410
   157
//   while (!bfs_stgw.finished()) { 
marci@410
   158
//     std::cout << " " << stGW::OutEdgeIt(bfs_stgw) << "\n";
marci@410
   159
//     ++bfs_stgw; 
marci@410
   160
//   }
marci@410
   161
marci@410
   162
marci@410
   163
  Timer ts;
marci@410
   164
  ts.reset();
marci@410
   165
  stGW::EdgeMap<int> max_flow(stgw);
marci@410
   166
  MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@410
   167
    max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, max_flow);
marci@410
   168
//  while (max_flow_test.augmentOnShortestPath()) { }
marci@410
   169
  typedef ListGraph MutableGraph;
marci@410
   170
//  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
marci@410
   171
  while (max_flow_test.augmentOnBlockingFlow2()) {
marci@410
   172
   std::cout << max_flow_test.flowValue() << std::endl;
marci@410
   173
  }
marci@410
   174
  std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl;
marci@410
   175
  std::cout << "elapsed time: " << ts << std::endl;
marci@410
   176
//   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
marci@410
   177
//     std::cout << e << ": " << max_flow[e] << "\n"; 
marci@410
   178
//   }
marci@410
   179
//   std::cout << "\n";
marci@410
   180
marci@410
   181
  ts.reset();
marci@410
   182
  stGW::EdgeMap<int> pre_flow(stgw);
marci@476
   183
  MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@465
   184
    pre_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, pre_flow/*, true*/);
marci@410
   185
  pre_flow_test.run();
marci@410
   186
  std::cout << "pre flow value: " << max_flow_test.flowValue() << std::endl;
marci@410
   187
  std::cout << "elapsed time: " << ts << std::endl;
marci@410
   188
//   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
marci@410
   189
//     std::cout << e << ": " << pre_flow[e] << "\n"; 
marci@410
   190
//   }
marci@410
   191
//   std::cout << "\n";
marci@410
   192
marci@410
   193
  return 0;
marci@410
   194
}