Proofs: Palindrome partition by backtracking
These are the human proofs of Palindrome partition by backtracking, at full length.
Correctness of palindrome partition by backtracking full
This proof shows: correctness.
We prove that PALINDROME-PARTITIONS outputs every palindromic partition of exactly one time, in increasing lexicographic order of the sequences of piece lengths, and that it stops. Indices are 0-based and includes both ends.
Lemma 1 (the table). After lines L1 to L4, is true if and only if is a palindrome, for , and is the increasing list of all such that is a palindrome.
Proof. By induction on . If , the string is a palindrome if and only if , which is what line L3 computes. If , then is a palindrome if and only if and is a palindrome, because the mirror pairs of are the pair and the mirror pairs of . The entry is in row , which line L1 fills before row , and it is correct by the induction hypothesis since . Line L4 appends to exactly when is true, and line L2 visits in increasing order.
Let be the list of the palindromic partitions of the suffix , sorted in increasing lexicographic order of their sequences of piece lengths. So holds one element, the empty partition. For , each partition of has a unique first piece , which is a palindrome, followed by a partition of . So by Lemma 1, is the concatenation, over in in increasing order, of the lists " followed by " for in . Partitions with a shorter first piece come first in the order, and those with the same first piece are ordered by the rest.
Lemma 2 (one frame). Suppose that at some moment the stack is with the frame pushed on top, and is a list of pieces whose total length is . Then the loop of lines L6 to L11 runs until the stack is again, without reading or changing before that moment. In this time it outputs exactly followed by for each in , in the order of . When the stack is again, is without its last piece if , and if .
Proof. By strong induction on .
Case . Line L7 reads the frame , and line L8 outputs , which is followed by the empty partition, the only element of . Then it pops the frame and removes the last piece of if .
Case . Let , with because is a palindrome. We show by induction on that, for , the loop reaches a state where the top frame is , the stack below it is , , and the outputs so far are followed by the elements of with first piece . For this is the given state. If the claim holds for some , then line L8 does not apply since , line L9 does not apply since , and lines L10 and L11 set the top frame to , append the piece to and push . Now is followed by , of total length , and . By the induction hypothesis of the lemma, applied with followed by in the role of , the loop outputs , then , then , for each in in order, and comes back to the top frame with , since and the last piece is removed. These are the elements of with first piece , so the claim holds for . At , line L9 applies: it pops the frame, so the stack is , and it removes the last piece of if . By the decomposition of above, the outputs are followed by each element of , in order. In all these steps only the top frame and the frames above it are read or changed, so is untouched.
Theorem. PALINDROME-PARTITIONS stops, and it outputs each palindromic partition of exactly one time, in increasing lexicographic order of the sequences of piece lengths.
Proof. Line L5 makes the stack hold only the frame with empty, of total length 0. Lemma 2 with empty and shows that the loop comes to an empty stack, so it stops at line L6, and the outputs are the elements of , the partitions of , each one time and in order. For the output is the single empty partition. Each step of the loop either pushes a frame for a new partial partition or pops a frame, and Lemma 2 shows that each pushed frame is popped; the number of partial partitions is finite, so the number of steps is finite.