added topological ordering to the exhaustive search