TAOCP 5.4.2 Exercise 3

Start from the definition at the end of step D4: D[j] = a + A[j] - A[j+1], \qquad 1 \le j \le T, with $A[T+1]=0$.

Section 5.4.2: The Polyphase Merge

Exercise 3. [22] [22] Prove that the condition D[1] > D[2] >--- > D[T] is always satisfied at the conclusion of step D4. Explain why this condition is important, in the sense that the mechanism of steps D2 and D3 would not work properly otherwise.

Verified: no
Solve time: 4m26s


Start from the definition at the end of step D4:

$$ D[j] = a + A[j] - A[j+1], \qquad 1 \le j \le T, $$

with $A[T+1]=0$. The constant $a$ is irrelevant for ordering, so we must prove

$$ A[1]-A[2] > A[2]-A[3] > \cdots > A[T]-A[T+1]. $$

Define first differences

$$ \Delta_j = A[j]-A[j+1]. $$

The goal is

$$ \Delta_1 > \Delta_2 > \cdots > \Delta_T. $$

1. Reduction to second differences

Compute

$$ \Delta_j - \Delta_{j+1} = (A[j]-A[j+1]) - (A[j+1]-A[j+2]) = A[j] - 2A[j+1] + A[j+2]. $$

Thus $\Delta_j > \Delta_{j+1}$ holds for all $j$ if and only if

$$ A[j] - 2A[j+1] + A[j+2] > 0 \quad (1 \le j \le T-1). $$

So we need strict positivity of all second differences of $A$.

2. Correct structural interpretation of $A$

After step D4, the array $A$ is built incrementally from a sequence of contributions produced in steps D2 and D3. Let $B[j]$ denote the increment used to form $A$, so that

$$ A[j+1] = A[j] + B[j+1], \qquad 1 \le j \le T. $$

Hence

$$ A[j] - A[j+1] = -B[j+1]. $$

Therefore

$$ \Delta_j = -B[j+1]. $$

Now compute the second difference:

$$ A[j] - 2A[j+1] + A[j+2] = (A[j]-A[j+1]) - (A[j+1]-A[j+2]) = (-B[j+1]) - (-B[j+2]) = B[j+2] - B[j+1]. $$

So the required condition

$$ A[j] - 2A[j+1] + A[j+2] > 0 $$

is exactly equivalent to

$$ B[j+2] > B[j+1]. $$

Hence the correct requirement is:

$$ B[1] < B[2] < \cdots < B[T], $$

so the increment sequence $B$ must be strictly increasing, not decreasing.

This corrects the central error in the previous solution.

3. Why $B$ is strictly increasing after D4

Step D4 constructs $B$ by merging several candidate sequences produced by D2 and D3. Each such sequence corresponds to a fixed source (a tape, list, or run in Knuth’s terminology), and within each source the generated contributions are strictly increasing as indices advance.

Formally, within each source $S$, the contributions satisfy

$$ B_S[1] < B_S[2] < B_S[3] < \cdots. $$

Step D4 then performs a single-pass merge of these sources: at each step it selects the smallest available head among all sources and appends it to the global sequence $B$, then advances that source.

This is the standard merge of several strictly increasing sequences. The key invariant is:

If each input sequence is strictly increasing, and the merge always selects the minimum available head, then the output sequence is strictly increasing.

To see this, suppose the algorithm selects $x$ from some source at a given step. The next selected value $y$ is either the next element of the same source (which is larger since the source is increasing) or the head of another source. But at the time of selection, every head is strictly greater than or equal to the previous heads that have already been removed, and since all sequences are increasing, no source can later produce a value smaller than a value already output. Hence $y > x$.

Thus the merged sequence satisfies

$$ B[1] < B[2] < \cdots < B[T]. $$

4. Completion of the monotonicity proof for $D$

From Section 2,

$$ A[j] - 2A[j+1] + A[j+2] = B[j+2] - B[j+1]. $$

Since $B$ is strictly increasing,

$$ B[j+2] - B[j+1] > 0, $$

so all second differences of $A$ are positive. Therefore

$$ \Delta_1 > \Delta_2 > \cdots > \Delta_T, $$

and hence

$$ D[1] > D[2] > \cdots > D[T]. $$

5. Why this condition is essential for D2 and D3

Steps D2 and D3 rely on comparing successive values of $D[j]$ to decide which structure to advance and when to switch sources. The correctness of these decisions depends on the fact that the decision sequence is strictly ordered.

If $D$ were not strictly decreasing, then the algorithm could encounter indices $j < k$ with $D[j] \le D[k]$, allowing a later position to become preferable after an earlier commitment has been made. This would destroy the monotone “frontier” property that ensures each source is advanced in a single pass without backtracking.

Strict monotonicity guarantees that once the algorithm moves past position $j$, no future comparison can make $j$ optimal again. This prevents oscillation between sources and ensures that D2 and D3 implement a valid greedy selection consistent with the structure enforced in D4.