private boolean topSort(int course, Neighbors[] graph, boolean[] used, Deque<Integer> stack, boolean[] dfs) {
        if (used[course]) {
            return false;
        }
        if (dfs[course]) {
            return true;
        }
        dfs[course] = true;
        for (int adj : graph[course].neighbor) {
            if (topSort(adj, graph, used, stack, dfs)) {
                return true;
            }
        }
        dfs[course] = false;
        used[course] = true;
        stack.offerFirst(course);
        return false;
    }
Date 03/01/2016
@author Tushar Roy

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, return the ordering of courses you should take to finish all courses.
There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.

Time complexity O(n)
Space complexity O(n)

https://leetcode.com/problems/course-schedule-ii/
src/com/interview/graph/CourseSchedule.java