-1

I am learning suffix array construction from this link.

Here is the code that I have ported from c++ to java

class Entry  implements Comparable<Entry> {

    int [] nr = new int[2];
    int p=0;

    public int compareTo(Entry that){
        if (this.nr[0] == that.nr[0]){
            if(this.nr[1] < that.nr[1]){
                return 1;
            }else{
                return 0;
            }
        }else if(this.nr[0]<that.nr[0]){
            return 1;
        }else{
            return 0;
        }
    }
}

public class SuffixArray {

    private static final int MAXN = 65536;
    private static final int MAXLG = 17;

    private static Entry [] entries = new Entry[MAXN];
    private static int [][] matrix = new int [MAXLG] [MAXN];

    private static int step=0, count=0;
    private static int N=0;

    public static void process(String S) {

        N = S.length();
        for(int i=0;i<N;i++){
            matrix[0][i] =  (S.charAt(i)-'a');
        }
        for(int i=0;i<N;i++){
            entries[i] = new Entry();
        }

        for(step=1,count=1; (count>>1)<N;step++, count<<=1){
            for(int i=0;i<N;i++){
                entries[i].nr[0] = matrix[step-1][i];                
                entries[i].nr[1] = 
                    i + ((count<N) ? matrix[step-1][i+count] : -1) ;
                entries[i].p=i;
            }               
           Arrays.sort(entries, 0, N, new EntryComparator());
           for(int i=0;i<N;i++){
              matrix[step][entries[i].p]=i;           
              if(i>0  
                  && entries[i].nr[0]== entries[i-1].nr[0]        
                  && entries[i].nr[1]==entries[i-1].nr[1]) {
                 matrix[step][entries[i].p]= matrix[step][entries[i-1].p];
              }
           }
        }         
    }

    public static void main(String[] args) {

        String S ="mississippi";
        process(S);
    }

The post says that you can get suffix array from the last row of matrix.
But what is the last row of the matrix ?
For the String "mississippi" I always see array at matrix[N-1] as all zeros.
I cannot see any error in the code also.
Could someone help me figure where I am wrong ?
How can I get the suffix array from the matrix ?

sam manual
  • 127
  • 6

1 Answers1

0

After lot of debugging I got the answer. Pasting code that I worked out for finding suffix arrays:

class Entry  implements Comparable<Entry> {

    int [] nr = new int[2];
    int p=0;

    public int compareTo(Entry that){

        if(this.nr[0] == that.nr[0]) {
            if (this.nr[1] < that.nr[1]) {
                return -1;
            } else {
                return 0;
            }
        }else if (this.nr[0] < that.nr[0] ) {
            return -1;
        } return  0;
    }

}

public class SuffixArray {

    private static final int MAXN = 65536;
    private static final int MAXLG = 17;

    private static Entry [] entries = new Entry[MAXN];
    private static int [][] matrix = new int [MAXLG] [MAXN];

    private static int step=0, count=0;
    private static int N=0;

    public static void process(String S) {

        N = S.length();
        for(int i=0;i<N;i++){
            matrix[0][i] =  (S.charAt(i)-'a');
        }

        for(int i=0;i<MAXLG;i++){
            entries[i] = new Entry();
        }

        for(step=1,count=1; (count>>1)<N;step++, count<<=1) {

            for(int i=0;i<N;i++){
                entries[i].nr[0] = matrix[step-1][i];
                entries[i].nr[1] = (i+count<N)  ? matrix[step-1][i+count] : -1 ;                 
                entries[i].p=i;
            }

            Arrays.sort(entries,0,N);

            for(int i=0;i<N;i++){
                matrix[step][entries[i].p]=i;
                if(i>0 
                    && entries[i].nr[0]== entries[i-1].nr[0] 
                    && entries[i].nr[1]==entries[i-1].nr[1]) {                     
                    matrix[step][entries[i].p]= matrix[step][entries[i-1].p];
                }
            }
        }
    }

    public static void main(String[] args) {
        String S ="banana";
        process(S);
    }
}

S="banana" and N=len(S)= 5

Taking from index 0, the last row in matrix is at N=4.

matrix[4][0....5] is the suffix array

suffix array [3,2,5,1,4,0]

sam manual
  • 127
  • 6