Topological sorting.
Works almost like the tsort(1) utility.
