Quadrangle-Inequality Optimization

Quadrangle-inequality optimization exploits decision monotonicity in a state transition and is also commonly called decision-monotonicity-optimized DP.

Basic knowledge

Consider the simplest case, in which we need to solve the following series of optimization problems:

f(i)=min⁡1≤j≤iw(j,i)(1≤i≤n)(1)f(i) = \min_{1 \leq j \leq i} w(j,i) \qquad \left(1 \leq i \leq n\right) \tag{1}

Assume that the cost function w(j,i)w(j,i) can be computed in O(1)O(1) time.

Convention

A dynamic-programming state transition can often be written as a series of optimization problems. Taking (1) as an example, these problems have a parameter ii, and both the objective function and the feasible region may depend on ii. For each problem, after the parameter ii is fixed, choose a feasible solution jj to minimize the value of the objective function. For convenience, an optimization problem with parameter ii will be called “problem ii” below; its feasible solution jj is called “decision jj”, and the value attained by the objective function at the optimal solution is called “state f(i)f(i)”. We also denote the smallest optimal decision point for problem ii by opt⁡(i)\operatorname{opt}(i).

In general, the total time complexity of these problems is O(n2)O(n^2), because for problem ii we must consider every possible decision jj. When decision monotonicity holds, the decision space can be narrowed effectively, improving the total complexity.

  • Decision monotonicity: for any i1<i2i_1 < i_2, we have opt⁡(i1)≤opt⁡(i2)\operatorname{opt}(i_1) \leq \operatorname{opt}(i_2).
Note

For a problem ii, the set of optimal decisions need not be an interval. Decision monotonicity can in fact be defined on sets of optimal decisions. For sets AA and BB, define A≤BA \leq B if and only if for every a∈Aa\in A and b∈Bb\in B, we have min⁡{a,b}∈A\min\{a,b\}\in A and max⁡{a,b}∈B\max\{a,b\}\in B. This implies the monotonicity of the smallest (and largest) optimal decision point, which is the definition used here. The conclusions below stated for the smallest optimal decision point also apply to the largest optimal decision point. However, it is possible for the smallest optimal decision of a larger problem to be strictly smaller than the largest optimal decision of a smaller problem; that is, for some i1<i2i_1 < i_2, we may have optmax(i1)>optmin(i2)\mathop{\mathrm{optmax}}(i_1) > \mathop{\mathrm{optmin}}(i_2). Therefore, code should always ensure that it computes either the smallest or the largest optimal decision point consistently.

On the other hand, the problems sharing the same smallest optimal decision form an interval. As a function of the smallest optimal decision, this interval is strictly increasing. In other words, let j1=opt⁡(i1)j_1 = \operatorname{opt}(i_1) and j2=opt⁡(i2)j_2 = \operatorname{opt}(i_2). If j1<j2j_1 < j_2, then necessarily i1<i2i_1 < i_2. Equivalently, if the problem intervals on which decisions j1<j2j_1 < j_2 can be the smallest optimal decision are respectively [lj1,rj1][l_{j_1},r_{j_1}] and [lj2,rj2][l_{j_2},r_{j_2}], then necessarily rj1<lj2r_{j_1} < l_{j_2}.

The most common way to determine decision monotonicity is through the quadrangle inequality. In different contexts, this property is also called the Monge property (when describing a matrix Aj,iA_{j,i}) or submodularity (when describing a function f([j,i])f([j,i]) whose variable is an interval).

  • Quadrangle inequality: if, for every a≤b≤c≤da\leq b\leq c\leq d,

    w(a,c)+w(b,d)≤w(a,d)+w(b,c),w(a,c)+w(b,d) \leq w(a,d)+w(b,c),

    then ww is said to satisfy the quadrangle inequality (abbreviated as “crossing no greater than containing”). If equality always holds, ww is said to satisfy the quadrangle identity.

Unless otherwise stated, we always assume a≤b≤c≤da\leq b\leq c\leq d below. The quadrangle inequality is a sufficient but not necessary condition for decision monotonicity.

Theorem 1

If ww satisfies the quadrangle inequality, then problem (1) satisfies decision monotonicity.

Proof

We can prove this by contradiction. Suppose that for some c<dc < d, we have a=opt⁡(d)<opt⁡(c)=ba = \operatorname{opt}(d) < \operatorname{opt}(c) = b. Then a<b≤c<da < b \leq c < d. By the optimality conditions, w(a,d)≤w(b,d)w(a,d) \leq w(b,d) and w(b,c)<w(a,c)w(b,c) < w(a,c). Thus, w(a,d)−w(b,d)≤0<w(a,c)−w(b,c)w(a,d) - w(b,d) \leq 0 < w(a,c) - w(b,c), contradicting the quadrangle inequality.

The quadrangle inequality can be understood as saying that, on a suitable domain, the cost function ww has a nonpositive second mixed difference ΔiΔjw(j,i)\Delta_i\Delta_jw(j,i).

Using decision monotonicity, many common algorithms can optimize their time complexity to O(nlog⁡n)O(n\log n). These algorithms differ in their applicable ranges, implementation difficulty, and running efficiency, so the appropriate algorithm must be chosen for the actual setting. This mainly depends on the properties of w(j,i)w(j,i). Unless otherwise stated, this article assumes that w(i,j)w(i,j) supports random access, meaning that w(j,i)w(j,i) can be queried or computed in O(1)O(1) time. However, w(j,i)w(j,i) is not always so easy to compute. Therefore, in addition to the basic case, we discuss methods for optimizing DP with decision monotonicity when w(j,i)w(j,i) has only the following properties:

  • Moving access: w(j,i)w(j,i) can be obtained from w(j±1,i)w(j\pm 1,i) or w(j,i±1)w(j,i\pm 1) by an O(1)O(1)-time transition. (This is similar to the situation in Mo's algorithm.)
  • Dynamic computation: computing w(j,i)w(j,i) depends on {f(j′):j′<j}\{f(j'):j' < j\}. This means that ff and ww can only be computed in order. The interval-partition problem without a limit on the number of intervals introduced below belongs to this case.

These two properties are not mutually exclusive: w(j,i)w(j,i) may require dynamic computation while supporting only moving access.

Divide and conquer

To solve all states, it is enough to solve all optimal decision points. For every 1≤i≤n1 \leq i \leq n, solve opt⁡(i)\operatorname{opt}(i) by first computing opt⁡(n/2)\operatorname{opt}(n/2), then separately computing, for 1≤i<n/21 \leq i < n/2 and for n/2<i≤nn/2 < i \leq n, opt⁡(i)\operatorname{opt}(i). At this point, opt⁡(i)\operatorname{opt}(i) for the first half is known to lie between 11 and opt⁡(n/2)\operatorname{opt}(n/2), inclusive, while opt⁡(i)\operatorname{opt}(i) for the second half is known to lie between opt⁡(n/2)\operatorname{opt}(n/2) and nn, inclusive. Process the two subintervals in the same way until the optimal decision for every problem has been computed. Recording the upper and lower search bounds during the divide-and-conquer process keeps the complexity at O(nlog⁡n)O(n\log n). The recursion tree has O(log⁡n)O(\log n) levels, and each decision point is computed at most twice at each level, so the total number of computations is O(nlog⁡n)O(n\log n).

Reference implementation
val_t w(int j, int i);  // Cost function
val_t f[N];             // Optimal value
int opt[N];             // Smallest optimal decision

// Recursively solve the problems in [l,r]
// Their smallest optimal decision points are known to lie in [opt_l, opt_r]
void calc(int l, int r, int opt_l, int opt_r) {
  int mid = (l + r) / 2;
  // Find the optimal decision point for problem mid
  for (int j = opt_l; j <= std::min(opt_r, mid); ++j) {
    if (w(j, mid) < f[mid]) {
      f[mid] = w(j, mid);
      opt[mid] = j;
    }
  }
  // Derive the decision intervals of the two parts from decision monotonicity, then recurse
  if (l < mid) calc(l, mid - 1, opt_l, opt[mid]);
  if (r > mid) calc(mid + 1, r, opt[mid], opt_r);
}

// Solve the problems on the whole interval [1,n]
void solve(int n) {
  // The array f must be cleared before every call to the recursive function
  std::fill(f + 1, f + n + 1, inf);
  // Initially, we only know that every decision for the problems in [1,n] lies in [1,n]
  calc(1, n, 1, n);
}

Besides the basic random-access case, the divide-and-conquer algorithm can also handle the case where w(j,i)w(j,i) supports only moving access. During the computation, maintain a cursor (j,i)(j,i) and the corresponding value w(j,i)w(j,i); whenever a new value is needed, move the cursor directly to the new position and update the function value. The time complexity remains O(nlog⁡n)O(n\log n). For a more detailed discussion, see the section Simplified LARSCH algorithm below. However, divide and conquer cannot handle the case where w(j,i)w(j,i) must be computed dynamically, because it cannot compute the smallest optimal decision opt⁡(n/2)\operatorname{opt}(n/2) at the midpoint before the problems in the left half have been solved.

Binary-decision queue

Notice that, for each decision point jj, the problems ii for which it can be the smallest optimal decision necessarily form an interval. A monotone queue can record the problem interval that each decision point can solve so far; the optimal solution for a problem can then be obtained naturally from the decision points stored in the queue.

More specifically, the algorithm scans decision points in order. When it reaches decision point kk, the queue stores a triple consisting of each feasible decision point jj considered so far and the left and right endpoints ljl_j and rjr_j of the interval of problems it can solve. For a problem in the interval [lj,rj][l_j,r_j], jj is the smallest optimal decision among all decision points considered so far (that is, the decision points in [1,k][1,k]). At any moment, the decision points stored in the queue need not be consecutive, but the unresolved problems [j,n][j,n] should be the disjoint union of the problem intervals stored in the queue.

To show that, during queue updates, decision point jj is the smallest optimal decision for a contiguous interval of problems ii, we need to strengthen the preceding conclusion slightly:

Corollary 1

Let opt⁡k(i)\operatorname{opt}_k(i) be the smallest optimal decision when only decisions in [1,k][1,k] are considered, for problem ii. If ww satisfies the quadrangle inequality, then for any i1<i2i_1 < i_2, we have opt⁡k(i1)≤opt⁡k(i2)\operatorname{opt}_k(i_1) \leq \operatorname{opt}_k(i_2).

Proof

Let MM be a sufficiently large positive real number. The function w′(j,i)=w(j,i)+M[j>k]w'(j,i) = w(j,i) + M[j > k] still satisfies the quadrangle inequality, where [⋅][\cdot] is the Iverson bracket. Consider the auxiliary DP with w′w' as its cost function. In the auxiliary DP, for any problem ii, a decision j>kj > k cannot be the smallest optimal decision, so opt⁡′(i)=opt⁡k′(i)=opt⁡k(i)\operatorname{opt}'(i) = \operatorname{opt}'_k(i) = \operatorname{opt}_k(i). Applying Theorem 1 to the auxiliary DP gives this corollary.

The algorithm proceeds as follows:1

  • Initially, the queue is empty. As in a monotone queue, when considering each next decision jj, perform both a dequeue and an enqueue operation.
  • Dequeue: first remove the previous problem j−1j-1 from the queue. If the right endpoint of the problem interval solvable by the decision at the front of the queue is exactly j−1j-1, pop the front directly; otherwise, update the left endpoint of the interval solvable by the front decision to jj.
  • Enqueue: to enqueue decision jj, first compare it with the decision j′j' at the back of the queue.
    • If, for problem lj′l_{j'}, the decision jj being enqueued is strictly better than the existing decision j′j', namely w(j,lj′)<w(j′,lj′)w(j,l_{j'}) < w(j',l_{j'}), pop the decision j′j' from the back. Continue until the queue is empty or the decision j′j' at the back is better than jj for problem lj′l_{j'}.
    • If the queue is empty, enqueue (j,j,n)(j,j,n), meaning that decision jj is the optimal solution for all unresolved problems.
    • If the decision j′j' at the back of the queue is no worse, for problem rj′r_{j'}, than the decision jj being enqueued, then, when rj′<nr_{j'} < n, enqueue (j,rj′+1,n)(j,r_{j'}+1,n), meaning that jj is the smallest optimal decision for the problems [rj′+1,n][r_{j'}+1,n]. Otherwise, do not enqueue jj, because it is not better than the existing decision.
    • The final case is that the decision j′j' at the back is strictly better than the decision jj being enqueued for problem lj′l_{j'}, but strictly worse for problem rj′r_{j'}. This means that there is a problem i∈(lj′,rj′]i\in(l_{j'},r_{j'}] such that the smallest optimal decision for the problems [lj′,i−1][l_{j'},i-1] is j′j' and the smallest optimal decision for the problems [i,rj′][i,r_{j'}] is jj. Therefore, use binary search to find the smallest i∈[lj′,rj′]i\in[l_{j'},r_{j'}] satisfying w(j,i)<w(j′,i)w(j,i) < w(j',i), change the right endpoint rj′r_{j'} of the interval at the back of the queue to i−1i-1, and enqueue (j,i,n)(j,i,n).
  • Once decision jj has been processed, all decisions up to jj have been processed. The decision at the front of the queue is then the smallest optimal decision for problem jj, so record the corresponding optimal solution.
Reference implementation
val_t w(int j, int i);  // Cost function
val_t f[N];             // Optimal value
int opt[N];             // Smallest optimal decision
int lt[N], rt[N];       // Problem interval [l_j,r_j] solvable by decision j

// Solve the problems on the whole interval [1,n]
void solve(int n) {
  std::deque<int> dq;  // Monotone queue storing all feasible decisions

  // Consider all problems and decisions in order; indices start at 1
  for (int j = 1; j <= n; ++j) {
    // Dequeue
    if (!dq.empty() && rt[dq.front()] < j) dq.pop_front();
    if (!dq.empty()) lt[dq.front()] = j;
    // Enqueue
    while (!dq.empty() && w(j, lt[dq.back()]) < w(dq.back(), lt[dq.back()])) {
      dq.pop_back();
    }
    if (dq.empty()) {
      lt[j] = j;
      rt[j] = n;
      dq.emplace_back(j);
    } else if (w(j, rt[dq.back()]) >= w(dq.back(), rt[dq.back()])) {
      if (rt[dq.back()] < n) {
        lt[j] = rt[dq.back()] + 1;
        rt[j] = n;
        dq.emplace_back(j);
      }
    } else {
      int ll = lt[dq.back()], rr = rt[dq.back()], i = rr;
      // Binary search
      while (ll <= rr) {
        int mm = (ll + rr) / 2;
        if (w(j, mm) < w(dq.back(), mm)) {
          i = mm;
          rr = mm - 1;
        } else {
          ll = mm + 1;
        }
      }
      rt[dq.back()] = i - 1;
      lt[j] = i;
      rt[j] = n;
      dq.emplace_back(j);
    }
    // Compute
    f[j] = w(dq.front(), j);
    opt[j] = dq.front();
  }
}

As in a monotone queue, each decision point is enqueued at most once and dequeued at most once. Dequeue is O(1)O(1), while enqueue is O(log⁡n)O(\log n) because it may require binary search, so the total time complexity is O(nlog⁡n)O(n\log n).

Because the binary-decision queue algorithm considers all problems and decision points in order, it can handle cases where w(j,i)w(j,i) requires dynamic computation. This is its advantage over divide and conquer. However, because the binary-search step in the algorithm relies on random access to w(j,i)w(j,i), it cannot handle the case where w(j,i)w(j,i) supports only moving access.

Example 1: [POI2011] Lightning Conductor

Given a sequence of length nn, a1,a2,⋯ ,ana_1,a_2,\cdots,a_n, for every 1≤i≤n1 \leq i \leq n, find the smallest nonnegative integer fif_i satisfying

∀j∈[1,n]:aj≤ai+fi−∣i−j∣.\forall j\in\left[1,n\right]:a_j \leq a_i + f_i - \sqrt{|i-j|}.
Idea

Clearly, rearranging the inequality gives the desired integer fi=max⁡j{aj+∣i−j∣−ai}f_i = \max_{j}\{a_j+\sqrt{|i-j|}-a_i\}. Consider the case j≤ij \leq i first; the other case is similar. We then obtain the state transition

fi=−min⁡j≤i{−aj−i−j+ai}.f_i = -\min_{j\le i}\{-a_j-\sqrt{i-j}+a_i\}.

Since −x-\sqrt{x} is convex, it is easy to see (as described in detail later) that the function w(l,r)=−al−r−l+arw(l, r) = -a_l - \sqrt{r-l} + a_r satisfies the quadrangle inequality. Applying the algorithm above therefore solves this problem in O(nlog⁡n)O(n\log n) time.

Implementation
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#define all \
  for (auto i : {&q, &l, &r}) (*i)
using namespace std;
int n, a[500009], ans[500009];
deque<int> q, l, r;

double f(int i, int j) { return a[i] + sqrtl(j - i); }

void work() {
  all.clear();
  for (int i = 0; i < n; ++i) {
    if (q.size() && r.front() < i) all.pop_front();  // Dequeue from the front
    if (q.size()) l.front() = i;
    for (; q.size() && f(q.back(), l.back()) <= f(i, l.back());)  // Dequeue from the back
      all.pop_back();
    if (q.empty())  // Enqueue
      q.emplace_back(i), l.emplace_back(i), r.emplace_back(n);
    else if (f(q.back(), n) < f(i, n)) {
      int ll = l.back(), rr = n, mid;
      for (; ll <= rr;) {
        mid = (ll + rr) >> 1;
        if (f(q.back(), mid) < f(i, mid))
          rr = mid - 1;
        else
          ll = mid + 1;
      }
      r.back() = rr;
      q.emplace_back(i), l.emplace_back(ll), r.emplace_back(n);
    }
    ans[i] = max(ans[i], (int)(ceil(f(q.front(), i))) - a[i]);
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n; cin >> a[i++]);
  work();
  reverse(a, a + n);
  reverse(ans, ans + n);
  work();
  reverse(ans, ans + n);
  for (int i = 0; i < n; cout << ans[i++] << '\n');
}

Simplified LARSCH algorithm

The preceding two algorithms cannot handle the case where w(j,i)w(j,i) requires both dynamic computation and moving access. This section introduces an algorithm that overcomes both difficulties at once. It is a simplified version of the LARSCH algorithm2 proposed by Larmore and Schieber in 1991, hence the name simplified LARSCH algorithm. The original algorithm can solve decision-monotone DP problems in O(n)O(n) time, but its implementation is more complicated and is not covered here.

We still use divide and conquer. When solving the problems in the interval (l,r](l,r], assume that we know:

  • for problems in [1,l][1,l], the smallest optimal decision for problem ii is opt⁡(i)\operatorname{opt}(i), together with its optimal value, and
  • when only decisions in [1,l][1,l] are considered, the smallest optimal decision for problem rr is opt⁡l(r)\operatorname{opt}_l(r), together with its optimal value.

When solving the problems in (l,r](l,r] is complete, we need the smallest optimal decision and optimal value for every problem in (l,r](l,r].

Let mid\textit{mid} be the midpoint of (l,r](l,r]. The process is:

  1. Iterate over decisions i∈[opt⁡(l),opt⁡l(r)]i\in[\operatorname{opt}(l),\operatorname{opt}_l(r)], updating the smallest optimal decision and optimal value for problem mid\textit{mid}.
  2. Recursively solve the problems in (l,mid](l,\textit{mid}].
  3. Iterate over decisions i∈(l,mid]i\in(l,\textit{mid}], updating the optimal value for problem rr.
  4. Recursively solve the problems in (mid,r](\textit{mid},r].

Before recursing on the whole interval [1,n][1,n], first use decision j=1j=1 to update problems i∈{1,n}i\in\{1,n\}. The algorithm terminates when the recursion reaches l=rl=r.

Reference implementation
val_t w(int j, int i);  // Cost function
val_t f[N];             // Optimal value
int opt[N];             // Smallest optimal decision

// Update problem i with decision j
void check(int j, int i) {
  if (w(j, i) < f[i]) {
    f[i] = w(j, i);
    opt[i] = j;
  }
}

// Recursively solve the problems in (l, r]
void calc(int l, int r) {
  int mid = (l + r + 1) / 2;
  for (int j = opt[l]; j <= opt[r]; ++j) check(j, mid);
  if (mid < r) calc(l, mid);
  for (int j = l + 1; j <= mid; ++j) check(j, r);
  if (mid > l) calc(mid, r);
}

// Solve the problems on the whole interval [1, n]
void solve(int n) {
  // Clear the array f
  std::fill(f + 1, f + n + 1, inf);
  // Initialization
  check(1, 1);
  check(1, n);
  // Recursively solve the problems in (1, n]
  calc(1, n);
}

First, we can establish the correctness of this algorithm. It is enough to check that the prerequisites stated above hold before every recursive step (steps 2 and 4). By Corollary 1 from the preceding section, opt⁡(l)=opt⁡l(l)≤opt⁡l(mid)≤opt⁡l(r)\operatorname{opt}(l)=\operatorname{opt}_l(l)\le\operatorname{opt}_l(\textit{mid})\le\operatorname{opt}_l(r). Thus, after step 1, opt⁡l(mid)\operatorname{opt}_l(\textit{mid}) is known, so the prerequisites for recursively solving the problems in (l,mid](l,\textit{mid}] hold before step 2. Since {opt⁡(i):i∈[1,l]}\{\operatorname{opt}(i):i\in[1,l]\} was already known, step 2 additionally obtains the values of {opt⁡(i):i∈(l,mid]}\{\operatorname{opt}(i):i\in(l,\textit{mid}]\}, so after it {opt⁡(i):i∈[1,mid]}\{\operatorname{opt}(i):i\in[1,\textit{mid}]\} is known. At the same time, since opt⁡l(r)\operatorname{opt}_l(r) was already known, after step 3, opt⁡mid(r)\operatorname{opt}_\textit{mid}(r) is known as well. Therefore, the prerequisites for recursively solving the problems in (mid,r](\textit{mid},r] also hold before step 4.

Next, we show that the complexity remains O(nlog⁡n)O(n\log n). The recursion tree has O(log⁡n)O(\log n) levels. For each node at the same level of the recursion tree, the algorithm iterates over the decisions in [opt⁡(l),opt⁡l(r)][\operatorname{opt}(l),\operatorname{opt}_l(r)] and (l,mid](l,\textit{mid}], respectively. Since opt⁡(l)≤opt⁡l(r)≤opt⁡(r)\operatorname{opt}(l)\le\operatorname{opt}_l(r)\le\operatorname{opt}(r), each decision point is traversed O(1)O(1) times at any one level. Therefore, the total number of traversals at each level is O(n)O(n). If one access or computation of w(j,i)w(j,i) takes O(1)O(1) time, the algorithm has time complexity O(nlog⁡n)O(n\log n).

For cases where w(j,i)w(j,i) supports only moving access, the complexity of this algorithm is still O(nlog⁡n)O(n\log n). In this case, maintain separately for steps 1 and 3 a cursor (j,i)(j,i) and the current value of w(j,i)w(j,i). Whenever a new value is needed, move the cursor (j,i)(j,i) directly from its previous position to the new position and transition the value of w(j,i)w(j,i). It is easy to verify that the total number of such direct updates while traversing the recursion tree is O(nlog⁡n)O(n\log n). Therefore, the time complexity remains O(nlog⁡n)O(n\log n).

Complexity proof for non-random access

We only need to prove that the total number of cursor moves is O(nlog⁡n)O(n\log n). Let AA and BB be the cursors corresponding to steps 1 and 3, respectively. In fact, before solving the problems in (l,r](l,r], we can ensure that cursor AA is at (opt⁡(l),l)(\operatorname{opt}(l),l) and cursor BB is at (l,l)(l,l); afterward, cursor AA is at (opt⁡(r),r)(\operatorname{opt}(r),r) and cursor BB is at (r,r)(r,r).

Consider the following cursor-movement rules. In step 1, move cursor AA along the path

(opt⁡(l),l)→(opt⁡(l),mid)→(opt⁡l(r),mid)→(opt⁡(l),mid)→(opt⁡(l),l)(\operatorname{opt}(l),l)\to(\operatorname{opt}(l),\textit{mid})\to(\operatorname{opt}_l(r),\textit{mid})\to(\operatorname{opt}(l),\textit{mid})\to(\operatorname{opt}(l),l)

At this point, both cursors AA and BB are at the prescribed positions before solving the problems in (l,mid](l,\textit{mid}]. In step 2, as prescribed, cursor AA moves to (opt⁡(mid),mid)(\operatorname{opt}(\textit{mid}),\textit{mid}) and cursor BB moves to (mid,mid)(\textit{mid},\textit{mid}). In step 3, move cursor BB along the path

(mid,mid)→(l,mid)→(l,r)→(mid,r)→(mid,mid)(\textit{mid},\textit{mid}) \to (l,\textit{mid}) \to (l, r) \to (\textit{mid},r) \to (\textit{mid},\textit{mid})

At this point, both cursors AA and BB are at the prescribed positions before solving the problems in (mid,r](\textit{mid},r]. In step 4, as prescribed, cursor AA moves to (opt⁡(r),r)(\operatorname{opt}(r),r) and cursor BB moves to (r,r)(r,r). Thus both cursors are at the prescribed positions after solving the problems in (l,r](l,r]. This movement rule satisfies the requirements above, and it is sufficient to perform all computations in steps 1 and 3. Directly counting the cursor moves under this rule shows that step 1 requires

2(opt⁡l(r)−opt⁡(l))+2(mid−l)2(\operatorname{opt}_l(r) - \operatorname{opt}(l)) + 2(\textit{mid} - l)

moves, while step 3 requires

2(mid−l)+2(r−mid)=2(r−l)2(\textit{mid}-l)+2(r-\textit{mid}) = 2(r-l)

moves. Summing these move counts over all nodes of the recursion tree, and using the fact that all intervals [l,r][l,r] and [opt⁡(l),opt⁡l(r)][\operatorname{opt}(l),\operatorname{opt}_l(r)] at one level can overlap only at endpoints, shows that the total number of moves is O(nlog⁡n)O(n\log n).

The movement rule above inserts more waypoints than the cursor uses during the actual computation, so the actual number of cursor moves cannot exceed the estimate under this rule. Hence the actual number of cursor moves is also O(nlog⁡n)O(n\log n).

Because this algorithm computes the problems in (l,r](l,r] only after computing the optimal values in [1,l][1,l], including f(i)f(i), it can also handle cases where w(j,i)w(j,i) requires dynamic computation.

Interval-partition problem

Consider partitioning an interval into several subintervals. Formally, partition the given interval [1,n][1,n] into [a1,b1],⋯ ,[ak,bk][a_1,b_1],\cdots,[a_k,b_k], where a1=1a_1=1, bk=nb_k=n, and bi+1=ai+1b_{i}+1=a_{i+1} for every i<ki < k. The cost of a partition is ∑i=1kw(ai,bi)\sum_{i=1}^kw(a_i,b_i). The goal is to minimize this cost. The following 1D1D state transition can be written:

f(i)=min⁡1≤j≤if(j−1)+w(j,i)(1≤i≤n)f(i) = \min_{1\leq j\leq i} f(j-1)+w(j,i) \qquad (1\leq i\leq n)

Here, f(0)=0f(0)=0. If w(j,i)w(j,i) satisfies the quadrangle inequality, then f(j−1)+w(j,i)f(j-1)+w(j,i) necessarily satisfies it as well, because the first term contains no crossing term involving jj and ii and therefore cancels in the mixed difference. However, because the cost function depends on preceding subproblems, this transition can only be computed in order. Therefore, it cannot use the first divide-and-conquer algorithm described above and is generally suitable only for the binary-decision queue or simplified LARSCH algorithm. The time complexity is O(nlog⁡n)O(n\log n).

Fixed number of intervals

The problem above can be strengthened by fixing the number of intervals: partition the interval into exactly mm subintervals. The number of intervals after partitioning becomes one dimension of the state. The corresponding 2D1D transition is:

f(k,i)=min⁡1≤j≤if(k−1,j−1)+w(j,i)(1≤k≤m, 1≤i≤n)(2)f(k,i) = \min_{1\leq j\leq i} f(k-1,j-1)+w(j,i) \qquad (1\leq k\leq m,\ 1\leq i\leq n) \tag{2}

Here, f(0,0)=0f(0,0)=0, while f(0,i)=f(k,0)=∞f(0,i)=f(k,0)=\infty for every 1≤k≤m1\leq k\leq m and 1≤i≤n1\leq i\leq n. By the same reasoning as above, f(k−1,j−1)+w(j,i)f(k-1,j-1)+w(j,i) necessarily satisfies the quadrangle inequality. The computation of layer ii no longer depends on results from that same layer, so every layer can be computed using any algorithm described in the preceding section. The time complexity is O(mnlog⁡n)O(mn\log n).

For this problem, decision monotonicity also permits other optimization algorithms. The second approach relies on the following result and is very similar to the Knuth optimization algorithm described in detail below.

Theorem 2

If ww satisfies the quadrangle inequality, then for problem (2), opt⁡(k−1,i)≤opt⁡(k,i)≤opt⁡(k,i+1)\operatorname{opt}(k-1,i) \leq \operatorname{opt}(k,i) \leq \operatorname{opt}(k,i+1).

Proof

The second inequality is simply decision monotonicity within layer kk. The first inequality is the key point.

We prove opt⁡(k,i)≤opt⁡(k+1,i)\operatorname{opt}(k,i) \leq \operatorname{opt}(k+1,i). Suppose there are the following two partitions of [1,i][1,i] (with labels in reverse order): [ak,dk],⋯ ,[a1,d1][a_{k},d_{k}],\cdots,[a_1,d_1] and [bk+1,ck+1],⋯ ,[b1,c1][b_{k+1},c_{k+1}],\cdots,[b_1,c_1]. For every interval, its left endpoint is the smallest optimal decision for the problem ending at its right endpoint; likewise, considering all possible partitions from right to left, its right endpoint is the smallest optimal decision for the problem associated with its left endpoint. For example, djd_j and cjc_j are respectively the smallest optimal right endpoints of the first interval from the left when [aj,i][a_j,i] and [bj,i][b_j,i] are divided into jj parts. By decision monotonicity, if aj−1>bj−1a_{j-1} > b_{j-1}, equivalently dj>cjd_j > c_j, then necessarily aj>bja_j > b_j. Therefore, if the claimed inequality were false, we would have a1>b1a_1 > b_1, and induction would then prove ak>bka_{k} > b_{k}, contradicting the setup.

The first inequality can also be proved as follows. Again consider the two partitions above. If the claim is false, then a1>b1a_1 > b_1; but since ak<bka_{k} < b_{k}, we can find the smallest j>1j>1 such that aj≤bja_j \leq b_j. Then aj−1>bj−1a_{j-1} > b_{j-1}, and hence dj>cjd_j>c_j. We have found intervals satisfying aj≤bj≤cj<dja_j \leq b_j \leq c_j < d_j. Recombine the two partitions as follows. Consider the partition [bk+1,ck+1],⋯ ,[bj+1,cj+1],[bj,dj],[aj−1,dj−1],⋯ ,[a1,d1][b_{k+1},c_{k+1}],\cdots,[b_{j+1},c_{j+1}],[b_j,d_j],[a_{j-1},d_{j-1}],\cdots,[a_1,d_1], which has (k+1)(k+1) parts. By the assumed optimality,

w(bk+1,ck+1)+⋯+w(bj+1,cj+1)+w(bj,cj)+w(bj−1,cj−1)+⋯+w(b1,c1)≤w(bk+1,ck+1)+⋯+w(bj+1,cj+1)+w(bj,dj)+w(aj−1,dj−1)+⋯+w(a1,d1).\begin{aligned} &w(b_{k+1},c_{k+1})+\cdots+w(b_{j+1},c_{j+1})+w(b_j,c_j)+w(b_{j-1},c_{j-1})+\cdots+w(b_1,c_1) \\ &\qquad \leq w(b_{k+1},c_{k+1})+\cdots+w(b_{j+1},c_{j+1})+w(b_j,d_j)+w(a_{j-1},d_{j-1})+\cdots+w(a_1,d_1). \end{aligned}

Likewise, consider the partition [ak,dk],⋯ ,[aj+1,dj+1],[aj,cj],[bj−1,cj−1],⋯ ,[b1,c1][a_{k},d_{k}],\cdots,[a_{j+1},d_{j+1}],[a_j,c_j],[b_{j-1},c_{j-1}],\cdots,[b_1,c_1], which has kk parts. Then

w(ak,dk)+⋯+w(aj+1,dj+1)+w(aj,dj)+w(aj−1,dj−1)+⋯+w(a1,d1)<w(ak,dk)+⋯+w(aj+1,dj+1)+w(aj,cj)+w(bj−1,cj−1)+⋯+w(b1,c1).\begin{aligned} &w(a_{k},d_{k})+\cdots+w(a_{j+1},d_{j+1})+w(a_j,d_j)+w(a_{j-1},d_{j-1})+\cdots+w(a_1,d_1) \\ &\qquad < w(a_{k},d_{k})+\cdots+w(a_{j+1},d_{j+1})+w(a_j,c_j)+w(b_{j-1},c_{j-1})+\cdots+w(b_1,c_1). \end{aligned}

The inequality is strict because a1>b1a_1 > b_1, while by assumption a1a_1 is the smallest optimal left endpoint among the last intervals of all kk-part partitions. Adding the two inequalities gives w(bj,cj)+w(aj,dj)<w(bj,dj)+w(aj,cj)w(b_j,c_j) + w(a_j,d_j) < w(b_j,d_j) + w(a_j,c_j), contradicting the quadrangle inequality. The original claim follows.

Using this result, we can restrict the search range of decision jj. In the implementation, traverse kk forward and ii backward; brute-force search over jj within the previously established bounds guarantees O(n(n+m))O(n(n+m)) time.

Note

The complexity here is not O(nm)O(nm). The correct calculation must account for the n×mn\times m state matrix. For problem (i,k)(i,k), only decisions satisfying opt⁡(k−1,i)≤j≤opt⁡(k,i+1)\operatorname{opt}(k-1,i) \leq j \leq \operatorname{opt}(k,i+1) need to be considered, so the total number of decisions scanned by the problems on each antidiagonal (that is, with a fixed i−ki-k) is O(n)O(n). There are (n+m)(n+m) such diagonals, giving total time complexity O(n(n+m))O(n(n+m)).

The final optimization method starts from the following observation.

Theorem 3

If ww satisfies the quadrangle inequality, then the optimal value g(k):=f(n,k)g(k):=f(n,k) of problem (2) is a convex function of kk.

Proof

We prove g(k−1)+g(k+1)≥2g(k)g(k-1) + g(k+1) \ge 2g(k). Consider optimal partitions with (k−1)(k-1) and (k+1)(k+1) segments, namely [a1,d1],⋯ ,[ak−1,dk−1][a_1,d_1],\cdots,[a_{k-1},d_{k-1}] and [b1,c1],⋯ ,[bk+1,ck+1][b_1,c_1],\cdots,[b_{k+1},c_{k+1}]. Let 1≤j≤k−11 \leq j \leq k-1 be the smallest index satisfying cj+1≤djc_{j+1} \leq d_j; it exists because ck<n=dk−1c_{k} < n = d_{k-1}. By minimality, bj+1>ajb_{j+1} > a_j. Hence aj<bj+1≤cj+1≤dja_j < b_{j+1} \leq c_{j+1} \leq d_j. As above, exchange the suffixes of the two existing partitions to obtain:

[a1,d1],⋯ ,[aj−1,dj−1],[aj,cj+1],[bj+2,cj+2],⋯ ,[bk+1,ck+1],[b1,c1],⋯ ,[bj,cj],[bj+1,dj],[aj+1,dj+1],⋯ ,[ak−1,dk−1].\begin{aligned} & [a_1,d_1],\cdots,[a_{j-1},d_{j-1}],[a_j,c_{j+1}],[b_{j+2},c_{j+2}],\cdots,[b_{k+1},c_{k+1}], \\ & [b_1,c_1],\cdots,[b_j,c_j],[b_{j+1},d_j],[a_{j+1},d_{j+1}],\cdots,[a_{k-1},d_{k-1}]. \end{aligned}

Both resulting partitions have kk segments, so optimality gives

2g(k)≤w(a1,d1)+⋯+w(aj−1,dj−1)+w(aj,cj+1)+w(bj+2,cj+2)+⋯+w(bk+1,ck+1)+w(b1,c1)+⋯+w(bj,cj)+w(bj+1,dj)+w(aj+1,dj+1)+⋯+w(ak−1,dk−1)≤w(a1,d1)+⋯+w(aj−1,dj−1)+w(aj,dj)+w(aj+1,dj+1)+⋯+w(ak−1,dk−1)+w(b1,c1)+⋯+w(bj,cj)+w(bj+1,cj+1)+w(bj+2,cj+2)+⋯+w(bk+1,ck+1)=g(k−1)+g(k+1).\begin{aligned} 2g(k) &\le w(a_1,d_1) + \cdots + w(a_{j-1},d_{j-1}) + w(a_j,c_{j+1}) + w(b_{j+2},c_{j+2}) + \cdots + w(b_{k+1},c_{k+1}) \\ &\quad + w(b_1,c_1) + \cdots + w(b_j,c_j) + w(b_{j+1},d_j) + w(a_{j+1},d_{j+1}) + \cdots + w(a_{k-1},d_{k-1}) \\ &\le w(a_1,d_1) + \cdots + w(a_{j-1},d_{j-1}) + w(a_j,d_j) + w(a_{j+1},d_{j+1}) + \cdots + w(a_{k-1},d_{k-1}) \\ &\quad + w(b_1,c_1) + \cdots + w(b_j,c_j) + w(b_{j+1},c_{j+1}) + w(b_{j+2},c_{j+2}) + \cdots + w(b_{k+1},c_{k+1}) \\ &= g(k-1) + g(k+1). \end{aligned}

The second inequality is exactly the quadrangle inequality, which proves the desired convexity.

This result guarantees that the problem can be solved by WQS binary search (called the Aliens trick abroad). Specifically, consider the parameterized cost function wc(j,i):=w(j,i)+cw_c(j,i):=w(j,i)+c and solve the problem without a limit on the number of intervals, obtaining the optimal value fc(n)f_c(n). As the real parameter cc increases, the number of intervals in the corresponding optimum decreases monotonically. Therefore, binary search can find the parameter for which the optimal number of intervals is exactly mm; denote this parameter by cc. The optimum of the original problem is f(n,m)=fc(n)−cmf(n,m) = f_c(n)-cm. Here, the real number cc can be viewed as the Lagrange multiplier for the interval-count constraint. The implementation has many details; see the WQS binary search page. Its time complexity is O(nlog⁡nlog⁡C)O(n\log n\log C), where CC is a constant.

The three algorithms for interval partitioning with a fixed number of intervals have different advantages at different data scales; choose one according to the specific problem.

Example 3: P4767 [IOI2000] Post Office (enhanced) P6246 [IOI2000] Post Office (further enhanced)

There are some villages beside a highway. The highway is represented by the integer axis, and each village is identified by a single integer coordinate. No two villages are at the same location. The distance between two positions is the absolute value of the difference between their integer coordinates.

Post offices will be built in some, but not necessarily all, of the villages. Choose their locations so that the sum of the distances from each village to its nearest post office is minimized.

Write a program that, given the village positions and the number of post offices, computes the minimum possible total distance from every village to its nearest post office.

Idea

Every village has a nearest post office, and each post office therefore serves a set of villages; this set is clearly an interval.

Divide the nn villages into mm intervals and choose one post office in each interval.

By elementary mathematics, for interval [i,j][i,j], the post office should be built at the ⌊i+j2⌋\left\lfloor\dfrac{i+j}2\right\rfloor-th village. Prefix sums make w(i,j)w(i,j) easy to compute.

The problem is now an interval-partition problem with a fixed number of intervals. The function ww can be shown to satisfy the quadrangle inequality, so the optimizations above apply directly.

Implementation 1, the second optimization above, complexity O(n(n+m))O(n(n+m))
#include <iostream>
constexpr int N = 3009;
constexpr int M = 309;
using namespace std;
int n, m, a[N], s[N], g[M][N], p[M][N];

int f(int i, int j) {
  int k = (i + j) >> 1;
  return a[k] * (k - i + 1) - (s[k] - s[i - 1]) + (s[j] - s[k]) -
         a[k] * (j - k);
}

int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; cin >> a[i], s[i] = s[i - 1] + a[i], ++i);
  for (int i = 1; i <= n; ++i) g[0][i] = 1 << 30, p[m + 1][i] = i;
  for (int i = 1; i <= m; ++i) p[i][0] = 1;
  for (int i = 1; i <= n; ++i)
    for (int j = m; j; --j) {
      g[j][i] = 1 << 30;
      for (int k = p[j][i - 1]; k <= p[j + 1][i]; ++k) {
        int tmp = g[j - 1][k - 1] + f(k, i);
        if (tmp < g[j][i]) g[j][i] = tmp, p[j][i] = k;
      }
    }
  cout << g[m][n];
}
Implementation 2, WQS binary search, complexity O(nlog⁡nlog⁡C)O(n\log n\log C)
#include <deque>
#include <iostream>
#define all \
  for (auto i : {&q, &l, &r}) (*i)
using namespace std;
long long n, m, a[500009], s[500009], u, v, w, sum[500009], cnt[500009];
deque<int> q, l, r;

long long f(int i, int j) {
  int k = (i + j) >> 1;
  return sum[i - 1] + v + a[k] * (k - i + 1) - (s[k] - s[i - 1]) +
         (s[j] - s[k]) - a[k] * (j - k);
}

void work() {
  all.clear();
  for (int i = 1; i <= n; ++i) {
    if (q.size() && r.front() < i) all.pop_front();  // Dequeue from the front
    if (q.size()) l.front() = i;
    for (; q.size() && f(q.back(), l.back()) >= f(i, l.back());)  // Dequeue from the back
      all.pop_back();
    if (q.empty())  // Enqueue
      q.emplace_back(i), l.emplace_back(i), r.emplace_back((int)n);
    else if (f(q.back(), n) >= f(i, n)) {
      int ll = l.back(), rr = n, mid;
      for (; ll <= rr;) {
        mid = (ll + rr) >> 1;
        if (f(q.back(), mid) >= f(i, mid))
          rr = mid - 1;
        else
          ll = mid + 1;
      }
      r.back() = rr;
      q.emplace_back(i), l.emplace_back(ll), r.emplace_back((int)n);
    }
    sum[i] = f(q.front(), i);
    cnt[i] = cnt[q.front() - 1] + 1;
  }
}

int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; cin >> a[i], s[i] = s[i - 1] + a[i], ++i);
  for (w = 2e12; u <= w;) {  // WQS binary search
    v = (u + w) >> 1;
    work();
    if (cnt[n] < m)
      w = v - 1;
    else
      u = v + 1;
  }
  v = w;
  work();
  cout << sum[n] - m * v;
}

Interval-merging problem

Another class of dynamic programs that can be optimized by the quadrangle inequality is the interval-merging problem: repeatedly merge the nn unit intervals [i,i][i,i] until obtaining [1,n][1,n]. Each time [j,k][j,k] and [k+1,i][k+1,i] are merged, pay cost w(j,i)w(j,i). The goal is to find a merging order with minimum total cost. The corresponding 2D1D transition is:

f(j,i)=min⁡j≤k<if(j,k)+f(k+1,i)+w(j,i)(1≤j<i≤n)(3)f(j,i) = \min_{j \leq k < i} f(j,k) + f(k+1,i) + w(j,i) \qquad (1\le j< i\le n) \tag{3}

The initial cost is f(i,i)=0f(i,i)=0. The brute-force algorithm has total complexity O(n3)O(n^3), while decision monotonicity reduces it to O(n2)O(n^2). This algorithm was first proposed by Knuth for the optimal binary search tree problem and was further studied and summarized by Yao Chufeng; internationally it is called Knuth's optimization or the Knuth–Yao speedup.

In addition to the quadrangle inequality, decision monotonicity for interval merging requires the cost function to satisfy interval-containment monotonicity.

  • Interval-containment monotonicity: if, for all a≤b≤c≤da \leq b \leq c \leq d,

    w(b,c)≤w(a,d),w(b,c) \leq w(a,d),

    then ww is said to be monotone under interval containment.

This is essentially a first-order condition on the cost function: w(j,i)w(j,i) decreases with jj and increases with ii.

Lemma 1

If ww satisfies interval-containment monotonicity and the quadrangle inequality, then the state f(j,i)f(j,i) satisfies the quadrangle inequality.

Proof

Assume without loss of generality that a≤b≤c≤da \leq b \leq c \leq d. We prove f(a,d)+f(b,c)≥f(a,c)+f(b,d)f(a,d) + f(b,c) \geq f(a,c) + f(b,d) by induction on d−ad-a. If a=ba=b or c=dc=d, the claim is an equality. In the general case, split into cases according to the position of d′=opt⁡(a,d)d'=\operatorname{opt}(a,d).

In the first case, c≤d′c \leq d' or d′<bd' < b, meaning that [b,c][b,c] is contained in either [a,d′][a,d'] or [d′+1,d][d'+1,d].

Without loss of generality, assume c≤d′c \leq d'; the other case is analogous. Then

f(a,d)+f(b,c)=f(a,d′)+f(d′+1,d)+w(a,d)+f(b,c)≥f(a,c)+f(b,d′)+f(d′+1,d)+w(a,d)≥f(a,c)+f(b,d′)+f(d′+1,d)+w(b,d)≥f(a,c)+f(b,d).\begin{aligned} f(a,d) + f(b,c) & = f(a,d') + f(d'+1,d) + w(a,d) + f(b,c) \\ & \geq f(a,c) + f(b,d') + f(d'+1,d) + w(a,d) \\ & \geq f(a,c) + f(b,d') + f(d'+1,d) + w(b,d) \\ & \geq f(a,c) + f(b,d). \end{aligned}

Here, the first inequality follows from the induction hypothesis f(a,c)+f(b,d′)≤f(a,d′)+f(b,c)f(a,c) + f(b,d') \leq f(a,d') + f(b,c), the second from interval-containment monotonicity w(b,d)≤w(a,d)w(b,d) \leq w(a,d), and the third from the optimality condition f(b,d)≤f(b,d′)+f(d′+1,d)+w(b,d)f(b,d) \leq f(b,d') + f(d'+1,d) + w(b,d).

In the second case, b≤d′<cb \leq d' < c, so d′d' lies in [b,c][b,c]. Consider the position of c′=opt⁡(b,c)c'=\operatorname{opt}(b,c).

Without loss of generality, assume c′≤d′c' \leq d', meaning that [b,c′][b,c'] is contained in [a,d′][a,d']; the other case is analogous. Then

f(a,d)+f(b,c)=f(a,d′)+f(d′+1,d)+w(a,d)+f(b,c′)+f(c′+1,c)+w(b,c)≥f(a,c′)+f(c′+1,c)+w(b,c)+f(b,d′)+f(d′+1,d)+w(a,d)≥f(a,c′)+f(c′+1,c)+w(a,c)+f(b,d′)+f(d′+1,d)+w(b,d)≥f(a,c)+f(b,d).\begin{aligned} f(a,d) + f(b,c) & = f(a,d') + f(d'+1,d) + w(a,d) + f(b,c') + f(c'+1,c) + w(b,c) \\ & \geq f(a,c') + f(c'+1,c) + w(b,c) + f(b,d') + f(d'+1,d) + w(a,d) \\ & \geq f(a,c') + f(c'+1,c) + w(a,c) + f(b,d') + f(d'+1,d) + w(b,d) \\ & \geq f(a,c) + f(b,d). \end{aligned}

Here, the first inequality follows from the induction hypothesis f(a,c′)+f(b,d′)≤f(a,d′)+f(b,c′)f(a,c') + f(b,d') \leq f(a,d') + f(b,c'), the second from the quadrangle inequality w(a,c)+w(b,d)≤w(a,d)+w(b,c)w(a,c) + w(b,d) \leq w(a,d) + w(b,c), and the third from the optimality conditions for f(a,c)f(a,c) and f(b,d)f(b,d).

Theorem 4

If ww satisfies interval-containment monotonicity and the quadrangle inequality, then the smallest optimal decision opt⁡(j,i)\operatorname{opt}(j,i) in problem (3) satisfies

opt⁡(j,i−1)≤opt⁡(j,i)≤opt⁡(j+1,i).(j+1<i)\operatorname{opt}(j,i-1) \leq \operatorname{opt}(j,i) \leq \operatorname{opt}(j+1,i). \qquad (j + 1 < i)
Proof

Lemma 1 proves that f(j,i)f(j,i) satisfies the quadrangle inequality. Therefore, the objective f(j,k)+f(k+1,i)+w(j,i)f(j,k) + f(k+1,i) + w(j,i), for fixed jj, satisfies the quadrangle inequality as a function of (k,i)(k,i), so Theorem 1 gives opt⁡(j,i−1)≤opt⁡(j,i)\operatorname{opt}(j,i-1) \leq \operatorname{opt}(j,i). Terms that do not simultaneously contain (k,i)(k,i) do not affect the quadrangle inequality. Similarly, for fixed ii, it satisfies the quadrangle inequality as a function of (k,j)(k,j), so opt⁡(j,i)≤opt⁡(j+1,i)\operatorname{opt}(j,i) \leq \operatorname{opt}(j+1,i). This proves the result.

Using this result, we can likewise restrict the search range of decision point kk. Traverse interval lengths i−j+1i-j+1 in increasing order, then traverse all intervals [j,i][j,i] of that length. Brute-force all kk between opt⁡(j,i−1)\operatorname{opt}(j,i-1) and opt⁡(j+1,i)\operatorname{opt}(j+1,i) to obtain f(j,i)f(j,i) and record opt⁡(j,i)\operatorname{opt}(j,i). For each fixed length, the total decision-space size is O(n)O(n), and there are O(n)O(n) possible lengths, so the total complexity is O(n2)O(n^2).

Reference implementation
val_t w(int j, int i);  // Cost function
val_t f[N][N];          // Optimal values
int opt[N][N];          // Smallest optimal decisions

// Solve the problem for the whole interval [1,n]
void solve(int n) {
  // Initialize
  for (int i = 1; i <= n; ++i) {
    f[i][i] = 0;
    opt[i][i] = i;
  }
  // Enumerate interval lengths
  for (int len = 2; len <= n; ++len) {
    // Enumerate all intervals of length len
    for (int j = 1, i = len; i <= n; ++j, ++i) {
      f[j][i] = inf;
      for (int k = opt[j][i - 1]; k <= opt[j + 1][i]; ++k)
        if (f[j][i] > f[j][k] + f[k + 1][i] + w(j, i)) {
          f[j][i] = f[j][k] + f[k + 1][i] + w(j, i);  // Update the state value
          opt[j][i] = k;  // Update the (smallest) optimal decision
        }
    }
  }
}

Classes of functions satisfying the quadrangle inequality

The following properties make it easier to prove that a function satisfies the quadrangle inequality.

Property 1: If w1(j,i)w_1(j,i) and w2(j,i)w_2(j,i) both satisfy the quadrangle inequality (or interval-containment monotonicity), then for any c1,c2≥0c_1,c_2\geq 0, c1w1+c2w2c_1w_1+c_2w_2 also satisfies the same property.

Property 2: If there are functions f(x)f(x) and g(x)g(x) such that w(j,i)=f(j)−g(i)w(j,i) = f(j)-g(i), then ww satisfies the quadrangle identity. If ff and gg are increasing, ww also satisfies interval-containment monotonicity.

Property 3: Let h(x)h(x) be an increasing convex function. If w(j,i)w(j,i) satisfies the quadrangle inequality and interval-containment monotonicity, then h(w(j,i))h(w(j,i)) also satisfies both.

Property 4: Let h(x)h(x) be convex. If w(j,i)w(j,i) satisfies the quadrangle identity and interval-containment monotonicity, then h(w(j,i))h(w(j,i)) satisfies the quadrangle inequality.

One clarification is needed first. Textbooks differ on the definition of a convex function; here, convex means lower-convex, that is, its first derivative is increasing when differentiable.

Proof

The first two properties follow directly from the definitions. We prove Property 3; Property 4 is analogous. Since h(x)h(x) is monotone, h(w(j,i))h(w(j,i)) automatically preserves interval-containment monotonicity. The key point is the quadrangle inequality.

Consider the mixed second difference over a≤j≤b≤c≤i≤da \leq j \leq b \leq c \leq i \leq d.

ΔiΔjh(w(j,i))=h(w(b,d))−h(w(a,c)+Δjw(j,c)+Δiw(a,i))+h(w(a,c)+Δjw(j,c)+Δiw(a,i))−h(w(a,c)+Δjw(j,c))−h(w(a,c)+Δiw(a,i))+h(w(a,c)).\begin{aligned} \Delta_i\Delta_j h\left(w(j,i)\right) &= h\left(w(b,d)\right) - h\left(w(a,c) + \Delta_jw(j,c) + \Delta_iw(a,i)\right) \\ &\quad + h\left(w(a,c) + \Delta_jw(j,c) + \Delta_iw(a,i)\right) - h\left(w(a,c) + \Delta_jw(j,c)\right) \\ &\quad - h\left(w(a,c) + \Delta_iw(a,i)\right) + h\left(w(a,c)\right). \end{aligned}

By interval-containment monotonicity, Δiw(a,i):=w(a,d)−w(a,c)≥0\Delta_iw(a,i) := w(a,d) - w(a,c) \geq 0 and Δjw(j,c):=w(b,c)−w(a,c)≤0\Delta_jw(j,c) := w(b,c) - w(a,c) \leq 0. Convexity of h(x)h(x) gives, for t1,t2≥0t_1,t_2\geq 0, h(x+t1−t2)−h(x+t1)≤h(x−t2)−h(x)h(x + t_1 - t_2) - h(x + t_1) \leq h(x - t_2) - h(x), so the last two lines are nonpositive. The quadrangle inequality gives w(b,d)≤w(a,c)+Δjw(j,c)+Δiw(a,i)=w(b,c)+w(a,d)−w(a,c)w(b,d) \leq w(a,c) + \Delta_jw(j,c) + \Delta_iw(a,i) = w(b,c) + w(a,d) - w(a,c); since h(x)h(x) is increasing, the difference in the first line is also nonpositive. Thus the total mixed second difference is nonpositive, which is the quadrangle inequality.

This proof is the discrete version of the following derivative proof.

∂2∂x∂yh(w(x,y))=h′′(w(x,y))∂∂xw(x,y)∂∂yw(x,y)+h′(w(x,y))∂2∂x∂yw(x,y)≤0.\frac{\partial^2}{\partial x\partial y}h(w(x,y)) = h''(w(x,y))\frac{\partial }{\partial x}w(x,y)\frac{\partial}{\partial y}w(x,y) + h'(w(x,y))\frac{\partial^2}{\partial x\partial y}w(x,y) \leq 0.

This is immediate when h′≥0h' \geq 0, h′′≥0h'' \geq 0, wx≤0w_x \leq 0, wy≥0w_y \geq 0, and wxy≤0w_{xy} \leq 0. Interval-containment monotonicity gives the first-order conditions on ww, while the quadrangle inequality gives the second-order condition.

Exercises

References and notes

Footnotes

  1. “Worse” and “better” in the algorithm descriptions refer to lexicographic order: compare function values first, then decision points. “Better” means either a smaller function value or the same value with a smaller decision point. ↩

  2. Larmore, Lawrence L., and Baruch Schieber. “On-line dynamic programming with applications to the prediction of RNA secondary structure.” Journal of Algorithms 12, no. 3 (1991): 490–515. ↩

This content comes from OI Wiki: Quadrangle-Inequality Optimization. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.