Cse2012 Design and Analysis of Algorithms Lab Digital Assignment 2
Cse2012 Design and Analysis of Algorithms Lab Digital Assignment 2
Cse2012 Design and Analysis of Algorithms Lab Digital Assignment 2
ALGORITHMS
LAB DIGITAL ASSIGNMENT 2
Slot: L47+48
Submitted to: Mr. Sivanesan S Sir
Aim: To write an executable program for Naïve String-matching with proper pseudocode
code and flow chart.
Problem Analysis:
Naïve String-matching Algorithms is basically brute force for string matching algorithm. It is
very easy to understand as depicted below. This string matching is one of least efficient. Let
us consider a string of length of n. The main objective of this searching algorithm is to find
the position of the given pattern length of which is less than n in minimum number
searches/iterations
Example of Naïve String-matching Algorithms
Flowchart:
Pseudocode:
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]
printf("Pattern found at index %d \n", i);
}
Actual code:
#include <bits/stdc++.h>
using namespace std;
// Driver Code
int main()
{
char txt[100];
char pat[100];
cin>>txt;
cin>>pat;
search(pat, txt);
return 0;
}
Output screenshot:
Aim: Using KMP algorithm for pattern searching
Analysis:
The KMP matching algorithm uses property of pattern having same sub-patterns appearing
more than once in the pattern of the pattern. The basic idea behind KMP’s algorithm is:
whenever we detect a mismatch (after some matches), we already know some of the
characters in the text of the next window. We take advantage of this information to avoid
matching the characters that we know will anyway match.
Flowchart :
Pseudocode:
n ← length [Text]
m ← length [Pattern]
LPS← COMPUTE-PREFIX-FUNCTION (P)
q←0 // numbers of characters matched
for i ← 1 to n // scan S from left to right
do while q > 0 and Pattern[q + 1] ≠ T [i]
do q ← LPS[q] // next character does not match
If Pattern[q + 1] = Text[i]
then q ← q + 1 // next character matches
If q = m // is all of p matched?
then print "Pattern occurs with shift" i - m
q ← LPS[q] // look for the next match
Code
#include <bits/stdc++.h>
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;
}
}
}
}
Else if (len == 0)
{
lps[i] = 0;
i++;
}
}
}
}
int main()
{
char txt[100];
char pat[100];
cin>>txt;
cin>>pat;
KMPSearch(pat, txt);
return 0;
}
Output screenshots
AIM:
Using Rabin-Karp algorithm for pattern searching
Problem analysis:
Rabin-Karp algorithm slides the pattern one by one. Rabin Karp algorithm matches the hash
value of the pattern with the hash value of current substring of text, and if the hash values
match then only it starts matching individual characters.
Flowchart:
Pseudocode :
Begin
patLen := pattern Length
strLen := string Length
patHash := 0 and strHash := 0, h := 1
maxChar := total number of characters in character set
#define d 256
}
if (j == M)
cout<<"Pattern found at index "<< i<<endl;
}
if ( i < N-M )
{
t = (d*(t - txt[i]*h) + txt[i+M])%q;
if (t < 0)
t = (t + q);
}
}
}
/* Driver code */
int main()
{
char txt[100];
char pat[100];
cin>>txt;
cin>>pat;
int q;
cin>>q;
search(pat, txt, q);
return 0;
}
Output:
Result analysis:
Complexity analysis table
Name of Name of sample Time Space Ranking based
techniques outcome complexity complexity on
(in Big O complexities*
notation )
Naive String Naive String O(m*n) O(n) 1
matching matching
Algorithm Algorithm
(Brute force)
KMP algorithm KMP algorithm O(n) O(n) 2
while (i <= N - M)
{
int j;
int main()
{
char txt[100];
char pat[100];
cin>>txt;
cin>>pat;
search(pat, txt);
return 0;
}
Output screenshot
Conclusion:
We have written more optimised code for Naive String-matching Algorithm for a specific
case i.e., when all the characters of the pattern are different.
We have analysed and studied the KMP algorithm and Rabin-Karp algorithm.