Note 1: it’s not possible to do topological order for non DAG graph
Note 2: there can be multiple topological order
Backward implementation by DFS pseudo-code
\begin{algorithm}
\caption{topologicalSort}
\begin{algorithmic}
\Function{DFS}{$s, clock, S$}
\State s.state $\gets$ active
\ForAll{edge $u \to v$}
\If{v.state $=$ new}
\State clock $\gets$ \Call{DFS}{$v, clock$}
\Else
\If{v.state $=$ active}
\State fail \Comment{$\;$it should not has cycle because it should be DAG}
\EndIf
\EndIf
\State s.state $\gets$ finish
\EndFor
\State $S[clock] \gets v$
\State $clock \gets clock - 1$
\Return $clock$
\EndFunction
\State \\
\Function{topologicalSort}{$s, V$}
\ForAll{vertex $v \in V$}
\State v.state $\gets$ new
\EndFor
\State $S \gets \emptyset$
\State $clock \gets |V|$
\ForAll{vertex $v \in V$}
\If{v.state $=$ new}
\State $clock \gets$ \Call{DFS}{$v, clock, S$}
\EndIf
\EndFor
\Return $S[1 \dots |V|]$
\EndFunction
\end{algorithmic}
\end{algorithm}