Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Exercises 9 1.

Longest increasing subsequence We are given an array A[1::n] of n integers and want to nd a longest strictly increasing subsequence in A. We say a sequence of integers (x1 ; x2 ; :::; xm ) is strictly increasing if x1 < x2 < ::: < xm : Here is an example of an array A of length n = 14: A= 3 4 1 12 4 6 10 5 7 11 9 4 2 8

The subsequence (a1 ; a2 ; a7 ; a10 ) = (3; 4; 10; 11) is strictly increasing. Give an algorithm to nd the longest increasing subsequence. The running time should be O(n2 ): Solution. Let L(j ) the length of the the longest strictly increasing subsequence ending with the element A[j ]: Then L(1) = 1 L(j ) = max

k<j A[k]<A[j ]

L(k ) + 1

(the optimal way to obtain a strictly increasing subsequence that ends at A[j ] is to extend the longest increasing subsequence ending at some earlier position k ). The answer to our problem is maxfL[1]; L[2]; :::; L[n]g: Running time: we have n subproblems, each of them takes O(n) time, so the total time is O(n2 ): 2. Game strategy Consider a row of coins of values v1 ; :::; vn , with n even. Two players, A and B , play the following game: in each round, a player takes either the rst or the last coin from the row. Determine the maximum amount of money that A can win, no matter what B does. Solution. Denote by M (i; j ) the maximum amount A can win if is his turn and the remaining coins are vi ; :::; vj : The solution to our problem is M (1; n): The base case: M (i; i) = vi ; M (i; i + 1) = maxfvi ; vi+1 g; M (i; i + 2) = maxfvi ; vi+2 g: The general recurrence comes from the observation that, when the remaining coins are vi ; :::; vj ; A can take either vi or vj : Thus: M (i; j ) = maxfminfM (i + 1; j 1); M (i + 2; j )g + vi ; minf(M (i; j 2); M (i + 1; j 1)g + vj g Running time: O(n2 ) subproblems, each one takes constant time to solve, so the total running time is O(n2 ): 3. Shipping items We are given n quantities s1 ; :::; sn that must be shipped. There are two choices - choice A: in order to ship si ; the cost is rsi - choice B: to ship any item, the cost is C , but the contracts must be made for blocks of 4 consecutive quantities.
1

The goal is to nd a schedule of the choices A and B such that the total amount paid to be minimum. Solution. Let M (i) the minimum amount that must be paid to ship the items s1 ; :::; si ; so the solution to our problem is M (n): Then M (0) = 0; M (1) = rs1 ; M (2) = r(s1 + s2 ); M (3) = r(s1 + s2 + s3 ) M (i) = minfM (i 4) + 4C; M (i 1) + rsi g: It s immediate to see that the running time is O(n): 4. Maximal independent set in trees In a graph, an independent set is a set of vertices such that no two of them are adjacent. We are given as input a tree T with n vertices, with the root r. Give a linear-time algorithm to output the size of a maximal independent set in T . Solution: For each vertex v of the tree, we have two choices: either we put it into the independent set, or not, and this decision in uences the decision we must take for the neighbours of v . Thus, it appears a natural idea to store, for each vertex v of the tree, two quantities: s+ (v ) = the size of a maximum independent set which includes v s (v ) = the size of a maximum independent set which does not include v . To nd s+ (v ); s (v ); we must have these quantities computed for all the children of v , so we must do a post-order traversal of the tree. The answer to our problem is maxfs+ (r); s (r)g: The base case: v is a leaf. Then s+ (v ) = 1; s (v ) = 0: If v has the children v1 ; :::; vd ; s+ (v ) = 1 +
d X i=1 d X i=1

s(vi )

s (v ) =

maxfs+ (vi ); s (vi )g

Running time: each subproblem fs+ (v ); s (v )g takes a time proportional to the number of children of v . Thus, the total running time is linear in the number of edges of the tree, which is n 1: 5. Gerrymandering Suppose we have a set of n precincts P1 ; P2 ; :::; Pn , each containing m registered voters. We re supposed to divide these precincts into two districts, each consisting of n=2of the precincts. Now, for each precinct, we have information on how many voters are registered to each of two political parties. (Suppose for simplicity that every voter is registered to one of these two.) So, for each precinct Pi , there are ai voters for party A. We ll say that the set of precincts is susceptible to gerrymandering if it is possible to perform the division into two districts in such a way that the same party (let s call it A) holds a majority in both districts.
2

Give an algorithm to determine whether a given set of precincts is susceptible to gerrymandering; the running time of your algorithm should be polynomial in n and m. Example: Suppose we have n = 4 precincts, and the following information on registered voters. precinct Number registered for Party Number registered for Party B 1 55 45 2 43 57 3 60 40 4 47 53 This set of precincts is susceptible, since if we grouped precincts 1 and 4 into one district, and precincts 2 and 3 into the other, then party A would have a majority in both districts. This example is a quick illustration of the basic unfairness in gerrymandering: although party A holds only a slim majority in the overall population (205 to 195), it ends up with a majority in not one but both districts. Solution. The rst step is to obtain a mathematical relation to express whether or not it is possible to gerrymander. Suppose that the given set of precincts is susceptible to gerrymandering in favor of party A, and let D and D the two sets of n=2 precincts. If A has majority in D then X mn ai > 4
i2D

If A has majority in D; then X ai =


n X i21

ai

i2D

i2D

ai >

mn : 4

Thus
n X i21

ai

mn X mn > ai > 4 4
i2D

(1)

It is easy to see that the converse is also true, i.e. if there exists a set D of n=2 precincts such that (1) holds, then D and D is a partitioning of the n precincts which gives party A the majority in both districts. This observation leads to an idea to solve this problem: we check if there is a subset D of n=2 precincts such that (1) to be true. P Let P (i; j; k ) = 1 if there is a set M f1; :::; ig such that jM j = j and al = k: P (i; j; k ) = 0 otherwise. Then:
l2M

P (i; j; k ) = 0 when j > i or k > jm or k < 0 and P (i; j; k ) = 1 if P (i P (i; j; k ) = 1 if P (i


3

1; j; k ) = 1 1; j 1; k ai ) = 1

We can group the previous two relations into a single one: P (i; j; k ) = maxfP (i 1; j; k ); P (i 1; j 1; k ai )g:

After completing the matrix P , we check for all k such that X mn <k< ai 4 i21
n

mn ; 4

if P (n; n=2; k ) = 1: If we nd such a k , it means that party A can gerrymander. The number of subproblems: i ranges from 1 to n j ranges from 1 to n k ranges from 0 to jm Thus we have mn3 subproblems, and each of them takes constant time, so the total running time is O(mn3 ):

You might also like