Question

In: Computer Science

How many character comparisons will the BMH algorithm perform to solve the pattern search problem shown...

How many character comparisons will the BMH algorithm perform to solve the pattern search problem shown below?

text:    my next door neighbor is a witch
pattern: is

explain c language

Solutions

Expert Solution

Boyer Moore Horspool :- Boyer Moore Horspool Algorithm is a pattren searching algorithm, basically BMH is uses two approaches to find substrings in a string.

Do not forget to like my work.

1)Bad Character Heuristic

2)Good Suffix Heuristic

text:    my next door neighbor is a witch
pattern: is

I am going to solve this problem using Bad Character Heuristic.

In simple bad character means current character of pattern is not matched with character of string .

Algorithm:-

on wrong match, we shift the pattern until –
1) The wrong match becomes a match
2) Pattern N move past the wrong matched character.

Program in C:-

#include<stdio.h>
# include <string.h>
# include <limits.h>

# define CHAR 140


int max (int first, int second) {
return (first > second)? first: second;
}

void BMH(char *str, int n,int mismatch[CHAR])
{
   int i;
   for (i = 0; i < CHAR; i++)
mismatch[i] = -1;
   for (i = 0; i < n; i++)
       mismatch[(int) str[i]] = i;
}
void search( char *text, char *pattern)
{
   int c = strlen(pattern);
   int d = strlen(text);
int mismatch[CHAR];
   BMH(pattern, c, mismatch);
   int a = 0;
   while(a <= (d - c))
   {
       int j = c-1;
       while(j >= 0 && pattern[j] == text[a+j])
           j--;
       if (j < 0)
       {
           printf("Character compared:=%d",a);
a += (a+c < d)? c-mismatch[text[a+c]] : 1;

       }
       else
           a += max(1, j - mismatch[text[a+j]]);
   }
}
int main(){
   char text[ ] = "my next door neighbor is a witch";
   char pattern[ ] = "is";
   search(text, pattern);
   return 0;
}

Output:-

Character compared:22

Total 22 characters are compared to match pattern

Please hit that thumbs-up or like button to motivate me.>3

Thank you!!


Related Solutions

How much time does an algorithm take to solve a problem of size n if this...
How much time does an algorithm take to solve a problem of size n if this algorithm uses 2n2 + 2n operations, each requiring 10-8 seconds, with these values of n? a) 10: b) 20: c) 50: d) 100
Write an algorithm (flowchart OR Pseudocode) for the following problem Take input character from the user...
Write an algorithm (flowchart OR Pseudocode) for the following problem Take input character from the user unless he enters '$'. Thereafter display how may vowels were entered by the user. Also display the number of each vowel ('a', 'e', 'i', 'o' and 'u') separately. For example if the user enters B a b e c o o d i u g o a l $ Then we have the output below: #A=2 #E=1 #I=1 #O=3 #U=2
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) Requirements Choose one problem with an algorithm and implement it. You should show and explain the result whatever you got. I recommend using N-Queen problem (at least N=8 or more) or any simple perfect games. For example, - N-Queen problem with hill climbing - N-Queen problem with simulated annealing - N-Queen problem with genetic algorithm - Tic-Tac-Toe with Minimax
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) N-Queen problem with genetic algorithm Please use the N-Queen problem (at least N=8 or more) or any simple perfect games. Please provide a screenshot of output and please heavily comment the code. Thanks!
Show that if P=NP then there is a polynomial-time algorithm for the following search problem: given...
Show that if P=NP then there is a polynomial-time algorithm for the following search problem: given a graph, find its largest clique.
Objectives: Learn to analyze a computational problem and construct an algorithm to solve it Learn to...
Objectives: Learn to analyze a computational problem and construct an algorithm to solve it Learn to use sequential statements to build a basic program to implement the algorithm, using correct programming format Learn to use the scanf function to read and store values entered on the keyboard by the user, and the printf function used to display output results on the computer monitor Learn to convert math formulas into correct arithmetic expressions using variables, constants, and library math functions Learn...
Part1: 1.An algorithm is . a) a series of actions that solve a particular problem. b)...
Part1: 1.An algorithm is . a) a series of actions that solve a particular problem. b) an english description of a problem to be solved. c) the process of converting between data types. d) None of the above. 2. Program control is best defined as . a) the degree of control a program has over the computer on which it is executed. b) the line of code that is executing at a given time. c) the order in which a...
perform each of the following steps: a) Read the problem statement. b) Formulate the algorithm using...
perform each of the following steps: a) Read the problem statement. b) Formulate the algorithm using pseudocode and top-down, stepwise refinement. c) Define the algorithm in JavaScript. d) Test, debug and execute the JavaScript. e) Process three complete sets of data. Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording the number of miles driven and the number of gallons used for each tankful. Develop a script...
Can you please answer this problem, this is just for another view: Perform an internet search...
Can you please answer this problem, this is just for another view: Perform an internet search to locate news articles on some of the issues today concerning nursing homes in San Antonio. Write a 2 page, double spaced paper (Minimum pages 2; it can be longer of course) stating what you have discovered from your research is a major issue within this vulnerable community and possible ways to transform/change the problem. Then address your social obligation and ways you might...
how to write a genetic algorithm for a knapsack problem .
how to write a genetic algorithm for a knapsack problem .
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT