copied topological sort code to BOT

from hop