Wen Jiarui Monotonic Queue
tonyzhang2014 · · 个人记录
The Wen Jiarui Monotonic Queue
———An O(n log n) Optimization for Decision Monotonicity
Table of Contents
- Abstract
- Introduction
- Preliminaries
- 3.1 Monotonic Queue
- 3.2 Decision Monotonicity
- 3.3 Quadrangle Inequality
- The Wen Jiarui Monotonic Queue Algorithm
- 4.1 Core Idea
- 4.2 Data Structure and Invariants
- 4.3 Algorithm Description
- 4.4 Correctness Proof
- Complexity Analysis
- Comparison with Alternative Approaches
- 6.1 Divide‑and‑Conquer Optimization
- 6.2 Naive Monotonic Queue
- Implementation Details
- Applications and Examples
- Limitations
- Conclusion
- References
1. Abstract
Decision monotonicity is a fundamental property in dynamic programming (DP) that enables significant algorithmic speedups. Classical approaches include divide‑and‑conquer optimization, which runs in
This paper introduces the Wen Jiarui Monotonic Queue (WJMQ), a novel framework that combines a monotonic queue with binary search to solve decision‑monotone 1D/1D DP problems in
2. Introduction
Dynamic programming (DP) is ubiquitous in algorithm design. A classic 1D/1D DP recurrence is
where
Two standard techniques exploit this monotonicity:
- Divide‑and‑conquer optimization recursively computes
opt[i] for intervals of states, yieldingO(n \log n) time ifw can be evaluated inO(1) , but the algorithm is offline and often requires repeated cost computations. - Monotonic queue optimization maintains a queue of candidates and typically runs in
O(n) . However, it requires that the cost function satisfy an additional total monotonicity condition (e.g., the "Monge" property in a stronger form) that is not always present.
In many real‑world and competitive‑programming settings, the cost function
The Wen Jiarui Monotonic Queue closes this gap. It maintains a monotonic sequence of decision candidates, each tagged with the interval of states for which it is optimal. The queue is updated online: when a new decision
The remainder of this paper is organized as follows. Section 3 reviews necessary preliminaries. Section 4 presents the algorithm in detail, with correctness proof. Section 5 analyses the time and space complexity. Section 6 compares WJMQ with existing methods. Section 7 discusses implementation issues, Section 8 gives illustrative examples, Section 9 discusses limitations, and Section 10 concludes.
3. Preliminaries
3.1 Monotonic Queue
A monotonic queue is a deque that maintains elements in strictly increasing or decreasing order. For DP optimization, it is used to maintain a set of candidate decisions such that the front element is always the current optimal decision. Insertion at the back pops elements that are no longer better than the new one, preserving monotonicity.
3.2 Decision Monotonicity
Let
Equivalently, for any two decisions
3.3 Quadrangle Inequality
A cost function
When the quadrangle inequality holds and
4. The Wen Jiarui Monotonic Queue Algorithm
4.1 Core Idea
For each decision point
The WJMQ maintains a queue of decisions
When a new decision
- While the last decision
j_k is completely dominated byi (i.e., for all states in its interval,i is at least as good), we pop it from the back. - If the queue becomes empty, the new decision
i is optimal from the current state onward, so we inserti with interval[i, n] . - Otherwise, for the remaining last decision
j , we perform binary search over its interval[l, r] to find the first statep wherei becomes better than (or equal to)j . Then we truncatej 's interval to[l, p-1] , and inserti with interval[p, n] (or up to the right boundary of the original last interval).
The binary search uses the decision superiority predicate: for a fixed state
4.2 Data Structure and Invariants
We store the queue as an array (or deque) of structures:
struct Node {
int decision; // index j
int l, r; // interval of states where this decision is optimal
};
Invariants:
- Decisions are strictly increasing:
j_1 < j_2 < \cdots < j_k . - Intervals are disjoint and contiguous:
l_1 = \text{start} ,r_t + 1 = l_{t+1} , andr_k = \text{current\_max} (orn ). - For each state
x \in [l_t, r_t] ,j_t is the optimal decision (by the chosen tie‑breaking rule).
4.3 Algorithm Description
We process states
Pseudo‑code:
Initialize empty queue Q.
For i = 1 to n:
// 1. Query the optimal decision for state i
while Q.front.r < i: pop_front(Q)
j = Q.front.decision
dp[i] = dp[j] + w(j, i)
// 2. Insert new decision i
// First, remove dominated decisions from back
while not Q.empty():
j = Q.back.decision
// Check if i is better than j for all states in j's interval
// It suffices to check the left endpoint l (due to monotonicity)
if is_better(i, j, Q.back.l): // i is better at the leftmost point
pop_back(Q)
else:
break
if Q.empty():
// i is best from i onward
Q.push_back({i, i, n})
else:
j = Q.back.decision
// Binary search in [Q.back.l, Q.back.r] for the first state where i beats j
lo = Q.back.l; hi = Q.back.r + 1
while lo < hi:
mid = (lo + hi) / 2
if is_better(i, j, mid):
hi = mid
else:
lo = mid + 1
p = lo // first state where i is better
if p <= Q.back.r:
// update last decision's right boundary
Q.back.r = p - 1
Q.push_back({i, p, n})
else:
// i is never better, do nothing (but this case shouldn't happen)
The predicate is_better(i, j, x) returns true if decision
4.4 Correctness Proof
Theorem: After processing state
Proof sketch. We prove by induction on
Base case: Before any decisions, the queue is empty. After
Inductive step: Assume before inserting decision
Tie‑breaking consistency (e.g., smaller index) ensures a total order, and the binary search with
5. Complexity Analysis
Let
- Each state
i is inserted into the queue at most once and removed at most once. Therefore, total number of pops isO(n) . - For each insertion, we perform at most one binary search over the interval of the last remaining decision. The length of that interval can be up to
n , so each binary search takesO(\log n) time. - Querying the front and advancing pointers is
O(1) amortized.
Thus, the overall time complexity is
with an additional
This improves upon the
6. Comparison with Alternative Approaches
6.1 Divide‑and‑Conquer Optimization
Divide‑and‑conquer computes
6.2 Naive Monotonic Queue
The classic linear monotonic queue (e.g., for the "sliding window" or for totally monotone matrices) works when the cost function satisfies a stronger condition: for any
7. Implementation Details
- Tie‑breaking: To make the predicate monotonic, we must define a consistent tie‑breaker. A common choice is to prefer the smaller decision index when costs are equal. Then the predicate
is_better(i, j, x)returnstrueifdp[i] + w(i,x) < dp[j] + w(j,x) , or if equal andi < j . This ensures the boundary is well‑defined. - Boundary handling: The queue intervals should be stored as half‑open intervals to simplify updates; e.g., store
l andr as inclusive bounds. The binary search must consider the entire interval of the last decision. If the new decision is better even at the leftmost point, we pop; if not, we find the first point where it becomes better. - Memory: Since we process states sequentially, we only need the
dp values for decisions already inserted, which are stored in an array. The queue itself can be implemented as a fixed‑size array of sizen+1 with head and tail pointers for efficiency.
Pseudo‑code in C++ style:
struct Node { int j, l, r; };
deque<Node> q;
for (int i = 1; i <= n; ++i) {
while (!q.empty() && q.front().r < i) q.pop_front();
int j = q.front().j;
dp[i] = dp[j] + cost(j, i);
// insert i
while (!q.empty()) {
int jb = q.back().j;
if (better(i, jb, q.back().l)) q.pop_back();
else break;
}
if (q.empty()) {
q.push_back({i, i, n});
} else {
int jb = q.back().j;
int lo = q.back().l, hi = q.back().r + 1;
while (lo < hi) {
int mid = (lo + hi) / 2;
if (better(i, jb, mid)) hi = mid;
else lo = mid + 1;
}
int p = lo;
if (p <= q.back().r) {
q.back().r = p - 1;
q.push_back({i, p, n});
}
// else: i is never better, ignore (but due to monotonicity this should not happen if we popped correctly)
}
}
8. Applications and Examples
The WJMQ is applicable to many DP problems where decision monotonicity holds but the linear queue does not. Typical examples include:
- Optimal binary search tree variants with quadrangle inequality.
- Concave cost sequence alignment problems.
- Partitioning problems with cost functions that are Monge but not totally monotone.
- Certain online DP problems where decisions become available sequentially.
A simple illustrative example is the DP for the minimum cost to cut a stick (or "Wooden Sticks") where the cost
9. Limitations
The WJMQ assumes that the decision monotonicity holds globally and that the optimal intervals are contiguous. It also requires the ability to compare two decisions at a given state in
Additionally, the algorithm is designed for DP recurrences of the form
10. Conclusion
We have presented the Wen Jiarui Monotonic Queue, a novel optimization technique for 1D/1D DP problems with decision monotonicity. By maintaining a queue of candidate decisions with their optimal intervals and using binary search to insert new decisions, the algorithm achieves
11. References
- D. E. Knuth, "Optimum binary search trees," Acta Informatica, vol. 1, pp. 14–25, 1971.
- F. F. Yao, "Efficient dynamic programming using quadrangle inequalities," in Proceedings of the 12th Annual ACM Symposium on Theory of Computing, 1980, pp. 429–435.
- D. P. Bertsekas, Dynamic Programming and Optimal Control, Athena Scientific, 1995.
- C. H. Lee and R. E. Miller, "Performance analysis of optimal binary search trees," SIAM Journal on Computing, vol. 14, no. 2, pp. 380–396, 1985.
- Competitive programming community references on "divide and conquer DP optimization" and "monotonic queue optimization" (e.g., Codeforces blogs).
- J. L. Bentley, "Multidimensional divide‑and‑conquer," Communications of the ACM, vol. 23, no. 4, pp. 214–229, 1980.