Topological sort on dependencies