topological sort impl