{"id":28327,"date":"2017-10-15T18:30:16","date_gmt":"2017-10-15T13:00:16","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=28327"},"modified":"2017-10-15T18:30:16","modified_gmt":"2017-10-15T13:00:16","slug":"find-orientation-pattern-matrix","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/find-orientation-pattern-matrix\/","title":{"rendered":"C Programming-Find orientation of a pattern in a matrix"},"content":{"rendered":"<p>Given a matrix of characters and a pattern, find the orientation of pattern in the matrix. In other words, find if pattern appears in matrix in horizontal or vertical direction. Achieve this in minimum time possible.<\/p>\n<pre>Input:\r\nmat[N][N] = { {'a', 'b', 'c', 'd', 'e'},\r\n              {'f', 'g', 'h', 'i', 'j'},\r\n              {'k', 'l', 'm', 'n', 'o'},\r\n              {'p', 'q', 'r', 's', 't'},\r\n              {'u', 'v', 'w', 'x', 'y'}};\r\npattern = \"pqrs\";\r\n\r\nOutput: Horizontal<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p>A simple solution is for each row and column, use Naive pattern searching algorithm to find the orientation of pattern in the matrix. The time complexity of Naive pattern searching algorithm for every row is O(NM) where N is size of the matrix and M is length of the pattern. So, the time complexity of this solution will be <strong>O(N*(NM))<\/strong> as each of N rows and N columns takes O(NM) time.<\/p>\n<p><strong>Can we do better?<\/strong><br \/>\nThe idea is to use KMP pattern matching algorithm for each row and column. The KMP matching algorithm improves the worst case to O(N + M). The total cost of a KMP search is linear in the number of characters of string and pattern. For a N x N matrix and pattern of length M, complexity of this solution will be <strong>O(N*(N+M))<\/strong> as each of N rows and N columns will take O(N + M) time.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Program<\/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\">\/\/ C program for finding orientation of the pattern<br\/>\/\/ using KMP pattern searching algorithm<br\/>#include&lt;stdio.h&gt;<br\/>#include&lt;string.h&gt;<br\/>#include&lt;stdlib.h&gt;<br\/>#define N 5<br\/> <br\/>\/\/ Used in KMP Search for preprocessing the pattern<br\/>void computeLPSArray(char *pat, int M, int *lps)<br\/>{<br\/>    \/\/ length of the previous longest prefix suffix<br\/>    int len = 0;<br\/>    int i = 1;<br\/> <br\/>    lps[0] = 0; \/\/ lps[0] is always 0<br\/> <br\/>    \/\/ the loop calculates lps[i] for i = 1 to M-1<br\/>    while (i &lt; M)<br\/>    {<br\/>        if (pat[i] == pat[len])<br\/>        {<br\/>            len++;<br\/>            lps[i++] = len;<br\/>        }<br\/>        else \/\/ (pat[i] != pat[len])<br\/>        {<br\/>            if (len != 0)<br\/>            {<br\/>                \/\/ This is tricky. Consider the example<br\/>                \/\/ AAACAAAA and i = 7.<br\/>                len = lps[len-1];<br\/> <br\/>                \/\/ Also, note that we do not increment i here<br\/>            }<br\/>            else \/\/ if (len == 0)<br\/>            {<br\/>                lps[i++] = 0;<br\/>            }<br\/>        }<br\/>    }<br\/>}<br\/> <br\/>int KMPSearch(char *pat, char *txt)<br\/>{<br\/>    int M = strlen(pat);<br\/> <br\/>    \/\/ create lps[] that will hold the longest prefix suffix<br\/>    \/\/ values for pattern<br\/>    int *lps = (int *)malloc(sizeof(int)*M);<br\/>    int j = 0; \/\/ index for pat[]<br\/> <br\/>    \/\/ Preprocess the pattern (calculate lps[] array)<br\/>    computeLPSArray(pat, M, lps);<br\/> <br\/>    int i = 0; \/\/ index for txt[]<br\/>    while (i &lt; N)<br\/>    {<br\/>        if (pat[j] == txt[i])<br\/>        {<br\/>            j++;<br\/>            i++;<br\/>        }<br\/>        if (j == M)<br\/>        {<br\/>            \/\/ return 1 is pattern is found<br\/>            return 1;<br\/>        }<br\/>        \/\/ mismatch after j matches<br\/>        else if (i &lt; N &amp;&amp; pat[j] != txt[i])<br\/>        {<br\/>            \/\/ Do not match lps[0..lps[j-1]] characters,<br\/>            \/\/ they will match anyway<br\/>            if (j != 0)<br\/>                j = lps[j-1];<br\/>            else<br\/>                i = i+1;<br\/>        }<br\/>    }<br\/>    free(lps); \/\/ to avoid memory leak<br\/>    \/\/ return 0 is pattern is not found<br\/>    return 0;<br\/>}<br\/> <br\/>\/\/ Function to find orientation of pattern in the matrix<br\/>\/\/ It uses KMP pattern searching algorithm<br\/>void findOrientation(char mat[][N], char *pat)<br\/>{<br\/>    \/\/ allocate memory for string contaning cols<br\/>    char *col = (char*) malloc(N);<br\/> <br\/>    for (int i = 0; i &lt; N; i++)<br\/>    {<br\/>        \/\/ search in row i<br\/>        if (KMPSearch(pat, mat[i]))<br\/>        {<br\/>            printf(&quot;Horizontal\\n&quot;);<br\/>            return;<br\/>        }<br\/> <br\/>        \/\/ Construct an array to store i&#039;th column<br\/>        for (int j = 0; j &lt; N; j++)<br\/>            col[j] = *(mat[j] + i);<br\/> <br\/>        \/\/ Search in column i<br\/>        if (KMPSearch(pat, col))<br\/>            printf(&quot;Vertical\\n&quot;);<br\/>    }<br\/> <br\/>    \/\/ to avoid memory leak<br\/>    free(col);<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    char mat[N][N] =<br\/>    {<br\/>        {&#039;a&#039;, &#039;b&#039;, &#039;c&#039;, &#039;d&#039;, &#039;e&#039;},<br\/>        {&#039;f&#039;, &#039;g&#039;, &#039;h&#039;, &#039;i&#039;, &#039;j&#039;},<br\/>        {&#039;k&#039;, &#039;l&#039;, &#039;m&#039;, &#039;n&#039;, &#039;o&#039;},<br\/>        {&#039;p&#039;, &#039;q&#039;, &#039;r&#039;, &#039;s&#039;, &#039;t&#039;},<br\/>        {&#039;u&#039;, &#039;v&#039;, &#039;w&#039;, &#039;x&#039;, &#039;y&#039;}<br\/> <br\/>    };<br\/>    char pat[] = &quot;pqrs&quot;;<br\/> <br\/>    findOrientation(mat, pat);<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output :<\/strong><\/p>\n<pre>Horizontal<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C Programming-Find orientation of a pattern in a matrix &#8211; Matrix &#8211; Given a matrix of characters and a pattern, find the orientation of pattern<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83529,82291],"tags":[83537,66753,83530,83540,83541,83539,83542,83543,83534,83531,83532,83544,83538,83535,83533,83545,83536],"class_list":["post-28327","post","type-post","status-publish","format-standard","hentry","category-geometric","category-matrix","tag-2-in-place-rotation-of-2d-array-by-90-degree-clockwise","tag-android-orientation","tag-bfs-in-2d-matrix","tag-drawaspatterninrect","tag-glitch-in-the-matrix","tag-in-orientation","tag-inverse-matrix-in-r","tag-matrix-in-excel","tag-matrix-rotation-java","tag-matrix-transpose-geeksforgeeks","tag-maze-shortest-path-c","tag-orientation","tag-pattern-matrix","tag-rotate-matrix-c","tag-rotation-matrix-anticlockwise-in-c","tag-sector","tag-you-are-given-an-nxn-2d-matrix-representing-an-image-rotate-the-image-by-90-degrees-clockwise"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/28327","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=28327"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/28327\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=28327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=28327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=28327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}