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