-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcycle.java
65 lines (51 loc) · 1.76 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
53
54
55
56
57
58
59
60
61
62
63
64
65
// import java.util.*;
// import java.io.*;
// import java.lang.*;
// class cycle {
// public static void main(String[] args) {
// int V = 6;
// ArrayList<ArrayList<Integer>> graph = new ArrayList<>(V);
// // Vertex - 0
// ArrayList<Integer> neighbors = new ArrayList<Integer>();
// neighbors.add(1);
// graph.add(neighbors);
// // Vertex - 1
// neighbors = new ArrayList<Integer>();
// neighbors.add(2);
// graph.add(neighbors);
// // Vertex - 2
// neighbors = new ArrayList<Integer>();
// neighbors.add(0);
// graph.add(neighbors);
// if (isCyclic(V, graph))
// System.out.println("Cycle detected");
// else
// System.out.println("No cycles detected");
// }
// private static boolean checkCycle(int node, ArrayList<ArrayList<Integer>> adj, int vis[], int dfsVis[]) {
// vis[node] = 1;
// dfsVis[node] = 1;
// for (Integer neighbor : adj.get(node)) {
// if (vis[neighbor] == 0) {
// if (checkCycle(neighbor, adj, vis, dfsVis) == true) {
// return true;
// }
// } else if (dfsVis[neighbor] == 1) {
// return true;
// }
// }
// dfsVis[node] = 0;
// return false;
// }
// public static boolean isCyclic(int N, ArrayList<ArrayList<Integer>> adj) {
// int vis[] = new int[N];
// int dfsVis[] = new int[N];
// for (int i = 0; i < N; i++) {
// if (vis[i] == 0) {
// if (checkCycle(i, adj, vis, dfsVis) == true)
// return true;
// }
// }
// return false;
// }
// }