implemented topological sort on constraints