0% found this document useful (0 votes)
49 views51 pages

Data Structures: Hapter

Uploaded by

munshijubair7
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)
49 views51 pages

Data Structures: Hapter

Uploaded by

munshijubair7
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/ 51

Data Structures

Chapter 03
9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 1
Data Structures- Chapter 3
 String
– A finite sequence S of zero or more characters is called a string.
– The number of characters in a string is called its length.
– The string with zero characters is called the empty string or null string.
‘THE END’, ‘’, ‘--’. Lengths: 7, 0 and 2 respectively.
– Let S1 and S2 be strings. The string consisting of the characters of S1
followed by the characters of S2 is called the concatenation of S1 and
S2. It will be denoted by S1//S2. Example: ‘THE’ // ‘END’ =
‘THEEND’.
– A string Y is called a substring of a string S, if there exist strings X and
Z such that S= X // Y // Z. If X is an empty string, Y is called an initial
substring of S. If Z is an empty string, Y is called a terminal substring
of S.

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 2


Data Structures- Chapter 3
 STORING STRINGS

Strings are generally, stored in three types of structures:

• Fixed-length structures,
• Variable-length structures with fixed maximums and
• Linked structures.

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 3


Data Structures- Chapter 3
 Record- Oriented, Fixed-Length Storage

– In fixed-length storage each line of print is viewed as a record, where


all records have the same length, i,e., where each record accommodates
the same number of characters. Since data are frequently input on
terminals with 80-column images or using 80-column cards, we will
assume our records have length 80 unless otherwise stated or implied.
For example, consider the following program. This will appear in
memory as pictured in Fig. 3.2.

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 4


Data Structures- Chapter 3
 Record- Oriented, Fixed-Length Storage

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 5


Data Structures- Chapter 3
 Record- Oriented, Fixed-Length Storage

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 6


Data Structures- Chapter 3
 Record- Oriented, Fixed-Length Storage
– Advantages:
• The ease of accessing data from any given record
• The ease of updating data in any given record

– Disadvantages:
• Time is wasted reading an engine record if most of the storage
consists of inessential blank space.
• Certain records may require more space than available.
• When the correction consists of more or fewer characters than the
original text, changing a misspelled word requires the entire record
to be changed.

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 7


Data Structures- Chapter 3
 Record- Oriented, Fixed-Length Storage

Suppose we wanted to insert a new record. This would require that all
succeeding records be moved to new memory locations. This disadvantage
can be easily remedied using a liner array POINT which gives the address
of each successive record, so that the records need not be stored in
consecutive locations in memory. Accordingly, inserting a new record will
require will only an updating of the array POINT.

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 8


Data Structures- Chapter 3
 Record- Oriented, Fixed-Length Storage

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 9


Data Structures- Chapter 3
 Variable-Length Storage with Fixed Maximum
– Although strings may be stored in fixed-length memory locations as
above, there are advantages in knowing the actual length of each string.
For example, one then does not have to read the entire record when the
string occupies only the beginning part of the memory location. Also,
certain string operations depend on having such variable-length strings.
The storage of variable-length strings in memory cells with fixed
lengths can be done in two general ways:

• One can use a marker, such as two dollar signs ($$), to signal the
end the string.
• One can list the length of the string-as an additional item in the
pointer array

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 10


Data Structures- Chapter 3
 Variable-Length Storage with Fixed Maximum
• One can use a marker, such as two dollar signs ($$), to signal the
end the string.

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 11


Data Structures- Chapter 3
 Variable-Length Storage with Fixed Maximum
• One can list the length of the string-as an additional item in the
pointer array

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 12


Data Structures- Chapter 3
 Variable-Length Storage with Fixed Maximum
One might be tempted to store strings one after another by using some separation
marker, such as the two dollar sings ($$) in Fig. 3.5(a), or by using a pointer array
giving the location the strings, as in fig. 3.5(b). These ways of storing will
obviously save space and are sometimes used in secondary memory when records
are relatively permanent and require little change.

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 13


Data Structures- Chapter 3
 Linked Storage

• Computers are being used very frequently today for word processing, i.e.,
for inputting, processing and outputting printed matter. Therefore, the
computer must be able to correct the printed matter, which usually means
deleting, changing and inserting words, phrases, sentences and even
paragraphs in the text. Accordingly, for most extensive word processing
applications, strings are stored by means of linked lists.

• By a linked list, we mean a linearly ordered sequence of memory cells,


called nodes, where each node contains an item, called a link, which points
to the next node in the list. Figure 3.6 is a schematic diagram of such a
linked list

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 14


Data Structures- Chapter 3
 Linked Storage

• Strings may be stored in linked lists as follows. Each memory cell is


assigned one character or a fixed number of characters, and a link
contained in the cell gives the address of the cell containing the next
character or group of characters in the string. For example, consider this
famous quotation:
To be or not to be, that is the question.

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 15


Data Structures- Chapter 3
 Linked Storage

Figure 3.7 (a) shows how the string would appear in memory with
character per node, and Fig. 3.7 (b) shows how it would appear with four
characters per node.

Fig. 3.7 (a): One character per node

Fig. 3.7 (b): Four characters per node

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 16


Data Structures- Chapter 3
 String Operations
Various string operations have been developed. These are described below.

• Substring
• Indexing
• Concatenation
• Length

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 17


Data Structures- Chapter 3
 String Operations

• Substring
Accessing a substring from a given string requires three pieces of
information:
– The name of the string or the string itself
– The position of the first character of the substring in the given string and
– The length of the substring or the position of the last character of the substring.

Format of the substring operation:


SUBSTRING (string, initial, length)

Example: SUBSTRING (‘TO BE OR NOT TO BE’, 4, 7) = 'BE OR N'


SUBSTRING (‘THE END’, 4, 4) = ' END'

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 18


Data Structures- Chapter 3
 String Operations

• Indexing
– Indexing, also called pattern matching, refers to finding the position
where a string pattern P first appears in a given string text T.
– Format of this operation:
INDEX (text, pattern)
If the pattern P does not appear in the text T, then INDEX = 0.

Example: Suppose T contains the text: ‘HIS FATHER IS THE PROFESSOR',


Then, INDEX (T, 'THE’) = 7,
INDEX (T, 'THEN') = 0 and
INDEX (T, ‘ THE ') = 14

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 19


Data Structures- Chapter 3
 String Operations

• Concatenation
– Let S1, and S2 be strings. The concatenation of, S1 and S2, which we
denote by S1 // S2 is the string consisting of the characters of S1
followed by the characters of S2.

– Example: Suppose S1 = 'THE’ and S2 = 'END‘.


Then,
S1//S2 = ‘THEEND' but S1//‘ ’//S2= ‘THE END’

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 20


Data Structures- Chapter 3
 String Operations

• Length
– The number of characters in a string is called its length.
– Format:
LENGTH (string)

– Example: LENGTH (‘COMPUTER' ) = 8,


LENTGTH (‘’)=0

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 21


Data Structures- Chapter 3
 Word Processing
The operations usually associated with word processing are the following:
• Insertion
• Deletion and
• Replacement

• Insertion
Suppose in a given text T we want to insert a string S so that S begins in
position K. This operation is denoted by:
INSERT (text, position, string)
Example: INSERT (‘ABCDEFG’, 3, ‘XYZ’) = ‘ABXYZCDEFG’

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 22


Data Structures- Chapter 3
 Word Processing

• Deletion
Suppose in a given text T we want to delete the substring which begins in
position K and has length L. This operation is denoted by:
DELETE (text, position, length)
Example: DELETE (‘ABCDEFG’, 4, 2) = ‘ABCFG’

• Replacement
Suppose in a given text T we want to replace the first occurrence of a
pattern P1 by a pattern P2. This operation is denoted by:
REPLACE (text, pattern1, pattern2)
Example: REPLACE (‘XABYABZ’, ‘AB’, ‘C’) = ‘XCYABZ’

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 23


Data Structures- Chapter 3
 Example 3.6

Suppose T = ‘ABCDEFG’ and P = ‘CD’.


Then INDEX(T, P)=3 and LENGTH(P) = 2.
Hence, DELETE (‘ABCDEFG’, 3, 2) = ‘ABEFG’

Suppose T = ‘ABCDEFG’ and P=‘DC’.


Then INDEX(T, P) = 0 and LENGTH(P)= 2.
Hence, DELETE (‘ABCDEFG’ 0, 2) = ‘ABCDEFG’

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 24


Data Structures- Chapter 3
 Implementation of word processing operations using string operations

Insertion
The INSERT function can be implemented using the string operation as
follows:
INSERT(T, K, S) = SUBSTRING(T, 1, K-1)//S//
SUBSTRING(T, K, LENGTH(T)-K+1)

Deletion
The DELETE function can be implemented using the string operation as
follows:
DELETE(T, K, L) = SUBSTRING(T, 1, K-1)//
SUBSTRING(T, K+L, LENGTH(T)-K-L+1)

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 25


Data Structures- Chapter 3
 Implementation of word processing operations using string operations

Replacement
The REPLACE function can be implemented using the string operation. It
can be executed by using the following three steps:

REPLACE(T, P1, P2) =


K = INDEX(T, P1)
T = DELETE(T, K, LENGTH(P1))
INSERT(T, K, P2)

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 26


Data Structures- Chapter 3
 Algorithm 3.1: Write an algorithm that deletes every occurrence of a
pattern ‘P’ in the text ‘T’.

A text T and a pattern P are in memory. This algorithm deletes every


occurrence of P in T.

1. Set K = INDEX(T, P)
2. Repeat while K ≠ 0
a) Set T = DELETE(T, K, LENGTH (P))
b) Set K = INDEX(T, P)
[End of loop]
3. Write: T
4. Exit.

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 27


Data Structures- Chapter 3
 Example 3.7:
(a) Suppose T = XABYABZ and P = AB.
After the 1st execution, T = XYABZ
After the 2nd execution, T = XYZ, the final output.

(b) Suppose T = XAAABBBY and P = AB.


After the 3rd execution, T = XY, the final output.

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 28


Data Structures- Chapter 3
 Algorithm 3.2: Write an algorithm that replaces every occurrence of a
pattern ‘P’ in text T by another pattern Q.

A text T and patterns P and Q are in memory. This algorithm replaces every
occurrence of P in T by Q.

1. Set K = INDEX(T, P)
2. Repeat while K ≠ 0
a) Set T = REPLACE(T, P, Q)
b) Set K = INDEX(T, P)
3. [End of loop]
4. Write: T.
5. Exit.
9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 29
Data Structures- Chapter 3
 Example 3.8:
(a) Suppose T = XABYABZ, P = AB, and Q = C.
After the 1st execution, T = XCYABZ
After the 2nd execution, T = XCYCZ, the final output.

(b) Suppose T = XAY, P = A, Q = AB


After the 1st execution, T = XABY
After the 2nd execution, T = XAB2Y
………………………………………..
After the nth execution, T = XABnY
An infinite loop arises here, since P is a substring of Q.

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 30


Data Structures- Chapter 3
 Pattern Matching Algorithms
Pattern matching is the problem of deciding whether or not a given string
pattern P appears in a string text T.

First Pattern Matching Algorithm 3.3


P and T are strings with lengths R and S respectively, and are stored as
arrays with one character per element. This algorithm finds the INDEX of
P in T.

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 31


Data Structures- Chapter 3
 First Pattern Matching Algorithm 3.3

1. Set K = 1 and MAX = S - R + 1


2. Repeat Steps 3 to 5 while K ≤ MAX
3. Repeat for L = 1 to R
If P[L] ≠ T[K + L - 1], then go to Step 5.
[End of inner loop]
4. Set INDEX = K and exit.
5. Set K = K + 1
6. [End of Step 2 outer loop]
7. Set INDEX = 0
8. Exit.

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 32


Data Structures- Chapter 3
 Example 3.9
a) Suppose P = aaba and T = cdcd……cd = (cd)10.
Here S = 20, R = 4, Maximum substring = S – R + 1 =17.
Therefore, number of comparison, C = 1+1+1+………..+1=17

b) Suppose P = aaba and T = ababaaba ……


For substring W1 =abab, it needs comparison N1 = 2
For substring W2 =baba, it needs comparison N2 = 1
For substring W3 =abaa, it needs comparison N3 = 2
For substring W4 =baab, it needs comparison N4 = 1
For substring W5 =aaba, it needs comparison N5 = 4
Accordingly,
C = 2+1+2+1+4 = 10

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 33


Data Structures- Chapter 3
 Example 3.9

Suppose P = aaab and T = aa……a = a20. Here P does not appear in T.


Every Wk = aaaa; hence every Nk = 4, since the first three letters of P do
match.
Accordingly,
C = 4+4+……….+4 = 17 . 4 = 68

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 34


Data Structures- Chapter 3
 Second Pattern Matching Algorithm 3.4
The pattern matching table F(Q1,T) of a pattern P is in memory and the
input is an N-character string T = T1 T2 . . . . . TN. This algorithm finds the
INDEX of P in T.
1. Set K = 1 and S1 = Q0
2. Repeat Steps 3 to 5 while SK ≠ P and K ≤ N
3. Read: TK
4. Set SK+1 = F(SK, TK)
5. Set K = K + 1.
[End of Step 2 loop]
6. If SK = P, then
INDEX = K – LENGTH(P)
Else
INDEX = 0
[End of If Structure]
9/1/2024 7. Exit. Md. Golam Moazzam, Dept. of CSE, JU 35
Data Structures- Chapter 3
 Example: Consider the pattern P = aaba. Construct the table and the
corresponding labeled directed graph used in the “fast,” or second pattern
matching algorithm.
Solution:
The initial substrings of P:
Q0 = Ʌ, Q1 = a, Q2 = aa, Q3 = aab, Q4 = aaba = P

For each character t, the entry f(Qi, t) in the table is the largest Q which
appears as a terminal substring in the string Qit. We compute:

f(Ʌ,a) = a f(a,a) = aa f(aa,a) = aa f(aab,a) =P

f(Ʌ,b) = Ʌ f(a,b) = Ʌ f(aa,b) = aab f(aab,b) = Ʌ

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 36


Data Structures- Chapter 3
 Example: Consider the pattern P = aaba. Construct the table and the
corresponding labeled directed graph used in the “fast,” or second pattern
matching algorithm.
Solution:
The required table appears in Fig. 3.8(a). The corresponding graph appears in
Fig. 3.8(b), where there is a node corresponding to each Q and an arrow from
Qi to Qj labeled by character t for each entry f(Qi,t) = Qj in the table.

a b
Q0 Q1 Q0
Q1 Q2 Q0
Q2 Q2 Q3
Q3 P Q0
Fig. 3.8 (a): Pattern Matching Table Fig. 3.8 (b): Pattern Matching Graph

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 37


Data Structures- Chapter 3
Example 3.10: (a) Consider the text T = aabcaba and the pattern P = aaba.
Apply the second pattern matching algorithm to find the pattern P in T.
Solution:

Pattern Matching Table: a b


Q0 Q1 Q0
Q1 Q2 Q0
Q2 Q2 Q3
Q3 P Q0
Sequence of states:
a a b c a b a
Q0 Q1 Q2 Q3 Q0 Q1 Q0 Q1

Finally, P does not appear in T. So, INDEX = 0

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 38


Data Structures- Chapter 3
Example 3.10: (b) Consider the text T = abcaabaca and the pattern P = aaba.
Apply the second pattern matching algorithm to find the pattern P in T.
Solution:
Pattern Matching Table: a b
Q0 Q1 Q0
Q1 Q2 Q0
Q2 Q2 Q3
Q3 P Q0
Sequence of states:
a b c a a b a
Q0 Q1 Q0 Q0 Q1 Q2 Q3 P

Finally, P is found in T.
Thus, INDEX = 8 – LENGTH (P) = 8 – 4 = 4

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 39


Data Structures- Chapter 3
 Solved Problem 3.1: Let W be the string ABCD.
a) Find the length of W.
b) List all substrings of W.
c) List all the initial substrings of W.

Solution:
a) 4
b) Substrings: Ʌ, A, B, C, D, AB, BC, CD, ABC, BCD, ABCD
c) Initial substrings: Ʌ, A, AB, ABC. ABCD

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 40


Data Structures- Chapter 3
 Solved Problem 3.18: For each of the following patterns P and texts T,
find the number C of comparisons to find the INDEX of P in T using the
‘slow’ algorithm.
a) P=abc, T=(ab)5 b) P=abc, T=(ab)2n
c) P=aaa, T=(aabb)3 d) P=aaa, T=abaabbaaabbbaaaabbbb
Solution:
a) P=abc
T=(ab)5=ababababab
Maximum substrings = 10 - 3 + 1 = 8
Comparison, C = 3 + 1 + 3 + 1 + 3 + 1+ 3 + 1 = 16
INDEX(T, P) = 0, since P does not appear in T.

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 41


Data Structures- Chapter 3
 Solved Problem 3.18: For each of the following patterns P and texts T,
find the number C of comparisons to find the INDEX of P in T using the
‘slow’ algorithm.
a) P=abc, T=(ab)5 b) P=abc, T=(ab)2n
c) P=aaa, T=(aabb)3 d) P=aaa, T=abaabbaaabbbaaaabbbb
Solution:
b) P=abc
T=(ab)2n
Maximum substrings = 2.2n -3 + 1 = 4n – 2 = 2 (2n - 1)
Comparison, C = 3 + 1 + 3 + 1 + . . . . . . . + 3 + 1 = (2n - 1) (3 + 1)
= 4(2n - 1)
INDEX(T, P)=0, since P does not appear in T.

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 42


Data Structures- Chapter 3
 Solved Problem 3.19: Suppose P is an r-character string and T is an s-
character string, and suppose C(n) denotes the number of comparisons
when First Pattern matching algorithm is applied to P and T.
(Here n = r + s)
(a) Find the complexity C(n) for the best case.
(b) Prove that the maximum value of C(n) occurs when r = (n + 1)/4.

Solution:

a) The best case occurs when P is an initial substring of T, or, in other


words, when INDEX(T, P) = 1. In this case,
C(n) = r.

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 43


Data Structures- Chapter 3
 Solved Problem 3.19: Suppose P is an r-character . . . . . .
(b) Prove that the maximum value of C(n) occurs when r = (n + 1)/4.

Solution:
b) In general, when P is an r-character string and T is an s-character string,
the data size for the algorithm is
n=r+s
The worst case occurs when every character of P except the last matches
every substring Wk. In this case,
C(n) = r (s – r + 1)
For fixed n, we have s = n - r, so that,
C(n) = r (n - 2r + 1)
= nr -2r2 + r

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 44


Data Structures- Chapter 3
 Solved Problem 3.19: Suppose P is an r-character . . . . . .
(b) Prove that the maximum value of C(n) occurs when r = (n + 1)/4.

Solution:
Here n is fixed, so C = C(n) may be viewed as a function of r. According to
Calculus, the maximum value of C occurs when C/ = dc/dr = 0 (here C' is
the derivative of C with respect to r). Using calculus, we obtain:
C/ = n - 4r + 1 = 0
Therefore, r = (n+1)/4

The maximum value of C(n) occurs when r = (n + 1)/4.

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 45


Data Structures- Chapter 3
 Solved Problem 3.20: Consider the pattern P = aaabb. Construct the
table and the corresponding labeled directed graph used in the “fast,”
or second pattern matching algorithm.
Solution: The initial substrings of P:
Q0 = Ʌ, Q1 = a, Q2 = aa,
Q3 = aaa, Q4 = aaab, Q5 = aaabb = P

For each character t, the entry f(Qi, t) in the table is the largest Q which
appears as a terminal substring in the string Qit. We compute:

f(Ʌ,a) = a, f(a,a) = aa, f(aa,a) = aaa, f(aaa,a) = aaa, f(aaab,a) = a

f(Ʌ,b) = Ʌ, f(a,b) = Ʌ, f(aa,b) = Ʌ, f(aaa,b) = aaab, f(aaab,b) = P

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 46


Data Structures- Chapter 3
 Solved Problem 3.20: Consider the pattern P = aaabb. Construct the
table and the corresponding labeled directed graph used in the “fast,”
or second pattern matching algorithm.
Solution:
The required table appears in Fig. 3.10(a).

a b
Q0 Q1 Q0
Q1 Q2 Q0
Q2 Q3 Q0
Q3 Q3 Q4
Q4 Q1 P

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 47


Data Structures- Chapter 3
 Solved Problem 3.20: Consider the pattern P = aaabb. Construct the
table and the corresponding labeled directed graph used in the “fast,”
or second pattern matching algorithm.
Solution: The corresponding graph appears in Fig. 3.10(b), where there is a
node corresponding to each Q and an arrow from Qi to Qj labeled by
character t for each entry f(Qi,t) = Qj in the table.

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 48


Data Structures- Chapter 3
 Solved Problem 3.21: Find the table and corresponding graph for the
second pattern matching algorithm where the pattern is P = ababab.
Solution: The initial substrings of P are:
Q0 = Ʌ, Q1 = a, Q2 = ab, Q3 = aba, Q4 = abab,
Q5 = ababa, Q6 = ababab = P

The function f giving the entries in the table follows:


f(Ʌ, a) = a f(Ʌ, b) = Ʌ
f(a, a) = a f(a, b) = ab
f(ab, a) = aba f(ab, b) = Ʌ
f(aba, a) = a f(aba, b) = abab
f(abab, a) = ababa f( abab, b) = Ʌ
f(ababa, a) = a f(ababa, b) = P

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 49


Data Structures- Chapter 3
 Solved Problem 3.21: Find the table and corresponding graph for the
second pattern matching algorithm where the pattern is P = ababab.
Solution:
The table appears in Fig. 3.11(a).

a b
Q0 Q1 Q0
Q1 Q1 Q2
Q2 Q3 Q0
Q3 Q1 Q4
Q4 Q5 Q0
Q5 Q1 P

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 50


Data Structures- Chapter 3
 Solved Problem 3.21: Find the table and corresponding graph for the
second pattern matching algorithm where the pattern is P = ababab.
Solution:
The corresponding graph appears in Fig. 3.11(b).

9/1/2024 Md. Golam Moazzam, Dept. of CSE, JU 51

You might also like