Consider n chords on a circle, each defined by its endpoints. Describe an O(n lg n)-time algorithm to determine the number of pairs of chords that intersect inside the circle. (For example, if the n chords are all diameters that meet at the center, then the correct answer is
. ). Assume that no two chords share an endpoint.
Solution
VerifiedWe imagine that we are given points on the circle, labeled in clockwise order of appearance in the circle, where the starting point can be chosen arbitrarily, i.e. north pole of the circle.
Furthermore, we are given the set of chords determined by the endpoints from . It is assumed that no two chords share an endpoint. We can assume that we have a mapping that produces for each point on the circle its counterpart that makes a chord in constant time. E.g. if , that means that 2nd and 6th point encountered on the tour around the circle, starting from the north pole and going clockwise, make a chord. Of course, in this case as well.
Now produce the array as follows. First point on the circle (again - starting from the north pole and going clockwise), denoted with 1, is given a , . Its chord-generating-counterpart, point , is given the same key. Proceed to the next keyless point, which can be either 3 or 2, depending on whether or not, and give it the next key . Give the same key to its chord-generating counterpart and continue.
For example, array could begin like this:
Notice that chords are , , , .
Most importantly, you might notice that
Create an account to view solutions
Create an account to view solutions
Recommended textbook solutions

Computer Organization and Design MIPS Edition: The Hardware/Software Interface
5th EditionDavid A. Patterson, John L. Hennessy

Introduction to Algorithms
3rd EditionCharles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen