Chinaunix首页 | 论坛 | 博客
  • 博客访问: 955524
  • 博文数量: 116
  • 博客积分: 3923
  • 博客等级: 中校
  • 技术积分: 1337
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-23 01:22
文章分类

全部博文(116)

文章存档

2013年(1)

2012年(17)

2011年(69)

2009年(29)

分类: LINUX

2012-01-16 00:31:12

KMP算法C实现

  1. /*
  2.  * file:    kmp.c
  3.  * author:    vincent.cws2008@gmail.com
  4.  * history:
  5.  * @2012-01-15: initial - text search implementation, Knuth-Morris-Pratt
  6.  *
  7.  */

  8. #define DEBUG

  9. #include <stdlib.h>

  10. #ifdef DEBUG
  11. #include <stdio.h>
  12. #endif

  13. #define __malloc(x) malloc(x)
  14. #define __free(x) free(x)
  15. #define __assert(x)


  16. #ifdef DEBUG
  17. static void __kmp_test(unsigned char *W, unsigned int wlen, unsigned int *T)
  18. {
  19.     unsigned int i=0;
  20.     printf("i:\tW[i]\tT[i]\n");
  21.     while (i < wlen) {
  22.         printf("%d:\t%c\t%d\n", i, W[i], T[i]);
  23.         i++;
  24.     }
  25. }
  26. #endif

  27. static void __kmp_table(unsigned char *W, unsigned int wlen, unsigned int *T)
  28. {
  29.     unsigned int pos=2, cnd=0;
  30.     T[0]=-1;
  31.     T[1]=0;
  32.     while (pos < wlen) {
  33.         if (W[pos-1] == W[cnd]){
  34.             cnd = cnd+1;
  35.             T[pos] = cnd;
  36.             pos = pos+1;
  37.         }
  38.         else if (cnd > 0) {
  39.             cnd = T[cnd];
  40.         }
  41.         else {
  42.             T[pos]=0;
  43.             pos=pos+1;
  44.         }
  45.     }
  46. }

  47. unsigned int kmp_search(unsigned char *S, unsigned int slen,
  48.                         unsigned char *W, unsigned int wlen)
  49. {
  50.     unsigned int m=0, i=0;
  51.     unsigned int *T;
  52.     __assert(S && W);
  53.     
  54.     T = (unsigned int*)__malloc(wlen * sizeof(unsigned int));
  55.     __assert(T);
  56.     __kmp_table(W, wlen, T);

  57. #ifdef DEBUG
  58.     __kmp_test(W, wlen, T);
  59. #endif

  60.     while (m+i < slen) {
  61.         if (W[i] == S[m+i]) {
  62.             if (i == wlen-1)
  63.             {
  64.                 __free(T);
  65.                 return m;
  66.             }
  67.             i = i+1;
  68.         }
  69.         else {
  70.             m = m+i-T[i];
  71.             if (T[i] > -1)
  72.                 i = T[i];
  73.             else
  74.                 i = 0;
  75.         }
  76.     }
  77.     __free(T);
  78.     return slen;
  79. }

  1. /*
  2.  * file:    main.c
  3.  * author:    vincent.cws2008@gmail.com
  4.  * history:
  5.  * @2012-01-15: test KMP
  6.  *
  7.  */


  8. #include <stdio.h>
  9. #include <string.h>

  10. #define S1 "ABC ABCDAB ABCDABCDABDE"
  11. #define W1 "ABCDABD"

  12. #define S2    S1
  13. #define W2 "PARTICIPATE IN PARACHUTE"

  14. #define STRING_AND_LEN(s) (s), strlen(s)

  15. unsigned int kmp_search(unsigned char *S, unsigned int slen,
  16.                         unsigned char *W, unsigned int wlen);

  17. void main()
  18. {
  19.     unsigned int index = 0;
  20.     index = kmp_search(STRING_AND_LEN(S1), STRING_AND_LEN(W1));
  21.     printf("input: \n[text]%s\n[word]%s\noutput: [index]=%d\n", S1, W1, index);

  22.     printf("=====================\n");

  23.     index = kmp_search(STRING_AND_LEN(S2), STRING_AND_LEN(W2));
  24.     printf("input: \n[text]%s\n[word]%s\noutput: [index]=%d\n", S2, W2, index);

  25.     printf("=====================\n");

  26. }

输出结果如下:




附件源代码: kmp.rar  


下面是具体分析,来自wikipedia:

%E2%80%93Morris%E2%80%93Pratt_algorithm

=====================================================================================

KMP algorithm


Worked example of the search algorithm

To illustrate the algorithm's details, we work through a (relatively artificial) run of the algorithm. At any given time, the algorithm is in a state determined by two integers, m and i, which denote respectively the position within S which is the beginning of a prospective match for W, and the index in W denoting the character currently under consideration. This is depicted, at the start of the run, like


  1.              1         2
  2. m: 01234567890123456789012
  3. S: ABC ABCDAB ABCDABCDABDE
  4. W: ABCDABD
  5. i: 0123456


We proceed by comparing successive characters of W to "parallel" characters of S, moving from one to the next if they match. However, in the fourth step, we get S[3] is a space and W[3] = 'D', a mismatch. Rather than beginning to search again at S[1], we note that no 'A' occurs between positions 0 and 3 in S except at 0; hence, having checked all those characters previously, we know there is no chance of finding the beginning of a match if we check them again. Therefore we move on to the next character, setting m = 4 and i = 0. (m will first become 3 since m + i - T[i] = 0 + 3 - 0 = 3 and then become 4 since T[0] = -1)


  1.              1         2
  2. m: 01234567890123456789012
  3. S: ABC ABCDAB ABCDABCDABDE
  4. W: ABCDABD
  5. i: 0123456

We quickly obtain a nearly complete match "ABCDAB" when, at W[6] (S[10]), we again have a discrepancy. However, just prior to the end of the current partial match, we passed an "AB" which could be the beginning of a new match, so we must take this into consideration. As we already know that these characters match the two characters prior to the current position, we need not check them again; we simply reset m = 8, i = 2 and continue matching the current character. Thus, not only do we omit previously matched characters of S, but also previously matched characters of W.


  1.              1         2
  2. m: 01234567890123456789012
  3. S: ABC ABCDAB ABCDABCDABDE
  4. W: ABCDABD
  5. i: 0123456

This search fails immediately, however, as the pattern still does not contain a space, so as in the first trial, we return to the beginning of W and begin searching at the next character of S: m = 11, reset i = 0. (m will first become 10 since m + i - T[i] = 8 + 2 - 0 = 10 and then become 11 since T[0] = -1)


  1.              1         2
  2. m: 01234567890123456789012
  3. S: ABC ABCDAB ABCDABCDABDE
  4. W: ABCDABD
  5. i: 0123456


Once again we immediately hit upon a match "ABCDAB" but the next character, 'C', does not match the final character 'D' of the word W. Reasoning as before, we set m = 15, to start at the two-character string "AB" leading up to the current position, set i = 2, and continue matching from the current position.


  1.              1         2
  2. m: 01234567890123456789012
  3. S: ABC ABCDAB ABCDABCDABDE
  4. W: ABCDABD
  5. i: 0123456

----------------------------------------------------------------------------------------------

This time we are able to complete the match, whose first character is S[15].

Description of and pseudocode for the search algorithm

The above example contains all the elements of the algorithm. For the moment, we assume the existence of a "partial match" table T, described , which indicates where we need to look for the start of a new match in the event that the current one ends in a mismatch. The entries of T are constructed so that if we have a match starting at S[m] that fails when comparing S[m + i] to W[i], then the next possible match will start at index m + i - T[i] in S (that is, T[i] is the amount of "backtracking" we need to do after a mismatch). This has two implications: first, T[0] = -1, which indicates that if W[0] is a mismatch, we cannot backtrack and must simply check the next character; and second, although the next possible match will begin at index m + i - T[i], as in the example above, we need not actually check any of the T[i] characters after that, so that we continue searching from W[T[i]]. The following is a sample implementation of the KMP search algorithm.

  1. algorithm kmp_search:
  2.     input:
  3.         an array of characters, S (the text to be searched)
  4.         an array of characters, W (the word sought)
  5.     output:
  6.         an integer (the zero-based position in S at which W is found)

  7.     define variables:
  8.         an integer, m ← 0 (the beginning of the current match in S)
  9.         an integer, i ← 0 (the position of the current character in W)
  10.         an array of integers, T (the table, computed elsewhere)

  11.     while m+i is less than the length of S, do:
  12.         if W[i] = S[m + i],
  13.             if i equals the (length of W)-1,
  14.                 return m
  15.             let i ← i + 1
  16.         otherwise,
  17.             let m ← m + i - T[i],
  18.             if T[i] is greater than -1,
  19.                 let i ← T[i]
  20.             else
  21.                 let i ← 0
  22.             
  23.     (if we reach here, we have searched all of S unsuccessfully)
  24.     return the length of S


----------------------------------------------------------------------------------------------

Efficiency of the search algorithm

Assuming the prior existence of the table T, the search portion of the Knuth–Morris–Pratt algorithm has , where k is the length of S and the O is . As except for the fixed overhead incurred in entering and exiting the function, all the computations are performed in the while loop, we will calculate a bound on the number of iterations of this loop; in order to do this we first make a key observation about the nature of T. By definition it is constructed so that if a match which had begun at S[m] fails while comparing S[m + i] to W[i], then the next possible match must begin at S[m + (i - T[i])]. In particular the next possible match must occur at a higher index than m, so that T[i] < i.

Using this fact, we will show that the loop can execute at most 2k times. For in each iteration, it executes one of the two branches in the loop. The first branch invariably increases i and does not change m, so that the index m + i of the currently scrutinized character of S is increased. The second branch adds i - T[i] to m, and as we have seen, this is always a positive number. Thus the location m of the beginning of the current potential match is increased. Now, the loop ends if m + i = k; therefore each branch of the loop can be reached at most k times, since they respectively increase either m + i or m, and m ≤ m + i: if m = k, then certainly m + i ≥ k, so that since it increases by unit increments at most, we must have had m + i = k at some point in the past, and therefore either way we would be done.

Thus the loop executes at most 2k times, showing that the time complexity of the search algorithm is O(k).

Here is another way to think about the runtime: Let us say we begin to match W and S at position i and p, if W exists as a substring of S at p, then W[0 through m] == S[p through p+m]. Upon success, that is, the word and the text matched at the positions(W[i] == S[p+i]), we increase i by 1 (i++). Upon failure, that is, the word and the text does not match at the positions(W[i] != S[p+i]), the text pointer is kept still, while the word pointer roll-back a certain amount(i = T[i], where T is the jump table) And we attempt to match W[T[i]] with S[p+i]. The maximum number of roll-back of i is bounded by i, that is to say, for any failure, we can only roll-back as much as we have progressed up to the failure. Then it is clear the runtime is 2k.

============================================================================================



"Partial match" table (also known as "failure function")

The goal of the table is to allow the algorithm not to match any character of S more than once. The key observation about the nature of a linear search that allows this to happen is that in having checked some segment of the main string against an initial segment of the pattern, we know exactly at which places a new potential match which could continue to the current position could begin prior to the current position. In other words, we "pre-search" the pattern itself and compile a list of all possible fallback positions that bypass a maximum of hopeless characters while not sacrificing any potential matches in doing so.

We want to be able to look up, for each position in W, the length of the longest possible initial segment of W leading up to (but not including) that position, other than the full segment starting at W[0] that just failed to match; this is how far we have to backtrack in finding the next match. Hence T[i] is exactly the length of the longest possible proper initial segment of W which is also a segment of the substring ending at W[i - 1]. We use the convention that the empty string has length 0. Since a mismatch at the very start of the pattern is a special case (there is no possibility of backtracking), we set T[0] = -1, as discussed .

Worked example of the table-building algorithm

We consider the example of W = "ABCDABD" first. We will see that it follows much the same pattern as the main search, and is efficient for similar reasons. We set T[0] = -1. To find T[1], we must discover a of "A" which is also a prefix of W. But there are no proper suffixes of "A", so we set T[1] = 0. Likewise, T[2] = 0.

Continuing to T[3], we note that there is a shortcut to checking all suffixes: let us say that we discovered a which is a and ending at W[2] with length 2 (the maximum possible); then its first character is a proper prefix of a proper prefix of W, hence a proper prefix itself, and it ends at W[1], which we already determined cannot occur in case T[2]. Hence at each stage, the shortcut rule is that one needs to consider checking suffixes of a given size m+1 only if a valid suffix of size m was found at the previous stage (e.g. T[x]=m).

Therefore we need not even concern ourselves with substrings having length 2, and as in the previous case the sole one with length 1 fails, so T[3] = 0.

We pass to the subsequent W[4], 'A'. The same logic shows that the longest substring we need consider has length 1, and although in this case 'A' does work, recall that we are looking for segments ending before the current character; hence T[4] = 0 as well.

Considering now the next character, W[5], which is 'B', we exercise the following logic: if we were to find a subpattern beginning before the previous character W[4], yet continuing to the current one W[5], then in particular it would itself have a proper initial segment ending at W[4] yet beginning before it, which contradicts the fact that we already found that 'A' itself is the earliest occurrence of a proper segment ending at W[4]. Therefore we need not look before W[4] to find a terminal string for W[5]. Therefore T[5] = 1.

Finally, we see that the next character in the ongoing segment starting at W[4] = 'A' would be 'B', and indeed this is also W[5]. Furthermore, the same argument as above shows that we need not look before W[4] to find a segment for W[6], so that this is it, and we take T[6] = 2.

Therefore we compile the following table:

i 0 1 2 3 4 5 6
W[i] A B C D A B D
T[i] -1 0 0 0 0 1 2

Other example more interesting and complex:

i 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
W[i] P A R T I C I P A T E
I N
P A R A C H U T E
T[i] -1 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 1 2 3 0 0 0 0 0
Description of pseudocode for the table-building algorithm

The example above illustrates the general technique for assembling the table with a minimum of fuss. The principle is that of the overall search: most of the work was already done in getting to the current position, so very little needs to be done in leaving it. The only minor complication is that the logic which is correct late in the string erroneously gives non-proper substrings at the beginning. This necessitates some initialization code.

  1. algorithm kmp_table:
  2.     input:
  3.         an array of characters, W (the word to be analyzed)
  4.         an array of integers, T (the table to be filled)
  5.     output:
  6.         nothing (but during operation, it populates the table)

  7.     define variables:
  8.         an integer, pos ← 2 (the current position we are computing in T)
  9.         an integer, cnd ← 0 (the zero-based index in W of the next
  10. character of the current candidate substring)

  11.     (the first few values are fixed but different from what the algorithm
  12. might suggest)
  13.     let T[0]-1, T[1] ← 0

  14.     while pos is less than the length of W, do:
  15.         (first case: the substring continues)
  16.         if W[pos - 1] = W[cnd],
  17. let cnd ← cnd + 1, T[pos] ← cnd, pos ← pos + 1

  18.         (second case: it doesn't, but we can fall back)
  19.         otherwise, if cnd > 0, let cnd ← T[cnd]

  20.         (third case: we have run out of candidates. Note cnd = 0)
  21.         otherwise, let T[pos] ← 0, pos ← pos + 1


The complexity of the table algorithm is O(n), where n is the length of W. As except for some initialization all the work is done in the while loop, it is sufficient to show that this loop executes in O(n) time, which will be done by simultaneously examining the quantities pos and pos - cnd. In the first branch, pos - cnd is preserved, as both pos and cnd are incremented simultaneously, but naturally, pos is increased. In the second branch, cnd is replaced by T[cnd], which we saw is always strictly less than cnd, thus increasing pos - cnd. In the third branch, pos is incremented and cnd is not, so both pos and pos - cnd increase. Since pos ≥ pos - cnd, this means that at each stage either pos or a lower bound for pos increases; therefore since the algorithm terminates once pos = n, it must terminate after at most 2n iterations of the loop, since pos - cnd begins at 1. Therefore the complexity of the table algorithm is O(n).

===============================================END=========================================





阅读(6159) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~