4th_Sem_DAA_Module_4
4th_Sem_DAA_Module_4
Naive pattern searching is the simplest method among other pattern searching
algorithms. It checks for all character of the main string to the pattern.
Naive algorithm is exact string matching(means finding one or all exact occurrences
of a pattern in a text) algorithm.
This algorithm is helpful for smaller texts. It does not need any pre-processing phases.
We can find substring by checking once for the string. It also does not occupy extra
space to perform the operation.
The naive approach tests all the possible placement of Pattern P [1…….m] relative to
text T [1……n]. We try shift s = 0, 1…….n-m, successively and for each shift s.
Compare T [s+1…….s+m] to P [1……m].It returns all the valid shifts found.
NAIVE-STRING-MATCHER (T, P)
1. n ← length [T]
2. m ← length [P]
3. for s ← 0 to n -m
4. do if P [1.....m] = T [s + 1....s + m]
5. then print "Pattern occurs with shift"
Analysis: This for loop from 3 to 5 executes for n-m + 1(we need at least m characters at the
end) times and in iteration we are doing m comparisons. So the total complexity is O (n-
m+1).
The test on line 4 determines whether the current shift is valid or not;this test involves
an implicit loop to check corresponding character positions until all positions.
Example 1:
[1]
Example 2:
Example 3:
Input:
Main String: “ABAAABCDBBABCDDEBCABC”
pattern: “ABC”
Output:
Pattern found at position: 4
Pattern found at position: 10
Pattern found at position: 18
txt[] = "BBACCAADDEE";
pat[] = "HBB";
txt[] = "DDDDDDDDDDDD";
pat[] = "DDDDD";
2) Worst case also occurs when only the last character is different.
txt[] = "VVVVVVVVVVVVK";
pat[] = "VVVK";
The number of comparisons in the worst case is O(m*(n-m+1)).
[2]
Whenever a character mismatch occurs after matching of several characters, the
comparison begins by going back in from the character which follows the last.
l: length of Pattern
[3]
i: index of character within the pattern
= 456 mod 11
=5
= 123 mod 11
=6
Now, compare the hash value of pattern and the substring. If they match, check
whether characters are matching or not. If they do, we found our match otherwise,
move to the next characters.
In the above example, hash value did not matched. Hence, we move to the next
character.
The naive solution to this problem is to compare the pattern with every possible
substring of the text, starting from the leftmost position and moving rightwards. This
takes O(n*m) time, where 'n' is the length of the text and 'm' is the length of the
pattern.
When we work with long text documents, the brute force and naive approaches may
result in redundant comparisons. To avoid such redundancy, Knuth, Morris, and Pratt
developed a linear sequence-matching algorithm named the KMP pattern matching
algorithm. It is also referred to as Knuth Morris Pratt pattern matching algorithm.
[4]
How does KMP Algorithm work?
The KMP algorithm starts the search operation from left to right. It uses the prefix
function to avoid unnecessary comparisons while searching for the pattern. This
function stores the number of characters matched so far which is known as LPS
value. The following steps are involved in KMP algorithm −
If not, use the prefix function to skip the unnecessary comparisons. If the LPS value
of previous character from the mismatched character is '0', then start comparison from
index 0 of pattern with the next character in the text. However, if the LPS value is
more than '0', start the comparison from index value equal to LPS value of the
previously mismatched character.
The KMP algorithm takes O(n + m) time and O(m) space. It is faster than the naive
solution because it skips the redundant comparisons, and only compares each
character of the text at most once.
Input:
pattern: "AAABC"
Output:
[5]
What is N Queen Problem?
In N-Queen problem, we are given an NxN chessboard and we have to
place N number of queens on the board in such a way that no two queens attack each
other. A queen will attack another queen if it is placed in horizontal, vertical or
diagonal points in its way. The most popular approach for solving the N Queen puzzle
is Backtracking.
0 0 1 0
1 0 0 0
0 0 0 1
0 1 0 0
Follow the below steps to solve n queen problem using the backtracking approach −
Place the first queen in the top-left cell of the chessboard.
After placing a queen in the first cell, mark the position as a part of the solution and
then recursively check if this will lead to a solution.
Now, if placing the queen doesnt lead to a solution. Then go to the first step and place
queens in other cells. Repeat until all cells are tried.
If placing queen returns a lead to solution return TRUE.
[6]
Hamiltonian Circuit Problem :
A Hamiltonian cycle is a cycle that contains all vertices in a graph . If a graph has
circuit, is a graph cycle (i.e., closed loop) through a graph that visits each node exactly
The Hamiltonian cycle problem is a special case of the travelling salesman problem,
obtained by setting the distance between two cities to one if they are adjacent and two
otherwise, and verifying that the total distance travelled is equal to n (if so, the route is
a Hamiltonian circuit; if there is no Hamiltonian circuit then the shortest route will be
longer).
Example:-
Solution: Firstly, we start our search with vertex 'a.' this vertex 'a' becomes the root of
Next, we choose vertex 'b' adjacent to 'a' as it comes first in lexicographical order (b, c,
d).
[7]
Next, we select 'e' adjacent to 'd.'
Next, we select vertex 'f' adjacent to 'e.' The vertex adjacent to 'f' is d and e, but they
have already visited. Thus, we get the dead end, and we backtrack one step and remove
From backtracking, the vertex adjacent to 'e' is b, c, d, and f from which vertex 'f' has
already been checked, and b, c, d have already visited. So, again we backtrack one
step. Now, the vertex adjacent to d are e, f from which e has already been checked, and
adjacent of 'f' are d and e. If 'e' vertex, revisited them we get a dead state. So again we
Now, adjacent to c is 'e' and adjacent to 'e' is 'f' and adjacent to 'f' is 'd' and adjacent to
'd' is 'a.' Here, we get the Hamiltonian Cycle as all the vertex other than the start vertex
[8]
Subset Sum Problem :
Subset sum problem is to find subset of elements that are selected from a given set
whose sum adds up to a given number K. We are considering the set contains non-
negative values.
The Subset-Sum Problem is to find a subset's' of the given set S = (S1 S2 S3...Sn)
where the elements of the set S are n positive integers in such a manner that s'∈S and
sum of the elements of subset's' is equal to some positive integer 'X.'
The Subset-Sum Problem can be solved by using the backtracking approach. In this
implicit tree is a binary tree. The root of the tree is selected in such a way that
represents that no decision is yet taken on any input. We assume that the elements of
the given set are arranged in increasing order:
S1 ≤ S2 ≤ S3... ≤ Sn
The left child of the root node indicated that we have to include 'S1' from the set 'S'
and the right child of the root indicates that we have to execute 'S1'. Each node stores
the total of the partial solution elements. If at any stage the sum equals to 'X' then the
search is successful and terminates.
The dead end in the tree appears only when either of the two inequalities exists:
s'+ Si + 1 > X
Example: Given a set S = (3, 4, 5, 6) and X =9. Obtain the subset sum using
Backtracking approach.
Solution:
S'= (∅)
The implicit binary tree for the subset sum problem is shown as fig:
[9]
The number inside a node is the sum of the partial solution elements at a particular
level.
Thus, if our partial solution elements sum is equal to the positive integer 'X' then at
that time search will terminate, or it continues if all the possible solution needs to be
obtained.
[10]