You are given n pairs of numbers. In every pair, the first number is always smaller than the second number. 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.

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}}

This problem is a variation of standard Longest Increasing Subsequence problem. Following is a simple two step process.

  • Sort given pairs in increasing order of first (or smaller) element.
  • 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.
[ad type=”banner”]

The following code is a slight modification of method 2 of this post.

[pastacode lang=”c” manual=”%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” message=”C” highlight=”” provider=”manual”/]

Output :

Length of maximum size chain is 3

Time Complexity: O(n^2) where n is the number of pairs.

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.

[ad type=”banner”]