Problem 1
Part a
For and , define
where substrings defined by invalid ranges are considered to be the empty string.
The natural recurrence is
Proof.
The cases for and are clear. Suppose is the SCS of and . Suppose has length . If , then must be . must be the SCS of and ; if it weren’t, we would be able to construct a shorter common subsequence of and by the usual cut-and-paste argument. Thus, . If , then must be either or , and the same argument yields either is the SCS of and or the SCS of and respectively.□
The algorithm runs in time.
Part b
The problem of finding the longest bitonic subsequence can be reduced to the problem of finding the longest increasing subsequence :
Below, we implement an algorithm for which runs in time.
returns the largest index of such that .
Claim 336.1.
In the course of its execution, if the algorithm places in position of , there exists an increasing subsequence of of length ending in .
Proof.
his is clear for. Suppose the claim is true for . Suppose the algorithm places in position . There exists such that and the algorithm placed in position . By the induction hypothesis, there exists an increasing subsequence of length ending in ; appending gives one of length .□
It is also clear that if is any increasing subsequence of , the length of at the end of execution will be greater than or equal to . Thus, it follows that is the length of the longest common subsequence.
This allows to run in time.
Part c
It is clear that there exists an alternating subsequence of of length . Since counts the number of oscillations, a longer oscillating subsequence cannot exist. The algorithm runs in time.
Part d
A sequence is convex iff its consecutive differences are strictly increasing:
So we must find the longest subsequence of indices whose consecutive differences are strictly increasing.
Define for and . Let
Then the natural recurrence is
The algorithm
accepts two arrays and of size and returns an array of size such that . accepts the output of and sorts it such that the values in the second slot are increasing. returns the maximum index in such that using binary search.
Correctness
Claim 336.2.
After the outer loop has processed all indices , for every the table entry equals the length of the longest convex subsequence whose last two indices are (Also ).
We initialize . The inner loop for constructs for . The list contains only the sentinel pair , so pos(F, D[1,j]) picks that sentinel and sets , which is correct.
Inductive step. Suppose the invariant holds for all . Consider fixed . We form the list of pairs for (including the sentinel with and ). We sort in increasing order of the second slot, and take prefix maxima over the first slot, that is, we set to be . Thus pos(F, D[a,b]) finds the maximal among indices with . The assignment
for each exactly implements the recurrence in (E1). Therefore after processing index , the invariant holds for all pairs ending at .
Complexity
- Precomputing differences: .
- For each we sort pairs in , totaling .
- For each we then answer queries
posby binary search on , with an overall complexity of .
Thus, the algorithm runs in .
Problem 2
Let P be a set of n points evenly distributed on the unit circle, and let S be a set of m line segments with endpoints in P. The endpoints of the m segments are not necessarily distinct; n could be significantly smaller than 2m. (a) Describe an algorithm to find the size of the largest subset of segments in S such that every pair is disjoint. Two segments are disjoint if they do not intersect even at their endpoints. (b) Describe an algorithm to find the size of the largest subset of segments in S such that every pair is interior-disjoint. Two segments are interior-disjoint if their intersection is either empty or an endpoint of both segments. (c) Describe an algorithm to find the size of the largest subset of segments in S such that every pair intersects. (d) Describe an algorithm to find the size of the largest subset of segments in S such that every pair crosses. Two segments cross if they intersect but not at their endpoints.
Let be the list of points and be the list of edges. Represent edges by objects with properties , , and , where , , and for all edges .
Part a
Define
for , with .
The recurrence is (any out of bounds access returns 0)
The edges are preprocessed to obtain a list , where is a list of all edges with . This can be done in time.
Edge is accessed for the computation of with . Thus, the for loop takes time overall. Filling in the DP array takes time, so the total running time is .
Part b
We only need to slightly modify the definitions from part a. Let be if there exists an edge with and , and otherwise.
The algorithm is essentially the same as in part a, and runs in time.
Part d
Let be a 2d array such that is an array containing all edges such that sorted in decreasing order of the property . Assign to be the index of in for all .
Let be an array such that is the number of edges satisfying .
Let be a 2d array such that is an array containing all edges such that sorted in decreasing order of the property .
The preprocessing requires time. runs in time.
Correctness
We wish to encode the information provided about the circle as an enumeration of the edges and a string over the alphabet such that
- Each symbol in the alphabet appears exactly once, and appears before .
- and with intersect(interiors intersect, no shared endpoints) iff is a subsequence of
- iff appears before in
- iff appears before in .
Let the enumeration be given by the property of the edges assigned above. Construct like so:
Suppose , , .
- Suppose and intersect in the interior of the circle. We must have . By the manner in which we assigned the property, . Also, . Therefore, is a subsequence of .
- Suppose and do not intersect, even at their endpoints. Then, we have and . It follows that , is a subsequence of , and since each symbol appears only once, cannot be a subsequence of .
- Suppose and . Since every slot of is sorted in decreasing order of the property , . It follows that is a subsequence of , and thus cannot be a subsequence of .
- Suppose and . Again, . Since each slot of is sorted in decreasing order of the property , it follows that is a subsequence of , and thus cannot be a subsequence of .
We have shown that is a subsequence of and intersect in the interior of the circle. Since is constructed in such a way that the ‘s appear in increasing order of the edge s, it. suffices to find the longest increasing subsequence of ‘s in tails such that . This is exactly what does.
Part c
We need only make a few changes to the solution of part d.
Let be a 2d array such that is an array containing all edges such that sorted in increasing order of the property . Assign to be the index of in for all .
Let be an array such that is the number of edges satisfying .
Let be a 2d array such that is an array containing all edges such that sorted in increasing order of the property .
This makes recognize edges which share an endpoint as intersecting vertices.
Now, returns the required result in time.
Problem 3
We can assume all sequences start with . Note that no shortest sequence will have two consecutive increments, since can be replaced by . Thus, if a shortest sequence has doublings, it can have at most increments. Such sequences of and can be interpreted as binary numbers, which the following algorithm exploits:
Let denote the length of the shortest sequence of increments and doublings which achieves from . It is evident that the algorithm follows a sequence of increments and doubles, and that after the conclusion of the for loop. Thus, is an upper bound for . Suppose the sequence discovered by uses doublings and increments. Then, . Suppose is the shortest possible sequence, achieving with doublings and increments. Suppose . Since and no two increments can appear consecutively, we have , a contradiction. If , we would have and equality of the form
with , , and , which is impossible. This, is optimal.
Problem 4
Part a
Let be the columns of . Let denote the tuple . Clearly . If and then any linear combination of vectors in may be viewed as if it were in , hence is linearly independent, i.e. . Further, if with then , since if that were the case, would be spanned by fewer than or equal to vectors, contradicting the independence of . Let . Indeed since and is linearly independent. Hence is a matroid.
Part b
Let and let . Clearly since must contain some maximal independent set of . If and then and since contains a maximal independent set of , so does and hence .
Suppose , and . For all , suppose , that is, does not contain a maximal independent set of . It follows that every maximal independent set of in contains . Since , must contain a maximal independent set of . Since does not contain , cannot lie in , so must intersect . However, by using the exchange property of , every element can be replaced with an element , where is an independent set in . Let be the set obtained from after all elements of have been replaced with elements of . Since all maximal independent sets have the same cardinality, is a maximal independent set of . Since we made at most exchanges, , and , does not contain . This is a contradiction, since we have previously determined that all independent sets of in must contain .
Part c
Let and let be the given partition of . Clearly since for all . If and then for all , hence . If with then there is an for which and . Let then . Hence is a matroid.
Problem 5
The mechanism to pick the vertex with the shortest distance has been modified to use buckets in pace of the priority queue used in the vanilla algorithm. Bounding the weights by allows us to bound maximum weight of a path in the graph by . Every vertex is processed, since a vertex can only be inserted into a later bucket or at the end of the current bucket.
Each edge is processed once, and we pass through buckets. Thus, the algorithm runs in time.
Problem 6
Suppose is a binary tree that is not full. Then there is a node which has only one child . We split the solution into two cases:
- If is the root node then delete and use are the root node instead.
- If is not the root node then it has a parent, say . Here, delete and attach to .
In both cases, we have shown that there is a new tree which uses less bits to encode the data (since we are deleting an edge). Hence cannot correspond to an optimal code.