Added topological sort to misc.py