Pattern Searching

Last Updated : 03 Apr, 2024

Pattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Pattern searching algorithms play important role in tasks such as text processing, data mining, and information retrieval.

Pattern-Searching

Patten Searching

What is Pattern Searching?

Pattern searching is a fundamental operation in data structures and algorithms. It involves finding a specific pattern or substring within a given text or string. Efficient pattern searching algorithms are essential for various applications, including text processing, information retrieval, and bioinformatics.

Important Pattern Searching Algorithms:

1. Naive String Matching

The naive string matching algorithm is the simplest pattern searching algorithm. It compares the pattern with every possible substring of the text. The time complexity of this algorithm is O(mn), where m is the length of the pattern and n is the length of the text.

2. Knuth-Morris-Pratt (KMP) Algorithm

The KMP algorithm is a heuristic algorithm that uses a precomputed failure function to skip unnecessary comparisons. The failure function indicates the length of the longest proper prefix of the pattern that is also a suffix of the current substring. The time complexity of the KMP algorithm is O(m + n), where m is the length of the pattern and n is the length of the text.

3. Rabin-Karp Algorithm

The Rabin-Karp algorithm is a heuristic algorithm that uses hashing to compare the pattern with the text. It computes a hash value for the pattern and for each substring of the text. If the hash values match, it performs a character-by-character comparison to confirm the match. The time complexity of the Rabin-Karp algorithm is O(m + n), where m is the length of the pattern and n is the length of the text.

4. Aho-Corasick Algorithm

The Aho-Corasick algorithm is a deterministic finite automaton (DFA) based algorithm that is used for multiple pattern searching. It constructs a DFA from a set of patterns and then uses the DFA to find all occurrences of the patterns in the text. The time complexity of the Aho-Corasick algorithm is O(m + n), where m is the total length of all patterns and n is the length of the text.

Applications of Pattern Searching

Pattern searching algorithms have numerous applications, including:

  • Text Processing: Searching for keywords in a document, finding and replacing text, spell checking, and plagiarism detection.
  • Information Retrieval: Finding relevant documents in a database, web search, and data mining.
  • Bioinformatics: Searching for DNA sequences in a genome, protein analysis, and gene expression analysis.
  • Network Security: Detecting malicious patterns in network traffic, intrusion detection, and malware analysis.
  • Data Mining: Identifying patterns in large datasets, customer segmentation, and fraud detection.

Learn Basics of Pattern Searching:

Standard Pattern Searching Algorithms:

Practice Problems on Pattern Searching:

Quick Links:

Recommended:



Share your thoughts in the comments

Similar Reads

Numpy | Sorting, Searching and Counting
.numpy-table { font-family: arial, sans-serif; border-collapse: collapse; border: 1px solid #5fb962; width: 100%; } .numpy-table td, th { background-color: #c6ebd9; border: 1px solid #5fb962; text-align: left; padding: 8px; } .numpy-table td:nth-child(odd) { background-color: #c6ebd9; } .numpy-table th { border: 1px solid #5fb962; text-align: left;
6 min read
Pattern Searching using C++ library
Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function that prints all occurrences of pat[] in txt[]. You may assume that n > m.Examples: Input : txt[] = "geeks for geeks" pat[] = "geeks" Output : Pattern found at index 0 Pattern found at index 10 Input : txt[] = "aaaa" pat[] = "aa" Output : Pattern found at index 0 Pattern found a
3 min read
C Program for KMP Algorithm for Pattern Searching[duplicate]
C/C++ Code // 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 // value
2 min read
Python Program for KMP Algorithm for Pattern Searching[duplicate]
Python Code # Python program for KMP Algorithm def KMPSearch(pat, txt): M = len(pat) N = len(txt) # create lps[] that will hold the longest prefix suffix # values for pattern lps = [0]*M j = 0 # index for pat[] # Preprocess the pattern (calculate lps[] array) computeLPSArray(pat, M, lps) i = 0 # index for txt[] while i < N: if pat[j] == txt[i]:
2 min read
C Program for Rabin-Karp Algorithm for Pattern Searching
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 ind
3 min read
Java Program for Rabin-Karp Algorithm for Pattern Searching
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 ind
3 min read
Python Program for Rabin-Karp Algorithm for Pattern Searching
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 inde
3 min read
PHP Program for Rabin-Karp Algorithm for Pattern Searching
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 ind
3 min read
Real time optimized KMP Algorithm for Pattern Searching
In the article, we have already discussed the KMP algorithm for pattern searching. In this article, a real-time optimized KMP algorithm is discussed. From the previous article, it is known that KMP(a.k.a. Knuth-Morris-Pratt) algorithm preprocesses the pattern P and constructs a failure function F(also called as lps[]) to store the length of the lon
7 min read
Rabin-Karp algorithm for Pattern Searching in Matrix
Given matrices txt[][] of dimensions m1 x m2 and pattern pat[][] of dimensions n1 x n2, the task is to check whether a pattern exists in the matrix or not, and if yes then print the top most indices of the pat[][] in txt[][]. It is assumed that m1, m2 ? n1, n2 Examples: Input: txt[][] = {{G, H, I, P} {J, K, L, Q} {R, G, H, I} {S, J, K, L} } pat[][]
17 min read