-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresource-allocation-graph.cpp
More file actions
86 lines (80 loc) · 2.06 KB
/
resource-allocation-graph.cpp
File metadata and controls
86 lines (80 loc) · 2.06 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
#include <bits/stdc++.h>
using namespace std;
map<string, set<string>> graph;
map<string, bool> vis;
string source;
bool deadlock = false;
void dfs(string node)
{
vis[node] = true;
for (string n: graph[node])
{
if (vis[n])
{
if (n == source) //... Cycle exists
{
deadlock = true;
}
}
else dfs(n);
}
}
int main()
{
set<string> nodes; //... Contains all the process & resources
string from, to;
while (cin >> from >> to)
{
graph[from].insert(to); //... The edge direction is "from" -> "to"
nodes.insert(from);
nodes.insert(to);
}
for (string node: nodes) //... Checking every node as source for cycle detection
{
for (string n: nodes)
{
vis[n] = false;
}
source = node;
dfs(node);
if (deadlock)
{
cout << "Deadlock";
return 0;
}
}
cout << "No Deadlock";
}
/*//... Sample Input-Output:
___________________________________________________________________________________________________________________________________________________________________________________________________________________________
Input:
T1 R1
R1 T2
T2 R3
R3 T5
T2 R4
R4 T3
T3 R5
T2 R5
R5 T4
T4 R2
R2 T1
___________________________________________________________________________________________________________________________________________________________________________________________________________________________
Output:
Deadlock
___________________________________________________________________________________________________________________________________________________________________________________________________________________________
Input:
T1 R1
T2 R3
R3 T5
T2 R4
R4 T3
T3 R5
T2 R5
R5 T4
T4 R2
R2 T1
___________________________________________________________________________________________________________________________________________________________________________________________________________________________
Output:
No Deadlock
*///...