Download presentation
Presentation is loading. Please wait.
1
Quadtrees Hierarchical grids
Seminar on Geometric Approximation Algorithms Speaker : Hadar Boker
2
Lecture Outline Motivation Quadtrees construction
Fast point location in a quadtree Compressed quadtree : definition Efficient construction of compressed quadtrees Fast point location in a compressed quadtree Dynamic quadtree : Ordering of nodes and points Performing operations on a quadtree using Q-order
3
Preliminaries For a grid G, we call the width or side-length.
G partitions the plane into square regions, which we call grid cells. Every grid cell □ of G has a unique ID; let p=(x,y) be any point in □, and consider the pair of integer numbers id□=id(p)=(└x/┘,└y/┘). For a point set P and a parameter , the partition of P into subsets by the grid G is denoted by G(P). Two points p,qP belong to the same set in the partition G(P), if both points belong to the same grid cell; that is, id(p)=id(q).
4
Motivation Let Pmap be a partition of the unit square into polygons.
Preprocess Pmap for point-location queries. So that given a query point we need figure out which polygon contains the query. How can we solve this problem? One possibility : quadtrees !
5
Quadtrees Construction
□v Build a tree T, where: Every node v corresponds to a cell □v (and the root?) Each node has four children The conflict list of the square □v is a list of all the polygon of Pmap that intersect □v. Let be a constant (which denote at least the number of polygons of Pmap meeting in one common corner) The construction is recursive.
6
Back to our problem… Given a query point q, in the unit square:
How can we compute the polygon of Pmap containing q? We assume that the polygons are simple (e.g. triangles), and thus, the quadtree would have linear complexity in the input size. Can we perform a better point-location query?
7
Fast point-location: Definitions
A square is a canonical square if: It is contained inside the unit square It is a cell in a grid Gr r is a negative power of 2 It might correspond to a node in a quadtree We will refer to such a grid Gr as a canonical grid. Consider a node v of depth i, its associated square □v. Then the side-length of □v is 2-i, and it is a canonical square in the canonical grid G2-i. We will refer to l (v) = -i as the level of v.
8
Fast point-location: Definitions
Unit square □r root □w 1/8 □u v □v 1/4 u 1/2 w Level (-i) Canonical grid (G2-i) Side length (2-i) Depth (i) Node G1 20=1 root -1 G1/2 2-1=1/2 1 v -2 G1/4 2-2=1/4 2 u -3 G1/8 2-3=1/8 3 w
9
Fast point-location: Definitions
A node v of a quadtree is uniquely defined by the triple id(v)=(l (v), └x/r┘,└y/r┘( where (x,y) is any point in □v , and r=2l(v). Corollary: Given a query point q and a desired level l we can compute the ID of the quadtree cell of this level that contains q in constant time. How can this help us?
10
Fast point-location: Algorithm
binary search for the leaf QTGetNode(T,q,d): returns (in constant time) the node v of depth d in the quadtree T such that □v contains the point q.
11
Fast point-location: Example
□r r □u □z u z q QTFastPLI(T,q,l=0,h=3) m = └(0+3)/2┘=1 v := QTGetNode(T,q,1) := u (≠null) w := child(v,q) = z (≠null) QTFastPLI(T,q, l=1+1, h=3) m =└(2+3)/2┘=2 v := QTGetNode(T,q,2) := z (≠null) w := child(v,q) = null return w ( = z )
12
Fast point-location: Algorithm
Lemma: given a quadtree T of size n and of height h, one can preprocess it (using hashing) in linear time, such that one can perform a point-location query in O(log h) time. However, the height, h, may be unbounded.
13
Compressed quadtrees
14
Spread Definition: For a set P of n points in a metric space, let max p,qP ||p-q|| min p,q P, pq ||p-q|| be the spread of p. Intuitively, the spread tells us the range of distances that P possesses. Φ(P) =
15
Spread Lemma: Let P be a set of n points contained in the unit square,
such that diam(P) = max p,qP ||p-q|| ≥ ½. Let T be a quadtree of P constructed over the unit square, where no leaf contains more than one point of P. then, the depth of T is bounded by O(log Φ), it can be constructed in O(n log Φ) time, and the total size of T is O(n log Φ), where Φ=Φ(P).
16
Spread Proof: Build recursively a quadtree T for P (how?)
Diam 2l(v) Now, let us bound the depth of T: Consider p,qP , a node v with l (v)=└log ||p-q||┘- 1 diam(□v) 2l(v) □v Thus, □v can not contain both p and q. In particular, any node u of T of level r = -┌log Φ┐-2 can contain at most one point of p (i.e. u is a leaf). Thus, all the nodes of T are of depth O(log Φ).
17
How can quadtrees be compressed?
The quadtree generated by the Lemma has a lot of nodes which are of degree one. A node v has more than one child, only if it has at least two children x and y such that both □x and □y contain points of p. How can we get more compact data structure? For example: v y x
18
How can quadtrees be compressed?
Store inside each node v, its □v and its level l (v). A path of vertices, all of degree one, will be replaced by the first vertex in this path. Its only child would be the last vertex in this path. This compressed node v has a single child, region rgv that v “controls” seen as the gray area below. If v is uncompressed node the region that v is in charge of is a square rgv = □v The resulting tree is a compressed quadtree A compressed quadtree has linear size
19
Compressed quadtrees: Example
This compressed quadtree has depth and size θ(n). It looks like a linked list.
20
Bit twiddling & compressed quadtrees
Definition: Let α,β[0,1) be two real numbers. Assume these numbers in base two are written as α=0.α1α2… and β=0.β1β2… Let bit∆(α,β) be the index of the first bit after the period in which they differ. For example: bit∆(7/8=0.1112, 3/4=0.1102) = 3 Lemma: if one can compute a compressed quadtree of two points in constant time, then one can compute bit∆(α,β) in constant time.
21
Bit twiddling & compressed quadtrees
Proof: Given α and β we need to compute bit∆(α,β) If |α-β|>1/128: compute bit∆(α,β) in constant time. Otherwise, build a compressed trie for a set {α,β}: The root is a compressed node. Its only child has side-length 2-i (l(v) = -i) α and β are identical in the first i bits. They differ at the (i+1)th bit, as the next level of the quadtree splits them into different subtrees. r
22
Efficient construction of compressed quadtrees
Theorem: Given a set P of n points in the plane, one can compute a compressed quadtree of P in O(n log n) deterministic time. In our proof we will use two lemmas: Lemma 1: Given a set P of n points in Rd, and parameter k, one can compute in O(n(n/k)d) deterministic time, a ball b that contains k points of P and its radius radius(b) ≤2ropt(P,k), where ropt(P,k) is the radius of the smallest ball in Rd containing k points of P. Lemma 2: For any point set P and α>0, we have that if α≤2ropt(P,k) then any cell of grid Gα contains at most 5k points.
23
Efficient construction of compressed quadtrees
Proof (of Theorem): Compute, in linear time, a disk D of radius r, which contains at least n/10 of the points of P, such that r≤2ropt(P,n/10). (According to lemma 1, it’s possible) Consider the grid Gα where α=2└log r┘. It has a cell that contains at least (n/10)/25 points of P (since D covered by 25 grid cell of Gα and α≥r/2) No grid cell contains more than 5(n/10) points, by lemma 2. r α
24
Efficient construction of compressed quadtrees
Proof (cont.): Compute Gα(P) and find the cell □ containing the largest number of points. Let Pin be the set of points inside this cell □ and Pout be the set of points outside this cell □. we know that |Pin| ≥ n/250 and |Pout| ≥ n/2 Compute (recursively) the compressed quadtrees for Pin (i.e. Tin) and Pout (i.e. Tout).
25
Efficient construction of compressed quadtrees
Proof (cont.): Create a node in both quadtrees that corresponds to □ For Tin this would just be the root node vin (Pin⊆ □) For Tout this would be new leaf node vout (Pout ∩ □ = ∅) Hang vout on vin creating a compressed quadtree for P. The overall construction time is: T(n) = O(n) + T(|Pin|) + T(|Pout|) = O(n log n) Where did the bit∆ operation get used?
26
Fingering a compressed quadtree: definitions
Back to “Point Location query”… Traversing down the quadtree Binary search Finger tree Definition: Let T be a tree with n nodes. A separator in T is a node v, such that if we remove v from T, we remain with a forest, such that every tree in the forest has at most ┌n/2┐ vertices.
27
Fingering a compressed quadtree: definitions
Lemma: Every tree has a separator, and it can be computed in linear time. Proof: consider T to be a rooted tree with n nodes. Precompute for every node the number of nodes in its subtree (bottom-up) Set v to be lowest node in T such that its subtree has ≥ ┌n/2┐nodes in it (how?) How to build our finger tree?
28
Fingering a compressed quadtree: fast point location
Finger tree 1 3 3 2 1 4 5 4 5 2 7 6 7 6 We get a balanced tree which has cross pointers (fingers) into the original tree
29
Fingering a compressed quadtree: fast point location
2 1 Given a query point q… Traverse T’ (finger tree), while at node fvT’: If q □v, continue into the child of fv which corresponds to the connected component outside □v Otherwise continue into the child that contains q. We have to check O(1) children, constant time per node D(n) ≤ 1+ D(┌n/2┐) = O(log n) , while D:= depth(T’) Thus, Point location query in T’ takes logarithmic time. Now, let us bound the preprocessing time 3 4 5 6 7
30
Fingering a compressed quadtree: fast point location
Theorem: Given a compressed quadtree T of size n, one can preprocess it, in time O(n log n), such that given a query point, one can return the lowest node in T whose region contains q in O(log n) time. Proof: T(n) = O(n) where n1,…,nt are the sizes of the subtrees formed by removing the separator node from T. We know that t=O(1) and ni ≤ ┌n/2┐, for all i. As such, T(n) = O(n log n).
31
Dynamic quadtrees
32
Dynamic quadtrees: introduction
The idea is: define an order on the compressed quadtree nodes. store the compressed quadtree nodes in a data structure for ordered sets. How to perform basic operations: In regular quadtree: Point location Overlaying In compressed quadtrees: Insertion & Deletion
33
Ordering of nodes and points
Consider a regular quadtree T, and DFS traversal on T. Consider any two canonical squares □ and ■ in T. If □ gets visited before ■, we denote this fact by □ < ■ Consider a point p and a canonical square □Gi. If p□ then we will say that □ < p Otherwise, let ■ be the cell in Gi that contains p We have that □ < p if and only if □ < ■ Consider two points p and q which lie in different cells, in Gi: □p and □q. Then p<q if and only if □p < □q
34
Ordering of nodes and points
We will refer to the ordering induced by < as the Q-order The ordering < when restricted only to points, is know as the Z-order
35
Computing the Q-order quickly
Definition: for any two points p,q[0,1)2, let lca(p,q) denote the smallest canonical square that contains both p and q. ( The Ica of two cells is the Ica of their centers ) To compute lca(p,q) we use bit∆ operation Q-order can be resolved in constant time. q p
36
Computing the Q-order quickly
determine Q-order of two cells □ and ■ : If □ ⊆ ■ then ■ < □ If ■ ⊆ □ then □ < ■ Otherwise, let ◙ = Ica(□,■). determine which children of ◙ contain □ and ■ resolve this query using DFS
37
Performing a point location in a (regular) quadtree using Q-order
The quadtree leaf containing q is the last cell □ in the ordered list such that □ < q. Correctness: Let □q be the leaf of T whose cell contains q. By definition: □q < q The only bad scenario: □q < □ < q But this implies: □ must be contained inside □q Contradiction to our assumption that □q is a leaf. w z q u v
38
Performing overlaying two (regular) quadtrees using Q-order
Given two quadtrees T’ and T’’ we would like to overlay them to compute their combined quadtree. (= minimal quadtree such that every cell of T’ and T’’ appears in it) Observation: if the two quadtrees are given as sorted list of their cells then their overlay is just the merged list, with replication removed.
39
Performing overlaying two (regular) quadtrees using Q-order
z3 z4 w1 w2 u4 u3 w1 u3 w2 u4 z1 z2 v3 v4 u1 v1 v2 u2 u1 u2 r < u1 < u2 < v1 <…< v4 < u3 < w1< … < w4 < u4 r < u1 < u2 < u3 < w1 < w2 < z1…< z4 < w3 < w4 < u4 T2 : T1 : w3 w4 r z3 z4 u3 w1 w2 u4 z1 z2 v3 v4 v1 v2 u1 u2 r < u1 < u2 < v1 <…< v4 < u3 < w1 < w2 < z1 <…< z4 < w3 < w4 < u4
40
Performing operations on a (regular) quadtree using Q-order
Conclusions Given a quadtree T, with its leaves stored in an ordered-set data-structure D according to the Q-order. Point-location query: O(Q(n)) time, where Q(n) is the time to perform search query in D. Compute merged quadtree: linear time.
41
Performing operations on a compressed quadtree using Q-order
Note: A compressed node v, is stored according to the square □v but in fact it is in charge of the compressed region rgv This will make things more complicated.
42
Point location in a compressed quadtree using Q-order
If the query point q is contained inside a leaf of T: use simple binary search for predecessor of q. However, if q is contained inside the region of a compressed node, w, the predecessor to q might be some arbitrary leaf, v, that is contained inside w. (note: q is not stored in the quadtree) What to do in such a case?
43
Point location in a compressed quadtree using Q-order
Example 2 Example 1 w w q q v v u z s u z s Q-order: w < z < s < v Predecessor Q-order(T,q) = □v q□v Ica(□v,q) = □u PredecessorQ-order(T, □u) = □w * node u was removed (compressing) Q-order: w < z < s < v Predecessor Q-order(T,q) = □v q□v Ica(□v,q) = □w PredecessorQ-order(T, □w) = □w * node u was removed (compressing)
44
Insertions in a compressed quadtree using Q-order
q - a point to be inserted into the quadtree. w - the node such that qrgw There are 3 possibilities for insertions Case 1: The node w is a leaf and there is no point in it. store q at w.
45
Insertions in a compressed quadtree using Q-order
Case 2: The node w is a leaf, and there is a point p already stored in w. p q p q r Ica(p,q) = □w r □w □w w t w t □t □t u (p) v (q) q r p q p r Ica(p,q) □w w □w t □w □t w t z □t u (p) v (q)
46
Insertions in a compressed quadtree using Q-order
Case 3: The node w is a compressed node. q w w w Ica(□z,q) □w s u z Node of Ica (□z,q) = u t z t s z Node of q “store p”? t s
47
Deletions in a compressed quadtree using Q-order
Deletion is done in a similar fashion: Delete the point from the node that contains it Trim away nodes that are no longer necessary q w w w Delete q u s t u w v t s (q)
48
Performing operations on a compressed quadtree using Q-order
Conclusions Given a compressed quadtree T, with its leaves stored in an ordered-set data-structure D according to the Q-order. Point-location query: O(Q(n)) time, where Q(n) is the time to perform search query in D. Maintain a compressed quadtree: O(log n) time per operation (insertions, deletions and point location queries are supported).
49
Summary Quadtrees is one of the most powerful geometric data structure
One important application: point-location query In a (regular) quadtree, point-location takes O(log h) time (using binary search) A compressed quadtree has linear size In compressed quadtree, point location takes O(log n) time (using finger tree) In dynamic quadtrees we store the nodes in a data structure for ordered sets (sorted by Q-order) We can perform operations on such quadtrees (point-location, insertion, deletion etc).
50
Thank you for listening!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.