-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.java
69 lines (57 loc) · 1.57 KB
/
Graph.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
import java.util.*;
class Graph {
private int V;
private LinkedList<Integer> adj[];
public Graph(int V) {
this.V = V;
adj = new LinkedList[V];
for (int i = 0; i < V; i++) {
adj[i] = new LinkedList();
}
}
public void addEdge(int v, int w) {
adj[v].add(w);
}
private boolean isCyclicUtil(int v, boolean visited[], boolean recStack[]) {
if (recStack[v]) {
return true;
}
if (visited[v]) {
return false;
}
visited[v] = true;
recStack[v] = true;
// Recur for all the vertices adjacent to this vertex
for (Integer neighbor : adj[v]) {
if (isCyclicUtil(neighbor, visited, recStack)) {
return true;
}
}
recStack[v] = false;
return false;
}
public boolean isCyclic() {
boolean visited[] = new boolean[V];
boolean recStack[] = new boolean[V];
for (int i = 0; i < V; i++) {
if (!visited[i]) {
if (isCyclicUtil(i, visited, recStack)) {
return true;
}
}
}
return false;
}
public static void main(String args[]) {
Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(1, 2);
g.addEdge(2, 3);
g.addEdge(3, 1); // Adding cycle
if (g.isCyclic()) {
System.out.println("Graph contains cycle");
} else {
System.out.println("Graph doesn't contain cycle");
}
}
}