A topological sorting algorithm