/
DetectCycle.java
51 lines (42 loc) · 1.54 KB
/
DetectCycle.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
/*
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 would be added by GfG's Online Judge.*/
/*Complete the function below*/
//https://practice.geeksforgeeks.org/problems/detect-cycle-in-a-directed-graph/1
class GfG
{
public boolean hasCycle(int v,LinkedList<Integer>[] alist,boolean[] visited, boolean[] explored) {
for (int i = 0; i < v; i++) {
boolean hasCycle = tracePathForCycle(i, alist[i], alist, visited, explored);
if (hasCycle) {
return hasCycle;
} else {
visited[i] = true;
}
}
return false;
}
public boolean tracePathForCycle(int vertex, LinkedList<Integer> connectedNodes, LinkedList<Integer>[] graph, boolean[] visited, boolean[] explored) {
if (visited[vertex]) {
return false;
}
if (explored[vertex]) {
return true;
}
if (connectedNodes.size() <= 0) {
explored[vertex] = false;
return false;
}
explored[vertex] = true;
for (Integer neighbour : connectedNodes) {
//System.out.println("Here Vertex- " + vertex + " Neighbour = "+ neighbour);
boolean hasCycle = tracePathForCycle(neighbour, graph[neighbour], graph, visited, explored);
if (hasCycle){
return hasCycle;
}
}
explored[vertex] = false;
return false;
}
}