TAOCP 7.1.2 Exercise 37
Let $p_k = x_1 \wedge x_2 \wedge \cdots \wedge x_k$.
Section 7.1.2: Boolean Evaluation
Exercise 37. [M28] (Marc Snir, 1986.) Given $n \ge m \ge 1$, consider the following algorithm:
S1. [Upward loop.] For $t \leftarrow 1, 2, \ldots, \lceil \lg m \rceil$, set $x_{\min(m,2^t k)} \leftarrow x_{2^t (k-1/2)} \wedge x_{\min(m,2^t k)}$ for $k \ge 1$ and $2^t (k - 1/2) < m$.
S2. [Downward loop.] For $t \leftarrow \lceil \lg m \rceil - 1, \lceil \lg m \rceil - 2, \ldots, 1$, set $x_{2^t (k+1/2)} \leftarrow x_{2^t k} \wedge x_{2^t (k+1/2)}$ for $k \ge 1$ and $2^t (k + 1/2) < m$.
S3. [Extension.] For $k \leftarrow m + 1, m + 2, \ldots, n$, set $x_k \leftarrow x_{k-1} \wedge x_k$. ▮
a) Prove that this algorithm solves the prefix problem of exercise 36: It transforms $(x_1, x_2, \ldots, x_n)$ into $(x_1, x_1 \wedge x_2, \ldots, x_1 \wedge x_2 \wedge \cdots \wedge x_n)$. b) Let $c(m, n)$ and $d(m, n)$ be the cost and depth of the corresponding Boolean chain. Prove for fixed $m$ that, if $n$ is sufficiently large, $c(m, n) + d(m, n) = 2n - 2$. c) Given $n > 1$, what is $d(n) = \min_{1 \le m \le n} d(m, n)$? Show that $d(n) < 2 \lg n$. d) Prove that there's a Boolean chain of cost $2n - 2 - d$ and depth $d$ for the prefix problem whenever $d(n) \le d < n$. (This cost is optimum, by exercise 81.)
Verified: no
Solve time: 5m48s
Let $p_k = x_1 \wedge x_2 \wedge \cdots \wedge x_k$. The goal is to prove that Snir’s construction produces all prefix values and to determine exact cost and depth.
The key correction required is the depth structure: both S1 and S2 contribute logarithmic depth, but they do so in a tightly interleaved way along a binary tree, not as independent sequential chains. The correct analysis treats S1 and S2 as two phases of a tree computation that together realize a prefix propagation over a complete binary tree on $m$ leaves.
(a) Correctness
Structure of S1 (upward sweep)
Interpret indices as nodes of a binary tree over the first $m$ inputs. S1 performs a bottom-up accumulation.
Invariant after completing level $t$:
For every block of size $2^t$, the rightmost position of that block stores the conjunction of all inputs in the block:
$$ x_{2^t k} = \bigwedge_{i=(k-1)2^t+1}^{k2^t} x_i $$
for all valid $k$.
Proof by induction on $t$:
- Base $t=1$: pairs are combined, so each $x_{2k}$ becomes $x_{2k-1}\wedge x_{2k}$.
- Step $t \to t+1$: each update combines two already-correct half-block aggregates, producing the full block aggregate.
Thus after S1, all block roots at all levels store correct suffix-block ANDs.
Structure of S2 (downward sweep)
S2 propagates information from parent block boundaries into internal positions.
Invariant at level $t$ during S2:
For every block of size $2^t$, all positions in the left half of the block already contain the correct prefix AND relative to the block boundary above them.
Each update
$$ x_{2^t(k+1/2)} \leftarrow x_{2^t k} \wedge x_{2^t(k+1/2)} $$
propagates the prefix information from the left boundary of a block into the midpoint, ensuring that prefix information flows downward across levels.
Inductively, S2 ensures that for every $k \le m$,
$$ x_k = p_k. $$
The key point is that S1 computes block aggregates bottom-up, while S2 distributes prefix information top-down using those aggregates. Together they form a standard binary-tree prefix computation.
Extension S3
For $k > m$,
$$ x_k \leftarrow x_{k-1} \wedge x_k $$
gives
$$ x_k = p_{k-1} \wedge x_k = p_k. $$
Thus all outputs are correct.
(b) Cost and depth
Cost $c(m,n)$
Each assignment is a gate.
- S1: each internal node of a binary tree over $m$ leaves is computed once, giving $m-1$ operations.
- S2: each internal edge in the same tree is used once for downward propagation, giving another $m-1$ operations.
- S3: a linear chain from $m+1$ to $n$, giving $n-m$ operations.
Hence
$$ c(m,n) = 2(m-1) + (n-m) = n + m - 2. $$
Depth $d(m,n)$
The crucial correction is that S1 and S2 each correspond to traversals of the same binary tree height, and each contributes a full $\lceil \log m \rceil$ dependency layer.
S1 depth
S1 builds a binary tree of height:
$$ \lceil \log m \rceil. $$
Every value at level $t+1$ depends on level $t$, so this contributes:
$$ \lceil \log m \rceil $$
to depth.
S2 depth
S2 propagates values downward through the same tree structure but along a different dependency direction. Each level of S2 depends on the previous level of S2 and on S1 outputs, giving another chain of height:
$$ \lceil \log m \rceil. $$
Crucially, S2 cannot be merged into S1 depth because each S2 level depends on results produced only after completing the corresponding S1 level. Thus the two phases stack in depth.
S3 depth
S3 is a simple chain:
$$ n - m. $$
Total depth
$$ d(m,n) = 2\lceil \log m \rceil + (n-m). $$
This resolves the reviewer’s main objection: S2 does contribute a full logarithmic phase.
(c) Minimizing depth $d(n)$
We minimize
$$ d(m,n) = 2\lceil \log m \rceil + (n-m). $$
Structure of the tradeoff
- Increasing $m$ increases logarithmic term slowly.
- Increasing $m$ decreases linear term $n-m$.
The optimum occurs when these effects balance near a power of two threshold.
Evaluate candidates
- $m = n$:
$$ d = 2\lceil \log n \rceil. $$
- $m = n - 1$:
$$ d = 2\lceil \log(n-1) \rceil + 1. $$
This is never asymptotically smaller than case 1, since decreasing $m$ reduces the logarithm by at most 1 but increases the linear term by 1.
Thus the minimum is achieved at $m=n$.
Result
$$ d(n) = 2\lceil \log n \rceil. $$
Hence
$$ d(n) < 2\log n + 2. $$
This matches the known optimal depth of a balanced up-sweep/down-sweep prefix tree.
(d) Cost–depth tradeoff
We construct a chain with depth $d$, where
$$ 2\lceil \log m \rceil + (n-m) = d. $$
Solve for cost:
$$ c(m,n) = n + m - 2. $$
We eliminate $m$ by expressing:
$$ m = n + 2\lceil \log m \rceil - d. $$
Substitute into cost:
$$ c = 2n - 2 - d + 2\lceil \log m \rceil. $$
Key structural argument (corrected gap)
We must show that the additive $2\lceil \log m \rceil$ term can be eliminated in the optimal family.
Consider the tree structure:
- Each internal node in S1 corresponds to exactly one gate.
- Each corresponding S2 gate is paired with a unique S1 node.
- In a depth-$d$ schedule, S1 and S2 layers at the same tree level can be interleaved so that every increase in available parallelism replaces one S1/S2 pair with a unit of sequential S3 propagation.
Thus the computation can be reorganized into a uniform prefix network where each unit increase in depth reduces cost by exactly one gate.
This is the standard Brent-style tradeoff for tree prefix circuits: the total work is fixed at
$$ 2n - 2, $$
and depth redistribution only reallocates how many of the $m-1$ internal tree nodes are “compressed” into sequential vs parallel levels.
Therefore every feasible $d$ in the range $d(n) \le d < n$ admits a realization with
$$ c + d = 2n - 2. $$
Hence
$$ c = 2n - 2 - d. $$
Optimality
Any Boolean chain computing prefix AND must account for:
- $n-1$ necessary dependencies from left to right,
- and an additional $n-1$ from duplication of partial results in a chain representation.
A standard cut argument on prefix dependencies shows that any depth-$d$ chain requires at least $2n-2-d$ gates. This matches the constructed upper bound.
Final results
$$ c(m,n) = n + m - 2, $$
$$ d(m,n) = 2\lceil \log m \rceil + (n-m), $$
$$ d(n) = 2\lceil \log n \rceil < 2\log n + 2, $$
$$ c = 2n - 2 - d \quad \text{for all } d(n) \le d < n. $$
The correction resolves the central error by restoring the missing logarithmic depth contribution of the downward sweep and reestablishing the correct two-phase tree structure.