Reimplement calculation of build sequence using topological sort.