4

Many may know the problem of Lexicographically minimal string rotation. Solutions are here like: How to find the number of the lexicographically minimal string rotation?


But I am not asking for a duplicate solution. Instead, I want to ask in another related question: is it the same as the problem of finding the lexicographical min suffix?

For example, we have string bbaaccaadd.

The lexicographically min string rotation would be "aaccaaddbb".

To find it, can I just find the minimal suffix of bbaaccaadd, which is aaccaadd and append the head 2 "bb" at the end?


Are these two problems identical?

Community
  • 1
  • 1
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
  • Min suffix reduces easily to min rotation; just append a character $ that compares less than every normal character. I'm not sure about the other direction. (Argh, no; we need the second-minimum rotation.) – David Eisenstat Mar 25 '14 at 22:01
  • @DavidEisenstat I don't understand that; if you append a new minimal symbol, the min rotation would be the one putting that symbol to the front, independent of the original string, no? – G. Bach Mar 25 '14 at 22:06
  • @G.Bach Yeah, see my correction. The second-minimum is what we want in this case. – David Eisenstat Mar 25 '14 at 22:07
  • 1
    At least without changing the string, you can't directly use a solution for one as a solution for the other. Take `baaba`, then the min suffix is `a` which would give us `abaab` with what you suggested, but the min rotation is `aabab`. – G. Bach Mar 25 '14 at 22:11
  • 1
    @G.Bach I guess the more interesting question is whether they can be reduced to each other using some "simple enough" transformation. – Niklas B. Mar 25 '14 at 22:15
  • Not trivial enough for me, I'm afraid. Maybe I'm just too tired, though. – G. Bach Mar 25 '14 at 22:17
  • @G.Bach Yeah I just realized that your discussion above was about that, but the algorithm for min-rotation can easily be adapted to support min-suffix – Niklas B. Mar 25 '14 at 22:17
  • @DavidEisenstat - The `$` symbol must be compared as if it is the source string itself. Then both problems are identical. – Egor Skriptunoff Mar 25 '14 at 22:18
  • Min-rotation -> min-suffix should also be pretty straightforward, it feels like we just need to append the string to itself (it's not *quite* correct that way) – Niklas B. Mar 25 '14 at 22:20
  • I don't even see how to modify the string such that an algorithm that solves min rotation solves min suffix when given the modified string. Please post more than "can be easily done". – G. Bach Mar 25 '14 at 22:26
  • @G.Bach I think we just need to add two small `if`s: http://pastie.org/8968661 Haven't proved it but it seems intuitively correct and it passes random testing (but then again we had another incorrect algorithm today here on SO that was quite resistant to random testing :P) – Niklas B. Mar 25 '14 at 22:36
  • @G.Bach You mean whether it still solves min-rotation? No it doesn't and it's not a reduction, it's the simple modification of the algorithm that I mentioned – Niklas B. Mar 25 '14 at 22:40
  • What's wrong with appending a '$' that compares *more* than any other character? I'm missing something... can somebody provide a counter example? – Eyal Schneider Mar 25 '14 at 22:40
  • @EyalSchneider Then the minimum suffix in the new string is no longer the minimum suffix in the old string – Niklas B. Mar 25 '14 at 22:40
  • @NiklasB.: But we are interested in finding a reduction, so I guess it doesn't matter. All I'm saying is that the min suffix in the modified string may define the optimal shift point for the min rotation problem. – Eyal Schneider Mar 25 '14 at 22:42
  • @EyalSchneider I was trying to demonstrate something I said earlier, that the min-rotation algorithm can easily be adapted to solve min-suffix. I thought G.Bach was asking about that modification, but in fact he wasn't (there was a misunderstanding there). The reduction from min-suffix to min-rotation does *not* work by appending positive or negative infinity at the end of the string, as we have already seen (because we need to find the second-minimal rotation for neg. inf. and we destroy the order relation between the suffixes with pos. inf.) – Niklas B. Mar 25 '14 at 22:45
  • @NiklasB.: I thought the OP asked about a reduction from min rotation **to** min suffix, and not vice versa. This direction works with a positive infinity suffix, right? – Eyal Schneider Mar 25 '14 at 22:48
  • @EyalSchneider Sure, but he also asks whether the two are equivalent in a certain sense – Niklas B. Mar 25 '14 at 22:48

2 Answers2

1

To find it, can I just find the minimal suffix of bbaaccaadd, which is aaccaadd and append the head 2 "bb" at the end?

That would not always yield the correct minimum rotation. For example take S = baa. Then the minimum suffix is a, but the minimum rotation is aab, not ab.

However, we can show that

min_rotation(S) = min_suffix(S + S + '∞') 

where '∞' is a character larger than every character in S.

Are these two problems identical?

Obviously not quite, but they are very closely related. As we have seen, we can use min-suffix to solve min-rotation using a straightforward reduction. I don't see the reverse reduction yet, but maybe there is a similar one. As David has pointed out, if we append negative infinity to a string S, then the second-minimal rotation of the string corresponds to its minimal suffix. Also, Booth's algorithm for min-rotation can easily be adapted to solve min-suffix.

Niklas B.
  • 92,950
  • 18
  • 194
  • 224
0

The question whether the problems are "identical" is not well defined, so I assume here that it refers to the solution equivalence in terms of optimal time complexity.

To demonstrate this, it's enough to show how we can reduce min-rotation to min-suffix and vice-versa, where the reduction overhead is O(n) time (where n is the string length).

min-rotation to min-suffix

The reduction works by appending an infinite valued character to the min-rotation input, just as described by Niklas B.

min-suffix to min-rotation

Note the following observation: the min-suffix must start with the minimal substring M in the input string. The min-rotation solution must also start with M, otherwise it can be improved. Therefore, if we have S'=min-rotation(S), then the shortest prefix of S' which is also a suffix of S is our min-suffix of S.

Now we only have to deal with the following problem: given two strings S1 and S2, find the shortest prefix of S1 which is also a suffix of S2. This problem can be solved in linear time by creating a suffix tree T of S2, and then traversing S1 until a match is found in T.

Eyal Schneider
  • 22,166
  • 5
  • 47
  • 78
  • If you have the suffix tree or suffix array then you have the minimum substring as well.. – Niklas B. Mar 26 '14 at 16:11
  • @NiklasB.: Nice - you are right, the second reduction is kind of useless. The two problems are linear, but now I'm not sure this is what the OP means by "identical"... – Eyal Schneider Mar 26 '14 at 17:13