TopologicalSort class added for nonterminal sort