Proofs: Generic backtracking algorithm
These are the human proofs of Generic backtracking algorithm, at full length.
Backtracking visits exactly the reachable prefixes and outputs exactly the solutions full
This proof shows: correctness, termination.
We use the pseudocode of the page. A sequence with is reachable when, for each from 1 to , and hold. The empty sequence is reachable. We assume that each set is finite and that each test returns true or false.
Lemma. Let be reachable, and call BACKTRACK with . The call terminates. During the call, BACKTRACK is called (directly or deeper) exactly once for each reachable proper extension with , these calls start in lexicographic order of the candidate orders, the call outputs exactly the reachable extensions of length (including itself when ), and on return is unchanged and has length .
Proof. By induction on .
Base, . Line L1 is true. Line L2 outputs , which is the only reachable extension of length , and line L3 returns without a call and without a change to . A sequence of length has no proper extension of length at most .
Step, . Assume the lemma for all reachable prefixes of length . Line L1 is false. Let be the candidates of in their order. The loop of line L4 handles them one at a time. We show by induction on that, before the iteration for , has length and . This holds for . In the iteration for , line L5 sets , so .
- If is false, the sequence is not reachable, and neither is any extension of it, because reachability needs the test at position . Line L7 does not run, and no call is made for these sequences.
- If the test is true, is reachable. Line L7 calls BACKTRACK. By the induction hypothesis, this call terminates, makes exactly one call for each reachable proper extension of , in lexicographic order, outputs exactly the reachable extensions of of length , and leaves of length with prefix .
In both cases line L8 removes , so has length and prefix before the next iteration. Each reachable proper extension of starts with exactly one , so it is either itself, for which line L7 makes the call, or a proper extension of it, which the inner call handles. So each one gets exactly one call. The calls under start before the calls under , and within one they are in lexicographic order by the hypothesis, so all calls are in lexicographic order. The outputs are the union over of the reachable extensions of length under , which is the set of reachable extensions of length of . The loop has iterations and each one terminates, so the call terminates, and on return is as it was at the start.
Theorem. BACKTRACK with empty terminates and outputs exactly the reachable sequences of length , each one once, in lexicographic order of the candidate orders. If the tests are prefix-closed, that is, implies for each , then the output is the set of sequences with for each and .
Proof. The empty sequence is reachable, so the lemma with gives termination and the output, each sequence once because each reachable sequence of length gets exactly one call, and each call outputs only its own sequence at line L2. For the second part, let satisfy the domain conditions and . Prefix closure applied times gives for each , so the sequence is reachable. Conversely, a reachable sequence of length satisfies by definition.
Number of calls. By the lemma, the number of calls is the number of reachable sequences of length 0 to . Each call runs line L6 once for each candidate, so the number of tests is over the reachable with , which is at most .