<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE article  PUBLIC "-//NLM//DTD Journal Publishing DTD v3.0 20080202//EN" "http://dtd.nlm.nih.gov/publishing/3.0/journalpublishing3.dtd"><article xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" dtd-version="3.0" xml:lang="en" article-type="research article"><front><journal-meta><journal-id journal-id-type="publisher-id">JCC</journal-id><journal-title-group><journal-title>Journal of Computer and Communications</journal-title></journal-title-group><issn pub-type="epub">2327-5219</issn><publisher><publisher-name>Scientific Research Publishing</publisher-name></publisher></journal-meta><article-meta><article-id pub-id-type="doi">10.4236/jcc.2020.89008</article-id><article-id pub-id-type="publisher-id">JCC-103077</article-id><article-categories><subj-group subj-group-type="heading"><subject>Articles</subject></subj-group><subj-group subj-group-type="Discipline-v2"><subject>Computer Science&amp;Communications</subject></subj-group></article-categories><title-group><article-title>
 
 
  A Novel Mathematical Model for Similarity Search in Pattern Matching Algorithms
 
</article-title></title-group><contrib-group><contrib contrib-type="author" xlink:type="simple"><name name-style="western"><surname>P.</surname><given-names>Vinod-Prasad</given-names></name><xref ref-type="aff" rid="aff1"><sub>1</sub></xref><xref ref-type="corresp" rid="cor1"><sup>*</sup></xref></contrib></contrib-group><aff id="aff1"><label>1</label><addr-line>Department of Technology, Ministry of Higher Education, Sur, Oman</addr-line></aff><pub-date pub-type="epub"><day>03</day><month>09</month><year>2020</year></pub-date><volume>08</volume><issue>09</issue><fpage>94</fpage><lpage>99</lpage><history><date date-type="received"><day>18,</day>	<month>July</month>	<year>2020</year></date><date date-type="rev-recd"><day>21,</day>	<month>September</month>	<year>2020</year>	</date><date date-type="accepted"><day>24,</day>	<month>September</month>	<year>2020</year></date></history><permissions><copyright-statement>&#169; Copyright  2014 by authors and Scientific Research Publishing Inc. </copyright-statement><copyright-year>2014</copyright-year><license><license-p>This work is licensed under the Creative Commons Attribution International License (CC BY). http://creativecommons.org/licenses/by/4.0/</license-p></license></permissions><abstract><p>
 
 
  Modern applications require large databases to be searched for regions that are similar to a given pattern. The DNA sequence analysis, speech and text recognition, artificial intelligence, Internet of Things, and many other applications highly depend on pattern matching or similarity searches. In this paper, we discuss some of the string matching solutions developed in the past. Then, we present a novel mathematical model to search for a given pattern and it’s near approximates in the text.
 
</p></abstract><kwd-group><kwd>String Matching</kwd><kwd> Pattern Matching</kwd><kwd> Similarity Search</kwd><kwd> Substring Search</kwd></kwd-group></article-meta></front><body><sec id="s1"><title>1. Introduction</title><p>Similarity searches commonly known as approximate string matching allow for some mismatches between the text and the pattern. Similarity searches are widely used in computational biology, search engines, data mining, signal processing, digital dictionaries, and many other applications where exact and similar patterns are to be searched over a given text. Most of the algorithms developed prior to the 1990’s such as Morris and Pratt [<xref ref-type="bibr" rid="scirp.103077-ref1">1</xref>], Aho and Corasick [<xref ref-type="bibr" rid="scirp.103077-ref2">2</xref>], Knuth et al. [<xref ref-type="bibr" rid="scirp.103077-ref3">3</xref>], Boyer and Moore [<xref ref-type="bibr" rid="scirp.103077-ref4">4</xref>], Horspool [<xref ref-type="bibr" rid="scirp.103077-ref5">5</xref>], and Karp and Rabin [<xref ref-type="bibr" rid="scirp.103077-ref6">6</xref>], were designed to search for exact pattern matches in the text. However, with some modifications, they can also be used to find approximate matches.</p><p>The term “distance” is often used when comparing two strings for similarity. A lesser distance is expected for a greater similarity. The Hamming distance [<xref ref-type="bibr" rid="scirp.103077-ref7">7</xref>] approximates the similarity between two strings of equal length by measuring the number of character mismatches at corresponding locations. Let R and S be two non-empty equal-length strings of size M such that R = r 0 r 1 ⋯ r M − 1 and S = s 0 s 1 ⋯ s M − 1 . Then, the Hamming distance between R and S is given by ham(R, S) = number of locations where r i ≠ s i , 0 ≤ i ≤ M − 1 . Clearly, if R and S are identical, then ham(R, S) = 0. For a given pattern P modern applications may require database to be searched for exact or approximate matches of P. In the literature, this problem is sometimes referred as “string matching with k mismatches” which can be stated as follows: Given the text T = t 0 t 1 ⋯ t N − 1 of size N, and the pattern P = p 0 p 1 ⋯ p M − 1 of size M defined over a finite set of text character called alphabet λ. Let hd<sub>i</sub> be the Hamming distance such that h d i = h a m ( P , t i t i + 1 ⋯ t i + M − 1 ) , where, 0 ≤ i ≤ ( N − M ) . Then, for a given integer k such that 0 ≤ k ≤ M , report all locations iin T where h d i ≤ k .</p><p>To solve the “k-mismatch” problem, Landau and Vishkin [<xref ref-type="bibr" rid="scirp.103077-ref8">8</xref>] proposed a suffix-tree-based algorithm. They used a suffix tree to preprocess the text and the pattern in O(N + M) time, and then report k mismatches in O(kN) time. However, the algorithm requires O(k(M + N)) space, which is a concern when N becomes large. Galil and Giancarlo [<xref ref-type="bibr" rid="scirp.103077-ref9">9</xref>] presented an algorithm that uses O(kN) time and O(M) space to solve the same problem. Amir et al. [<xref ref-type="bibr" rid="scirp.103077-ref10">10</xref>] developed an algorithm to identify all such locations in O ( N k log 2 k ) time. As “k” increases the performance of these algorithms deteriorates, and approaches O(MN) as “k” approaches M. For k=M, the problem becomes independent of k and reduces to “string matching with mismatches”, which can be stated as follows: Given the text T = t 0 t 1 ⋯ t N − 1 , and the pattern P = p 0 p 1 ⋯ p M − 1 , for every i such that 0 ≤ i ≤ ( N − M ) , output the Hamming distance hd<sub>i</sub> such that h d i = h a m ( P , t i t i + 1 ⋯ t i + M − 1 ) . Abrahamson [<xref ref-type="bibr" rid="scirp.103077-ref11">11</xref>] applied a technique known as the Boolean convolution of the pattern and the text to solve the problem in O ( N M log M ) time and O(N) space. Using a linked list, Yates and Perleberg [<xref ref-type="bibr" rid="scirp.103077-ref12">12</xref>] presented O(N + Nf<sub>max</sub>) time and O(2M + σ) space algorithm, where f<sub>max</sub> is the frequency of the most commonly occurring character in the pattern. Many of these algorithms are covered in Crochemore et al. [<xref ref-type="bibr" rid="scirp.103077-ref13">13</xref>]. For a detailed survey on approximate string matching refer to Navarro [<xref ref-type="bibr" rid="scirp.103077-ref14">14</xref>], Boytsov [<xref ref-type="bibr" rid="scirp.103077-ref15">15</xref>]. Most of the algorithms developed in the past use data-structures and methods outlined above to create indexes over the text or the pattern to accelerate the search process. However, their costly maintenance has always been a cause of concern.</p></sec><sec id="s2"><title>2. Assumptions and Notations</title><p>We use the following assumptions and notations. “λ” represents a finite non-empty ordered set of characters called an alphabet, such that | λ | = σ is the size of the alphabet. “λ<sub>i</sub>” is the i<sup>th</sup> character in λ such that 1 ≤ i ≤ σ . String S is a finite sequence of characters defined over alphabet λ. S[i] or S<sub>i</sub> represents the i<sup>th</sup> character of string S, where, “i” is refers to as the shift, location, or index in S. Both T and P represent non-empty text and pattern strings defined over the alphabet λ. N and M represent sizes of the text and the pattern respectively such that M ≤ N. We use the phrase “Number of matches of P in T at shift t”, which refers to the number of character matches when pattern P is aligned with shift t in T.</p></sec><sec id="s3"><title>3. The Model</title><p>Traditional, pattern matching algorithms attempts to align P from the first character of T which may result in losing some valuable information regarding the character matches. Let’s consider an example: suppose pattern P = ABCDEF, and text T = CDEFABCD. Assuming 0 being the initial index of the strings, a four character match is found when P is aligned at the index −2 in T. All traditional algorithms would lose this information as attempts are made to align P from index 0 in T. In other words, traditional algorithms consider the indexes in the range 0 ≤ i ≤ N − M , where i represent the text index. However, as we have seen, considering i’s in the range ( 1 − M ) ≤ i ≤ ( N − 1 ) may provide additional information, particularly when the pattern is considerably large, and the character matches exist at opposite ends of the pattern or text. The lemma and proof given below are already discussed in our previous work [<xref ref-type="bibr" rid="scirp.103077-ref16">16</xref>]. However, a brief discussion is provided below for a prompt reference.</p><p>The Lemma</p><p>Let T and P be non-empty text and pattern strings defined over an alphabet λ such that: T = t 0 t 1 ⋯ t N − 1 and P = p 0 p 1 ⋯ p M − 1 . Corresponding to every index j in P, we define a set R<sub>j</sub> such that R j = { i – j | t i = p j , ∀ 0 ≤ i ≤ N − 1 } . Further, let S represent a set such that S = R 0 ∩ R 1 ∩ R 2 ∩ ⋯ ∩ R M − 1 .Then:</p><p>1) Every integer s ∈ S represents an index in T where an exact match of P is found when P is aligned at s in T.</p><p>2) The cardinality |S| represents the number of occurrences of P in T. If |S| =0 then P is not present in T.</p><p>Proof: Suppose P is found in T when P is aligned at index s in T. Then, we have to show that s ∈ S . If P appears in T at shift (index)s that means all M characters of pattern P = p 0 p 1 ⋯ p M − 1 can be successfully matched with T = t s t s + 1 t s + 2 ⋯ t s + M − 1 . Hence, ∀ j in P such that 0 ≤ j ≤ M − 1 , we have T s + j = p j . Now, from the definition of R<sub>j</sub>, we get R j = { ( s + j ) − j = s } ⇒ ∀ 0 ≤ j ≤ M − 1 , s ∈ R j ⇒ s ∈ S . Further, since all s ∈ S represent an exact match of P when P is aligned at index s in T ⇒ |S| = Number of occurrences of P in T.</p><p>Example 1: Let T = GCABABABCBA be a text array and P = ABAB be a pattern array of size 4. For the given text we have i such that 0 ≤ i ≤ 10 . For each shift j ( 0 ≤ j ≤ 3 ) in P we create a set R<sub>j</sub> such that R j = { i − j | T [ i ] = p j , 0 ≤ i ≤ 10 } . Which gives: R 0 = { 2 , 4 , 6 , 10 } , R 1 = { 2 , 4 , 6 , 8 } , R 2 = { 0 , 2 , 4 , 8 } , and R 3 = { 0 , 2 , 4 , 6 } . Hence, R 0 ∩ R 1 ∩ R 2 ∩ R 3 = S = { 2 , 4 } ⇒ | S | = 2 , which shows P occurs in T twice at locations 2 and 4.</p><p>Corollary 1: Given the M sets R<sub>j</sub> defined as in the lemma given above. Let f<sub>s</sub> be the frequency of the occurrence of an integer “s” in all sets. Then, f<sub>s</sub> represents the number of character matches when P is aligned with shift s in T.</p><p>Proof: We have already proved that any s ∈ S = R 0 ∩ R 1 ∩ R 2 ∩ ⋯ ∩ R M − 1 represents exactly M character matches of P = p 0 , p 1 , p 2 , ⋯ , p M − 1 at shift s in T ⇒ each s ∈ R j represents a single character match p<sub>j</sub> of P ⇒ the frequency of integer “s” = f<sub>s</sub> = Number of character matches of P at shift s in T. Please note: for an exact match of P at shift s in T, “s” must be present in all M sets, in that case: s ∈ S , i.e. f<sub>s</sub>= M. Also, M - f<sub>s</sub> represents the Hamming distance, i.e. the number of character mismatches when pattern P is aligned with shift s in T.</p><p>Example 2: Consider example 1, the integer 6 appears in three sets: R<sub>0</sub>, R<sub>1</sub> and R<sub>3</sub>. Hence, the frequency of the integer 6 is equal to f<sub>6</sub> = 3. Which shows three character matches of P when P is aligned with shift 6 in T. Similarly, f 0 = f 8 = 2 reveal two character matches of P when P is aligned with locations 0 and 80 in T.</p></sec><sec id="s4"><title>4. Model Implementation</title><p>We present another example to see how the model described above can be implemented successfully. Consider the pattern array P = FCTHZCTZCF, and the text array T = SKRFCTHZCTZCFTYCTZGHTTCTHZTHZFCTHZCTZCFT. The first column of <xref ref-type="table" rid="table1">Table 1</xref> below summarizes all unique characters of the pattern. The second and third columns of the table present the shifts of the corresponding pattern characters in the text and in the pattern respectively. Each row of the last column represents set R<sub>j</sub> as defined in the lemma.</p><p>As noted above, the frequency of occurrence f<sub>s</sub> of an integer “s” in all set represents the number of character matches of P at shift sin T. Therefore, we simply need a mechanism for counting the number of occurrences of individual “s” in all sets in <xref ref-type="table" rid="table1">Table 1</xref>. This can be done using an array of integers “hit[<xref ref-type="bibr" rid="scirp.103077-ref"></xref>]” of size N with all cells initialized to 0. Then, for each s ∈ R j the count at hit[s] is incremented by one. In other words, each s ∈ R j induces a hit at “hit[s]”. <xref ref-type="fig" rid="fig1">Figure 1</xref> shows the resulting array. The first row of <xref ref-type="fig" rid="fig1">Figure 1</xref> represents array indexes, and middle row indicates the hit count at the corresponding index. Note that locations 3 and 29 have been hit 10 times, which indicate 10 character matches of P and T at alignment locations 3 and 29 in T. Moreover, location 21</p><table-wrap id="table1" ><label><xref ref-type="table" rid="table1">Table 1</xref></label><caption><title> Hit index</title></caption><table><tbody><thead><tr><th align="center" valign="middle" >Pattern Character</th><th align="center" valign="middle" >Shift in T (i)</th><th align="center" valign="middle" >Shift in P (j)</th><th align="center" valign="middle" >R j = { i − j | T [ i ] = p j , ∀ 0 ≤ i ≤ N − 1 }</th></tr></thead><tr><td align="center" valign="middle"  rowspan="2"  >F</td><td align="center" valign="middle"  rowspan="2"  >3, 12, 29, 38</td><td align="center" valign="middle" >0</td><td align="center" valign="middle" >R<sub>0</sub> = 3, 12, 29, 38<sub> </sub></td></tr><tr><td align="center" valign="middle" >9</td><td align="center" valign="middle" >R<sub>9</sub> = −6, 3, 20, 29</td></tr><tr><td align="center" valign="middle"  rowspan="3"  >C</td><td align="center" valign="middle"  rowspan="3"  >4, 8, 11, 15, 22, 30, 34, 37</td><td align="center" valign="middle" >1</td><td align="center" valign="middle" >R<sub>1</sub> = 3, 7, 10, 14, 21, 29, 33, 36</td></tr><tr><td align="center" valign="middle" >5</td><td align="center" valign="middle" >R<sub>5</sub> = −1, 3, 6, 10, 17, 25, 29, 32</td></tr><tr><td align="center" valign="middle" >8</td><td align="center" valign="middle" >R<sub>8</sub> = −4, 0, 3, 7, 14, 22, 26, 29</td></tr><tr><td align="center" valign="middle"  rowspan="2"  >T</td><td align="center" valign="middle"  rowspan="2"  >5, 9, 13, 16, 20, 21, 23, 26, 31, 35, 39</td><td align="center" valign="middle" >2</td><td align="center" valign="middle" >R<sub>2</sub> = 3, 7, 11, 14, 18, 19, 21, 24, 29, 33,37</td></tr><tr><td align="center" valign="middle" >6</td><td align="center" valign="middle" >R<sub>6</sub> = −1, 3, 7, 10, 14, 15, 17, 20, 25, 29, 33</td></tr><tr><td align="center" valign="middle" >H</td><td align="center" valign="middle" >6, 19, 24, 27, 32</td><td align="center" valign="middle" >3</td><td align="center" valign="middle" >R<sub>3</sub> = 3, 16, 21, 24, 29</td></tr><tr><td align="center" valign="middle"  rowspan="2"  >Z</td><td align="center" valign="middle"  rowspan="2"  >7, 10, 17, 25, 28, 33, 36</td><td align="center" valign="middle" >4</td><td align="center" valign="middle" >R<sub>4</sub> = 3, 6, 13, 21, 24, 29, 32</td></tr><tr><td align="center" valign="middle" >7</td><td align="center" valign="middle" >R<sub>7</sub> = 0, 3, 10, 18, 21, 26, 29</td></tr></tbody></table></table-wrap><p>has been hit 5 times, location 14 has been hit 4 times, representing 5 and 4 character matches of P at alignment locations 21 and 14 respectively.</p><p>The method described above has two shortcomings. First, we need an array of size N, which is undesirable for large values of N. To resolve this, the hit[<xref ref-type="bibr" rid="scirp.103077-ref"></xref>] can be assumed as cyclic that allow us to reuse the previously used array cells. Second, as we have seen in previous examples that we might get negative values of s for which the array cell does not exist. For example, s = −1 in the two sets R<sub>5</sub> and R<sub>6</sub> in <xref ref-type="table" rid="table1">Table 1</xref>, suggesting that we can obtain two character matches if P is aligned at location −1 in T. Such hits are ignored in <xref ref-type="fig" rid="fig1">Figure 1</xref> because as we cannot record hits at negative array locations. This issue can be resolved by assuming the initial index of the text file to be ≥M − 1 rather than 0. This will ensure that s ≥ 0 for all s ∈ R j . <xref ref-type="fig" rid="fig2">Figure 2</xref> shows the re-indexed version of the array, where, each text character location is hyped by M. Therefore, index M = 10 in the re-indexed array corresponds to location 0 of the actual text array, index 9 corresponds to location −1, and so on. The benefit is straightforward; with the re-indexed array, we can say that 2 hits are found if the pattern is aligned at location 9, which corresponds to location -1 in the actual text array.</p></sec><sec id="s5"><title>5. Conclusion</title><p>Highly practical solutions can be drawn based on the model we have presented in this paper. The novel approach we have followed may become an alternative to existing solutions.</p></sec><sec id="s6"><title>Acknowledgements</title><p>We thank anonymous reviewers for their comments and suggestions.</p></sec><sec id="s7"><title>Conflicts of Interest</title><p>The author declares no conflicts of interest regarding the publication of this paper.</p></sec><sec id="s8"><title>Cite this paper</title><p>Vinod-Prasad, P. (2020) A Novel Mathematical Model for Similarity Search in Pattern Matching Algorithms. Journal of Computer and Communications, 8, 94-99. https://doi.org/10.4236/jcc.2020.89008</p></sec></body><back><ref-list><title>References</title><ref id="scirp.103077-ref1"><label>1</label><mixed-citation publication-type="other" xlink:type="simple">Morris, J. and Pratt, V. (1970) A Linear Pattern Matching Algorithm. Technical Report 40, Computing Center, University of California, Berkeley.</mixed-citation></ref><ref id="scirp.103077-ref2"><label>2</label><mixed-citation publication-type="other" xlink:type="simple">Aho, A. and Corasick, M. (1975) Efficient String Searching: An Aid to Bibliographic Search. Communications of the ACM, 18, 333-340.https://doi.org/10.1145/360825.360855</mixed-citation></ref><ref id="scirp.103077-ref3"><label>3</label><mixed-citation publication-type="other" xlink:type="simple">Knuth, D., Morris, J. and Pratt, V. (1977) Fast Pattern Matching in Strings. SIAM Journal on Computing, 6, 323-350. https://doi.org/10.1137/0206024</mixed-citation></ref><ref id="scirp.103077-ref4"><label>4</label><mixed-citation publication-type="other" xlink:type="simple">Boyer, R. and Moore, S. (1977) A Fast String Searching Algorithm. Communications of the ACM, 20, 762-772. https://doi.org/10.1145/359842.359859</mixed-citation></ref><ref id="scirp.103077-ref5"><label>5</label><mixed-citation publication-type="other" xlink:type="simple">Horspool, N. (1980) Practical Fast Searching in Strings. Software Practice and Experience, 10, 501-506. https://doi.org/10.1002/spe.4380100608</mixed-citation></ref><ref id="scirp.103077-ref6"><label>6</label><mixed-citation publication-type="other" xlink:type="simple">Karp, R. and Rabin, M. (1987) Efficient Randomized Pattern-Matching Algorithms. IBM Journal Research and Development, 31, 249-260.https://doi.org/10.1147/rd.312.0249</mixed-citation></ref><ref id="scirp.103077-ref7"><label>7</label><mixed-citation publication-type="other" xlink:type="simple">Hamming, R. (1950) Error Detecting and Error Correcting Codes. Bell System Technical Journal, 29, 147-160. https://doi.org/10.1002/j.1538-7305.1950.tb00463.x</mixed-citation></ref><ref id="scirp.103077-ref8"><label>8</label><mixed-citation publication-type="other" xlink:type="simple">Landau, G. and Vishkin, U. (1986) Efficient String with k Mismatches. Theoretical Computer Science, 43, 239-249. https://doi.org/10.1016/0304-3975(86)90178-7</mixed-citation></ref><ref id="scirp.103077-ref9"><label>9</label><mixed-citation publication-type="other" xlink:type="simple">Galil, Z. and Giancarlo, R. (1986) Improved String Matching with k Mismatches. SIGACT News, 17, 52-54. https://doi.org/10.1145/8307.8309</mixed-citation></ref><ref id="scirp.103077-ref10"><label>10</label><mixed-citation publication-type="other" xlink:type="simple">Amir, A., Lewenstein, M. and Porat, E. (2004) A Faster Algorithms for String Matching with k Mismatches. Journal of Algorithms, 50, 257-275.https://doi.org/10.1016/S0196-6774(03)00097-X</mixed-citation></ref><ref id="scirp.103077-ref11"><label>11</label><mixed-citation publication-type="other" xlink:type="simple">Abrahamson, K. (1987) Generalized String Matching. SIAM Journal of Computing, 16, 1039-1051. https://doi.org/10.1137/0216067</mixed-citation></ref><ref id="scirp.103077-ref12"><label>12</label><mixed-citation publication-type="other" xlink:type="simple">Baeza-Yates, R. and Perleberg, C.H. (1996) Fast and Practical String Matching. Information Processing Letters, 59, 21-27.https://doi.org/10.1016/0020-0190(96)00083-X</mixed-citation></ref><ref id="scirp.103077-ref13"><label>13</label><mixed-citation publication-type="other" xlink:type="simple">Crochemore, M., Hancart, C. and Lecroq, T. (2007) Algorithms on Strings. Cambridge University Press, Cambridge. https://doi.org/10.1017/CBO9780511546853</mixed-citation></ref><ref id="scirp.103077-ref14"><label>14</label><mixed-citation publication-type="other" xlink:type="simple">Navarro, G. (2001) A Guided Tour to Approximate String Matching. ACM Computing Surveys, 33, 31-88. https://doi.org/10.1145/375360.375365</mixed-citation></ref><ref id="scirp.103077-ref15"><label>15</label><mixed-citation publication-type="other" xlink:type="simple">Boytsov, L. (2011) Indexing Methods for Approximate Dictionary Searching: Comparative Analysis. ACM Journal of Experimental Algorithmics, 16, Article No. 1.1.https://doi.org/10.1145/1963190.1963191</mixed-citation></ref><ref id="scirp.103077-ref16"><label>16</label><mixed-citation publication-type="other" xlink:type="simple">Vinod-Prasad, P. (2016) A Novel Algorithm for String Matching with Mismatches. 5th International Conference of Pattern Recognitions Applications and Methods, Rome, February 2016, 638-644. https://doi.org/10.5220/0005752006380644</mixed-citation></ref></ref-list></back></article>