0% found this document useful (0 votes)
9 views4 pages

Short Notes On Brute

Uploaded by

Janhavi Bhati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Short Notes On Brute

Uploaded by

Janhavi Bhati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Short Notes on Brute-Force Pattern Matching Algorithm

1. Definition:
The brute-force algorithm is a simple method to solve pattern matching problems by exhaustively searching
for the pattern P in the text T. It systematically tests all possible placements of the pattern P within the text T.
2. Algorithm Steps:
o Start with the first character of T.
o Compare the characters of P with the corresponding characters in T.
o If all characters of P match, return the starting index of the match.
o If not, move to the next position in T and repeat until a match is found or the end of T is reached.
o If no match is found, return that P is not a substring of T.
3. Performance:
o The algorithm has two nested loops:
 Outer loop: Iterates over possible starting positions in T.
 Inner loop: Compares characters of P with T at the current starting position.
o Worst-case time complexity: O(n × m), where n is the length of T and m is the length of P.
o Example of worst-case: When P does not match T at any index, all possible placements are tested.
4. Example:
Text (T): "abacaabaccabacabaabb"
Pattern (P): "abacab"
The algorithm compares P with every substring of T until a match is found, requiring 27 comparisons in this
case.
5. Limitations:
o Inefficient for large inputs or when m (pattern length) is close to n/2.
o Quadratic time complexity in the worst case.

Descriptive Questions and Answers


Q1. Explain the brute-force pattern matching algorithm.
Answer:
The brute-force pattern matching algorithm is a straightforward approach to finding a pattern P in a text T. It works by
systematically checking every possible starting position of P in T. The algorithm compares each character of P with
the corresponding character in T until either all characters match (indicating a successful match) or a mismatch
occurs. If no match is found after checking all positions, the algorithm concludes that P is not a substring of T. The
worst-case time complexity is O(n × m), where n is the length of T and m is the length of P.

Q2. Write the pseudocode for the brute-force pattern matching algorithm.
Answer:
Algorithm BruteForceMatch(T, P):
Input: Text T with n characters, Pattern P with m characters
Output: Starting index of the first substring of T matching P, or "No match"

for i ← 0 to n−m:
j←0
while (j < m and T[i + j] == P[j]):
j←j+1
if j == m:
return i
return "No match"

Q3. Analyze the time complexity of the brute-force pattern matching algorithm.
Answer:
The algorithm uses two nested loops:
1. Outer Loop: Iterates from 0 to n - m, performing at most n - m + 1 iterations.
2. Inner Loop: Compares each character of P with the corresponding character in T. In the worst case, it
performs m comparisons for each iteration of the outer loop.
The total number of comparisons in the worst case is (n - m + 1) × m, which simplifies to O(n × m). If m = n/2, the
time complexity becomes O(n²).

Q4. What are the advantages and disadvantages of the brute-force pattern matching algorithm?
Answer:
Advantages:
 Simple and easy to implement.
 Works well for small text and pattern sizes.
Disadvantages:
 Inefficient for large inputs due to its quadratic time complexity in the worst case.
 Performs redundant comparisons when mismatches occur.

Q5. Solve the following: Find the first occurrence of the pattern P = "abc" in the text T = "aababcabcd". Show the
steps.
Answer:
1. Start at index 0 in T:
o Compare "abc" with "aab" → mismatch.
2. Move to index 1:
o Compare "abc" with "aba" → mismatch.
3. Move to index 2:
o Compare "abc" with "bab" → mismatch.
4. Move to index 3:
o Compare "abc" with "abc" → match found.
Output: Starting index is 3.

To illustrate the comparisons done by the brute-force pattern matching algorithm for the text T = "aaabaadaabaaa"
and pattern P = "aabaaa", we examine each starting index in the text where the pattern is tested.

Initial Setup
Text (T): aaabaadaabaaa
Pattern (P): aabaaa
Length of T = 13, Length of P = 6.
The algorithm attempts to align P starting at every position i from 0 to n-m (i.e., from 0 to 7).

Step-by-Step Comparisons
1. Starting at index 0 (T[0:6] = aaabaa):
 Compare P[0] with T[0]: a == a → match.
 Compare P[1] with T[1]: a == a → match.
 Compare P[2] with T[2]: b == b → match.
 Compare P[3] with T[3]: a == a → match.
 Compare P[4] with T[4]: a == a → match.
 Compare P[5] with T[5]: a ≠ d → mismatch.
Result: No match at index 0.

2. Starting at index 1 (T[1:7] = aabaad):


 Compare P[0] with T[1]: a == a → match.
 Compare P[1] with T[2]: a == a → match.
 Compare P[2] with T[3]: b == b → match.
 Compare P[3] with T[4]: a == a → match.
 Compare P[4] with T[5]: a ≠ d → mismatch.
Result: No match at index 1.
3. Starting at index 2 (T[2:8] = abaada):
 Compare P[0] with T[2]: a == a → match.
 Compare P[1] with T[3]: a == b → mismatch.
Result: No match at index 2.

4. Starting at index 3 (T[3:9] = baadaa):


 Compare P[0] with T[3]: a ≠ b → mismatch.
Result: No match at index 3.

5. Starting at index 4 (T[4:10] = aadaab):


 Compare P[0] with T[4]: a == a → match.
 Compare P[1] with T[5]: a ≠ d → mismatch.
Result: No match at index 4.

6. Starting at index 5 (T[5:11] = adaaba):


 Compare P[0] with T[5]: a ≠ d → mismatch.
Result: No match at index 5.

7. Starting at index 6 (T[6:12] = daabaa):


 Compare P[0] with T[6]: a ≠ d → mismatch.
Result: No match at index 6.

8. Starting at index 7 (T[7:13] = aabaaa):


 Compare P[0] with T[7]: a == a → match.
 Compare P[1] with T[8]: a == a → match.
 Compare P[2] with T[9]: b == b → match.
 Compare P[3] with T[10]: a == a → match.
 Compare P[4] with T[11]: a == a → match.
 Compare P[5] with T[12]: a == a → match.
Result: Match found at index 7.

Summary of Comparisons
Index Substring of T Comparisons Match/No Match
0 aaabaa 6 comparisons (mismatch at T[5]) No Match
1 aabaad 6 comparisons (mismatch at T[5]) No Match
2 abaada 2 comparisons (mismatch at T[3]) No Match
3 baadaa 1 comparison (mismatch at T[3]) No Match
4 aadaab 2 comparisons (mismatch at T[5]) No Match
5 adaaba 1 comparison (mismatch at T[5]) No Match
6 daabaa 1 comparison (mismatch at T[6]) No Match
7 aabaaa 6 comparisons (all match) Match at index 7

Final Output
The brute-force pattern matching algorithm finds the pattern "aabaaa" in the text "aaabaadaabaaa" starting at index
7.

To construct the last function efficiently for a pattern string PP of length mm over an alphabet Σ\Sigma, you can use
the following method. The last function maps each character in Σ\Sigma to the last occurrence of that character in
PP, or −1-1 if the character does not occur in PP.
Steps to Construct the Last Function
Create an array last of size ∣Σ∣|\Sigma| (size of the alphabet). Initialize all entries of last to −1-1.
1. Initialize the Array:

2. Enumerate Characters:
Assume characters in Σ\Sigma are enumerable (e.g., 'a' to 'z', or ASCII characters). Use these indices to access
the last array.
3. Traverse the Pattern:
Iterate through the pattern PP from left to right (indices 00 to m−1m-1).
o For each character P[i]P[i], set last[P[i]] = i.
4. Output:
The last array now contains the index of the last occurrence of each character in PP, or −1-1 for characters
not in PP.

Pseudocode
Algorithm ConstructLastFunction(P, Σ):

Output: Array last of size |Σ|, where last[c] is the last occurrence of c in P, or -1 if c ∉ P
Input: Pattern string P of length m, Alphabet Σ

1. Initialize array last of size |Σ|: last[c] ← -1 for all c in Σ


2. for i ← 0 to m-1 do
last[P[i]] ← i
3. return last

Time Complexity Analysis


1. Initialization:
Initializing the array last takes O(∣Σ∣)O(|\Sigma|), as all entries are set to −1-1.
2. Traversal of Pattern:
Iterating through PP takes O(m)O(m), as there are mm characters in the pattern.
Thus, the total time complexity is:
O(∣Σ∣)+O(m)=O(m+∣Σ∣)O(|\Sigma|) + O(m) = O(m + |\Sigma|)

Example
Inputs:
 P="abacab"P = \text{"abacab"}
 Σ={a,b,c,d}\Sigma = \{a, b, c, d\}
Execution:
1. Initialize last = [-1, -1, -1, -1] for Σ={a,b,c,d}\Sigma = \{a, b, c, d\}.
2. Traverse PP:
o At i=0i = 0: P[0]=aP[0] = a, update last[a]=0last[a] = 0.
o At i=1i = 1: P[1]=bP[1] = b, update last[b]=1last[b] = 1.
o At i=2i = 2: P[2]=aP[2] = a, update last[a]=2last[a] = 2.
o At i=3i = 3: P[3]=cP[3] = c, update last[c]=3last[c] = 3.
o At i=4i = 4: P[4]=aP[4] = a, update last[a]=4last[a] = 4.
o At i=5i = 5: P[5]=bP[5] = b, update last[b]=5last[b] = 5.
Output:
last=[4,5,3,−1]\text{last} = [4, 5, 3, -1]
Here, last[a]=4last[a] = 4, last[b]=5last[b] = 5, last[c]=3last[c] = 3, and last[d]=−1last[d] = -1.
This efficient O(m+∣Σ∣)O(m + |\Sigma|) method ensures rapid computation of the last occurrence table, often used in
algorithms like the Boyer-Moore pattern matching.

You might also like