garimasingh128 / CP-DSA-Cpp-C

🕺 Give me data and I will structure it! 🔥
59 stars 100 forks source link

boyer-moore-algorithm-for-pattern-searching.cpp #5

Closed nitintayal008 closed 3 years ago

nitintayal008 commented 3 years ago

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. A typical problem statement would be- 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. 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 processes the pattern and creates different arrays for both heuristics. At every step, it slides the pattern by the 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.

pull-assistant[bot] commented 3 years ago
Score: 1.00

Best reviewed: commit by commit


Optimal code review plan

     boyer-moore-algorithm-for-pattern-searching.cpp

Powered by Pull Assistant. Last update f7428ac ... f7428ac. Read the comment docs.