The bitap algorithm is an approximate string matching algorithm. The algorithm tells whether a given text contains a substring which is "approximately equal" to a given pattern, where approximate equality is defined in terms of Levenshtein distance if the substring and pattern are within a given distance k of each other, then the algorithm considers them equal. The algorithm begins by precomputing a set of bitmasks containing one bit for each element of the pattern. Then it is able to do most of the work with bitwise operations, which are extremely fast. The bitap algorithm is perhaps best known as one of the underlying algorithms of the Unix utilityagrep, written by Udi Manber, Sun Wu, and Burra Gopal. Manber and Wu's original paper gives extensions of the algorithm to deal with fuzzy matching of general regular expressions. Due to the data structures required by the algorithm, it performs best on patterns less than a constant length, and also prefers inputs over a small alphabet. Once it has been implemented for a given alphabet and word lengthm, however, its running time is completely predictableit runs in O operations, no matter the structure of the text or the pattern. The bitap algorithm for exact string searching was invented by Bálint Dömölki in 1964 and extended by R. K. Shyamasundar in 1977, before being reinvented by Ricardo Baeza-Yates and Gaston Gonnet in 1989 which also extended it to handle classes of characters, wildcards, and mismatches. In 1991, it was extended by Manber and Wu to handle also insertions and deletions. This algorithm was later improved by Baeza-Yates and Navarro in 1996 and later by Gene Myers for long patterns in 1998.
Exact searching
The bitap algorithm for exact string searching, in full generality, looks like this in pseudocode: algorithm bitap_search is input:text as a string. pattern as a string. output: string m := length ifm = 0 then returntext /* Initialize the bit array R. */ R := new array of bit, initially all 0 R := 1 fori := 0; i < length; i += 1 do /* Update the bit array. */ fork := m; k ≥ 1; k -= 1 do R := R & ifRthen return + 1 return null Bitap distinguishes itself from other well-known string searching algorithms in its natural mapping onto simple bitwise operations, as in the following modification of the above program. Notice that in this implementation, counterintuitively, each bit with value zero indicates a match, and each bit with value 1 indicates a non-match. The same algorithm can be written with the intuitive semantics for 0 and 1, but in that case we must introduce another instruction into the inner loop to set R |= 1. In this implementation, we take advantage of the fact that left-shifting a value shifts in zeros on the right, which is precisely the behavior we need. Notice also that we require CHAR_MAX additional bitmasks in order to convert the condition in the general implementation into bitwise operations. Therefore, the bitap algorithm performs better when applied to inputs over smaller alphabets. #include #include const char *bitap_bitwise_search
Fuzzy searching
To perform fuzzy string searching using the bitap algorithm, it is necessary to extend the bit array R into a second dimension. Instead of having a single array R that changes over the length of the text, we now have k distinct arrays R1..k. Array Ri holds a representation of the prefixes of pattern that match any suffix of the current string with i or fewer errors. In this context, an "error" may be an insertion, deletion, or substitution; see Levenshtein distance for more information on these operations. The implementation below performs fuzzy matching using the fuzzy bitap algorithm. However, it only pays attention to substitutions, not to insertions or deletionsin other words, a Hamming distance of k. As before, the semantics of 0 and 1 are reversed from their conventional meanings. #include #include #include const char *bitap_fuzzy_bitwise_search