32
Nearest-neighbor search using Kd-trees

Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Nearest-neighbor search using Kd-trees

Page 2: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Kd-tree

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10ℓ9

Page 3: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

The nearest-neighbor problem

• Given a set of points 𝑃 and a query point 𝑞, return the point 𝑝 ∈ 𝑃closest to 𝑞

• Often, we would like to preprocess 𝑃 into a data structure that efficiently answers such queries

Page 4: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Nearest-neighbor search using a Kd-tree

• Search the tree recursively to find the point (leaf) in the same cell as the query point 𝑞

• This is our candidate for the nearest neighbor of 𝑞

• On the way up search each subtree where a closer point than the current candidate might be found

Page 5: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Nearest-neighbor search using Kd-tree

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

ℓ9

Page 6: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Step 1: find the cell of 𝑞

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

ℓ9

Page 7: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Step 1: find the cell of 𝑞

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

ℓ9

Page 8: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Step 1: find the cell of 𝑞

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

ℓ9

Page 9: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Step 1: find the cell of 𝑞

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

ℓ9

Page 10: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Current candidate: 𝑝10ℓ1

ℓ7ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

ℓ9

Page 11: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Step 2

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

Current candidate: 𝑝10

ℓ9

Page 12: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Step 2

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

Current candidate: 𝑝10

ℓ9

Page 13: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Step 2

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

Current candidate: 𝑝10

ℓ9

Page 14: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Step 2

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

Current candidate: 𝑝10

ℓ9

Page 15: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Step 2

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

Current candidate: 𝑝10

ℓ9

Page 16: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Step 2

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

Current candidate: 𝑝10

ℓ9

Page 17: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Step 2

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

Current candidate: 𝑝10

ℓ9

Page 18: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Step 2

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

Current candidate: 𝑝10

ℓ9

Page 19: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Updating the candidate to 𝑝7ℓ1

ℓ7ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

Current candidate: 𝑝7

ℓ9

Page 20: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Updating the candidate to 𝑝7ℓ1

ℓ7ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

Current candidate: 𝑝7

ℓ9

Page 21: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

Current candidate: 𝑝7

ℓ9

Page 22: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

Current candidate: 𝑝7

ℓ9

Page 23: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

Current candidate: 𝑝7

ℓ9

Page 24: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Updating the candidate to 𝑝8ℓ1

ℓ7ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

Current candidate: 𝑝8

ℓ9

Page 25: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Updating the candidate to 𝑝8ℓ1

ℓ7ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

Current candidate: 𝑝8

ℓ9

Page 26: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

Current candidate: 𝑝8

ℓ9

Page 27: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

Current candidate: 𝑝8

ℓ9

Page 28: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

Current candidate: 𝑝8

ℓ9

Page 29: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

ℓ1ℓ7

ℓ5

ℓ4 ℓ6

ℓ3

ℓ2

ℓ8

𝑝5𝑝4

𝑝2

𝑝1

𝑝3 𝑝6

𝑝7𝑝8

𝑝9

𝑝10

ℓ1

ℓ2 ℓ3

ℓ4 ℓ5 ℓ6 ℓ7

ℓ8

𝑝1 𝑝2

𝑝3 𝑝5𝑝4ℓ9

𝑝6 𝑝7

𝑝8 𝑝9 𝑝10

𝒒

ℓ9

𝑝8 is the nearest neighbor

Page 30: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Testing for intersection

ℓ: 𝑥 = 𝑎

𝒒 = (𝒒𝒙, 𝒒𝒚)

The current candidate:𝑝

𝑤

The box and the circle intersect if: 𝑞𝑥 +𝑤 > 𝑎

Page 31: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Taken from https://courses.cs.washington.edu/courses/cse373/02au/lectures/lecture22l.pdf

Page 32: Nearest-neighbor search using Kd-treesacg.cs.tau.ac.il/courses/computational-geometry... · Kd-tree ℓ1 ℓ7 ℓ5 ℓ4 ℓ6 ℓ3 ℓ2 ℓ8 5 4 2 1 3 6 7 8 9 10 ℓ1 ℓ2 ℓ3 ℓ4

Complexity

• For a “nicely spread” point set, NN-search on Kd-trees runs in 𝑂(𝑑 log 𝑛) average time

• In the worst case the query complexity is 𝑂(𝑑𝑛)

• Space complexity is 𝑂(𝑑𝑛)