Skip to main content

Loop invariants

More generally, to argue about the behavior of our loop, consider the following while loop:

while P do T\text{\textbf{while} } P \text{\textbf{ do }} T

where PP is a predicate whose validity depends on (a collection of) variables XX and TT be a set of program instructions. Applying instructions TT on variables XX would result in T(X)T(X). Let RR be a predicate on XX. We say that RR is a loop invariant if the following holds: P(X)&R(X)    R(T(X))P(X) \& R(X) \implies R(T(X)) That is, if the statement RR is satisfied before the program enters the loop and RR is loop-invariant, then RR must be satisfied after the program leaves the loop.

Exercise 7

Prove: If R1R_1 and R2R_2 are loop invariants for a while-loop, then R1&R2R_1 \& R_2 is also a loop invariant for the same while-loop.

Let us look at a more complicated example where proving the behavior of the program is much less obvious. We are given a collection of squares S\mathcal{S} of arbitrary sizes in the plane. Our goal is to pick as many non-overlapping (a.k.a. independent) squares as possible. How well can we solve this problem?

A collection of squares and the choice made by the algorithm (grey).

Figure 3: A collection of squares and the choice made by the algorithm (grey).

This problem is among the NP-hard problems, so we should not expect to solve it exactly by an efficient algorithm. In these notes, you will see a very simple heuristic that is always guaranteed to give at least 1/41/4-fraction of an optimal solution (i.e. if an optimal solution has rr rectangles, the heuristic reports at least r/4r/4 rectangles). The idea is quite intuitive: We should be tempted to pick squares that have smaller areas, rather than the big ones. This motivates the following algorithm.

INDSQUARES(S\mathcal{S})

  1. Initialize solution I\mathcal{I} \leftarrow \emptyset
  2. while S\mathcal{S} \neq \emptyset do
    1. Let XX be the smallest square in S\mathcal{S}
    2. II{X}\mathcal{I} \leftarrow \mathcal{I} \cup \{X\}
    3. Remove from S\mathcal{S} all its squares that overlap with XX
  3. return I\mathcal{I}

Let M\mathcal{M} denotes the maximum-cardinality of a subset of non-overlapping squares. Our goal is to argue that I\mathcal{I} contains only non-overlapping squares and that IM/4|\mathcal{I}| \geq |\mathcal{M}|/4.

Theorem 2

The following are loop invariants:

  • (R1) No squares in S\mathcal{S} overlap with I\mathcal{I}.

  • (R2) I\mathcal{I} is a set of non-overlapping squares

  • (R3) IMS/4|\mathcal{I}| \geq |\mathcal{M} - \mathcal{S}|/4

Proof:

The first is easy to prove: Each while-loop updates S\mathcal{S} and I\mathcal{I}. Suppose that (R1) holds before the loop. Now, when XX is added into I\mathcal{I}, all squares overlapping with XX are removed from S\mathcal{S}. Therefore, (R1) continues to hold after this update. The second requires the first. Suppose that (R2) holds before the loop. Since I\mathcal{I} is non-overlapping before the loop, from (R1), we know that XX cannot overlap with anything in I\mathcal{I}. Therefore, I{X}\mathcal{I} \cup \{X\} is also non-overlapping. Finally, suppose that (R3) holds before the loop, so we have that IMS/4|\mathcal{I}| \geq |\mathcal{M}- \mathcal{S}|/4 (before). We need to argue that I+1M(SS)/4|\mathcal{I}|+1 \geq |\mathcal{M} - (\mathcal{S} - \mathcal{S}')|/4 where SS\mathcal{S}' \subseteq \mathcal{S} contains the squares of S\mathcal{S} that overlap with XX. Notice that M(SS)=(MS)(MS)\mathcal{M} - (\mathcal{S} - \mathcal{S}') = (\mathcal{M} -\mathcal{S}) \cup (\mathcal{M} \cap \mathcal{S}') It suffices to argue that MS4|\mathcal{M} \cap \mathcal{S}'| \leq 4. Consider each square QSMQ \in \mathcal{S}' \cap \mathcal{M}. This square QQ must be at least as large as XX (since we choose XX to be smallest in S\mathcal{S}). Moreover, QQ overlaps with XX, so QQ must contain a corner of XX1. However, since XX has only four corners, there can be at most four non-overlapping squares that contain its corner. It follows that MS4|\mathcal{M} \cap \mathcal{S}'| \leq 4.

At the end of the algorithm, we can use (R2) to say that I\mathcal{I} is non-overlapping and (R3) to say that IM/4=M/4|\mathcal{I}| \geq |\mathcal{M} - \emptyset|/4 = |\mathcal{M}|/4. Notice that (R1) is not needed to make this implication; it is stated for the sole purpose of arguing that (R2) is a loop invariant. Proving algorithmic properties rigorously often requires this trick.

Exercise 8

Consider a collection of closed intervals I1,,InRI_1,\ldots, I_n \subseteq {\mathbb R} on the line where Ij=[lj,rj]I_j = [l_j, r_j]. Our goal is to choose as many non-overlapping intervals as possible.

  • Consider the heuristic AA: Choose the smallest interval, remove all intervals that overlap with it, and repeat. Prove that this heuristic always returns a solution of size at least half of that of the optimal solution.

  • Consider the heuristic BB: Choose the interval with minimum rjr_j, remove all intervals that overlap with it, and repeat. Prove that heuristic BB always returns an optimal solution.

Footnotes

  1. We assume that the squares in the S\mathcal{S} are parallel for ease of explanation.