-
Notifications
You must be signed in to change notification settings - Fork 0
/
Flow_Network.h
356 lines (319 loc) · 10.1 KB
/
Flow_Network.h
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//
// Created by Ryan.Zurrin001 on 1/15/2022.
//
#ifndef FLOWNETWORK_FLOW_NETWORK_H
#define FLOWNETWORK_FLOW_NETWORK_H
#include "FlowEdge.h"
class Flow_Network {
public:
Flow_Network(int V);
Flow_Network(int V, int E);
Flow_Network(const Flow_Network& other);
Flow_Network& operator=(const Flow_Network& other);
~Flow_Network();
int V() const;
int E() const;
int flow() const;
void addEdge(FlowEdge* e);
void addEdge(int v, int w, double capacity);
void addEdge(int v, int w, double capacity, double cost);
bool validEdge(int v, int w) const;
// return vector iterator to the edges
vector<FlowEdge*>::iterator adj(int v);
vector<FlowEdge*>::iterator edges() const;
// get the edge
FlowEdge* edge(int v, int w) const;
double edgeCost(int v, int w) const;
// get val at the edge
// overload the << operator to print the graph
friend ostream& operator<<(ostream& os, const Flow_Network& g);
// overload the subscript operator to return the edge
FlowEdge& operator[](int v);
// overload the () operator to return the edge
FlowEdge& operator()(int v);
FlowEdge& operator()(int v, int w);
FlowEdge *edge(int i);
vector<FlowEdge*>* _adj;
double get_capacity(int &i);
void set_capacity(int &i, double d);
private:
int _V;
int _E;
};
Flow_Network::Flow_Network(int V) {
_V = V;
_E = 0;
_adj = new vector<FlowEdge*>[_V];
}
Flow_Network::Flow_Network(int V, int E) {
if (V < 0) {
throw std::invalid_argument("Number of vertices must be nonnegative");
}
if (E < 0) {
throw std::invalid_argument("Number of edges must be nonnegative");
}
_V = V;
_E = 0;
_adj = new vector<FlowEdge*>[_V];
// initialize a random flow network with V vertices and E edges. The capacities
// are integer values between 0 and 99 and the flow values are all 0.
srand(time(NULL));
for (int i = 0; i < E; i++) {
int v = rand() % V;
int w = rand() % V;
int c = rand() % 100;
while (v == w) {
w = rand() % V;
}
// check to see if the edge already exists
if (validEdge(v, w)) {
i--;
continue;
}
addEdge(v, w, c);
}
}
Flow_Network::Flow_Network(const Flow_Network &other) {
_V = other._V;
_E = other._E;
_adj = new vector<FlowEdge*>[_V];
for (int v = 0; v < _V; v++) {
for (FlowEdge* e : other._adj[v]) {
_adj[v].push_back(new FlowEdge(*e));
}
}
}
Flow_Network &Flow_Network::operator=(const Flow_Network &other) {
if (this != &other) {
_V = other._V;
_E = other._E;
_adj = new vector<FlowEdge*>[_V];
for (int v = 0; v < _V; v++) {
for (FlowEdge* e : other._adj[v]) {
_adj[v].push_back(new FlowEdge(*e));
}
}
}
return *this;
}
Flow_Network::~Flow_Network() {
for (int v = 0; v < _V; v++) {
for (FlowEdge* e : _adj[v]) {
delete e;
}
}
delete[] _adj;
}
int Flow_Network::V() const {
return _V;
}
int Flow_Network::flow() const {
int flow = 0;
for (int v = 0; v < _V; v++) {
for (FlowEdge* e : _adj[v]) {
flow += e->flow();
}
}
return flow;
}
int Flow_Network::E() const {
return _E;
}
void Flow_Network::addEdge(FlowEdge *e) {
int v = e->from();
int w = e->to();
if (v < 0 || v >= _V) {
cout << "vertex " + std::to_string(v) + " is not between 0 and " + std::to_string(_V - 1);
return;
}
if (w < 0 || w >= _V) {
cout << "vertex " + std::to_string(w) + " is not between 0 and " + std::to_string(_V - 1) << endl;
return;
}
if (v == w) {
cout << "vertex " + std::to_string(v) + " is equal to vertex " + std::to_string(w) + "\n";
return;
}
if (validEdge(v, w)) {
cout << "edge (" + std::to_string(v) + ", " + std::to_string(w) + ") is a duplicate" << endl;
return;
}
_adj[v].push_back(e);
_E++;
}
void Flow_Network::addEdge(int v, int w, double capacity) {
if (v < 0 || v >= _V) {
cout << "vertex " + std::to_string(v) + " is not between 0 and " + std::to_string(_V - 1);
return;
}
if (w < 0 || w >= _V) {
cout << "vertex " + std::to_string(w) + " is not between 0 and " + std::to_string(_V - 1) << endl;
return;
}
if (v == w) {
cout << "vertex " + std::to_string(v) + " is equal to vertex " + std::to_string(w) + "\n";
return;
}
if (validEdge(v, w)) {
cout << "edge (" + std::to_string(v) + ", " + std::to_string(w) + ") is a duplicate" << endl;
return;
}
_adj[v].push_back(new FlowEdge(v, w, capacity));
_E++;
}
void Flow_Network::addEdge(int v, int w, double capacity, double cost) {
if (v < 0 || v >= _V) {
cout << "vertex " + std::to_string(v) + " is not between 0 and " + std::to_string(_V - 1);
return;
}
if (w < 0 || w >= _V) {
cout << "vertex " + std::to_string(w) + " is not between 0 and " + std::to_string(_V - 1) << endl;
return;
}
if (v == w) {
cout << "vertex " + std::to_string(v) + " is equal to vertex " + std::to_string(w) + "\n";
return;
}
if (validEdge(v, w)) {
cout << "edge (" + std::to_string(v) + ", " + std::to_string(w) + ") is a duplicate" << endl;
return;
}
_adj[v].push_back(new FlowEdge(v, w, capacity, cost));
_E++;
}
vector<FlowEdge *>::iterator Flow_Network::adj(int v) {
if (v < 0 || v >= _V) {
throw std::invalid_argument("vertex " + std::to_string(v) + " is not between 0 and " + std::to_string(_V - 1));
}
return _adj[v].begin();
}
bool Flow_Network::validEdge(int v, int w) const {
for (FlowEdge* e : _adj[v]) {
if (e->to() == w) {
return true;
}
}
return false;
}
vector<FlowEdge *>::iterator Flow_Network::edges() const {
vector<FlowEdge *> edges;
for (int v = 0; v < _V; v++) {
for (FlowEdge* e : _adj[v]) {
edges.push_back(e);
}
}
return edges.begin();
}
ostream &operator<<(ostream &os, const Flow_Network &g) {
os << "Flow_Network(" << g.V() << " vertices, " << g.E() << " edges)" << endl;
for (int v = 0; v < g.V(); v++) {
os << " " << v << ": ";
for (FlowEdge* e : g._adj[v]) {
os << *e << " ";
}
os << endl;
}
return os;
}
FlowEdge &Flow_Network::operator[](int v) {
if (v < 0 || v >= _V) {
throw std::invalid_argument("vertex " + std::to_string(v) + " is not between 0 and " + std::to_string(_V - 1));
}
return *_adj[v][0];
}
FlowEdge &Flow_Network::operator()(int v) {
if (v < 0 || v >= _V) {
throw std::invalid_argument("vertex " + std::to_string(v) + " is not between 0 and " + std::to_string(_V - 1));
}
return *_adj[v][0];
}
FlowEdge &Flow_Network::operator()(int v, int w) {
if (v < 0 || v >= _V) {
throw std::invalid_argument("vertex " + std::to_string(v) + " is not between 0 and " + std::to_string(_V - 1));
}
if (w < 0 || w >= _V) {
throw std::invalid_argument("vertex " + std::to_string(w) + " is not between 0 and " + std::to_string(_V - 1));
}
for (FlowEdge* e : _adj[v]) {
if (e->to() == w) {
return *e;
}
}
throw std::invalid_argument("edge (" + std::to_string(v) + ", " + std::to_string(w) + ") is not in the graph");
}
FlowEdge *Flow_Network::edge(int v, int w) const {
if (v < 0 || v >= _V) {
throw std::invalid_argument("vertex " + std::to_string(v) + " is not between 0 and " + std::to_string(_V - 1));
}
if (w < 0 || w >= _V) {
throw std::invalid_argument("vertex " + std::to_string(w) + " is not between 0 and " + std::to_string(_V - 1));
}
for (FlowEdge* e : _adj[v]) {
if (e->to() == w) {
return e;
}
}
return nullptr;
}
FlowEdge *Flow_Network::edge(int i) {
if (i < 0 || i >= _E) {
throw std::invalid_argument("edge " + std::to_string(i) + " is not between 0 and " + std::to_string(_E - 1));
}
int count = 0;
for (int v = 0; v < _V; v++) {
for (FlowEdge* e : _adj[v]) {
if (count == i) {
return e;
}
count++;
}
}
throw std::invalid_argument("edge " + std::to_string(i) + " is not in the graph");
}
double Flow_Network::edgeCost(int v, int w) const {
if (v < 0 || v >= _V) {
throw std::invalid_argument("vertex " + std::to_string(v) + " is not between 0 and " + std::to_string(_V - 1));
}
if (w < 0 || w >= _V) {
throw std::invalid_argument("vertex " + std::to_string(w) + " is not between 0 and " + std::to_string(_V - 1));
}
for (FlowEdge* e : _adj[v]) {
if (e->to() == w) {
return e->capacity() - e->flow();
}
}
throw std::invalid_argument("edge (" + std::to_string(v) + ", " + std::to_string(w) + ") is not in the graph");
}
double Flow_Network::get_capacity(int &i) {
if (i < 0 || i >= _E) {
throw std::invalid_argument("edge " + std::to_string(i) + " is not between 0 and " + std::to_string(_E - 1));
}
int count = 0;
for (int v = 0; v < _V; v++) {
for (FlowEdge* e : _adj[v]) {
if (count == i) {
return e->residualCapacityTo(v);
}
count++;
}
}
throw std::invalid_argument("edge " + std::to_string(i) + " is not in the graph");
}
void Flow_Network::set_capacity(int &i, double d) {
// adds resodiial flow to the edge
if (i < 0 || i >= _E) {
throw std::invalid_argument("edge " + std::to_string(i) + " is not between 0 and " + std::to_string(_E - 1));
}
int count = 0;
for (int v = 0; v < _V; v++) {
for (FlowEdge* e : _adj[v]) {
if (count == i) {
e->addResidualFlowTo(v, d);
return;
}
count++;
}
}
throw std::invalid_argument("edge " + std::to_string(i) + " is not in the graph");
}
#endif //FLOWNETWORK_FLOW_NETWORK_H