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.

The correctness of the BM pattern match<strong>in</strong>g algorithm follows from the fact that<br />

each time the method makes a shift, it is guaranteed not to "skip" over any possible<br />

matches. For last(c) is the location of the last occurrence of c <strong>in</strong> P.<br />

The worst-case runn<strong>in</strong>g time of the BM algorithm is O(nm+|σ|). Namely, the<br />

computation of the last function takes time O(m+|σ|) <strong>and</strong> the actual search for the<br />

pattern takes O(nm) time <strong>in</strong> the worst case, the same as the brute-force algorithm.<br />

An example of a text-pattern pair that achieves the worst case is<br />

The worst-case performance, however, is unlikely to be achieved for English text,<br />

for, <strong>in</strong> this case, the BM algorithm is often able to skip large portions of text. (See<br />

Figure 12.4.) Experimental evidence on English text shows that the average number<br />

of comparisons done per character is 0.24 for a five-character pattern str<strong>in</strong>g.<br />

Figure 12.4: An example of a Boyer-Moore execution<br />

on English text.<br />

754

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

Saved successfully!

Ooh no, something went wrong!