若对于 i≤i′≤j≤j′i\leq i'\leq j \leq j'i≤i′≤j≤j′,二维数组 aaa 满足如下性质:
ai,j+ai′,j′≤ai,j′+ai′,ja_{i,j} + a_{i',j'} \leq a_{i,j'} + a_{i', j}ai,j+ai′,j′≤ai,j′+ai′,j
则称数组 aaa 满足四边形不等式。
若对于 i≤i′≤j≤j′i\leq i'\leq j \leq j'i≤i′≤j≤j′,二维数组 aaa 满足如下性质:
ai′,j≤ai,j′a_{i',j} \leq a_{i,j'}ai′,j≤ai,j′
则称数组 aaa 满足关于区间包含的单调性。
动态规划中有一种常见的转移方程(一般见于区间 DP):
fi,j={mini≤k<j{x∣x=wi,j+fi,k+fk+1,j}i<j0i=j∞i>jf_{i,j}=\begin{cases} \min\limits_{i\leq k< j}\{x|x=w_{i,j}+f_{i,k}+f_{k+1,j}\}&i<j\\ 0&i=j\\ \infty&i>j\end{cases}fi,j=⎩⎨⎧i≤k<jmin{x∣x=wi,j+fi,k+fk+1,j}0∞i<ji=ji>j
如果这时 wi,jw_{i,j}wi,j 同时满足四边形不等式和区间包含单调性,则可以使用四边形不等式优化。
推导过程:
首先可以证明二维数组 fff 也满足四边形不等式,即
fi,j+fi′,j′≤fi,j′+fi′,jf_{i,j} + f_{i',j'} \leq f_{i,j'} + f_{i', j}fi,j+fi′,j′≤fi,j′+fi′,j
分类讨论:
① 若 i=i′≤j≤j′i = i'\leq j \leq j'i=i′≤j≤j′ :
fi,j+fi′,j′=fi′,j+fi,j′\begin{aligned} f_{i,j} + f_{i',j'} &= f_{i',j} + f_{i,j'}\end{aligned}fi,j+fi′,j′=fi′,j+fi,j′
② 若 i<i′≤j=j′i < i'\leq j = j'i<i′≤j=j′ :
fi,j+fi′,j′=fi,j′+fi′,j\begin{aligned} f_{i,j} + f_{i',j'} &= f_{i,j'} + f_{i',j}\end{aligned}fi,j+fi′,j′=fi,j′+fi′,j
③ 若 i<i′=j<j′i < i' =j < j'i<i′=j<j′ :
原不等式则转换为:
fi,j+fj,j′≤fi,j′+fj,j\begin{aligned} f_{i,j} + f_{j,j'} &\leq f_{i,j'} + f_{j, j} \end{aligned}fi,j+fj,j′≤fi,j′+fj,j
因 fj,j=0f_{j,j} =0fj,j=0,又转化为:
fi,j+fj,j′≤fi,j′\begin{aligned} f_{i,j} + f_{j,j'} &\leq f_{i,j'} \end{aligned}fi,j+fj,j′≤fi,j′
对于所有 iii,都有:
fi,i+1+fi+1,i+2=wi,i+1+wi+1,i+2+fi,i+fi+1,i+1+fi+1,i+1+fi+2,i+2=wi,i+1+wi+1,i+2≤wi,i+2≤fi,i+2\begin{aligned} f_{i,i+1} + f_{i+1,i+2} &= w_{i,i+1} + w_{i+1,i+2} + f_{i,i} + f_{i+1,i+1} + f_{i+1,i+1} + f_{i+2,i+2}\\&= w_{i,i+1} + w_{i+1,i+2}\\&\leq w_{i,i+2}\\&\leq f_{i, i+2}\end{aligned}fi,i+1+fi+1,i+2=wi,i+1+wi+1,i+2+fi,i+fi+1,i+1+fi+1,i+1+fi+2,i+2=wi,i+1+wi+1,i+2≤wi,i+2≤fi,i+2
设 k=maxi≤x≤j{x∣fi,j=wi,j+fi,x+fx+1,j}k=\max\limits_{i\leq x\leq j}\{x|f_{i,j} = w_{i,j} + f_{i,x} + f_{x+1,j}\}k=i≤x≤jmax{x∣fi,j=wi,j+fi,x+fx+1,j}。
此时,若 k≤i′=jk\leq i' = jk≤i′=j
假设 fk+1,j+fi′j′≤fk+1,j′f_{k+1,j} + f_{i'j'} \leq f_{k+1,j'}fk+1,j+fi′j′≤fk+1,j′ 成立,则有
fi,j+fj,j′≤wi,j+fi,k+fk+1,j+fj,j′≤wi,j′+fi,k+fk+1,j+fj,j′≤wi,j+fi,k+fk+1,j′≤fi,j′\begin{aligned} f_{i,j} + f_{j,j'} &\leq w_{i,j} + f_{i,k} + f_{k+1,j} + f_{j,j'}\\&\leq w_{i,j'} + f_{i,k} + f_{k+1,j} + f_{j,j'}\\ &\leq w_{i,j}+f_{i,k}+f_{k+1,j'}\\&\leq f_{i,j'}\end{aligned}fi,j+fj,j′≤wi,j+fi,k+fk+1,j+fj,j′≤wi,j′+fi,k+fk+1,j+fj,j′≤wi,j+fi,k+fk+1,j′≤fi,j′
若 k>i′=jk > i' = jk>i′=j
假设 fk+1,j+fi′j′≤fk+1,j′f_{k+1,j} + f_{i'j'} \leq f_{k+1,j'}fk+1,j+fi′j′≤fk+1,j′ 成立,则有
fi,j+fj,j′≤fi,j+wj,j′+fj,k+fk+1,j′≤wi,j′+fi,j+fi′,k+fk+1,j′≤wi,j+fi,k+fk+1,j′≤fi,j′\begin{aligned} f_{i,j} + f_{j,j'} &\leq f_{i,j} + w_{j,j'} + f_{j,k} + f_{k+1,j'}\\&\leq w_{i,j'} + f_{i,j} + f_{i',k} + f_{k+1,j'} \\ &\leq w_{i,j}+f_{i,k}+f_{k+1,j'}\\&\leq f_{i,j'}\end{aligned}fi,j+fj,j′≤fi,j+wj,j′+fj,k+fk+1,j′≤wi,j′+fi,j+fi′,k+fk+1,j′≤wi,j+fi,k+fk+1,j′≤fi,j′
综述,通过数学归纳法可知:
fi,j+fj,j′≤fi,j′\begin{aligned} f_{i,j} + f_{j,j'} &\leq f_{i,j'} \end{aligned}fi,j+fj,j′≤fi,j′
④ 若 i<i′<j<j′i < i' <j < j'i<i′<j<j′ :
设 k1=maxi≤x≤j′{x∣fi′,j=wi′,j+fi′,x+fx+1,j}k_1=\max\limits_{i\leq x\leq j'}\{x|f_{i',j} = w_{i',j} + f_{i',x} + f_{x+1,j}\}k1=i≤x≤j′max{x∣fi′,j=wi′,j+fi′,x+fx+1,j}。
设 k2=maxi′≤x≤j{x∣fi,j′=wi,j′+fi,x+fx+1,j′}k_2=\max\limits_{i'\leq x\leq j}\{x|f_{i,j'} = w_{i,j'} + f_{i,x} + f_{x+1,j'}\}k2=i′≤x≤jmax{x∣fi,j′=wi,j′+fi,x+fx+1,j′}。
若 k1≤k2k_1\leq k_2k1≤k2,因为 i≤k1≤j′,i′≤k2≤ji \leq k_1 \leq j', i' \leq k_2\leq ji≤k1≤j′,i′≤k2≤j,所以 i≤k1≤k2≤ji\leq k_1\leq k_2 \leq ji≤k1≤k2≤j。
所以 i≤k1≤j,i′≤k2≤j′i\leq k_1\leq j, i'\leq k_2 \leq j'i≤k1≤j,i′≤k2≤j′。
此时,
fi,j+fi′,j′≤wi,j+wi′,j′+fi,k2+fk2+1,j+fi′,k1+fk1+1,j′≤wi,j′+wi′,j+fi,k2+fk2+1,j+fi′,k1+fk1+1,j′≤wi,j′+wi′,j+fi,k2+fi′,k1+fk2+1,j+fk1+1,j′≤wi,j′+wi′,j+fi,k2+fi′,k1+fk1+1,j+fk2+1,j′≤(wi,j′+fi,k2+fk2+1,j′)+(wi′,j+fi′,k1+fk1+1,j)≤fi,j′+fi′,j\begin{aligned} f_{i,j}+f_{i',j'} &\leq w_{i,j} + w_{i',j'} + f_{i,k_2} + f_{k_2+1,j} + f_{i',k_1} + f_{k_1+1,j'}\\ &\leq w_{i,j'} +w_{i',j} + f_{i,k_2} + f_{k_2+1,j} + f_{i',k_1} + f_{k_1 + 1,j'}\\ &\leq w_{i,j'} +w_{i',j} + f_{i,k_2} + f_{i',k_1} + f_{k_2+1,j} + f_{k_1 + 1,j'}\\ &\leq w_{i,j'} +w_{i',j} + f_{i,k_2} + f_{i',k_1} + f_{k_1 + 1,j} + f_{k_2+1,j'}\\ &\leq (w_{i,j'} + f_{i,k_2} + f_{k_2+1,j'}) + (w_{i',j} + f_{i',k_1} + f_{k_1 + 1,j})\\ &\leq f_{i,j'}+f_{i',j} \end{aligned}fi,j+fi′,j′≤wi,j+wi′,j′+fi,k2+fk2+1,j+fi′,k1+fk1+1,j′≤wi,j′+wi′,j+fi,k2+fk2+1,j+fi′,k1+fk1+1,j′≤wi,j′+wi′,j+fi,k2+fi′,k1+fk2+1,j+fk1+1,j′≤wi,j′+wi′,j+fi,k2+fi′,k1+fk1+1,j+fk2+1,j′≤(wi,j′+fi,k2+fk2+1,j′)+(wi′,j+fi′,k1+fk1+1,j)≤fi,j′+fi′,j
若 k1>k2k_1> k_2k1>k2,因为 i≤k1≤j′,i′≤k2≤ji \leq k_1 \leq j', i' \leq k_2\leq ji≤k1≤j′,i′≤k2≤j,所以 i′≤k2<k1≤j′i'\leq k_2 < k_1 \leq j'i′≤k2<k1≤j′。
所以 i′≤k1≤j′,i≤k2≤ji'\leq k_1\leq j', i\leq k_2 \leq ji′≤k1≤j′,i≤k2≤j。
此时,
fi,j+fi′,j′≤wi,j+wi′,j′+fi,k1+fk1+1,j+fi′,k2+fk2+1,j′≤wi,j′+wi′,j+fi,k1+fk1+1,j+fi′,k2+fk2+1,j′≤wi,j′+wi′,j+fi,k1+fi′,k2+fk1+1,j+fk2+1,j′≤wi,j′+wi′,j+fi,k1+fi′,k2+fk2+1,j+fk1+1,j′≤(wi,j′+fi,k1+fk1+1,j′)+(wi′,j+fi′,k2+fk2+1,j)≤fi,j′+fi′,j\begin{aligned} f_{i,j}+f_{i',j'} &\leq w_{i,j} + w_{i',j'} + f_{i,k_1} + f_{k_1+1,j} + f_{i',k_2} + f_{k_2+1,j'}\\ &\leq w_{i,j'} +w_{i',j} + f_{i,k_1} + f_{k_1+1,j} + f_{i',k_2} + f_{k_2 + 1,j'}\\ &\leq w_{i,j'} +w_{i',j} + f_{i,k_1} + f_{i',k_2} + f_{k_1+1,j} + f_{k_2 + 1,j'}\\ &\leq w_{i,j'} +w_{i',j} + f_{i,k_1} + f_{i',k_2} + f_{k_2 + 1,j} + f_{k_1 + 1,j'}\\ &\leq (w_{i,j'} + f_{i,k_1} + f_{k_1+1,j'}) + (w_{i',j} + f_{i',k_2} + f_{k_2 + 1,j})\\ &\leq f_{i,j'}+f_{i',j} \end{aligned}fi,j+fi′,j′≤wi,j+wi′,j′+fi,k1+fk1+1,j+fi′,k2+fk2+1,j′≤wi,j′+wi′,j+fi,k1+fk1+1,j+fi′,k2+fk2+1,j′≤wi,j′+wi′,j+fi,k1+fi′,k2+fk1+1,j+fk2+1,j′≤wi,j′+wi′,j+fi,k1+fi′,k2+fk2+1,j+fk1+1,j′≤(wi,j′+fi,k1+fk1+1,j′)+(wi′,j+fi′,k2+fk2+1,j)≤fi,j′+fi′,j
综述,由数学归纳法可知,fi,j+fi′,j′≤fi,j′+fi′,jf_{i,j}+f_{i',j'}\leq f_{i,j'} + f_{i',j}fi,j+fi′,j′≤fi,j′+fi′,j。
根据 ①②③④,得,当 i≤i′≤j≤j′i\leq i' \leq j \leq j'i≤i′≤j≤j′ 时,fi,j+fi′,j′≤fi,j′+fi′,jf_{i,j}+f_{i',j'}\leq f_{i,j'} + f_{i',j}fi,j+fi′,j′≤fi,j′+fi′,j。
假设 ki,j=maxi≤x≤j{x∣fi,j=wi,j+fi,x+fx+1,j}k_{i,j} = \max\limits_{i\leq x\leq j}\{x|f_{i,j}=w_{i,j}+f_{i,x}+f_{x+1,j}\}ki,j=i≤x≤jmax{x∣fi,j=wi,j+fi,x+fx+1,j}
则可以推出 ki,jk_{i,j}ki,j 单调
ki−1,j≤ki,j≤ki,j+1k_{i-1,j} \leq k_{i,j} \leq k_{i,j+1}ki−1,j≤ki,j≤ki,j+1
证明:
若 i>ji > ji>j,
ki−1,j=ki,j=ki,j+1=∞k_{i-1,j} = k_{i, j} = k_{i, j+1} = \inftyki−1,j=ki,j=ki,j+1=∞
若 i=ji = ji=j,
ki,j=0<∞=ki,j+1ki,j=0<∞=ki+1,jk_{i,j} = 0 < \infty = k_{i, j+1}\\k_{i,j}= 0<\infty = k_{i+1,j}ki,j=0<∞=ki,j+1ki,j=0<∞=ki+1,j
若 i<ji < ji<j,
我们假设 fi,j,k=wi,j+fi,k+fk+1,jf_{i,j,k} = w_{i,j} + f_{i,k} + f_{k + 1, j}fi,j,k=wi,j+fi,k+fk+1,j。
则 fi,j,ki,j=fi,jf_{i,j,k_{i,j}} = f_{i,j}fi,j,ki,j=fi,j
对于任意 k≤k′<jk\leq k' < jk≤k′<j,有
fk+1,j+fk′+1,j+1≤fk+1,j+1+fk′+1,jf_{k + 1, j} + f_{k' + 1,j+1} \leq f_{k + 1, j+1} + f_{k' + 1, j}fk+1,j+fk′+1,j+1≤fk+1,j+1+fk′+1,j
等式两边增加 wi,j+fi,k+wi,j+1+fi,k′w_{i,j} + f_{i,k} + w_{i,j + 1} + f_{i,k'}wi,j+fi,k+wi,j+1+fi,k′,得
wi,j+fi,k+wi,j+1+fi,k′+fk+1,j+fk′+1,j+1≤wi,j+fi,k+wi,j+1+fi,k′+fk+1,j+1+fk′+1,jwi,j+fi,k+fk+1,j+wi,j+1+fi,k′+fk′+1,j+1≤wi,j+1+fi,k+fk+1,j+1+wi,j+fi,k′+fk′+1,jfi,j,k+fi,j+1,k′≤fi,j+1,k+fi,j,k′fi,j,k−fi,j,k′≤fi,j+1,k−fi,j+1,k′\begin{aligned} w_{i,j} + f_{i,k} + w_{i,j + 1} + f_{i,k'} + f_{k + 1, j} + f_{k' + 1,j+1} &\leq w_{i,j} + f_{i,k} + w_{i,j + 1} + f_{i,k'} + f_{k + 1, j+1} + f_{k' + 1, j}\\ w_{i,j} + f_{i,k} + f_{k + 1, j} + w_{i,j + 1} + f_{i,k'} + f_{k' + 1,j+1} &\leq w_{i,j + 1} + f_{i,k} + f_{k + 1, j+1} + w_{i,j} + f_{i,k'} + f_{k' + 1, j}\\ f_{i,j,k} + f_{i,j+1,k'} &\leq f_{i,j + 1,k} + f_{i,j,k'}\\ f_{i,j,k} - f_{i,j,k'} &\leq f_{i,j+1,k} - f_{i,j+1,k'}\end{aligned}wi,j+fi,k+wi,j+1+fi,k′+fk+1,j+fk′+1,j+1wi,j+fi,k+fk+1,j+wi,j+1+fi,k′+fk′+1,j+1fi,j,k+fi,j+1,k′fi,j,k−fi,j,k′≤wi,j+fi,k+wi,j+1+fi,k′+fk+1,j+1+fk′+1,j≤wi,j+1+fi,k+fk+1,j+1+wi,j+fi,k′+fk′+1,j≤fi,j+1,k+fi,j,k′≤fi,j+1,k−fi,j+1,k′
所以,fi,j,k′≤fi,j,kf_{i,j,k'} \leq f_{i,j,k}fi,j,k′≤fi,j,k 可以推出 fi,j+1,k′≤fi,j+1,kf_{i,j+1,k'} \leq f_{i,j+1,k}fi,j+1,k′≤fi,j+1,k,即:
fi,j,k′≤fi,j,k→fi,j+1,k′≤fi,j+1,kf_{i,j,k'} \leq f_{i,j,k} \to f_{i,j+1,k'} \leq f_{i,j+1,k}fi,j,k′≤fi,j,k→fi,j+1,k′≤fi,j+1,k
对于所有 k<si,jk < s_{i,j}k<si,j,都有 fi,j,si,j=fi,j≤fi,j,kf_{i,j,s_{i,j}} = f_{i,j} \leq f_{i,j,k}fi,j,si,j=fi,j≤fi,j,k。
则对于所有 k<si,jk< s_{i,j}k<si,j,有 fi,j+1,si,j≤fi,j+1,kf_{i,j+1,s_{i,j}} \leq f_{i,j+1,k}fi,j+1,si,j≤fi,j+1,k。
所以 si,j≤si,j+1s_{i,j} \leq s_{i,j+1}si,j≤si,j+1
对于任意 i<k≤k′i < k\leq k'i<k≤k′,有
fi,k+fi+1,k′≤fi,k′+fi+1,kf_{i, k} + f_{i + 1, k'} \leq f_{i, k'} + f_{i + 1, k}fi,k+fi+1,k′≤fi,k′+fi+1,k
等式两边增加 wi,j+fk+1,j+wi+1,j+fk′+1,jw_{i,j} + f_{k+1,j} + w_{i+1,j} + f_{k' + 1, j}wi,j+fk+1,j+wi+1,j+fk′+1,j,得
wi,j+fk+1,j+wi+1,j+fk′+1,j+fi,k+fi+1,k′≤wi,j+fk+1,j+wi+1,j+fk′+1,j+fi,k′+fi+1,kwi,j+fi,k+fk+1,j+wi+1,j+fi+1,k′+fk′+1,j≤wi,j+fi,k′+fk′+1,j+wi+1,j+fi+1,k+fk+1,jfi,j,k+fi+1,j,k′≤fi,j,k′+fi+1,j,kfi,j,k−fi,j,k′≤fi+1,j,k−fi+1,j,k′\begin{aligned} w_{i,j} + f_{k+1,j} + w_{i+1,j} + f_{k' + 1, j} + f_{i, k} + f_{i + 1, k'} &\leq w_{i,j} + f_{k+1,j} + w_{i+1,j} + f_{k' + 1, j} + f_{i, k'} + f_{i + 1, k}\\ w_{i,j} + f_{i, k} + f_{k+1,j} + w_{i+1,j} + f_{i + 1, k'} + f_{k' + 1, j} &\leq w_{i,j} + f_{i, k'} + f_{k' + 1, j} + w_{i+1,j} + f_{i + 1, k} + f_{k+1,j}\\ f_{i,j,k} + f_{i+1,j,k'} &\leq f_{i,j,k'} + f_{i+1,j,k}\\ f_{i,j,k} - f_{i,j,k'} &\leq f_{i+1,j,k} - f_{i+1,j,k'}\end{aligned}wi,j+fk+1,j+wi+1,j+fk′+1,j+fi,k+fi+1,k′wi,j+fi,k+fk+1,j+wi+1,j+fi+1,k′+fk′+1,jfi,j,k+fi+1,j,k′fi,j,k−fi,j,k′≤wi,j+fk+1,j+wi+1,j+fk′+1,j+fi,k′+fi+1,k≤wi,j+fi,k′+fk′+1,j+wi+1,j+fi+1,k+fk+1,j≤fi,j,k′+fi+1,j,k≤fi+1,j,k−fi+1,j,k′
所以,fi,j,k′≤fi,j,kf_{i,j,k'} \leq f_{i,j,k}fi,j,k′≤fi,j,k 可以推出 fi+1,j,k′≤fi+1,j,kf_{i+1,j,k'} \leq f_{i+1,j,k}fi+1,j,k′≤fi+1,j,k,即:
fi,j,k′≤fi,j,k→fi+1,j,k′≤fi+1,j,kf_{i,j,k'} \leq f_{i,j,k} \to f_{i+1,j,k'} \leq f_{i+1,j,k}fi,j,k′≤fi,j,k→fi+1,j,k′≤fi+1,j,k
对于所有 k<si,jk < s_{i,j}k<si,j,都有 fi,j,si,j=fi,j≤fi,j,kf_{i,j,s_{i,j}} = f_{i,j} \leq f_{i,j,k}fi,j,si,j=fi,j≤fi,j,k。
则对于所有 k<si,jk< s_{i,j}k<si,j,有 fi,j+1,si,j≤fi+1,j,kf_{i,j+1,s_{i,j}} \leq f_{i+1,j,k}fi,j+1,si,j≤fi+1,j,k。
所以 si,j≤si+1,js_{i,j} \leq s_{i+1,j}si,j≤si+1,j
综述,si−1,j≤si,j≤si,j+1s_{i-1,j} \leq s_{i,j} \leq s_{i,j+1}si−1,j≤si,j≤si,j+1
所以,si,j−1≤si,j≤si+1,js_{i,j-1} \leq s_{i,j} \leq s_{i+1,j}si,j−1≤si,j≤si+1,j
所以 fi,jf_{i,j}fi,j 转移方程可以转换为
fi,j={minsi,j−1≤k<si+1,j{x∣x=wi,j+fi,k+fk+1,j}i<j0i=j∞i>jf_{i,j}=\begin{cases} \min\limits_{s_{i,j-1}\leq k< s_{i+1,j}}\{x|x=w_{i,j}+f_{i,k}+f_{k+1,j}\}&i<j\\ 0&i=j\\ \infty&i>j\end{cases}fi,j=⎩⎨⎧si,j−1≤k<si+1,jmin{x∣x=wi,j+fi,k+fk+1,j}0∞i<ji=ji>j
我们缩小了 kkk 的范围,从而缩小的计算量。
最终时间复杂度为 O(n2)O(n^2)O(n2)(原时间复杂度为 O(n3)O(n^3)O(n3))