CDQ Convolution DP: Batch Transitions Without Reading the Future
An ordinary convolution assumes both input arrays are known. In a recurrence such as
one input is the answer we are still computing. Performing a single transform on guessed values does not solve the dependency problem. CDQ schedules small completed pieces and batches their contributions to future states.
The order is the algorithm
Use half-open intervals . Before solving an interval, each accumulator inside it already contains all contributions from sources smaller than . At a leaf, no internal earlier source remains, so the accumulator becomes final.
At an internal node with midpoint :
- Solve completely.
- Add transitions from sources in to destinations in .
- Solve .
Step two is a convolution. Define for , and for , with . Coefficient of is the contribution required by destination .
Adding coefficients outside the right half is a bug. The convolution may contain left-to-left terms that have already been handled deeper in the left recursion.
trace
Choose , , and all later kernel values zero. Then .
At the root interval , solving the left half finalizes . Its contribution to destination two is . Its contribution to destination three is .
Now solve . State two is already five. Its internal transition adds to state three, making twelve. The recurrence was respected at every read.
Exact-once proof
Consider one transition with . In the recursion tree there is exactly one lowest node whose left half contains and right half contains . That node processes this transition in its convolution. Descendants cannot contain both indices in opposite halves, and ancestors have both in one child. Every contribution is therefore added once and only once.
This proof is more reusable than memorizing indices. It works for other CDQ optimizations where the middle step is a geometric sweep, a Fenwick tree, or another batch primitive.
Complexity
At a recursion level, interval lengths sum to . Using NTT, that level costs in the worst case; summing levels yields . Local temporary arrays should be released before recursing right, or reused carefully. The direct implementation’s peak storage can remain despite its many allocations.
Our header implements this exact recurrence as cdq_recurrence(kernel,n). It ignores kernel[0] deliberately because transitions must come from strictly earlier states. A nonzero self-transition requires algebraic rearrangement and an invertibility condition; it must not silently enter this routine.
When CDQ is not your fastest choice
For this particular fixed linear kernel, generating functions give , and Newton inversion computes a whole prefix in . CDQ is still valuable because the scheduling idea extends to recurrences with leaf-local processing, changing transition sets, or additional restrictions that prevent one global fraction.
Do not claim CDQ is necessary merely because an unknown array appears in a convolution. First ask whether algebra can eliminate the recurrence entirely.
Exercise. Add a known source term to every state. Derive both the CDQ initialization and the generating function , taking care of the constant term. Compare against a quadratic solver on random kernels with many zero coefficients.
Support & Share
If this article helped you, please share or support!





























































Conversation
Comments powered by Waline