Lexicographically minimal string rotation: Difference between revisions

Content deleted Content added
Modified markup to favor bold/italics instead of in-sentence math markup.
Line 18:
==Algorithms==
===The Naive Algorithm===
The naive algorithm for finding the lexicographically minimal rotation of a string is to iterate through successive rotations while keeping track of the most lexicographically minimal rotation encountered. If the string is of length <math>'''''n</math>''''', this algorithm runs in <math>'''''O(n^{<sup>2})</mathsup>)''''' time in the worst case.
 
===Booth's Algorithm===
Line 56:
f[j-k] = i+1
</source>
Of interest is that removing all lines of code which modify the value of <math>'''''k</math>''''' results in the original Knuth-Morris-Pratt preprocessing function. Booth's algorithm runs in <math>'''''O(n)</math>''''' time, where <math>n</math> is the length of the string. The algorithm performs at most <math>'''''3n</math>''''' comparisons in the worst case, and requires auxiliary memory of length <math>'''''n</math>''''' to hold the failure function table.
 
===Shiloach's Fast Canonization Algorithm===
Line 73:
| author = Yossi Shiloach }}
</ref>
proposed an algorithm improving on Booth's result in terms of performance. It was observed that if there are '''''q''''' equivalent lexicographically minimal rotations of a string of length '''''n''''', then the string must consist of ''q'' equal substrings of length '''''d=n/q'''''. The algorithm requires only '''''n + d/2''''' comparisons and constant space in the worst case.
 
The algorithm is divided into two phases. The first phase is a quick sieve which rules out indices that are obviously not starting locations for the lexicographically minimal rotation. The second phase then finds the lexicographically minimal rotation start index from the indices which remain.