{"id":26498,"date":"2017-12-20T21:21:47","date_gmt":"2017-12-20T15:51:47","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26498"},"modified":"2017-12-20T21:21:47","modified_gmt":"2017-12-20T15:51:47","slug":"maximum-length-chain-pairs","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/maximum-length-chain-pairs\/","title":{"rendered":"Maximum Length Chain of Pairs"},"content":{"rendered":"<p>You are given n pairs of numbers. In every pair, the first number is always smaller than the second number. <span id=\"more-23245\"><\/span>A pair (c, d) can follow another pair (a, b) if b < c. Chain of pairs can be formed in this fashion. Find the longest chain which can be formed from a given set of pairs.<\/p>\n<p>For example, if the given pairs are {{5, 24}, {39, 60}, {15, 28}, {27, 40}, {50, 90} }, then the longest chain that can be formed is of length 3, and the chain is {{5, 24}, {27, 40}, {50, 90}}<\/p>\n<p>This problem is a variation of standard Longest Increasing Subsequence problem. Following is a simple two step process.<\/p>\n<ul>\n<li>Sort given pairs in increasing order of first (or smaller) element.<\/li>\n<li>Now run a modified LIS process where we compare the second element of already finalized LIS with the first element of new LIS being constructed.<\/li>\n<\/ul>\n[ad type=\u201dbanner\u201d]\n<p>The following code is a slight modification of method 2 of this post.<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0A%2F%2F%20Structure%20for%20a%20pair%0Astruct%20pair%0A%7B%0A%20%20int%20a%3B%0A%20%20int%20b%3B%0A%7D%3B%0A%20%0A%2F%2F%20This%20function%20assumes%20that%20arr%5B%5D%20is%20sorted%20in%20increasing%20order%0A%2F%2F%20according%20the%20first%20(or%20smaller)%20values%20in%20pairs.%0Aint%20maxChainLength(%20struct%20pair%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20int%20i%2C%20j%2C%20max%20%3D%200%3B%0A%20%20%20int%20*mcl%20%3D%20(int*)%20malloc%20(%20sizeof(%20int%20)%20*%20n%20)%3B%0A%20%0A%20%20%20%2F*%20Initialize%20MCL%20(max%20chain%20length)%20values%20for%20all%20indexes%20*%2F%0A%20%20%20for%20(%20i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B%20)%0A%20%20%20%20%20%20mcl%5Bi%5D%20%3D%201%3B%0A%20%0A%20%20%20%2F*%20Compute%20optimized%20chain%20length%20values%20in%20bottom%20up%20manner%20*%2F%0A%20%20%20for%20(%20i%20%3D%201%3B%20i%20%3C%20n%3B%20i%2B%2B%20)%0A%20%20%20%20%20%20for%20(%20j%20%3D%200%3B%20j%20%3C%20i%3B%20j%2B%2B%20)%0A%20%20%20%20%20%20%20%20%20if%20(%20arr%5Bi%5D.a%20%3E%20arr%5Bj%5D.b%20%26%26%20mcl%5Bi%5D%20%3C%20mcl%5Bj%5D%20%2B%201)%0A%20%20%20%20%20%20%20%20%20%20%20%20mcl%5Bi%5D%20%3D%20mcl%5Bj%5D%20%2B%201%3B%0A%20%0A%20%20%20%2F%2F%20mcl%5Bi%5D%20now%20stores%20the%20maximum%20chain%20length%20ending%20with%20pair%20i%0A%20%0A%20%20%20%2F*%20Pick%20maximum%20of%20all%20MCL%20values%20*%2F%0A%20%20%20for%20(%20i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B%20)%0A%20%20%20%20%20%20if%20(%20max%20%3C%20mcl%5Bi%5D%20)%0A%20%20%20%20%20%20%20%20%20max%20%3D%20mcl%5Bi%5D%3B%0A%20%0A%20%20%20%2F*%20Free%20memory%20to%20avoid%20memory%20leak%20*%2F%0A%20%20%20free(%20mcl%20)%3B%0A%20%0A%20%20%20return%20max%3B%0A%7D%0A%20%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20function%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20struct%20pair%20arr%5B%5D%20%3D%20%7B%20%7B5%2C%2024%7D%2C%20%7B15%2C%2025%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B27%2C%2040%7D%2C%20%7B50%2C%2060%7D%20%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%20%20%20printf(%22Length%20of%20maximum%20size%20chain%20is%20%25d%5Cn%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20maxChainLength(%20arr%2C%20n%20))%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output :<\/strong><\/p>\n<pre>Length of maximum size chain is 3<\/pre>\n<p>Time Complexity: O(n^2) where n is the number of pairs.<\/p>\n<p>The given problem is also a variation of Activity Selection problem and can be solved in (nLogn) time. To solve it as a activity selection problem, consider the first element of a pair as start time in activity selection problem, and the second element of pair as end time.<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Maximum Length Chain of Pairs &#8211; Dynamic Programming &#8211; The given problem is also a variation of Activity Selection problem and can be solved in (nLogn) time.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,83519,70145],"tags":[78251,72847,72842,72845,70483,72848,72840,72846,72987,72843,72992,72854,72844,72850,72839,78962,78960,78961,78957,78958,78955,72852,72855,78959,72853,78956,72851],"class_list":["post-26498","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-closest-pair","category-dynamic-programming","tag-box-stacking-problem","tag-concept-of-dynamic-programming","tag-define-dynamic-programming","tag-definition-of-dynamic-programming","tag-dynamic-programming","tag-dynamic-programming-code-generation-algorithm","tag-dynamic-programming-definition","tag-dynamic-programming-in-data-structure","tag-dynamic-programming-in-java","tag-dynamic-programming-problems","tag-dynamic-programming-python","tag-dynamic-programming-set-1","tag-dynamic-programming-software","tag-explain-dynamic-programming","tag-how-to-solve-dynamic-programming-problems","tag-longest-chain","tag-longest-string-chain","tag-longest-string-chain-c","tag-maximum-length-of-pairs-in-increasing-order","tag-maximumlengthchainpair","tag-print-maximum-length-chain-of-pairs","tag-problems-on-dynamic-programming","tag-simple-dynamic-programming-example","tag-string-chain","tag-types-of-dynamic-programming","tag-you-are-given-pairs-of-number","tag-youtube-dynamic-programming"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26498","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=26498"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26498\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}