Use a topological sort to avoid re-executions