0

My code calculates the starting position of the intervall correctly but not the end position:

    int left;
    int bot = 0; int top = textLength;

    while(bot != top)
    {
        int mid = (bot+top)/2;

        if(pattern.compareTo(text.substring(suffixArray.get(mid))) > 0) bot = mid + 1;
        else top = mid;
    }

    left = bot;



    int right;
    bot = left; top = textLength;

    while(bot != top)
    {
        int mid = (bot+top)/2;

        if(pattern.compareTo(text.substring(suffixArray.get(mid))) < 0) top = mid;
        else bot = mid+1;
    }

    right = bot;

I compared it to several pseudo codes on the internet and I don't really see why it's not working. What am I missing?

tenacious
  • 91
  • 3
  • 11
  • Can't see anything immediately wrong with it. Usual suspects are OBOE, but those seem consistent. What do you mean when you say it's not calculating the end position correctly? – Ordous Dec 10 '14 at 15:32
  • @Ordous No matter what pattern I'm looking for right is always the same as left – tenacious Dec 10 '14 at 17:31
  • Wait, what exactly are the contents of `suffixArray`? The positions of the suffixes? Are they actually sorted lexographically, or numerically? – Ordous Dec 10 '14 at 17:34
  • suffixArray contains the starting positions of the sorted suffixes of the text. So yes, it is sorted lexographically. – tenacious Dec 10 '14 at 17:41

1 Answers1

1

The search for right differs only in >= instead of >

    if(pattern.compareTo(text.substring(suffixArray.get(mid))) >= 0) bot = mid + 1;
    else top = mid;

So I would think

right = bot;

to point to the next higher value.

So better check first whether all is ordered:

String old = text.substring(suffixArray.get(0));
for (int i = 1; i < textLength; ++i) {
    String next = text.substring(suffixArray.get(i));
    if (old.compareTo(next) >= 0) {
        System.err.printf("Wrong order at [%d] '%s' >= [%d] '%s'%n",
            i - 1, old, i, next);
    }
    old = next;
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138