15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

428<br />

Sequential Text Search<br />

Searching <strong>for</strong> the string GAG<br />

G A T A G<br />

A<br />

G<br />

A<br />

T<br />

Figure 3-5:<br />

The Shift Or<br />

algorithm<br />

creates<br />

a twodimensional<br />

array.<br />

G 0 1 1 1 0 1 0 1 1<br />

A 1 0 1 1 1 0 1 0 1<br />

G 1 1 1 1 1 1 0 1 1<br />

As soon as the algorithm finds three 0s that <strong>for</strong>m a diagonal line in the twodimensional<br />

array, it can identify the exact location of the GAG string in the<br />

much larger string. The Shift Or algorithm gets its name because the matching<br />

string patterns look like binary numbers where the 0 constantly gets<br />

shifted one place to the right, like this:<br />

G → 011<br />

A → 101<br />

G → 110<br />

Although the shift or algorithm takes more steps than a simple brute <strong>for</strong>ce<br />

search, it’s much faster. Sometimes the simplest algorithms aren’t always the<br />

fastest.<br />

The finite automaton string search algorithm<br />

First, the algorithm creates a finite state machine, which is a directed graph<br />

(see Book III, Chapter 5) where each node represents a single character in the<br />

target string to find. So if you wanted to find the string GAG, this algorithm<br />

creates a finite state machine consisting of three nodes with one node representing<br />

a starting state where the string hasn’t been found yet. The first node<br />

represents finding the first letter G; the second node represents finding the<br />

second letter A; and the third node represents finding the final letter G, as<br />

shown in Figure 3-6.<br />

After this algorithm creates a finite state machine <strong>for</strong> the target string, it next<br />

examines each character in the search string. In this example, the search<br />

string is GAAGGAGAA.

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

Saved successfully!

Ooh no, something went wrong!