-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCycleDetection.java
82 lines (73 loc) · 2 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//BFS code
class Pair{
int first;
int second;
public Pair(int first, int second){
this.first=first;
this.second=second;
}
}
class Solution {
// Function to detect cycle in an undirected graph.
public boolean isCycle(ArrayList<ArrayList<Integer>> adj) {
int c=0;
for(ArrayList<Integer> sublist : adj){
c++;
}
boolean vis[]= new boolean[c+1];
if(check(0,adj,vis))
return true;
else
return false;
}
public boolean check(int src,ArrayList<ArrayList<Integer>> adj, boolean vis[]){
vis[src]=true;
Queue<Pair> q=new LinkedList<>();
q.add(new Pair(src,-1));
while(!q.isEmpty()){
int pres=q.peek().first;
int parent=q.peek().second;
q.remove();
for(int i: adj.get(pres)){
if(vis[i]==false){
vis[i]=true;
q.add(new Pair(i,pres));
}
else if(parent != i){
return true;
}
}
}
return false;
}
}
//DFS Code
class Solution {
// Function to detect cycle in an undirected graph.
public boolean isCycle(ArrayList<ArrayList<Integer>> adj) {
int V=0;
for(ArrayList<Integer> sublist : adj){
V++;
}
int vis[] = new int[V];
for(int i = 0;i<V;i++) {
if(vis[i] == 0) {
if(dfs(i, vis, adj, -1)) return true;
}
}
return false;
}
public static boolean dfs(int node, int vis[], ArrayList<ArrayList<Integer>> adj, int parent){
vis[node]=1;
for(Integer it: adj.get(node)){
if(vis[it]==0){
if(dfs(it,vis,adj,parent)){
return true;
}
else if(parent!=it)
return true;
}
}
return false;
}
}