Professional Documents
Culture Documents
PROYECTO FINAL
Alumnos:
Arequipa-Per
Contenido
INTRODUCCION ........................................................................................................................ 2
AUTOMATA FINITO .................................................................................................................. 3
AUTOMATA DE STRING MATCHING........................................................................................... 4
PSEUDOCODIGO DE STRING MATCHING USANDO UN AUTOMATA FINITO: ............................... 5
FUNCION DE TRANSICION: ........................................................................................................ 6
Seguimiento String Matching basado en Autmatas Finitos ...................................................... 6
COMPLEJIDAD TEMPORAL ........................................................................................................ 6
CODIGO AUTOMATA MATCHER EN JAVA .................................................................................. 7
BIBLIOGRAFIA ........................................................................................................................... 9
1
CONCORDANCIA DE CADENAS
INTRODUCCION
Entre sus muchas otras aplicaciones, los algoritmos de comparacin de cadenas buscan
patrones particulares en las secuencias de ADN. Los motores de bsqueda de Internet
tambin los utilizan para encontrar pginas web relevantes para las consultas.
Suponemos que el texto es una matriz de longitud y que el patrn es una matriz
de longitud < . Asumimos adems que los elementos de y son caracteres
extrados de un alfabeto finito .
Texto
Patron
2
Muchos algoritmos de comparacin de cadenas construyen un autmata finito, una
mquina simple para procesar informacin, que explora la cadena de texto para todas
las ocurrencias del patrn . Esta seccin presenta un mtodo para construir tal
autmata.
Estos autmatas de String matching son muy eficientes: examinan cada carcter de texto
exactamente una vez, tomando un tiempo constante por carcter de texto. El tiempo de
matching utilizado (despus de pre procesar el patrn para construir el autmata) es
( ). El tiempo para construir el autmata, sin embargo, puede ser grande si es
grande.
AUTOMATA FINITO
: .
0 : , 0 .
: , .
: .
: Qx Q,
a b
0 1 0
1 0 0
() = 0
() = (() , ) ,
3
AUTOMATA DE STRING MATCHING
( ) = max {: }
Las aristas a la derecha que forman la "espina" del autmata, mostrados resaltados en la
figura, corresponden a coincidencias exitosas entre los caracteres de patrn y de entrada.
entradas
estado a b c
0 1 0 0 a
1 1 2 0 b
2 3 0 0 a
3 1 4 0 b
4 5 0 0 a
5 1 4 6 c
6 7 0 0 a
7 1 2 0
4
La funcin de transicin correspondiente , y la cadena de patrones = . Las
entradas correspondientes a coincidencias exitosas entre patrn y caracteres de entrada
se muestran sombreadas.
1 2 3 4 5 6 7 8 9 10 11
[ ] a b a b a b a c a b a
( ) 0 1 2 3 4 5 4 5 6 7 2 3
5
FUNCION DE TRANSICION:
COMPLEJIDAD TEMPORAL
6
CODIGO AUTOMATA MATCHER EN JAVA
/**
* This static method implements the algorithm for exact string matching
* that constructs a finite automaton, and uses it in order to detect
* a pattern. The running time is {@code n + sm}, where {@code n} is the
* length of the text to search, {@code m} is the length of the pattern,
* and {@code s} is the amount of distinct characters in the pattern.
*
* @param text the text to search in.
* @param pattern the pattern to search for.
* @param startIndex the character index from which to start matching.
* @return the smallest index no less than {@code startIndex} of the
* position where the pattern occurs if there is a match, or
* {@code NOT_FOUND_INDEX} if there is no match.
*/
public static int match(String text, String pattern, int startIndex) {
if (pattern.isEmpty()) {
return 0;
}
int n = text.length();
Integer m = pattern.length();
if (m > n) {
return NOT_FOUND_INDEX;
}
TransitionFunction transitionFunction =
computeTransitionFunction(pattern);
Integer j = 0;
j = transitionFunction.getState(j, text.charAt(i));
if (m.equals(j)) {
return i - m + 1;
}
}
return NOT_FOUND_INDEX;
}
7
public static int match(String text, String pattern) {
return match(text, pattern, 0);
}
TransitionFunction(String pattern) {
int m = pattern.length();
this.function = new HashMap[m + 1];
function[0].put(pattern.charAt(0), 1);
if (i < m) {
function[i].put(pattern.charAt(i), i + 1);
lps = function[lps].get(pattern.charAt(i));
}
}
}
8
Integer getState(int currentState, char character) {
return function[currentState].get(character);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Set<Character> alphabet = new TreeSet<>(function[0].keySet());
Character[] array = new Character[alphabet.size()];
alphabet.toArray(array);
sb.append('\n');
}
return sb.toString();
}
}
}
BIBLIOGRAFIA
1. https://www.ics.uci.edu/~eppstein/161/960222.html
2. D. Gusfield: Algorithms on Strings, Trees, and Sequences. Cambridge University Press,
1997.
3. T.H. Cormen, C.E. Leiserson, R.L. Rivest, C. Stein: Introduction to Algorithms (3rd ed.).
MIT Press, 2009.
4. http://web.cs.mun.ca/~wang/courses/cs6783-13f
5. http://stackoverflow.com/questions/36820916/finite-automata-string-matcher
6. http://codereview.stackexchange.com/questions/110097/collection-of-exact-string-
matching-algorithms-in-java