Top Banner
CSE373, Winter 2020 L11: Quadtrees Quadtrees CSE 373 Winter 2020 Instructor: Hannah C. Tang Teaching Assistants: Aaron Johnston Ethan Knutson Nathan Lipiarski Amanda Park Farrell Fileas Sam Long Anish Velagapudi Howard Xiao Yifan Bai Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan
28

Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

May 17, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

QuadtreesCSE 373 Winter 2020

Instructor: Hannah C. Tang

Teaching Assistants:

Aaron Johnston Ethan Knutson Nathan Lipiarski

Amanda Park Farrell Fileas Sam Long

Anish Velagapudi Howard Xiao Yifan Bai

Brian Chan Jade Watkins Yuma Tou

Elena Spasova Lea Quan

Page 2: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

Announcements

❖ Homework 4: Heap is released and due Wednesday

▪ Hint: you will need an additional data structure to improve the runtime for changePriority(). It does not affect the correctness of your PQ at all. Please use a built-in Java collection instead of implementing your own.

▪ Hint: If you implemented a unittest that tested the exact thing the autograder described, you could run the autograder’s test in the debugger (and also not have to use your tokens).

❖ Please look at posted QuickCheck; we had a few corrections!

2

Page 3: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

Lecture Outline

❖ Heaps, cont.: Floyd’s buildHeap

❖ Review: Set/Map data structures and logarithmic runtimes

❖ Multi-dimensional Data

❖ Uniform and Recursive Partitioning

❖ Quadtrees

3

Page 4: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

Other Priority Queue Operations

❖ The two “primary” PQ operations are:

▪ removeMax()

▪ add()

❖ However, because PQs are used in so many algorithms there are three common-but-nonstandard operations:

▪ merge(): merge two PQs into a single PQ

▪ buildHeap(): reorder the elements of an array so that its contents can be interpreted as a valid binary heap

▪ changePriority(): change the priority of an item already in the heap

4

Page 5: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

buildHeap: Naïve Implementation

❖ buildHeap() takes an array of size N and applies the heap-ordering principle to it

❖ Naïve implementation:

▪ Start with an empty array (representing an empty binary heap)

▪ Call add() N times

▪ Runtime: ??

❖ Can we do better?

5

Page 6: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

buildHeap: Clever Implementation

❖ ~½ of all nodes in a complete binary tree are leaves

▪ Remember that 20 + 21 + … 2n

= 2n+1 – 1

❖ Clever implementation:

▪ Start with full array (representing a binary heap with lots of violations)

▪ Call percolateDown() N/2 times

▪ Runtime: ??

6

20: 1

21: 2

22: 4

23: 8

This “clever implementation” is called Floyd’s Algorithm

Page 7: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

pollev.com/uwcse373

❖ What is buildHeap()’s runtime?

▪ Start with full array (representing a binary heap with lots of violations)

▪ Call percolateDown() N/2 times

A. Θ(1)

B. Θ(log N)

C. Θ(N)

D. Θ(N log N)

E. I’m not sure …

7

20: 1

21: 2

22: 4

23: 8

Page 8: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

Lecture Outline

❖ Heaps, cont.: Floyd’s buildHeap

❖ Review: Set/Map data structures and logarithmic runtimes

❖ Multi-dimensional Data

❖ Uniform and Recursive Partitioning

❖ Quad-Trees

8

Page 9: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

ADT / Data Structure Taxonomy

❖ Search Trees (“left is less-than, right is greater-than”)

▪ Binary Search Trees (branching factor == 2)

• Plain BST (unbalanced)

– Balanced BSTs: LLRB (other examples: “Classic” Red-Black, AVL, Splay, etc)

▪ B-Trees (have a branching factor >2; balanced)

• 2-3 Trees

• 2-3-4 Trees

❖ Hash Tables (will cover later!)

9

Maps and Sets

AD

TD

ata

Stru

ctu

res

that

Imp

lem

ent

Page 10: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

Why Does Balance Matter?

❖ Balanced trees help us avoid considering all of the data all of the time

▪ Binary Search Tree: Discarding approximately half of the remaining data at each recursive step leads to a logarithmic runtime

▪ Binary Heap: Recursively percolating up one level approximately halves the number of potential positions to consider, again leading to a logarithmic runtime

10

9

5 17

8 311

6 5

7

2 1 3 4

Page 11: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

Lecture Outline

❖ Heaps, cont.: Floyd’s buildHeap

❖ Review: Set/Map data structures and logarithmic runtimes

❖ Multi-dimensional Data

❖ Uniform and Recursive Partitioning

❖ Quad-Trees

11

Page 12: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

Autocomplete as a 1-Dimensional Range Search

❖ Location names can be sorted 1-dimensionally (lexicographically aka dictionary ordering)

❖ Since the data is sorted, we could run two binary searches on the array

▪ Range Search Runtime: ??

▪ Insert Runtime: ??

12

Sanaa SantiagoSao

PauloSeattle Sendai Seoul

Page 13: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

Autocomplete as a 1-Dimensional Range Search

❖ Or we could do a range search in a balanced BST

▪ Range Search Runtime: ??

▪ Insert Runtime: ??

13

Seattle

Santiago Sendai

Sao Paulo

SeoulSanaa

void printRange(Node root, Key lo, Key hi) {

if (root == null) return;

if (lo < root->key)

printRange(root->left, lo, hi);

if (lo <= root->key && root->key >= hi)

print(root->key);

if (root->key > hi)

printRange(root->right, lo, hi);

}

Page 14: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

Geo-locating a Click on a 2D Map

❖ Why do some map clicks resolve to a lat/lng?

❖ And other clicks resolve to a point-of-interest?

14

Page 15: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

2-d Range Search: Naïve Implementation

❖ Check every point for containment in the click target

❖ Range Search

▪ Scan through all the keys and collect matchingresults

▪ Runtime: ??

❖ Nearest Neighbour

▪ Range Search, hope for an non-empty result, iterate through results and choose nearest

▪ Runtime: ??

❖ Insert

▪ Put key anywhere

▪ Runtime: ??15

A(-1, -1)

(2, 2)

B(0, 1)C

D(1, 0)

E

(-2, -2)

F

(-3, 2.5)

Page 16: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

Lecture Outline

❖ Heaps, cont.: Floyd’s buildHeap

❖ Review: Set/Map data structures and logarithmic runtimes

❖ Multi-dimensional Data

❖ Uniform and Recursive Partitioning

❖ Quad-Trees

16

Page 17: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

Uniform Partitioning

❖ Divide space into non-overlapping subspaces

▪ Known as “spatial partitioning problem”

❖ Uniform partitioning strategy

▪ Partition space into uniform rectangular buckets (“bins”)

▪ Ex: 4x4 grid of such buckets.

17

A(-1, -1)

(2, 2)

B(0, 1)

C

D(1, 0)

E

(-2, -2)

F

(-3, 2.5)

Page 18: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

pollev.com/uwcse373

What is the runtime to find thenearest neighbour to our blue point, assuming N points are evenly spread out across a 16-bin uniform partition?

A. Θ(1)

B. Θ(log N)

C. Θ(N)

D. Θ(N2)

E. I’m not sure …

18

Page 19: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

Recursive Partitioning: An x-coordinate BST?

❖ Suppose we put points into a BST map ordered by x-coordinate.

19

A(-1, -1)

(2, 2)

B(0, 1)C

D(1, 0)

E

(-2, -2)

F

(-3, 2.5)

A (-1, -1)

B (2, 2)

C (0, 1)

D (1, 0)

E (-2, -2)

F (-3, 2.5)

Page 20: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

Recursive Partitioning: An x-coordinate BST?

❖ Range Searching becomes:“What are all the points with x-coordinate less than -1.5?”

20

A(-1, -1)

(2, 2)

B(0, 1)C

D(1, 0)

E

(-2, -2)

F

(-3, 2.5)

A (-1, -1)

B (2, 2)

C (0, 1)

D (1, 0)

E (-2, -2)

F (-3, 2.5)

Pruned

Page 21: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

Recursive Partitioning: A y-coordinate BST?

But in a y-coordinate BST, we can’t prune anything!

21

A (-1, -1)

B (2, 2)

C (0, 1)

D (1, 0)

E (-2, -2)

F (-3, 2.5)

A(-1, -1)

(2, 2)

B(0, 1)C

D(1, 0)

E

(-2, -2)

F

(-3, 2.5)

Page 22: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

Lecture Outline

❖ Heaps, cont.: Floyd’s buildHeap

❖ Review: Set/Map data structures and logarithmic runtimes

❖ Multi-dimensional Data

❖ Uniform and Recursive Partitioning

❖ Quad-Trees

22

Page 23: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

Recursive Partitioning: Quadtree

❖ 2-dimensional data

▪ Keys are located on a plane

▪ Recursive decision: northwest, northeast, southwest, southeast.

❖ 1-dimensional data

▪ Keys are ordered on a line

▪ Recursive decision: left or right

23

A

Anortheast

southeast

left rightnorthwest

southwest

Binary Search Tree Quadtree

Page 24: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

Using Quadtrees to Recursively Partition

❖ Quadtrees produce recursive, hierarchical partitionings

▪ Each point owns 4 subspaces

24

A

B

C

D

E

Uniform Partitioning

A

B

D

E

Recursive Partition (quadtree)

C

Page 25: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

Quadtree: Insert

25

ANW

NE SESW

B

C

SW

SE

D

E

A(-1, -1)

(2, 2)

B(0, 1)

C

D

(1, 0)

E

(-2, -2)

Demo: https://docs.google.com/presentation/d/1vqAJkvUxSh-Eq4iIJZevjpY29nagNTjx-4N3HpDi0UQ/present?ueb=true&slide=id.g11ecaeaf56_0_0

Page 26: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

Quadtree: Range Search

We can prune unnecessary subspaces!

26

ANW

NE SESW

B

C

SW

SE

D

E

A(-1, -1)

(2, 2)

B(0, 1)

C

D

(1, 0)

E

(-2, -2)

Demo: https://docs.google.com/presentation/d/1ZVvh_Q15Lh2D1_NnzZ4PR_aDsLBwvAU9JYQAwlSuXSM/present?ueb=true&slide=id.g52a9824549_0_129

Page 27: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

3-dimensional Data and Beyond

❖ Oct-trees are generalization of quadtrees for 3D data

❖ Quadtree Applications: https://www.ics.uci.edu/~eppstein/gina/quadtree.html

27

Octree (WhiteTimberwolf/Wikimedia)

Page 28: Quadtrees - courses.cs.washington.edu · Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan. L11: Quadtrees CSE373, Winter 2020 Announcements ...

CSE373, Winter 2020L11: Quadtrees

tl;dr

❖ A Priority Queue’s core functionality is removeMax and add

▪ changePriority can be Θ(log N) if you use an auxiliary data structure

▪ buildHeap can be Θ(N) if you percolate carefully

❖ Recursively subdividing input:

▪ allows you to find one piece data without examining all of it

▪ often yields logarithmic runtime

❖ Quadtrees allow you to recursively partition 2-dimensional data using a single 4-way question

28

Range SearchNearest

NeighbourAdd

Θ(log N) Θ(log N) Θ(log N)