Content deleted Content added
→Booth's Algorithm: Fixed to much nicer implementation |
m Open access bot: arxiv updated in citation with #oabot. |
||
(34 intermediate revisions by 23 users not shown) | |||
Line 1:
In [[computer science]], the '''lexicographically minimal string rotation''' (LMSR) or '''lexicographically least circular substring''' is the problem of finding the [[String_(computer_science)#Rotations|rotation of a
It is possible for a string to have multiple | author = Kellogg S. Booth
| last2 = Colbourn | first2 = Charles J.
Line 8 ⟶ 10:
| volume = 10
| issue = 1
| pages =
| year = 1980
| url = http://epubs.siam.org/sicomp/resource/1/smjcat/v10/i1/p203_s
| doi = 10.1137/0210015
| issn = 0097-5397
}}
</ref>
A common implementation trick when dealing with circular strings is to concatenate the string to itself instead of having to perform [[
==Algorithms==
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 '''''n''''', this algorithm runs in '''''O(n<sup>2</sup>)''''' time in the worst case.▼
===
▲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
===Booth's algorithm===
An efficient algorithm was proposed by Booth (1980).<ref>{{cite journal
| author = Kellogg S. Booth
Line 27 ⟶ 31:
| publisher = Elsevier
| volume = 10
| number =
| pages =
| year = 1980
| doi = 10.1016/0020-0190(80)90149-0
| issn = 0020-0190 }}
</ref>
The algorithm uses a modified preprocessing function from the [[
<
def
"""Booth's lexicographically minimal string rotation algorithm."""
n = len(
f = [-1
k = 0
for j in range(1, 2 * n):
i = f[j - k - 1]
while i != -1 and
if
k = j - i - 1
i = f[i]
if i == -1 and
if
k = j
f[j - k] = -1
else:
f[j - k] = i + 1
return k
</syntaxhighlight>
Of interest is that removing all lines of code which modify the value of '''''k''''' results in the original Knuth-Morris-Pratt preprocessing function, as '''''k''''' (representing the rotation) will remain zero. Booth's algorithm runs in '''''O(n)''''' time, where '''''n''''' is the length of the string. The algorithm performs at most '''''3n''''' comparisons in the worst case, and requires auxiliary memory of length '''''n''''' to hold the failure function table.▼
▲Of interest is that removing all lines of code which modify the value of
===Shiloach's
Shiloach (1981)<ref>{{cite journal
| title = Fast canonization of circular strings
| journal = Journal of Algorithms
Line 66 ⟶ 69:
| volume = 2
| number = 2
| pages =
| year = 1981
| issn = 0196-6774
| doi = 10.1016/0196-6774(81)90013-4
| author = Yossi Shiloach }}
</ref>
proposed an algorithm improving on Booth's result in terms of performance. It was observed that if there are
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.
===Duval's Lyndon
Duval (1983)<ref>
| title = Factorizing words over an ordered alphabet
| journal = Journal of Algorithms
| publisher = Elsevier
| volume =
| number =
| pages =
| year = 1983
| issn = 0196-6774
| doi = 10.1016/0196-6774(83)90017-2
| author = Jean Pierre Duval }}
</ref>
proposed an efficient algorithm involving the factorization of the string into its component [[Lyndon word
==Variants==
Line 101 ⟶ 102:
| volume = 8
| number = 5
| pages =
| year = 1979
| doi = 10.1016/0020-0190(79)90114-5
| issn = 0020-0190 }}
</ref>
proposed an algorithm to efficiently compare two circular strings for equality without a normalization requirement. An additional application which arises from the algorithm is the fast generation of certain chemical structures without repetitions.
A variant for [[quantum computing]] was proposed by Wang & Ying (2024).<ref name="Wang">{{cite journal
| author = Wang, Q., Ying, M.
| title = Quantum Algorithm for Lexicographically Minimal String Rotation
| journal = Theory Comput Syst
| volume = 68
| pages = 29–74
| year = 2024
| doi = 10.1007/s00224-023-10146-8
| arxiv = 2012.09376
}}
</ref> They show that the quantum algorithm outperforms any (classical) randomized algorithms in both worst and average cases.
==See also==
* [[Lyndon word]]
* [[
==References==
Line 118 ⟶ 130:
[[Category:Problems on strings]]
[[Category:Lexicography]]
[[Category:Articles with example code]]
|