Algorithm Search⌘K

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 TT be a PQ-tree over UU in which every P-node has at least 2 children and every Q-node at least 3, and let S⊆US \subseteq U. For a node xx, write L(x)L(x) for its set of leaves and Fr(x)\mathrm{Fr}(x) for its set of frontiers, as defined on the page. For sets of sequences A,BA, B, write A⋅B={ab:a∈A,b∈B}A \cdot B = \{ ab : a \in A, b \in B \}. Call a sequence over L(x)L(x) right-full if it has the form ege g where ee has no element of SS and gg has only elements of SS, and write Fr→(x)\mathrm{Fr}_{\to}(x) for the right-full frontiers of xx. Write FrS(x)\mathrm{Fr}_S(x) for the frontiers of xx in which the elements of S∩L(x)S \cap L(x) are consecutive. For a list of trees y1,…,ymy_1, \ldots, y_m, let Fr(y1,…,ym)=Fr(y1)⋯Fr(ym)\mathrm{Fr}(y_1, \ldots, y_m) = \mathrm{Fr}(y_1) \cdots \mathrm{Fr}(y_m).

Lemma 1 (reversal). For every node xx, the reverse of a frontier of xx is a frontier of xx.

Proof. By induction on the height. A leaf is its own reverse. If f=fσ(1)⋯fσ(d)f = f_{\sigma(1)} \cdots f_{\sigma(d)} for a node with children c1,…,cdc_1, \ldots, c_d, then its reverse is fˉσ(d)⋯fˉσ(1)\bar f_{\sigma(d)} \cdots \bar f_{\sigma(1)}, where fˉi∈Fr(ci)\bar f_i \in \mathrm{Fr}(c_i) 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. □\square

So, for a list of trees, the reverses of the sequences in Fr(y1,…,ym)\mathrm{Fr}(y_1, \ldots, y_m) form exactly Fr(ym,…,y1)\mathrm{Fr}(y_m, \ldots, y_1).

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 Fr\mathrm{Fr}. Hence Fr(Q(y1,…,ym))=Fr(y1,…,ym)∪Fr(ym,…,y1)\mathrm{Fr}(Q(y_1, \ldots, y_m)) = \mathrm{Fr}(y_1, \ldots, y_m) \cup \mathrm{Fr}(y_m, \ldots, y_1) for every m≥1m \ge 1, and Fr(P(y1,…,ym))\mathrm{Fr}(P(y_1, \ldots, y_m)) is the union of Fr(yσ(1),…,yσ(m))\mathrm{Fr}(y_{\sigma(1)}, \ldots, y_{\sigma(m)}) over all permutations σ\sigma, 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. □\square

Every node that REDUCE builds is normalized, so the node rules hold for its result.

Lemma 3 (labels). If xx is partial, every frontier of xx contains an element of SS and an element not in SS. A child of an empty node is empty, and a child of a full node is full.

Proof. Immediate from the definitions 0<k(x)<∣x∣0 < k(x) < \lvert x \rvert, k(x)=0k(x) = 0 and k(x)=∣x∣k(x) = \lvert x \rvert. □\square

Lemma 4 (SINGLY). Let xx be a partial node. If Fr→(x)=∅\mathrm{Fr}_{\to}(x) = \emptyset, then SINGLY(x)(x) fails. Otherwise SINGLY(x)(x) returns a list y1,…,ymy_1, \ldots, y_m with m≥2m \ge 2, each yiy_i empty or full, the empty ones before the full ones, and Fr(y1,…,ym)=Fr→(x)\mathrm{Fr}(y_1, \ldots, y_m) = \mathrm{Fr}_{\to}(x).

Proof. By induction on the height of xx. A partial node is not a leaf, so let c1,…,cdc_1, \ldots, c_d be its children.

P-node, lines L10 and L11. Take a right-full frontier f=fσ(1)⋯fσ(d)f = f_{\sigma(1)} \cdots f_{\sigma(d)}. Each fif_i is a contiguous piece of ff. If two children cic_i, cjc_j are partial with cic_i first in the order, then fif_i has an element of SS that comes before an element of fjf_j that is not in SS, 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, Fr→(x)=∅\mathrm{Fr}_{\to}(x) = \emptyset and line L10 fails. Suppose there is at most one, cpc_p. In a right-full ff, every empty child comes before every child with an element of SS, every full child comes after every child with an element not in SS, and fpf_p is itself right-full. Conversely, every concatenation that puts the empty children first in any order, then a right-full frontier of cpc_p if it exists, then the full children in any order, is right-full and is a frontier of xx. So Fr→(x)=Fr(P(empty children))⋅Fr→(cp)⋅Fr(P(full children))\mathrm{Fr}_{\to}(x) = \mathrm{Fr}(P(\text{empty children})) \cdot \mathrm{Fr}_{\to}(c_p) \cdot \mathrm{Fr}(P(\text{full children})), with absent factors left out. If Fr→(cp)=∅\mathrm{Fr}_{\to}(c_p) = \emptyset the set is empty, and SINGLY(cp)(c_p) fails by induction. Otherwise induction replaces the middle factor by Fr\mathrm{Fr} of the list SINGLY(cp)(c_p), which is the list of line L11. The trees P(empty children)P(\text{empty children}) and P(full children)P(\text{full children}) are empty and full. The list has at least 2 entries: xx is partial, so it has an element of SS and an element not in SS, and either both groups are present, or cpc_p is present and contributes at least 2 entries.

Q-node, lines L12 and L13. A frontier is f1⋯fdf_1 \cdots f_d or fd⋯f1f_d \cdots f_1. 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 dd labels would be equal; they are not all empty or all full, since xx is partial, and not all partial, since d≥3d \ge 3 and at most one is partial. So if neither direction satisfies the condition, Fr→(x)=∅\mathrm{Fr}_{\to}(x) = \emptyset and line L12 fails. Otherwise Fr→(x)=Fr(ci1)⋯Fr→(cp)⋯Fr(cid)\mathrm{Fr}_{\to}(x) = \mathrm{Fr}(c_{i_1}) \cdots \mathrm{Fr}_{\to}(c_p) \cdots \mathrm{Fr}(c_{i_d}) for the one valid direction i1,…,idi_1, \ldots, i_d, and induction on cpc_p 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(cp)(c_p), in the order empty before full, and there are at least d−1+2≥2d - 1 + 2 \ge 2 of them if cpc_p exists and d≥3d \ge 3 otherwise. □\square

Lemma 5 (ROOT). Let xx be a node with S⊆L(x)S \subseteq L(x) such that either xx has at least two non-empty children, or xx has exactly one non-empty child and it is full. Then ROOT(x)(x) fails if FrS(x)=∅\mathrm{Fr}_S(x) = \emptyset, and otherwise returns a tree x′x' with L(x′)=L(x)L(x') = L(x) and Fr(x′)=FrS(x)\mathrm{Fr}(x') = \mathrm{Fr}_S(x).

Proof. No child contains all of SS and is partial: if a child cc is partial and S⊆L(c)S \subseteq L(c), then cc is the only non-empty child, which the hypothesis excludes. Take f∈Fr(x)f \in \mathrm{Fr}(x) in which SS is consecutive, and let BB be the block of positions of SS in ff. Each child cic_i occupies a contiguous segment of ff. A child whose segment lies inside BB is full, and one disjoint from BB is empty. A partial child has a segment that meets BB and its complement, so the segment contains one end of BB and sticks out on that side; it cannot contain both ends, since then S⊆L(ci)S \subseteq L(c_i). Each end of BB 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 xx of this shape has SS consecutive.

P-node, lines L5 to L7. If there are three or more partial children, FrS(x)=∅\mathrm{Fr}_S(x) = \emptyset 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 Fr(P(empty children,P(full children)))\mathrm{Fr}(P(\text{empty children}, P(\text{full children}))), the tree of line L7. If there are partial children p1p_1 and possibly p2p_2, the frontiers in FrS(x)\mathrm{Fr}_S(x) place the block "p1p_1 right-full, the full children in any order, p2p_2 reversed right-full" or its mirror image with p1p_1 and p2p_2 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 Fr(Z)\mathrm{Fr}(Z) for the list ZZ of line L6, and those of the second are, by Lemma 1 applied to each entry, the reverses Fr\mathrm{Fr} of the reversed list. By Lemma 2 these together are Fr(Q(Z))\mathrm{Fr}(Q(Z)), so FrS(x)=Fr(P(empty children,Q(Z)))\mathrm{Fr}_S(x) = \mathrm{Fr}(P(\text{empty children}, Q(Z))). If SINGLY fails for p1p_1 or p2p_2, Lemma 4 says that that child has no right-full frontier, and by Lemma 1 none whose reverse is right-full, so FrS(x)=∅\mathrm{Fr}_S(x) = \emptyset, 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 FrS(x)=∅\mathrm{Fr}_S(x) = \emptyset. If it holds, the frontiers in the given direction are Fr\mathrm{Fr} 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 FrS(x)=Fr(Q(list))\mathrm{Fr}_S(x) = \mathrm{Fr}(Q(\text{list})) by Lemma 2. A failure of SINGLY again means FrS(x)=∅\mathrm{Fr}_S(x) = \emptyset. □\square

Theorem. REDUCE(T,S)(T, S) returns a tree T′T' with Fr(T′)={ π∈Fr(T):S is consecutive in π }\mathrm{Fr}(T') = \{\, \pi \in \mathrm{Fr}(T) : S \text{ is consecutive in } \pi \,\}, every P-node of T′T' has at least 2 children and every Q-node at least 3, and REDUCE fails exactly when this set is empty. REDUCE terminates after O(n)O(n) steps.

Proof. If ∣S∣≤1\lvert S \rvert \le 1, SS is consecutive in every sequence, and line L1 returns TT. Otherwise the walk of line L3 keeps the invariant S⊆L(x)S \subseteq L(x): it holds at the root, and it moves to cc only when cc is the only non-empty child of xx. When it stops, xx is not a leaf, since ∣L(x)∣≥∣S∣≥2\lvert L(x) \rvert \ge \lvert S \rvert \ge 2, and xx meets the hypothesis of Lemma 5: either xx has at least two non-empty children, or one non-empty child that is full. (If xx is full, all of its at least 2 children are full.) Let x0,x1,…,xt=xx_0, x_1, \ldots, x_t = x be the nodes of the walk. For s<ts < t, S⊆L(xs+1)S \subseteq L(x_{s+1}), and L(xs+1)L(x_{s+1}) is a contiguous piece of every frontier of xsx_s, so SS is consecutive in a frontier of xsx_s if and only if it is consecutive in the piece of xs+1x_{s+1}. Hence replacing xs+1x_{s+1} by a tree with frontiers FrS(xs+1)\mathrm{Fr}_S(x_{s+1}) turns xsx_s into a tree with frontiers FrS(xs)\mathrm{Fr}_S(x_s), and by induction from s=ts = t, where Lemma 5 applies, up to s=0s = 0, the result is Fr(T′)=FrS(T)\mathrm{Fr}(T') = \mathrm{Fr}_S(T). A failure at line L4 means FrS(x)=∅\mathrm{Fr}_S(x) = \emptyset, so FrS(T)=∅\mathrm{Fr}_S(T) = \emptyset 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 nn leaves in which every inner node has at least 2 children has at most 2n−12n - 1 nodes, and each call does work proportional to the number of children of its node, so the total is O(n)O(n). □\square

Corollary. Starting from UNIVERSAL(U)(U) and reducing by S1,…,SmS_1, \ldots, S_m in turn gives a tree whose frontiers are exactly the orders of UU in which every SiS_i is consecutive, or a failure exactly when no such order exists. This follows from the theorem by induction on mm, since Fr(UNIVERSAL(U))\mathrm{Fr}(\text{UNIVERSAL}(U)) is the set of all orders of UU. ■\blacksquare

Back to PQ-tree