0% found this document useful (0 votes)
14 views5 pages

Ps 5 Sol

The document provides solutions to exercises from an algorithms course. It discusses solutions for maintaining order statistics and interval trees, finding collinear point triplets, and detecting intersecting disks. It also analyzes strategies for an adversarial permutation game and maintaining a running balance in a transaction database.

Uploaded by

Random Swag
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)
14 views5 pages

Ps 5 Sol

The document provides solutions to exercises from an algorithms course. It discusses solutions for maintaining order statistics and interval trees, finding collinear point triplets, and detecting intersecting disks. It also analyzes strategies for an adversarial permutation game and maintaining a running balance in a transaction database.

Uploaded by

Random Swag
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/ 5

Introduction to Algorithms November 1, 2002

Massachusetts Institute of Technology 6.046J/18.410J


Professors Erik Demaine and Shafi Goldwasser Handout 20

Problem Set 5 Solutions


(Exercises were not to be turned in, but we’re providing the solutions for your own interest.)

Exercise 5-1. First, we find the rank r of x using OS-R ANK . Then we query the (r + i)th order
statistic, which is x’s ith successor. Both of these operations require O (log n) time.

Exercise 5-2. Yes, the black-heights can be maintained. If x is a black node, then its black height
is one more than its children’s black heights; if x is red, then its black height is its children’s black
heights. By Theorem 14.1, this information can be maintained without changing the O (log n)
asymptotic running times of the tree operations.

Exercise 5-3. We modify the comparison operator to break left-endpoint ties by right endpoint.
That is, i1 < i2 if and only if min[i1 ℄ < min[i2 ℄ or (min[i1 ℄ = min[i2 ℄ and max[i1 ℄ < max[i2 ℄).
This ordering is consistent with what is assumed by the interval tree operations, so their running
times and correctness remain. I NTERVAL -S EARCH -E XACTLY (T ; i) works in the following way:
if the left endpoint of the root is less than min[i℄, we go right; if greater, we go left. If equal, do the
following: if the right endpoint of the root is less than max[i℄, we go right; if greater, we go left. If
equal, return the root.
Correctness follows from the following observations: any interval identical to i must have the
same left endpoint, so the first comparison eliminates intervals whose left endpoints cannot pos-
sibly match. Furthermore, because left-endpoint ties are broken by right-endpoints, the second
comparison eliminates intervals whose left endpoints match i’s, but whose right endpoints cannot
possibly match i’s.
The running time is O (log n) because the algorithm does O (1) work at each node it visits, and
descends one level each time.

Exercise 5-4. For each point p in the set, sort all the other points by polar angle relative to p
(exercise 33.1-3). Then test each pair of adjacent elements in the sorted list for collinearity with
p, using the cross product (the points are collinear if the cross product is 0). This algorithm is

correct because if three points a; b; are collinear and b is between a and , then sorting around
a will find this triplet. Conversely, the algorithm only reports that three points are collinear when

it conclusively determines this fact. Sorting requires O (n log n) time, and it is performed O (n)
times, for a total running time of O (n2 log n).

Exercise 5-5. First, note that we can detect in O (1) time if two disks, of radii r1 and r2 , intersect:
they do if and only if the squared distance between their centers is at most (r1 + r2 )2 . This test can
be performed using only multiplications and additions.
Our algorithm is very similar to A NY-S EGMENTS -I NTERSECT : for each disk, we have 2 event
points: one for the leftmost point of the disk, and one for its rightmost point. When encountering
2 Handout 20: Problem Set 5 Solutions

a leftmost point in the scan, we the y -coordinate of the center of the disk to the tree (with a pointer
to the disk itself as auxiliary data). The rest of the algorithm remains the same.
The O (n log n) running time is obvious. The proof of correctness exactly mirrors the one in the
book.

Problem 5-1. Omniscient adversaries.

(a) The adversarial strategy is the following: find a longest uninterrupted chain. Call its
final element x, and the element immediately following the chain z . Insert an element
y such that x < y < z . There are two cases: if y is not duplicated in any higher

level, then the new structure now has a longest uninterrupted chain that is one element
longer than in the old one. (This occurs with probability 1/2.) If y is duplicated in a
higher level, then the original chain is still uninterrupted, so the longest uninterrupted
chain in the new structure is always no shorter than it was in the old one.
(b) We prove that after employing the above strategy, the length of the longest uninter-
rupted chain is n=10, with probability 1 2 n for some constant > 0. This qualifies
as high probability, because the error probability is exponentially decaying with n.
Consider the event that the length of the longest uninterrupted chain is less than n=10.
This means that out of n coin flips, more than 9n=10 of them came up “heads.” Simi-
larly to the analysis from class, we compute this probability to be at most:
! !
9 10 (1 2) = 10 (1 2)
n 9n=10 n 9n=10
= =
n= n=

 (10 )n= (1 2) n=
e
10
=
9 10

= 2 n e = n=
( lg(10 )) 10 9 10

 2 n= n=
3 10 9 10

= 2 n= 6 10

(c) If there is an uninterrupted chain of length (n), then for any element near the middle
of the chain, the running time of S EARCH is (n). Likewise, inserting any element
near the middle of the chain requires (n) time. A red-black tree, by contrast, al-
ways has O (log n)-time S EARCH and I NSERT operations, regardless of whether the
adversary knows everything about the layout of the data structure. (In fact, because
red-black trees are deterministic, an adversary can in fact know the exact layout of the
tree at any point, simply by keeping an identical copy for itself.)
Handout 20: Problem Set 5 Solutions 3

Problem 5-2. Survivor: MIT

(a) We put the elements 1; : : : ; n in a circular, doubly-linked list (i.e., each element has
a pointer to both its successor and its predecessor, and the list “wraps around” from
n to 1). Then we walk around the list, removing every mth element and appending it

to a list containing the Survivor permutation. We repeat until the entire circular list is
empty.
To create the doubly-linked list requires O (n) time. To remove an element x from a
doubly-linked list requires O (1) time, because we need only modify the predecessor
pointer of x’s successor, and the successor pointer of x’s predecessor. Appending to
the end of the Survivor permutation list can also be done in O (1) time. For each person
kicked out, we traverse m pointers and do O (1) pointer updates. Since n total people
are kicked out, the running time is O (mn). Correctness of the algorithm is apparent
from the description of the problem.
(b) We maintain a dynamic order statistics red-black tree, described in the textbook and
in lecture. First we insert every element 1; : : : ; n, and first remove element m. After
removing person x, the next person to be removed is (essentially) the mth successor of
x. However, we must take into account “wrap-around,” in case x doesn’t have so many

true successors. We can compute the “mth wrap-around successor” of x, immediately


after removing x, as follows:
1. find the rank r of the predecessor of x,
2. find the number of elements k still in the tree,
3. find the element having rank (r + m) mod k (or rank k , if (r + m)  0 mod k );
this is the mth wrap-around successor.
We now analyze the running time of this procedure. Constructing the initial tree re-
quires O (n log n) time. Deleting each element requires O (log n) time. Step 1 requires
O (log n) time (see CLRS). Step 2 requires O (1) time, simply by examining the aug-

mented data at the root of the tree. Step 3 also requires O (log n) time. This loop is
performed n times, for a total running time of O (n log n).

Problem 5-3. Accounting


Our data structure will be a red-black tree, ordered by the dates of the transactions, where each node
x is augmented by a field delta, which is the total change (increase) in the account balance over

all nodes in x’s subtree. This information is merely the sum of all values of nodes in x’s subtree
(including x). It is clear that this information can maintained without affecting the running times of
the red-black tree operations (by Theorem 14.1 from CLRS): delta[x℄ = sum[x℄ + delta[left[x℄℄ +
delta[right[x℄℄, and delta[nil℄ = 0.
From this description, I NITIALIZE is trivial: simply construct an initially-nil red-black tree. Like-
wise, I NS T RANS and D ELT RANS are straightforward by Theorem 14.1: simply use the red-black
tree I NSERT and D ELETE procedures, along with extra code to maintain delta according to the
equations above.
4 Handout 20: Problem Set 5 Solutions

BALANCE (date) is the only non-trivial operation. It is not enough to merely sum the delta values
of all elements preceding date, because there may be up to n of them. Instead, we consider the
path (v1 ; : : : ; vk ) from date to the root, where v1 is the node for date and vk is the root. Initially
set an accumulator variable to delta[left[v1 ℄℄. Then follow the path: whenever vi 1 = right[vi ℄ we
add sum[vi ℄ and delta[left[vi ℄℄ to the accumulator. The balance is the value of the accumulator at
the end of the path.
The running time of this is O (log n), because the tree is balanced (so the path is short). To see
correctness, we need to show that the value of every transaction that occurs before date is counted
exactly once. Consider any date y < date, and look at its path (w1 ; : : : ; wm ) to the root (where
w1 is the node for y , and wm is the root). Then this path intersects the path from date to the root

at some node, and is identical to it from that point on. The from the root downward, we have
wm = vk ; wm 1 = vk 1 ; : : : ; wm (k i) = vk (k i) = vi , and vk (k i+1) 6= vi 1 (where the paths

diverge or one path ends).


There are now two cases: if m (k i) = 1, then the node for y (which is vi ) is on the path
P
from date to the root. Because y < date, it must be that vi 1 is the right child of vi , so in fact
the algorithm adds vi to the accumulator. If m (k i) > 1, then the node for y must be
in the left subtree of vi (otherwise we would have y > date[vi ℄  date because the path to date
diverges or ends at vi ). Then date is at vi or is in the right subtree of vi . If it is at vi , then vi = v1
and the accumulator initially is set to the value of delta[left[v1 ℄℄, which includes (exactly once) the
value of the transaction on date y . If date is in the right subtree of vi , then vi 1 = right[vi ℄, so
delta[left[vi ℄℄ is added to the accumulator by the algorithm, and includes (exactly once) the value
of y ’s transaction. This completes the proof of correctness.

Problem 5-4. Counting chord intersections

(a) Suppose p1 = (x1 ; y1 ) and p2 = (x2 ; y2 ). Because p1 and p2 are on a circle, to sort
points by polar angle one simply sorts the points having non-negative y -coordinates by
their x-coordinates in decreasing order, then the points having negative y -coordinates
by their x-coordinates in increasing order. Therefore p1 < p2 if and only if: y1 > 0
and y2  0, or y1 ; y2  0 and x1 > x2 , or y1 ; y2 < 0 and x1 < x2 . This method of
comparison is nice because it doesn’t use any multiplications or additions.
Alternatively, we can use the cross-product: p1 < p2 if the path from the origin to p1
to p2 “turns left” (and does not cross from below the x-axis to above). That is, p1 < p2
if p2  p1 is negative, and NOT (y1 < 0 and y2  0).
(b) We take the 2n endpoints of the chords, where each point also has a pointer to the other
endpoint of its chord. We then sort the points in O (n log n) time using the comparison
operator from above. We then make one pass over the sorted points, with j initially set
to 1: at each point, if it is unlabelled, then label it Aj , label its corresponding endpoint
Bj (by following the pointer), and increment j . If it is already labelled, do nothing.

It is clear from the description of the algorithm that in a sweep around the circle,
Aj always precedes Aj +1 . In addition, each chord has a unique j such that Aj and
Handout 20: Problem Set 5 Solutions 5

Bj are its endpoints, because once a point is labelled its label never changes, and
corresponding endpoints are always labelled with the same j . Finally, Aj precedes Bj
because for a chord labelled with (Aj ; Bj ), the first endpoint encountered is labelled
Aj .

The running time of this algorithm is O (n log n) for the sorting, and O (n) for the
sweep.
(c) We sort and label the endpoints as described above. Then we initialize to zero and
make a sweep over the sorted points, doing the following at each point: if the point
is labelled Aj , add j to a dynamic order-statistics tree. If the point is labelled Bj ,
increment by the size of the tree minus the rank of j in the tree, then remove j from
the tree. After the loop, return as the number of intersections.
For the proof of correctness, we take the following convention: we assign each in-
tersection between two chords to the first chord encountered in a counterclockwise
sweep. Now we argue correctness of the algorithm: consider a particular chord j
having endpoints (Aj ; Bj ). The number of other chords which intersect this chord
(according to our convention) is the number of Ak s which appear after Aj and before
Bj , without a corresponding Bk in that range. By our labelling, all such Ak s will have

k > j . At any point in the algorithm, the tree contains all k such that Ak has been

encountered and Bk has not. Therefore the number of chords crossing chord j is the
number of values k such that k > j and k is in the tree; this is the size of the tree
minus the rank of j in the tree. Because is the sum of all these numbers, it is the total
number of chord pairs which intersect.
For the running time, note that sorting takes O (n log n) time. In addition, insertion,
deletion, and order statistic queries require O (log n) time apiece, and a constant num-
ber of such operations are performed for each of the 2n endpoints. Therefore the total
running time is O (n log n).

You might also like