-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph.h
413 lines (408 loc) · 15.2 KB
/
graph.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#pragma once
#include <condition_variable>
#include <vector>
#include <stack>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <thread>
namespace gns {
const size_t UNDEFINED = std::numeric_limits<size_t>::max();
const size_t INF = std::numeric_limits<size_t>::max();
struct UndirectedEdge {
size_t from, to, weight;
UndirectedEdge(size_t from, size_t to, size_t weight): from(from), to(to), weight(weight) {}
UndirectedEdge() {}
};
struct Edge {
size_t to, weight;
Edge(size_t to, size_t weight): to(to), weight(weight) {}
explicit Edge(const UndirectedEdge &edge): to(edge.to), weight(edge.weight) {}
Edge(): to(0), weight(0) {}
};
typedef std::vector < UndirectedEdge > EdgesList;
typedef std::vector < std::vector < Edge > > AdjencyList;
class Graph {
AdjencyList adjacencyList;
public:
explicit Graph(size_t vertexNumber): adjacencyList(vertexNumber) {}
Graph() {}
void addEdge(const UndirectedEdge &edge) {
adjacencyList[edge.from].push_back(Edge(edge));
}
void addEdge(size_t from, size_t to, size_t weight) {
adjacencyList[from].push_back(Edge(to, weight));
}
std::vector < Edge > &operator[] (size_t vertexNumber){
return adjacencyList[vertexNumber];
}
const std::vector < Edge > &operator[] (size_t vertexNumber) const{
return adjacencyList[vertexNumber];
}
void resize(size_t size) {
adjacencyList.resize(size);
}
size_t size() const {
return adjacencyList.size();
}
};
void merge(Graph &updatedGraph, const Graph &addedGraph) {
updatedGraph.resize(std::max(updatedGraph.size(),
addedGraph.size()));
for(size_t k = 0; k < addedGraph.size(); k++)
updatedGraph[k].insert(updatedGraph[k].end(),
addedGraph[k].begin(), addedGraph[k].end());
}
Graph invert(const Graph &graph) {
Graph invertedGraph(graph.size());
for(size_t i = 0; i < graph.size(); i++)
for(size_t j = 0; j < graph[i].size(); j++)
invertedGraph.addEdge(graph[i][j].to, i, graph[i][j].weight);
return invertedGraph;
}
namespace prns {
template < class EnterFunc, class VisitFunc, class LeaveFunc >
void DFS(const Graph &graph, size_t root, EnterFunc onEnter, VisitFunc goByTheEdge, LeaveFunc onLeave) {
if(onEnter(root))
return;
for(size_t i = 0; i < graph[root].size(); i++) {
size_t to = graph[root][i].to;
size_t weight = graph[root][i].weight;
if(goByTheEdge(UndirectedEdge(root, to, weight)))
DFS(graph, to, onEnter, goByTheEdge, onLeave);
}
onLeave(root);
}
void componentTreeSearch(const Graph &graph, std::vector < bool > &used,
Graph &tree, size_t vertex, const std::vector < size_t > &colors, size_t color) {
DFS(graph, vertex, [&used, &colors, color] (size_t vertex) {
if(used[vertex] || colors[vertex] != color)
return true;
used[vertex] = true;
return false;
}, [&used, &colors, &tree, color](UndirectedEdge edge) {
if(!used[edge.to] && colors[edge.to] == color) {
tree.addEdge(edge);
return true;
}
return false;
}, [](size_t vertex) {} );
}
Graph subGraphWithZeroEdges(const Graph &graph) {
Graph ans(graph.size());
for(size_t i = 0; i < graph.size(); i++)
for(size_t j = 0; j < graph[i].size(); j++)
if(graph[i][j].weight == 0)
ans[i].push_back(graph[i][j]);
return ans;
}
void getDFS_vertexList(const Graph &graph, std::stack < size_t > &out,
std::vector<bool> &used, size_t vertex = 0) {
DFS(graph, vertex, [&used](size_t vertex) {
if(used[vertex])
return true;
used[vertex] = true;
return false;
}, [](UndirectedEdge edge) { return true; },
[&out](size_t vertex) {
out.push(vertex);
});
}
void paint(const Graph &graph, std::vector <size_t> &colors, size_t color, size_t vertex) {
DFS(graph, vertex, [&colors, color](size_t vertex) {
if(colors[vertex] != UNDEFINED)
return true;
colors[vertex] = color;
return false;
}, [](UndirectedEdge edje) { return true; }, [](size_t vertex){});
}
void restruct(Graph &graph, std::vector < size_t > &edgeMinWeight) {
size_t size = graph.size();
edgeMinWeight.assign(size, INF);
for(size_t i = 0; i < graph.size(); ++i)
for(size_t j = 0; j < graph[i].size(); j++)
edgeMinWeight[graph[i][j].to] = std::min(edgeMinWeight[graph[i][j].to], graph[i][j].weight);
for(size_t i = 0; i < graph.size(); ++i)
for(size_t j = 0; j < graph[i].size(); j++)
graph[i][j].weight -= edgeMinWeight[graph[i][j].to];
}
void restoreFromRestruct(Graph &graph, const std::vector < size_t > &edgeMinWeight) {
for(size_t i = 0; i < graph.size(); ++i)
for(size_t j = 0; j < graph[i].size(); j++)
graph[i][j].weight += edgeMinWeight[graph[i][j].to];
}
void getTree(const Graph &graph, std::vector < bool > &used, Graph &tree, size_t root) {
DFS(graph, root, [&used](size_t vertex) {
if(used[vertex])
return true;
used[vertex] = true;
return false;
}, [&used, &tree](UndirectedEdge edge) {
if(edge.weight == 0 && !used[edge.to]) {
tree.addEdge(edge);
return true;
}
return false;
}, [](size_t vertex) {});
}
void buildMSTfromCondence(Graph &cond, Graph &tree, Graph &zero, Graph &ans,
std::vector < EdgesList > &mainEdges, std::vector < size_t > colors, size_t color, size_t root) {
size_t size = zero.size();
std::vector<bool> isExist(color, false);
std::vector<bool> used(size, false);
for(size_t i = 0; i < color; i++) {
for(size_t j = 0; j < tree[i].size(); j++)
isExist[tree[i][j].to] = true;
for(size_t j = 0; j < cond[i].size(); j++) {
size_t to = cond[i][j].to;
if(isExist[to]) {
isExist[to] = false;
UndirectedEdge edge = mainEdges[i][j];
ans.addEdge(edge);
Graph compTree(size);
componentTreeSearch(zero, used, ans, edge.to, colors, colors[edge.to]);
}
}
}
Graph compTree(size);
componentTreeSearch(zero, used, ans, root, colors, colors[root]);
}
void threadFord_Bellman(std::pair<size_t, size_t> arg1,
std::pair<AdjencyList *, std::vector<size_t> *> arg2,
std::pair<size_t *, std::condition_variable *> arg3,
std::pair<size_t, std::mutex *> arg4) {
size_t begin = arg1.first;
size_t end = arg1.second;
AdjencyList *adjacencyList = arg2.first;
std::vector < size_t > *len = arg2.second;
size_t *finished = arg3.first;
std::condition_variable *condition = arg3.second;
size_t threadsAmount = arg4.first;
std::mutex *finishedMutex = arg4.second;
for(size_t k = 0; k < adjacencyList->size(); k++) {
for(size_t i = begin; i < end; i++)
for(size_t j = 0; j < (*adjacencyList)[i].size(); j++)
if((*len)[(*adjacencyList)[i][j].to] != UNDEFINED)
(*len)[i] = std::min((*len)[(*adjacencyList)[i][j].to] + (*adjacencyList)[i][j].weight, (*len)[i]);
std::unique_lock<std::mutex> lock(*finishedMutex);
(*finished)++;
lock.unlock();
if((*finished) < threadsAmount * (k + 1)) {
std::mutex my_mutex;
std::unique_lock<std::mutex> my_lock(my_mutex);
condition->wait(my_lock, [finished, threadsAmount, k] {
return (*finished) >= threadsAmount * (k + 1);
});
} else {
condition->notify_all();
}
}
}
};
Graph getComponentTree(const Graph &graph, size_t vertex, const std::vector < size_t > &colors) {
size_t size = graph.size();
Graph tree(size);
std::vector < bool > used(size, false);
prns::componentTreeSearch(graph, used, tree, vertex, colors, colors[vertex]);
return tree;
}
size_t splitIntoComponents(const Graph &graph, std::vector < size_t > &colors) {
size_t size = graph.size();
colors.assign(size, UNDEFINED);
std::vector < bool > used(size, false);
std::stack < size_t > st;
for(size_t i = 0; i < graph.size(); i++)
prns::getDFS_vertexList(graph, st, used, i);
Graph inverted = invert(graph);
size_t color = 0;
while(!st.empty()) {
size_t next = st.top();
st.pop();
if(colors[next] == UNDEFINED) {
prns::paint(inverted, colors, color++, next);
}
}
return color;
}
std::vector < EdgesList > restoreEdgesList(const Graph &graph, Graph &cond,
const std::vector < size_t > &colors, size_t color) {
cond = Graph(color);
std::vector < EdgesList > mainEdges(color);
Graph fullCond(color);
std::vector < EdgesList > prevEdges(color);
for(size_t i = 0; i < graph.size(); i++)
for(size_t j = 0; j < graph[i].size(); j++) {
size_t to = graph[i][j].to;
size_t weight = graph[i][j].weight;
fullCond.addEdge(colors[i], colors[to], weight);
prevEdges[colors[i]].push_back(UndirectedEdge(i, to, weight));
}
std::vector < Edge > dirLst(color);
EdgesList undirLst(color);
std::vector < bool > isExist(color, false);
for(size_t i = 0; i < color; i++) {
for(size_t j = 0; j < fullCond[i].size(); j++) {
size_t to = fullCond[i][j].to;
size_t weight = fullCond[i][j].weight;
if(!isExist[to] || dirLst[to].weight > weight) {
isExist[to] = true;
dirLst[to] = Edge(to, weight);
undirLst[to] = prevEdges[i][j];
}
}
for(size_t j = 0; j < fullCond[i].size(); j++) {
size_t to = fullCond[i][j].to;
if(isExist[to]) {
isExist[to] = false;
if(to != i) {
cond[i].push_back(dirLst[to]);
mainEdges[i].push_back(undirLst[to]);
}
}
}
}
return mainEdges;
}
bool isConnected(const Graph &graph, size_t root) {
size_t size = graph.size();
std::vector < size_t > colors(size, UNDEFINED);
prns::paint(graph, colors, 0, root);
for(size_t i = 0; i < size; i++)
if(colors[i])
return false;
return true;
}
bool isTree(const Graph &graph, size_t root) {
size_t size = graph.size();
size_t sum = 0;
for(size_t i = 0; i < size; i++)
sum += graph[i].size();
return (sum == size - 1) && isConnected(graph, root);
}
Graph getMST(Graph &graph, size_t root) {
size_t size = graph.size();
std::vector < size_t > add(size, 0);
std::vector < bool > used(size, false);
prns::restruct(graph, add);
Graph tree(size), ans(size);
prns::getTree(graph, used, tree, root);
if(isTree(tree, root)) {
prns::restoreFromRestruct(graph, add);
prns::restoreFromRestruct(tree, add);
return tree;
}
Graph cond;
std::vector < size_t > colors;
Graph zero = prns::subGraphWithZeroEdges(graph);
size_t color = splitIntoComponents(zero, colors);
std::vector < EdgesList > mainEdges = restoreEdgesList(graph, cond, colors, color);
tree = getMST(cond, colors[root]);
prns::buildMSTfromCondence(cond, tree, zero, ans, mainEdges, colors, color, root);
prns::restoreFromRestruct(graph, add);
prns::restoreFromRestruct(ans, add);
return ans;
}
std::istream &operator>>(std::istream &in, Graph &graph) {
size_t n, m;
in >> n >> m;
graph = Graph(n);
for(size_t i = 0; i < m; i++) {
size_t from, to, weight;
in >> from >> to >> weight;
from--; to--;
graph[from].push_back(Edge(to, weight));
}
return in;
}
std::ostream &operator<<(std::ostream &out, const Graph &graph) {
for(size_t i = 0; i < graph.size(); i++) {
out << i + 1 << ": ";
for(size_t j = 0; j < graph[i].size(); j++)
out << graph[i][j].to + 1 << '(' << graph[i][j].weight << ") ";
out << std::endl;
}
return out;
}
Graph getRandomGraph(size_t size, size_t minAmount, size_t maxAmount, size_t minWeight, size_t maxWeight) {
Graph graph(size);
srand((unsigned int)time(NULL));
size_t count = minAmount + rand() % (maxAmount - minAmount + 1);
int deltaW = maxWeight - minWeight + 1;
for(size_t i = 0; i < count; i++) {
size_t from = rand() % size;
size_t to = rand() % size;
int weight = minWeight + rand() % deltaW;
graph.addEdge(from, to, weight);
}
return graph;
}
Graph getRandomTree(size_t minSize, size_t maxSize, size_t minWeight, size_t maxWeight) {
srand((unsigned int)time(NULL));
size_t size = minSize + rand() % (maxSize - minSize + 1);
Graph tree(size);
int deltaW = maxWeight - minWeight + 1;
std::vector < size_t > vertex(size);
for(size_t i = 0; i < size; i++)
vertex[i] = i;
for(size_t firstUnused = 1; firstUnused < size; firstUnused++) {
size_t nextUsed = rand() % firstUnused;
size_t nextUnused = firstUnused + rand() % (size - firstUnused);
size_t weight = minWeight + rand() % deltaW;
tree.addEdge(vertex[nextUsed], vertex[nextUnused], weight);
std::swap(vertex[firstUnused], vertex[nextUnused]);
}
return tree;
}
size_t getTotalWeight(const Graph &graph) {
size_t weight = 0;
for(size_t i = 0; i < graph.size(); i++)
for(size_t j = 0; j < graph[i].size(); j++)
weight += graph[i][j].weight;
return weight;
}
std::vector < size_t > Ford_Bellman(const Graph &graph, size_t root) {
size_t size = graph.size();
std::vector < size_t > len(size, INF);
len[root] = 0;
for(size_t i = 0; i < size; i++)
for(size_t j = 0; j < size; j++)
if(len[j] != INF)
for(size_t k = 0; k < graph[j].size(); k++) {
size_t to = graph[j][k].to;
size_t weight = graph[j][k].weight;
len[to] = std::min(len[to], len[j] + weight);
}
return len;
}
std::vector < size_t > multiThreadedFord_Bellman(const Graph &graph, size_t root) {
size_t size = graph.size();
AdjencyList adjacencyList(size);
for(size_t i = 0; i < size; i++)
for(size_t j = 0; j < graph[i].size(); j++)
adjacencyList[graph[i][j].to].push_back(Edge(i, graph[i][j].weight));
const size_t threadsAmount = 5;
size_t leftInd[threadsAmount], rightInd[threadsAmount];
for(size_t i = 0; i < threadsAmount; i++)
leftInd[i] = i * size / threadsAmount;
for(size_t i = 0; i < threadsAmount - 1; i++)
rightInd[i] = leftInd[i + 1];
rightInd[threadsAmount - 1] = size;
std::vector < std::thread* > threads(threadsAmount);
std::vector < size_t > len(size, UNDEFINED);
len[root] = 0;
std::mutex finishedMutex;
std::condition_variable condition;
size_t finished = 0;
for(size_t j = 0; j < threadsAmount; j++)
threads[j] = new std::thread(prns::threadFord_Bellman,
std::make_pair(leftInd[j], rightInd[j]),
std::make_pair(&adjacencyList, &len),
std::make_pair(&finished, &condition),
std::make_pair(threadsAmount, &finishedMutex));
for(size_t j = 0; j < threadsAmount; j++) {
threads[j]->join();
delete threads[j];
}
return len;
}
};