Add comments.

This commit is contained in:
Oleksii Trekhleb 2018-06-21 17:59:31 +03:00
parent 81d17bc4f9
commit e558231474

View File

@ -1,10 +1,10 @@
# Z-algorithm # Z Algorithm
The Z-algorithm finds occurrences of a "word" `W` The Z-algorithm finds occurrences of a "word" `W`
within a main "text string" `T` in linear time. within a main "text string" `T` in linear time `O(|W| + |T|)`.
Given a string `S` of length `n`, the algorithm produces Given a string `S` of length `n`, the algorithm produces
an array, `Z` where `Z[i]` represents the ongest substring an array, `Z` where `Z[i]` represents the longest substring
starting from `S[i]` which is also a prefix of `S`. Finding starting from `S[i]` which is also a prefix of `S`. Finding
`Z` for the string obtained by concatenating the word, `W` `Z` for the string obtained by concatenating the word, `W`
with a nonce character, say `$` followed by the text, `T`, with a nonce character, say `$` followed by the text, `T`,
@ -12,7 +12,7 @@ helps with pattern matching, for if there is some index `i`
such that `Z[i]` equals the pattern length, then the pattern such that `Z[i]` equals the pattern length, then the pattern
must be present at that point. must be present at that point.
While the `Z` array can be computed with two nested loops, the While the `Z` array can be computed with two nested loops in `O(|W| * |T|)` time, the
following strategy shows how to obtain it in linear time, based following strategy shows how to obtain it in linear time, based
on the idea that as we iterate over the letters in the string on the idea that as we iterate over the letters in the string
(index `i` from `1` to `n-1`), we maintain an interval `[L,R]` (index `i` from `1` to `n-1`), we maintain an interval `[L,R]`
@ -21,7 +21,42 @@ and `S[L...R]` is a prefix that is also a substring (if no such
interval exists, just let `L=R=-1`). For `i=1`, we can interval exists, just let `L=R=-1`). For `i=1`, we can
simply compute `L` and `R` by comparing `S[0...]` to `S[1...]`. simply compute `L` and `R` by comparing `S[0...]` to `S[1...]`.
**Example of Z array**
```
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
```
Other examples
```
str = a a a a a a
Z[] = x 5 4 3 2 1
```
```
str = a a b a a c d
Z[] = x 1 0 2 1 0 0
```
```
str = a b a b a b a b
Z[] = x 0 6 0 4 0 2 0
```
**Example of Z box**
![z-box](https://ivanyu.me/wp-content/uploads/2014/09/zalg1.png)
## Complexity ## Complexity
- **Time:** `O(|W| + |T|)` - **Time:** `O(|W| + |T|)`
- **Space:** `O(|W|)` - **Space:** `O(|W|)`
## References
- [GeeksForGeeks](https://www.geeksforgeeks.org/z-algorithm-linear-time-pattern-searching-algorithm/)
- [YouTube](https://www.youtube.com/watch?v=CpZh4eF8QBw&t=0s&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8&index=70)
- [Z Algorithm by Ivan Yurchenko](https://ivanyu.me/blog/2013/10/15/z-algorithm/)