Produces topological sorting of a graph