Re:Line breaking (2014)
by anameaname on Saturday March 03, 2018 @03:11PM
(#16511306)
Isn't line breaking an NP complete problem, akin to bin packing? I seem to recall hearing a presentation about it in the context of autoformatters for code. For example, Auto formatting Java code, which is typically verbose, relies on heuristics to avoid computational snares.
[ Parent
| on HN ]
It depends what on what bells&whistles you want, but line breaking is “theoretically” quadratic but, for most use cases, basically linear. A real misfortune is that the Knuth-Plass algorithm was published before we had a good grasp on how to present algorithms well. The actual algorithm is basically just a linear pass (reverse) along with a memo-table. (This is usually described as “dynamic programming”, but I find that term less than useless.)
Re:Line breaking (2014)
by peter_l_downs on Saturday March 03, 2018 @04:38PM
(#16511745)
Yes, but as this blog post shows, you can apply dynamic programming (and then various other optimizations) to make it fast for common uses.
Even if it is NP Complete, that would really just limit you on the size of a text for which you could find the absolute best line breaking.
Consider, the traveling salesman for the capitals of the US is easily solvable, due to the small size of the problem space. Similarly, I'd imagine the actual problem space to not be that large on line breaking.
Specifically, since line breaking is limited to the paragraphs you are looking at, this probably isn't an issue in most cases. Degenerate cases could be checked for and processing limited to not try too much.