-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathass.java
More file actions
68 lines (62 loc) · 1.98 KB
/
ass.java
File metadata and controls
68 lines (62 loc) · 1.98 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
import java.util.*;
import java.util.LinkedList;
import javax.management.Query;
public class ass {
public static class edge{
int src;
int dest;
int wt;
edge(int s , int d , int w){
this.src = s;
this.dest = d;
this.wt = w;
}
}
public static void createGraph(ArrayList<edge>graph[]){
for(int i = 0 ;i<graph.length;i++){
graph[i] = new ArrayList<>();
}
graph[0].add(new edge(0,1,1));
graph[0].add(new edge(0,3,1));
graph[1].add(new edge(1,2,1));
graph[2].add(new edge(2,0,1));
graph[2].add(new edge(2,1,1));
graph[3].add(new edge(3,0,1));
graph[3].add(new edge(3,4,1));
graph[4].add(new edge(4,3,1));
}
public static boolean detectCycle(ArrayList<edge>graph[]){
boolean visited[] = new boolean[graph.length];
for(int i = 0 ;i<graph.length;i++){
if(!visited[i]){
return detectCycleUtil(graph, i, -1,visited);
}
}
return false;
}
public static boolean detectCycleUtil(ArrayList<edge>graph[],int src,int parent,boolean visited[]){
Queue<Integer>q = new LinkedList<>();
q.add(src);
visited[src] = true;
while(!q.isEmpty()){
int curr = q.remove();
for(int i = 0;i<graph[curr].size();i++){
edge e = graph[curr].get(i);
if(!visited[e.dest]){
visited[e.dest] = true; q.add(e.dest);
detectCycleUtil(graph, e.dest, curr, visited);
}
else if(visited[e.dest]&&e.dest!=parent){
return true;
}
}
}
return false;
}
public static void main(String arg[]){
int v = 5;
ArrayList<edge>graph[] = new ArrayList[v];
createGraph(graph);
System.out.print(detectCycle(graph));
}
}