{"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=&#8221;banner&#8221;]\n<p>Following is Python implementation of the above Dynamic Programming solution.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Python<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Dynamic Programming implementation of longest bitonic subsequence problem<br\/>&quot;&quot;&quot;<br\/>lbs() returns the length of the Longest Bitonic Subsequence in<br\/>arr[] of size n. The function mainly creates two temporary arrays<br\/>lis[] and lds[] and returns the maximum lis[i] + lds[i] - 1.<br\/> <br\/>lis[i] ==&gt; Longest Increasing subsequence ending with arr[i]<br\/>lds[i] ==&gt; Longest decreasing subsequence starting with arr[i]<br\/>&quot;&quot;&quot;<br\/> <br\/>def lbs(arr):<br\/>    n = len(arr)<br\/> <br\/> <br\/>    # allocate memory for LIS[] and initialize LIS values as 1<br\/>    # for all indexes<br\/>    lis = [1 for i in range(n+1)]<br\/> <br\/>    # Compute LIS values from left to right<br\/>    for i in range(1 , n):<br\/>        for j in range(0 , i):<br\/>            if ((arr[i] &gt; arr[j]) and (lis[i] &lt; lis[j] +1)):<br\/>                lis[i] = lis[j] + 1<br\/> <br\/>    # allocate memory for LDS and initialize LDS values for<br\/>    # all indexes<br\/>    lds = [1 for i in range(n+1)]<br\/>     <br\/>    # Compute LDS values from right to left<br\/>    for i in reversed(range(n-1)): #loop from n-2 downto 0<br\/>        for j in reversed(range(i-1 ,n)): #loop from n-1 downto i-1<br\/>            if(arr[i] &gt; arr[j] and lds[i] &lt; lds[j] + 1):<br\/>                lds[i] = lds[j] + 1<br\/> <br\/> <br\/>    # Return the maximum value of (lis[i] + lds[i] - 1)<br\/>    maximum = lis[0] + lds[0] - 1<br\/>    for i in range(1 , n):<br\/>        maximum = max((lis[i] + lds[i]-1), maximum)<br\/>     <br\/>    return maximum<br\/> <br\/># Driver program to test the above function<br\/>arr =  [0 , 8 , 4, 12, 2, 10 , 6 , 14 , 1 , 9 , 5 , 13,<br\/>        3, 11 , 7 , 15]<br\/>print &quot;Length of LBS is&quot;,lbs(arr)<\/code><\/pre> <\/div>\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=&#8221;banner&#8221;]\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}]}}