C Programming: Boyer-Moore Algorithm

beginner
21 min

C Programming: Boyer-Moore Algorithm

Welcome to a comprehensive guide on the Boyer-Moore Algorithm, a powerful string search algorithm, in the world of C programming! Let's dive into the fascinating world of string searching algorithms and learn how to implement the Boyer-Moore Algorithm in your C programs.

šŸŽÆ Objective: By the end of this tutorial, you'll understand the Boyer-Moore Algorithm and be able to implement it in your C programs for efficient string search operations.

Introduction

In this lesson, we'll explore:

  • What is the Boyer-Moore Algorithm?
  • Why use the Boyer-Moore Algorithm?
  • How the Boyer-Moore Algorithm works

What is the Boyer-Moore Algorithm?

The Boyer-Moore Algorithm is a string search algorithm, developed by Robert S. Boyer and J. Stolfi in 1977, that provides linear search time complexity (O(n)). It's an improvement over the traditional naive string search algorithm, which has a search time complexity of O(n²).

Why Use the Boyer-Moore Algorithm?

The Boyer-Moore Algorithm's primary advantage is its efficiency. It's designed to minimize the number of comparisons between the pattern and the text by using some heuristics. This results in significant time savings, especially for long patterns and large texts.

How the Boyer-Moore Algorithm Works

The Boyer-Moore Algorithm consists of two main components:

  1. Good Suffix Shift: This technique shifts the pattern as much as possible without overwriting any character in the text that is already aligned with the pattern's last character.

  2. Bad Character Shift: If a character from the pattern matches with a character in the text but the character before it doesn't, we call this a "bad character." The algorithm uses the "Bad Character Shift" heuristic to move the pattern to the right by a distance equal to the length of the pattern minus the index of the bad character, if possible.

Now that we have a basic understanding of the Boyer-Moore Algorithm, let's implement it in C!

Implementing the Boyer-Moore Algorithm in C

In this section, we'll provide two complete examples: a simple implementation and an optimized implementation using the "Bad Character Shift" heuristic.

Example 1: Simple Implementation

c
#include <stdio.h> #include <string.h> void boyer_moore_simple(char *pattern, char *text) { int m = strlen(pattern); int n = strlen(text); int i, j; for (i = 0; i < n - m + 1; ) { for (j = m - 1; j >= 0 && pattern[j] == text[i + j]; j--); if (j == -1) { i += m; printf("Match found at position %d\n", i); } else { i += j + 1; } } }

Example 2: Optimized Implementation

c
#include <stdio.h> #include <string.h> void boyer_moore_bad_character(char *pattern, char *text) { int m = strlen(pattern); int n = strlen(text); int bad_char_shift[256]; int i, j; for (i = 0; i < 256; i++) bad_char_shift[i] = m; for (i = 0; i < m - 1; i++) { char c = pattern[i]; for (j = i + 1; j < m; j++) bad_char_shift[pattern[j]] = min(bad_char_shift[pattern[j]], m - j); bad_char_shift[c] = min(bad_char_shift[c], i); } int i_text = 0; int i_pattern = 0; while (i_text < n) { for (i_pattern = m - 1; i_pattern >= 0 && pattern[i_pattern] == text[i_text + i_pattern]; i_pattern--); if (i_pattern == -1) { i_text += m; printf("Match found at position %d\n", i_text); } else { i_text += bad_char_shift[text[i_text + i_pattern]] + 1; i_pattern = min(i_pattern, m - 1); } } }

šŸ“ Note: The min function is not provided in the C standard library, so you can implement it as follows:

c
int min(int a, int b) { return (a < b) ? a : b; }

Practical Applications

The Boyer-Moore Algorithm is useful in various applications, such as text editors, web browsers, and search engines, where efficient string search operations are essential.

Quiz

Quick Quiz
Question 1 of 1

What is the time complexity of the Boyer-Moore Algorithm in the worst-case scenario?

Conclusion

In this tutorial, we've learned about the Boyer-Moore Algorithm, a powerful string search algorithm in C programming. By understanding the Good Suffix Shift and Bad Character Shift techniques, we've implemented two versions of the algorithm and explored its practical applications.

We hope this tutorial has been helpful and encouraging! As you continue to learn and practice, you'll become more proficient in implementing efficient string search algorithms in C. Happy coding! šŸŽ‰