Z-Algorithm

@z

Z-Algorithm is a string searching algorithm that uses a preprocessed array to find all occurrences of a pattern within a main string. It is particularly useful for finding all occurrences of a pattern in a long string, as it allows for efficient searching and multiple match detection.

Pseudo-code
function Z(string T):
  T := Preprocess string T to create a Z array.
  for i from 0 to n-1:
    for j from i+1 to n-1:
      if P[j] matches P[i] and the remaining characters match:
        update the Z array to record the longest match.
  return the Z array.
function Preprocess(string T):
  Z := Initialize an empty Z array of size n.
  for i from 0 to n-1:
    Z[i] := Find the longest substring ending at i that matches a prefix of T.
  return Z.
                
  • T The main string being searched.
  • P The pattern string that is being searched for.
  • m The length of the main string T.
  • n The length of the pattern string P.
Recipe Steps
1

Imagine you have a long string of letters and a shorter string of letters that you want to find inside the long string. First, write down the long string and the shorter string.

2

Then, create a new table next to the long string and fill it with zeros. This table will help us find where the shorter string appears in the long string.

3

Now, go through the long string and the shorter string one letter at a time. When the letters match, fill in the corresponding cell in the table with a value that indicates how many letters match so far.

4

If the letters don't match, reset the value in the table to zero and start checking again from the beginning of the shorter string.

5

Repeat steps 3 and 4 until you have checked all letters in the long string.

6

Finally, examine the table to find where the shorter string appears in the long string.

Questions & Answers

The Z-Algorithm is used to find all occurrences of a pattern within a main string.

The Z array is used to record the longest match of the pattern string within the main string.

The Z-Algorithm allows for efficient searching and multiple match detection.
- The Z-Algorithm can be slow for very large strings and patterns. Consider using other algorithms like Knuth-Morris-Pratt or Rabin-Karp for larger inputs.
- The Z-Algorithm assumes that the pattern string is a substring of the main string. If this is not the case, you may need to modify the algorithm or use a different approach.


Related Algorithms
Binary Search

Binary Search is a very fast algorithm used to find a value in a sorted list by repeatedly cutting the search space in half until the item is found or nothing is left.

View
Counting Sort

Counting Sort is a sorting algorithm that does not compare elements. Instead, it counts how many times each number appears and uses that information to place elements directly in their correct position.

View
Dijkstra's Algorithm

Dijkstra's Algorithm finds the shortest path from one starting node to all other nodes in a graph by always expanding the currently closest node first.

View