-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.cpp
179 lines (125 loc) · 5.06 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include <boost/graph/graphviz.hpp>
#include "graph.h"
#include "pushrelabel.h"
Graph test_pr() {
/// Graph build : cf Cormen book page 727
// Max flow should be 23
size_t num_vertices = 6;
Graph g(num_vertices);
add_edge(0, 1, EdgeProperties{16, 0}, g);
add_edge(0, 2, EdgeProperties{13, 0}, g);
add_edge(1, 3, EdgeProperties{12, 0}, g);
add_edge(2, 1, EdgeProperties{4, 0}, g);
add_edge(2, 4, EdgeProperties{14, 0}, g);
add_edge(3, 2, EdgeProperties{9, 0}, g);
add_edge(3, 5, EdgeProperties{20, 0}, g);
add_edge(4, 3, EdgeProperties{7, 0}, g);
add_edge(4, 5, EdgeProperties{4, 0}, g);
// caution : need to symmetrize the graph
symmetrize_graph(g);
split_edges(g);
return g;
}
Graph test_pr2() {
/// https://www.cs.purdue.edu/homes/ayg/CS251/slides/chap10c.pdf
// Max flow should be 5
size_t num_vertices = 7;
Graph g(num_vertices);
add_edge(0, 1, EdgeProperties{2, 0}, g);
add_edge(0, 2, EdgeProperties{3, 0}, g);
add_edge(0, 3, EdgeProperties{1, 0}, g);
add_edge(1, 3, EdgeProperties{2, 0}, g);
add_edge(1, 4, EdgeProperties{1, 0}, g);
add_edge(2, 3, EdgeProperties{2, 0}, g);
add_edge(2, 5, EdgeProperties{2, 0}, g);
add_edge(3, 4, EdgeProperties{2, 0}, g);
add_edge(3, 5, EdgeProperties{4, 0}, g);
add_edge(3, 6, EdgeProperties{2, 0}, g);
add_edge(4, 6, EdgeProperties{2, 0}, g);
add_edge(5, 6, EdgeProperties{1, 0}, g);
// caution : need to symmetrize the graph
symmetrize_graph(g);
split_edges(g);
return g;
}
Graph test_pr3() {
/// https://developers.google.com/optimization/flow/maxflow
// Max flow should be 60
size_t num_vertices = 5;
Graph g(num_vertices);
add_edge(0, 1, EdgeProperties{20, 0}, g);
add_edge(0, 2, EdgeProperties{30, 0}, g);
add_edge(0, 3, EdgeProperties{10, 0}, g);
add_edge(1, 2, EdgeProperties{40, 0}, g);
add_edge(1, 4, EdgeProperties{30, 0}, g);
add_edge(2, 3, EdgeProperties{10, 0}, g);
add_edge(2, 4, EdgeProperties{20, 0}, g);
add_edge(3, 2, EdgeProperties{5, 0}, g);
add_edge(3, 4, EdgeProperties{20, 0}, g);
// caution : need to symmetrize the graph
symmetrize_graph(g);
split_edges(g);
return g;
}
int main() {
// EXAMPLE 1
std::cout << "Beginning example 1" << std::endl;
auto g = test_pr();
auto vertices = boost::vertices(g);
auto min_cut = compute_min_cut(g, 0, 5);
for (auto &it = vertices.first; it != vertices.second; it++) {
std::cout << *it << " " << g[*it].cut_class << std::endl;;
}
std::cout << " =========== " << std::endl;
auto edges = boost::edges(g);
for (auto &it = edges.first; it != edges.second; it++) {
std::cout << source(*it, g) << " " << target(*it, g) << " " << g[*it].flow << "/" << g[*it].capacity << std::endl;
}
std::cout << " =========== " << std::endl;
auto min_cut_boost = compute_min_cut_boost(g, 0, 5);
vertices = boost::vertices(g);
for (auto &it2 = vertices.first; it2 != vertices.second; it2++) {
std::cout << *it2 << " " << g[*it2].cut_class << std::endl;;
}
std::cout << " =========== " << std::endl;
std::cout << "Value of min cut : " << min_cut << std::endl;
std::cout << "Result from boost : " << min_cut_boost << std::endl;
std::cout << "Press key to continue ";
std::cin.get();
// EXAMPLE 2
std::cout << "Beginning example 2" << std::endl;
auto g2 = test_pr2();
auto vertices2 = boost::vertices(g2);
auto min_cut2 = compute_min_cut(g2, 0, 6);
for (auto &it = vertices2.first; it != vertices2.second; it++) {
std::cout << *it << " " << g2[*it].cut_class << std::endl;
}
std::cout << " =========== " << std::endl;
auto min_cut2_boost = compute_min_cut_boost(g2, 0, 6);
vertices2 = boost::vertices(g2);
for (auto &it = vertices2.first; it != vertices2.second; it++) {
std::cout << *it << " " << g2[*it].cut_class << std::endl;
}
std::cout << " =========== " << std::endl;
auto edges2 = boost::edges(g2);
for (auto &it = edges2.first; it != edges2.second; it++) {
std::cout << source(*it, g2) << " " << target(*it, g2) << " " << g2[*it].flow << "/" << g2[*it].capacity << std::endl;
}
std::cout << "Value of min cut : " << min_cut2 << std::endl;
std::cout << "Result from boost : " << min_cut2_boost << std::endl;
std::cout << "Press key to continue ";
std::cin.get();
// EXAMPLE 3
std::cout << "Beginning example 2" << std::endl;
auto g3 = test_pr3();
auto min_cut3 = compute_min_cut(g3, 0, 4);
auto min_cut3_boost = compute_min_cut_boost(g3, 0, 4);
auto edges3 = boost::edges(g3);
for (auto &it = edges3.first; it != edges3.second; it++) {
std::cout << source(*it, g3) << " " << target(*it, g3) << " " << g3[*it].flow << "/" << g3[*it].capacity << std::endl;
}
std::cout << "Value of min cut : " << min_cut3 << std::endl;
std::cout << "Result from boost : " << min_cut3_boost << std::endl;
return 0;
}