Escrito por estudiantes que aprobaron Inmediatamente disponible después del pago Leer en línea o como PDF ¿Documento equivocado? Cámbialo gratis 4,6 TrustPilot
logo-home
Examen

Dsa Module 5- String Matching Algorithms & Naive Method Analysis

Puntuación
-
Vendido
-
Páginas
17
Grado
A+
Subido en
25-02-2026
Escrito en
2025/2026

Dsa Module 5- String Matching Algorithms & Naive Method Analysis

Institución
Grado

Vista previa del contenido

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 ()

Escuela, estudio y materia

Institución
Estudio
Grado

Información del documento

Subido en
25 de febrero de 2026
Número de páginas
17
Escrito en
2025/2026
Tipo
Examen
Contiene
Preguntas y respuestas

Temas

$23.99
Accede al documento completo:

¿Documento equivocado? Cámbialo gratis Dentro de los 14 días posteriores a la compra y antes de descargarlo, puedes elegir otro documento. Puedes gastar el importe de nuevo.
Escrito por estudiantes que aprobaron
Inmediatamente disponible después del pago
Leer en línea o como PDF

Conoce al vendedor

Seller avatar
Los indicadores de reputación están sujetos a la cantidad de artículos vendidos por una tarifa y las reseñas que ha recibido por esos documentos. Hay tres niveles: Bronce, Plata y Oro. Cuanto mayor reputación, más podrás confiar en la calidad del trabajo del vendedor.
Writewise Chamberlain College Of Nursing
Seguir Necesitas iniciar sesión para seguir a otros usuarios o asignaturas
Vendido
47
Miembro desde
1 año
Número de seguidores
5
Documentos
1834
Última venta
2 semanas hace
Writewise - Stuvia US

Writewise - Stuvia US Certified tutor, offering accurate, reliable, and current study materials to support students in their exam preparation and assignments. Aiming to provide the best resources, such as summaries, nursing exam test. Up-to-date exams and assignments, Detailed test banks with verified questions and answers, Elaborate exam solutions, Case studies and discussions Customized package deals tailored to your needs. I’m committed to providing only high-quality documents to ensure the best outcomes. Get instant access to expertly prepared materials designed to help you excel in your academic journey. Reach out today and take a step closer to achieving your goals! Always be Encouraged to leave a review after sale, all complements and comments, positive &amp; Negative are appreciated to guide for better changes.

Lee mas Leer menos
3.0

4 reseñas

5
1
4
0
3
2
2
0
1
1

Documentos populares

Recientemente visto por ti

Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes