It integrates the problems of text justification and hyphenation into a single algorithm by using a discrete dynamic programming method to minimize a loss function that attempts to quantify the aesthetic qualities desired in the finished output.
[1][2][3] The algorithm works by dividing the text into a stream of three kinds of objects: boxes, which are non-resizable chunks of content, glue, which are flexible, resizeable elements, and penalties, which represent places where breaking is undesirable (or, if negative, desirable).
[2] The loss function, known as "badness", is defined in terms of the deformation of the glue elements, and any extra penalties incurred through line breaking.
[1] Making hyphenation decisions follows naturally from the algorithm, but the choice of possible hyphenation points within words, and optionally their preference weighting, must be performed first, and that information inserted into the text stream in advance.
[3][4] Typically, the cost function for this technique should be modified so that it does not count the space left on the final line of a paragraph; this modification allows a paragraph to end in the middle of a line without penalty.
The same technique can also be extended to take into account other factors such as the number of lines or costs for hyphenating long words.
[2] A naive brute-force exhaustive search for the minimum badness by trying every possible combination of breakpoints would take an impractical
The classic Knuth-Plass dynamic programming approach to solving the minimization problem is a worst-case
algorithm but usually runs much faster in close to linear time.
: The difference here is that the first line is broken before BB instead of after it, yielding a better right margin and a lower cost 11.