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 , all widths are , every glue has , and is a forced penalty. A break sequence to is a sequence of legal breaks that contains every forced break . Put . It is feasible if each line is feasible. Its class is the class of its last line, and the class of the empty sequence () is decent. Its demerits are the sum of the demerits of its lines, where line uses the class of line and whether is a flagged penalty. For a legal break and a class , let be the least demerits of a feasible break sequence to of class , or if there is none. Let .
Lemma 1 (the demerits split). Let be a feasible break sequence to with last line , and let be its prefix to , of class . The demerits of are those of plus , where is the demerits of the line after a line of class . This value depends only on , and .
Proof. The measures , and depend only on the items between and , so , the badness, the class and the penalty term depend only on and . The term depends on whether and are flagged penalties, and the term on the class of and on .
Lemma 2 (optimal prefix). If , then over all and classes such that , the line is feasible with class , and no forced break lies strictly between and . If no such pair exists, .
Proof. Each such pair gives a feasible break sequence to of class : take an optimal sequence to of class and add . It contains every forced break , because those are in the prefix and none lies strictly between and . By Lemma 1 its demerits are . So the minimum is . Conversely, an optimal sequence to of class has a last line , feasible with class , with no forced break strictly between and , and its prefix is a feasible sequence to of some class with demerits . By Lemma 1, .
Lemma 3 (pruning is safe). Let be the width at full shrink of the line without the item , as in line L10. If , then . If , then no line with is feasible.
Proof. The line contains the items of the line , plus possibly and later items, and the dropped items after are the same, except that if the first kept item after is beyond , the line is empty and . Each box adds its width to , each glue adds , and each penalty adds 0, so . Now let and . The natural width minus the shrink of the line is plus the width of if it is a penalty, so it is . If the shrink is positive, then gives . If , then and . Either way the line is not feasible.
Lemma 4 (invariant). When line L5 starts for a legal break , the active list holds, for each pair with , , no forced break strictly between and , and the line feasible, a node with . Every node in the list has and a chain of links that spells a feasible break sequence to of class with demerits . Moreover, after line L12 for , there is a node exactly for the classes with .
Proof. By induction on 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 and an empty chain. A node is made at line L12 from a pair stored at line L9, with for a node of class such that the line is feasible of class . The node was in the list at , so no forced break lies strictly between and (a forced break clears the list at line L10 after its own nodes are compared). By induction the chain of is a feasible sequence to with demerits , so the chain of the new node is a feasible sequence to of class with demerits (Lemma 1). Hence .
Membership. Take as in the first statement. Since , the node was created when was processed, by the last statement for (or it is the start node). At each legal break with , line L10 kept it: is not forced, since no forced break lies strictly between and , and , since otherwise Lemma 3 would make the line infeasible.
The minimum. At , 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 over the stored values for class equals by Lemma 2, and is none exactly when . Line L12 creates the node with for these classes.
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 times, each pass looks at a finite list, and line L14 follows links to strictly smaller positions until . A paragraph is a break sequence to , and is a legal break. By Lemma 4, after line L12 for the nodes at are exactly the classes with , with . 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 , which is , the least demerits of any feasible paragraph, and its chain of links is a feasible paragraph with exactly these demerits.