Course schedule (cycle detection)

Decide whether every course can be finished. A directed graph can be fully ordered only if it has no cycle.

Time O(V + E)Space O(V)Code C#, Java, C++ and Python
Watch it animatedStep through it, with the running line highlighted. Free, no sign-in needed to watch.

The idea

Colour every node white (unvisited). DFS from each white node, turning it grey (in progress) on entry and black (done) on exit. A grey node reached again means the recursion has looped back onto its own path, which is exactly a cycle. A black node is already proven safe and is never revisited.

Code

public static bool CanFinish(int n, Dictionary<char, List<char>> graph) {
    var state = new Dictionary<char, int>(); // 0 unvisited, 1 in progress, 2 done
    foreach (var u in graph.Keys)
        if (state.GetValueOrDefault(u) == 0 && HasCycle(u, graph, state))
            return false;
    return true;
}

static bool HasCycle(char u, Dictionary<char, List<char>> graph, Dictionary<char, int> state) {
    state[u] = 1; // in progress: sits on the current recursion path
    foreach (char v in graph[u]) {
        int s = state.GetValueOrDefault(v);
        if (s == 1) return true; // back edge to a grey node: a cycle
        if (s == 0 && HasCycle(v, graph, state)) return true;
    }
    state[u] = 2; // done: can never sit on a cycle
    return false;
}
static boolean canFinish(int n, Map<Character, List<Character>> graph) {
    Map<Character, Integer> state = new HashMap<>(); // 0 unvisited, 1 in progress, 2 done
    for (char u : graph.keySet())
        if (state.getOrDefault(u, 0) == 0 && hasCycle(u, graph, state))
            return false;
    return true;
}

static boolean hasCycle(char u, Map<Character, List<Character>> graph, Map<Character, Integer> state) {
    state.put(u, 1); // in progress: sits on the current recursion path
    for (char v : graph.get(u)) {
        int s = state.getOrDefault(v, 0);
        if (s == 1) return true; // back edge to a grey node: a cycle
        if (s == 0 && hasCycle(v, graph, state)) return true;
    }
    state.put(u, 2); // done: can never sit on a cycle
    return false;
}
bool hasCycle(char u, unordered_map<char, vector<char>>& graph, unordered_map<char, int>& state) {
    state[u] = 1; // in progress: sits on the current recursion path
    for (char v : graph[u]) {
        int s = state[v];
        if (s == 1) return true; // back edge to a grey node: a cycle
        if (s == 0 && hasCycle(v, graph, state)) return true;
    }
    state[u] = 2; // done: can never sit on a cycle
    return false;
}

bool canFinish(int n, unordered_map<char, vector<char>>& graph) {
    unordered_map<char, int> state; // 0 unvisited, 1 in progress, 2 done
    for (auto& [u, _] : graph)
        if (state[u] == 0 && hasCycle(u, graph, state))
            return false;
    return true;
}
def can_finish(n: int, graph: dict[str, list[str]]) -> bool:
    state: dict[str, int] = {}  # 0 unvisited, 1 in progress, 2 done
    for u in graph:
        if state.get(u, 0) == 0 and has_cycle(u, graph, state):
            return False
    return True

def has_cycle(u: str, graph: dict[str, list[str]], state: dict[str, int]) -> bool:
    state[u] = 1  # in progress: sits on the current recursion path
    for v in graph[u]:
        s = state.get(v, 0)
        if s == 1: return True  # back edge to a grey node: a cycle
        if s == 0 and has_cycle(v, graph, state): return True
    state[u] = 2  # done: can never sit on a cycle
    return False

Tips

In the .NET library

A back edge to a grey node is the one and only sign of a cycle in a directed graph. An edge to a black node is always safe, even if it looks like it "goes backwards" in the recursion.