Repository navigation
Expand file tree
/
Copy pathgraph.cc
More file actions
514 lines (424 loc) · 13 KB
/
Copy pathgraph.cc
File metadata and controls
514 lines (424 loc) · 13 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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <climits>
class Graph
{
public:
void addVertex(const std::string& str);
void addEdge(const std::string& startVertex, const std::string& endVertex, int weight);
std::vector<std::string> DFS() const;
std::vector<std::string> topoSort() const;
std::vector<std::string> BFS() const;
bool pathExistsDFS(const std::string& startVertex,
const std::string& endVertex,
std::vector<std::string>& path) const;
bool pathExistsBFS(const std::string& startVertex,
const std::string& endVertex,
std::vector<std::string>& path) const;
bool isCyclic() const;
bool isTree() const;
Graph cloneGraph() const;
private:
class Edge;
struct Vertex
{
explicit Vertex(const std::string& name_) : name(name_) {}
void addEdge(const Vertex* vertex, int weight);
bool isCyclic(std::unordered_set<std::string>& visited,
std::unordered_set<std::string>& visited_rec) const;
void DFS(std::unordered_set<std::string>& visited, std::vector<std::string>& vec) const;
bool topoSort(std::unordered_set<std::string>& visited,
std::unordered_set<std::string>& visited_rec,
std::stack<std::string>& vertex_stack) const;
void BFS(std::unordered_set<std::string>& visited, std::queue<const Vertex*>& q) const;
bool pathExistsDFS(const Vertex& endVertex,
std::unordered_set<std::string>& visited,
std::vector<std::string>& path) const;
void pathExistsBFS(std::unordered_set<std::string>& visited,
std::queue<const Vertex*>& q) const;
void clone(std::unordered_set<std::string>& visited, std::queue<const Vertex*>& q) const;
std::vector<Edge> edgeList;
std::string name;
};
struct Edge
{
explicit Edge(const Vertex* vertex_, int weight_) : vertex(vertex_), weight(weight_) {}
const Vertex* vertex;
int weight;
};
std::unordered_map<std::string, Vertex> vertexMap_;
};
void Graph::addVertex(const std::string& str)
{
vertexMap_.emplace(str, str);
}
void Graph::addEdge(const std::string& startVertex, const std::string& endVertex, int weight)
{
auto startItr = vertexMap_.find(startVertex);
auto endItr = vertexMap_.find(endVertex);
if ((startItr != vertexMap_.end()) && (endItr != vertexMap_.end())) {
startItr->second.addEdge(&(endItr->second), weight);
}
}
std::vector<std::string> Graph::DFS() const
{
std::unordered_set<std::string> visited;
std::vector<std::string> vec;
for (const auto& itr : vertexMap_) {
itr.second.DFS(visited, vec);
}
return vec;
}
std::vector<std::string> Graph::topoSort() const
{
std::unordered_set<std::string> visited;
std::unordered_set<std::string> visited_rec;
std::stack<std::string> vertex_stack;
for (const auto& itr : vertexMap_) {
if (itr.second.topoSort(visited, visited_rec, vertex_stack)) {
/* cyclic, cannot do topo sort */
return {};
}
}
std::vector<std::string> vec;
while (!vertex_stack.empty()) {
vec.emplace_back(vertex_stack.top());
vertex_stack.pop();
}
return vec;
}
std::vector<std::string> Graph::BFS() const
{
if (vertexMap_.empty()) {
return {};
}
std::vector<std::string> vec;
std::unordered_set<std::string> visited;
std::queue<const Vertex*> q;
auto itr = vertexMap_.begin();
visited.emplace(itr->first);
q.push(&(itr->second));
while (!q.empty()) {
const Vertex* vertex = q.front();
vec.emplace_back(vertex->name);
q.pop();
vertex->BFS(visited, q);
}
return vec;
}
bool Graph::pathExistsDFS(const std::string& startVertex,
const std::string& endVertex,
std::vector<std::string>& path) const
{
auto startItr = vertexMap_.find(startVertex);
auto endItr = vertexMap_.find(endVertex);
if ((startItr == vertexMap_.end()) || (endItr == vertexMap_.end())) {
return false;
}
std::unordered_set<std::string> visited;
return startItr->second.pathExistsDFS(endItr->second, visited, path);
}
bool Graph::pathExistsBFS(const std::string& startVertex,
const std::string& endVertex,
std::vector<std::string>& path) const
{
auto startItr = vertexMap_.find(startVertex);
auto endItr = vertexMap_.find(endVertex);
if ((startItr == vertexMap_.end()) || (endItr == vertexMap_.end())) {
throw "invalid vertex";
}
std::unordered_set<std::string> visited;
std::queue<const Vertex*> q;
q.push(&(startItr->second));
while (!q.empty()) {
const Vertex* vertex = q.front();
path.emplace_back(vertex->name);
if (vertex == &endItr->second) {
return true;
}
q.pop();
vertex->pathExistsBFS(visited, q);
}
return false;
}
bool Graph::isCyclic() const
{
std::unordered_set<std::string> visited;
std::unordered_set<std::string> visited_rec;
for (const auto& itr : vertexMap_) {
if (itr.second.isCyclic(visited, visited_rec)) {
return true;
}
}
return false;
}
bool Graph::isTree() const
{
std::unordered_set<std::string> visited;
std::unordered_set<std::string> visited_rec;
for (const auto& itr : vertexMap_) {
if ((visited.find(itr.first) == visited.end()) && itr.second.isCyclic(visited, visited_rec)) {
return false;
}
}
/* Not cyclic, now check if graph is connected */
for (const auto& itr : vertexMap_) {
if (visited.find(itr.first) == visited.end()) {
return false;
}
}
return true;
}
Graph Graph::cloneGraph() const
{
std::unordered_set<std::string> visited;
Graph graph;
std::queue<const Vertex*> q;
auto itr = vertexMap_.begin();
visited.emplace(itr->first);
q.push(&(itr->second));
while (!q.empty()) {
const Vertex* vertex = q.front();
graph.addVertex(vertex->name);
q.pop();
vertex->clone(visited, q);
}
for (const auto& itr : graph.vertexMap_) {
auto vertex_itr = vertexMap_.find(itr.first);
for (const Edge& edge : vertex_itr->second.edgeList) {
graph.addEdge(itr.first, edge.vertex->name, edge.weight);
}
}
return graph;
}
void Graph::Vertex::addEdge(const Vertex* vertex, int weight)
{
edgeList.emplace_back(vertex, weight);
}
bool Graph::Vertex::isCyclic(std::unordered_set<std::string>& visited,
std::unordered_set<std::string>& visited_rec) const
{
if (visited.find(name) != visited.end()) {
return false;
}
if (visited_rec.find(name) != visited_rec.end()) {
return true;
}
visited.emplace(name);
visited_rec.emplace(name);
for (const Edge& edge : edgeList) {
if (edge.vertex->isCyclic(visited, visited_rec)) {
return true;
}
}
visited_rec.erase(name);
return false;
}
void Graph::Vertex::DFS(std::unordered_set<std::string>& visited,
std::vector<std::string>& vec) const
{
if (visited.find(name) == visited.end()) {
visited.emplace(name);
vec.emplace_back(name);
for (const Edge& edge : edgeList) {
edge.vertex->DFS(visited, vec);
}
}
}
bool Graph::Vertex::topoSort(std::unordered_set<std::string>& visited,
std::unordered_set<std::string>& visited_rec,
std::stack<std::string>& vertex_stack) const
{
if (visited.find(name) != visited.end()) {
return false;
}
if (visited_rec.find(name) != visited_rec.end()) {
return true;
}
visited.emplace(name);
visited_rec.emplace(name);
for (const Edge& edge : edgeList) {
if (edge.vertex->topoSort(visited, visited_rec, vertex_stack)) {
return true;
}
}
visited_rec.erase(name);
vertex_stack.push(name);
return false;
}
void Graph::Vertex::BFS(std::unordered_set<std::string>& visited,
std::queue<const Vertex*>& q) const
{
for (const Edge& edge : edgeList) {
if (visited.find(edge.vertex->name) == visited.end()) {
visited.emplace(edge.vertex->name);
q.push(edge.vertex);
}
}
}
bool Graph::Vertex::pathExistsDFS(const Vertex& endVertex,
std::unordered_set<std::string>& visited,
std::vector<std::string>& path) const
{
if (visited.find(name) != visited.end()) {
return false;
}
visited.emplace(name);
if (this == &endVertex) {
return true;
}
for (const Edge& edge : edgeList) {
if (edge.vertex->pathExistsDFS(endVertex, visited, path)) {
return true;
}
}
return false;
}
void Graph::Vertex::pathExistsBFS(std::unordered_set<std::string>& visited,
std::queue<const Vertex*>& q) const
{
for (const Edge& edge : edgeList) {
if (visited.find(edge.vertex->name) == visited.end()) {
visited.emplace(edge.vertex->name);
q.push(edge.vertex);
}
}
}
void Graph::Vertex::clone(std::unordered_set<std::string>& visited,
std::queue<const Vertex*>& q) const
{
for (const Edge& edge : edgeList) {
if (visited.find(edge.vertex->name) == visited.end()) {
visited.emplace(edge.vertex->name);
q.push(edge.vertex);
}
}
}
class TaskScheduler
{
public:
void addTask(const std::string& name,
const std::vector<std::string>& input_files,
const std::vector<std::string>& output_files,
int time);
std::vector<std::string> getTaskSchedOrder() const;
int getTotalTime() const;
private:
struct Task
{
Task(std::string task_name,
std::vector<std::string> input_files,
std::vector<std::string> output_files,
int time)
: task_name_(std::move(task_name)),
input_files_(std::move(input_files)),
output_files_(std::move(output_files)),
time_(time)
{
}
std::string task_name_;
std::vector<std::string> input_files_;
std::vector<std::string> output_files_;
int time_;
};
using TaskMap = std::unordered_map<std::string, Task>;
using TaskEndTimeMap = std::unordered_map<std::string, int>;
using FileMap = std::unordered_map<std::string, const Task&>;
TaskMap taskMap;
FileMap getFileMap() const;
int getTaskEndTime(const Task& task,
const FileMap& fileMap,
std::unordered_set<std::string>& taskVisited,
TaskEndTimeMap& taskEndTimeMap) const;
};
void TaskScheduler::addTask(const std::string& taskName,
const std::vector<std::string>& input_files,
const std::vector<std::string>& output_files,
int time)
{
taskMap.try_emplace(taskName, taskName, input_files, output_files, time);
}
std::vector<std::string> TaskScheduler::getTaskSchedOrder() const
{
Graph graph;
FileMap fileMap = getFileMap();
if (fileMap.empty()) {
return std::vector<std::string>();
}
for (const auto& taskItr : taskMap) {
for (const std::string& fileName : taskItr.second.input_files_) {
auto fileItr = fileMap.find(fileName);
graph.addEdge(fileItr->first, taskItr.first, 0);
}
}
return graph.topoSort();
}
TaskScheduler::FileMap TaskScheduler::getFileMap() const
{
FileMap file_map;
for (const auto& itr : taskMap) {
for (const std::string& fileName : itr.second.output_files_) {
auto pair = file_map.emplace(fileName, itr.second);
if (!pair.second) {
return FileMap();
}
}
}
return file_map;
}
int TaskScheduler::getTotalTime() const
{
FileMap file_map = getFileMap();
if (file_map.empty()) {
return -1;
}
int endTime = 0;
TaskEndTimeMap taskEndTimeMap;
std::unordered_set<std::string> taskVisited;
for (const auto& itr : taskMap) {
int taskEndTime = getTaskEndTime(itr.second, file_map, taskVisited, taskEndTimeMap);
if (taskEndTime == -1) {
return -1;
}
taskVisited.erase(taskVisited.begin(), taskVisited.end());
endTime = std::max(endTime, taskEndTime);
}
return endTime;
}
int TaskScheduler::getTaskEndTime(const Task& task,
const FileMap& fileMap,
std::unordered_set<std::string>& taskVisited,
TaskEndTimeMap& taskEndTimeMap) const
{
if (taskVisited.find(task.task_name_) != taskVisited.end()) {
return -1;
}
taskVisited.emplace(task.task_name_);
auto itr = taskEndTimeMap.find(task.task_name_);
if (itr != taskEndTimeMap.end()) {
return itr->second;
} else {
int startTime = 0;
for (const std::string& fileName : task.input_files_) {
auto itr = fileMap.find(fileName);
if (itr == fileMap.end()) {
return -1;
}
int dep_task_end_time = getTaskEndTime(itr->second, fileMap, taskVisited, taskEndTimeMap);
if (dep_task_end_time == -1) {
return -1;
}
startTime = std::max(startTime, dep_task_end_time);
}
int endTime = startTime + task.time_;
taskEndTimeMap.emplace(task.task_name_, endTime);
return endTime;
}
}
int main() {}