3

I was implementing the 2D closest pair algorithm using sweepline, and it says that you need to find the six points above a certain y coordinate. What I did is I put the points in a TreeSet sorted by y-coordinate, and used the tailSet method to get all points above a certain point, and iterate up to 6 times.

I was wondering if the complexity of the tailSet operation is O(log n), and if it is, is iterating over the tailSet at most six times also O(log n)?

Reference: http://people.scs.carleton.ca/~michiel/lecturenotes/ALGGEOM/sweepclosestpair.pdf

2 Answers2

5

AFAIK Taking the tailSet is O(log n), but iterating over the the last m elements is O(m * log n)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Oh I see. Iterating 6 times would still be O (log n). Thanks for this! Now I can implement sweeplines :D Edit: I thought pressing enter would introduce a newline. – Alvin John Burgos May 22 '12 at 17:09
  • @assylias you are right depending on what operations you are talking about. In terms of the ones you care about it could be `O(m)` but in terms of navigation cost it is higher. `O(m * log n)` is an upper bound. – Peter Lawrey Dec 01 '15 at 10:07
3

Hmm... It’s strange to me. I thought that in terms of big O a process of creating a tailSet inside java.util.TreeSet is O(1).

Small clarification: tailSet(), headSet() and subSet() return very small objects that delegate all the hard work to methods of the underlying set. No new set is constructed. Hence O(1) and pretty insignificant.

a link for discussion

acridity
  • 31
  • 2