topological sort implementation