Algorithms for GIS

154
Algorithms for GIS: Quadtrees

Transcript of Algorithms for GIS

Page 1: Algorithms for GIS

Algorithms for GIS:

Quadtrees

Page 2: Algorithms for GIS

Quadtree

• A data structure that corresponds to a hierarchical subdivision of the plane • Start with a square (containing inside input data)

• Divide into 4 equal squares (quadrants) • Continue subdividing each quadrant recursively • Subdivide a square until it satisfies a stopping condition, usually that a

quadrant is “small” enough • for e.g. contains at most 1 point

Page 3: Algorithms for GIS
Page 4: Algorithms for GIS
Page 5: Algorithms for GIS
Page 6: Algorithms for GIS
Page 7: Algorithms for GIS
Page 8: Algorithms for GIS
Page 9: Algorithms for GIS
Page 10: Algorithms for GIS
Page 11: Algorithms for GIS

Quadtrees

Page 12: Algorithms for GIS

Quadtrees

• Conceptually simple data structure

Page 13: Algorithms for GIS

Quadtrees

• Conceptually simple data structure• Generalizes to d dimensions

Page 14: Algorithms for GIS

Quadtrees

• Conceptually simple data structure• Generalizes to d dimensions

• d=3: octree

Page 15: Algorithms for GIS

Quadtrees

• Conceptually simple data structure• Generalizes to d dimensions

• d=3: octree

Page 16: Algorithms for GIS

Quadtrees

• Conceptually simple data structure• Generalizes to d dimensions

• d=3: octree• Can be built for many types of data

Page 17: Algorithms for GIS

Quadtrees

• Conceptually simple data structure• Generalizes to d dimensions

• d=3: octree• Can be built for many types of data

• points, edges, polygons, images, etc

Page 18: Algorithms for GIS

Quadtrees

• Conceptually simple data structure• Generalizes to d dimensions

• d=3: octree• Can be built for many types of data

• points, edges, polygons, images, etc • Can be used for many different tasks

Page 19: Algorithms for GIS

Quadtrees

• Conceptually simple data structure• Generalizes to d dimensions

• d=3: octree• Can be built for many types of data

• points, edges, polygons, images, etc • Can be used for many different tasks

• search, point location, neighbors, joins, unions, etc

Page 20: Algorithms for GIS

Quadtrees

• Conceptually simple data structure• Generalizes to d dimensions

• d=3: octree• Can be built for many types of data

• points, edges, polygons, images, etc • Can be used for many different tasks

• search, point location, neighbors, joins, unions, etc • dynamic

Page 21: Algorithms for GIS

Quadtrees

• Conceptually simple data structure• Generalizes to d dimensions

• d=3: octree• Can be built for many types of data

• points, edges, polygons, images, etc • Can be used for many different tasks

• search, point location, neighbors, joins, unions, etc • dynamic

• Theoretical bounds not great, but widely used in practice

Page 22: Algorithms for GIS

Quadtrees

• Conceptually simple data structure• Generalizes to d dimensions

• d=3: octree• Can be built for many types of data

• points, edges, polygons, images, etc • Can be used for many different tasks

• search, point location, neighbors, joins, unions, etc • dynamic

• Theoretical bounds not great, but widely used in practice • LOTS of applications

Page 23: Algorithms for GIS

Quadtrees

• Conceptually simple data structure• Generalizes to d dimensions

• d=3: octree• Can be built for many types of data

• points, edges, polygons, images, etc • Can be used for many different tasks

• search, point location, neighbors, joins, unions, etc • dynamic

• Theoretical bounds not great, but widely used in practice • LOTS of applications

• Many variants of quadtrees have been proposed

Page 24: Algorithms for GIS

Quadtrees

• Conceptually simple data structure• Generalizes to d dimensions

• d=3: octree• Can be built for many types of data

• points, edges, polygons, images, etc • Can be used for many different tasks

• search, point location, neighbors, joins, unions, etc • dynamic

• Theoretical bounds not great, but widely used in practice • LOTS of applications

• Many variants of quadtrees have been proposed• Hundreds of papers

Page 25: Algorithms for GIS
Page 26: Algorithms for GIS
Page 27: Algorithms for GIS
Page 28: Algorithms for GIS
Page 29: Algorithms for GIS
Page 30: Algorithms for GIS
Page 31: Algorithms for GIS
Page 32: Algorithms for GIS

Outline

• Point quadtrees

• Extensions and applications

Illustrate the core properties of quadtrees

Page 33: Algorithms for GIS

Point-quadtree

Questions:

1. Size? Height?

2. How to build it and how fast?

3. What can we do with it?

Let P = set of n points in the plane

Problem: Store P in a quadtree such that every square has <= 1 point.

Page 34: Algorithms for GIS

Let P = set of n points in the plane

Page 35: Algorithms for GIS

Let P = set of n points in the plane

Page 36: Algorithms for GIS

Let P = set of n points in the plane

Page 37: Algorithms for GIS

Let P = set of n points in the plane

Page 38: Algorithms for GIS

Let P = set of n points in the plane

Page 39: Algorithms for GIS

Let P = set of n points in the plane

Page 40: Algorithms for GIS

Let P = set of n points in the plane

Page 41: Algorithms for GIS

Let P = set of n points in the plane

Page 42: Algorithms for GIS

Let P = set of n points in the plane

Quadtree: tree corresponding to the subdivision

Page 43: Algorithms for GIS

Let P = set of n points in the plane

Quadtree: tree corresponding to the subdivision

Page 44: Algorithms for GIS

Let P = set of n points in the plane

NW SW NE SE

Quadtree: tree corresponding to the subdivision

Page 45: Algorithms for GIS

Let P = set of n points in the plane

NW SW NE SE

Quadtree: tree corresponding to the subdivision

Page 46: Algorithms for GIS

Let P = set of n points in the plane

NW SW NE SE

Quadtree: tree corresponding to the subdivision

Page 47: Algorithms for GIS

Let P = set of n points in the plane

NW SW NE SE

Quadtree: tree corresponding to the subdivision

Page 48: Algorithms for GIS

Let P = set of n points in the plane

NW SW NE SE

Quadtree: tree corresponding to the subdivision

Page 49: Algorithms for GIS

Let P = set of n points in the plane

NW SW NE SE

Quadtree: tree corresponding to the subdivision

Page 50: Algorithms for GIS

Let P = set of n points in the plane

NW SW NE SE

Quadtree: tree corresponding to the subdivision

Page 51: Algorithms for GIS

Let P = set of n points in the plane

NW SW NE SE

Quadtree: tree corresponding to the subdivision

Page 52: Algorithms for GIS

Let P = set of n points in the plane

NW SW NE SE

Quadtree: tree corresponding to the subdivision

Page 53: Algorithms for GIS

Let P = set of n points in the plane

NW SW NE SE

Quadtree: tree corresponding to the subdivision

Page 54: Algorithms for GIS

Let P = set of n points in the plane

NW SW NE SE

…..

Quadtree: tree corresponding to the subdivision

Page 55: Algorithms for GIS

Exercises

• Pick n=10 points in the plane and draw their quadtree.

• Show a set of (10) points that have a balanced quadtree.

• Show a set of (10) points that have an unbalanced quadtree.

• Draw the quadtree corresponding to a regular grid • how many nodes does it have? • how many leaves? height?

• Consider a set of points with a uniform distribution. What can you say about the quadtree ?

• Let’s look at sets of 2 points in the plane. • Sketch the smallest possible quad tree for two points in the plane. • Sketch the largest possible quad tree for two points in the plane. • An upper bound for the height of a quadtree for 2 points ????

Let P = set of n points in the plane

Page 56: Algorithms for GIS

Quadtree size P = set of n points in the plane

Page 57: Algorithms for GIS

Quadtree size P = set of n points in the plane

Theorem:

Page 58: Algorithms for GIS

Quadtree size P = set of n points in the plane

Theorem:

The height of a quadtree storing P is at most lg (s/d) + 3/2 , where s is the side of the original square and d is the distance between the closest pair of points in P.

Page 59: Algorithms for GIS

Quadtree size

Proof: • Each level divides the side of the quadrant into two. After i levels, the side of the quadrant is s/2i

• A quadrant will be split as long as the two closest points will fit inside it. • In the worst case the closest points will fit diagonally in a quadrant and the “last” split will

happen at depth i such that s sqrt(2)/2i = d… • The height of the tree is i+1

P = set of n points in the plane

Theorem:

The height of a quadtree storing P is at most lg (s/d) + 3/2 , where s is the side of the original square and d is the distance between the closest pair of points in P.

Page 60: Algorithms for GIS

Quadtree size

Proof: • Each level divides the side of the quadrant into two. After i levels, the side of the quadrant is s/2i

• A quadrant will be split as long as the two closest points will fit inside it. • In the worst case the closest points will fit diagonally in a quadrant and the “last” split will

happen at depth i such that s sqrt(2)/2i = d… • The height of the tree is i+1

P = set of n points in the plane

Theorem:

The height of a quadtree storing P is at most lg (s/d) + 3/2 , where s is the side of the original square and d is the distance between the closest pair of points in P.

• What does this mean? • The distance between points can be arbitrarily small, so the height of a

quadtree can be arbitrarily large in the worst case

Page 61: Algorithms for GIS

Building a quadtree Let P = set of n points in the plane

Page 62: Algorithms for GIS

Building a quadtree

• Let’s come up with a (recursive) algorithm to build quadtree of P

Let P = set of n points in the plane

Page 63: Algorithms for GIS

Building a quadtree

• Let’s come up with a (recursive) algorithm to build quadtree of P

//create quadtree of P and return its root

Let P = set of n points in the plane

Page 64: Algorithms for GIS

Building a quadtree

• Let’s come up with a (recursive) algorithm to build quadtree of P

//create quadtree of P and return its root

buildQuadtree(set of points P, square S)

Let P = set of n points in the plane

Page 65: Algorithms for GIS

Building a quadtree Let P = set of n points in the plane

Page 66: Algorithms for GIS

Building a quadtree

//create quadtree of P and return its root

Let P = set of n points in the plane

Page 67: Algorithms for GIS

Building a quadtree

//create quadtree of P and return its root buildQuadtree(set of points P, square S)

Let P = set of n points in the plane

Page 68: Algorithms for GIS

Building a quadtree

//create quadtree of P and return its root buildQuadtree(set of points P, square S)

• if P has at most one point:

Let P = set of n points in the plane

Page 69: Algorithms for GIS

Building a quadtree

//create quadtree of P and return its root buildQuadtree(set of points P, square S)

• if P has at most one point: • build a leaf node , store P in it, and return

node

Let P = set of n points in the plane

Page 70: Algorithms for GIS

Building a quadtree

//create quadtree of P and return its root buildQuadtree(set of points P, square S)

• if P has at most one point: • build a leaf node , store P in it, and return

node • else

Let P = set of n points in the plane

Page 71: Algorithms for GIS

Building a quadtree

//create quadtree of P and return its root buildQuadtree(set of points P, square S)

• if P has at most one point: • build a leaf node , store P in it, and return

node • else

• partition S into 4 quadrants S1, S2, S3, S4 and use them to partition P into P1, P2, P3, P4

Let P = set of n points in the plane

Page 72: Algorithms for GIS

Building a quadtree

//create quadtree of P and return its root buildQuadtree(set of points P, square S)

• if P has at most one point: • build a leaf node , store P in it, and return

node • else

• partition S into 4 quadrants S1, S2, S3, S4 and use them to partition P into P1, P2, P3, P4

• create a node

Let P = set of n points in the plane

Page 73: Algorithms for GIS

Building a quadtree

//create quadtree of P and return its root buildQuadtree(set of points P, square S)

• if P has at most one point: • build a leaf node , store P in it, and return

node • else

• partition S into 4 quadrants S1, S2, S3, S4 and use them to partition P into P1, P2, P3, P4

• create a node • node ->child1 = buildQuadtree(P1, S1)

Let P = set of n points in the plane

Page 74: Algorithms for GIS

Building a quadtree

//create quadtree of P and return its root buildQuadtree(set of points P, square S)

• if P has at most one point: • build a leaf node , store P in it, and return

node • else

• partition S into 4 quadrants S1, S2, S3, S4 and use them to partition P into P1, P2, P3, P4

• create a node • node ->child1 = buildQuadtree(P1, S1)• node ->child2 = buildQuadtree(P2, S2)

Let P = set of n points in the plane

Page 75: Algorithms for GIS

Building a quadtree

//create quadtree of P and return its root buildQuadtree(set of points P, square S)

• if P has at most one point: • build a leaf node , store P in it, and return

node • else

• partition S into 4 quadrants S1, S2, S3, S4 and use them to partition P into P1, P2, P3, P4

• create a node • node ->child1 = buildQuadtree(P1, S1)• node ->child2 = buildQuadtree(P2, S2)• node ->child3 = buildQuadtree(P3, S3)

Let P = set of n points in the plane

Page 76: Algorithms for GIS

Building a quadtree

//create quadtree of P and return its root buildQuadtree(set of points P, square S)

• if P has at most one point: • build a leaf node , store P in it, and return

node • else

• partition S into 4 quadrants S1, S2, S3, S4 and use them to partition P into P1, P2, P3, P4

• create a node • node ->child1 = buildQuadtree(P1, S1)• node ->child2 = buildQuadtree(P2, S2)• node ->child3 = buildQuadtree(P3, S3)• node ->child4 = buildQuadtree(P4, S4)

Let P = set of n points in the plane

Page 77: Algorithms for GIS

Building a quadtree

//create quadtree of P and return its root buildQuadtree(set of points P, square S)

• if P has at most one point: • build a leaf node , store P in it, and return

node • else

• partition S into 4 quadrants S1, S2, S3, S4 and use them to partition P into P1, P2, P3, P4

• create a node • node ->child1 = buildQuadtree(P1, S1)• node ->child2 = buildQuadtree(P2, S2)• node ->child3 = buildQuadtree(P3, S3)• node ->child4 = buildQuadtree(P4, S4)• return node

Let P = set of n points in the plane

Page 78: Algorithms for GIS

Building a quadtree

//create quadtree of P and return its root buildQuadtree(set of points P, square S)

• if P has at most one point: • build a leaf node , store P in it, and return

node • else

• partition S into 4 quadrants S1, S2, S3, S4 and use them to partition P into P1, P2, P3, P4

• create a node • node ->child1 = buildQuadtree(P1, S1) • node ->child2 = buildQuadtree(P2, S2) • node ->child3 = buildQuadtree(P3, S3) • node ->child4 = buildQuadtree(P4, S4) • return node

Let P = set of n points in the plane

How long does this take, function of n and height h?

Page 79: Algorithms for GIS

• Total time = total time in partitioning + total time in recursion

Building a quadtree

Page 80: Algorithms for GIS

P1 P2 P3 P4

Ppartition P into P1, P2, P3 P4 takes O(|P|) = O(n)

Let P = set of n points in the plane Partitioning

partition P1, P2, P3 P4 into their quadrants takes O(|P1|) + O(|P2|) + O(|P3|) + O(|P4|) = O(|P|) = O(n)

A quadtree for P of height h

Page 81: Algorithms for GIS

• Partitioning P into P1, P2, P3, P4 runs in time O(|P|) • We cannot bound P1, P2, P3, P4 (each can have anywhere between 0 points

and n points) • But if we look at all nodes at same level in the quadtree: together they form a

partition of the input square and the union of their points is P

==> The time to partition, at every level, is O(n)

==> Summed over the entire quadtree partition will take O(h x n) in total

Partitioning Let P = set of n points in the plane

Page 82: Algorithms for GIS

Building a quadtree

• Recursion • Every recursive call creates a node • How many nodes?

P1 P2 P3 P4

P

Let P = set of n points in the plane

A quadtree for P of height h

Page 83: Algorithms for GIS

Building a quadtree

P1 P2 P3 P4

P

How many nodes?

A quadtree for P of height h

Page 84: Algorithms for GIS

Building a quadtree

• nodes (N) = internal nodes (I)+ leaves (L)

P1 P2 P3 P4

P

How many nodes?

A quadtree for P of height h

Page 85: Algorithms for GIS

Building a quadtree

• nodes (N) = internal nodes (I)+ leaves (L)

P1 P2 P3 P4

P

How many nodes?

A quadtree for P of height h

Page 86: Algorithms for GIS

Building a quadtree

• nodes (N) = internal nodes (I)+ leaves (L)

• Each node has 0 or 4 children

P1 P2 P3 P4

P

How many nodes?

A quadtree for P of height h

Page 87: Algorithms for GIS

Building a quadtree

• nodes (N) = internal nodes (I)+ leaves (L)

• Each node has 0 or 4 children

P1 P2 P3 P4

P

How many nodes?

A quadtree for P of height h

Page 88: Algorithms for GIS

Building a quadtree

• nodes (N) = internal nodes (I)+ leaves (L)

• Each node has 0 or 4 children

• A relation between I and L?

P1 P2 P3 P4

P

How many nodes?

A quadtree for P of height h

Page 89: Algorithms for GIS

Building a quadtree

• nodes (N) = internal nodes (I)+ leaves (L)

• Each node has 0 or 4 children

• A relation between I and L?

L = 3 I + 1

(Proof: by induction)…

P1 P2 P3 P4

P

A quadtree for P of height h

How many nodes?

Page 90: Algorithms for GIS

Building a quadtree

P1 P2 P3 P4

P

A quadtree for P of height h

How many nodes?

Page 91: Algorithms for GIS

Building a quadtree

• nodes (N) = internal nodes (I)+ leaves (L)

P1 P2 P3 P4

P

A quadtree for P of height h

How many nodes?

Page 92: Algorithms for GIS

Building a quadtree

• nodes (N) = internal nodes (I)+ leaves (L)• L = 3 I + 1 —> N = I + 3I + 1 = 4I + 1

P1 P2 P3 P4

P

A quadtree for P of height h

How many nodes?

Page 93: Algorithms for GIS

Building a quadtree

• nodes (N) = internal nodes (I)+ leaves (L)• L = 3 I + 1 —> N = I + 3I + 1 = 4I + 1 • How many internal nodes?

P1 P2 P3 P4

P

A quadtree for P of height h

How many nodes?

Page 94: Algorithms for GIS

Building a quadtree

• nodes (N) = internal nodes (I)+ leaves (L)• L = 3 I + 1 —> N = I + 3I + 1 = 4I + 1 • How many internal nodes?

• can be unbounded

P1 P2 P3 P4

P

A quadtree for P of height h

How many nodes?

Page 95: Algorithms for GIS

Building a quadtree

• nodes (N) = internal nodes (I)+ leaves (L)• L = 3 I + 1 —> N = I + 3I + 1 = 4I + 1 • How many internal nodes?

• can be unbounded• want to express function of h

P1 P2 P3 P4

P

A quadtree for P of height h

How many nodes?

Page 96: Algorithms for GIS

Building a quadtree

• nodes (N) = internal nodes (I)+ leaves (L)• L = 3 I + 1 —> N = I + 3I + 1 = 4I + 1 • How many internal nodes?

• can be unbounded• want to express function of h

P1 P2 P3 P4

P

A quadtree for P of height h

How many nodes?

Page 97: Algorithms for GIS

Building a quadtree

• nodes (N) = internal nodes (I)+ leaves (L)• L = 3 I + 1 —> N = I + 3I + 1 = 4I + 1 • How many internal nodes?

• can be unbounded• want to express function of h

• in the best case each leaf contains one point (no empty leaves); in the worst case there can be many empty leaves ==> many internal nodes

P1 P2 P3 P4

P

A quadtree for P of height h

How many nodes?

Page 98: Algorithms for GIS

Building a quadtree

• nodes (N) = internal nodes (I)+ leaves (L)• L = 3 I + 1 —> N = I + 3I + 1 = 4I + 1 • How many internal nodes?

• can be unbounded• want to express function of h

• in the best case each leaf contains one point (no empty leaves); in the worst case there can be many empty leaves ==> many internal nodes

P1 P2 P3 P4

P

A quadtree for P of height h

How many nodes?

Page 99: Algorithms for GIS

Building a quadtree

• nodes (N) = internal nodes (I)+ leaves (L)• L = 3 I + 1 —> N = I + 3I + 1 = 4I + 1 • How many internal nodes?

• can be unbounded• want to express function of h

• in the best case each leaf contains one point (no empty leaves); in the worst case there can be many empty leaves ==> many internal nodes

• at each level, the internal nodes partition the original square and each internal node contains at least 2 points ==> O(n) internal nodes per level —> O( n x h)

P1 P2 P3 P4

P

A quadtree for P of height h

How many nodes?

Page 100: Algorithms for GIS

Building a quadtree

• nodes (N) = internal nodes (I)+ leaves (L)• L = 3 I + 1 —> N = I + 3I + 1 = 4I + 1 • How many internal nodes?

• can be unbounded• want to express function of h

• in the best case each leaf contains one point (no empty leaves); in the worst case there can be many empty leaves ==> many internal nodes

• at each level, the internal nodes partition the original square and each internal node contains at least 2 points ==> O(n) internal nodes per level —> O( n x h)

P1 P2 P3 P4

P

A quadtree for P of height h

How many nodes?

O( n x h) nodes

Page 101: Algorithms for GIS

Summary

Theorem: A quadtree for a set P of points in the plane:

• has height h = O(lg (1/d)) (where d is closest distance) • has O(h x n) nodes; and • can be built in O(h x n) time.

Page 102: Algorithms for GIS

Summary

Theorem: A quadtree for a set P of points in the plane:

• has height h = O(lg (1/d)) (where d is closest distance) • has O(h x n) nodes; and • can be built in O(h x n) time.

• Theoretical worst case:

Page 103: Algorithms for GIS

Summary

Theorem: A quadtree for a set P of points in the plane:

• has height h = O(lg (1/d)) (where d is closest distance) • has O(h x n) nodes; and • can be built in O(h x n) time.

• Theoretical worst case: • height and size are unbounded

Page 104: Algorithms for GIS

Summary

Theorem: A quadtree for a set P of points in the plane:

• has height h = O(lg (1/d)) (where d is closest distance) • has O(h x n) nodes; and • can be built in O(h x n) time.

• Theoretical worst case: • height and size are unbounded

• In practice:

Page 105: Algorithms for GIS

Summary

Theorem: A quadtree for a set P of points in the plane:

• has height h = O(lg (1/d)) (where d is closest distance) • has O(h x n) nodes; and • can be built in O(h x n) time.

• Theoretical worst case: • height and size are unbounded

• In practice: • often h=O(n) ==> size = O(n2), build time is O(n2)

Page 106: Algorithms for GIS

Summary

Theorem: A quadtree for a set P of points in the plane:

• has height h = O(lg (1/d)) (where d is closest distance) • has O(h x n) nodes; and • can be built in O(h x n) time.

• Theoretical worst case: • height and size are unbounded

• In practice: • often h=O(n) ==> size = O(n2), build time is O(n2) • For sets of points that are uniformly distributed, quadtrees have height

h = O(lg n), size O(n) and can be built in O( n lg n) time.

Page 107: Algorithms for GIS

Compressed (point) quadtrees

Page 108: Algorithms for GIS

Exercise Let P = set of n points in the plane

• Draw a quadtree of arbitrarily large size corresponding to a small set of points in the plane (pick n=2 or n=3).

• How many leaves are empty / non-empty? • Why is the size of the quadtree super-linear?

• Compress the quadtree as follows: • compress paths of nodes with 3 empty children into one node • this node is called a donut • a node may have 5 children, an empty donut + 4 regular quadrants

Page 109: Algorithms for GIS

Compressed quadtrees Let P = set of n points in the plane

A compressed quadtree is a regular quadtree where paths of nodes with 3 empty children are compressed into one node (called: donut)

• a node may have 5 children, an empty donut + 4 regular quadrants

Page 110: Algorithms for GIS

Compressed quadtrees Let P = set of n points in the plane

A compressed quadtree is a regular quadtree where paths of nodes with 3 empty children are compressed into one node (called: donut)

• a node may have 5 children, an empty donut + 4 regular quadrants

Page 111: Algorithms for GIS

Compressed quadtrees Let P = set of n points in the plane

A compressed quadtree is a regular quadtree where paths of nodes with 3 empty children are compressed into one node (called: donut)

• a node may have 5 children, an empty donut + 4 regular quadrants

• What does this mean in terms of size?

Page 112: Algorithms for GIS

Compressed quadtrees Let P = set of n points in the plane

A compressed quadtree is a regular quadtree where paths of nodes with 3 empty children are compressed into one node (called: donut)

• a node may have 5 children, an empty donut + 4 regular quadrants

• What does this mean in terms of size?

Theorem: A compressed quadtree has O(n) nodes and h=O(n) height.

Page 113: Algorithms for GIS

Compressed quadtrees Let P = set of n points in the plane

A compressed quadtree is a regular quadtree where paths of nodes with 3 empty children are compressed into one node (called: donut)

• a node may have 5 children, an empty donut + 4 regular quadrants

• What does this mean in terms of size?

Theorem: A compressed quadtree has O(n) nodes and h=O(n) height.

• Can you argue why..?

Page 114: Algorithms for GIS

Applications of quadtrees

Page 115: Algorithms for GIS

Applications of quadtrees

• Hundreds of papers • Specialized quadtrees

• customized for specific types of data (images, edges, polygons) • customized for specific applications • customized for large data

• Used to answer queries on spatial data such as: • point location • nearest neighbor (NN) • k-NNs • range searching • find all segments intersecting a given segment • meshing • …

Page 116: Algorithms for GIS

Example: Neighbor finding

Given a node v and a direction (N, S, E, W) find a node v’ such that region(v’) is adjacent to region(v) in the given direction.

• two regions (squares) are adjacent iff they share an edge

Page 117: Algorithms for GIS

NORTH_Neighbor=?

Example: Neighbor finding

Given a node v and a direction (N, S, E, W) find a node v’ such that region(v’) is adjacent to region(v) in the given direction.

• two regions (squares) are adjacent iff they share an edge

Page 118: Algorithms for GIS

NORTH_Neighbor=

Example: Neighbor finding

Given a node v and a direction (N, S, E, W) find a node v’ such that region(v’) is adjacent to region(v) in the given direction.

• two regions (squares) are adjacent iff they share an edge

Page 119: Algorithms for GIS

NORTH_Neighbor=?

Example: Neighbor finding

Given a node v and a direction (N, S, E, W) find a node v’ such that region(v’) is adjacent to region(v) in the given direction.

• two regions (squares) are adjacent iff they share an edge

Page 120: Algorithms for GIS

NORTH_Neighbor=?

Example: Neighbor finding

Given a node v and a direction (N, S, E, W) find a node v’ such that region(v’) is adjacent to region(v) in the given direction.

• two regions (squares) are adjacent iff they share an edge

Page 121: Algorithms for GIS

NORTH_Neighbor=?

Example: Neighbor finding

Given a node v and a direction (N, S, E, W) find a node v’ such that region(v’) is adjacent to region(v) in the given direction.

• two regions (squares) are adjacent iff they share an edge

Page 122: Algorithms for GIS

NORTH_Neighbor=

Example: Neighbor finding

Given a node v and a direction (N, S, E, W) find a node v’ such that region(v’) is adjacent to region(v) in the given direction.

• two regions (squares) are adjacent iff they share an edge

Page 123: Algorithms for GIS

NORTH_Neighbor=

Example: Neighbor finding

Given a node v and a direction (N, S, E, W) find a node v’ such that region(v’) is adjacent to region(v) in the given direction.

• two regions (squares) are adjacent iff they share an edge

neighbor of the parent

Page 124: Algorithms for GIS

NORTH_Neighbor=?

Example: Neighbor finding

Given a node v and a direction (N, S, E, W) find a node v’ such that region(v’) is adjacent to region(v) in the given direction.

• two regions (squares) are adjacent iff they share an edge

Page 125: Algorithms for GIS

NORTH_Neighbor=NULL

Example: Neighbor finding

Given a node v and a direction (N, S, E, W) find a node v’ such that region(v’) is adjacent to region(v) in the given direction.

• two regions (squares) are adjacent iff they share an edge

Page 126: Algorithms for GIS

NORTH_Neighbor=?

Example: Neighbor finding

Given a node v and a direction (N, S, E, W) find a node v’ such that region(v’) is adjacent to region(v) in the given direction.

• two regions (squares) are adjacent iff they share an edge

Page 127: Algorithms for GIS

NORTH_Neighbor=

Example: Neighbor finding

Given a node v and a direction (N, S, E, W) find a node v’ such that region(v’) is adjacent to region(v) in the given direction.

• two regions (squares) are adjacent iff they share an edge

Page 128: Algorithms for GIS

NORTH_Neighbor=?

Example: Neighbor finding

Given a node v and a direction (N, S, E, W) find a node v’ such that region(v’) is adjacent to region(v) in the given direction.

• two regions (squares) are adjacent iff they share an edge

Page 129: Algorithms for GIS

NORTH_Neighbor=?

Example: Neighbor finding

Given a node v and a direction (N, S, E, W) find a node v’ such that region(v’) is adjacent to region(v) in the given direction.

• two regions (squares) are adjacent iff they share an edge

Page 130: Algorithms for GIS

NORTH_Neighbor=?

Example: Neighbor finding

Given a node v and a direction (N, S, E, W) find a node v’ such that region(v’) is adjacent to region(v) in the given direction.

• two regions (squares) are adjacent iff they share an edge

• try to find a node v’ at the same depth as v

• if not possible, find the deepest

Page 131: Algorithms for GIS

• try to find a node v’ at the same depth as v • if not possible, find the deepest

NW SW NE SE

…..NORTH_Neighbor=?

Visualizing it on the tree..

Page 132: Algorithms for GIS

• try to find a node v’ at the same depth as v • if not possible, find the deepest

NW SW NE SE

…..NORTH_Neighbor=?

Visualizing it on the tree..

Page 133: Algorithms for GIS

• try to find a node v’ at the same depth as v • if not possible, find the deepest

NW SW NE SE

…..NORTH_Neighbor=?

Visualizing it on the tree..

Page 134: Algorithms for GIS

• try to find a node v’ at the same depth as v • if not possible, find the deepest

NW SW NE SE

…..NORTH_Neighbor=?

Visualizing it on the tree..

Page 135: Algorithms for GIS

• try to find a node v’ at the same depth as v • if not possible, find the deepest

NW SW NE SE

…..NORTH_Neighbor=?

Visualizing it on the tree..

Page 136: Algorithms for GIS

• try to find a node v’ at the same depth as v • if not possible, find the deepest

NW SW NE SE

…..NORTH_Neighbor=?

Visualizing it on the tree..

Page 137: Algorithms for GIS

• try to find a node v’ at the same depth as v • if not possible, find the deepest

NW SW NE SE

…..NORTH_Neighbor=?

Visualizing it on the tree..

Is the North_neighbor always a sibling or an uncle?

Page 138: Algorithms for GIS

NW SW NE SE

….. …..

Visualizing it on the tree..

• try to find a node v’ at the same depth as v • if not possible, find the deepest Could be a nephew/niece, but we prefer the sibling..

Page 139: Algorithms for GIS

• try to find a node v’ at the same depth as v • if not possible, find the deepest

NW SW NE SE

…..NORTH_Neighbor=?

Visualizing it on the tree..

Come up with an example where the search for a North_neighbor is a great-uncle

Page 140: Algorithms for GIS

Come up with an example where the North_neighbor is a

• great-uncle.

• great-great-uncle • …

Example: Neighbor finding

Page 141: Algorithms for GIS

NW SW NE SE

Page 142: Algorithms for GIS

NW SW NE SE

Page 143: Algorithms for GIS

NW SW NE SE

Page 144: Algorithms for GIS

//input: a node v in a quadtree

//output: the deepest node v’ whose depth is at most the depth of v such that region(v’) is a north-neighbor of region(v), and NULL if there is no such node

North_Neighbor(v) • if v==root: … • if v==SW-child of parent(v):… • if v==SE-child of parent(v): …

//if we reached here, v must be NW or NE child • x <—- North_Neighbor(parent(v))

• if x is NULL or a leaf: • .…

• else: • …..

Example: Neighbor finding

Page 145: Algorithms for GIS

//input: a node v in a quadtree

//output: the deepest node v’ whose depth is at most the depth of v such that region(v’) is a north-neighbor of region(v), and NULL if there is no such node

North_Neighbor(v) • if v==root: return NULL • if v==SW-child of parent(v): return NW-child of parent(v) • if v==SE-child of parent(v): return NE-child of parent(v)

//if we reached here, v must be NW or NE child • x <—- North_Neighbor(parent(v))

• if x is NULL or a leaf: return x • else:

• if v ==NW-child of parent(v): return SW-child(x) • else: return SE-child(x)

Example: Neighbor finding

Page 146: Algorithms for GIS

//input: a node v in a quadtree

//output: the deepest node v’ whose depth is at most the depth of v such that region(v’) is a north-neighbor of region(v), and NULL if there is no such node

North_Neighbor(v) • if v==root: return NULL • if v==SW-child of parent(v): return NW-child of parent(v) • if v==SE-child of parent(v): return NE-child of parent(v)

//if we reached here, v must be NW or NE child • x <—- North_Neighbor(parent(v))

• if x is NULL or a leaf: return x • else:

• if v ==NW-child of parent(v): return SW-child(x) • else: return SE-child(x)

Example: Neighbor finding

give an example that would trigger several recursive calls

Page 147: Algorithms for GIS

More applications

• Used to answer queries on spatial data such as: • point location • nearest neighbor (NN) • k-NNs • range searching • find all segments intersecting a given segment • meshing • …

How would you do these?

Page 148: Algorithms for GIS

NN=?

Page 149: Algorithms for GIS

NN=?

Page 150: Algorithms for GIS

find all points in this range

Page 151: Algorithms for GIS

find all points in this range

Page 152: Algorithms for GIS

Applications

• Image analysis/compression

Page 153: Algorithms for GIS

Applications

• Used for fast rendering (LOD) • Level i in the qdt —> scene at a certain resolution • bottom level has full resolution • render scene at a resolution dependent on its distance from the viewpoint

Page 154: Algorithms for GIS