# Graph based Java DFS Solution

1. Construct a graph from the given input.
2. Detect if there are cycles in the graph. (https://www.youtube.com/watch?v=rKQaZuoUR4M)
``````public class Solution {

Set<Integer> white = new HashSet<>();
Set<Integer> grey = new HashSet<>();
Set<Integer> black = new HashSet<>();

public boolean canFinish(int numCourses, int[][] prerequisites) {
Graph graph = new Graph(numCourses);
for(int i=0;i<prerequisites.length;i++) {
}
return detectCycles(graph, numCourses);
}

private boolean detectCycles(Graph graph, int numCourses) {
int i = 0;
while(i < numCourses) {
i++;
}
for(int j=0;j<graph.edges.length;j++) {
if(exploreChildren(graph, j) == -1) return false;
}
return true;
}

private int exploreChildren(Graph graph, int c) {
List<Integer> l = graph.edges[c];
white.remove(c);
if(l != null) {
for(Integer i : l) {
if(black.contains(i)) continue;
if(grey.contains(i)) return -1;
if(exploreChildren(graph, i) == -1) return -1;
grey.remove(i);
}
}
grey.remove(c);
return 1;
}
}

class Graph {
List<Integer>[] edges;
public Graph(int numOfVertices) {