Question

Advanced Data Structures Give 3 Pattern/String Matching algorithms. Given a Text and a Pattern, apply the...

Advanced Data Structures

Give 3 Pattern/String Matching algorithms. Given a Text and a Pattern, apply the Boyer-Moore algorithm, The KMP algorithm(this is the one i need help on the most), The Brute Force algorithm and match the pattern in the below string. Also, write the algorithm.

Text: CBADBCACBADCBBACACBCAABCA

Pattern: ACBCAABC

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* C program for Boyer Moore Algorithm with Good Suffix heuristic to find pattern in given text string */

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

// preprocessing for strong good suffix rule
void preprocess_strong_suffix(int *shift, int *bpos,
                               char *pat, int m)
{
   // m is the length of pattern
   int i=m, j=m+1;
   bpos[i]=j;

   while(i>0)
   {
       /*if character at position i-1 is not equivalent to
       character at j-1, then continue searching to right
       of the pattern for border */
       while(j<=m && pat[i-1] != pat[j-1])
       {
           /* the character preceding the occurrence of t in
           pattern P is different than the mismatching character in P,
           we stop skipping the occurrences and shift the pattern
           from i to j */
           if (shift[j]==0)
               shift[j] = j-i;

           //Update the position of next border
           j = bpos[j];
       }
       /* p[i-1] matched with p[j-1], border is found.
       store the beginning position of border */
       i--;j--;
       bpos[i] = j;
   }
}

//Preprocessing for case 2
void preprocess_case2(int *shift, int *bpos,
                   char *pat, int m)
{
   int i, j;
   j = bpos[0];
   for(i=0; i<=m; i++)
   {
       /* set the border position of the first character of the pattern
       to all indices in array shift having shift[i] = 0 */
       if(shift[i]==0)
           shift[i] = j;

       /* suffix becomes shorter than bpos[0], use the position of
       next widest border as value of j */
       if (i==j)
           j = bpos[j];
   }
}

/*Search for a pattern in given text using
Boyer Moore algorithm with Good suffix rule */
void search(char *text, char *pat)
{
   // s is shift of the pattern with respect to text
   int s=0, j;
   int m = strlen(pat);
   int n = strlen(text);

   int bpos[m+1], shift[m+1];

   //initialize all occurrence of shift to 0
   for(int i=0;i<m+1;i++) shift[i]=0;

   //do preprocessing
   preprocess_strong_suffix(shift, bpos, pat, m);
   preprocess_case2(shift, bpos, pat, m);

   while(s <= n-m)
   {

       j = m-1;

       /* Keep reducing index j of pattern while characters of
           pattern and text are matching at this shift s*/
       while(j >= 0 && pat[j] == text[s+j])
           j--;

       /* If the pattern is present at the current shift, then index j
           will become -1 after the above loop */
       if (j<0)
       {
           printf("pattern occurs at shift = %d\n", s);
           s += shift[0];
       }
       else
           /*pat[i] != pat[s+j] so shift the pattern
           shift[j+1] times */
           s += shift[j+1];
   }

}

//Driver
int main()
{
   char text[] = "ABAAAABAACD";
   char pat[] = "ABA";
   search(text, pat);
   return 0;
}

// C++ program for implementation of KMP pattern searching algorithm
#include <bits/stdc++.h>

void computeLPSArray(char* pat, int M, int* lps);

// Prints occurrences of txt[] in pat[]
void KMPSearch(char* pat, char* txt)
{
   int M = strlen(pat);
   int N = strlen(txt);

   // create lps[] that will hold the longest prefix suffix
   // values for pattern
   int lps[M];

   // Preprocess the pattern (calculate lps[] array)
   computeLPSArray(pat, M, lps);

   int i = 0; // index for txt[]
   int j = 0; // index for pat[]
   while (i < N) {
       if (pat[j] == txt[i]) {
           j++;
           i++;
       }

       if (j == M) {
           printf("Found pattern at index %d ", i - j);
           j = lps[j - 1];
       }

       // mismatch after j matches
       else if (i < N && pat[j] != txt[i]) {
           // Do not match lps[0..lps[j-1]] characters,
           // they will match anyway
           if (j != 0)
               j = lps[j - 1];
           else
               i = i + 1;
       }
   }
}

// Fills lps[] for given patttern pat[0..M-1]
void computeLPSArray(char* pat, int M, int* lps)
{
   // length of the previous longest prefix suffix
   int len = 0;

   lps[0] = 0; // lps[0] is always 0

   // the loop calculates lps[i] for i = 1 to M-1
   int i = 1;
   while (i < M) {
       if (pat[i] == pat[len]) {
           len++;
           lps[i] = len;
           i++;
       }
       else // (pat[i] != pat[len])
       {
           // This is tricky. Consider the example.
           // AAACAAAA and i = 7. The idea is similar
           // to search step.
           if (len != 0) {
               len = lps[len - 1];

               // Also, note that we do not increment
               // i here
           }
           else // if (len == 0)
           {
               lps[i] = 0;
               i++;
           }
       }
   }
}

// Driver program to test above function
int main()
{
   char txt[] = "ABABDABACDABABCABAB";
   char pat[] = "ABABCABAB";
   KMPSearch(pat, txt);
   return 0;
}

// C++ program for Naive Pattern Searching algorithm
#include <bits/stdc++.h>
using namespace std;

void search(char* pat, char* txt)
{
   int M = strlen(pat);
   int N = strlen(txt);

   /* A loop to slide pat[] one by one */
   for (int i = 0; i <= N - M; i++) {
       int j;

       /* For current index i, check for pattern match */
       for (j = 0; j < M; j++)
           if (txt[i + j] != pat[j])
               break;

       if (j == M) // if pat[0...M-1] = txt[i, i+1, ...i+M-1]
           cout << "Pattern found at index "
               << i << endl;
   }
}

// Driver Code
int main()
{
   char txt[] = "AABAACAADAABAAABAA";
   char pat[] = "AABA";
   search(pat, txt);
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Advanced Data Structures Give 3 Pattern/String Matching algorithms. Given a Text and a Pattern, apply the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Give 3 Pattern/String Matching algorithms. Given a Text and a Pattern, apply the Boyer-Moore algorithm, The...

    Give 3 Pattern/String Matching algorithms. Given a Text and a Pattern, apply the Boyer-Moore algorithm, The KMP algorithm, The Brute Force algorithm and match the pattern in the below string. Also, write the algorithm. Text: CBADBCACBADCBBACACBCAABCA Pattern: ACBCAABC Answer the entire question Step by Step written out not typed. Don't answer if your answering part of question, typing solution, or guessing it.

  • Task Algorithms: Pattern Matching (in java) Write a program that gets two strings from user, size...

    Task Algorithms: Pattern Matching (in java) Write a program that gets two strings from user, size and pattern, and checks if pattern exists inside size, if it exists then program returns index of first character of pattern inside size, otherwise it returns -1. The method should not use built-in methods such as indexOf , find, etc. Only charAt and length are allowed to use. Analyze the time complexity of your algorithm. Your solution is not allowed to be> = O...

  • If the length of the array P is 4 and the length of the array T...

    If the length of the array P is 4 and the length of the array T is 14, how many shifts of P need to be performed in the string searching algorithm? a. 9 b. 10 c. 11 d. 12 What is the best case for the naive string search algorithm? a. The first character of the pattern P isn't present in text T b. All characters in pattern P are different c. All characters in text T are different...

  • Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P...

    Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P inside a (generally much larger) text T. The goal is to implement and compare four classical string-matching algorithms. Input: Your code should work for any text either inputted directly or read in from a file. However, for testing - input file has been provided: The Gettysburg Address (by President Abraham Lincoln, 1863) You should minimally search for these three patterns in each text: FREE,...

  • Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P...

    Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P inside a (generally much larger) text T. The goal is to implement and compare four classical string-matching algorithms. Input: Your code should work for any text either inputted directly or read in from a file. However, for testing - input file has been provided: The Gettysburg Address (by President Abraham Lincoln, 1863) You should minimally search for these three patterns in each text: FREE,...

  • Learn how to use the advanced data structures (such as: list, tuple, string, dictionary, and set)...

    Learn how to use the advanced data structures (such as: list, tuple, string, dictionary, and set) ·         Learn and practice how to use functions. ·         Learn and practice how to use file I/Os. ·         Appreciate the importance of data validations. ·         Provide appropriate documentation and good programming style. Python Programming Description of the problem: Write a “censor” program that first reads a file with “bad words” such as “sex”, “drug”, “rape”, “kill”, and so on, places them in a set, and then reads an...

  • Java - Data structures and algorithms VI. Matching Game Consider a matching game in which you...

    Java - Data structures and algorithms VI. Matching Game Consider a matching game in which you have a list of random integer values between 10 and 99. You remove from the list any pair of consecutive integers that match. If first integer has digits xly1 and the second integer has digits x2y2 the match is found if any ofthe following is true xi is the same as x2 x1 is the same as y2 yl is the same as x2...

  • JAVA DATA STRUCTURES Write a line-based text editor. The command syntax is similar to the Unix...

    JAVA DATA STRUCTURES Write a line-based text editor. The command syntax is similar to the Unix line editor ed. The internal copy of the file is maintained as a linked list of lines. To be able to go up and down in the file, you have to maintain a doubly linked list. Most commands are represented by a one-character string. Some are two characters and require an argument (or two). Support the commands shown below: Command Function Go to the...

  • Coding for Python - The pattern detection problem – part 3: def pattern_search_max(data_series, pattern, threshold) Plea...

    Coding for Python - The pattern detection problem – part 3: def pattern_search_max(data_series, pattern, threshold) Please do not use 'print' or 'input' statements. Context of the assignment is: In this assignment, your goal is to write a Python program to determine whether a given pattern appears in a data series, and if so, where it is located in the data series. Please see attachments below: We need to consider the following cases: Case 1 - It is possible that the...

  • In this assignment you will practice using Data Structures and Object Oriented concepts in Java. Your implementation should target the most efficient algorithms and data structures. You will be graded...

    In this assignment you will practice using Data Structures and Object Oriented concepts in Java. Your implementation should target the most efficient algorithms and data structures. You will be graded based on the efficiency of your implementation. You will not be awarded any points if you use simple nested loops to implement the below tasks. You should use one or more of the below data structures: - ArrayList : - JavaDoc: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html - Tutorial: http://docs.oracle.com/javase/tutorial/collections/interfaces/list.html Question You are provided with...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT