Main features
no preprocessing phase;
constant extra space needed;
always shifts the window by exactly 1 position to the right;
comparisons can be done in any order;
searching phase in
O(
mn) time complexity;
2
n expected text characters comparisons.
Description
The brute force algorithm consists in checking, at all positions in the text between 0 and n-m, whether an occurrence of the pattern starts there or not. Then, after each attempt, it shifts the pattern by exactly one position to the right.
The brute force algorithm requires no preprocessing phase, and a constant extra space in addition to the pattern and the text. During the searching phase the text character comparisons can be done in any order. The time complexity of this searching phase is O(mn) (when searching for am-1b in an for instance). The expected number of text character comparisons is 2n.
The C code
void BF(char *x, int m, char *y, int n) {
int i, j;
/* Searching */
for (j = 0; j <= n - m; ++j) {
for (i = 0; i < m && x == y[i + j]; ++i);
if (i >= m)
OUTPUT(j);
}
}
This algorithm can be rewriting to give a more efficient algorithm in practice as follows:
#define EOS '\0'
void BF(char *x, int m, char *y, int n) {
char *yb;
/* Searching */
for (yb = y; *y != EOS; ++y)
if (memcmp(x, y, m) == 0)
OUTPUT(y - yb);
}
The example
First attemptG | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
1 | 2 | 3 | 4 | |
G | C | A | G | A | G | A | G | |
Second attemptG | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
| 1 | |
| G | C | A | G | A | G | A | G | |
Third attemptG | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
| 1 | |
| G | C | A | G | A | G | A | G | |
Fourth attemptG | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
| 1 | |
| G | C | A | G | A | G | A | G | |
Fifth attemptG | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
| 1 | |
| G | C | A | G | A | G | A | G | |
Sixth attemptG | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
| G | C | A | G | A | G | A | G | |
Seventh attemptG | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
| 1 | |
| G | C | A | G | A | G | A | G | |
Eighth attemptG | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
| 1 | |
| G | C | A | G | A | G | A | G | |
Ninth attemptG | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
| 1 | 2 | |
| G | C | A | G | A | G | A | G | |
Tenth attemptG | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
| 1 | |
| G | C | A | G | A | G | A | G | |
Eleventh attemptG | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
| 1 | 2 | |
| G | C | A | G | A | G | A | G | |
Twelfth attemptG | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
| 1 | |
| G | C | A | G | A | G | A | G | |
Thirteenth attemptG | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
| 1 | 2 | |
| G | C | A | G | A | G | A | G | |
Fourteenth attemptG | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
| 1 | |
| G | C | A | G | A | G | A | G | |
Fifteenth attemptG | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
| 1 | |
| G | C | A | G | A | G | A | G | |
Sixteenth attemptG | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
| 1 | |
| G | C | A | G | A | G | A | G | |
Seventeenth attemptG | C | A | T | C | G | C | A | G | A | G | A | G | T | A | T | A | C | A | G | T | A | C | G |
| 1 | |
| G | C | A | G | A | G | A | G |
The Brute Force algorithm performs 30 character comparisons on the example.
文章评论(0条评论)
登录后参与讨论