-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlowNetwork.h
241 lines (215 loc) · 6.7 KB
/
FlowNetwork.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
//
// Created by Ryan.Zurrin001 on 1/13/2022.
//
#ifndef GRAPH_CPP_FLOWNETWORK_H
#define GRAPH_CPP_FLOWNETWORK_H
#include "FlowEdge.h"
#include "Bag.h"
class FlowNetwork {
int _V;
int _E;
int _source;
int _sink;
public:
Bag<FlowEdge*> *_adj;
FlowNetwork(int V);
FlowNetwork(int V, int E, int source, int sink);
FlowNetwork(istream &in);
~FlowNetwork();
void validateVertex(int v) const;
void addEdge(FlowEdge* e);
void addEdge(int v, int w, double capacity);
void removeEdge(FlowEdge *pEdge);
bool containsEdge(FlowEdge *pEdge);
int V() const;
int E() const;
int source() const { return _source; }
int sink() const { return _sink; }
void setSource(int source) { _source = source; }
void setSink(int sink) { _sink = sink; }
Bag<FlowEdge*>::Iterator adj(int v);
Bag<FlowEdge*>::Iterator edges();
string toString() const;
friend ostream &operator<<(ostream &os, const FlowNetwork &G);
// inner class for iterating over the edges
class FlowNetworkIterator : public Bag<FlowEdge>::Iterator {
public:
FlowNetworkIterator(Bag<FlowEdge>::Iterator *it) : Bag<FlowEdge>::Iterator(
reinterpret_cast<Node<struct FlowEdge> *>(it)) {}
FlowNetworkIterator(const FlowNetworkIterator &it) : Bag<FlowEdge>::Iterator(it) {}
FlowNetworkIterator &operator=(const FlowNetworkIterator &it) {
Bag<FlowEdge>::Iterator::operator=(it);
return *this;
}
FlowNetworkIterator &operator++() {
Bag<FlowEdge>::Iterator::operator++();
return *this;
}
FlowNetworkIterator operator++(int) {
FlowNetworkIterator it = *this;
operator++();
return it;
}
};
};
FlowNetwork::FlowNetwork(int V) {
if (V < 0) throw "Number of vertices must be nonnegative";
_V = V;
_E = 0;
_adj = new Bag<FlowEdge*>[V];
for (int v = 0; v < V; v++) {
_adj[v] = Bag<FlowEdge*>();
}
}
FlowNetwork::FlowNetwork(int V, int E, int source, int sink) {
if (V < 0) throw "Number of vertices must be nonnegative";
if (E < 0) throw "Number of edges must be nonnegative";
if (source < 0 || source >= V) throw "Invalid source";
if (sink < 0 || sink >= V) throw "Invalid sink";
_V = V;
_E = 0;
_source = source;
_sink = sink;
_adj = new Bag<FlowEdge*>[V];
for (int v = 0; v < V; v++) {
_adj[v] = Bag<FlowEdge*>();
}
// initialize a random flow network with V vertices and E edges. The
// capacities are between 0 and 99 and the flow values are 0;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> vert(0, V - 1);
std::uniform_int_distribution<> dis(0, 99); // for capacities
for (int i = 0; i < E; i++) {
// randomly generate the vertices from 0 to V-1
int v = vert(gen);
int w = vert(gen);
int c = dis(gen);
if (v == w) { // no self loops
i--;
continue;
}
while (v == _sink) v = vert(gen); // no output edges from the sink
while (w == _source) w = vert(gen); // no input edges to the source
FlowEdge *e = new FlowEdge(v, w, c);
if (containsEdge(e)) {
delete e;
i--;
continue;
} else {
addEdge(e);
}
}
}
FlowNetwork::FlowNetwork(istream &in) {
in >> _V;
if (_V < 0) throw "Number of vertices must be nonnegative";
in >> _E;
if (_E < 0) throw "Number of edges must be nonnegative";
_adj = new Bag<FlowEdge*>[V()];
for (int v = 0; v < V(); v++) {
_adj[v] = Bag<FlowEdge*>();
}
for (int i = 0; i < E(); i++) {
int v, w;
double c;
in >> v >> w >> c;
if (v < 0 || v >= V()) throw "vertex " + to_string(v) + " is not between 0 and " + to_string(V() - 1);
if (w < 0 || w >= V()) throw "vertex " + to_string(w) + " is not between 0 and " + to_string(V() - 1);
addEdge(new FlowEdge(v, w, c));
}
}
FlowNetwork::~FlowNetwork() {
unordered_set<FlowEdge*> edges;
// before deleting the edge add to a set to avoid duplicate deletion
for (int v = 0; v < V(); v++) {
for (int i = 0; i < _adj[v].size(); i++) {
edges.insert(_adj[v].get(i));
}
}
for (auto e : edges) {
if (e != nullptr) delete e;
}
delete[] _adj;
}
void FlowNetwork::validateVertex(int v) const {
if (v < 0 || v >= V()) throw "vertex " + to_string(v) +
" is not between 0 and " + to_string(V() - 1);
}
void FlowNetwork::addEdge(FlowEdge* e) {
int v = e->from();
int w = e->to();
validateVertex(v);
validateVertex(w);
_adj[v].add(e);
_adj[w].add(e);
_E++;
}
int FlowNetwork::V() const {
return _V;
}
int FlowNetwork::E() const {
return _E;
}
Bag<FlowEdge*>::Iterator FlowNetwork::adj(int v) {
validateVertex(v);
return _adj[v].begin();
}
Bag<FlowEdge*>::Iterator FlowNetwork::edges() {
Bag<FlowEdge*> bag;
for (int v = 0; v < V(); v++) {
for (FlowEdge* e : _adj[v]) {
if (e->from() != v) bag.add(e);
}
}
return bag.begin();
}
string FlowNetwork::toString() const {
stringstream ss;
ss << V() << " " << E() << endl;
for (int v = 0; v < V(); v++) {
ss << " " << v << ": ";
for (Bag<FlowEdge*>::Iterator it = _adj[v].begin(); it != _adj[v].end(); ++it) {
if ((*it)->to() != v) ss << *(*it) << " ";
}
ss << endl;
}
return ss.str();
}
ostream &operator<<(ostream &os, const FlowNetwork &G) {
os << G.toString();
return os;
}
void FlowNetwork::removeEdge(FlowEdge *pEdge) {
// remove all occurrences of pEdge from _adj
int v = pEdge->from();
int w = pEdge->to();
_adj[v].remove(pEdge);
_adj[w].remove(pEdge);
delete pEdge;
_E--;
}
bool FlowNetwork::containsEdge(FlowEdge *pEdge) {
// remove all occurrences of pEdge from _adj
int v = pEdge->from();
int w = pEdge->to();
for (Bag<FlowEdge*>::Iterator it = _adj[v].begin(); it != _adj[v].end(); it++) {
// check that the to and from vertices are the same
if ((*it)->to() == w && (*it)->from() == v) {
return true;
}
}
return false;
}
void FlowNetwork::addEdge(int v, int w, double capacity) {
if (v < 0 || v >= V()) throw "vertex " + to_string(v) + " is not between 0 and " + to_string(V() - 1);
if (w < 0 || w >= V()) throw "vertex " + to_string(w) + " is not between 0 and " + to_string(V() - 1);
FlowEdge *e = new FlowEdge(v, w, capacity);
if (containsEdge(e)) {
delete e;
return;
} else {
addEdge(e);
}
}
#endif //GRAPH_CPP_FLOWNETWORK_H