In solving a problem, it is often difficult to obtain the optimal solution due to strict constraints, making it computationally expensive. Lagrange Relaxation is a method that loosens some particular constraints of the problem with the objective to optimize the findings. This is done by introducing Lagrange multipliers for those relaxed constraints. The solution to the problem with the relaxed constraints is then used as the boundary (both upper and lower) for the original problem.
The Aliens problem from IOI 2016 brought significant attention for this method, making it also known as Aliens' Trick. Ever since, this method has started to gain popularity among competitive programmers. In this blog, we will delve deeper into this method; especially in solving the Aliens problem. If you are not familiar with the Convex Hull Trick yet, I suggest you to read my blog here.
Motivational Problem: IOI 2016 - Aliens
We have a low-resolution photo of a remote planet's area, which is divided into an KaTeX can only parse string typed expression
m×m grid consisting of KaTeX can only parse string typed expression
n points that we are interested in. Our satellite, passing over the main diagonal of the grid, can take high-resolution photos of square areas. Each square's opposite corners must be on the grid's main diagonal. The satellite can take at most KaTeX can only parse string typed expression
k high-resolution photos. Our task is to choose up to KaTeX can only parse string typed expression
k square areas to photograph ensuring:
- Every cell with a point of interest is captured.
- The total number of photographed cells is minimized.
We want to find the smallest possible total number of photographed cells.
Constraints
KaTeX can only parse string typed expression
1≤k≤n≤105
KaTeX can only parse string typed expression
1≤m≤106
Subtasks
no. | points | KaTeX can only parse string typed expression n | KaTeX can only parse string typed expression m | KaTeX can only parse string typed expression k |
---|
1 - 3 | KaTeX can only parse string typed expression 25 | KaTeX can only parse string typed expression ≤500 | KaTeX can only parse string typed expression ≤1 000 | |
4 | KaTeX can only parse string typed expression 16 | KaTeX can only parse string typed expression ≤4 000 | | |
5 | KaTeX can only parse string typed expression 19 | KaTeX can only parse string typed expression ≤50 000 | | KaTeX can only parse string typed expression ≤100 |
6 | KaTeX can only parse string typed expression 40 | | | |
On contest score distribution
Naive Dynamic Programming (25 points)
Notice that due to constraint of "each square's opposite corners must be on the grid's main diagonal", we can always interchange the value of KaTeX can only parse string typed expression
ri and KaTeX can only parse string typed expression
ci as the new point is a reflection point across the main diagonal. As a result, the order of KaTeX can only parse string typed expression
ri and KaTeX can only parse string typed expression
ci is irrelevant. For each point KaTeX can only parse string typed expression
i, visualize it as a segment that spans the range KaTeX can only parse string typed expression
[li,ri]=[min(ri,ci),max(ri,ci)]. Consequently, a photo that covers a point KaTeX can only parse string typed expression
(a,a) to KaTeX can only parse string typed expression
(b,b) will capture point KaTeX can only parse string typed expression
i if and only if KaTeX can only parse string typed expression
a≤li≤ri≤b.
Further, this observation indicates that capturing a point KaTeX can only parse string typed expression
j (KaTeX can only parse string typed expression
i<j) will also encompass point KaTeX can only parse string typed expression
i if KaTeX can only parse string typed expression
lj≤li≤ri≤cj. With that, we may simply discard point KaTeX can only parse string typed expression
i. After such eliminations, all remaining points will have the property: for points KaTeX can only parse string typed expression
i and KaTeX can only parse string typed expression
j (KaTeX can only parse string typed expression
i<j), KaTeX can only parse string typed expression
ri<rj and KaTeX can only parse string typed expression
ci<cj.
Let KaTeX can only parse string typed expression
dpi,j denotes the minimum cost to cover first KaTeX can only parse string typed expression
i points with at most KaTeX can only parse string typed expression
j photos. Then,
KaTeX can only parse string typed expression
dp0,jdpi,j=0=t<imindpt,j−1+(ri−lt+1+1)2−max(rt−lt+1+1,0)2
Here is a breakdown of the components:
- The
KaTeX can only parse string typed expression
dpt,j−1 represents the cost of capturing the first KaTeX can only parse string typed expression
t points using KaTeX can only parse string typed expression
j−1 photos. Then, we aim to cover the point KaTeX can only parse string typed expression
t+1 up to point KaTeX can only parse string typed expression
i using KaTeX can only parse string typed expression
1 photo.
- The expression
KaTeX can only parse string typed expression
(ri−lt+1+1)2 calculates the cost of capturing the point KaTeX can only parse string typed expression
t+1 up to point KaTeX can only parse string typed expression
i using KaTeX can only parse string typed expression
1 photo.
- The
KaTeX can only parse string typed expression
max(rt−lt+1+1,0)2 part is accounted to an overcharging cost due to the overlap between point KaTeX can only parse string typed expression
t and point KaTeX can only parse string typed expression
t+1. Hence, it is substracted to correct the cost.
This dynamic programming solution works in KaTeX can only parse string typed expression
O(n2k).
Convex Hull Trick (60 points)
We had:
KaTeX can only parse string typed expression
dp0,jdpi,j=0=t<imindpt,j−1+(ri−lt+1+1)2−max(rt−lt+1+1,0)2
Our dynamic programming can be expressed as:
KaTeX can only parse string typed expression
dp0,jdpi,j=0=t<imin(mt,j−1⋅xi,j+ct,j−1)+Ci,j
where:
KaTeX can only parse string typed expression
mt,j−1=−2(lt+1−1)
KaTeX can only parse string typed expression
xi,j=ri
KaTeX can only parse string typed expression
ct,j−1=dpt,j−1+(lt+1−1)2−max(rt−lt+1+1,0)2
KaTeX can only parse string typed expression
Ci,j=ri2
This Convex Hull Trick approach runs about an KaTeX can only parse string typed expression
O(kn) armortized time. Note that it is not KaTeX can only parse string typed expression
O(knlogn) as we can use a KaTeX can only parse string typed expression
std::deque instead of a KaTeX can only parse string typed expression
std::vector here, given that the point queries are in increasing order.
Lagrange Relaxation (100 points)
The following solution idea is inspired by and adapted from the official solution.
Denote KaTeX can only parse string typed expression
f(i,j) to be a function that represents our dynamic programming solution, i.e. KaTeX can only parse string typed expression
f(i,j)=dpi,j. We want to relax the constraints on the number of photos taken. Let's assume we don't have the KaTeX can only parse string typed expression
k constraint and we want to apply a cost on each photo taken to make up for it. Define KaTeX can only parse string typed expression
L(i,j,λ) to be KaTeX can only parse string typed expression
f(i,j)+λj, implying an additional cost of KaTeX can only parse string typed expression
λ for each photo taken.
For a KaTeX can only parse string typed expression
λ, we may define KaTeX can only parse string typed expression
g(i) to represent another optimization problem that satisfies the following:
KaTeX can only parse string typed expression
g(i)=k=1minnL(i,k,λ)=k=1minnf(i,k)+λk=t<iming(t)+(ri−lt+1+1)2−max(rt−lt+1+1,0)2+λ
Solving KaTeX can only parse string typed expression
g(n) for a given KaTeX can only parse string typed expression
λ allows us to obtain the minimum number of photos required to capture all KaTeX can only parse string typed expression
n points and achieve the minimum cost when each photo is charged KaTeX can only parse string typed expression
λ cost, let's define it as KaTeX can only parse string typed expression
p(λ).
A function KaTeX can only parse string typed expression
f(i,j) is said to be convex if it satisfies: KaTeX can only parse string typed expression
f(i,j−1)−f(i,j)≥f(i,j)−f(i,j+1). Alternatively, we may conclude a function KaTeX can only parse string typed expression
f(i,j) to be convex if it is in the form:
KaTeX can only parse string typed expression
f(i,j)=t<iminf(t,j−1)+C(t+1,i)
and KaTeX can only parse string typed expression
C(x,y) is a Monge Array, i.e. KaTeX can only parse string typed expression
∀x,y, it holds KaTeX can only parse string typed expression
C(x,y)+C(x+1,y+1)≤C(x,y+1)+C(x+1,y). Claim that KaTeX can only parse string typed expression
f(i,j) is convex; then, we want to prove that KaTeX can only parse string typed expression
C(x,y)=(ry−lx+1)2−max(rx−1−lx+1,0)2 is a Monge Array.
Convexity of f(i, j)
Based on the previous observations, we have KaTeX can only parse string typed expression
ri<rj and KaTeX can only parse string typed expression
ci<cj for points KaTeX can only parse string typed expression
i and KaTeX can only parse string typed expression
j when KaTeX can only parse string typed expression
i<j holds.
KaTeX can only parse string typed expression
(lx+1−lx)(ry+1−ry)≥0⇒ry(lx−1)+ry+1(lx+1−1)−ry(lx+1−1)−ry+1(lx−1) ≥0⇒(ry−lx+1)2+(ry+1−lx+1+1)2−(ry−lx+1+1)2 −(ry+1−lx+1)2≤0⇒C(x,y)+C(x+1,y+1)−C(x+1,y)−C(x,y+1)≤0⇒C(x,y)+C(x+1,y+1)≤C(x+1,y)+C(x,y+1)
It is shown that KaTeX can only parse string typed expression
C(x,y) is a Monge Array; hence, KaTeX can only parse string typed expression
f(i,j) is convex.
Given that KaTeX can only parse string typed expression
f(i,j) is convex, so is KaTeX can only parse string typed expression
L(i,j,λ). This implies:
KaTeX can only parse string typed expression
p(λ)=minx s.t.L(n,x,λ)≤L(n,x+1,λ)=minx s.t.f(n,x)−f(n,x+1)≤λ
With that we may find KaTeX can only parse string typed expression
λk∈[0,m2] that satisfies KaTeX can only parse string typed expression
k≤p(λk) and KaTeX can only parse string typed expression
p(λk+1)≤k. This can be done using binary search with KaTeX can only parse string typed expression
O(nlogm) time complexity (the linear KaTeX can only parse string typed expression
n is obtained from computing KaTeX can only parse string typed expression
g(n) using Convex Hull Trick).
Last, we only need to find the exact value of KaTeX can only parse string typed expression
f(n,k), which is our original objective. The interval KaTeX can only parse string typed expression
[p(λk+1),p(λk)] signifies the range we are interested in. Considering that KaTeX can only parse string typed expression
∀i∈[p(λk+1),p(λk)−1]:f(n,i)−f(n,i+1) remains constant (slope is constant), we may use linear interpolation to calculate our answer to the original problem. Let KaTeX can only parse string typed expression
Δ=f(n,p(λk))−f(n,p(λk+1)); then,
KaTeX can only parse string typed expression
f(n,k)=f(n,p(λk))−(k−p(λk))p(λk+1)−p(λk)Δ
The overall solution operates in KaTeX can only parse string typed expression
O(nlogn+nlogm).
Lagrange Relaxation in General
Basic Idea
-
Decomposition of the Original Problem: Decompose the original problem into a blend of a simpler optimization problem and an added term to penalize constraint violations.
-
Lagrange Function Formulation: In the equation below, KaTeX can only parse string typed expression
f(x) represents the objective function, KaTeX can only parse string typed expression
g(x) denotes the constraint equations, KaTeX can only parse string typed expression
b stands for their boundaries, and KaTeX can only parse string typed expression
λ acts as the Lagrange multiplier:
KaTeX can only parse string typed expression
L(x,λ)=f(x)+λ(g(x)−b)
-
Bounds Identification: Establish both the lower and upper bounds wherein the solution to the original problem lies within the defined interval.
Importance of Convexity
-
Guaranteed Global Optima: Convexity ensures that the solution obtained from the relaxed problem, supported by Lagrange multipliers, offers a valid and global optimum bound.
-
Optimization Simplification: A convex optimization landscape simplifies the process, reducing potential local minima or maxima that can complicate the search for an optimal solution.
-
Efficient Solutions: When the Lagrange function is convex, it facilitates the efficient determination of the solution for the relaxed problem. This is important since Lagrange relaxation primarily aims to transform intricate problems into manageable sub-problems.
References
-
Chethiya Abeysinghe, "IOI 2016 - Solutions," International Olympiad in Informatics.
-
Mamnoon Siam, "Attack on Aliens," Siam's Blog. https://mamnoonsiam.github.io/posts/attack-on-aliens.html.
-
upobir, "Quadrangle Inequality Properties," Codeforces. https://codeforces.com/blog/entry/86306.
© 2023 Yohandi. All rights reserved.