0% found this document useful (0 votes)
104 views2 pages

CSE 2012-Design and Analysis of Algorithms Practice Problem Sheet (String Matching Problem)

This document provides 8 practice problems related to string matching algorithms: 1) handle patterns with empty space characters, 2) extend the Robin-Karp algorithm to 2D arrays, 3) modify Robin-Karp to find occurrences of any of k patterns, 4) efficiently compute the largest repetitive factor of symbols in a pattern, 5) propose a 2D string matching problem and algorithm, 6) modify Robin-Karp to find occurrences of k patterns in j texts, 7) design a linear-time algorithm to check if one text is a cyclic rotation of another, and 8) use a prefix function table to compute valid shifts of a pattern in a text.

Uploaded by

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

CSE 2012-Design and Analysis of Algorithms Practice Problem Sheet (String Matching Problem)

This document provides 8 practice problems related to string matching algorithms: 1) handle patterns with empty space characters, 2) extend the Robin-Karp algorithm to 2D arrays, 3) modify Robin-Karp to find occurrences of any of k patterns, 4) efficiently compute the largest repetitive factor of symbols in a pattern, 5) propose a 2D string matching problem and algorithm, 6) modify Robin-Karp to find occurrences of k patterns in j texts, 7) design a linear-time algorithm to check if one text is a cyclic rotation of another, and 8) use a prefix function table to compute valid shifts of a pattern in a text.

Uploaded by

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

CSE 2012- Design and Analysis of Algorithms

Practice Problem Sheet (String Matching


Problem)

Practice makes you Perfect

String Matching Problem


Given a text T [1, ..., n] of length n and a Pattern P [1, ..., m] of lenght m ≤ n.
Elements of P and T are characters drawn from a finite alphabet Σ. P occurs
with with shift s in text T if T [s + 1, s + 2, ..., s + m] = P [1, 2, ...m]. If P occurs
with shift s , we call s a valid shift. Otherwise, we call s an invalid shift. Task of
‘String Matching Problem’ is to find all valid shifts with which a given pattern
P occurs in T .
1. Let the pattern contains the occurence of empty space character, denoted
for the purpose of understanding as ⋄. The pattern may look like ab⋄ba⋄c,
which is just ab ba c, in turn just abbac. Design an algorithm to compute
the valid shifts of P . Analyse your algorithm with time-complexity.
2. Robin-Karp Algorithm discussed in the class, computes all the valid shifts
of s of P in T . Here P and T are one-dimensional arrays. Extend the
Robin-Karp algorithm where the P and T are of two dimensional arrays.
In other words, given a n × n of characters and a pattern of size m × m
m ≤ n, design an algorithm to identify the occurence of P in T .
3. Given a text T [1, ..., n] of length n and k Patterns P1 [1, ..., m], P2 [1, ..., m],
..., Pk [1, ..., m], modify the Robin-Karp algorithm to compute the oc-
curence of any one of the patterns in T .

4. Let y i denote the concatenation of a string y with itself i times. For


example (ab)3 is ababab. A string x ∈ Σ∗ is said to have a repetitive
factor r if x = y r , for some string y ∈ Σ∗ , r > 0. Let ρ(x) denote the
largest r such that x has a repetition factor r. Given P [1, 2, ..., m], design
an efficient algorithm to compute the ρ(Pi ), i = 1, 2, ...m. Here, Pi is the
i − th symbol in P . Analyse your running time with time-complexity.
5. String Matching Problem discussed in the class is of one dimension. Pro-
pose the 2-dimensional equivalent of the String Matching Problem, called
as 2D-String matching Problem. Design an algorithm for the 2D-String
Matching Problem. Analyse your running time with time-complexity.

1
6. Given j texts T1 [1, ..., n], T2 [1, ..., n],... Tj [1, ..., n], of length n and k Pat-
terns P1 [1, ..., m], P2 [1, ..., m], ..., Pk [1, ..., m], modify the Robin-Karp al-
gorithm to compute the occurrence of all the valid shifts of all the patterns
in all the texts. Analyse your running time with time-complexity.
7. Given two texts T , T ′ , design a linear-time algorithm to determine whether
the T is a cyclic rotation of T ′ . For example, ‘car’ is the cyclic rotation
of ‘arc’ since ‘car’ can be obtained by a cyclic rotation of the symbols in
‘arc’. Analyse your running time with time-complexity.
8. Consider the pattern P and the text T . Given the ‘Prefix-function Table‘
of the string P T (π table for the string PT ), design an algorithm to
compute the valid shifts of P in T . Analyse your running time with time-
complexity.

You might also like