Solutions by Prof. Ruiz
Input: - An array A[1...n] of n positions, containing a sorted (in ascending order) sequence of numbers. That is A[1] ≤ A[2] ≤ ... &le A[n]. - A value v. Output: - An index i such that v = A[i], OR the message "v is not in A" if v doesn't appear in A.Consider the following divide-and-conquer solution to this problem. This solution is called binary search: Compare the element in the midpoint of the array A with v and eliminate half of the array from further consideration, until either v is found in A, or no elements in A remain under consideration.
For example, let: ------------------------------------------------- A = | 3 | 3 | 4 | 6 | 7 | 10 | 12 | 17 | 25 | 32 | 41 | ------------------------------------------------- index: 1 2 3 4 5 6 7 8 9 10 11 n = 11 Our Binary-Search(A,v,1,n) algorithm would work as follows:
Solve this problem following the steps below.
Solution
See the solutions to HW5 Problem 4 (By Piotr Mardziel) of my offering of CS2223 in B term 2005.
7 3 4 9 2 8 4 5 2 1 6 3 10then the length of the longest decreasing CONTIGUOUS subsequence in this sequence is: 3, and in the maximal such subsequence is:
5 2 1The input-output description of this problem is:
Input: A sequence S = a1, a2,..., an (you can assume that the sequence is stored in an array) of length n, where each ai is a real number.
Output: Your pseudo-code should print the following output:
Solve this problem following the steps below.
Solution
Given a sequence a1...an, we divide the problem into the following n overlapping subproblems:SubPbm 1: Find the longest strictly decreasing contiguous subsequence in a1 that ends in a1 SubPbm 2: Find the longest strictly decreasing contiguous subsequence in a1 a2 that ends in a2 SubPbm 3: Find the longest strictly decreasing contiguous subsequence in a1 a2 a3 that ends in a3 ... SubPbm j: Find the longest strictly decreasing contiguous subsequence in a1 a2 a3 ... aj that ends in aj ... SubPbm n: Find the longest strictly decreasing contiguous subsequence in a1 a2 a3 ... aj... an that ends in anWe'll solve these subproblems in the order listed above.
Solution
We will use an array OPT[1..n] to store the solution to each of these subproblems:OPT[j] = Length of the longest strictly decreasing contiguous subsequence in a1...aj that ends in aj.
Solution
OPT[1]=1
Solution
We will solve Subpbm j using the solutions to Subpbms 1,...,j-1 as follows: The longest contiguous subsequence in a1 a2 a3 ... aj that ends in aj will be EITHER the result of concatenating the longest subsequence in a1 a2 a3 ... a(j-1) that ends in a(j-1), if a(j-1) > aj; OR aj by itself, if a(j-1) ≤ aj. In other words:| 1 + OPT[j-1], if a(j-1) > aj OPT[j] = | | 1, otherwiseAs an illustration, note that for the given example,Sequence: 7 3 4 9 2 8 4 5 2 1 6 3 10 Index: 1 2 3 4 5 6 7 8 9 10 11 12 13 we have: OPT[1]=1 the longest strictly decreasing contiguous subsequence in 7 ending in 7 has length 1 (namely 7) OPT[2]=2 the longest strictly decreasing contiguous subsequence in 7 3 ending in 3 has length 2 (namely 7 3) OPT[3]=1 the longest strictly decreasing contiguous subsequence in 7 3 4 ending in 4 has length 1 (namely 4) OPT[4]=1 the longest strictly decreasing contiguous subsequence in 7 3 4 9 ending in 9 has length 1 (namely 9) OPT[5]=2 the longest strictly decreasing contiguous subsequence in 7 3 4 9 2 ending in 2 has length 2 (namely 9 2) and so on. Note for instance that OPT[5] = 1 + OPT[4] since a4 = 9 > 2 = a5 and OPT[3] = 1 since a2 = 3 < 4 = a4.
Solution
- Base Case: For j=1: OPT[1]=1 correctly as the lenght of the longest strictly decreasing contiguous subsequence in a1 that ends in a1 is 1 (namely a1).
- Induction Step: Let 1 < j ≤ n.
Induction Hypothesis: Assume that for each i < j we have that OPT[i] correctly contains the length of the longest strictly decreasing contiguous subsequence in a1...ai that ends in ai.Now let's prove that this property holds for j too. We need to consider two cases:Hence, the property holds for j too and therefore our recurrence:
- If a(j-1) ≤ aj, then the longest strictly decreasing contiguous subsequence in a1...aj that ends in aj is just aj, and therefore OPT[j] should be equal to 1.
- If a(j-1) > aj, then the longest strictly decreasing contiguous subsequence in a1...aj that ends in aj consists of concatenating the longest strictly decreasing contiguous subsequence in a1...a(j-1) that ends in a(j-1) (which by our induction hypothesis has length OPT[j-1]) with aj. The length of the resulting subsequence is OPT[j-1]+1, and therefore OPT[j] should be equal to OPT[j-1]+1.
| 1 + OPT[j-1], if a(j-1) > aj OPT[j] = | | 1, otherwiseis correct for the given problem.
Solution
Once that all the OPT[j]'s are computed for 1 ≤ j ≤ n, the solution to the full problem (the lenght of the longest strictly decreasing contiguous subsequence in S) is equal to the maximum value in this array:max over all i, 1 ≤ i ≤ n {OPT[i]}
In the example above, the maximum value in OPT[1...n] will be OPT[10]=3 as 5 2 1 is the longest strictly decreasing contiguous subsequence.
Solution
Find-longest-strictly-decreasing-contiguous-subsequence(a1...an) { Array OPT[1...n] of integers OPT[1]:=1 O(1) For j := 2 to n do { n*O(1) If a(j-1) > aj Then { n*O(1) _ OPT[j]:= 1 + OPT[j-1] | } | n*O(1) Else { | OPT[j]:= 1 | } - } Let k be the index of the maximum value in the array OPT[1...n] (or such one index if the maximum value appears more than once in the array) O(n) Print that the length of the longest strictly decreasing contiguous subsequence in a1...an is OPT[k] O(1) Call Print-longest-strictly-decreasing-contiguous-subsequence with array OPT[0...n] and k O(n) } Print-longest-strictly-decreasing-contiguous-subsequence(array OPT[0...n], index k) { If OPT(k) > 1 Then { Print-longest-strictly-decreasing-contiguous subsequence2(OPT[0...n], k-1) } Print ak }Note that runtime of Print-longest-strictly-decreasing-contiguous-subsequence is O(n) since the longest strictly increasing contiguous subsequence has at most n elements and this procedure is called recursively once for each element of the contiguous subsequence.
Solution
The algorithm starts by assigning 1 to OPT[1], correctly implementing the base case. Given a value of j between 2 and n in the For loop, note that the body of this loop assigns OPT[j-1]+1 to OPT[j], if aj < a(j-1); and assigns 1 to OPT[j], if aj ≥ a(j-1) as the recurrence requires.
Solution
See the line by line runtime analysis above next to the algorithm. The total runtime is T(n)=O(n), where n is the length of the input sequence. Furthermore, note that in this case, T(n)=Θ(n) as the algorithm has to process each of the n element in the input sequence and so T(n)=&Omega(n).