-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_graph.cpp
More file actions
232 lines (222 loc) · 8.93 KB
/
base_graph.cpp
File metadata and controls
232 lines (222 loc) · 8.93 KB
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
//
// Created by andrea on 09/09/18.
//
#include <iostream>
#include "base_graph.h"
/**
* This constructor generate takes as input the size of the graph and creates an internal structure
* keeping a reference for each vertex and an incident matrix to know which vertex are connected.
* @param vertices_count size of the graph.
*/
BaseGraph::BaseGraph(int vertices_count) {
this->edge_matrix = new Edge**[vertices_count];
for (int i = 0; i < vertices_count; i++) {
this->vertices.emplace_back(DEFAULT_LABEL, DEFAULT_EXCESS);
this->edge_matrix[i] = new Edge*[vertices_count];
}
// initialize pointers to null
for (int i = 0; i < this->vertices.size(); i++) {
for (int j = 0; j < this->vertices.size(); j++) {
this->edge_matrix[i][j] = nullptr;
}
}
// initialize debug as disabled
this->verbose = false;
}
BaseGraph::~BaseGraph() {
for (int i = 0; i < this->vertices.size(); i++) {
for (int j = 0; j < this->vertices.size(); j++) {
delete this->edge_matrix[i][j];
}
delete this->edge_matrix[i];
}
delete this->edge_matrix;
}
void BaseGraph::printCurrentStatus() {
// print debug info if verbose is enabled
if (this->verbose) {
std::cout << "============================================================================ " << std::endl;
for (int i = 0; i < vertices.size(); i++) {
std::cout << " [vertex " << i << "] => label: " << vertices[i].label << ", excess: " << vertices[i].excess << std::endl;
}
std::cout << " [incidence-matrix]" << std::endl;
for (int i = 0; i < vertices.size(); i++) {
std::cout << " | ";
for (int j = 0; j < vertices.size(); j++) {
std::cout << (this->edge_matrix[i][j] != nullptr ? "1" : "0") << " | ";
}
std::cout << std::endl;
}
std::cout << " [flow-matrix]" << std::endl;
for (int i = 0; i < vertices.size(); i++) {
std::cout << " | ";
for (int j = 0; j < vertices.size(); j++) {
Edge* edge = this->edge_matrix[i][j];
int flow = edge != nullptr ? edge->flow : 0;
if (flow >= 0) {
if (flow <= 9) {
std::cout << " " << flow << " |";
} else {
std::cout << " " << flow << " |";
}
} else {
if (flow >= -9) {
std::cout << " " << flow << " |";
} else {
std::cout << flow << " |";
}
}
}
std::cout << std::endl;
}
std::cout << " [capacity-matrix]" << std::endl;
for (int i = 0; i < vertices.size(); i++) {
std::cout << " | ";
for (int j = 0; j < vertices.size(); j++) {
Edge* edge = this->edge_matrix[i][j];
int capacity = edge != nullptr ? edge->capacity : 0;
if (capacity >= 0) {
if (capacity <= 9) {
std::cout << " " << capacity << " |";
} else {
std::cout << " " << capacity << " |";
}
} else {
if (capacity >= -9) {
std::cout << " " << capacity << " |";
} else {
std::cout << capacity << " |";
}
}
}
std::cout << std::endl;
}
}
}
void BaseGraph::setVerbose(bool verbose) {
this->verbose = verbose;
}
void BaseGraph::preProcess(int s, int t) {
// the label of the source vertex is set to the number of vertices
// the label of all the other vertices (different from s) is set to 0
for (int i = 0; i < this->vertices.size(); i++) {
this->vertices[i].label = (i != s) ? 0 : (int) vertices.size();
}
// for each edge that goes from source to another vertex, the flow is set equal
// to the capacity and the excess of the destination vertex is set to the capacity
for (int i = 0; i < this->vertices.size(); i++) {
Edge* edge = this->edge_matrix[s][i];
if (edge != nullptr) {
edge->flow = edge->capacity;
this->vertices[i].excess = edge->capacity;
// the reverse link must be set to the inverse flow
this->edge_matrix[i][s]->flow = -edge->flow;
}
}
}
int BaseGraph::getActiveNode(int s, int t) {
// this method must be differently implemented in each variation of
// the graph: this basic implementation will not return any vertex.
return NO_ACTIVE_NODE_FOUND;
}
/**
* Try to push the overflowing flow from node u to all the connected nodes.
* @param u index of the node that have overflowing flow to push.
* @return if at least one admissible arc exists.
*/
bool BaseGraph::pushFlow(int u) {
// iterate all the adjacent nodes
for (int i = 0; i < this->vertices.size(); i++) {
Edge* edge = this->edge_matrix[u][i];
if (edge != nullptr) {
// vertex i is adjacent to vertex u
if (edge->flow < edge->capacity) {
// if flow is equal to capacity the push cannot be done
if (this->vertices[u].label > this->vertices[i].label) {
// the push can be done only if the label of the overflowing
// vertex if higher than the label of the adjacent one.
// the amount of flow that can be sent is equal to the minimum
// between the remaining flow on edge and excess flow.
int flow = std::min(edge->capacity - edge->flow, this->vertices[u].excess);
// this flow can be sent from vertex u to vertex i.
// the excessive flow in node u must be reduced.
this->vertices[u].excess -= flow;
// the incoming flow in node i must be added.
this->vertices[i].excess += flow;
// the flow on edge u->i must be increased.
edge->flow += flow;
// also the reverse edge must be updated.
this->edge_matrix[i][u]->flow -= flow;
// one arc has been found and some flow has been pushed
// so we can exit the cycle and return success.
if (this->verbose) {
std::cout << "=> Flow moved from " << u << " to " << i << " by " << flow << " units" << std::endl;
}
return true;
}
}
}
}
// if no push has been done, return false
return false;
}
bool BaseGraph::relabel(int u) {
// we have to look for the minimum label between the adjacent nodes
int minimum = -1;
// do the iteration
for (int i = 0; i < this->vertices.size(); i++) {
Edge *edge = this->edge_matrix[u][i];
if (edge != nullptr) {
// ensure that this edge is not full
if (edge->flow < edge->capacity) {
// obtain the label for this adjacent node
int label = this->vertices[i].label;
if (minimum == -1 || label < minimum) {
// the label of the adjacent node is the minimum
minimum = label;
this->vertices[u].label = label + 1;
}
}
}
}
// return if vertex has been relabeled
return minimum != -1;
}
void BaseGraph::addEdge(int u, int v, int capacity) {
this->edge_matrix[u][v] = new Edge(DEFAULT_FLOW, capacity);
this->edge_matrix[v][u] = new Edge(-DEFAULT_FLOW, 0);
}
int BaseGraph::getMaximumFlow(int s, int t) {
// the algorithm start pre-processing input data
preProcess(s, t);
// print current status after pre-processing
printCurrentStatus();
// enter the main cycle
int cycles = 0;
int activeNode = getActiveNode(s, t);
while (activeNode != NO_ACTIVE_NODE_FOUND) {
// an active node has been found
if (this->verbose) {
std::cout << "=> Current active node: " << activeNode << std::endl;
}
// try to push some flow in adjacent nodes
if (!pushFlow(activeNode)) {
// no admissible arc found
if (this->verbose) {
std::cout << "=> No admissible arc found, relabeling node " << activeNode << std::endl;
}
// relabel the node
relabel(activeNode);
}
// print current status after pre-processing
printCurrentStatus();
// check for another active node
activeNode = getActiveNode(s, t);
cycles += 1;
}
if (this->verbose) {
std::cout << "=> Cycles count: " << cycles << std::endl;
}
// no more active node found, return the maximum flow
return this->vertices[t].excess;
}