-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.cpp
266 lines (222 loc) · 7.25 KB
/
Graph.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
#include "Graph.h"
Vertex * Graph::findVertex(const std::string &code) const {
auto iter = vertexMap.find(code);
if (iter != vertexMap.end()) {
return iter->second;
} else {
return nullptr;
}
}
bool Graph::addVertex(const Reservoir& reservoir) {
if (findVertex(reservoir.getCode()) != nullptr)
return false;
vertexMap.insert({reservoir.getCode(), new Vertex(reservoir, Type::RESERVOIR)});
return true;
}
// Implement addVertex function for PumpingStation
bool Graph::addVertex(const PumpingStation& pumpingStation) {
if (findVertex(pumpingStation.getCode()) != nullptr)
return false;
vertexMap.insert({pumpingStation.getCode(), new Vertex(pumpingStation, Type::PUMPING_STATION)});
return true;
}
// Implement addVertex function for City
bool Graph::addVertex(const City& city) {
if (findVertex(city.getCode()) != nullptr)
return false;
vertexMap.insert({city.getCode(), new Vertex(city, Type::CITY)});
return true;
}
bool Graph::addVertex(Vertex* vertex) {
if (!vertex || findVertex(vertex->getCode()) != nullptr)
return false;
vertexMap.insert({vertex->getCode(), vertex});
return true;
}
void deleteMatrix(int **m, int n) {
if (m != nullptr) {
for (int i = 0; i < n; i++)
if (m[i] != nullptr)
delete [] m[i];
delete [] m;
}
}
void deleteMatrix(double **m, int n) {
if (m != nullptr) {
for (int i = 0; i < n; i++)
if (m[i] != nullptr)
delete [] m[i];
delete [] m;
}
}
void Graph::removeVertex(Vertex* vertex) {
// Erase the vertex from the vertex map
auto it = vertexMap.find(vertex->getCode());
if (it != vertexMap.end()) {
vertexMap.erase(it);
}
// Remove edges connected to the vertex being removed from other vertices' adjacency lists
for (auto& [code, v] : vertexMap) {
// Get the list of adjacent vertices (neighbors) of the current vertex
auto adj = v->getAdj(); // Pass by value instead of by reference
// Remove the edges connected to the vertex being deleted from the adjacency list
adj.erase(std::remove_if(adj.begin(), adj.end(), [vertex](Edge* e) {
return e->getDest() == vertex || e->getOrig() == vertex;
}), adj.end());
// Assign the modified adjacency list back to the current vertex
v->setAdj(adj);
}
// Finally, delete the vertex object
delete vertex;
}
double Graph::edmondsKarp(Vertex* s, Vertex* t) {
if (s == nullptr || t == nullptr || s == t)
throw std::logic_error("Invalid source and/or target vertex");
for (const auto& v : vertexMap) {
for (auto e : v.second->getAdj()) {
e->setFlow(0);
}
}
double maxFlow = 0;
while (findAugmentingPath(s, t)) {
double f = findMinResidualAlongPath(s, t);
augmentFlowAlongPath(s, t, f);
maxFlow += f;
}
// Calculate and save incoming flow for each city vertex
for (const auto& [key, v] : vertexMap) {
if (v->getType() == Type::CITY) {
double incomingFlow = 0;
for (auto e : v->getIncoming()) {
incomingFlow += e->getFlow();
}
v->setFlow(incomingFlow); // Assuming Vertex class has a setFlow method
}
}
return maxFlow;
}
void Graph::testAndVisit(std::queue< Vertex*> &q, Edge *e, Vertex *w, double residual) {
if (! w->isVisited() && residual > 0) {
w->setVisited(true);
w->setPath(e);
q.push(w);
}
}
bool Graph::findAugmentingPath(Vertex *s, Vertex *t) {
for(auto v : vertexMap) {
v.second->setVisited(false);
}
s->setVisited(true);
std::queue<Vertex *> q;
q.push(s);
while( ! q.empty() && ! t->isVisited()) {
auto v = q.front();
q.pop();
for(auto e: v->getAdj()) {
testAndVisit(q, e, e->getDest(), e->getCapacity() - e->getFlow());
}
for(auto e: v->getIncoming()) {
testAndVisit(q, e, e->getOrig(), e->getFlow());
}
}
return t->isVisited();
}
double Graph::findMinResidualAlongPath(Vertex *s, Vertex *t) {
double f = INF;
for (auto v = t; v != s; ) {
auto e = v->getPath();
if (e->getDest() == v) {
f = std::min(f, static_cast<double>(e->getCapacity() - e->getFlow()));
v = e->getOrig();
}
else {
f = std::min(f, static_cast<double>(e->getFlow()));
v = e->getDest();
}
}
return f;
}
void Graph::augmentFlowAlongPath(Vertex *s, Vertex *t, double f) {
for (auto v = t; v != s; ) {
auto e = v->getPath();
double flow = e->getFlow();
if (e->getDest() == v) {
e->setFlow(flow + f);
v = e->getOrig();
}
else {
e->setFlow(flow - f);
v = e->getDest();
}
}
}
double Graph::getMaxFlowToCity(const std::string& cityCode) {
Vertex* cityVertex = findVertex(cityCode);
if (cityVertex) {
return cityVertex->getFlow(); // Assuming getFlow returns the max flow for the city
} else {
throw std::invalid_argument("City not found in the graph");
}
}
void Graph::resetFlows(Graph& graph) {
for (auto& [_, vertex] : graph.vertexMap) {
for (auto& edge : vertex->getAdj()) {
edge->setFlow(0);
}
}
}
bool Graph::bfs(Vertex *source, Vertex* sink) const {
for (auto& vertex : vertexMap) {
Vertex* v = vertex.second;
v->setVisited(false);
v->setPath(nullptr);
}
std::queue<Vertex*> queue;
source->setVisited(true);
queue.push(source);
while (!queue.empty()) {
Vertex* v = queue.front();
queue.pop();
for (Edge* e : v->getAdj()) {
Vertex* w = e->getDest();
if (!w->isVisited() && e->getWeight() > 0) {
w->setVisited(true);
w->setPath(e);
if (w == sink) {
return true;
}
queue.push(w);
}
}
}
return false;
}
void Graph::updateFlow(Vertex* sink, int bottleneck) {
for (Vertex *v = sink; v->getPath() != nullptr; v = v->getPath()->getOrig()) {
Edge *e = v->getPath();
e->setWeight(e->getWeight() - bottleneck);
Edge *reverse = e->getReverse();
if (reverse != nullptr) {
reverse->setWeight(reverse->getWeight() + bottleneck);
}
}
}
Graph::~Graph() {
deleteMatrix(distMatrix, (int) vertexMap.size());
deleteMatrix(pathMatrix, (int) vertexMap.size());
}
const unordered_map<std::string, Vertex *> & Graph::getVertexMap() const {
return this->vertexMap;
}
void Graph::printGraph(const Graph& graph) {
const auto& vertexMap = graph.getVertexMap();
for (const auto& entry : vertexMap) {
const std::string& vertexName = entry.first;
const Vertex* vertex = entry.second;
std::cout << "Vertex: " << vertexName << ", Adj Size: " << vertex->getIncoming().size() << std::endl;
const auto& edges = vertex->getAdj();
for (const auto& edge : edges) {
std::cout << " Edge to: " << edge->getDest()->getCode() << ", Weight: " << edge->getWeight() << std::endl;
}
}
}