Iterative solution to topological sort