{"id":26244,"date":"2017-10-26T20:41:49","date_gmt":"2017-10-26T15:11:49","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26244"},"modified":"2017-10-26T20:41:49","modified_gmt":"2017-10-26T15:11:49","slug":"python-programming-longest-bitonic-subsequence","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-programming-longest-bitonic-subsequence\/","title":{"rendered":"Python Programming &#8211; Longest Bitonic Subsequence"},"content":{"rendered":"<p>Given an array arr[0 \u2026 n-1] containing n positive integers, a subsequence of arr[] is called Bitonic if it is first increasing, then decreasing. Write a function that takes an array as argument and returns the length of the longest bitonic subsequence.<span id=\"more-19729\"><\/span><br \/>\nA sequence, sorted in increasing order is considered Bitonic with the decreasing part as empty. Similarly, decreasing order sequence is considered Bitonic with the increasing part as empty.<\/p>\n<p><strong>Examples:<\/strong><\/p>\n<pre>Input arr[] = {1, 11, 2, 10, 4, 5, 2, 1};\r\nOutput: 6 (A Longest Bitonic Subsequence of length 6 is 1, 2, 10, 4, 2, 1)\r\n\r\nInput arr[] = {12, 11, 40, 5, 3, 1}\r\nOutput: 5 (A Longest Bitonic Subsequence of length 5 is 12, 11, 5, 3, 1)\r\n\r\nInput arr[] = {80, 60, 30, 40, 20, 10}\r\nOutput: 5 (A Longest Bitonic Subsequence of length 5 is 80, 60, 30, 20, 10)<\/pre>\n<p><strong>Solution<\/strong><br \/>\nThis problem is a variation of standard Longest Increasing Subsequence (LIS) problem. Let the input array be arr[] of length n. We need to construct two arrays lis[] and lds[] using Dynamic Programming solution of LIS problem. lis[i] stores the length of the Longest Increasing subsequence ending with arr[i]. lds[i] stores the length of the longest Decreasing subsequence starting from arr[i]. Finally, we need to return the max value of lis[i] + lds[i] \u2013 1 where i is from 0 to n-1.<\/p>\n[ad type=\u201dbanner\u201d]\n<p>Following is Python implementation of the above Dynamic Programming solution.<\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Dynamic%20Programming%20implementation%20of%20longest%20bitonic%20subsequence%20problem%0A%22%22%22%0Albs()%20returns%20the%20length%20of%20the%20Longest%20Bitonic%20Subsequence%20in%0Aarr%5B%5D%20of%20size%20n.%20The%20function%20mainly%20creates%20two%20temporary%20arrays%0Alis%5B%5D%20and%20lds%5B%5D%20and%20returns%20the%20maximum%20lis%5Bi%5D%20%2B%20lds%5Bi%5D%20-%201.%0A%20%0Alis%5Bi%5D%20%3D%3D%3E%20Longest%20Increasing%20subsequence%20ending%20with%20arr%5Bi%5D%0Alds%5Bi%5D%20%3D%3D%3E%20Longest%20decreasing%20subsequence%20starting%20with%20arr%5Bi%5D%0A%22%22%22%0A%20%0Adef%20lbs(arr)%3A%0A%20%20%20%20n%20%3D%20len(arr)%0A%20%0A%20%0A%20%20%20%20%23%20allocate%20memory%20for%20LIS%5B%5D%20and%20initialize%20LIS%20values%20as%201%0A%20%20%20%20%23%20for%20all%20indexes%0A%20%20%20%20lis%20%3D%20%5B1%20for%20i%20in%20range(n%2B1)%5D%0A%20%0A%20%20%20%20%23%20Compute%20LIS%20values%20from%20left%20to%20right%0A%20%20%20%20for%20i%20in%20range(1%20%2C%20n)%3A%0A%20%20%20%20%20%20%20%20for%20j%20in%20range(0%20%2C%20i)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20((arr%5Bi%5D%20%3E%20arr%5Bj%5D)%20and%20(lis%5Bi%5D%20%3C%20lis%5Bj%5D%20%2B1))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20lis%5Bi%5D%20%3D%20lis%5Bj%5D%20%2B%201%0A%20%0A%20%20%20%20%23%20allocate%20memory%20for%20LDS%20and%20initialize%20LDS%20values%20for%0A%20%20%20%20%23%20all%20indexes%0A%20%20%20%20lds%20%3D%20%5B1%20for%20i%20in%20range(n%2B1)%5D%0A%20%20%20%20%20%0A%20%20%20%20%23%20Compute%20LDS%20values%20from%20right%20to%20left%0A%20%20%20%20for%20i%20in%20reversed(range(n-1))%3A%20%23loop%20from%20n-2%20downto%200%0A%20%20%20%20%20%20%20%20for%20j%20in%20reversed(range(i-1%20%2Cn))%3A%20%23loop%20from%20n-1%20downto%20i-1%0A%20%20%20%20%20%20%20%20%20%20%20%20if(arr%5Bi%5D%20%3E%20arr%5Bj%5D%20and%20lds%5Bi%5D%20%3C%20lds%5Bj%5D%20%2B%201)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20lds%5Bi%5D%20%3D%20lds%5Bj%5D%20%2B%201%0A%20%0A%20%0A%20%20%20%20%23%20Return%20the%20maximum%20value%20of%20(lis%5Bi%5D%20%2B%20lds%5Bi%5D%20-%201)%0A%20%20%20%20maximum%20%3D%20lis%5B0%5D%20%2B%20lds%5B0%5D%20-%201%0A%20%20%20%20for%20i%20in%20range(1%20%2C%20n)%3A%0A%20%20%20%20%20%20%20%20maximum%20%3D%20max((lis%5Bi%5D%20%2B%20lds%5Bi%5D-1)%2C%20maximum)%0A%20%20%20%20%20%0A%20%20%20%20return%20maximum%0A%20%0A%23%20Driver%20program%20to%20test%20the%20above%20function%0Aarr%20%3D%20%20%5B0%20%2C%208%20%2C%204%2C%2012%2C%202%2C%2010%20%2C%206%20%2C%2014%20%2C%201%20%2C%209%20%2C%205%20%2C%2013%2C%0A%20%20%20%20%20%20%20%203%2C%2011%20%2C%207%20%2C%2015%5D%0Aprint%20%22Length%20of%20LBS%20is%22%2Clbs(arr)\u201d message=\u201dPython\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output :<\/strong><\/p>\n<pre> Length of LBS is 7\r\n<\/pre>\n<p>Time Complexity: O(n^2)<br \/>\nAuxiliary Space: O(n)<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Python Programming &#8211; Longest Bitonic Subsequence &#8211; Dynamic Programming arr[0.n-1] containing positive integers, a subsequence of arr[] is called Bitonic<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,1,70145,4148],"tags":[78010,78005,78006,78008,78003,72847,72842,72845,70483,78007,72848,72840,78009,72846,72994,72843,72992,72854,72844,72850,78002,72839,78001,78018,78020,78019,78011,78004,78000,72852,72855,72853,72851],"class_list":["post-26244","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-coding","category-dynamic-programming","category-python","tag-bitonic-array","tag-bitonic-sequence-example","tag-bitonic-sequence-in-parallel-algorithm","tag-bitonic-series","tag-bitonic-subsequence","tag-concept-of-dynamic-programming","tag-define-dynamic-programming","tag-definition-of-dynamic-programming","tag-dynamic-programming","tag-dynamic-programming-bitonic-sequence","tag-dynamic-programming-code-generation-algorithm","tag-dynamic-programming-definition","tag-dynamic-programming-for-bitonic-sequence","tag-dynamic-programming-in-data-structure","tag-dynamic-programming-in-python","tag-dynamic-programming-problems","tag-dynamic-programming-python","tag-dynamic-programming-set-1","tag-dynamic-programming-software","tag-explain-dynamic-programming","tag-find-the-length-of-longest-bitonic-subsequence-in-an-array","tag-how-to-solve-dynamic-programming-problems","tag-longest-bitonic-subsequence","tag-longest-bitonic-subsequence-in-python","tag-longest-bitonic-subsequence-in-python-code","tag-longest-bitonic-subsequence-in-python-program","tag-longest-decreasing-subsequence","tag-maximum-length-bitonic-subsequence-dynamic-programming","tag-printing-longest-bitonic-subsequence","tag-problems-on-dynamic-programming","tag-simple-dynamic-programming-example","tag-types-of-dynamic-programming","tag-youtube-dynamic-programming"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26244","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=26244"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26244\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}