Algorithm Search⌘K

Proofs: Knuth-Plass line breaking algorithm

These are the human proofs of Knuth-Plass line breaking algorithm, at full length.

Knuth-Plass returns a paragraph of least total demerits full

This proof shows: correctness, termination.

We use the definitions and the pseudocode of the page. The items are x0,…,xm−1x_0, \ldots, x_{m-1}, all widths are ≥0\ge 0, every glue has 0≤z≤w0 \le z \le w, and xm−1x_{m-1} is a forced penalty. A break sequence to bb is a sequence of legal breaks b1<⋯<bk=bb_1 < \cdots < b_k = b that contains every forced break <b< b. Put b0=−1b_0 = -1. It is feasible if each line (bt−1,bt)(b_{t-1}, b_t) is feasible. Its class is the class of its last line, and the class of the empty sequence (b=−1b = -1) is decent. Its demerits are the sum of the demerits of its lines, where line tt uses the class of line t−1t - 1 and whether bt−1b_{t-1} is a flagged penalty. For a legal break bb and a class cc, let OPT(b,c)\mathrm{OPT}(b, c) be the least demerits of a feasible break sequence to bb of class cc, or ∞\infty if there is none. Let OPT(−1,decent)=0\mathrm{OPT}(-1, \text{decent}) = 0.

Lemma 1 (the demerits split). Let σ\sigma be a feasible break sequence to bb with last line (a,b)(a, b), and let σ′\sigma' be its prefix to aa, of class c′c'. The demerits of σ\sigma are those of σ′\sigma' plus δ(a,b,c′)\delta(a, b, c'), where δ(a,b,c′)\delta(a, b, c') is the demerits of the line (a,b)(a, b) after a line of class c′c'. This value depends only on aa, bb and c′c'.

Proof. The measures LabL_{ab}, YabY_{ab} and ZabZ_{ab} depend only on the items between aa and bb, so rr, the badness, the class and the penalty term depend only on aa and bb. The term α\alpha depends on whether xax_a and xbx_b are flagged penalties, and the term γ\gamma on the class of (a,b)(a, b) and on c′c'. □\square

Lemma 2 (optimal prefix). If OPT(b,c)<∞\mathrm{OPT}(b, c) < \infty, then OPT(b,c)=min⁡(OPT(a,c′)+δ(a,b,c′))\mathrm{OPT}(b, c) = \min \bigl( \mathrm{OPT}(a, c') + \delta(a, b, c') \bigr) over all a<ba < b and classes c′c' such that OPT(a,c′)<∞\mathrm{OPT}(a, c') < \infty, the line (a,b)(a, b) is feasible with class cc, and no forced break lies strictly between aa and bb. If no such pair exists, OPT(b,c)=∞\mathrm{OPT}(b, c) = \infty.

Proof. Each such pair gives a feasible break sequence to bb of class cc: take an optimal sequence to aa of class c′c' and add bb. It contains every forced break <b< b, because those ≤a\le a are in the prefix and none lies strictly between aa and bb. By Lemma 1 its demerits are OPT(a,c′)+δ(a,b,c′)\mathrm{OPT}(a, c') + \delta(a, b, c'). So the minimum is ≥OPT(b,c)\ge \mathrm{OPT}(b, c). Conversely, an optimal sequence to bb of class cc has a last line (a,b)(a, b), feasible with class cc, with no forced break strictly between aa and bb, and its prefix is a feasible sequence to aa of some class c′c' with demerits ≥OPT(a,c′)\ge \mathrm{OPT}(a, c'). By Lemma 1, OPT(b,c)≥OPT(a,c′)+δ(a,b,c′)\mathrm{OPT}(b, c) \ge \mathrm{OPT}(a, c') + \delta(a, b, c'). □\square

Lemma 3 (pruning is safe). Let M(a,b)M(a, b) be the width at full shrink of the line (a,b)(a, b) without the item xbx_b, as in line L10. If a<b<b′a < b < b', then M(a,b′)≥M(a,b)M(a, b') \ge M(a, b). If M(a,b)>LM(a, b) > L, then no line (a,b′)(a, b') with b′≥bb' \ge b is feasible.

Proof. The line (a,b′)(a, b') contains the items of the line (a,b)(a, b), plus possibly xbx_b and later items, and the dropped items after aa are the same, except that if the first kept item after aa is beyond bb, the line (a,b)(a, b) is empty and M(a,b)=0M(a, b) = 0. Each box adds its width ≥0\ge 0 to MM, each glue adds w−z≥0w - z \ge 0, and each penalty adds 0, so M(a,b′)≥M(a,b)M(a, b') \ge M(a, b). Now let M(a,b)>LM(a, b) > L and b′≥bb' \ge b. The natural width minus the shrink of the line (a,b′)(a, b') is M(a,b′)M(a, b') plus the width of xb′x_{b'} if it is a penalty, so it is ≥M(a,b′)>L\ge M(a, b') > L. If the shrink ZZ is positive, then Lab′−Z>LL_{ab'} - Z > L gives r=(L−Lab′)/Z<−1r = (L - L_{ab'}) / Z < -1. If Z=0Z = 0, then Lab′>LL_{ab'} > L and r=−∞r = -\infty. Either way the line is not feasible. □\square

Lemma 4 (invariant). When line L5 starts for a legal break bb, the active list holds, for each pair (a,c′)(a, c') with a<ba < b, OPT(a,c′)<∞\mathrm{OPT}(a, c') < \infty, no forced break strictly between aa and bb, and the line (a,b)(a, b) feasible, a node (a,c′)(a, c') with D=OPT(a,c′)D = \mathrm{OPT}(a, c'). Every node (a,c′)(a, c') in the list has D=OPT(a,c′)D = \mathrm{OPT}(a, c') and a chain of links that spells a feasible break sequence to aa of class c′c' with demerits DD. Moreover, after line L12 for bb, there is a node (b,c)(b, c) exactly for the classes with OPT(b,c)<∞\mathrm{OPT}(b, c) < \infty.

Proof. By induction on bb over the legal breaks. Nodes enter the list only at line L1 and line L12, and leave it only at line L11.

Values and chains. The start node has D=0=OPT(−1,decent)D = 0 = \mathrm{OPT}(-1, \text{decent}) and an empty chain. A node (b,c)(b, c) is made at line L12 from a pair (d,a)(d, a) stored at line L9, with d=D(a)+δ(a,b,c′)d = D(a) + \delta(a, b, c') for a node aa of class c′c' such that the line (a,b)(a, b) is feasible of class cc. The node aa was in the list at bb, so no forced break lies strictly between aa and bb (a forced break clears the list at line L10 after its own nodes are compared). By induction the chain of aa is a feasible sequence to aa with demerits D(a)D(a), so the chain of the new node is a feasible sequence to bb of class cc with demerits dd (Lemma 1). Hence d≥OPT(b,c)d \ge \mathrm{OPT}(b, c).

Membership. Take (a,c′)(a, c') as in the first statement. Since OPT(a,c′)<∞\mathrm{OPT}(a, c') < \infty, the node (a,c′)(a, c') was created when aa was processed, by the last statement for aa (or it is the start node). At each legal break b′′b'' with a<b′′<ba < b'' < b, line L10 kept it: xb′′x_{b''} is not forced, since no forced break lies strictly between aa and bb, and M(a,b′′)≤LM(a, b'') \le L, since otherwise Lemma 3 would make the line (a,b)(a, b) infeasible.

The minimum. At bb, lines L5 to L9 examine all nodes in the list. By membership, every pair of Lemma 2 is examined with its exact value, and every examined node gives a real sequence. So min⁡\min over the stored values for class cc equals OPT(b,c)\mathrm{OPT}(b, c) by Lemma 2, and best[c]best[c] is none exactly when OPT(b,c)=∞\mathrm{OPT}(b, c) = \infty. Line L12 creates the node (b,c)(b, c) with D=OPT(b,c)D = \mathrm{OPT}(b, c) for these classes. □\square

Theorem. The algorithm terminates. If some feasible paragraph exists, it returns one with the least total demerits; otherwise it reports failure.

Proof. Termination: the loop of line L2 runs mm times, each pass looks at a finite list, and line L14 follows links to strictly smaller positions until −1-1. A paragraph is a break sequence to m−1m - 1, and xm−1x_{m-1} is a legal break. By Lemma 4, after line L12 for b=m−1b = m - 1 the nodes at m−1m - 1 are exactly the classes with OPT(m−1,c)<∞\mathrm{OPT}(m - 1, c) < \infty, with D=OPT(m−1,c)D = \mathrm{OPT}(m - 1, c). They are not removed afterwards, since the loop ends. If there are none, no feasible paragraph exists and line L13 reports failure. Otherwise line L14 takes the least DD, which is min⁡cOPT(m−1,c)\min_c \mathrm{OPT}(m - 1, c), the least demerits of any feasible paragraph, and its chain of links is a feasible paragraph with exactly these demerits. □\square

Back to Knuth-Plass line breaking algorithm