Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] in txt[]. You may assume that n > m.

Examples:

Input:  txt[] = "THIS IS A TEST TEXT"
        pat[] = "TEST"
Output: Pattern found at index 10

Input:  txt[] =  "AABAACAADAABAABA"
        pat[] =  "AABA"
Output: Pattern found at index 0
        Pattern found at index 9
        Pattern found at index 12
C programming for Pattern Searching Set 7 Boyer Moore Algorithm – Bad Character Heuristic

Pattern searching is an important problem in computer science. When we do search for a string in notepad/word file or browser or database, pattern searching algorithms are used to show the search results.

[ad type=”banner”]

We have discussed the following algorithms in the previous posts:

Naive Algorithm
KMP Algorithm
Rabin Karp Algorithm
Finite Automata based Algorithm

In this post, we will discuss Boyer Moore pattern searching algorithm. Like KMP and Finite Automata algorithms, Boyer Moore algorithm also preprocesses the pattern.
Boyer Moore is a combination of following two approaches.
1) Bad Character Heuristic
2) Good Suffix Heuristic
Both of the above heuristics can also be used independently to search a pattern in a text. Let us first understand how two independent approaches work together in the Boyer Moore algorithm. If we take a look at the Naive algorithm, it slides the pattern over the text one by one. KMP algorithm does preprocessing over the pattern so that the pattern can be shifted by more than one. The Boyer Moore algorithm does preprocessing for the same reason. It preporcesses the pattern and creates different arrays for both heuristics. At every step, it slides the pattern by max of the slides suggested by the two heuristics. So it uses best of the two heuristics at every step. Unlike the previous pattern searching algorithms, Boyer Moore algorithm starts matching from the last character of the pattern.

[ad type=”banner”]

In this post, we will discuss bad character heuristic, and discuss Good Suffix heuristic in the next post.

The idea of bad character heuristic is simple. The character of the text which doesn’t match with the current character of pattern is called the Bad Character. Whenever a character doesn’t match, we slide the pattern in such a way that aligns the bad character with the last occurrence of it in pattern. We preprocess the pattern and store the last occurrence of every possible character in an array of size equal to alphabet size. If the character is not present at all, then it may result in a shift by m (length of pattern). Therefore, the bad character heuristic takes O(n/m) time in the best case.

[pastacode lang=”c” manual=”%2F*%20Program%20for%20Bad%20Character%20Heuristic%20of%20Boyer%20Moore%20String%20Matching%20Algorithm%20*%2F%0A%20%0A%23%20include%20%3Climits.h%3E%0A%23%20include%20%3Cstring.h%3E%0A%23%20include%20%3Cstdio.h%3E%0A%20%0A%23%20define%20NO_OF_CHARS%20256%0A%20%0A%2F%2F%20A%20utility%20function%20to%20get%20maximum%20of%20two%20integers%0Aint%20max%20(int%20a%2C%20int%20b)%20%7B%20return%20(a%20%3E%20b)%3F%20a%3A%20b%3B%20%7D%0A%20%0A%2F%2F%20The%20preprocessing%20function%20for%20Boyer%20Moore’s%20bad%20character%20heuristic%0Avoid%20badCharHeuristic(%20char%20*str%2C%20int%20size%2C%20int%20badchar%5BNO_OF_CHARS%5D)%0A%7B%0A%20%20%20%20int%20i%3B%0A%20%0A%20%20%20%20%2F%2F%20Initialize%20all%20occurrences%20as%20-1%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20NO_OF_CHARS%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20badchar%5Bi%5D%20%3D%20-1%3B%0A%20%0A%20%20%20%20%2F%2F%20Fill%20the%20actual%20value%20of%20last%20occurrence%20of%20a%20character%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20size%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20badchar%5B(int)%20str%5Bi%5D%5D%20%3D%20i%3B%0A%7D%0A%20%0A%2F*%20A%20pattern%20searching%20function%20that%20uses%20Bad%20Character%20Heuristic%20of%0A%20%20%20Boyer%20Moore%20Algorithm%20*%2F%0Avoid%20search(%20char%20*txt%2C%20%20char%20*pat)%0A%7B%0A%20%20%20%20int%20m%20%3D%20strlen(pat)%3B%0A%20%20%20%20int%20n%20%3D%20strlen(txt)%3B%0A%20%0A%20%20%20%20int%20badchar%5BNO_OF_CHARS%5D%3B%0A%20%0A%20%20%20%20%2F*%20Fill%20the%20bad%20character%20array%20by%20calling%20the%20preprocessing%0A%20%20%20%20%20%20%20function%20badCharHeuristic()%20for%20given%20pattern%20*%2F%0A%20%20%20%20badCharHeuristic(pat%2C%20m%2C%20badchar)%3B%0A%20%0A%20%20%20%20int%20s%20%3D%200%3B%20%20%2F%2F%20s%20is%20shift%20of%20the%20pattern%20with%20respect%20to%20text%0A%20%20%20%20while(s%20%3C%3D%20(n%20-%20m))%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20j%20%3D%20m-1%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Keep%20reducing%20index%20j%20of%20pattern%20while%20characters%20of%0A%20%20%20%20%20%20%20%20%20%20%20pattern%20and%20text%20are%20matching%20at%20this%20shift%20s%20*%2F%0A%20%20%20%20%20%20%20%20while(j%20%3E%3D%200%20%26%26%20pat%5Bj%5D%20%3D%3D%20txt%5Bs%2Bj%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20j–%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20If%20the%20pattern%20is%20present%20at%20current%20shift%2C%20then%20index%20j%0A%20%20%20%20%20%20%20%20%20%20%20will%20become%20-1%20after%20the%20above%20loop%20*%2F%0A%20%20%20%20%20%20%20%20if%20(j%20%3C%200)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20printf(%22%5Cn%20pattern%20occurs%20at%20shift%20%3D%20%25d%22%2C%20s)%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20Shift%20the%20pattern%20so%20that%20the%20next%20character%20in%20text%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20aligns%20with%20the%20last%20occurrence%20of%20it%20in%20pattern.%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20The%20condition%20s%2Bm%20%3C%20n%20is%20necessary%20for%20the%20case%20when%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pattern%20occurs%20at%20the%20end%20of%20text%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20s%20%2B%3D%20(s%2Bm%20%3C%20n)%3F%20m-badchar%5Btxt%5Bs%2Bm%5D%5D%20%3A%201%3B%0A%20%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20Shift%20the%20pattern%20so%20that%20the%20bad%20character%20in%20text%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20aligns%20with%20the%20last%20occurrence%20of%20it%20in%20pattern.%20The%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20max%20function%20is%20used%20to%20make%20sure%20that%20we%20get%20a%20positive%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20shift.%20We%20may%20get%20a%20negative%20shift%20if%20the%20last%20occurrence%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20of%20bad%20character%20in%20pattern%20is%20on%20the%20right%20side%20of%20the%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20current%20character.%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20s%20%2B%3D%20max(1%2C%20j%20-%20badchar%5Btxt%5Bs%2Bj%5D%5D)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20funtion%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20char%20txt%5B%5D%20%3D%20%22ABAAABCD%22%3B%0A%20%20%20%20char%20pat%5B%5D%20%3D%20%22ABC%22%3B%0A%20%20%20%20search(txt%2C%20pat)%3B%0A%20%20%20%20return%200%3B%0A%7D%0A” message=”C” highlight=”” provider=”manual”/]

 

Output:

pattern occurs at shift = 4
The Bad Character Heuristic may take O(mn) time in worst case. The worst case occurs when all characters of the text and pattern are same. For example, txt[] = “AAAAAAAAAAAAAAAAAA” and pat[] = “AAAAA”.

[ad type=”banner”]