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