Proofs: PQ-tree
These are the human proofs of PQ-tree, at full length.
REDUCE keeps exactly the frontiers in which S is consecutive, and it terminates full
This proof shows: correctness, termination.
Let be a PQ-tree over in which every P-node has at least 2 children and every Q-node at least 3, and let . For a node , write for its set of leaves and for its set of frontiers, as defined on the page. For sets of sequences , write . Call a sequence over right-full if it has the form where has no element of and has only elements of , and write for the right-full frontiers of . Write for the frontiers of in which the elements of are consecutive. For a list of trees , let .
Lemma 1 (reversal). For every node , the reverse of a frontier of is a frontier of .
Proof. By induction on the height. A leaf is its own reverse. If for a node with children , then its reverse is , where by induction. The reversed order of the children is allowed at a P-node, which allows every order, and at a Q-node, which allows the given order and its reverse, and the reverse of the reverse is the given order.
So, for a list of trees, the reverses of the sequences in form exactly .
Lemma 2 (normalization). Replacing a node with one child by that child, or a Q-node with two children by a P-node with the same children, does not change . Hence for every , and is the union of over all permutations , also after normalization.
Proof. A node with one child has one order of its children. With two children, the permutations are the identity and the swap, which are exactly the identity and the reversal.
Every node that REDUCE builds is normalized, so the node rules hold for its result.
Lemma 3 (labels). If is partial, every frontier of contains an element of and an element not in . A child of an empty node is empty, and a child of a full node is full.
Proof. Immediate from the definitions , and .
Lemma 4 (SINGLY). Let be a partial node. If , then SINGLY fails. Otherwise SINGLY returns a list with , each empty or full, the empty ones before the full ones, and .
Proof. By induction on the height of . A partial node is not a leaf, so let be its children.
P-node, lines L10 and L11. Take a right-full frontier . Each is a contiguous piece of . If two children , are partial with first in the order, then has an element of that comes before an element of that is not in , by Lemma 3, which a right-full sequence does not allow. So there is at most one partial child, and if there are two or more, and line L10 fails. Suppose there is at most one, . In a right-full , every empty child comes before every child with an element of , every full child comes after every child with an element not in , and is itself right-full. Conversely, every concatenation that puts the empty children first in any order, then a right-full frontier of if it exists, then the full children in any order, is right-full and is a frontier of . So , with absent factors left out. If the set is empty, and SINGLY fails by induction. Otherwise induction replaces the middle factor by of the list SINGLY, which is the list of line L11. The trees and are empty and full. The list has at least 2 entries: is partial, so it has an element of and an element not in , and either both groups are present, or is present and contributes at least 2 entries.
Q-node, lines L12 and L13. A frontier is or . Read in the chosen direction, the frontier is right-full if and only if the labels read some empty children, then at most one partial child, then some full children, and the frontier of the partial child is right-full: this is the same argument as for the P-node, with the order fixed. Both directions cannot satisfy the label condition at once. If they did, the label sequence and its reverse would both be monotone in the order empty, partial, full, so all labels would be equal; they are not all empty or all full, since is partial, and not all partial, since and at most one is partial. So if neither direction satisfies the condition, and line L12 fails. Otherwise for the one valid direction , and induction on gives the list of line L13, or a failure exactly when this set is empty. Its entries are the empty and full children and the entries of SINGLY, in the order empty before full, and there are at least of them if exists and otherwise.
Lemma 5 (ROOT). Let be a node with such that either has at least two non-empty children, or has exactly one non-empty child and it is full. Then ROOT fails if , and otherwise returns a tree with and .
Proof. No child contains all of and is partial: if a child is partial and , then is the only non-empty child, which the hypothesis excludes. Take in which is consecutive, and let be the block of positions of in . Each child occupies a contiguous segment of . A child whose segment lies inside is full, and one disjoint from is empty. A partial child has a segment that meets and its complement, so the segment contains one end of and sticks out on that side; it cannot contain both ends, since then . Each end of lies in one segment, so there are at most two partial children, the non-empty children have adjacent segments, the partial ones are the outer ones of these, and the left one has a right-full frontier while the right one has the reverse of a right-full frontier. Conversely, any frontier of of this shape has consecutive.
P-node, lines L5 to L7. If there are three or more partial children, and line L5 fails. If there is none, the condition says only that the full children are adjacent, in any order, among the empty ones in any order, which is , the tree of line L7. If there are partial children and possibly , the frontiers in place the block " right-full, the full children in any order, reversed right-full" or its mirror image with and swapped, as one contiguous piece among the empty children in any order. By Lemma 4 and Lemma 1, the block sequences in the first orientation are for the list of line L6, and those of the second are, by Lemma 1 applied to each entry, the reverses of the reversed list. By Lemma 2 these together are , so . If SINGLY fails for or , Lemma 4 says that that child has no right-full frontier, and by Lemma 1 none whose reverse is right-full, so , and the failure is correct.
Q-node, lines L8 and L9. In each of the two directions, the shape above says: the non-empty children are consecutive in the list of children, the inner ones are full, the left outer one, if partial, has a right-full frontier, and the right outer one, if partial, the reverse of one. The label part of this condition is the same in both directions, since reversing a list keeps adjacency and the set of inner positions. If it fails, line L8 fails and . If it holds, the frontiers in the given direction are of the list of line L9, by Lemma 4 for the outer partial children, and those in the reverse direction are the reverses, by Lemma 1. So by Lemma 2. A failure of SINGLY again means .
Theorem. REDUCE returns a tree with , every P-node of has at least 2 children and every Q-node at least 3, and REDUCE fails exactly when this set is empty. REDUCE terminates after steps.
Proof. If , is consecutive in every sequence, and line L1 returns . Otherwise the walk of line L3 keeps the invariant : it holds at the root, and it moves to only when is the only non-empty child of . When it stops, is not a leaf, since , and meets the hypothesis of Lemma 5: either has at least two non-empty children, or one non-empty child that is full. (If is full, all of its at least 2 children are full.) Let be the nodes of the walk. For , , and is a contiguous piece of every frontier of , so is consecutive in a frontier of if and only if it is consecutive in the piece of . Hence replacing by a tree with frontiers turns into a tree with frontiers , and by induction from , where Lemma 5 applies, up to , the result is . A failure at line L4 means , so by the same equivalence. The node rules hold because every node built is normalized and the other nodes are unchanged.
For termination, line L2 visits each node once. The walk of line L3 goes down one level per step. ROOT is called once, and each call of SINGLY is on a proper descendant of the node that calls it, a different node each time, so there are fewer calls than nodes. A tree with leaves in which every inner node has at least 2 children has at most nodes, and each call does work proportional to the number of children of its node, so the total is .
Corollary. Starting from UNIVERSAL and reducing by in turn gives a tree whose frontiers are exactly the orders of in which every is consecutive, or a failure exactly when no such order exists. This follows from the theorem by induction on , since is the set of all orders of .