|
|
T = taagccctgatcgatactagtcgatgcca n = 29 P = taagcccttat m = 11 |
|
How to use last(c) to compute the jump distance:
T = taagccctgatcgatactagtcgatgcca n = 29
P = taagcccttat m = 11
01234567890123456789012345678 (ruler to find position)
P = taagcccttat m = 11
^
last(g) = 3 ................................. (1)
|
|
01234567890
P = taagcccttat
last(a) = 9
last(g) = 3
last(c) = 6
last(t) = 10
last(b) = -1
last(d) = -1
...
|
n = T.lenght();
m = P.length();
last = buildLastFunction(pattern);
/* -------------------------------------------
Start by matching the last character in P
------------------------------------------- */
i = m-1;
j = m-1;
repeat
{
if ( P[j] and T[i] matches )
{
if ( all characters in P matched )
return (match found at position i);
match "next" (previous) characters
}
else /* Mismatch */
slide P over by "skip distance" characters;
} (while i < n)
|
In Java:
int BMmatch (String T, String P)
{
int[] last;
int n;
int m;
int i, j;
n = T.lenght();
m = P.length();
last = buildLastFunction(pattern);
// last[c] = last(c)
i = m-1;
j = m-1;
do
{
if ( P[j] == T[i] )
{ /* -------------------------------
Characters match up
------------------------------- */
if ( j == 0 ) /* Last character matched up !!! */
{
return(i); // Found a match !
}
else
{
i--; // Match "next" pair of characters
j--;
}
}
else
{ /* --------------------------
Mismatch ==> Jump
-------------------------- */
i = i + (m-1) + last[c]; // Jump
j = m-1; // Match last character in pattern
}
} while ( i < n );
|
|
01234567890123456789012345678 (ruler to find position)
T = taagccctgatcgatactagtcgatgcca n = 29
P = tgatccctgat m = 11
last(g) = 8
|
|
|
n = T.lenght();
m = P.length();
last = buildLastFunction(pattern);
/* -------------------------------------------
Start by matching the last character in P
------------------------------------------- */
i = m-1;
j = m-1;
repeat
{
if ( P[j] and T[i] matches )
{
if ( all characters in P matched )
return (match found at position i);
match "next" (previous) characters
}
else /* Mismatch */
{
if ( *** mismatched location < last occurence of char *** )
slide P over by 1 character;
else
slide P over by "skip distance" characters;
} (while i < n)
|
In Java:
int BMmatch (String T, String P)
{
int[] last;
int n;
int m;
int i, j;
int last_i; // ***** Remember where we stopped last
n = T.lenght();
m = P.length();
last = buildLastFunction(pattern);
// last[c] = last(c)
i = m-1;
last_i = i; // ***** Remember where we stopped last
j = m-1;
do
{
if ( P[j] == T[i] )
{ /* -------------------------------
Characters match up
------------------------------- */
if ( j == 0 ) /* Last character matched up !!! */
{
return(i); // Found a match !
}
else
{
i--; // Match "next" pair of characters
j--;
}
}
else
{ /* --------------------------
Mismatch ==> Jump
-------------------------- */
if ( j < last[c] )
{
i = last_i + 1; // We passed the "last(c)" position
}
else
{
i = i + (m-1) + last[c]; // Normal jump
}
j = m-1; // Match last character in pattern
last_i = i; // ***** Remember where we stopped last
}
} while ( i < n );
|
Example input:
T = acgttagatactaggatgcca P = gata |