3

I've already searched for this information. Found this:

https://groups.google.com/forum/#!topic/uw.cs.cs240/MGfrsvKAiMA

and this:

How can worst case complexity of quad tree O(N)?

, but I don't believe it answers my problem. The first one, perhaps, but I don't understand the explanation. Up to the point.

I've got a quad tree over a discrete space (2d square table of entities). It is a region tree, as explained in english Wikipedia page (https://en.wikipedia.org/wiki/Quadtree#Types). Each region can hold only one entity. Each entity has its discrete coordinates.

I have implemented a method to find all entities in certain (discrete) AABB, working exactly as the queryRange() function in the aforementioned Wiki page.

My question is: what is the time complexity for this queryRange() function?

I've tried figuring it out myself, but it seems to depend on many various factors, such as: depth of the tree, number of elements in the tree, size of given AABB. I think in its core it is related to number of sub-trees visited by queryRange() recursion.

Also I'd be thankful for any credible sources on that. I'm writing a master thesis and I need citations. I really can't seem to google anything good on this topic though.

Community
  • 1
  • 1
Sushi271
  • 544
  • 2
  • 8
  • 22
  • Would a dependence on “leafs/nodes intersecting the AABB” be good enough for you? If I'm not mistaken, it should be easy to show that the complexity is $$\mathcal O(l) = \mathcal O(n)$$ where $$n$$ is the number of nodes and $$l$$ the number of leaves in the range. – Hermann Döppes Sep 13 '16 at 19:43
  • 1
    The bible of quad trees is Hannan Samet: Foundations of Multidimensional and Metric data Structures. You should find the desired info there. – AlexWien Sep 13 '16 at 20:20

1 Answers1

3

The time complexity of queries depends on many aspects:

  1. The data distribution
  2. The order of insertion (unless you rebalance the tree)
  3. Whether you store points or rectangles (points are much more common as far as I can tell)
  4. The type of queries you are performing

I'm no expert on complexity calculation, so there may be some way how average complexity can be calculated, but it may not be useful to a given real scenario.

Maximum complexity are a bit easier. Lets say n is the number of entries and h the heights of the tree.

A large query will obviously return all elements, so the complexity is O(n). A very narrow query will have to traverse from root to exactly one leaf, hence O(h). This gives combined O(n+h).

There are some border cases: If the data is, for example, shaped such that all points are on one line (0,1),(0,2),(0,3),(0,4),... and they are inserted in order, then the tree may degenerate to a list such that h=n, unless you perform some rebalancing.

If you are interested in quadtree variants, you may want to look at the recent PH-Tree. The PDF contains some complexity calculations. The PH-Tree is a region quadtree (decomposition into quadrants) based on bit-level-trie decomposition. The maximum depth is therefore equal to the number of bits in the values, usually 32 or 64. The structure is independent of the insertion order, so there is no rebalancing (nor is it required, because depth is limited). Some technical updates can be found here. (Disclaimer: This is based on my own research when I was at the University).

TilmannZ
  • 1,784
  • 11
  • 18
  • Thanks for answer. Unfortunately I have no time to implement another tree, I have to stick with what I have (I'm in final stages of my MSc thesis and deadline is very close). I don't think any rebalancing can be done in my quad-tree, as it always divides by 4 **equally**. Thus, no matter the order, final outcome depends on positions of points/entities (I do store points). I would have to move points to change tree's structure. As for O(n) in wide query, if I understand correctly, only leaves matter even if our tree is let's say 3 levels deep: then it's O(n + n/4 + n/16) = O(n) still - right? – Sushi271 Sep 16 '16 at 11:05
  • 1
    1) O(n + n/4 + n/8) = O(n), that's correct. 2) Sourcecode of the PH-Tree is available under Apache License, so you won't necessarily need to reimplement it if Java is okay. – TilmannZ Sep 16 '16 at 12:10
  • 1
    3) I was assuming that the total extent of data is not known when you insert the first element. If you don't know it, there may be lots of points outside your rootnode, i.e. you have to create nodes above the root, this can lead to very imbalanced trees. I think if you know the extent of the data at tree creation time, then unbalance is much less of an issue, with clustered data, still you may be able to reduce the depth of the tree by shifting the root node around a bit. – TilmannZ Sep 16 '16 at 12:10
  • 2) Java's not ok, project is written in C# with Unity engine. Never mind that. 3) Yup, I probably wasn't clear enough. Rootnode is constant size, square & even size is a power of two. Depth isn't really much of an issue though, cause this quad-tree was made for an RTS map - the common sizes: 64, 128, 256 give depth 6, 7, 8 respectively. It's really a negligible number (though _h_ still must be taken into consideration in O notation). I think all is clear now :) – Sushi271 Sep 16 '16 at 23:49