> For the complete documentation index, see [llms.txt](https://ugopireddy.gitbook.io/leet-code-solutions/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ugopireddy.gitbook.io/leet-code-solutions/solutions-201---250/207-leetcode-java-course-schedule-medium.md).

# 207 LeetCode Java: Course Schedule – Medium

### Problem:

There are a total of n courses you have to take, labeled from 0 to n - 1.

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: \[0,1]

Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?

For example:

2, \[\[1,0]] There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.

2, \[\[1,0],\[0,1]] There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.

Note: The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.

Hints:

This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.

### Thoughts:

This is a very obvious problem on Topological sort. In Introduction to Algorithm, it is the problem of a professor trying to find the order of wearing clothes.

But here, in this problem, it is not asking for the order of courses, but is asking for if it is possible to finish. So it is equivalent to finding if a cycle is in a directed graph.

So a DFS is helpful, because in DFS, whenever you meet GRAY (back edge), it indicates there is a cycle in the directed graph.

The solution below is based on a DFS.

### Solutions:

````java
public class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        HashMap<Integer, List<Integer>> adj = new HashMap<Integer, List<Integer>>();
        int[] color = new int[numCourses];//0 is white, 1 is gray 2, is black
        initAdj(adj, prerequisites);
        for (int i = 0; i < numCourses; i ++) {
            if (color[i] == 1) {
                return false;
            }
            if (color[i] == 0) {
                //DFS visit
                if (dfsVisit(adj, color, i) == false) {
                    return false;
                }
            }
        }
        return true;
    }
    private boolean dfsVisit(HashMap<Integer, List<Integer>> adj, int[] color, int i) {
        if (!adj.containsKey(i)) {
            color[i] = 2;
            return true;
        }
        color[i] = 1;
        for (Integer j:adj.get(i)) {
            if (color[j] == 1) {
                return false;
            }
            if (color[j] == 0) {
                if (dfsVisit(adj, color, j) == false) {
                    return false;
                }
            }
        }
        color[i] = 2;
        return true;
    }
    private void initAdj(HashMap<Integer, List<Integer>> adj , int[][] pre) {
        for (int i = 0; i < pre.length; i ++) {
            if (!adj.containsKey(pre[i][1])) {
                adj.put(pre[i][1], new LinkedList());
            }
            adj.get(pre[i][1]).add(pre[i][0]);
        }
    }
}```
````


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ugopireddy.gitbook.io/leet-code-solutions/solutions-201---250/207-leetcode-java-course-schedule-medium.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
