Quadtrees Hierarchical grids

Download Report

Transcript Quadtrees Hierarchical grids

Quadtrees
Hierarchical grids
Seminar on Geometric Approximation Algorithms
Speaker : Hadar Boker
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
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,qP 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).
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 !
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.
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?
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.
Fast point-location: Definitions
Unit □
square r
root
□w
1/8
□u
v
□v
1/4
u
1/2
w
Node
Depth (i)
Side
length (2-i)
Canonical
grid (G2-i)
Level (-i)
root
0
20=1
G1
0
v
1
2-1=1/2
G1/2
-1
u
2
2-2=1/4
G1/4
-2
w
3
2-3=1/8
G1/8
-3
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?
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.
Fast point-location: Example
□r
□u
r
□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 )
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.
Compressed quadtrees
Spread
 Definition: For a set P of n points in a metric space, let
max p,qP ||p-q||
Φ(P) =
min p,q P, pq ||p-q||
be the spread of p.
 Intuitively, the spread tells us the range of distances
that P possesses.
Spread
 Lemma:
Let P be a set of n points contained in the unit square,
such that diam(P) = max p,qP ||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).
Spread
 Proof:
Build recursively a quadtree T for P (how?)
2l(v)
2
Now, let us bound the depth of T:
Consider p,qP , a node v with l (v)=└log ||p-q||┘- 1
diam(□v)  2  2l (v)  2 || p  q || / 2  || p  q ||
l(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 Φ).
□v
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
v
points of p.
y
x
 How can we get more compact data structure?
For example:
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
Compressed quadtrees: Example
 This compressed quadtree has depth and size θ(n).
 It looks like a linked list.
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.
Bit twiddling & compressed quadtrees
 Proof:
Given α and β we need to compute bit∆(α,β)
r

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.
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.
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)

r
No grid cell contains more than
5(n/10) points, by lemma 2.
α
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).
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?
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.
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?
Fingering a compressed quadtree:
fast point location
Compressed quadtree
Finger tree
3
1
3
2
1
4
4
5
5
2
6
6
7
 We get a balanced tree which has cross pointers
(fingers) into the original tree
7
Fingering a compressed quadtree:
fast point location
 Given a query point q…
2
Traverse T’ (finger tree), while at node fvT’: 4 3
6 5
If q □v, continue into the child of fv which
7
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
1
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) +  T (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).
t
i 1
i
Dynamic quadtrees
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:
 Point location
 Insertion & Deletion
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
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
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 )
q
 To compute lca(p,q) we use bit∆ operation
 Q-order can be resolved in constant time.
p
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
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:
w
u
z
v
q

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.
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.
Performing overlaying two
(regular) quadtrees using Q-order
w3 w4
z3 z4
u3 w1 z1 z w2
2
u1
T1 :
r
r
w3 w4
u3 w1 w2
u4
u1
u2
r < u1 < u2 < u3 < w1 < w2
< z1…< z4 < w3 < w4 < u4
T2 :
w3
u3
u1
w4
u4
v3
v4
v1
v2 u2
r < u1 < u2 < v1 <…< v4 < u3
< w1< … < w4 < u4
r
z3 z4
w1 z z w2
1
2
u4
v3
v4
v1
v2
u2
r < u1 < u2 < v1 <…< v4 < u3 < w1 < w2 < z1 <…< z4 < w3 < w4 < u4
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.
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.
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?
Point location in a
compressed quadtree using Q-order
Example 2
Example 1
w
q
w
q
v
u
z
v
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
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)
* node u was removed (compressing)
Insertions in a compressed quadtree
using Q-order
 q - a point to be inserted into the quadtree.
w - the node such that qrgw
 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.
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
Ica(p,q) = □w
r
□w
□t
w
p
□w
□t
Ica(p,q)  □w
r
□w
□t
w
t
u
v
(p)
(q)
r
q
p
t
w
t
q
p
r
q
w
□w
t
z
□t
u
v
(p)
(q)
Insertions in a compressed quadtree
using Q-order
 Case 3: The node w is a compressed node.
q
w
w
w
u
z
Ica(□z,q)  □w
s
t
z
t
Node of Ica (□z,q) = u
s
z
t
Node of q
s
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
u
s
t
Delete q
w
u
v
t
s
(q)
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).
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).
Thank you for listening!