def top_sort_recursive(graph):
order, enter, state = [], set(graph), {}<EOL>def dfs(node):<EOL><INDENT>state[node] = GRAY<EOL>for k in graph.get(node, ()):<EOL><INDENT>sk = state.get(k, None)<EOL>if sk == GRAY:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if sk == BLACK:<EOL><INDENT>continue<EOL><DEDENT>enter.discard(k)<EOL>dfs(k)<EOL><DEDENT>order.append(node)<EOL>state[node] = BLACK<EOL><DEDENT>while enter: dfs(enter.pop())<EOL>return order<EOL>
Time complexity is the same as DFS, which is O(V + E)
        Space complexity: O(V)