Implement graph topological sort