Algorithm Search⌘K

Proofs: Weighted interval scheduling dynamic programming

These are the human proofs of Weighted interval scheduling dynamic programming, at full length.

The weighted interval DP returns a disjoint set of largest weight full

This proof shows: correctness, termination.

We use the pseudocode of the page. After line L1 the intervals are I1,…,InI_1, \ldots, I_n with r1≤r2≤⋯≤rnr_1 \le r_2 \le \cdots \le r_n, all of one type, with weights wj>0w_j > 0. Say that IiI_i ends in time for IjI_j when ri≤ljr_i \le l_j for half-open and open intervals and ri<ljr_i < l_j for closed intervals; this is the test counted on line L2. For a set AA of indices let w(A)=∑i∈Awiw(A) = \sum_{i \in A} w_i. A set is feasible when its intervals are pairwise disjoint. Let OPT(j)\mathrm{OPT}(j) be the largest weight of a feasible subset of {1,…,j}\{1, \ldots, j\}, with OPT(0)=0\mathrm{OPT}(0) = 0.

Lemma 1 (disjoint means ends in time). For i<ji < j, Ii∩Ij=∅I_i \cap I_j = \emptyset if and only if IiI_i ends in time for IjI_j.

Proof. If IiI_i ends in time, every point of IiI_i is at most rir_i, and less than rir_i for half-open and open intervals, while every point of IjI_j is at least ljl_j, and greater than ljl_j for open intervals. In each type the two sets are separated, so they are disjoint. Conversely, suppose that IiI_i does not end in time: ri≥ljr_i \ge l_j for closed intervals, ri>ljr_i > l_j for the others. Since i<ji < j, ri≤rjr_i \le r_j. For closed intervals, the point max⁡(li,lj)\max(l_i, l_j) lies in both: it is at least both left ends and at most ri≤rjr_i \le r_j, because li≤ril_i \le r_i and lj≤ril_j \le r_i. For half-open and open intervals, let L=max⁡(li,lj)L = \max(l_i, l_j); then L<ri≤rjL < r_i \le r_j, since li<ril_i < r_i and lj<ril_j < r_i, and the points strictly between LL and rir_i lie in both intervals. □\square

Lemma 2 (prefix). The indices ii that end in time for IjI_j are exactly 1,…,p[j]1, \ldots, p[j], and p[j]<jp[j] < j.

Proof. If IiI_i ends in time for IjI_j, then ri<rjr_i < r_j: for closed intervals ri<lj≤rjr_i < l_j \le r_j, and for the others ri≤lj<rjr_i \le l_j < r_j. So i<ji < j in the sorted order, whatever the order of equal right ends. The test "ends in time" depends on rir_i only through a threshold, ri≤ljr_i \le l_j or ri<ljr_i < l_j, and the rir_i are sorted, so the indices that pass form a prefix, of length p[j]p[j] by line L2. Index jj itself fails, so p[j]<jp[j] < j. □\square

Lemma 3 (recurrence). For j≥1j \ge 1, OPT(j)=max⁡(OPT(j−1),wj+OPT(p[j]))\mathrm{OPT}(j) = \max(\mathrm{OPT}(j - 1), w_j + \mathrm{OPT}(p[j])).

Proof. (≥\ge) A feasible subset of {1,…,j−1}\{1, \ldots, j - 1\} is a feasible subset of {1,…,j}\{1, \ldots, j\}. If BB is a feasible subset of {1,…,p[j]}\{1, \ldots, p[j]\}, then by Lemmas 2 and 1 every interval of BB is disjoint from IjI_j, so B∪{j}B \cup \{j\} is feasible, with weight wj+w(B)w_j + w(B).

(≤\le) Let AA be a feasible subset of {1,…,j}\{1, \ldots, j\} with w(A)=OPT(j)w(A) = \mathrm{OPT}(j). If j∉Aj \notin A, then A⊆{1,…,j−1}A \subseteq \{1, \ldots, j - 1\} and w(A)≤OPT(j−1)w(A) \le \mathrm{OPT}(j - 1). If j∈Aj \in A, every other i∈Ai \in A has i<ji < j and is disjoint from IjI_j, so by Lemmas 1 and 2, i≤p[j]i \le p[j]. So A∖{j}A \setminus \{j\} is a feasible subset of {1,…,p[j]}\{1, \ldots, p[j]\}, and w(A)≤wj+OPT(p[j])w(A) \le w_j + \mathrm{OPT}(p[j]). □\square

Theorem 1 (the table). After lines L3 to L5, M[j]=OPT(j)M[j] = \mathrm{OPT}(j) for 0≤j≤n0 \le j \le n.

Proof. By induction on jj. M[0]=0=OPT(0)M[0] = 0 = \mathrm{OPT}(0). For j≥1j \ge 1, line L5 reads M[j−1]M[j - 1] and M[p[j]]M[p[j]] with p[j]<jp[j] < j by Lemma 2, which equal OPT(j−1)\mathrm{OPT}(j - 1) and OPT(p[j])\mathrm{OPT}(p[j]) by induction, so M[j]=OPT(j)M[j] = \mathrm{OPT}(j) by Lemma 3. □\square

Theorem 2 (the traceback). Lines L6 to L9 terminate and return a feasible set SS with w(S)=OPT(n)w(S) = \mathrm{OPT}(n).

Proof. Invariant at the test of line L7: (a) w(S)+M[j]=M[n]w(S) + M[j] = M[n], and (b) SS is feasible and every interval of SS is disjoint from every IiI_i with i≤ji \le j.

It holds at the start with S=∅S = \emptyset and j=nj = n. Suppose it holds with j>0j > 0.

Line L8 takes jj. Then wj+M[p[j]]≥M[j−1]w_j + M[p[j]] \ge M[j - 1], so by line L5, M[j]=wj+M[p[j]]M[j] = w_j + M[p[j]], and w(S∪{j})+M[p[j]]=w(S)+M[j]=M[n]w(S \cup \{j\}) + M[p[j]] = w(S) + M[j] = M[n], which is (a). By (b), jj is disjoint from every interval of SS, so S∪{j}S \cup \{j\} is feasible. For i≤p[j]i \le p[j], IiI_i is disjoint from IjI_j by Lemmas 2 and 1, and from every interval of SS by (b), since i≤p[j]<ji \le p[j] < j. So (b) holds with p[j]p[j] in place of jj.

Line L9 skips jj. Then wj+M[p[j]]<M[j−1]w_j + M[p[j]] < M[j - 1], so M[j]=M[j−1]M[j] = M[j - 1], and (a) holds for j−1j - 1. (b) holds for j−1j - 1 since it held for jj.

Each pass lowers jj, by Lemma 2 in the first case, so the loop stops with j=0j = 0. Then (a) gives w(S)=M[n]−M[0]=OPT(n)w(S) = M[n] - M[0] = \mathrm{OPT}(n) by Theorem 1, and (b) says that SS is feasible.

Termination of the rest. Line L1 sorts nn keys, line L2 does nn binary searches, and the loop of line L4 runs nn times. ■\blacksquare

Back to Weighted interval scheduling dynamic programming