samedi 1 décembre 2018

Why does the longest prefix which is also suffix calculation part in the KMP have a time complexity of O(n) and not O(n^2)?

I was going through the code of KMP when I noticed the Longest Prefix which is also suffix calculation part of KMP. Here is how it goes,

void computeLPSArray(char* pat, int M, int* lps) 
{ 
    int len = 0; 

    lps[0] = 0;  

    int i = 1; 
    while (i < M) { 
       if (pat[i] == pat[len]) { 
          len++; 
          lps[i] = len; 
          i++; 
       } 
       else 
       { 
          if (len != 0) { 
              len = lps[len - 1]; //<----I am referring to this part

          } 
          else 
          { 
              lps[i] = 0; 
              i++; 
          } 
      } 
  } 
}

Now the part where I got confused was the one which I have shown in comments in the above code. Now we do know that when a code contains a loop like the following

int a[m];
memset(a, 0, sizeof(a));
for(int i = 0; i<m; i++){
   for(int j = i; j>=0; j--){
       a[j] = a[j]*2;//This inner loop is causing the same cells in the 1 
                     //dimensional array to be visited more than once. 
   }
}

The complexity comes out to be O(m*m). Similarly if we write the above LPS computation in the following format

while(i<M){
   if{....}
   else{
      if(len != 0){
          //doesn't this part cause the code to again go back a few elements
          //in the LPS array the same way as the inner loop in my above 
          //written nested for loop does? Shouldn't that mean the same cell
          //in the array is getting visited more than once and hence the 
          //complexity should increase to O(n^2)?
      }
   }

}

It might be that the way I think complexities are calculated is wrong. So please clarify.

Aucun commentaire:

Enregistrer un commentaire