rewrite topsort (without test)