-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeadlock_cycle.cpp
More file actions
45 lines (36 loc) · 1.07 KB
/
deadlock_cycle.cpp
File metadata and controls
45 lines (36 loc) · 1.07 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
#include <vector>
struct Graph_vertex;
bool has_cycle(Graph_vertex&);
enum class Vertex_color { white, gray, black };
struct Graph_vertex {
Vertex_color color = Vertex_color::white;
std::vector<Graph_vertex*> edges;
};
bool is_deadlocked(const std::vector<Graph_vertex>& vertices)
{
return std::any_of(
vertices.begin(),
vertices.end(),
[](Graph_vertex& vertex) { return vertex.color == Vertex_color::white && has_cycle(vertex); });
}
bool has_cycle(Graph_vertex& cur)
{
if (cur.color == Vertex_color::gray) { return true; }
cur.color = Vertex_color::gray;
for (auto& next : cur.edges) {
if (next->color != Vertex_color::black) {
if (has_cycle(*next)) { return true; }
}
}
cur.color = Vertex_color::black;
return false;
}
//bool has_cycle_exclusion(Graph_vertex& cur)
//{
// if (cur.color == Vertex_color::black) { return true; }
//
// cur.color = Vertex_color::black;
//
// for (auto& next : cur.edges) { if (has_cycle_exclusion(*next)) { return true; }}
// return false;
//}