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