Multiple string matching algorithms

Source: Internet
Author: User
Multi-String Matching
Section 1 raise questions

Multi-string matching means to specify some mode strings (substrings) and locate the first occurrence of any mode string in a piece of body (Main string. Specifically, the given m lengths are L1 and L2 ...... lm mode string array a [1 .. l1], a [1 .. l2]... A [1 .. ln]. Assume that the primary string is an array T [1 .. n], then a position X in the main string will satisfy a [1 .. ly] ε t [X... X + Y].

If the mode strings are superfc and King respectively, and the body is kingsuperfc, then the child strings (mode strings) can be found in the main string (body ). So how can we solve this problem? The simplest way is to enumerate each location from small to large and check the location. In the worst case, the time complexity of this algorithm is O (n * lx ).

Enumeration Method:

Int I = 0, j = 0; For (; I <n; I ++) {for (; j <m; j ++) {if (I + LJ-1 <n) {If (T [I .. I + Lj-1] = AJ [1... LJ]) {/*** output position I *** and judge whether the substring has been matched. If it has been matched, then break ***/} else {J = 0; break ;}}}}

However, this method is difficult to meet our expectation when solving the amount of data we are going to process. So we should come up with a better algorithm to solve this problem. To this end, we will introduce the KMP (knuth-Morris-Pratt) algorithm, the word Prefix Tree algorithm, and the suffix tree algorithm.

Section 2
KMP Algorithm

Definition: A mode string with a length of M. A [1 .. m], and a body T with the length of N [1 .. n], you need to find the starting position of substring A from primary string T.
Prefix function we know that common string matching algorithms must be traced back, But KMP does not need to trace back. What should I do when the primary string is partially matched? In this case, the prefix function of the mode string comes in handy.


This function can be implemented through the following program:

/***next[] function:***/k = 0;next[k] = 0;for(i = 1;i < m;i++){while(k > 0 && p[k] != p[i])k = next[k];if(p[k] == p[i])k = k + 1;next[i] = k;}


In the above Code, K can be M-1 = k + 1), and the number of executions will not exceed 1-1. Therefore, the complexity of this program is (m ).
Because the prefix function records the unique matching of the pattern string itself, you can adjust the prefix function according to the characters that do not match in the string matching algorithm, instead, we don't need to trace back to recompare.

The matching process is as follows:


Similar to the algorithm for prefix arrays, we can obtain the primary algorithm (the process of obtaining the primary algorithm is skipped ).
Section 3
Word Prefix Tree

The so-called word Prefix Tree is actually a word search tree. Ideally, the word tree is an infinitely scalable 26-fold tree. Each node is an edge that leads to 26 subnodes and is named A, B, and C... Z respectively.

For each node P, all the letters in the path from the root node to P can be connected to a string. we define this string as sp. On the contrary, for a string S, we can determine the corresponding position of the letter in the word tree based on the letters on the string, and determine the end node of the path. Therefore, the nodes on the word tree correspond to strings one by one. How is the word tree generated? It is very simple. You only need to insert data in a tree with an initial status empty. For example:
Therefore, the Insert Process for a string of n s [N] is as follows:

P = root; for (I = 0; I <n; I ++) {If (p-> wordtree [s [I] = NULL) malloc (p-> wordtree [s [I]); // initialize P = p-> wordtree [s [I];}

OK. Now there is a problem. What is the difference between the word Prefix Tree and the word tree? The difference is that each non-root node of the word Prefix Tree has a prefix pointer. But how is this prefix pointer generated? We can find the prefix pointer of the current node by following the prefix function in the KMP algorithm (we can see that the time complexity of the word Prefix Tree is related to the KMP algorithm. Defines the node to which a node's prefix Pointer Points. If the current node is P, the prefix pointer of the node with a smaller depth than P has been implemented. This operation can be completed by following the code below:

Q = p-> fathernode; char CH = Q-> P; q = Q-> prefix; while (Q! = Root & Q-> wordtree [CH] = NULL) q = Q-> prefix; If (Q-> wordtree [CH] = NULL) P = p-> root; elsep-> prefix = Q-> wordtree [CH];/*** where p-> prefix represents the prefix pointer of the P node ***/

For the above explanation, I searched an example to illustrate. For example:

We first find the prefix Q1 of the parent node of the P node when we use the prefix pointer of the P node. However, we can see that Q1 is not marked as the edge to the child node of the character B, therefore, you can only find the q1 prefix node, that is, Q2. This time, Q2 has an edge leading to Q3 marked as B. Therefore, Q3 is the prefix node of P nodes.

Now we can use the multi-String Matching Algorithm of the word Prefix Tree. The Code is as follows:

Int multicharmatch () {create a word Prefix Tree first; q = root; for (I = 0; I <n; I ++) {While (Q! = Root & Q-> wordtree [s [I] = NULL) q = Q-> prefix; If (Q-> wordtree [s [I]! = NULL) q = Q-> wordtree [s [I]; in this case, determine whether Q is in the mode string !} Return 0 ;}


Section 4
Suffix tree and mccreight Algorithm
The suffix tree is designed to effectively match and query strings. Its basic structure is a word tree composed of a string suffix (about the word tree, as described earlier ). For the string "ababc", see:

These five nodes represent five different suffixes. The figure shows that the number of nodes is an order of magnitude O (N * n), which will inevitably affect the execution efficiency of the program. Therefore, we improved the path compression, as shown in:

After compression, each node can have a maximum of 26 subnodes, And the strings on the edge of the subnode are different from each other. In this way, the number of nodes becomes O (n ).

Speaking of the suffix tree, you have to mention the mccreight algorithm.
Note: The following two paragraphs are from ljsspace.

Mccreight algorithm: the MCC algorithm is based on the brute force method. That is, it is known that the content of the input text string T is gradually shortened to the suffix length inserted into the tree until the last suffix (the character at the end) is inserted into the previous generated tree. The core idea of the mccreight algorithm is the concept of suffix Link (suffix connection) and head/tail. The suffix link of node X points to node y, which means that if the string starting from the root node to the end of node X is equal to Xw (the lowercase letter X indicates a single character, W indicates a string), then the string starting from the root node to the end of node y is equal to W. Head [I] refers to the longest prefix in the suffix tree T that suffix [I] shares with all suffixes.

For example, the basic process of mccreight algorithm can be described as: the first step: here the left branch of the tree corresponds to the insert suffix [I-1] After the effect, V and u are internal nodes, where V is the internal node corresponding to head [I-1], U is the previous internal node of V in the branch (U is closest to V, can also be equal to V ). Step 2: when inserting suffix [I], follow the suffix link of u first (because after inserting suffix [I-1], except that V may not have suffix link, the rest of the internal nodes have suffix link-this can be proved by induction), find the internal node S (u) in the tree T [I-1] (Note: suffix link must point to internal nodes ). Step 3: start inserting suffix [I], and then insert the suffix I in two small steps. The first small step: use FAST scan to search for leaves at the S (u) node until w nodes are found, this w node is the node that the suffix link of V should point to (but it is likely that this suffix link does not exist at this time), and the suffix link between V and W is established; the second small step: on the basis of finding W, use the slow scan (slow scan), that is, search the W node to the leaf direction until the head [I] is found. In this case, you can end inserting suffix [I. Note that the locations of U' must be recorded in both fastscan and slowscan. In this way, you can quickly jump to S (U') when inserting the next suffix [I + 1 '), starting from node S (U'), instead of searching from the root node like the brute force method, this is why the MCC algorithm can reach the linear complexity of O (n.


End of Section 5

Reprinted please indicate the source, original address: http://blog.csdn.net/fengchaokobe/article/details/7404247

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.