# Java DFS(5ms) and BFS(9ms) solution

• DFS solution in 5 ms：

``````public class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {

boolean[] visited = new boolean[numCourses];
for(int i = 0; i < numCourses; i++)
for(int i = 0; i < prerequisites.length; i++)
{
int curCourse = prerequisites[i][0];
int preCourse = prerequisites[i][1];
}
for(int i = 0; i < numCourses; i++)
{
return false;
}
return true;
}

private boolean dfs(List<Integer>[] adj, boolean[] visited, int course){

if(visited[course])         // have circle
return false;
visited[course] = true;
for (int i = 0; i < adj[course].size(); i++)
{
return false;
}
visited[course] = false;
return true;
}
}
``````

BFS solution in 9 ms：

``````public class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {

for(int i = 0; i < numCourses; i++)
int[] indegree = new int[numCourses];
int finishCount = 0;
for (int i = 0; i < prerequisites.length; i++)
{
int curCourse = prerequisites[i][0];
int preCourse = prerequisites[i][1];
indegree[curCourse]++;
}
for (int i = 0; i < numCourses; i++)
{
if (indegree[i] == 0)
}
{
int course = readyCourses.poll();        // finish
finishCount++;
{
indegree[nextCourse]--;
if (indegree[nextCourse] == 0)