-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
339 lines (239 loc) · 9.74 KB
/
main.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <iostream>
#include <math.h>
#include <boost/format.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/grid_graph.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/graph/reverse_graph.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/graph/push_relabel_max_flow.hpp>
#include <LEDA/core/string.h>
#include <LEDA/system/misc.h>
//#include <LEDA/graph/max_flow.h>
using namespace boost;
using namespace std;
//Type & Struct Definitions ___________________
// Bundled properties for graph
struct bundleVertex {
int id;
int excess;
int distance;
};
struct bundleEdge {
int residual;
};
struct bundleGraph;
// Define graph type
typedef adjacency_list<vecS, vecS, bidirectionalS, bundleVertex, bundleEdge, bundleGraph> Graph;
//Define graph traits type
typedef graph_traits<Graph> Traits;
//Define descriptor for vertices
typedef Traits::vertex_descriptor Vertex;
//Define descriptor for edges
typedef Traits::edge_descriptor Edge;
struct bundleGraph {
Vertex source;
Vertex sink;
};
// Function Declarations ___________________
// Create example graph used for visualising
Graph createExampleGraph();
// Print results to terminal
void printResults(Graph &G);
// Create graph visualisation dot language file
void visualise(Graph &G);
//Excess Scale functions -------
void ExcessScaleMaxFlow(Graph &G, Vertex source, Vertex sink);
// Handle preprocessing operations
void preprocess(Graph &G, Vertex source, Vertex sink);
// Calculate initial delta = 2 ^ log(maxCapacity)
float calcInitialDelta(Graph &G);
// Utility function for selecting node with smallest distance among nodes with large excess
Vertex selectNode(Graph &G, Vertex source, Vertex sink, float delta);
// implements push of flow or relabel of distance of current node
void pushRelabel(Graph &G, Vertex current, float delta);
// Custom Property Writer for Graphviz
template <class IdMap, class ExcessMap, class DistanceMap>
class vertex_writer {
public:
vertex_writer(IdMap i, ExcessMap e, DistanceMap d) : im(i), em(e), dm(d) {}
template <class Vertex>
void operator()(ostream &out, const Vertex& v) const {
out << "[label=\"ID:" << im[v] << "\nExcess: "<< em[v] <<"\nDistance: "<< dm[v] << "\"]";
}
private:
IdMap im;
ExcessMap em;
DistanceMap dm;
};
// Function for passing in the maps to custom property writer
template <class IdMap, class ExcessMap, class DistanceMap>
inline vertex_writer<IdMap, ExcessMap, DistanceMap>
make_vertex_writer(IdMap i, ExcessMap e, DistanceMap d) {
return vertex_writer<IdMap, ExcessMap, DistanceMap>(i, e, d);
}
int main() {
Graph exampleG = createExampleGraph();
Vertex source = exampleG[graph_bundle].source;
Vertex sink = exampleG[graph_bundle].sink;
/*Algorithm exectution and timing*/
cout<<"\t\tEXCESS SCALE MAX FLOW ALGORITHM"<<endl;
cout << "Case:\tExample Graph"<<endl;
float time = leda::used_time();
ExcessScaleMaxFlow(exampleG, source, sink);
cout << "\nImplementation Time: " << leda::used_time(time) << endl;
//MAX_FLOW_T(const graph& G, node s, node t, const edge_array< NT> & cap)
//cout << "BGL Time: " << leda::used_time(time) << endl<<endl;
visualise(exampleG); // dot -Tpng ./cmake-build-debug/graph.dot > exampleFinal.png
return 0;
}
// Function Definitions ___________________
Graph createExampleGraph() {
// Graph instantiation
Graph G;
// Graph creation
Vertex v1 = add_vertex(bundleVertex{1, 0, 2}, G);
Vertex v2 = add_vertex(bundleVertex{2, 0, 1}, G);
Vertex v3 = add_vertex(bundleVertex{3, 0, 1}, G);
Vertex v4 = add_vertex(bundleVertex{4, 0, 0}, G);
add_edge(v1, v2, bundleEdge{2}, G);
add_edge(v1, v3, bundleEdge{4}, G);
add_edge(v2, v3, bundleEdge{3}, G);
add_edge(v2, v4, bundleEdge{1}, G);
add_edge(v3, v4, bundleEdge{5}, G);
G[graph_bundle].source = v1;
G[graph_bundle].sink = v4;
return G;
}
void printResults(Graph &G) {
print_graph(G, get(&bundleVertex::id, G));
Graph::vertex_iterator vertexIt, vertexEnd;
tie(vertexIt, vertexEnd) = vertices(G);
cout << endl << endl;
for (; vertexIt != vertexEnd; ++vertexIt) {
cout << "excess of node "<< G[*vertexIt].id << " : " << G[*vertexIt].excess << endl;
}
//TODO: MAX FLOW IS EXCESS VALUE AT SINK...handle that
}
void visualise(Graph &G) {
ofstream dot("graph.dot");
write_graphviz(dot, G, make_vertex_writer(get(&bundleVertex::id, G),
get(&bundleVertex::excess, G),
get(&bundleVertex::distance, G)),
make_label_writer(get(&bundleEdge::residual, G)));
}
void ExcessScaleMaxFlow(Graph &G, Vertex source, Vertex sink) {
// Preprocessing
preprocess(G, source, sink);
// Calculate initial delta = 2 ^ log(maxCapacity)
float delta = calcInitialDelta(G);
// Core update loop
while(delta >= 1) {
// select node with smallest distance among nodes with large excess
Vertex currentNode = selectNode(G, source, sink, delta);
while(currentNode != 0) {
// push flow or relabel distance of current node
pushRelabel(G, currentNode, delta);
// select another node from updated graph
currentNode = selectNode(G, source, sink, delta);
}
delta /= 2; // update delta if no other selectable node
}
}
void preprocess(Graph &G, Vertex source, Vertex sink) {
// Store initial distances in this vector
std::vector<int> distances(num_vertices(G));
// Computation of exact distance labels via backward BFS from sink
reverse_graph<Graph> R = make_reverse_graph(G);
breadth_first_search(R,
sink,
visitor(make_bfs_visitor(record_distances(make_iterator_property_map(distances.begin(),
get(vertex_index, G)),
on_tree_edge()))));
Graph::vertex_iterator vertexIt, vertexEnd;
tie(vertexIt, vertexEnd) = vertices(G);
for (; vertexIt != vertexEnd; ++vertexIt) {
G[*vertexIt].distance = distances[*vertexIt];
}
// Saturate source's adjacent edges
Graph::out_edge_iterator outedgeIt, outedgeEnd;
tie(outedgeIt, outedgeEnd) = out_edges(source, G);
for(; outedgeIt != outedgeEnd; ++outedgeIt) {
Vertex src = boost::source(*outedgeIt, G);
Vertex target = boost::target(*outedgeIt, G);
int saturation = G[*outedgeIt].residual;
add_edge(target, src, bundleEdge{saturation}, G); // add reverse of saturated edge
remove_edge(*outedgeIt, G); // remove saturated edge
G[target].excess += saturation; // update target node's excess property
}
// Update source node's distance label to num_vertices
G[source].distance = (int) num_vertices(G);
}
float calcInitialDelta(Graph &G) {
std::vector<int> capacities(num_edges(G));
Graph::edge_iterator edgeIt, edgeEnd;
tie(edgeIt, edgeEnd) = edges(G);
for (; edgeIt!= edgeEnd; ++edgeIt) {
capacities.push_back(G[*edgeIt].residual);
}
int maxCap = *max_element(capacities.begin(), capacities.end());
return (float) pow(2, ceil(log2(maxCap)));
}
Vertex selectNode(Graph &G, Vertex source, Vertex sink, float delta) {
double min = std::numeric_limits<double>::infinity();
Vertex minNode = 0;
Graph::vertex_iterator vertexIt, vertexEnd;
tie(vertexIt, vertexEnd) = vertices(G);
for (; vertexIt != vertexEnd; ++vertexIt) {
// if node has large excess (cannot be source or sink)
if(G[*vertexIt].excess >= delta/2 && *vertexIt != source && *vertexIt != sink) {
if(G[*vertexIt].excess < min) {
min = G[*vertexIt].excess;
minNode = *vertexIt;
}
}
}
//return node with min excess amongst nodes with large excess
return minNode;
}
void pushRelabel(Graph &G, Vertex current, float delta) {
// stores candidate distances of adj nodes for raising current node's distance label to
std::vector<int> adjDist;
Graph::out_edge_iterator outedgeIt, outedgeEnd;
tie(outedgeIt, outedgeEnd) = out_edges(current, G);
for(; outedgeIt != outedgeEnd; ++outedgeIt) {
Vertex target = boost::target(*outedgeIt, G);
// if network contains an admissable arc
if(G[current].distance == G[target].distance + 1) {
int pushFlow = 0;
if(delta - G[target].excess > 0) {
pushFlow = std::min({G[current].excess, G[*outedgeIt].residual, (int) delta - G[target].excess}); // last arg ensures that excess doesnt exceed delta
} else {
pushFlow = std::min(G[current].excess, G[*outedgeIt].residual);
}
// implement Push
G[current].excess -= pushFlow;
G[target].excess += pushFlow;
G[*outedgeIt].residual -= pushFlow;
if(G[*outedgeIt].residual == 0) // saturation
remove_edge(*outedgeIt, G); // remove saturated edge
bool found;
Edge reverse;
boost::tie(reverse, found) = edge(target, current, G);
if(found) {
G[reverse].residual += pushFlow;
} else {
add_edge(target, current, bundleEdge{pushFlow}, G); // add reverse edge
}
return;
} else {
if(G[*outedgeIt].residual > 0)
adjDist.push_back(G[target].distance + 1);
}
}
// implement Relabel
int minDist = *min_element(adjDist.begin(), adjDist.end());
G[current].distance = minDist;
return;
}