Z Algorithm Basic

  1. #include<bits/stdc++.h>  
  2. using namespace std;  
  3. #define ll long long int  
  4. void getZarray(string str, int z[]){  
  5.     int n=str.length();  
  6.     int left,right,k;  
  7.     left=right=0;  
  8.     for(int i=1;i<n;i++){  
  9.         if(i>right){  
  10.             left=right=i;  
  11.             while(right<n && str[right-left]==str[right]){  
  12.                 right++;  
  13.             }  
  14.             z[i]=right-left;  
  15.             right--;  
  16.         }else{  
  17.             k=i-left;  
  18.             if(z[k]<right-i+1){  
  19.                 z[i]=z[k];  
  20.             }else{  
  21.                 left=i;  
  22.                 while(right<n && str[right-left]==str[right]){  
  23.                     right++;  
  24.                 }  
  25.                 z[i]=right-left;  
  26.                 right--;  
  27.             }  
  28.         }  
  29.     }  
  30. }  
  31. void search(string text,string pattern){  
  32.     string concat=pattern+"$"+text;  
  33.     int len=concat.length();  
  34.     int z[len];  
  35.     getZarray(concat,z);  
  36.     int flag=0;  
  37.     for(int i=0;i<len;i++){  
  38.         if(z[i]==pattern.length()){  
  39.             cout<<"Pattern found at index "<<i-pattern.length()-1<<endl;  
  40.             flag=1;  
  41.         }  
  42.     }  
  43.     if(!flag)  
  44. }  
  45. int main(){  
  46.     //freopen("input.txt","r",stdin);  
  47.     //freopen("output.txt","w",stdin);  
  48.     string text,pattern;  
  49.     while(true){  
  50.         cin>>text>>pattern;  
  51.         search(text,pattern);  
  52.     }  
  53.     return 0;  
  54. }  

Z algorithm (Linear time pattern searching Algorithm)

This algorithm finds all occurrences of a pattern in a text in linear time. Let length of text be n and of pattern be m, then total time taken is O(m + n) with linear space complexity. Now we can see that both time and space complexity is same as KMP algorithm but this algorithm is Simpler to understand.
In this algorithm, we construct a Z array.
What is Z Array?
For a string str[0..n-1], Z array is of same length as string. An element Z[i] of Z array stores length of the longest substring starting from str[i] which is also a prefix of str[0..n-1]. The first entry of Z array is meaning less as complete string is always prefix of itself.


Example:
Index            0   1   2   3   4   5   6   7   8   9  10  11 
Text             a   a   b   c   a   a   b   x   a   a   a   z
Z values         X   1   0   0   3   1   0   0   2   2   1   0 
More Examples:
str  = "aaaaaa"
Z[]  = {x, 5, 4, 3, 2, 1}

str = "aabaacd"
Z[] = {x, 1, 0, 2, 1, 0, 0}

str = "abababab"
Z[] = {x, 0, 6, 0, 4, 0, 2, 0}
 
How is Z array helpful in Searching Pattern in Linear time?
The idea is to concatenate pattern and text, and create a string “P$T” where P is pattern, $ is a special character should not be present in pattern and text, and T is text. Build the Z array for concatenated string. In Z array, if Z value at any point is equal to pattern length, then pattern is present at that point.
Example:
Pattern P = "aab",  Text T = "baabaa"

The concatenated string is = "aab$baabaa"

Z array for above concatenated string is {x, 1, 0, 0, 0, 
                                          3, 1, 0, 2, 1}.
Since length of pattern is 3, the value 3 in Z array 
indicates presence of pattern. 
How to construct Z array?
     A Simple Solution is two run two nested loops, the outer loop goes to every index and the inner loop finds length of the longest prefix that matches substring starting at current index. The time complexity of this solution is O(n2).
      We can construct Z array in linear time.
The idea is to maintain an interval [L, R] which is the interval with max R
such that [L,R] is prefix substring (substring which is also prefix). 

Steps for maintaining this interval are as follows – 

1) If i > R then there is no prefix substring that starts before i and 
   ends after i, so we reset L and R and compute new [L,R] by comparing 
   str[0..] to str[i..] and get Z[i] (= R-L+1).

2) If i <= R then let K = i-L,  now Z[i] >= min(Z[K], R-i+1)  because 
   str[i..] matches with str[K..] for atleast R-i+1 characters (they are in
   [L,R] interval which we know is a prefix substring).     
   Now two sub cases arise – 
      a) If Z[K] < R-i+1  then there is no prefix substring starting at 
         str[i] (otherwise Z[K] would be larger)  so  Z[i] = Z[K]  and 
         interval [L,R] remains same.
      b) If Z[K] >= R-i+1 then it is possible to extend the [L,R] interval
         thus we will set L as i and start matching from str[R]  onwards  and
         get new R then we will update interval [L,R] and calculate Z[i] (=R-L+1).
For better understanding of above step by step procedure please check this animation – http://www.utdallas.edu/~besp/demo/John2010/z-algorithm.htm

No comments:

Post a Comment