问题
How does the 'canonize' function (given below, from Ukkonen's paper) work, and in particular, when is the while loop finished? I think the value of p' - k' will always remain less than that of p - k. Am I right or wrong?
procedure canonize(s, (k, p)):
1. if p < k then return (s, k)
2. else
3. find the tk–transition g'(s, (k', p')) = s' from s;
4. while p' − k' <= p − k do
5. k = k + p' − k' + 1;
6. s = s';
7. if k <= p then find the tk–transition g'(s, (k', p')) = s' from s;
8. return (s, k).
回答1:
What the canonize function does is what is described at the very end of this SA post, where we consider a situation like this:
The situation is this:
The active point is at
(red,'d',3), i.e. three characters into thedefgedge going out of the red node.Now we follow a suffix link to the green node. In theory, our active node is now
(green,'d',3).Unfortunately that point does not exist, because the
deedge going out of the green node has got only 2 characters. Hence, we apply thecanonizefunction.
It works like this:
The starting character of the edge we are interested in is
d. This character is referred to as tk in Ukkonen's notation. So, "finding the tk-edge" means finding thedeedge at the green node.That edge is only two characters in length. I.e.
(p' - k') == 2in Ukkonen's notation. But the original edge had three characters:(p - k) == 3. So<=is true and we enter the loop.We shorten the edge we are looking for from
deftof. This is what thep := p + (k' - p') + 1step does.We advance to the state the
deedge points to, i.e. the blue state. That is whats := s'does.Since the remaining part
fof the edge is not empty (k <= p), we identify the relevant outgoing edge (that is thefgedge going out of the blue node). This step sets k' and p' to entirely new values, because they now refer to the stringfg, and its length (p' - k') will now be 2.The length of the remaining edge
f, (p - k), is now 1, and the length of the candidate edgefgfor the new active point, (p' - k'), is 2. Hence the loop conditionwhile (p' - k') <= (p - k) do
is no longer true, so the loop ends, and indeed the new (and correct) active point is (blue,'f',1).
[Actually, in Ukkonen's notation, the end pointer p of an edge points to the position of the final character of the edge, not the position that follows it. Hence, strictly speaking, (p - k) is 0, not 1, and (p' - k') is 1, not 2. But what matters is not the absolute value of the length, but the relative comparison of the two different lengths.]
A few final notes:
Pointers like p and k refer to positions in the original input text t. That can be quite confusing. For example, the pointers used in the
deedge at the green node will refer to some substringdeof t, and the pointers used in thefgedge at the blue node will refer to some substringfgof t. Although the stringdefgmust appear as one continuous string somewhere in t, the substringfgmight appear in other places, too. So, the pointer k of thefgedge is not necessarily the end pointer p of thedeedge plus one.What counts when we decide whether or not to end the loop is therefore not the absolute positions k or p, but the length (p - k) of the remaining edge compared to the length (p' - k') of the current candidate edge.
In your question, line 4 of the code snippet, there is a typo: It should be
k'instead ofk;.
回答2:
I had to change the canonize function because it didn't handle the auxiliary state properly. I added the following code after the 'p < k' if:
if (s == auxiliary)
{
s = root;
k++;
if (p < k)
return;
}
It seems to work now :)
来源:https://stackoverflow.com/questions/10097323/ukkonen-suffix-tree-procedure-canonize-unclear