0% found this document useful (0 votes)
17 views2 pages

Tut2 Sol

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)
17 views2 pages

Tut2 Sol

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/ 2

Algorithm Design and Analysis

CSE222 Winter ’20


Tutorial 2

Problem 1 Prove Master’s Theorem. (See the solution here)

Problem 2
a) Suppose you are given two sets {p1 , p2 , . . . , pn } and {q1 , q2 , . . . , qn } of n points on the
unit circle. Connect each point pi to the corresponding point qi . Describe and analyze
a divide-and-conquer algorithm to determine how many pairs of these line segments
intersect in O(n log2 n) time.

b) Bonus Question. Describe an algorithm for part (b) that runs in O(n log n) time.

Figure 1: Ten intersecting pairs of segments with endpoints on a circle.

a) Cut the circle at one point. Now if we traverse in any one direction there will a relative
ordering of the points. Now if we see each (pi , qi ) to be one interval, then the problem sim-
ply reduces to finding the number of overlapping intervals. For example, in the figure if
we cut the circle at say p4 , then the ordering will be {p4 , q4 , p2 , q1 , p6 , p7 , q3 , q5 , q2 , p5 , p1 , q6 , p3 , q7 }
and now if we number them according to their relative ordering, then we will get inter-
vals like I = {(1, 2), (3, 9), (4, 11), (5, 12), (6, 14), (7, 13), (8, 10)}.
Now suppose we have n such intervals and we divide them into two equal subsets of
intervals say A and B. We know the number of overlapping intervals in each of these
subsets and the start and end times are sorted individually. For some A[i].end find the
largest start time in B which is < A[i].end. Since B is sorted according to the start
times, we can add the number of intervals whose start time is < A[i].end.
Now we just need to remove the number of intervals whose end time is also < A[i].end

1
because such intervals are entirely contained inside some other intervals(in this case in-
side A[i]). For this find the largest end time in B which is < A[i].end.
The remaining merging step is exactly same as that of merge sort.
Since for each A[i], 2 binary searches are required, therefore running time T (n) =
2T ( n2 ) + O(n log n) = O(n log2 n).

b) Note that we are using divide and conquer only while sorting (and binary search). So,
let us sort the start and end times first. Now the step for counting the number of
overlaps will be exactly same. Let us consider a variable count for counting the number
of overlaps. Let us say that the interval array is I (sorted according to start time). For
some I[i] find the interval I[j] which has the largest start time < I[i].end. Add j − i to
count. Now find the interval which has the largest end time < I[i].end. Let us say the
position of this end time in the sorted end time array is k. Subtract k − i from count
(if k − i > 0).
Running time is dominated by sorting. Therefore T (n) = O(n log n)

Problem 3 Run the DP for SubsetSum on the following instance (2, 1, 3, 4, 6, 5; B = 15).
Show the table explicitly for each of the entries.

You might also like