A topological sort is a linear ordering of nodes in a DAG such that for every edge , node comes before node in the ordering. You can compute it using DFS: run DFS, add nodes to a stack during backtracking, then reverse the stack.
Or use Kahn's algorithm: repeatedly remove nodes with no incoming edges. Topological sort exists if and only if the graph is a DAG. If there is a cycle, no valid ordering exists because you would need some node to come before itself.