Problemadvanced
Least weight subsequence problem
Also called least weight subsequence.
Choose indices 0 = i0 < ... < ik = n minimizing the sum of w(i_j, i_j+1); line breaking with a concave cost is the main example.
The least weight subsequence problem asks for the cheapest way to go from position 0 to position by jumps forward, when a jump from to costs a given weight . It is a shortest path in a DAG whose nodes are and whose edges are all pairs , so the plain dynamic program takes time. Hirschberg and Larmore named it in 1987, with line breaking as the main example: the positions are the places between words, and is the cost of a line with the words to . When the weight satisfies the quadrangle inequality, which they called concave, the time drops to and then to .
In plain words
Think of a row of stepping stones numbered 0 to . You stand on stone 0 and must reach stone , always stepping forward, and each step from one stone to a later one has a price that you can look up. You want the cheapest walk.
For each stone, the cheapest walk to it ends with one last step, from some earlier stone. So the cheapest price of stone is the smallest of "cheapest price of plus the price of the step from to " over all earlier . Working from left to right, this takes about lookups.
When long steps get expensive fast enough, in the precise sense of the inequality below, most earlier stones can be ruled out without looking, and far fewer lookups are needed.
Definition
The input is an integer and a weight for each pair , given by a procedure that returns any in time. A subsequence is a list , for any , and its weight is . The problem is to find a subsequence of least weight.
The values
are the least weights of the subsequences that end at , and is the answer. The index that gives the minimum for is the last break before , and following these indices back from gives an optimal subsequence.
The concave case. Hirschberg and Larmore call concave when
This is the quadrangle inequality, the inequality of the Monge property for the array . Some authors call the same condition convex, because satisfies it when is a convex function, so the reader must check which inequality a paper means. The condition with is then usually called the convex case.
Variants
- Concave weights. The inequality above. This is the case of line breaking, and it has algorithms.
- Convex weights. The reverse inequality. It has algorithms of almost linear time, but no simple queue method of the kind that works for concave weights.
- Exactly parts. Ask for a subsequence with a given number of jumps. The dynamic program gets a second index, and for concave weights a Lagrangian method, the aliens trick, often removes it.
- States. The weight of a jump may depend on more than its two ends, for example on the jump before it. Then the nodes must carry this state, and the problem is a shortest path in a larger DAG. The demerits of the Knuth-Plass algorithm are of this kind: the fitness classes and the extra cost for two hyphens in a row depend on the line before, so they are not a function of two breaks, and the fast methods for the concave case do not apply to them directly.
- Online. The weights are known only when is reached, as when text arrives one word at a time.
Examples
A check value. Take and , a cost that wants every jump to have length 3. The weight is with convex, so it is concave in the sense above. The dynamic program gives
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 4 | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 0 | 1 |
and the subsequence 0, 3, 6, 10 has weight . The tests of the pages on the Hirschberg-Larmore algorithm and on Wilber's algorithm use this instance.
Line breaking with rigid spaces. Let the words have widths , let each space be 1 wide, and let . The line with words to has width . Put when the width is at most , and when it is larger. This is printing neatly without the free last line. It is for and a convex with values in , so it is concave.
Fewest lines. With when the line fits and otherwise, the answer is the fewest lines. This weight is also concave, and greedy line breaking solves it in linear time without a dynamic program.
Non-example: the last line free. Many layouts do not charge for the empty space of the last line, so for every whose last line fits. With for and , take , , and : the left side is and the right side is , so the inequality fails. The fix is to solve the problem for with the concave weight and then take in one more step.
Non-example: capped badness. The badness of TeX, , is not concave in general, because of the cap. For words of widths 2, 5 and 1, glue of width 2 with stretch 1 and shrink 1, and : , but .
Known algorithms
| algorithm | weights | time | notes |
|---|---|---|---|
| dynamic program (DP on a DAG) | any | all pairs | |
| Hirschberg-Larmore | concave | a queue of candidates and binary search; finds in order | |
| Wilber | concave | SMAWK on blocks; guesses values ahead and checks them | |
| Galil and Park | concave | a simpler linear method | |
| Klawe and Kleitman | convex | is the inverse Ackermann function | |
| greedy | 1 or by fit | fewest lines only |
The fast methods need each weight in time, which for line breaking means prefix sums of the widths. They also need finite weights or care with : ties between infinite values can break the matrix searching of Wilber's method, and a large finite penalty for a line that is too long avoids this.
| Algorithm | Time | Space | Properties | Code |
|---|---|---|---|---|
| Hirschberg-Larmore algorithm | O(n log n) worst | Θ(n) extra | deterministicofflineiterative | 1 language |
| Wilber's algorithm | O(n) worst | Θ(n) extra | deterministicofflinerecursive | 1 language |
Bounds
- General weights. In the model where is read through the procedure, every exact algorithm must read all weights in the worst case. If it skips , an adversary can make that weight small enough that the only optimal subsequence uses the jump from to .
- Concave weights. time and weight reads by Wilber's method. This is optimal when all the values are wanted, since they are numbers.
- Space. for the values and the back pointers.
Reductions
- To shortest paths. The problem is the shortest path from 0 to in the DAG with an edge of weight for each (shortest paths in a DAG). The nodes are already in topological order.
- To row minima. Put for . In the concave case this array is totally monotone, so the minimum of each row could be found by row minima of totally monotone matrices. But the entries of column need , which is known only after row is solved, so the matrix is not given in advance. Wilber's method solves this by working on blocks whose columns are already known, and by checking its guesses.
- From line breaking. Optimal text wrapping, printing neatly and the fewest-lines problem reduce to this problem, with the positions between words as indices.
Applications
- Line breaking. The main example. The fast algorithms apply when the cost of a line depends only on its two ends and is a convex function of its width, as for rigid spaces and ragged-right text.
- Splitting a sequence into blocks. Cutting a list of records into pages, a signal into pieces, or a text into segments (text segmentation) with a cost per piece is the same problem.
- Dynamic programs with a convex cost of the distance. Many recurrences of the form with a convex are concave instances; the convex hull trick solves the special case where the cost is linear in for each fixed .
References
- D. S. Hirschberg and L. L. Larmore, The least weight subsequence problem, SIAM Journal on Computing 16(4), 628-638, 1987, the definition of the problem, the concave condition, the O(n log n) algorithm and line breaking as the main application
- Robert Wilber, The concave least-weight subsequence problem revisited, Journal of Algorithms 9(3), 418-425, 1988, the O(n) algorithm for concave weights
- Zvi Galil and Kunsoo Park, A linear-time algorithm for concave one-dimensional dynamic programming, Information Processing Letters 33(6), 309-311, 1990, the simpler O(n) algorithm
- Maria M. Klawe and Daniel J. Kleitman, An almost linear time algorithm for generalized matrix searching, SIAM Journal on Discrete Mathematics 3(1), 81-97, 1990, the application to the case with the reverse inequality
- Donald E. Knuth and Michael F. Plass, Breaking paragraphs into lines, Software: Practice and Experience 11(11), 1119-1184, 1981, the line breaking problem with demerits that motivates the abstract problem
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag BREBA5 Edit this page History Report an error Markdown