Best word wrap algorithm?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Word wrapping, the process of breaking lines between words within a text, is an essential feature for text editors, web browsers, and other software that displays text. Efficient word wrap algorithms ensure that text is aesthetically pleasing and readable within a given width, avoiding broken words or uneven margins. This article explores several word wrap algorithms, with a focus on their advantages, disadvantages, and real-world applications.
Techniques for Word Wrapping
1. Greedy Algorithm
The Greedy Algorithm provides a straightforward approach to word wrapping. It attempts to place as many words as possible on each line until it exceeds the line's width.
Implementation Details:
- Start with the first word and add subsequent words to the line.
- If the next word can't fit within the given width, start a new line.
- Repeat until all words are processed.
Example: Consider the text: `"This is an example of word wrapping using a greedy approach."`
Assuming a line width of 15, the greedy algorithm would produce:
- Simple and easy to implement.
- Fast since it processes each word once (O(n) time complexity).
- Can lead to text that looks "ragged" or uneven since it doesn't consider the aesthetics of line lengths.
- Use a cost function where a larger amount of leftover space is penalized more heavily.
- Define `c[i,j]` as the cost of placing words `i` through `j` on a single line.
- Create a recurrence relation to compute the minimum total cost for placing all words up to a given position.
- Produces more aesthetically pleasing wraps.
- Minimizes total whitespace.
- More complex with a higher computational cost (O(n²) time complexity).
- Use penalty points for various breakpoint options.
- Optimize line breaks to balance evenness and minimize penalty points.
- Produces professional typesetting quality.
- Flexibility in adjusting penalty for different breaking scenarios.
- Complexity makes it harder to implement from scratch.
- Computationally intensive, especially with extensive custom rules.
- Text Editors: Basic word processors may use the greedy algorithm due to its simplicity and speed.
- Web Browsers: Might blend different approaches for handling text or leverage CSS styling for wrapping and alignment.
- Typesetting Programs: Comprehensive systems like LaTeX use the Knuth-Plass algorithm to ensure polished document layouts.
Related reading
- Better algorithm - Next semiprime
- Better optimization technique using if/else or dictionary
- Better results in set partition than by differencing
- Better way to check if all lists in a list are the same length?
- Better way to check if an element only exists in one array
- Better way to search for a node in binary tree
- Better ways to implement a modulo operation algorithm question
- BFS five letter word chain

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.