src/work/marci/bipartite_matching_try_2.cc
author jacint
Thu, 06 May 2004 09:26:23 +0000
changeset 538 d8863141824d
parent 501 20e4941a354a
child 555 995bc1f1a3ce
permissions -rw-r--r--
(none)
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@410
    10
#include <time_measure.h>
marci@410
    11
#include <for_each_macros.h>
marci@410
    12
#include <bfs_iterator.h>
marci@496
    13
#include <bipartite_graph_wrapper.h>
marci@410
    14
#include <maps.h>
marci@480
    15
#include <max_flow.h>
marci@410
    16
marci@410
    17
/**
marci@410
    18
 * Inicializalja a veletlenszamgeneratort.
marci@410
    19
 * Figyelem, ez nem jo igazi random szamokhoz,
marci@410
    20
 * erre ne bizzad a titkaidat!
marci@410
    21
 */
marci@410
    22
void random_init()
marci@410
    23
{
marci@410
    24
	unsigned int seed = getpid();
marci@410
    25
	seed |= seed << 15;
marci@410
    26
	seed ^= time(0);
marci@410
    27
marci@410
    28
	srand(seed);
marci@410
    29
}
marci@410
    30
marci@410
    31
/**
marci@410
    32
 * Egy veletlen int-et ad vissza 0 es m-1 kozott.
marci@410
    33
 */
marci@410
    34
int random(int m)
marci@410
    35
{
marci@410
    36
  return int( double(m) * rand() / (RAND_MAX + 1.0) );
marci@410
    37
}
marci@410
    38
marci@410
    39
using namespace hugo;
marci@410
    40
marci@410
    41
int main() {
marci@499
    42
  //typedef UndirListGraph Graph; 
marci@499
    43
  typedef BipartiteGraph<ListGraph> Graph;
marci@499
    44
  
marci@410
    45
  typedef Graph::Node Node;
marci@410
    46
  typedef Graph::NodeIt NodeIt;
marci@410
    47
  typedef Graph::Edge Edge;
marci@410
    48
  typedef Graph::EdgeIt EdgeIt;
marci@410
    49
  typedef Graph::OutEdgeIt OutEdgeIt;
marci@410
    50
marci@410
    51
  Graph g;
marci@410
    52
marci@410
    53
  std::vector<Graph::Node> s_nodes;
marci@410
    54
  std::vector<Graph::Node> t_nodes;
marci@410
    55
marci@410
    56
  int a;
marci@410
    57
  std::cout << "number of nodes in the first color class=";
marci@410
    58
  std::cin >> a; 
marci@410
    59
  int b;
marci@410
    60
  std::cout << "number of nodes in the second color class=";
marci@410
    61
  std::cin >> b; 
marci@410
    62
  int m;
marci@410
    63
  std::cout << "number of edges=";
marci@410
    64
  std::cin >> m; 
marci@410
    65
  
marci@500
    66
  std::cout << "Generatig a random bipartite graph..." << std::endl;
marci@499
    67
  for (int i=0; i<a; ++i) s_nodes.push_back(g.addNode(false));
marci@499
    68
  for (int i=0; i<b; ++i) t_nodes.push_back(g.addNode(true));
marci@410
    69
marci@410
    70
  random_init();
marci@410
    71
  for(int i=0; i<m; ++i) {
marci@410
    72
    g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
marci@410
    73
  }
marci@410
    74
marci@510
    75
//   std::cout << "Edges of the bipartite graph:" << std::endl;
marci@510
    76
//   FOR_EACH_LOC(EdgeIt, e, g) std::cout << e << " ";
marci@510
    77
//   std::cout << std::endl;
marci@410
    78
marci@510
    79
//   std::cout << "Nodes:" << std::endl;
marci@510
    80
//   FOR_EACH_LOC(Graph::NodeIt, v, g) std::cout << v << " ";
marci@510
    81
//   std::cout << std::endl;
marci@510
    82
//   std::cout << "Nodes in T:" << std::endl;
marci@510
    83
//   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) std::cout << v << " ";
marci@510
    84
//   std::cout << std::endl;
marci@510
    85
//   std::cout << "Nodes in S:" << std::endl;
marci@510
    86
//   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) std::cout << v << " ";
marci@510
    87
//   std::cout << std::endl;
marci@500
    88
marci@510
    89
//   std::cout << "Erasing the first node..." << std::endl;
marci@510
    90
//   NodeIt n;
marci@510
    91
//   g.first(n);
marci@510
    92
//   g.erase(n);
marci@510
    93
//   std::cout << "Nodes of the bipartite graph:" << std::endl;
marci@510
    94
//   FOR_EACH_GLOB(n, g) std::cout << n << " ";
marci@510
    95
//   std::cout << std::endl;
marci@500
    96
marci@510
    97
//   std::cout << "Nodes in T:" << std::endl;
marci@510
    98
//   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::T_CLASS) std::cout << v << " ";
marci@510
    99
//   std::cout << std::endl;
marci@510
   100
//   std::cout << "Nodes in S:" << std::endl;
marci@510
   101
//   FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, Graph::S_CLASS) std::cout << v << " ";
marci@510
   102
//   std::cout << std::endl;
marci@500
   103
marci@499
   104
  typedef stGraphWrapper<Graph> stGW;
marci@499
   105
  stGW stgw(g);
marci@499
   106
  ConstMap<stGW::Edge, int> const1map(1);
marci@410
   107
marci@410
   108
  Timer ts;
marci@410
   109
  ts.reset();
marci@499
   110
  stGW::EdgeMap<int> flow(stgw);
marci@410
   111
  MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@499
   112
    max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow);
marci@500
   113
  max_flow_test.run();
marci@410
   114
//  while (max_flow_test.augmentOnShortestPath()) { }
marci@500
   115
//  typedef ListGraph MutableGraph;
marci@410
   116
//  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
marci@500
   117
//  while (max_flow_test.augmentOnBlockingFlow2()) {
marci@500
   118
//   std::cout << max_flow_test.flowValue() << std::endl;
marci@500
   119
//  }
marci@410
   120
  std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl;
marci@410
   121
  std::cout << "elapsed time: " << ts << std::endl;
marci@510
   122
//   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
marci@510
   123
//     if (flow[e]) std::cout << e << std::endl; 
marci@510
   124
//   }
marci@510
   125
//   std::cout << std::endl;
marci@410
   126
marci@410
   127
  return 0;
marci@410
   128
}