{"id":25472,"date":"2017-10-25T19:45:17","date_gmt":"2017-10-25T14:15:17","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25472"},"modified":"2017-10-25T19:45:17","modified_gmt":"2017-10-25T14:15:17","slug":"searching-patterns-set-5-finite-automata-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/searching-patterns-set-5-finite-automata-2\/","title":{"rendered":"C programming for Searching for Patterns Set 5 Finite Automata"},"content":{"rendered":"<p>Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] in txt[]. You may assume that n &gt; m.<\/p>\n<p><strong>Examples:<\/strong><\/p>\n<pre>Input:  txt[] = \"THIS IS A TEST TEXT\"\r\n        pat[] = \"TEST\"\r\nOutput: Pattern found at index 10\r\n\r\nInput:  txt[] =  \"AABAACAADAABAABA\"\r\n        pat[] =  \"AABA\"\r\nOutput: Pattern found at index 0\r\n        Pattern found at index 9\r\n        Pattern found at index 12\r\n<img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-25432\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Searching-for-Patterns-Set-2-KMP-Algorithm.png\" alt=\"C programming for Searching for Patterns Set 2 KMP Algorithm\" width=\"704\" height=\"384\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Searching-for-Patterns-Set-2-KMP-Algorithm.png 704w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Searching-for-Patterns-Set-2-KMP-Algorithm-300x164.png 300w\" sizes=\"(max-width: 704px) 100vw, 704px\" \/><\/pre>\n<p>Pattern searching is an important problem in computer science. When we do search for a string in notepad\/word file or browser or database, pattern searching algorithms are used to show the search results.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>We have discussed the following algorithms in the previous posts:<\/p>\n<p>Naive Algorithm<br \/>\nKMP Algorithm<br \/>\nRabin Karp Algorithm<\/p>\n<p>In this post, we will discuss Finite Automata (FA) based pattern searching algorithm. In FA based algorithm, we preprocess the pattern and build a 2D array that represents a Finite Automata. Construction of the FA is the main tricky part of this algorithm. Once the FA is built, the searching is simple. In search, we simply need to start from the first state of the automata and the first character of the text. At every step, we consider next character of text, look for the next state in the built FA and move to a new state. If we reach the final state, then the pattern is found in the text. The time complexity of the search process is O(n).<br \/>\nBefore we discuss FA construction, let us take a look at the following FA for pattern ACACAGA.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-25479\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Searching-for-Patterns-Set-5-Finite-Automata2.png\" alt=\"\" width=\"154\" height=\"188\" \/> <img decoding=\"async\" class=\"aligncenter size-full wp-image-25478\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Searching-for-Patterns-Set-5-Finite-Automata.png\" alt=\"C-Searching for Patterns Set 5 Finite Automata\" width=\"497\" height=\"181\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Searching-for-Patterns-Set-5-Finite-Automata.png 497w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Searching-for-Patterns-Set-5-Finite-Automata-300x109.png 300w\" sizes=\"(max-width: 497px) 100vw, 497px\" \/><\/p>\n<p>The above diagrams represent graphical and tabular representations of pattern ACACAGA.<\/p>\n<p>Number of states in FA will be M+1 where M is length of the pattern. The main thing to construct FA is to get the next state from the current state for every possible character. Given a character x and a state k, we can get the next state by considering the string \u201cpat[0..k-1]x\u201d which is basically concatenation of pattern characters pat[0], pat[1] \u2026 pat[k-1] and the character x. The idea is to get length of the longest prefix of the given pattern such that the prefix is also suffix of \u201cpat[0..k-1]x\u201d. The value of length gives us the next state. For example, let us see how to get the next state from current state 5 and character \u2018C\u2019 in the above diagram. We need to consider the string, \u201cpat[0..5]C\u201d which is \u201cACACAC\u201d. The length of the longest prefix of the pattern such that the prefix is suffix of \u201cACACAC\u201dis 4 (\u201cACAC\u201d). So the next state (from state 5) is 4 for character \u2018C\u2019.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>In the following code, computeTF() constructs the FA. The time complexity of the computeTF() is O(m^3*NO_OF_CHARS) where m is length of the pattern and NO_OF_CHARS is size of alphabet (total number of possible characters in pattern and text). The implementation tries all possible prefixes starting from the longest possible that can be a suffix of \u201cpat[0..k-1]x\u201d. There are better implementations to construct FA in O(m*NO_OF_CHARS) (Hint: we can use something like lps array construction in KMP algorithm). We have covered the better implementation in our next post on pattern searching.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include&lt;stdio.h&gt;<br\/>#include&lt;string.h&gt;<br\/>#define NO_OF_CHARS 256<br\/> <br\/>int getNextState(char *pat, int M, int state, int x)<br\/>{<br\/>  \/\/ If the character c is same as next character in pattern,<br\/>  \/\/ then simply increment state<br\/>  if (state &lt; M &amp;&amp; x == pat[state])<br\/>  return state+1;<br\/> <br\/>  int ns, i; \/\/ ns stores the result which is next state<br\/> <br\/>  \/\/ ns finally contains the longest prefix which is also suffix<br\/>  \/\/ in &quot;pat[0..state-1]c&quot;<br\/> <br\/>  \/\/ Start from the largest possible value and stop when you find<br\/>  \/\/ a prefix which is also suffix<br\/>  for (ns = state; ns &gt; 0; ns--)<br\/>  {<br\/>  if(pat[ns-1] == x)<br\/>  {<br\/>  for(i = 0; i &lt; ns-1; i++)<br\/>  {<br\/>  if (pat[i] != pat[state-ns+1+i])<br\/>  break;<br\/>  }<br\/>  if (i == ns-1)<br\/>  return ns;<br\/>  }<br\/>  }<br\/> <br\/>  return 0;<br\/>}<br\/> <br\/>\/* This function builds the TF table which represents Finite Automata for a<br\/>  given pattern *\/<br\/>void computeTF(char *pat, int M, int TF[][NO_OF_CHARS])<br\/>{<br\/>  int state, x;<br\/>  for (state = 0; state &lt;= M; ++state)<br\/>  for (x = 0; x &lt; NO_OF_CHARS; ++x)<br\/>  TF[state][x] = getNextState(pat, M, state, x);<br\/>}<br\/> <br\/>\/* Prints all occurrences of pat in txt *\/<br\/>void search(char *pat, char *txt)<br\/>{<br\/>  int M = strlen(pat);<br\/>  int N = strlen(txt);<br\/> <br\/>  int TF[M+1][NO_OF_CHARS];<br\/> <br\/>  computeTF(pat, M, TF);<br\/> <br\/>  \/\/ Process txt over FA.<br\/>  int i, state=0;<br\/>  for (i = 0; i &lt; N; i++)<br\/>  {<br\/>  state = TF[state][txt[i]];<br\/>  if (state == M)<br\/>  {<br\/>  printf (&quot;\\n Pattern found at index %d&quot;, i-M+1);<br\/>  }<br\/>  }<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>  char *txt = &quot;AABAACAADAABAAABAA&quot;;<br\/>  char *pat = &quot;AABA&quot;;<br\/>  search(pat, txt);<br\/>  return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>  Pattern found at index 0\r\n  Pattern found at index 9\r\n  Pattern found at index 13<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C programming for Searching for Patterns Set 5 Finite Automata &#8211; Searching and sorting &#8211; Pattern searching is an important problem in computer science.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,69866,71670],"tags":[73383,73480,73273,71808,73215,73492,73472,73490,73488,70940,73471,73473,34643,73183,73194,73189,71799,73491,71774,71823,71775,73489,71796,73484,73229,73270,73317,73477,70957,73485,73481,70949,73474,73483,73479,73476,6910,70939,73482,73478,70945,73486,70950,73493,70272,70308,73475,73487,73184,73188,73316],"class_list":["post-25472","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming","category-searching-and-sorting","tag-alphabet-pattern-programs-in-c","tag-automata-based-programming-examples","tag-best-pattern-matching-algorithm","tag-c-program-to-implement-lexical-analyser","tag-c-regular-expression","tag-definite-finite-automata","tag-dfa","tag-dfa-program-in-c-language","tag-finite-automata-to-regular-expression-examples","tag-java-regex","tag-java-regex-example","tag-java-regular-expression","tag-keyword","tag-kmp","tag-kmp-algorithm","tag-knuth-algorithm","tag-lexical-analyser-in-c","tag-lexical-analyser-program-in-c","tag-lexical-analysis","tag-lexical-analysis-program-in-c","tag-lexical-analyzer","tag-lexical-analyzer-in-c","tag-lexical-analyzer-program-in-c","tag-lexical-analyzer-program-in-c-language-with-output","tag-pattern-matching","tag-pattern-matching-algorithm","tag-pattern-matching-program-in-c","tag-perl-match","tag-perl-regular-expression","tag-perl-regular-expression-match","tag-python-re-match-example","tag-python-regex-example","tag-python-regular-expression-example","tag-re-match-python-example","tag-regex-java","tag-regular","tag-regular-expression","tag-regular-expression-in-java","tag-regular-expression-in-perl","tag-regular-expression-java","tag-regular-expression-php","tag-regular-expression-program-in-c","tag-regular-expression-python","tag-regular-expression-to-finite-automata-examples","tag-search-algorithms","tag-searching-c","tag-simple-express","tag-state-machine-pattern","tag-string-algorithms","tag-string-match","tag-string-pattern-programs-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25472"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25472\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}