COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/marci/bipartite_graph_wrapper_test.cc @ 607:327f7cf13843

Last change on this file since 607:327f7cf13843 was 602:580b329c2a0c, checked in by marci, 21 years ago

bfs_iterator -> bfs_dfs.h, some docs

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