23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

In the classic pattern match<strong>in</strong>g problem on str<strong>in</strong>gs, we are given a text str<strong>in</strong>g T of<br />

length n <strong>and</strong> apattern str<strong>in</strong>g P of length m, <strong>and</strong> want to f<strong>in</strong>d whether P is a substr<strong>in</strong>g<br />

of T. The notion of a "match" is that there is a substr<strong>in</strong>g of T start<strong>in</strong>g at some <strong>in</strong>dex i<br />

that matches P, character by character, so that T[i] = P[0], T[i + 1] = P[1], …, T[i +<br />

m− 1] = P[m − 1]. That is, P = T[i..i + m − 1]. Thus, the output from a pattern<br />

match<strong>in</strong>g algorithm could either be some <strong>in</strong>dication that the pattern P does not exist<br />

<strong>in</strong> T or an <strong>in</strong>teger <strong>in</strong>dicat<strong>in</strong>g the start<strong>in</strong>g <strong>in</strong>dex <strong>in</strong> T of a substr<strong>in</strong>g match<strong>in</strong>g P. This is<br />

exactly the computation performed by the <strong>in</strong>dexOf method of the <strong>Java</strong> Str<strong>in</strong>g<br />

<strong>in</strong>terface. Alternatively, one may want to f<strong>in</strong>d all the <strong>in</strong>dices where a substr<strong>in</strong>g of T<br />

match<strong>in</strong>g P beg<strong>in</strong>s.<br />

In this section, we present three pattern match<strong>in</strong>g algorithms (with <strong>in</strong>creas<strong>in</strong>g levels<br />

of difficulty).<br />

12.2.1 Brute Force.<br />

The brute force algorithmic design pattern is a powerful technique for algorithm<br />

design when we have someth<strong>in</strong>g we wish to search for or when we wish to optimize<br />

some function. In apply<strong>in</strong>g this technique <strong>in</strong> a general situation we typically<br />

enumerate all possible configurations of the <strong>in</strong>puts <strong>in</strong>volved <strong>and</strong> pick the best of all<br />

these enumerated configurations.<br />

In apply<strong>in</strong>g this technique to design the brute-force pattern match<strong>in</strong>g algorithm,<br />

we derive what is probably the first algorithm that we might th<strong>in</strong>k of for solv<strong>in</strong>g the<br />

pattern match<strong>in</strong>g problem—we simply test all the possible placements of P relative<br />

to T. This algorithm, shown <strong>in</strong> Code Fragment 12.1, is quite simple.<br />

Algorithm BruteForceMatch(T,P): Input: Str<strong>in</strong>gs T (text) with n characters <strong>and</strong> P<br />

(pattern) with m characters Output: Start<strong>in</strong>g <strong>in</strong>dex of the first substr<strong>in</strong>g of T<br />

match<strong>in</strong>g P, or an <strong>in</strong>dication that P is not a substr<strong>in</strong>g of T for i ← 0 to n − m {for<br />

each c<strong>and</strong>idate <strong>in</strong>dex <strong>in</strong> T} do j ← 0 while (j <strong>and</strong> T[i + j] = P[j]) do j ← j + 1 if j =<br />

m then return i return "There is no substr<strong>in</strong>g of T match<strong>in</strong>g P."<br />

Code Fragment 12.1: Brute-force pattern match<strong>in</strong>g.<br />

748

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!