| 
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@410
 | 
    13  | 
#include <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@410
 | 
    42  | 
  typedef UndirListGraph Graph; 
  | 
| 
marci@410
 | 
    43  | 
  typedef Graph::Node Node;
  | 
| 
marci@410
 | 
    44  | 
  typedef Graph::NodeIt NodeIt;
  | 
| 
marci@410
 | 
    45  | 
  typedef Graph::Edge Edge;
  | 
| 
marci@410
 | 
    46  | 
  typedef Graph::EdgeIt EdgeIt;
  | 
| 
marci@410
 | 
    47  | 
  typedef Graph::OutEdgeIt OutEdgeIt;
  | 
| 
marci@410
 | 
    48  | 
  | 
| 
marci@410
 | 
    49  | 
  Graph g;
  | 
| 
marci@410
 | 
    50  | 
  | 
| 
marci@410
 | 
    51  | 
  std::vector<Graph::Node> s_nodes;
  | 
| 
marci@410
 | 
    52  | 
  std::vector<Graph::Node> t_nodes;
  | 
| 
marci@410
 | 
    53  | 
  | 
| 
marci@410
 | 
    54  | 
  int a;
  | 
| 
marci@410
 | 
    55  | 
  std::cout << "number of nodes in the first color class=";
  | 
| 
marci@410
 | 
    56  | 
  std::cin >> a; 
  | 
| 
marci@410
 | 
    57  | 
  int b;
  | 
| 
marci@410
 | 
    58  | 
  std::cout << "number of nodes in the second color class=";
  | 
| 
marci@410
 | 
    59  | 
  std::cin >> b; 
  | 
| 
marci@410
 | 
    60  | 
  int m;
  | 
| 
marci@410
 | 
    61  | 
  std::cout << "number of edges=";
  | 
| 
marci@410
 | 
    62  | 
  std::cin >> m; 
  | 
| 
marci@410
 | 
    63  | 
  
  | 
| 
marci@410
 | 
    64  | 
  | 
| 
marci@410
 | 
    65  | 
  for (int i=0; i<a; ++i) s_nodes.push_back(g.addNode());
  | 
| 
marci@410
 | 
    66  | 
  for (int i=0; i<b; ++i) t_nodes.push_back(g.addNode());
  | 
| 
marci@410
 | 
    67  | 
  | 
| 
marci@410
 | 
    68  | 
  random_init();
  | 
| 
marci@410
 | 
    69  | 
  for(int i=0; i<m; ++i) {
 | 
| 
marci@410
 | 
    70  | 
    g.addEdge(s_nodes[random(a)], t_nodes[random(b)]);
  | 
| 
marci@410
 | 
    71  | 
  }
  | 
| 
marci@410
 | 
    72  | 
  | 
| 
marci@410
 | 
    73  | 
  Graph::NodeMap<int> ref_map(g, -1);
  | 
| 
marci@410
 | 
    74  | 
  | 
| 
marci@410
 | 
    75  | 
  IterableBoolMap< Graph::NodeMap<int> > bipartite_map(ref_map);
  | 
| 
marci@410
 | 
    76  | 
  for (int i=0; i<a; ++i) bipartite_map.insert(s_nodes[i], false);
  | 
| 
marci@410
 | 
    77  | 
  for (int i=0; i<b; ++i) bipartite_map.insert(t_nodes[i], true);
  | 
| 
marci@410
 | 
    78  | 
  | 
| 
marci@410
 | 
    79  | 
//   Graph::Node u;
  | 
| 
marci@410
 | 
    80  | 
//   std::cout << "These nodes will be in S:\n";
  | 
| 
marci@410
 | 
    81  | 
//   //FIXME azert kellene ++, es invalid vizsgalat u-bol, hogy ezt le lehessen 
  | 
| 
marci@410
 | 
    82  | 
//   //irni 1etlen FOR_EACH-csel.
  | 
| 
marci@410
 | 
    83  | 
//   for (bipartite_map.first(u, false); g.valid(u); bipartite_map.next(u)) 
  | 
| 
marci@410
 | 
    84  | 
//     std::cout << u << " ";
  | 
| 
marci@410
 | 
    85  | 
//   std::cout << "\n";
  | 
| 
marci@410
 | 
    86  | 
//   std::cout << "These nodes will be in T:\n";
  | 
| 
marci@410
 | 
    87  | 
//   for (bipartite_map.first(u, true); g.valid(u); bipartite_map.next(u)) 
  | 
| 
marci@410
 | 
    88  | 
//     std::cout << u << " ";
  | 
| 
marci@410
 | 
    89  | 
//   std::cout << "\n";
  | 
| 
marci@410
 | 
    90  | 
  | 
| 
marci@410
 | 
    91  | 
  typedef BipartiteGraphWrapper<Graph> BGW;
  | 
| 
marci@410
 | 
    92  | 
  BGW bgw(g, bipartite_map);
  | 
| 
marci@410
 | 
    93  | 
  | 
| 
marci@410
 | 
    94  | 
//   std::cout << "Nodes by NodeIt:\n";
  | 
| 
marci@410
 | 
    95  | 
//   FOR_EACH_LOC(BGW::NodeIt, n, bgw) {
 | 
| 
marci@410
 | 
    96  | 
//     std::cout << n << " ";
  | 
| 
marci@410
 | 
    97  | 
//   }
  | 
| 
marci@410
 | 
    98  | 
//   std::cout << "\n";
  | 
| 
marci@410
 | 
    99  | 
//   std::cout << "Nodes in S by ClassNodeIt:\n";
  | 
| 
marci@410
 | 
   100  | 
//   FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.S_CLASS) {
 | 
| 
marci@410
 | 
   101  | 
//     std::cout << n << " ";
  | 
| 
marci@410
 | 
   102  | 
//   }
  | 
| 
marci@410
 | 
   103  | 
//   std::cout << "\n";
  | 
| 
marci@410
 | 
   104  | 
//   std::cout << "Nodes in T by ClassNodeIt:\n";
  | 
| 
marci@410
 | 
   105  | 
//   FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.T_CLASS) {
 | 
| 
marci@410
 | 
   106  | 
//     std::cout << n << " ";
  | 
| 
marci@410
 | 
   107  | 
//   }
  | 
| 
marci@410
 | 
   108  | 
//   std::cout << "\n";
  | 
| 
marci@410
 | 
   109  | 
//   std::cout << "Edges of the bipartite graph:\n";
  | 
| 
marci@410
 | 
   110  | 
//   FOR_EACH_LOC(BGW::EdgeIt, e, bgw) {
 | 
| 
marci@410
 | 
   111  | 
//     std::cout << bgw.tail(e) << "->" << bgw.head(e) << std::endl;
  | 
| 
marci@410
 | 
   112  | 
//   }
  | 
| 
marci@410
 | 
   113  | 
  | 
| 
marci@410
 | 
   114  | 
  BGW::NodeMap<int> dbyj(bgw);
  | 
| 
marci@410
 | 
   115  | 
  BGW::EdgeMap<int> dbyxcj(bgw);
  | 
| 
marci@410
 | 
   116  | 
  | 
| 
marci@410
 | 
   117  | 
  typedef stGraphWrapper<BGW> stGW;
  | 
| 
marci@410
 | 
   118  | 
  stGW stgw(bgw);
  | 
| 
marci@410
 | 
   119  | 
  ConstMap<stGW::Edge, int> const1map(1);
  | 
| 
marci@410
 | 
   120  | 
//  stGW::NodeMap<int> ize(stgw);
  | 
| 
marci@410
 | 
   121  | 
  | 
| 
marci@410
 | 
   122  | 
//   BfsIterator< BGW, BGW::NodeMap<bool> > bfs(bgw);
  | 
| 
marci@410
 | 
   123  | 
//   Graph::NodeIt si;
  | 
| 
marci@410
 | 
   124  | 
//   Graph::Node s; 
  | 
| 
marci@410
 | 
   125  | 
//   s=g.first(si);
  | 
| 
marci@410
 | 
   126  | 
//   bfs.pushAndSetReached(BGW::Node(s));
  | 
| 
marci@410
 | 
   127  | 
//   while (!bfs.finished()) { ++bfs; }
 | 
| 
marci@410
 | 
   128  | 
  | 
| 
marci@410
 | 
   129  | 
//   FOR_EACH_LOC(stGW::NodeIt, n, stgw) { 
 | 
| 
marci@410
 | 
   130  | 
//     std::cout << "out-edges of " << n << ":\n"; 
  | 
| 
marci@410
 | 
   131  | 
//     FOR_EACH_INC_LOC(stGW::OutEdgeIt, e, stgw, n) { 
 | 
| 
marci@410
 | 
   132  | 
//       std::cout << " " << e << "\n";
  | 
| 
marci@410
 | 
   133  | 
//       std::cout << " aNode: " << stgw.aNode(e) << "\n";
  | 
| 
marci@410
 | 
   134  | 
//       std::cout << " bNode: " << stgw.bNode(e) << "\n";      
  | 
| 
marci@410
 | 
   135  | 
//     }
  | 
| 
marci@410
 | 
   136  | 
//     std::cout << "in-edges of " << n << ":\n"; 
  | 
| 
marci@410
 | 
   137  | 
//     FOR_EACH_INC_LOC(stGW::InEdgeIt, e, stgw, n) { 
 | 
| 
marci@410
 | 
   138  | 
//       std::cout << " " << e << "\n";
  | 
| 
marci@410
 | 
   139  | 
//       std::cout << " aNode: " << stgw.aNode(e) << "\n";
  | 
| 
marci@410
 | 
   140  | 
//       std::cout << " bNode: " << stgw.bNode(e) << "\n";     
  | 
| 
marci@410
 | 
   141  | 
//     }
  | 
| 
marci@410
 | 
   142  | 
//   }
  | 
| 
marci@410
 | 
   143  | 
//   std::cout << "Edges of the stGraphWrapper:\n"; 
  | 
| 
marci@410
 | 
   144  | 
//   FOR_EACH_LOC(stGW::EdgeIt, n, stgw) { 
 | 
| 
marci@410
 | 
   145  | 
//     std::cout << " " << n << "\n";
  | 
| 
marci@410
 | 
   146  | 
//   }
  | 
| 
marci@410
 | 
   147  | 
  | 
| 
marci@410
 | 
   148  | 
//   stGW::NodeMap<bool> b(stgw);
  | 
| 
marci@410
 | 
   149  | 
//   FOR_EACH_LOC(stGW::NodeIt, n, stgw) { 
 | 
| 
marci@410
 | 
   150  | 
//     std::cout << n << ": " << b[n] <<"\n";
  | 
| 
marci@410
 | 
   151  | 
//   }
  | 
| 
marci@410
 | 
   152  | 
  | 
| 
marci@410
 | 
   153  | 
//   std::cout << "Bfs from s: \n";
  | 
| 
marci@410
 | 
   154  | 
//   BfsIterator< stGW, stGW::NodeMap<bool> > bfs_stgw(stgw);
  | 
| 
marci@410
 | 
   155  | 
//   bfs_stgw.pushAndSetReached(stgw.S_NODE);
  | 
| 
marci@410
 | 
   156  | 
//   while (!bfs_stgw.finished()) { 
 | 
| 
marci@410
 | 
   157  | 
//     std::cout << " " << stGW::OutEdgeIt(bfs_stgw) << "\n";
  | 
| 
marci@410
 | 
   158  | 
//     ++bfs_stgw; 
  | 
| 
marci@410
 | 
   159  | 
//   }
  | 
| 
marci@410
 | 
   160  | 
  | 
| 
marci@410
 | 
   161  | 
  | 
| 
marci@410
 | 
   162  | 
  Timer ts;
  | 
| 
marci@410
 | 
   163  | 
  ts.reset();
  | 
| 
marci@410
 | 
   164  | 
  stGW::EdgeMap<int> max_flow(stgw);
  | 
| 
marci@410
 | 
   165  | 
  MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
  | 
| 
marci@410
 | 
   166  | 
    max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, max_flow);
  | 
| 
marci@410
 | 
   167  | 
//  while (max_flow_test.augmentOnShortestPath()) { }
 | 
| 
marci@410
 | 
   168  | 
  typedef ListGraph MutableGraph;
  | 
| 
marci@410
 | 
   169  | 
//  while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) {
 | 
| 
marci@410
 | 
   170  | 
  while (max_flow_test.augmentOnBlockingFlow2()) {
 | 
| 
marci@410
 | 
   171  | 
   std::cout << max_flow_test.flowValue() << std::endl;
  | 
| 
marci@410
 | 
   172  | 
  }
  | 
| 
marci@410
 | 
   173  | 
  std::cout << "max flow value: " << max_flow_test.flowValue() << std::endl;
  | 
| 
marci@410
 | 
   174  | 
  std::cout << "elapsed time: " << ts << std::endl;
  | 
| 
marci@410
 | 
   175  | 
//   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
 | 
| 
marci@410
 | 
   176  | 
//     std::cout << e << ": " << max_flow[e] << "\n"; 
  | 
| 
marci@410
 | 
   177  | 
//   }
  | 
| 
marci@410
 | 
   178  | 
//   std::cout << "\n";
  | 
| 
marci@410
 | 
   179  | 
  | 
| 
marci@410
 | 
   180  | 
  ts.reset();
  | 
| 
marci@410
 | 
   181  | 
  stGW::EdgeMap<int> pre_flow(stgw);
  | 
| 
marci@476
 | 
   182  | 
  MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
  | 
| 
marci@465
 | 
   183  | 
    pre_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, pre_flow/*, true*/);
  | 
| 
marci@410
 | 
   184  | 
  pre_flow_test.run();
  | 
| 
marci@410
 | 
   185  | 
  std::cout << "pre flow value: " << max_flow_test.flowValue() << std::endl;
  | 
| 
marci@410
 | 
   186  | 
  std::cout << "elapsed time: " << ts << std::endl;
  | 
| 
marci@410
 | 
   187  | 
//   FOR_EACH_LOC(stGW::EdgeIt, e, stgw) { 
 | 
| 
marci@410
 | 
   188  | 
//     std::cout << e << ": " << pre_flow[e] << "\n"; 
  | 
| 
marci@410
 | 
   189  | 
//   }
  | 
| 
marci@410
 | 
   190  | 
//   std::cout << "\n";
  | 
| 
marci@410
 | 
   191  | 
  | 
| 
marci@410
 | 
   192  | 
  return 0;
  | 
| 
marci@410
 | 
   193  | 
}
  |