112 M. ELDER4. Algorithm part oneThe first stage of the <strong>algorithm</strong> is <strong>to</strong> rearrange the <strong>in</strong>put word and freelyreduce, <strong>to</strong> convert it <strong>to</strong> a standard form. We assume the <strong>in</strong>put word haslength n and has nonnegative t-exponent sum.Proposition 4. Any word w ∈ G p of length n with nonnegative t-exponentsum can be converted <strong>to</strong> a word u = G w of the formsuch that• l(u) ≤ n,• k,q,m ≥ 0,• q ≥ k + m,• |ε 0 | > 0 if k>0,• |ε q | > 0 if m>0,• |ε i |
A LINEAR-TIME ALGORITHM TO COMPUTE GEODESICS 113• In row 5, write a blank symbol <strong>in</strong> the first address, then write the numbers 0through n <strong>in</strong> the rema<strong>in</strong><strong>in</strong>g addresses. This row <strong>in</strong>dicates the previous columnaddress that was read (so are “backwards po<strong>in</strong>ters”). As the <strong>algorithm</strong>progresses, the entries <strong>in</strong> this row may change.Here is List A <strong>in</strong> its <strong>in</strong>itial state, with <strong>in</strong>put word at 2 a ···at −1 .List A ↓column 0 1 2 3 4 ··· n − 1 n n+1word S a t t a ··· a t −1 Ft-exp<strong>to</strong> 1 2 3 4 5 ··· n n+1from 0 1 2 3 ··· n − 2 n − 1 nAs the <strong>algorithm</strong> progresses, we will “reorder” the word written <strong>in</strong> row 2us<strong>in</strong>g the po<strong>in</strong>ters <strong>in</strong> rows 4 and 5 (and leav<strong>in</strong>g the letters <strong>in</strong> row 2 fixed,possibly eras<strong>in</strong>g some). To read the word, start at the S symbol. Move <strong>to</strong> thecolumn address <strong>in</strong>dicated <strong>in</strong> row 4. At the beg<strong>in</strong>n<strong>in</strong>g this will be column 1.From the current column, read the entry <strong>in</strong> row 4 <strong>to</strong> move the the next column.Cont<strong>in</strong>ue until you reach the F symbol. At any stage, <strong>to</strong> step back <strong>to</strong> theprevious address, go <strong>to</strong> the column address <strong>in</strong>dicated by row 5. Throughoutthe <strong>algorithm</strong>, the po<strong>in</strong>ters will never po<strong>in</strong>t <strong>to</strong> or from a column which has ablank symbol <strong>in</strong> row 2. The po<strong>in</strong>ters allow us <strong>to</strong> rearrange and delete lettersfrom the word <strong>in</strong> row 2 efficiently (<strong>in</strong> constant <strong>time</strong>), without hav<strong>in</strong>g <strong>to</strong> moveany letters on the table.For convenience, we <strong>in</strong>dicate the current address be<strong>in</strong>g read by a cursor.We assume that mov<strong>in</strong>g the cursor from one position <strong>in</strong> the list <strong>to</strong> anothertakes constant <strong>time</strong> on a random access mach<strong>in</strong>e.Here are two subrout<strong>in</strong>es that we will use many <strong>time</strong>s. Each one takesconstant <strong>time</strong> <strong>to</strong> call.Subrout<strong>in</strong>e 1: Free reduction. This subrout<strong>in</strong>e elim<strong>in</strong>ates freely cancel<strong>in</strong>gpairs xx −1 <strong>in</strong> row 2 of List A, <strong>in</strong> constant <strong>time</strong>. Assume that the cursor ispo<strong>in</strong>t<strong>in</strong>g <strong>to</strong> column k, and that the entry <strong>in</strong> the address (2, k) is not blank.Read the entries <strong>in</strong> rows 2, 4 and 5 of column k. Say the letter <strong>in</strong> row 2is x, and the <strong>in</strong>tegers <strong>in</strong> rows 4 and 5 are i, j.If position j row 2 is x −1 , then we can cancel this pair of genera<strong>to</strong>rs fromthe word as follows:• Read the <strong>in</strong>teger <strong>in</strong> row 5 position j, and go <strong>to</strong> the address <strong>in</strong>dicated (sayit is r). In row 4 of this address, write i. In row 5 position i, writer.• Erase entries <strong>in</strong> columns j and k.• Go <strong>to</strong> position i.