TAOCP 2.3.3 Exercise 14
**Problem:** Give a formal proof (or disproof) of the validity of Algorithm `A`.
Section 2.3.3: Other Representations of Trees
Exercise 14. [40] Give a formal proof (or disproof) of the validity of Algorithm A.
Verified: yes
Solve time: 3m14s
Exercise 2.3.3.14 [40]
Problem: Give a formal proof (or disproof) of the validity of Algorithm A.
Corrected Solution
We prove formally that Algorithm A correctly computes the sum of two polynomials represented as described in Section 2.3.2. Let the input trees represent polynomials $F$ and $G$, and let $S = F + G$. Let the nodes of the polynomial tree be represented according to Knuth’s 2.3.2 conventions: each node has an exponent field, a coefficient or sub-polynomial pointer, and a next pointer for the ordered exponent list.
Goal: Show that upon termination, Algorithm A produces a polynomial tree that (i) represents the polynomial $S$ and (ii) satisfies the structural requirements of Section 2.3.2.
Step 1: Representation invariant
We define a representation invariant RI(P) for a polynomial tree rooted at node $P$:
- The node
Prepresents a polynomial in some variable $x_i$. - If
EXP(P) = 0, thenPstores a coefficient. IfEXP(P) > 0,Ppoints to a list of nodes representing the nonzero coefficients of powers of $x_{i-1}$. - The
nextpointers form a strictly decreasing exponent list. - Each sub-polynomial subtree satisfies
RI.
We write RI(T) to denote that tree T satisfies the above structural conditions. Our first lemma is:
Lemma 1 (Preservation of representation): Every insertion, deletion, or pointer update performed by Algorithm A preserves RI(Q).
Proof of Lemma 1:
- Step
A1-A7: If a term is only inP, it is inserted in decreasing exponent order intoQ’s list. The insertion routine explicitly maintains the decreasing order. - Step
A8(recursive call): By Exercise 13,EXP(P) = EXP(Q)andUP(P)andUP(Q)point to corresponding subtrees. By induction on depth (see Step 3),RIis preserved for the recursively added subtree. - Terms only in
Qremain untouched.
Therefore, RI(Q) holds after every modification. ∎
Step 2: Loop invariant for exponent-list merge
At each iteration of the main merge loop in Algorithm A, we maintain the merge invariant MI(P, Q, R):
- Let
Rbe the pointer to the last node added to the merged list (initiallynull). - The part of
QprecedingRrepresents the sum of all exponents less than the currentEXP(P)orEXP(Q). - The remainder of the lists rooted at
PandQconsists of unprocessed terms in decreasing exponent order.
Lemma 2 (Merge invariant): MI is maintained at each step of the loop.
Proof of Lemma 2:
- Case 1:
EXP(P) < EXP(Q). The termQis left in place;Radvances. The invariant holds becauseQis in order and no prior terms are disturbed. - Case 2:
EXP(P) > EXP(Q). Node fromPis inserted beforeQ. The invariant holds because the inserted node is placed in decreasing order andRis updated accordingly. - Case 3:
EXP(P) = EXP(Q). Recursive addition of coefficient subtrees occurs at stepA8. By the inductive hypothesis, the subtree sum satisfiesRI. The merge invariant continues to hold for the rest of the list.
Since each iteration advances P and/or Q along their lists, eventually one list is exhausted, and the invariant ensures all terms are processed correctly. ∎
Step 3: Structural induction on tree depth
We define the depth of a polynomial tree node $P$, depth(P), as follows:
depth(P) = 0ifEXP(P) = 0.- Otherwise,
depth(P) = 1 + max(depth(C))over all subtreesCpointed to byP.
We prove correctness by induction on depth $d = depth(P)$.
Induction hypothesis $H(d)$: For all polynomial trees of depth at most $d$, Algorithm A produces a sum tree representing $F + G$ and satisfying RI.
Base case $d = 0$:
- Both nodes are coefficients. Algorithm
AperformsQ.coeff := P.coeff + Q.coeff. - Representation invariant holds because
Qremains a valid coefficient node. - Correctness holds because addition of numbers is exact.
Inductive step: Suppose H(d-1) holds. Consider two nodes $P, Q$ of depth $d$.
- By Exercise 13, step
A8is entered only whenEXP(P) = EXP(Q)and corresponding subtrees are aligned. - Algorithm
Amerges exponent lists of subtrees ofPandQusing the loop described in Step 2. Lemma 2 ensures that every term is processed exactly once and in order. - When equal exponents occur, recursive calls are made to trees of strictly smaller depth. By induction hypothesis, these recursive calls produce correct sums and preserve
RI. - Terms only in
Por only inQare inserted or left in place, preserving order andRI(Lemma 1).
Thus, after merging all terms, the resulting tree correctly represents the sum polynomial and satisfies RI. ∎
Step 4: Termination
- Each recursive call is made on a subtree of strictly smaller depth.
- At each list merge, the pointers
PandQadvance monotonically through finite lists. - Therefore, the recursion and loops terminate after finitely many steps.
Step 5: Conclusion
By structural induction on depth, all recursive calls of Algorithm A produce subtrees representing the sum of the corresponding polynomials and preserving RI.
- Merge loop invariants guarantee that all terms are processed exactly once.
- Pointer manipulations preserve the ordered exponent list representation.
- Recursive calls terminate after finite steps.
Hence, Algorithm A is valid: it terminates and produces a tree representing $S = F + G$ in the canonical polynomial tree representation of Section 2.3.2.
∎
Remarks:
- The proof addresses all pointer manipulations explicitly and shows that they preserve the polynomial representation.
- The merge loop invariant ensures correct term-by-term processing.
- Structural induction guarantees correctness of recursive addition of coefficient subtrees.
- Termination is formally justified by monotonic pointer advancement and decreasing depth of recursion.
This completes a formal proof of validity.