DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment 9
Student Name Vishal UID 22BCS14496
Branch: BE-CSE Section/Group: 22BCS_IOT_639-B
Semester: 5th Date of Performance: 16 OCT, 2024
Subject Name: DAA Lab Subject Code: 22CHS-311
1. Aim: Develop a program and analyze complexity to find all occurrences of
a pattern P in a given strings.
2. Objective: To develop a program to find all occurrences of a pattern P in a string S, and
analyze the time complexity based on different algorithms (e.g., brute force, KMP, or
Boyer-Moore). Compare performance for varying pattern lengths and input sizes.
3. Algorithm:
Step 1: Build LPS Array (Longest Prefix Suffix)
• Initialize an array lps[] of size m (length of P).
• Set lps[0] = 0 and iterate through P to fill the rest of the lps[] values, where
lps[i] represents the longest proper prefix which is also a suffix for the
substring P[0…i].
• Time complexity: O(m).
Step 2: Start Matching P in S
• Set two pointers, i = 0 for S and j = 0 for P.
• Traverse through the string S. If P[j] == S[i], increment both i and j.
• If there is a mismatch, use lps[] to update j (i.e., set j = lps[j-1]), without
resetting i.
Step 3: Record Occurrence
• If j reaches the length of P (i.e., j == m), a full pattern match is found. Record
the index i - j as the occurrence.
• Then, reset j = lps[j-1] to continue searching.
Step 4: End the Search
• Continue until i traverses the entire string S.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
4. Implementation/Code:
#include <iostream>
#include <vector>
using namespace std;
// Function to build the LPS (Longest Prefix Suffix) array
vector<int> buildLPS(const string& P) {
int m = P.length();
vector<int> lps(m, 0);
int len = 0; // length of previous longest prefix suffix
int i = 1;
while (i < m) {
if (P[i] == P[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len - 1]; // use previous lps
} else {
lps[i] = 0;
i++;
}
}
}
return lps;
}
// KMP search function to find all occurrences of P in S
void KMPsearch(const string& S, const string& P) {
int n = S.length();
int m = P.length();
vector<int> lps = buildLPS(P);
int i = 0; // index for S
int j = 0; // index for P
bool found = false; // flag to check if any pattern is found
cout << "Searching for pattern: \"" << P << "\" in string: \"" << S <<
"\"\n";
cout << " - \n";
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
while (i < n) {
if (P[j] == S[i]) {
i++;
j++;
}
if (j == m) {
found = true;
cout << ">> Pattern found at index: " << (i - j) << " <<\n";
j = lps[j - 1]; // get the index from LPS array
} else if (i < n && P[j] != S[i]) {
if (j != 0) {
j = lps[j - 1]; // use the LPS array
} else {
i++;
}
}
}
if (!found) {
cout << ">> No occurrences of the pattern found. <<\n";
}
cout << " - \n";
}
int main() {
string S = "ababcababcababc";
string P = "ababc";
KMPsearch(S, P);
return 0;
}
5. Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
6. Time Complexity: The overall time complexity is: O(n + m) Space
Complexity: The overall space complexity is: O(m).
7. Learning Outcomes:
1. Efficient String Matching:
You will understand how the KMP algorithm efficiently finds all occurrences
of a pattern P in a string S, avoiding redundant comparisons.
2. LPS Array Utility:
You will learn how to construct and utilize the Longest Prefix Suffix (LPS)
array to skip unnecessary comparisons, optimizing the search process.
3. Time and Space Trade-offs:
You will gain insight into analyzing algorithms for both time and space
complexity, understanding why KMP achieves linear time complexity O(n +
m) with only O(m) space overhead.