{"id":25515,"date":"2017-10-25T19:55:08","date_gmt":"2017-10-25T14:25:08","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25515"},"modified":"2017-10-25T19:55:08","modified_gmt":"2017-10-25T14:25:08","slug":"c-programming-suffix-array-set-1-introduction","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-suffix-array-set-1-introduction\/","title":{"rendered":"C++ Programming for Suffix Array Set 1 Introduction"},"content":{"rendered":"<p>We strongly recommend to read following post on suffix trees as a pre-requisite for this post.<\/p>\n<p>Pattern Searching | Set 8 (Suffix Tree Introduction)<\/p>\n<p>A suffix array is a sorted array of all suffixes of a given string. The definition is similar to Suffix Tree which is compressed trie of all suffixes of the given text. Any suffix tree based algorithm can be replaced with an algorithm that uses a suffix array enhanced with additional information and solves the same problem in the same time complexity (Source Wiki).<br \/>\nA suffix array can be constructed from Suffix tree by doing a DFS traversal of the suffix tree. In fact Suffix array and suffix tree both can be constructed from each other in linear time.<br \/>\nAdvantages of suffix arrays over suffix trees include improved space requirements, simpler linear time construction algorithms (e.g., compared to Ukkonen\u2019s algorithm) and improved cache locality.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre>Let the given string be \"banana\".\r\n\r\n0 banana                          5 a\r\n1 anana     Sort the Suffixes     3 ana\r\n2 nana      ---------------->     1 anana  \r\n3 ana        alphabetically       0 banana  \r\n4 na                              4 na   \r\n5 a                               2 nana\r\n\r\nSo the suffix array for \"banana\" is {5, 3, 1, 0, 4, 2}<\/pre>\n[ad type=\u201dbanner\u201d]\n<p><strong>Naive method to build Suffix Array<\/strong><br \/>\nA simple method to construct suffix array is to make an array of all suffixes and then sort the array. Following is implementation of simple method.<\/p>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%2F%2F%20Naive%20algorithm%20for%20building%20suffix%20array%20of%20a%20given%20text%0A%23include%20%3Ciostream%3E%0A%23include%20%3Ccstring%3E%0A%23include%20%3Calgorithm%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20Structure%20to%20store%20information%20of%20a%20suffix%0Astruct%20suffix%0A%7B%0A%20%20%20%20int%20index%3B%0A%20%20%20%20char%20*suff%3B%0A%7D%3B%0A%20%0A%2F%2F%20A%20comparison%20function%20used%20by%20sort()%20to%20compare%20two%20suffixes%0Aint%20cmp(struct%20suffix%20a%2C%20struct%20suffix%20b)%0A%7B%0A%20%20%20%20return%20strcmp(a.suff%2C%20b.suff)%20%3C%200%3F%201%20%3A%200%3B%0A%7D%0A%20%0A%2F%2F%20This%20is%20the%20main%20function%20that%20takes%20a%20string%20\u2019txt\u2019%20of%20size%20n%20as%20an%0A%2F%2F%20argument%2C%20builds%20and%20return%20the%20suffix%20array%20for%20the%20given%20string%0Aint%20*buildSuffixArray(char%20*txt%2C%20int%20n)%0A%7B%0A%20%20%20%20%2F%2F%20A%20structure%20to%20store%20suffixes%20and%20their%20indexes%0A%20%20%20%20struct%20suffix%20suffixes%5Bn%5D%3B%0A%20%0A%20%20%20%20%2F%2F%20Store%20suffixes%20and%20their%20indexes%20in%20an%20array%20of%20structures.%0A%20%20%20%20%2F%2F%20The%20structure%20is%20needed%20to%20sort%20the%20suffixes%20alphabatically%0A%20%20%20%20%2F%2F%20and%20maintain%20their%20old%20indexes%20while%20sorting%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20suffixes%5Bi%5D.index%20%3D%20i%3B%0A%20%20%20%20%20%20%20%20suffixes%5Bi%5D.suff%20%3D%20(txt%2Bi)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Sort%20the%20suffixes%20using%20the%20comparison%20function%0A%20%20%20%20%2F%2F%20defined%20above.%0A%20%20%20%20sort(suffixes%2C%20suffixes%2Bn%2C%20cmp)%3B%0A%20%0A%20%20%20%20%2F%2F%20Store%20indexes%20of%20all%20sorted%20suffixes%20in%20the%20suffix%20array%0A%20%20%20%20int%20*suffixArr%20%3D%20new%20int%5Bn%5D%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20suffixArr%5Bi%5D%20%3D%20suffixes%5Bi%5D.index%3B%0A%20%0A%20%20%20%20%2F%2F%20Return%20the%20suffix%20array%0A%20%20%20%20return%20%20suffixArr%3B%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20print%20an%20array%20of%20given%20size%0Avoid%20printArr(int%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20for(int%20i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20arr%5Bi%5D%20%3C%3C%20%22%20%22%3B%0A%20%20%20%20cout%20%3C%3C%20endl%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20char%20txt%5B%5D%20%3D%20%22banana%22%3B%0A%20%20%20%20int%20n%20%3D%20strlen(txt)%3B%0A%20%20%20%20int%20*suffixArr%20%3D%20buildSuffixArray(txt%2C%20%20n)%3B%0A%20%20%20%20cout%20%3C%3C%20%22Following%20is%20suffix%20array%20for%20%22%20%3C%3C%20txt%20%3C%3C%20endl%3B%0A%20%20%20%20printArr(suffixArr%2C%20n)%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC++\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>\u00a0<\/p>\n<p><strong>Output:<\/strong><\/p>\n<pre>Following is suffix array for banana\r\n5 3 1 0 4 2<\/pre>\n<p>The time complexity of above method to build suffix array is O(n<sup>2<\/sup>Logn) if we consider a O(nLogn) algorithm used for sorting. The sorting step itself takes O(n<sup>2<\/sup>Logn) time as every comparison is a comparison of two strings and the comparison takes O(n) time.<br \/>\nThere are many efficient algorithms to build suffix array. We will soon be covering them as separate posts.<\/p>\n[ad type=\u201dbanner\u201d]\n<p><strong>Search a pattern using the built Suffix Array<\/strong><br \/>\nTo search a pattern in a text, we preprocess the text and build a suffix array of the text. Since we have a sorted array of all suffixes, <a href=\"http:\/\/geeksquiz.com\/binary-search\/\" target=\"_blank\" rel=\"noopener noreferrer\">Binary Search<\/a> can be used to search. Following is the search function. Note that the function doesn\u2019t report all occurrences of pattern, it only report one of them.<\/p>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%2F%2F%20This%20code%20only%20contains%20search()%20and%20main.%20To%20make%20it%20a%20complete%20running%0A%2F%2F%20above%20code%20or%20see%20http%3A%2F%2Fcode.geeksforgeeks.org%2FoY7OkD%0A%20%0A%2F%2F%20A%20suffix%20array%20based%20search%20function%20to%20search%20a%20given%20pattern%0A%2F%2F%20\u2019pat\u2019%20in%20given%20text%20\u2019txt\u2019%20using%20suffix%20array%20suffArr%5B%5D%0Avoid%20search(char%20*pat%2C%20char%20*txt%2C%20int%20*suffArr%2C%20int%20n)%0A%7B%0A%20%20%20%20int%20m%20%3D%20strlen(pat)%3B%20%20%2F%2F%20get%20length%20of%20pattern%2C%20needed%20for%20strncmp()%0A%20%0A%20%20%20%20%2F%2F%20Do%20simple%20binary%20search%20for%20the%20pat%20in%20txt%20using%20the%0A%20%20%20%20%2F%2F%20built%20suffix%20array%0A%20%20%20%20int%20l%20%3D%200%2C%20r%20%3D%20n-1%3B%20%20%2F%2F%20Initilize%20left%20and%20right%20indexes%0A%20%20%20%20while%20(l%20%3C%3D%20r)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20See%20if%20\u2019pat\u2019%20is%20prefix%20of%20middle%20suffix%20in%20suffix%20array%0A%20%20%20%20%20%20%20%20int%20mid%20%3D%20l%20%2B%20(r%20-%20l)%2F2%3B%0A%20%20%20%20%20%20%20%20int%20res%20%3D%20strncmp(pat%2C%20txt%2BsuffArr%5Bmid%5D%2C%20m)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20match%20found%20at%20the%20middle%2C%20print%20it%20and%20return%0A%20%20%20%20%20%20%20%20if%20(res%20%3D%3D%200)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22Pattern%20found%20at%20index%20%22%20%3C%3C%20suffArr%5Bmid%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Move%20to%20left%20half%20if%20pattern%20is%20alphabtically%20less%20than%0A%20%20%20%20%20%20%20%20%2F%2F%20the%20mid%20suffix%0A%20%20%20%20%20%20%20%20if%20(res%20%3C%200)%20r%20%3D%20mid%20-%201%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Otherwise%20move%20to%20right%20half%0A%20%20%20%20%20%20%20%20else%20l%20%3D%20mid%20%2B%201%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20We%20reach%20here%20if%20return%20statement%20in%20loop%20is%20not%20executed%0A%20%20%20%20cout%20%3C%3C%20%22Pattern%20not%20found%22%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20char%20txt%5B%5D%20%3D%20%22banana%22%3B%20%20%2F%2F%20text%0A%20%20%20%20char%20pat%5B%5D%20%3D%20%22nan%22%3B%20%20%20%2F%2F%20pattern%20to%20be%20searched%20in%20text%0A%20%0A%20%20%20%20%2F%2F%20Build%20suffix%20array%0A%20%20%20%20int%20n%20%3D%20strlen(txt)%3B%0A%20%20%20%20int%20*suffArr%20%3D%20buildSuffixArray(txt%2C%20n)%3B%0A%20%0A%20%20%20%20%2F%2F%20search%20pat%20in%20txt%20using%20the%20built%20suffix%20array%0A%20%20%20%20search(pat%2C%20txt%2C%20suffArr%2C%20n)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D%0A\u201d message=\u201dC++\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<p>Pattern found at index 2<br \/>\nThe time complexity of the above search function is O(mLogn). There are more efficient algorithms to search pattern once the suffix array is built. In fact there is a O(m) suffix array based algorithm to search a pattern. We will soon be discussing efficient algorithm for search.<\/p>\n[ad type=\u201dbanner\u201d]\n<p><strong>Applications of Suffix Array<\/strong><br \/>\nSuffix array is an extremely useful data structure, it can be used for a wide range of problems. Following are some famous problems where Suffix array can be used.<br \/>\n1) Pattern Searching<br \/>\n2) Finding the longest repeated substring<br \/>\n3) Finding the longest common substring<br \/>\n4) Finding the longest palindrome in a string<\/p>\n<p>See this for more problems where Suffix arrays can be used.<\/p>\n<p>This post is a simple introduction. There is a lot to cover in Suffix arrays. We have discussed a O(nLogn) algorithm for Suffix Array construction here. We will soon be discussing more efficient suffix array algorithms.<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>C++ Programming for Suffix Array Set 1 Introduction &#8211; Searching and sorting &#8211; A suffix array is a sorted array of all suffixes of a given string. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79658,83515,1,71670],"tags":[73640,73674,73641,70843,73666,73669,73654,73662,73635,73649,73634,73665,73637,73671,73673,73645,73655,73651,73664,73647,73652,73644,73643,73299,73648,73636,73656,73661,73632,73630,73633,73663,73653,73650,73670,73639,73667,73672,73642,73660,73659,73646,73638,73631,73628,73627,73668,73629,73657,73658],"class_list":["post-25515","post","type-post","status-publish","format-standard","hentry","category-array","category-c-programming-3","category-coding","category-searching-and-sorting","tag-10-suffixes","tag-array-from","tag-array-in-c-programming-examples-pdf","tag-array-programming","tag-common-prefixes-and-suffixes","tag-compare-strings-online","tag-competitive-programming-3-pdf","tag-competitive-programming-pdf","tag-data-structures-and-algorithms-in-c-pdf","tag-data-suffix","tag-example-of-suffix","tag-fm-index","tag-give-atleast-100-examples-of-affixes","tag-gram-suffix","tag-index-construction","tag-linestring","tag-new-method","tag-on-suffix","tag-prefix-and-suffix-examples-pdf","tag-prefix-suffix-pdf","tag-prefixes-and-suffixes-pdf","tag-search-suffix","tag-sort-strings-online","tag-string-algorithms-pdf","tag-string-reverse-online","tag-suffix","tag-suffix-and-prefix-pdf","tag-suffix-array-algorithm","tag-suffix-array-java","tag-suffix-examples","tag-suffix-line","tag-suffix-meaning-on-application","tag-suffix-of-or","tag-suffix-of-use","tag-suffix-on-application","tag-suffix-prefix","tag-suffix-table","tag-suffix-tree-algorithm","tag-suffixes-exercises-pdf","tag-suffixes-list-pdf","tag-suffixes-pdf","tag-table-suffix","tag-text-compression","tag-time-computers","tag-to-compute","tag-word-usage","tag-words-about-time","tag-words-with-suffix-y","tag-words-with-the-suffix-y","tag-words-with-y-and-x"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25515","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=25515"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25515\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}