Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Document preview thumbnail
Preview 3 out of 17 pages
Exam (elaborations)

Dsa Module 5- String Matching Algorithms & Naive Method Analysis

Document preview thumbnail
Preview 3 out of 17 pages

Dsa Module 5- String Matching Algorithms & Naive Method Analysis

Content preview

lOMoARcPSD|44613495




Dsa Module - 5 notes


Data Structure and Algorithms for Problem solving (Visvesvaraya Technological
University)




Scan to open on Studocu




Studocu is not sponsored or endorsed by any college or university
Downloaded by Samuel Wachira ndiang'ui ()

, lOMoARcPSD|44613495




Module – 5
String - Matching Algorithms




1. The Naive String-Matching Algorithm

The naive algorithm finds all valid shifts using a loop that checks the condition
P (1.. m) = T (s + 1… s + m) for each of the n - m + 1 possible values of s.



NAIVE-STRING-MATCHER (T, P)

1 n = T.length

2 m = P.length

3 for s = 0 to n - m

4 if P [1..m] == T [s + 1 … s + m]

5 print “Pattern occurs with shift” s


Show the comparisons the naive string matcher makes for the pattern P = 0001 in the text
T = 000010001010001.

Text (T): 000010001010001

Pattern (P): 0001 (length m = 4)

Text length: n = 15

Number of shifts: n - m + 1 = 12



We try shifts from s = 0 to 11, comparing substrings T[s:s+4] with P.




Downloaded by Samuel Wachira ndiang'ui ()

, lOMoARcPSD|44613495




Shift s T substring T[s:s+4] Compare with P Match? Comparisons

0 0000 0001 ✗ 4 (mismatch at 4th)

1 0000 0001 ✗ 4

2 0001 0001 ✓ 4

3 0010 0001 ✗ 1

4 0100 0001 ✗ 1

5 1000 0001 ✗ 1

6 0001 0001 ✓ 4

7 0010 0001 ✗ 1

8 0101 0001 ✗ 1

9 1010 0001 ✗ 1

10 0100 0001 ✗ 1

11 1001 0001 ✗ 1

Matches found at shifts: s = 2 and s = 6
Total comparisons: 4+4+4+1+1+1+4+1+1+1+1+1 = 24



Suppose that all characters in the pattern P are different. Show how to accelerate NAIVE-
STRING-MATCHER to run in time O(n).

If all characters in P are distinct:

• When a mismatch is found at position j, we can safely shift P by j positions instead of
just 1.

• Since no prefix of P is a suffix, there's no point in checking intermediate shifts.

This results in skipping comparisons and advances faster, which leads to O(n) time in the best
and average cases.

You can modify the loop:

s=0
while s <= n - m:




Downloaded by Samuel Wachira ndiang'ui ()

Document information

Uploaded on
February 25, 2026
Number of pages
17
Written in
2025/2026
Type
Exam (elaborations)
Contains
Questions & answers
£18.23

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
Writewise
3.4
(5)
Sold
53
Followers
6
Items
1827
Last sold
3 months ago



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these revision notes.

Didn't get what you expected? Choose another document

No problem! You can straightaway pick a different document that better suits what you're after.

Pay as you like, start learning straight away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and smashed it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions