src/work/marci/bipartite_graph_wrapper_test.cc
author marci
Thu, 29 Apr 2004 16:30:39 +0000
changeset 480 4fb0d1e166ea
parent 476 cfe550761745
child 496 7c463a7635d4
permissions -rw-r--r--
corrections
marci@368
     1
// -*- c++ -*-
marci@368
     2
#include <iostream>
marci@368
     3
#include <fstream>
marci@368
     4
#include <vector>
marci@368
     5
marci@368
     6
#include <list_graph.h>
marci@389
     7
//#include <smart_graph.h>
marci@368
     8
//#include <dimacs.h>
marci@368
     9
#include <time_measure.h>
marci@368
    10
#include <for_each_macros.h>
marci@368
    11
#include <bfs_iterator.h>
marci@368
    12
#include <graph_wrapper.h>
marci@379
    13
#include <maps.h>
marci@480
    14
#include <max_flow.h>
marci@368
    15
marci@368
    16
using namespace hugo;
marci@368
    17
marci@368
    18
int main() {
marci@368
    19
  typedef UndirListGraph Graph; 
marci@368
    20
  typedef Graph::Node Node;
marci@368
    21
  typedef Graph::NodeIt NodeIt;
marci@368
    22
  typedef Graph::Edge Edge;
marci@368
    23
  typedef Graph::EdgeIt EdgeIt;
marci@368
    24
  typedef Graph::OutEdgeIt OutEdgeIt;
marci@368
    25
marci@368
    26
  Graph g;
marci@409
    27
//   std::vector<Graph::Node> s_nodes;
marci@409
    28
//   std::vector<Graph::Node> t_nodes;
marci@409
    29
//   for (int i=0; i<3; ++i) s_nodes.push_back(g.addNode());
marci@409
    30
//   for (int i=0; i<3; ++i) t_nodes.push_back(g.addNode());
marci@409
    31
//   g.addEdge(s_nodes[0], t_nodes[2]);
marci@409
    32
//   g.addEdge(t_nodes[1], s_nodes[2]);
marci@409
    33
//   g.addEdge(s_nodes[0], t_nodes[1]);
marci@409
    34
  
marci@409
    35
//   Graph::NodeMap<int> ref_map(g, -1);
marci@409
    36
//   IterableBoolMap< Graph::NodeMap<int> > bipartite_map(ref_map);
marci@409
    37
//   for (int i=0; i<3; ++i) bipartite_map.insert(s_nodes[i], false);
marci@409
    38
//   for (int i=0; i<3; ++i) bipartite_map.insert(t_nodes[i], true);
marci@409
    39
marci@409
    40
  std::vector<Graph::Node> nodes;
marci@409
    41
  for (int i=0; i<3; ++i) nodes.push_back(g.addNode());
marci@409
    42
  for (int i=3; i<6; ++i) nodes.push_back(g.addNode());
marci@409
    43
  g.addEdge(nodes[0], nodes[3+2]);
marci@409
    44
  g.addEdge(nodes[3+1], nodes[2]);
marci@409
    45
  g.addEdge(nodes[0], nodes[3+1]);
marci@368
    46
  
marci@368
    47
  Graph::NodeMap<int> ref_map(g, -1);
marci@368
    48
  IterableBoolMap< Graph::NodeMap<int> > bipartite_map(ref_map);
marci@409
    49
  for (int i=0; i<3; ++i) bipartite_map.insert(nodes[i], false);
marci@409
    50
  for (int i=3; i<6; ++i) bipartite_map.insert(nodes[i], true);
marci@409
    51
marci@409
    52
  Graph::Node u;
marci@409
    53
  std::cout << "These nodes will be in S:\n";
marci@409
    54
  //FIXME azert kellene ++, es invalid vizsgalat u-bol, hogy ezt le lehessen 
marci@409
    55
  //irni 1etlen FOR_EACH-csel.
marci@409
    56
  for (bipartite_map.first(u, false); g.valid(u); bipartite_map.next(u)) 
marci@409
    57
    std::cout << u << " ";
marci@409
    58
  std::cout << "\n";
marci@409
    59
  std::cout << "These nodes will be in T:\n";
marci@409
    60
  for (bipartite_map.first(u, true); g.valid(u); bipartite_map.next(u)) 
marci@409
    61
    std::cout << u << " ";
marci@409
    62
  std::cout << "\n";
marci@409
    63
marci@368
    64
  typedef BipartiteGraphWrapper<Graph> BGW;
marci@368
    65
  BGW bgw(g, bipartite_map);
marci@409
    66
marci@409
    67
  std::cout << "Nodes by NodeIt:\n";
marci@409
    68
  FOR_EACH_LOC(BGW::NodeIt, n, bgw) {
marci@409
    69
    std::cout << n << " ";
marci@409
    70
  }
marci@409
    71
  std::cout << "\n";
marci@409
    72
  std::cout << "Nodes in S by ClassNodeIt:\n";
marci@409
    73
  FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.S_CLASS) {
marci@409
    74
    std::cout << n << " ";
marci@409
    75
  }
marci@409
    76
  std::cout << "\n";
marci@409
    77
  std::cout << "Nodes in T by ClassNodeIt:\n";
marci@409
    78
  FOR_EACH_INC_LOC(BGW::ClassNodeIt, n, bgw, bgw.T_CLASS) {
marci@409
    79
    std::cout << n << " ";
marci@409
    80
  }
marci@409
    81
  std::cout << "\n";
marci@409
    82
  std::cout << "Edges of the bipartite graph:\n";
marci@368
    83
  FOR_EACH_LOC(BGW::EdgeIt, e, bgw) {
marci@368
    84
    std::cout << bgw.tail(e) << "->" << bgw.head(e) << std::endl;
marci@368
    85
  }
marci@368
    86
marci@379
    87
  BGW::NodeMap<int> dbyj(bgw);
marci@379
    88
  BGW::EdgeMap<int> dbyxcj(bgw);
marci@368
    89
marci@379
    90
  typedef stGraphWrapper<BGW> stGW;
marci@379
    91
  stGW stgw(bgw);
marci@379
    92
  ConstMap<stGW::Edge, int> const1map(1);
marci@379
    93
  stGW::NodeMap<int> ize(stgw);
marci@379
    94
  stGW::EdgeMap<int> flow(stgw);
marci@379
    95
marci@379
    96
  BfsIterator< BGW, BGW::NodeMap<bool> > bfs(bgw);
marci@379
    97
  Graph::NodeIt si;
marci@379
    98
  Graph::Node s; 
marci@379
    99
  s=g.first(si);
marci@379
   100
  bfs.pushAndSetReached(BGW::Node(s));
marci@409
   101
  while (!bfs.finished()) { ++bfs; }
marci@379
   102
marci@409
   103
  FOR_EACH_LOC(stGW::NodeIt, n, stgw) { 
marci@409
   104
    std::cout << "out-edges of " << n << ":\n"; 
marci@409
   105
    FOR_EACH_INC_LOC(stGW::OutEdgeIt, e, stgw, n) { 
marci@409
   106
      std::cout << " " << e << "\n";
marci@409
   107
      std::cout << " aNode: " << stgw.aNode(e) << "\n";
marci@409
   108
      std::cout << " bNode: " << stgw.bNode(e) << "\n";      
marci@409
   109
    }
marci@409
   110
    std::cout << "in-edges of " << n << ":\n"; 
marci@409
   111
    FOR_EACH_INC_LOC(stGW::InEdgeIt, e, stgw, n) { 
marci@409
   112
      std::cout << " " << e << "\n";
marci@409
   113
      std::cout << " aNode: " << stgw.aNode(e) << "\n";
marci@409
   114
      std::cout << " bNode: " << stgw.bNode(e) << "\n";     
marci@409
   115
    }
marci@409
   116
  }
marci@409
   117
  std::cout << "Edges of the stGraphWrapper:\n"; 
marci@409
   118
  FOR_EACH_LOC(stGW::EdgeIt, n, stgw) { 
marci@409
   119
    std::cout << " " << n << "\n";
marci@409
   120
  }
marci@379
   121
marci@409
   122
  stGW::NodeMap<bool> b(stgw);
marci@409
   123
  FOR_EACH_LOC(stGW::NodeIt, n, stgw) { 
marci@409
   124
    std::cout << n << ": " << b[n] <<"\n";
marci@409
   125
  }
marci@409
   126
marci@409
   127
  std::cout << "Bfs from s: \n";
marci@409
   128
  BfsIterator< stGW, stGW::NodeMap<bool> > bfs_stgw(stgw);
marci@409
   129
  bfs_stgw.pushAndSetReached(stgw.S_NODE);
marci@409
   130
  while (!bfs_stgw.finished()) { 
marci@409
   131
    std::cout << " " << stGW::OutEdgeIt(bfs_stgw) << "\n";
marci@409
   132
    ++bfs_stgw; 
marci@409
   133
  }
marci@379
   134
  
marci@379
   135
  MaxFlow<stGW, int, ConstMap<stGW::Edge, int>, stGW::EdgeMap<int> > 
marci@379
   136
    max_flow_test(stgw, stgw.S_NODE, stgw.T_NODE, const1map, flow);
marci@409
   137
  while (max_flow_test.augmentOnShortestPath()) { }
marci@393
   138
marci@393
   139
  std::cout << max_flow_test.flowValue() << std::endl;
marci@368
   140
marci@368
   141
  return 0;
marci@368
   142
}