4.39 Exercise 4.39
Changing the order in which the restrictions are checked does not change the answer(s) that are found. This is intuitively obvious – the same values are tested, and the set of restrictions that are checked is the same, so all the sets of values that satisfy all of the restrictions must be the same no matter what order they are checked in.
However, the order that the restrictions are checked in does have an effect on the amount of time it may take to find an answer. The biggest optimization to be found is in moving the distinctness requirement to the end. The logic for this move is that this operation is vastly more expensive to check than any of the other requirements, and therefore should be tested as few times as possible. If we exhaust the rest of requirements first, then the number of times that distinct? is called is as low as it could possibly be.
For a more limited optimization, consider the fact that one of the first checks is that cooper is not equal to 1. cooper is the second value generated, which means that three other sets need to be tested to exhaustion before cooper gets a passing value.
For arbitrary programs, finding the optimal order to test requirements in may be very difficult. However, the order that the requirements for participants are being tested in in this program is (almost) the same as the order in which those participants were assigned values. If, before the distinctness check, we simply reversed the order in which the requirements were checked, we would do much better, and if we moved the requirement for fletcher and cooper not to be within one of each other to be after the check that miller is not above cooper, we would be even better at finding the one solution to the problem quickly.