second algorithm, topological sort