0% found this document useful (0 votes)
2 views

LU4 String Searching v1

The document discusses the brute-force method for string searching, which involves finding a pattern within a larger text by comparing characters sequentially. It outlines the algorithm steps, including aligning the pattern with the text and moving right to find matches or detect mismatches. The document also provides mathematical analysis of the algorithm's efficiency, detailing best, average, and worst-case scenarios based on input size and pattern characteristics.

Uploaded by

LekshmiRajesh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

LU4 String Searching v1

The document discusses the brute-force method for string searching, which involves finding a pattern within a larger text by comparing characters sequentially. It outlines the algorithm steps, including aligning the pattern with the text and moving right to find matches or detect mismatches. The document also provides mathematical analysis of the algorithm's efficiency, detailing best, average, and worst-case scenarios based on input size and pattern characteristics.

Uploaded by

LekshmiRajesh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

LU4: Brute Force –

String Searching
Mrs. G. Priyanka, AP(Sl.Gr)/CSE
Mepco Schlenk Engineering College, Sivakasi
Brute-Force : String Searching
• It is the process of finding pattern in the larger text. Also
called as String Matching.
• pattern: a string of m characters to search for.
• text: a (longer) string of n characters to search in.
• problem: given a string of n characters called the text and a
string of m characters (m ≤ n) called the pattern, find a
substring of the text that matches the pattern.
• Example:
• Pattern: Hi
• Text: Hello, Good Morning. Hi, Nice meeting you
Brute-Force : String Searching (Contd.,)

• Goal: To find i—the index of the leftmost character of the first


matching substring in the text.

• such that
ti = p0, ti+1=p1, ...,ti+j = pj , ... ,ti+m−1 = pm−1
• Solution: If match occur, return index i. Otherwise return -1.
Brute-Force : String Searching (Contd.,)

Step 1 Align pattern at beginning of text


Step 2 Moving from left to right, compare each
character of
pattern to the corresponding character in text until
• all characters are found to match (successful
search); or
• a first mismatch is detected
Step 3 While pattern is not found and the text is not
yet
exhausted, realign pattern one position to the
right and
repeat Step 2
Brute-Force : String Searching-Example
Text: b a c b a b a b a b a c a

Pattern: a b a b a c a
Brute-Force : String Searching-Example (Contd.,)
Text: b a c b a b a b a b a c a

Pattern: a b a b a c a
Brute-Force : String Searching-Example (Contd.,)
Text: b a c b a b a b a b a c a

Pattern: a b a b a c a
Brute-Force : String Searching-Example (Contd.,)
Text: b a c b a b a b a b a c a

Pattern: a b a b a c a
Brute-Force : String Searching-Example (Contd.,)
Text: b a c b a b a b a b a c a

Pattern: a b a b a c a
Brute-Force : String Searching-Example (Contd.,)
Text: b a c b a b a b a b a c a

Pattern: a b a b a c a
Brute-Force : String Searching-Example (Contd.,)
Text: b a c b a b a b a b a c a

Pattern: a b a b a c a
Brute-Force : String Searching-Example (Contd.,)
Text: b a c b a b a b a b a c a

Pattern: a b a b a c a
Brute-Force : String Searching-Example (Contd.,)
Text: b a c b a b a b a b a c a

Pattern: a b a b a c a
Brute-Force : String Searching-Example (Contd.,)
Text: b a c b a b a b a b a c a

Pattern: a b a b a c a
Brute-Force : String Searching-Example (Contd.,)
Text: b a c b a b a b a b a c a

Pattern: a b a b a c a
Brute-Force : String Searching-Example (Contd.,)
Text: b a c b a b a b a b a c a

Pattern: a b a b a c a
Brute-Force : String Searching-Example (Contd.,)
Text: b a c b a b a c a b a c a

Pattern: a b a b a c a
Brute-Force : String Searching-Example (Contd.,)
Text: b a c b a b a c a b a c a

Pattern: a b a b a c a
Brute-Force : String Searching-Example (Contd.,)

Text:
Pattern:
Brute-Force : String Searching - Algorithm
Brute-Force : String Searching – Algorithm (Contd.,)

Mathematical Analysis of algorithm:


• Input Size: n (as m<<n)
• Basic Operation: Comparison
• Does number of times basic operation execution depend only
on input size?
• No!!!!. It also depends on the specifics of the input.
• Hence best, worst and average case efficiencies has to be examined.
• Best Case: Ω(m) as pattern of length m matches in the
beginning of the text.
• Average Case: Ω(n+m) [with equal probability assumption, the
average number of comparisons is proportional to the sum of the lengths
of the text and the pattern]
O(n) [for searching random text, efficiency is considerably better]
Brute-Force : String Searching – Algorithm (Contd.,)

Mathematical Analysis of algorithm: (Contd.,)


• Worst Case:

=
=
= m(n-m-0+1)
=m.(n-m+1)
Therefore, C(n) = O(nm)
It means that, it may have to make all m comparisons before shifting
the pattern, and this can happen for (n−m+1) possible starting
positions in the input text.
Exercises
• Pattern: 001011
Text: 10010101101001100101111010

• Pattern: happy
Text: It is never too late to have a happy childhood.

You might also like