Topological sort. 2 version: with and without Recursion