-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCycleDetection.java
68 lines (65 loc) · 1.66 KB
/
CycleDetection.java
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
{
import java.io.*;
import java.util.*;
import java.lang.*;
class Graph{
@SuppressWarnings("unchecked")
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t-- > 0){
int v = in.nextInt();
int e = in.nextInt();
LinkedList<Integer>[] alist = new LinkedList[v];
for(int i=0;i<v;i++) alist[i] = new LinkedList<Integer>();
while(e-->0) {
int n1 = in.nextInt();
int n2 = in.nextInt();
alist[n1].add(n2);
}
boolean[] visited = new boolean[v];
boolean[] explored = new boolean[v];
GfG g = new GfG();
if(g.hasCycle(v,alist,visited,explored)) {
System.out.println(1);
}
else
System.out.println(0);
}
}
}
}
/*Please note that it's Function problem i.e.
you need to write your solution in the form of Function(s) only.
Driver Code to call/invoke your function is mentioned above.*/
/*Complete the function below*/
class GfG
{
public boolean DFS(int x,LinkedList<Integer> adj[],boolean vis[], boolean expl[])
{
if(expl[x])
return true;
if(vis[x])
return false;
vis[x] = true;
expl[x]=true;
Iterator<Integer> itr = adj[x].iterator();
while(itr.hasNext()){
int n = itr.next();
if(DFS(n, adj, vis, expl))
return true;
}
expl[x]=false;
return false;
}
public boolean hasCycle(int v,LinkedList<Integer>[] alist,boolean[] vis,boolean[] explored)
{
for(int i=0; i<v; i++){
if(DFS(i, alist, vis, explored))
return true;
explored[i]=false;
}
return false;
}
}