-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCycle.java
52 lines (42 loc) · 1.13 KB
/
Cycle.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
import java.util.*;
class Cycle{
public boolean cycle(int v,int[][] adj,boolean[] visited,int parent)
{
visited[v]=true;
int i;
i=1;
while(i<=adj[v].length-1){
if(!visited[i] && adj[v][i]==1){
if(cycle(i,adj,visited,v))return true;
}
else if(i!=parent) return true;
i++;
}
return false;
}
public boolean isCyclic(int[][] adj){
boolean[] visited=new boolean[adj[0].length];
for(int u=1;u<=adj[0].length;u++){
if(!visited[u] && cycle(u,adj,visited,-1)) return true;
}
return false;
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no. of vertices");
int n=sc.nextInt();
System.out.println("Enter the no. of edges");
int edges=sc.nextInt();
System.out.println("Enter the edges");
int[][] adj=new int[n+1][n+1];
System.out.println();
for(int i=0;i<edges;i++){
int x=sc.nextInt();int y=sc.nextInt();
adj[x][y]=1;
adj[y][x]=1;
}
Cycle ob=new Cycle();
if(ob.isCyclic(adj))System.out.println("The Undirected Graph contains a cycle");
else System.out.println("The undirected graph does not contain a cycle");
}
}