0% found this document useful (0 votes)
50 views7 pages

hw1 Soln

The document describes an algorithmic problem to find non-dominated points in a set of points. It provides: 1) A definition of dominated and non-dominated points; 2) An O(n log n) divide-and-conquer algorithm that recursively divides the point set into halves, finds non-dominated points in each half, and merges the results; 3) Details of the merging step and analysis showing the algorithm runs in O(n log n) time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views7 pages

hw1 Soln

The document describes an algorithmic problem to find non-dominated points in a set of points. It provides: 1) A definition of dominated and non-dominated points; 2) An O(n log n) divide-and-conquer algorithm that recursively divides the point set into halves, finds non-dominated points in each half, and merges the results; 3) Details of the merging step and analysis showing the algorithm runs in O(n log n) time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ESO207A: Data Structures and Algorithms

Homework 1: Divide and Conquer HW Due Date: Aug 19, 2018

Problem 1.

(a) State whether each of the following statement is true or false, with a brief reason. (i) n2 =
O(2n ), (ii) n3 = O(n4 log n), (iii) n2 = Ω(n3 ), (iv) 3n = Θ(n!) (v) 2n = Θ(2n+c ), for any
constant c. (10)
(i) True. For n ≥ 5, n2 ≤ 2n and so n2 = O(2n ).
(ii) True. Assume log n is log2 n. For n ≥ 2, n3 ≤ n4 log2 n. If we assume log n as logb c for
some base, b, then log2 c = logb c log2 b. Hence for n ≥ 2, n3 ≤ (log2 b)n4 logb n.
(iii) False. For n2 = Ω(n3 ), (equivalently, n3 = O(n2 )), it must be the case that asymp-
totically, n3 < cn2 , for some positive c. But that is not true, since for any constant c, for
n ≥ 2(c + 1), c · n2 < n3 .
(iv) False. 3n = O(n!) but n! is not O(3n ). Why? Stirling’s approximation says that
n! ≥ (n/e)n ≥ 3n , for any n ≥ 3e. So 3n = O(n!). But for any constant c > 0,
n! ≥ (n/e)n > c · 3n , for all n > 3e(c + 1)1/n . Hence, n! cannot be O(3n ). Note that
for f (n) = Θ(g(n)), both f (n) = O(g(n)) and g(n) = O(f (n)) must hold.

(b) Solve the following recurrence equations. Assume that T (1) = Θ(1) and T (2) = Θ(1) for all
the functions below. Use the recursion tree method (unrolling the recurrence eqn as done in
class). Do not use the master formula. Make and state appropriate simplifying assumptions
if needed. (4 × 5 = 20).

1. T (n) = T (n/2) + n.
2. T (n) = 4T (n/3) + n2 .
3. T (n) = 3T (n/3) + n.
4. T (n) = 2T (n/2) + n log n.

1. T (n) = T (2n/3) + n.
For simplicity, let n be a power of 3.

T (n) = n + T (2n/3) = n + (2n/3) + T ((2/3)2 n)


= n + (2n/3) + (2/3)2 n + . . . + T ((2/3)k n)
= Θ(n) + Θ(1)
= Θ(n)

The unrolling of recursion proceeds up to k steps where, (2/3)k n ≥ 1 > (2/3)k+1 n. The
sum n + (2/3)n + (2/3)2 n . . . is a geometric progression and its sum is upper bounded
by 3n and lower bounded by n + (2/3)n = 5n/3. Hence, the sum is Θ(n).
2. T (n) = 4T (n/3) + n2 .

1
For simplicity, let n be a power of 3.
T (n) = n2 + 4T (n/3)
= n2 + 4(n/3)2 + 42 T (n/32 )
= n2 + 4(n/3)2 + 42 (n/32 )2 + . . . + 4k (n/3k )2 + . . . + 4log3 n T (1)
= n2 + (4/9)n2 + (4/9)2 n2 + . . . + (4/9)log3 n−1 + +4log3 n T (1) .
Plog3 n j j 2 2
The summation j=0 4 (n/3 ) is a geometric series with common ratio 4/3 < 1
2
and hence the sum is Θ( first term ), which is Θ(n ). The last term is 4 3 T (1) = log n

3log3 4 log3 n Θ(1) = nlog3 4 Θ(1), which does not dominate.


3. T (n) = 3T (n/3) + n.
Let n be a power of 3.
T (n) = n + 3T (n/3)
= n + 3(n/3) + 32 T (n/32 )
= n + 3(n/3) + 32 (n/32 ) + . . . + 3k (n/3k ) + . . . + 3log3 n T (1)
= Θ(n log n) .
4. T (n) = 2T (n/2) + n log n.
Let n be a power of 2.
T (n) = n log n + 2T (n/2)
= n log(n) + 2(n/2) log(n/2) + 22 T (n/22 )
= n log(n) + 2(n/2) log(n/2) + . . . + 2k (n/2k ) log(n/2k ) + . . . + 2log2 n T (1)
The general term 2k (n/2k ) log(n/2k ) = n(log n − k). So the summation is

log2 n
X n
(n log n − n · k) = n log2 n − log n(log n + 1) = Θ(n log2 n) .
2
k=0

Problem 2. Non-dominated points (35)


A two-dimensional point (x, y) is said to dominate another two-dimensional point (u, v) if x ≥ u
and y ≥ v. Given a set P of points, a point p = (x, y) is said to be a non-dominated point of P
(also called a maximal point) if no other point q in P dominates p. See Figure 1 for examples.
Given a set of n points P placed in arbitrary order in an array, give a time efficient algorithm
find non dom(P, n) to find the set of all non-dominated points in P . Notes:

1. For simplicity, assume that no two points have the same x-coordinate or the same y-coordinate.
2. A point p is represented as a structure with two attributes p.x and p.y. The set of points P ,
is represented as an array P [1, . . . , n] of points in arbitrary order.
3. You can find the index of the median of the points in P [k, . . . , l] by the x coordinate by using
an informal statement like “let i = median(P, k, l) by x-coordinate” Similarly, a statement
like “let i =median(P, k, l) by y coordinate” can be used to find the index of the median of
the points in P [k, . . . , l] by the y coordinates. These functions run in time O(k − l + 1). The
median of n points is returned as the b(n + 1)/2c th ranked item.

2
4. Full marks will be provided for a correct solution that takes O(n log n) time.

5. A correct solution of O(n2 ) time will earn only 15 points in total.

Figure 1: Dominated and non-dominated points in a point-set

Solution. The idea behind a divide and conquer solution is illustrated in Figure 2.
The top-level call is find non dom(P, 1, n). Before calling this routine, assume that P is sorted in
increasing order according to P.x. This advance sorting of P makes calls to median unnecessary.
So point 3 of the notes may be ignored.
The call to find non dom(P, k, l) returns a pair (N, r), where, N is an array of r points which
forms a non-dominating set of points in the set P [k, . . . , l] and remain sorted in increasing order
of x-coordinate. Note that this makes N to be automatically sorted in decreasing order of y-
coordinate.
The pre-sorting of the point set P ensures that as points are dropped from P , the points that
remain are sorted in order of x coordinate.

find non dom(P, k, l) // Assume P [1 . . . n] is pre-sorted by x coordinate.


1. if l==k
2. Create a new array N of size 1
3. N [1] = P [k]
4. return (N, 1)
5. let m = b(k + l)/2 // due to pre-sorting
6. (N1 , r) = find non dom(P, k, m)
7. (N2 , u) = find non dom (P, m + 1, l)
8. return find non dom merge(N1 , r, N2 , u)

The algorithm uses the procedure find non dom merge(N1 , r, N2 , s) for merging two sets of non-
dominating points, N1 with r points and N2 with s points and all points of N1 have smaller
x-coordinate than all points in N2 . This merge routine assumes the following.

3
Figure 2: The set of points is divided into two almost equal parts by the median according to x
coordinate. The set of non-dominated points in each set are computed. All the circled points,
i.e., non-dominated points of the left half, lying in the shaded region on or below p.y line, are
eliminated. The non-dominating set consists of all points from the left-half non-dominating set
above the shaded region and all the points in the non-dominating set of the second half.

1. N1 and N2 are both sorted by x-coordinate. N1 is the non-dominated point set of the first
half of points P [k . . . m] and N2 is the non-dominated set for the second half P [m + 1 . . . k].
2. N2 .x ≥ N1 .x that is, for every p ∈ N1 and every q ∈ N2 , q.x ≥ p.x.

Refer to Figure 2. All points in N1 that have y value lower than or equal to the highest y-point
in N2 (which is the first point in the sorted order by x) are dominated by the highest y point of
N2 . Conversely, each point in N1 with y value larger than the highest y value point of N2 is not
dominated by it, and therefore cannot be dominated by any other point of N2 , since they all have
smaller y values. On the other hand, no point in N2 can be dominated by a point in N1 since their
x values are larger.

find non dom merge(N1 , r, N2 , s)


1. // first find the points p in N1 with higher y-coordinate than the first point of N2 .
2. t=0
3. while t < r and N1 [t + 1].y > N2 [1].y
4. t=t+1
5. Create a new array N [1, . . . , t + s].
6. copy first t elements of N1 into the respective first t positions of N .
7. Subsequently, copy all s elements of N2 into positions t + 1, . . . t + s of N .
8. return (N, s + t)

The time complexity of find non dom merge (N1 , r, N2 , s) is O(r + s). Going back to the

4
function find non dom(P, k, l), in lines 5 and 6, the recursive calls find non dom(P, k, m) and
find non dom(P, m + 1, l) takes time T (d(l − k + 1)/2e) and T (b(l − k + 1)/2c) by recursion. The
non-dominating set from the first recursive call is N1 with r elements and so r ≤ d(l − k + 1)/2e.
Similarly, the non-dominating set from the second recursive call is N2 with s elements, where,
s ≤ b(l − k + 1)/2c. Thus, r + s ≤ (k − l + 1).
The time taken by find non dom merge (N1 , r, N2 , u) is O(r + s), which is O(l − k + 1). Thus,
letting l − k + 1 = n, we get the recurrence relation,
T (n) = T (dn/2e) + T (bn/2c) + O(n)
whose solution is T (n) = O(n log n).
Alternative Solution. This solution does not use the divide and conquer method. Let P [1, . . . , n]
be sorted in increasing order by x coordinates, with ties broken by y coordinate in increasing order.
Now scan P backwards from the point with highest x. We will maintain a variable maxy which is
the maximum y-coordinate of any point so far in the backward scan. That is, if we have currently
scanned P [k . . . n] then, maxy = maxnj=k P [j].y We maintain the loop invariant that the point
P [k − 1] is included in the non-dominating set iff P [k − 1].y > maxy The initialization is prior to
running the loop: maxy = −∞.
To see the maintenance, suppose the invariant is true for n downwards to k ≥ 2. Now consider
k − 1. Then, ym = maxnt=k P [t].y. If P [k − 1].y ≤ ym , then, P [k − 1] is dominated by at least one
point in P [k . . . n] and we discard P [k − 1]. Otherwise, P [k − 1] is included into N , which is filled
in the backwards order (increasing order of y, and decreasing order of x).

find non dom(P, n) //assumes n ≥ 1


1. sort P [1, . . . , n] in increasing order of x, ties broken in increasing order of y.
2. Create a new array N [1, . . . , n]
3. maxy = −∞
4. t = 0
5. for k = n downto 1
6. if P [k].y > maxy
7. t=t+1
8. N [t] = P [k]
9. maxy = P [k].y
10. Reverse array N [1 . . . t] to get it sorted by x-coordinate increasing.

The time required is dominated by the sorting in line 1, which requires Θ(n log n) time.

Problem 3. (35)
An important court trial is going on that has N witnesses. However, not all witnesses are honest;
a witness is either true or false. A true witness always speaks the truth, whereas a false witness
lies in at least one question in a given question set. To test the witnesses’ credibility, the judge
speaks to them in pairs (e.g., W 1 and W 2). Both are asked the same questions about the case and
the answers given by each one is presented to the other. Each of them (e.g., W 1) is now asked
whether the other person (i.e. W 2) is telling the truth or not (and vice-versa). The possibilities
are as follows:

5
W1 says W2 says Conclusion
Case 1. W2 is true W1 is true Both are true, or both are false witnesses
Case 2. W2 is true W1 is false At least one is a false witness
Case 3. W2 is false W1 is true At least one is a false witness
Case 4. W2 is false W1 is a false At least one is a false witness

a. Show that if there are more than N/2 false witnesses, the judge cannot necessarily pick out the
true witnesses, irrespective of the pairwise test strategy used. Assume that the false witnesses
can conspire to fool the judge.
Suppose the every bad witness does the following. Given a good witness’s account, it says
it is false, and given a bad witness’s account, it says it is true. Now the good witnesses will
report truthfully, and so it is impossible for the judge to distinguish between the set of good
witnesses and the set of bad witnesses (who are consistent by conspiracy).

b. Assuming that more than N/2 witnesses are true, the judge now wants to identify one true
witness. Show that bN/2c pairwise tests are sufficient to reduce this problem to that of
approximately half its size.
Solution. Arbitrarily number the witnesses from 1 to N . Consider the following algorithm.

procedure ReduceWitnesses(M, N )
// M is a list of N witnesses (array)
1. Let L = φ.
2. Arbitrarily pair the witnesses. // if N is odd, there may be one unpaired witness
3. for each pair that is of type case 1
4. choose an arbitrary witness from the pair
5. add it to L
6. if L has even length and there is an unpaired witness x // for odd N
7. add it to L
8. return (L, |L|)

The number of tests is bN/2c, as we are pairing witness 1 with 2, 3 with 4, . . ., 2i − 1 with
2i, for i = 1, 2, . . . , bN/2c. If N is odd, then the final N th witness is unpaired.
Note that the algorithm removes pairs belonging to cases 2,3 and 4, and picks one member
(arbitrarily) from case 1 pairs.
Pairs belonging to cases 2 and 3 have one good and one bad witness pairing. Since the number
of good witnesses are overall in majority, removing all such (one good, one bad) pairings still
leaves the good witnesses in majority. Pairs belonging to case 4 are both bad, and so removing
such pairs still leaves the number of good witnesses in majority (only increases the fraction of
good witnesses holding the number constant). Finally, each pair belonging to case 1 is either
both good or both bad, so choosing one witness from one pair means, for a good pair, we
choose a good witness, and from a bad pair, we choose a bad witness.
Assume N is even. After removing all case 2, case 3 and case 4 pairs, the good witnesses are
in majority. Then, choosing one witness from each case 1 pair, reduces the problem size to
N/2 or smaller and the number of good witnesses remains in majority.

6
Assume N is odd. Remove all case 2,3 and 4 pairs. There would be an unpaired witness.
Suppose L is empty, that is there are no case 1 pairs. Then the unpaired witness is good since
good witnesses are in majority. If L has odd length, then the number of good pairs are more
than the number of bad pairs (vice-versa is not possible), so good items are in majority in L.
Now suppose L has even length. Let x be the unpaired witness. In the worst case, L may
have equal number of good and bad witnesses, in which x must be good and we can add x
to L. Otherwise, L has two more good witnesses than bad, in which case, even if x is bad,
it can be added to L and still the number of good witnesses are in majority. So in this case
also, we can add x to L.

c. Show that the true witnesses can be identified with Θ(N ) pairwise tests, assuming that more
than N/2 witnesses are true. Argue and solve the recurrence that describes the number of
tests.
Recursively call the procedure ReduceWitnesses. Initally, when called on witness array of
size N with majority of good witnesses, it gives a witness array of size at most dN/2e with
the property that there is a majority of true witnesses. We can call this recursively, until the
size reduces to 1, in which case we have a single true witness. This true witness who has been
identified can then be tested against all the others in succession and its verdict tells whether
the new witness is true or false.

procedure FindOneGoodWitness(M, N )
// M is a list of N witnesses (array)
1. if N == 1 return M // M has only one good witness: end of recursion.
2. (L, l) = ReduceWitnesses(M, N )
3. return FindOneGoodWitness (L, l)

The number of witness pairings in each step of ReduceWitness(M, N ) is at most N/2. Let
T (N ) be the number of witness pairings in the procedure FindOneGoodWitness(M, N ). Then,
T (N ) ≤ T (N/2) + O(N ). The solution of this recurrence equation is T (N ) = O(N ).
Once one good witness has been identified, pair this witness against all other witnesses and
follow its verdict. It can separate all the good vs bad witnesses.

You might also like