116 M. ELDERHere we show List A for the <strong>in</strong>put word at 2 atat −1 at −1 at −1 ata ···,andthecorrespond<strong>in</strong>g List B, as an example.List A↓column 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ···word S a t t a t a t −1 a t −1 a t −1 a t at-exp 1 2 3 2 1 0 1<strong>to</strong> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15from 0 1 2 3 4 5 6 7 8 9 10 11 12 13List Bt-exp ··· −6 −5 −4 −3 −2 −1 0 1 2 3 4 5 6 ···strike 1 11 2 3 5strike 2 9 7strike 3 13When an entry occurs <strong>in</strong> the last row of List B at some position labeledtexp, mean<strong>in</strong>g the same exponent has occurred 3 <strong>time</strong>s , then we have a prefixof the form either NPNP or PNPN, so we apply Corollary 2 as follows.Suppose the entries <strong>in</strong> this column are p a ,p b ,p 3 ,withp 3 the most recentlyadded. These correspond <strong>to</strong> the positions <strong>in</strong> List A where the value texp haveappeared.To beg<strong>in</strong> with, the word written <strong>in</strong> row 2 of List A appears <strong>in</strong> the correc<strong>to</strong>rder (from left <strong>to</strong> right), the po<strong>in</strong>ters have only been used <strong>to</strong> possibly skipblank addresses. So at the start of this step we know that p a comes before p b .However, as the <strong>algorithm</strong> progresses, we will not know which of p a and p bcomes first <strong>in</strong> the word. That is, as we <strong>in</strong>troduce po<strong>in</strong>ters <strong>to</strong> List A <strong>to</strong> movesubwords around, a letter <strong>in</strong> column p could sit before a letter <strong>in</strong> column qwith q
A LINEAR-TIME ALGORITHM TO COMPUTE GEODESICS 117Now one of columns q a ,q b must conta<strong>in</strong> a t ±1 <strong>in</strong> row 2, with sign opposite<strong>to</strong> that of p 3 .• If q a is same sign as p 3 ,thenordermustbep a − q a − p b − q b − p 3 .• If q b is same sign as p 3 ,thenordermustbep b − q b − p a − q a − p 3 .• Both q a ,q b have opposite sign <strong>to</strong> p 3 . In this case, we look at the letters <strong>in</strong>row 2 of columns p a ,p b . If the letter at address (2,p a ) has opposite sign<strong>to</strong> that <strong>in</strong>(2,p 3 ), then it must come first, s<strong>in</strong>ce one of p a ,p b must match upwith q a ,q b .If both p a ,p b have same letter as p 3 <strong>in</strong> row 2, then we are <strong>in</strong> a situationlike tat −1 atat −1 at. But s<strong>in</strong>ce there is a t ±1 letter preced<strong>in</strong>g both p a and p b ,then the t-exponent before p a ,p b ,p 3 are read is the same, and is recordedthree <strong>time</strong>s. This case cannot arise s<strong>in</strong>ce we apply this procedure the first<strong>time</strong> we see the same number more than twice.Us<strong>in</strong>g this subrout<strong>in</strong>e, we can determ<strong>in</strong>e the correct order of the columnsp a ,p b and q a ,q b ,<strong>in</strong>O(n) <strong>time</strong>. Rename the first position p 1 and second p 2 ,and q 1 ,q 2 as appropriate. So we have p 1 − q 1 − p 2 − q 2 − p 3 .The subword between positions q 1 and p 2 has t-exponent 0, as does thesubword from q 2 <strong>to</strong> p 3 . By commut<strong>in</strong>g one of these subwords (us<strong>in</strong>g Lemma 1),we can place a t next <strong>to</strong> a t −1 somewhere and get a free cancellation. Theprecise <strong>in</strong>struction will depend on the letters at each of these addresses, andwe will consider each situation case-by-case.Case 1.List Acolumn ··· p 1 ··· q 1 ··· p 2 ··· q 2 ··· p 3 ···word t t −1 t t −1 tt-exp k 1 k 2<strong>to</strong> i 1 i 2 i 3from j 1 j 2 j 3Between p 1 and q 1 we have only a letters (or noth<strong>in</strong>g). So we will commutethe subword q 1 − p 2 back <strong>to</strong>wards p 1 as follows:• j 1 row 4, replace p 1 by i 2• i 2 row 5, replace q 1 by j 1• p 2 row 4, replace i 3 by i 1• i 1 row 5, replace p 1 by p 2• j 2 row 4, replace q 1 by i 3• i 3 row 5, replace p 2 by j 2• delete columns p 1 ,q 1• delete p 1 and q 1 from List B columns k 1 and k 2 , respectively.