Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1.  
  2. /*
  3. * Assignment: Longest increasing subsequence
  4. *
  5. * Sequences are a natural source of computational problems. One such
  6. * family of problems involves finding subsequences with specified
  7. * properties in a given sequence. This exercise asks you to write
  8. * a program that, given a sequence
  9. *
  10. * s(0), s(1), ..., s(n-1)
  11. *
  12. * of integers as input, finds a ___longest increasing subsequence___
  13. * of the sequence s.
  14. *
  15. * For example, suppose we are given as input the sequence
  16. *
  17. * 72, 16, 51, 17, 6, 21, 92, 59, 54, 78, 41, 33, 94,
  18. * 85, 83, 56, 2, 46, 57, 44, 73, 6, 47, 47, 0.
  19. *
  20. * In this sequence, a longest increasing subsequence has length 7.
  21. * One example of such an increasing subsequence is
  22. *
  23. * 16 < 17 < 21 < 54 < 56 < 57 < 73.
  24. *
  25. * More generally, your program must be such that
  26. * given a sequence
  27. *
  28. * s(0), s(1), ..., s(n-1)
  29. *
  30. * of integers as input, the program returns a subsequence
  31. *
  32. * s(i_1), s(i_2), ..., s(i_k)
  33. *
  34. * that meets all of the following three properties:
  35. *
  36. * 1. The positions that define the subsequence are increasing:
  37. *
  38. * 0 <= i_1 < i_2 < ... < i_k <= n-1
  39. *
  40. * 2. The subsequence is increasing:
  41. *
  42. * s(i_1) < s(i_2) < ... < s(i_k)
  43. *
  44. * 3. The length k of the subsequence is as large as possible.
  45. *
  46. *
  47. * Hints:
  48. *
  49. * You can solve this exercise using __dynamic programming__ as a
  50. * solution strategy. That is, you __tabulate__ solutions of carefully
  51. * designed subproblems and use these solutions to gradually build your
  52. * way up to solve larger and larger subproblems until you solve
  53. * the original problem. Here are some more detailed hints:
  54. *
  55. * a) Suppose that for all i = 0, 1, ..., j-1 we know L(i), the length
  56. * of a longest increasing subsequence in s(0), s(1), ..., s(i)
  57. * that terminates at position i.
  58. * b) Suppose we have already computed in an array the values
  59. * L(0), L(1), ..., L(j-1). How can we make use of these values
  60. * to compute the next value L(j) ?
  61. * c) Furthermore, suppose for each i we know p(i), the position
  62. * prior to the last position i in a longest increasing subsequence
  63. * that terminates at position i.
  64. * (Note that such a position p(i) need not always exist, however.)
  65. * d) How can we make use of the values p(i) for i <= j to determine
  66. * a concrete increasing subsequence of length L(j)
  67. * that terminates at j ?
  68. * e) Design your program to complete the arrays L and p, one position
  69. * at a time.
  70. * f) Use the arrays L and p to return a concrete longest increasing
  71. * subsequence in the given sequence s.
  72. *
  73. */
  74.  
  75. package object longestIncreasingSubsequence {
  76.  
  77. /** Returns a longest increasing subsequence in the sequence s.
  78. * If there are multiple such subsequences, any subsequence will do. */
  79.  
  80. def longestIncreasingSubsequence(s: Seq[Int]): Seq[Int] = {
  81. require(s.length > 0)
  82.  
  83. val L = new Array[Int](s.length)
  84. val p = new Array[Option[Int]](s.length)
  85. L(0) = 1
  86. p(0) = None
  87. val sWithIndex = s.zipWithIndex
  88. for (elem <- sWithIndex.tail) {
  89. val smaller = sWithIndex.take(elem._2+1).filter(_._1<elem._1)
  90. if (smaller.isEmpty) {
  91. L(elem._2) = 1
  92. p(elem._2) = None
  93. } else {
  94. val biggestSubsequence = smaller.map((x) => (L(x._2), x._2)).maxBy(_._1)
  95. L(elem._2) = biggestSubsequence._1 + 1
  96. p(elem._2) = Some(biggestSubsequence._2)
  97. }
  98. }
  99. val subSequence = new scala.collection.mutable.ArrayBuffer[Int]
  100. var pointer = L.indexOf(L.max)
  101. subSequence += s(pointer)
  102. while(p(pointer).nonEmpty) {
  103. pointer = p(pointer).get
  104. subSequence += s(pointer)
  105. }
  106. subSequence.reverse
  107. }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement