Greedy line breaking 2026-11-05
Greedy line breaking commits each line after filling it with the longest available fitting prefix of the remaining input. In a fixed-width model, the input consists of ordered, indivisible typesetting boxes, a common typesetting line width , and nonnegative fixed word spacing. Assume every individual box fits. The greedy algorithm scans forward, adds the next item if it fits, and otherwise starts a new line. Incrementally maintaining the used width gives time complexity for input boxes.
This rule minimizes the number of lines under these assumptions. To see why, let be the last item covered by the first greedy lines, and the last item covered by the first lines of any feasible alternative. After a layout finishes, take its endpoint to remain for subsequent indices. The first greedy line reaches at least as far as the alternative's first line. Suppose . If the greedy layout has already reached , the next inequality is immediate. Otherwise, removing the alternative's already covered items from its next line leaves a fitting suffix, because widths and gaps are nonnegative. The next greedy line can reach at least that endpoint. By mathematical induction,Thus an alternative cannot finish in fewer lines.
Line count and paragraph appearance are different objectives. With widths , gap 1 and width 7, greedy grouping has lengths . The alternative also uses three lines. The line breaking with squared slack cost model assigns cost 20 to this alternative and 26 to the greedy layout when the final line is free. The line breaking diagram shows the two layouts. Global paragraph costs can justify dynamic programming even when greedy packing already uses the minimum line count.
The fixed-width assumptions matter when applying this result. Hyphenation introduces additional break opportunities, while adjustable typesetting glue changes how a candidate line fits. Those choices belong to richer layout models such as the Knuth-Plass line-breaking algorithm.
Knuth-Plass line-breaking algorithm 2026-11-05
The Knuth-Plass line-breaking algorithm chooses paragraph breaks using dynamic programming over a representation of the material as typesetting boxes, typesetting glue and typesetting penalties. Boxes supply unbreakable widths; glue supplies normal spacing together with stretch and shrink allowances; penalties express preferences about permitted breaks and can account for a hyphen added when a break is taken.[ref]
Breaking paragraphs into lines by Donald E. Knuth and Michael F. Plass. pp.1119–1122, summary and formulation
Text justification makes the glue geometry useful. In an illustrative line, suppose box widths total 24 and two glues each have normal width 3 and stretch allowance 2. To fill width 32, the line adjustment ratio isEach glue then becomes , giving . This example demonstrates proportional stretching; it is one ingredient of paragraph optimization.
The dynamic programming state retains information needed to score later choices. In particular, a line-breaking fitness class records a spacing category, allowing an abrupt change between neighbouring lines to affect the cost. Break position alone would discard that relevant history. The original active nodes include line number and fitness class, with the best accumulated demerits stored as their value.[ref]
Breaking paragraphs into lines by Donald E. Knuth and Michael F. Plass. pp.1158–1159, active nodes, line numbers and fitness classes
The line breaking with squared slack cost example isolates break selection with fixed word spacing. Here, adjustable typesetting glue and typesetting penalties participate in the paragraph optimization. Donald E. Knuth and Michael F. Plass's 1981 paper documents the method and its development in the TeX project.[ref]
Breaking paragraphs into lines by Donald E. Knuth and Michael F. Plass. pp.1175–1176, authors' historical account
Line breaking 2026-11-05
Line breaking selects where a paragraph is divided into successive lines. The choice combines the widths of words and spaces with a measure of acceptable layout. A glyph advance supplies part of the underlying geometry, while the chosen breaks determine which content shares a CSS line box in a web layout.
A greedy algorithm can put as much content as possible on each line before proceeding. Greedy line breaking is easy to compute, but an early choice can leave an awkward short line later. Line breaking with squared slack cost gives a small, explicit example. Suppose four word widths are , each internal space has width 1, and the permitted line width is 7, all in arbitrary consistent units. Greedy grouping produces line lengths . An alternative grouping produces . Charging squared unused width on every line except the last givesAn immediately fuller first line can therefore produce a worse paragraph under this particular cost.
Dynamic programming makes the global choice systematic. Let be the minimum cost for the first words, and let include words through and their internal spaces. For feasible lines, set , except that for the final line. ThenPossible break positions form a directed acyclic graph; a feasible line is an edge, so the same calculation finds a shortest path from the start to the end of the paragraph.
Donald E. Knuth and Michael F. Plass developed a richer paragraph-wide method using boxes, adjustable glue and penalties. Their Knuth-Plass line-breaking algorithm accommodates typographic choices beyond the fixed spaces and squared costs in the illustrative model above.[ref]
Breaking paragraphs into lines by Donald E. Knuth and Michael F. Plass. 1981 paper, summary and introduction; paragraph-wide optimization and dynamic programming
Michael F. Plass 2026-11-05
Michael F. Plass is a computer scientist whose work connects typography with mathematical optimization. With Donald E. Knuth, he coauthored the 1981 paper that describes the Knuth-Plass line-breaking algorithm: choosing breaks for an entire paragraph with dynamic programming, allowing later text to influence earlier choices. The original byline places both authors in Stanford University's Computer Science Department.[ref]
Breaking paragraphs into lines by Donald E. Knuth and Michael F. Plass. Page1119: original byline, affiliation and summary
The authors' historical account credits Michael F. Plass with introducing feasible breakpoints into the developing algorithm in summer 1977, reducing the active possibilities considered. They report its implementation in the first complete version of TeX in March 1978. These dates describe the development of the method, rather than the later publication date.[ref]
Breaking paragraphs into lines by Donald E. Knuth and Michael F. Plass. Page1176: feasible-breakpoint refinement and implementation chronology
An illustrative spacing calculation shows one ingredient of the model. Suppose fixed boxes total 24 width units and two spaces each have normal width 3 and stretchability 2. Their natural total width is 30. For a target width of 32, the adjustment ratio is ; each space becomes , giving . This original numerical example shows proportional stretching, before the paragraph-wide choice of breaks.
The full method also represents break preferences with penalties and accounts for relevant relationships between successive lines. Line breaking with squared slack cost is a useful simpler model for understanding global choices, while the published Knuth-Plass line-breaking algorithm handles adjustable spacing and additional preferences.[ref]
Breaking paragraphs into lines by Donald E. Knuth and Michael F. Plass. Pages1121–1122: boxes, glue and penalties; pages1158–1159: active-node state
