Given a sequence of words, and a limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly. Assume that the length of each word is smaller than the line width.

The word processors like MS Word do task of placing line breaks. The idea is to have balanced lines. In other words, not have few lines with lots of extra spaces and some lines with small amount of extra spaces.

The extra spaces includes spaces put at the end of every line except the last one.  
The problem is to minimize the following total cost.
 Cost of a line = (Number of extra spaces in the line)^3
 Total Cost = Sum of costs for all lines

For example, consider the following string and line width M = 15
 "Geeks for Geeks presents word wrap problem" 
     
Following is the optimized arrangement of words in 3 lines
Geeks for Geeks
presents word
wrap problem 

The total extra spaces in line 1, line 2 and line 3 are 0, 2 and 3 respectively. 
So optimal value of total cost is 0 + 2*2 + 3*3 = 13

Please note that the total cost function is not sum of extra spaces, but sum of cubes (or square is also used) of extra spaces. The idea behind this cost function is to balance the spaces among lines. For example, consider the following two arrangement of same set of words:

  • There are 3 lines. One line has 3 extra spaces and all other lines have 0 extra spaces. Total extra spaces = 3 + 0 + 0 = 3. Total cost = 3*3*3 + 0*0*0 + 0*0*0 = 27.
  • There are 3 lines. Each of the 3 lines has one extra space. Total extra spaces = 1 + 1 + 1 = 3. Total cost = 1*1*1 + 1*1*1 + 1*1*1 = 3.

Total extra spaces are 3 in both scenarios, but second arrangement should be preferred because extra spaces are balanced in all three lines. The cost function with cubic sum serves the purpose because the value of total cost in second scenario is less.

[ad type=”banner”]

Method 1 (Greedy Solution)
The greedy solution is to place as many words as possible in the first line. Then do the same thing for the second line and so on until all words are placed. This solution gives optimal solution for many cases, but doesn’t give optimal solution in all cases. For example, consider the following string “aaa bb cc ddddd” and line width as 6. Greedy method will produce following output.

aaa bb 
cc 
ddddd

Extra spaces in the above 3 lines are 0, 4 and 1 respectively. So total cost is 0 + 64 + 1 = 65.

But the above solution is not the best solution. Following arrangement has more balanced spaces. Therefore less value of total cost function.

aaa
bb cc
ddddd

Extra spaces in the above 3 lines are 3, 1 and 1 respectively. So total cost is 27 + 1 + 1 = 29.

Despite being sub-optimal in some cases, the greedy approach is used by many word processors like MS Word and OpenOffice.org Writer.

[ad type=”banner”]

Method 2 (Dynamic Programming)
The following Dynamic approach strictly follows the algorithm given in solution of Cormen book. First we compute costs of all possible lines in a 2D table lc[][]. The value lc[i][j] indicates the cost to put words from i to j in a single line where i and j are indexes of words in the input sequences. If a sequence of words from i to j cannot fit in a single line, then lc[i][j] is considered infinite (to avoid it from being a part of the solution). Once we have the lc[][] table constructed, we can calculate total cost using following recursive formula. In the following formula, C[j] is the optimized total cost for arranging words from 1 to j.

Word Wrap Problem

The above recursion has overlapping subproblem property. For example, the solution of subproblem c(2) is used by c(3), C(4) and so on. So Dynamic Programming is used to store the results of subproblems. The array c[] can be computed from left to right, since each value depends only on earlier values.
To print the output, we keep track of what words go on what lines, we can keep a parallel p array that points to where each c value came from. The last line starts at word p[n] and goes through word n. The previous line starts at word p[p[n]] and goes through word p[n] – 1, etc. The function printSolution() uses p[] to print the solution.
In the below program, input is an array l[] that represents lengths of words in a sequence. The value l[i] indicates length of the ith word (i starts from 1) in theinput sequence.

[pastacode lang=”c” manual=”%2F%2F%20A%20Dynamic%20programming%20solution%20for%20Word%20Wrap%20Problem%0A%23include%20%3Climits.h%3E%0A%23include%20%3Cstdio.h%3E%0A%23define%20INF%20INT_MAX%0A%20%0A%2F%2F%20A%20utility%20function%20to%20print%20the%20solution%0Aint%20printSolution%20(int%20p%5B%5D%2C%20int%20n)%3B%0A%20%0A%2F%2F%20l%5B%5D%20represents%20lengths%20of%20different%20words%20in%20input%20sequence.%20For%20example%2C%20%0A%2F%2F%20l%5B%5D%20%3D%20%7B3%2C%202%2C%202%2C%205%7D%20is%20for%20a%20sentence%20like%20%22aaa%20bb%20cc%20ddddd%22.%20%20n%20is%20size%20of%20%0A%2F%2F%20l%5B%5D%20and%20M%20is%20line%20width%20(maximum%20no.%20of%20characters%20that%20can%20fit%20in%20a%20line)%0Avoid%20solveWordWrap%20(int%20l%5B%5D%2C%20int%20n%2C%20int%20M)%0A%7B%0A%20%20%20%20%2F%2F%20For%20simplicity%2C%201%20extra%20space%20is%20used%20in%20all%20below%20arrays%20%0A%20%0A%20%20%20%20%2F%2F%20extras%5Bi%5D%5Bj%5D%20will%20have%20number%20of%20extra%20spaces%20if%20words%20from%20i%20%0A%20%20%20%20%2F%2F%20to%20j%20are%20put%20in%20a%20single%20line%0A%20%20%20%20int%20extras%5Bn%2B1%5D%5Bn%2B1%5D%3B%20%20%0A%20%0A%20%20%20%20%2F%2F%20lc%5Bi%5D%5Bj%5D%20will%20have%20cost%20of%20a%20line%20which%20has%20words%20from%20%0A%20%20%20%20%2F%2F%20i%20to%20j%0A%20%20%20%20int%20lc%5Bn%2B1%5D%5Bn%2B1%5D%3B%0A%20%20%0A%20%20%20%20%2F%2F%20c%5Bi%5D%20will%20have%20total%20cost%20of%20optimal%20arrangement%20of%20words%20%0A%20%20%20%20%2F%2F%20from%201%20to%20i%0A%20%20%20%20int%20c%5Bn%2B1%5D%3B%0A%20%0A%20%20%20%20%2F%2F%20p%5B%5D%20is%20used%20to%20print%20the%20solution.%20%20%0A%20%20%20%20int%20p%5Bn%2B1%5D%3B%0A%20%0A%20%20%20%20int%20i%2C%20j%3B%0A%20%0A%20%20%20%20%2F%2F%20calculate%20extra%20spaces%20in%20a%20single%20line.%20%20The%20value%20extra%5Bi%5D%5Bj%5D%0A%20%20%20%20%2F%2F%20indicates%20extra%20spaces%20if%20words%20from%20word%20number%20i%20to%20j%20are%0A%20%20%20%20%2F%2F%20placed%20in%20a%20single%20line%0A%20%20%20%20for%20(i%20%3D%201%3B%20i%20%3C%3D%20n%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20extras%5Bi%5D%5Bi%5D%20%3D%20M%20-%20l%5Bi-1%5D%3B%0A%20%20%20%20%20%20%20%20for%20(j%20%3D%20i%2B1%3B%20j%20%3C%3D%20n%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20extras%5Bi%5D%5Bj%5D%20%3D%20extras%5Bi%5D%5Bj-1%5D%20-%20l%5Bj-1%5D%20-%201%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Calculate%20line%20cost%20corresponding%20to%20the%20above%20calculated%20extra%0A%20%20%20%20%2F%2F%20spaces.%20The%20value%20lc%5Bi%5D%5Bj%5D%20indicates%20cost%20of%20putting%20words%20from%0A%20%20%20%20%2F%2F%20word%20number%20i%20to%20j%20in%20a%20single%20line%0A%20%20%20%20for%20(i%20%3D%201%3B%20i%20%3C%3D%20n%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20for%20(j%20%3D%20i%3B%20j%20%3C%3D%20n%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(extras%5Bi%5D%5Bj%5D%20%3C%200)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20lc%5Bi%5D%5Bj%5D%20%3D%20INF%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20else%20if%20(j%20%3D%3D%20n%20%26%26%20extras%5Bi%5D%5Bj%5D%20%3E%3D%200)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20lc%5Bi%5D%5Bj%5D%20%3D%200%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20lc%5Bi%5D%5Bj%5D%20%3D%20extras%5Bi%5D%5Bj%5D*extras%5Bi%5D%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Calculate%20minimum%20cost%20and%20find%20minimum%20cost%20arrangement.%0A%20%20%20%20%2F%2F%20%20The%20value%20c%5Bj%5D%20indicates%20optimized%20cost%20to%20arrange%20words%0A%20%20%20%20%2F%2F%20from%20word%20number%201%20to%20j.%0A%20%20%20%20c%5B0%5D%20%3D%200%3B%0A%20%20%20%20for%20(j%20%3D%201%3B%20j%20%3C%3D%20n%3B%20j%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20c%5Bj%5D%20%3D%20INF%3B%0A%20%20%20%20%20%20%20%20for%20(i%20%3D%201%3B%20i%20%3C%3D%20j%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(c%5Bi-1%5D%20!%3D%20INF%20%26%26%20lc%5Bi%5D%5Bj%5D%20!%3D%20INF%20%26%26%20(c%5Bi-1%5D%20%2B%20lc%5Bi%5D%5Bj%5D%20%3C%20c%5Bj%5D))%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20c%5Bj%5D%20%3D%20c%5Bi-1%5D%20%2B%20lc%5Bi%5D%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20p%5Bj%5D%20%3D%20i%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20printSolution(p%2C%20n)%3B%0A%7D%0A%20%0Aint%20printSolution%20(int%20p%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20int%20k%3B%0A%20%20%20%20if%20(p%5Bn%5D%20%3D%3D%201)%0A%20%20%20%20%20%20%20%20k%20%3D%201%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20k%20%3D%20printSolution%20(p%2C%20p%5Bn%5D-1)%20%2B%201%3B%0A%20%20%20%20printf%20(%22Line%20number%20%25d%3A%20From%20word%20no.%20%25d%20to%20%25d%20%5Cn%22%2C%20k%2C%20p%5Bn%5D%2C%20n)%3B%0A%20%20%20%20return%20k%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20int%20l%5B%5D%20%3D%20%7B3%2C%202%2C%202%2C%205%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(l)%2Fsizeof(l%5B0%5D)%3B%0A%20%20%20%20int%20M%20%3D%206%3B%0A%20%20%20%20solveWordWrap%20(l%2C%20n%2C%20M)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C” highlight=”” provider=”manual”/]

Output :

Line number 1: From word no. 1 to 1
Line number 2: From word no. 2 to 3
Line number 3: From word no. 4 to 4

Time Complexity: O(n^2)
Auxiliary Space: O(n^2) The auxiliary space used in the above program cane be optimized to O(n)

[ad type=”banner”]