src/work/marci/bipartite_matching_try_2.cc
author marci
Fri, 30 Apr 2004 17:48:50 +0000
changeset 500 1a45623b4796
parent 499 767f3da8ce0e
child 501 20e4941a354a
permissions -rw-r--r--
misc
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@499
    75
  std::cout << "Edges of the bipartite graph:" << std::endl;
marci@499
    76
  FOR_EACH_LOC(EdgeIt, e, g) std::cout << e << " ";
marci@499
    77
  std::cout << std::endl;
marci@410
    78
marci@500
    79
  std::cout << "Nodes in T:" << std::endl;
marci@500
    80
  FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, g.T_CLASS) std::cout << v << " ";
marci@500
    81
  std::cout << std::endl;
marci@500
    82
  std::cout << "Nodes in S:" << std::endl;
marci@500
    83
  FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, g.S_CLASS) std::cout << v << " ";
marci@500
    84
  std::cout << std::endl;
marci@500
    85
marci@500
    86
  std::cout << "Erasing the first node..." << std::endl;
marci@500
    87
  NodeIt n;
marci@500
    88
  g.first(n);
marci@500
    89
  g.erase(n);
marci@500
    90
  std::cout << "Nodes of the bipartite graph:" << std::endl;
marci@500
    91
  FOR_EACH_GLOB(n, g) std::cout << n << " ";
marci@500
    92
  std::cout << std::endl;
marci@500
    93
marci@500
    94
  std::cout << "Nodes in T:" << std::endl;
marci@500
    95
  FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, g.T_CLASS) std::cout << v << " ";
marci@500
    96
  std::cout << std::endl;
marci@500
    97
  std::cout << "Nodes in S:" << std::endl;
marci@500
    98
  FOR_EACH_INC_LOC(Graph::ClassNodeIt, v, g, g.S_CLASS) std::cout << v << " ";
marci@500
    99
  std::cout << std::endl;
marci@500
   100
marci@499
   101
  typedef stGraphWrapper<Graph> stGW;
marci@499
   102
  stGW stgw(g);
marci@499
   103
  ConstMap<stGW::Edge, int> const1map(1);
marci@410
   104
marci@410
   105
  Timer ts;
marci@410
   106
  ts.reset();
marci@499
   107
  stGW::EdgeMap<int> flow(stgw);
marci@410
   108
  MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@499
   109
    max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow);
marci@500
   110
  max_flow_test.run();
marci@410
   111
//  while (max_flow_test.augmentOnShortestPath()) { }
marci@500
   112
//  typedef ListGraph MutableGraph;
marci@410
   113
//  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
marci@500
   114
//  while (max_flow_test.augmentOnBlockingFlow2()) {
marci@500
   115
//   std::cout << max_flow_test.flowValue() << std::endl;
marci@500
   116
//  }
marci@410
   117
  std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl;
marci@410
   118
  std::cout << "elapsed time: " << ts << std::endl;
marci@499
   119
  FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
marci@499
   120
    if (flow[e]) std::cout << e << std::endl; 
marci@499
   121
  }
marci@499
   122
  std::cout << std::endl;
marci@410
   123
marci@410
   124
  return 0;
marci@410
   125
}