-
Notifications
You must be signed in to change notification settings - Fork 0
/
cycles.java
60 lines (57 loc) · 1.4 KB
/
cycles.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
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class graph {
int v;
int e;
LinkedList<Integer>[] adjlist;
boolean cycle = false;
graph(int v){
this.v = v;
this.e = 0;
adjlist = new LinkedList[this.v];
for(int i = 0; i < this.v; i++) {
adjlist[i] = new LinkedList<Integer>();
}
}
public void addedge(int v, int w) {
adjlist[v].add(w);
adjlist[w].add(v);
this.e++;
}
public boolean iscycle() {
boolean[] visited = new boolean[this.v];
for(int i = 0; i < this.v; i++) {
this.cycle = dfs(visited, i, -1);
}
return this.cycle;
}
public boolean dfs(boolean[] visited, int v, int p) {
visited[v] = true;
for(int w : adjlist[v]){
if(!visited[w])
dfs(visited, w, v);
else{
if(p > -1 && w != p)
this.cycle = true;
}
}
return this.cycle;
}
}
class cycles
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
graph g = new graph(4);
g.addedge(0,1);
//g.addedge(0,3);
//g.addedge(1,2);
g.addedge(2,3);
//g.addedge(3,4);
System.out.println(g.iscycle());
}
}