(*) TopologicalSort: refactor for reuse