几何查找与 Kd 树

在学习二叉搜索树与平衡树之后,我们将介绍其在图形化搜索中的重要应用。这一节包括了:

  • 1d range search: 基本数据是点,询问区间 [lo,hi] 中包含的点。
  • 1d interval search: 基本数据是区间,询问区间 [lo,hi] 所相交的所有区间。

Geometric interpretation:

  • Keys are point on a line.
  • Count/find points in a given 1d interval.

Build a BST out of the given keys (points).

  • count [∼log⁡N\sim \log{N}]. return rank(hi)-rank(lo)+1.
  • find [∼log⁡N+R\sim\log{N}+R].
    • Recursively find all keys in left subtree (if any could fall in range).
    • Check key in current node.
    • Recursively find all keys in right subtree (if any could fall in range).

NN is the total number of points, RR is the matching points in a single search.

Create a BST, where each node stores an interval (lo,hi)(lo, hi).

  • Use left endpoint as BST key.
  • Store max endpoint in subtree rooted at node.

A BST created this way is called interval search trees

To search for intervals that intersects query interval (lo,hi)(lo, hi):

  • Check the interval in current node.
  • If interval in node intersects query interval:
    • Recursively search all intervals in left subtree.
    • Recursively search all intervals in right subtree.
  • If interval in node doesn't intersects query interval: (# see proof below)
    • If left subtree is null, go right.
    • Else if max endpoint in left subtree is less than lolo, go right.
    • Else go left.

A tricky case: If the current interval doesn't intersect and search goes left, then there is either an intersection in left subtree or no intersections at all.

Pf. (#) We assume the interval with the max endpoint is [c, max]. no intersections in left subtree →\to lo<hi<clo<hi<c →\to no intersections in right.

The order of growth of running time for NN intervals is ∼Rlog⁡N\sim R\log{N} in average. It is better to use a red-black RST to guarantee performance.

Sweep-Line Algorithm

Sweep-line algorithm coverts:

  • Orthogonal line segment intersection problem →\to 1d range search.
  • Orthogonal rectangle intersection problem →\to 1d interval search.

Orthogonal line segment intersection.

Given NN horizontal and vertical line segments, find all intersections. (non-degeneracy assumption: all x- and y-coordinates are distinct).

To solve this, we maintain a sweep vertical line from left to right.

  • x-coordinates define events. [Nlog⁡NN\log{N}]
  • horizontal segment (left endpoint): insert y-coordinate into BST. [Nlog⁡NN\log{N}]
  • horizontal segment (right endpoint): remove y-coordinate from BST. [Nlog⁡NN\log{N}]
  • vertical segment: range search for interval of y-endpoints. [Nlog⁡N+RN\log{N}+R]

Note that RR refers to the number of intersections.

Sweep line reduces 2d line segment intersection to 1d range search

Orthogonal rectangle intersection.

Find all intersections among a set of NN orthogonal rectangles. (non-degeneracy assumption: all x- and y-coordinates are distinct)

To solve this, we maintain a sweep vertical line from left to right.

  • x-coordinate of left and right endpoints define events. [Nlog⁡NN\log{N}]
  • Maintain set of rectangles that intersect the sweep line in an interval search tree using y-intervals of rectangle. [Nlog⁡N+Rlog⁡NN\log{N}+R\log{N}]
  • Left endpoint: interval search for y-interval of rectangle; insert y-interval. [Nlog⁡NN\log{N}]
  • Right endpoint: remove y-interval. [Nlog⁡NN\log{N}]

Sweep line reduces 2d orthogonal rectangle intersection to 1d interval search

Kd Tree

普通的 BST 以及平衡树已经能够解决许多一维的图形搜索问题;借助扫描线算法的转化,它们也能够解决部分的二维图形搜索问题。那么当我们的基本数据有着大于等于二的维度呢?此时就需要依靠其他的空间划分树 (space partitioning tree) 来进行处理了。这里将会介绍其中一个简单优美的 k 维空间划分树:KD 树。

Given NN distinct 2d keys in a plane.

  • Range search: find all keys that lie in a 2d range.
  • Range count: number of keys that lie in a 2d range.

A simple solution is the grid implementation.

  • Divide space into M-by-M grid of squares. [rule of thumbs: M=NM=\sqrt{N}]
  • Create list of points contained in each square.
  • Use 2d array to directly index relevant square.
  • Insert: add (x,y)(x,y) to list for corresponding square.
  • Range search: examine only squares that intersect 2d range query.

Grid implementation is a fast, simple algorithm for evenly-distributed points.

  • Initialization. [NN]
  • Insert point. [11]
  • Range search. [RR]

However, it becomes inefficient when clustering happens. To solve this, we need data structures that adapts gracefully to data.

2d Tree

2d tree is a space-partitioning tree that recursively divide space into two halfplanes.

Space-partitioning trees. Use a tree to represent a recursive subdivision of 2d space.

Ex. 2d tree, Quadtree, BSP tree.

2d tree is essentially a BST, but alternate using x- and y-coordinates as key.

  • Search operation gives the rectangle containing point.
  • Insert operation further subdivides the plane.

2d tree partitions the plane according to the inserting points

Here is the inner class Node for a typical 2D tree implementation.

java
private static class Node {
    private Point2D p;    // the partitioning point
    private RectHV rect;  // the partitioning rectangle
    private Node lb;      // left or bottom
    private Node rt;      // right or top
}

View my code repository for this course; it contains two applications of 2d tree:

  • 2d orthogonal range search.
    • Typical case. [R+log⁡NR+\log{N}]
    • Worst case (even if tree is balanced). [R+NR+\sqrt{N}]
  • Nearest neighbor - find closest point to query point.
    • Typical case. [log⁡N\log{N}]
    • Worst case (even if tree is balanced). [NN]

From 2d to Kd

Kd tree. Recursively partition k-dimensional space into 2 halfspaces.

Kd tree is the generalization of 2d tree. It is an efficient data structure for processing k-dimensional data. The implementation is similar to 2d tree: we just cycle through dimensions ala 2d trees.

Cycle through every k dimensions level by level

CC BY-NC-SA 4.0
正在加载评论…