0% found this document useful (0 votes)
3 views5 pages

Experiment 9 DAA

The document outlines an experiment in the Design and Analysis of Algorithms course, focusing on developing a program to find all occurrences of a pattern in a string using the KMP algorithm. It details the steps for building the LPS array, matching the pattern, and recording occurrences, along with the implementation code. The overall time complexity is O(n + m) and space complexity is O(m), emphasizing efficient string matching and the utility of the LPS array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Experiment 9 DAA

The document outlines an experiment in the Design and Analysis of Algorithms course, focusing on developing a program to find all occurrences of a pattern in a string using the KMP algorithm. It details the steps for building the LPS array, matching the pattern, and recording occurrences, along with the implementation code. The overall time complexity is O(n + m) and space complexity is O(m), emphasizing efficient string matching and the utility of the LPS array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 9
Student Name: Sagandeep Singh UID: 22BCS14568
Branch: BE-CSE Section/Group: 22BCS_IOT-618/A
Semester: 5th Date of Performance:
Subject Name: Design and Analysis of Algorithms (DAA)
Subject Code:22CSH-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.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Step 4: End the Search


• Continue until i traverses the entire string S
4. Implementation/Code:
#include <iostream>
using namespace std;

const int MAX_PATTERN_SIZE = 100;

void buildLPS(const string& P, int lps[]) {


int m = P.length();
int len = 0;
int i = 1;

lps[0] = 0;

while (i < m) {
if (P[i] == P[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
}

void search(const string& S, const string& P) {


int n = S.length();
int m = P.length();
int lps[MAX_PATTERN_SIZE];

buildLPS(P, lps);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

int i = 0;
int j = 0;
bool found = false;

cout << "Searching for pattern: \"" << P << "\" in string: \"" << S << "\"\n";
cout << "--------------------------------------------\n";

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];
} else if (i < n && P[j] != S[i]) {
if (j != 0) {
j = lps[j - 1];
} else {
i++;
}
}
}

if (!found) {
cout << ">> No occurrences of the pattern found. <<\n";
}

cout << "--------------------------------------------\n";


}

int main() {
string S = "abcxsaganxyzsagansaganwsaganpqrsagan";
string P = "sagan";
search(S, P);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

return 0;
}
5. Output

Fig 1 longest string search

6. Time Complexity
The overall time complexity is: O(n + m)
Space Complexity:
The overall space complexity is: O(m).
7. Learning Outcome
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..
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

You might also like