C Programming: Z-Algorithm 🎯

beginner
25 min

C Programming: Z-Algorithm 🎯

Welcome to the fascinating world of C Programming! Today, we're going to dive deep into the Z-Algorithm, a powerful string-matching algorithm. Let's get started!

Understanding the Z-Algorithm 📝

The Z-Algorithm is a linear time algorithm used to find all occurrences of a pattern in a text. It's particularly useful in areas such as bioinformatics, data compression, and more!

Why the Z-Algorithm? 💡

  1. Efficient: The Z-Algorithm runs in linear time, O(n), making it faster than other string-matching algorithms like Knuth-Morris-Pratt (KMP) and Rabin-Karp.
  2. Simple: It has a straightforward implementation, making it easy to understand and implement.
  3. Versatile: The Z-Algorithm can be used to solve various problems, such as finding the longest prefix-suffix of a string.

Implementing the Z-Algorithm 🎯

Here's a step-by-step guide to implement the Z-Algorithm in C:

  1. Initialize the Z-array: The Z-array is an array of integers the same size as the pattern. Its purpose is to store the length of the longest prefix that is also a suffix of the substring from index i to the end of the pattern.
c
char pattern[100]; // Your pattern here int z[100]; // Z-array
  1. Fill the Z-array: Start from the rightmost index and work your way to the left, computing the Z-values.
c
int right = 0, length = strlen(pattern); for (int i = length - 1; i >= 0; i--) { // Find the maximum of Z[i+1] and length of the pattern minus i-1 (if i+1 is valid) int z_length = right > i ? min(right - i, z[i + 1]) : 0; // Find the length of the common suffix between pattern[i] and pattern[i+z_length+1...end] int k; for (k = i + z_length; k <= right; k--) { if (pattern[k] != pattern[i + z_length]) break; } // Update Z-value for current position z[i] = k - i - z_length; // Update the right boundary if (i + z_length > right) right = i + z_length; }
  1. Search for the pattern: Now that we have the Z-array, we can find all occurrences of the pattern in the text.
c
char text[1000]; // Your text here int n = strlen(text); int start_index; for (int i = 0; i <= n - length; i++) { int j; for (j = 0; j < length; j++) { if (text[i + j] != pattern[j]) break; } // If pattern found exactly if (j == length) { printf("Pattern found at index: %d\n", i); // Find next occurrence of the pattern start_index = i + length; for (i = start_index; i <= n - length; i++) { if (z[i - start_index] < length - z[start_index - 1]) break; printf("Pattern found at index: %d\n", i); start_index = i + length; } } }

Practical Example 🎯

Let's try the Z-Algorithm with a practical example:

c
char pattern[] = "ACTGACTGACTG"; char text[] = "ATCGATCGATCGATCGACTGACTGACTGACTGACTGACTGACTGACTG";

After running the code, we find the pattern at indices 17, 35, and 53.

Quiz Time 🎯

Quick Quiz
Question 1 of 1

What is the time complexity of the Z-Algorithm?

Wrapping Up 🎯

That's it for the Z-Algorithm! This powerful string-matching algorithm can help you solve complex problems in C Programming. Keep practicing, and remember to apply the concepts we've covered today! Happy coding! 🚀