-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeadlock detection.java
More file actions
121 lines (100 loc) · 2.89 KB
/
Deadlock detection.java
File metadata and controls
121 lines (100 loc) · 2.89 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
/*
* @author Sushil
* @description : This program detects deadlock in given set of dependencies or else prints the correct sequence of excecution
*/
import java.util.ArrayList;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.List;
import java.util.LinkedList;
public class task {
private final int V;
private final List<List<Integer>> adj; //A 2D list to store graph
public task(int V)
{
this.V = V;
adj = new ArrayList<>(V);
for (int i = 0; i < V; i++)
adj.add(new LinkedList<>());
}
// This function is a variation of DFSUytil() in
// https://www.geeksforgeeks.org/archives/18212
private boolean isCyclicUtil(int i, boolean[] visited,
boolean[] recStack)
{
// Mark the current node as visited and
// part of recursion stack
if (recStack[i])
return true;
if (visited[i])
return false;
visited[i] = true;
recStack[i] = true;
List<Integer> children = adj.get(i);
for (Integer c: children)
if (isCyclicUtil(c, visited, recStack))
return true;
recStack[i] = false;
return false;
}
private void addEdge(int source, int dest) {
adj.get(source).add(dest);
}
// Returns true if the graph contains a
// cycle, else false.
// This function is a variation of DFS() in
// https://www.geeksforgeeks.org/archives/18212
private boolean isCyclic()
{
// Mark all the vertices as not visited and
// not part of recursion stack
boolean[] visited = new boolean[V];
boolean[] recStack = new boolean[V];
// Call the recursive helper function to
// detect cycle in different DFS trees
for (int i = 0; i < V; i++)
if (isCyclicUtil(i, visited, recStack))
return true;
return false;
}
public static void main(String[] args) {
int D;
int n,i=0,temp1,temp2;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
D=sc.nextInt();
ArrayList<Integer> x=new ArrayList<Integer>();
ArrayList<Integer> y=new ArrayList<Integer>();
ArrayList<Integer> result=new ArrayList<Integer>();
task g=new task(n+1);
while(i++<D) //Loop for taking inputs in the array
{
temp1=sc.nextInt();
temp2=sc.nextInt();
if(temp1>n || temp2>D) //if edge number is not valid
{
System.err.println("Invalid input");
System.exit(0);
}
g.addEdge(temp1,temp2); //Add edges into the graph
x.add(temp1);
y.add(temp2);
}
if(g.isCyclic()) //If graph is cyclic
System.out.println("-1");
else
{
for(i=0;i<x.size();i++) //Find what edges to we can start with
{
if(!y.contains(x.get(i)))
result.add(x.get(i));
}
result.sort(null); //Sort the edges
List<Integer> deduped = result.stream().distinct().collect(Collectors.toList()); //Remove repeated elements
for(i=0;i<deduped.size();i++) //print the elements in distinct array
System.out.print(deduped.get(i)+" ");
System.out.println();
}
sc.close();
}
}