Programming Assignment 5: Kd-Trees


Write a symbol table data type that provides the ability to map from Point2D objects to arbitrary values. Use a 2d-tree to support efficient range search (find all of the points contained in a query rectangle) and nearest neighbor search (find a closest point to a query point). 2d-trees have numerous applications, ranging from classifying astronomical objects to computer animation to speeding up neural networks to mining data to image retrieval.

Range search and k-nearest neighbor


Geometric primitives. To get started, use the following geometric primitives for points and axis-aligned rectangles in the plane.

Geometric primitives

Use the immutable data type Point2D.java (part of algs4.jar) for points in the plane. Here is the subset of its API that you may use:

public class Point2D {
   public Point2D(double x, double y)              // construct the point (x, y)
   public  double x()                              // x-coordinate
   public  double y()                              // y-coordinate
   public  double distanceSquaredTo(Point2D that)  // square of Euclidean distance between two points
   public     int compareTo(Point2D that)          // for use in an ordered symbol table
   public boolean equals(Object that)              // does this point equal that?
   public    void draw()                           // draw to standard draw
   public  String toString()                       // string representation
}
Use the immutable data type RectHV.java (not part of algs4.jar) for axis-aligned rectanges. Here is the subset of its API that you may use:
public class RectHV {
   public    RectHV(double xmin, double ymin,      // construct the rectangle [xmin, xmax] x [ymin, ymax]
                    double xmax, double ymax)      // throw a java.lang.IllegalArgumentException if (xmin > xmax) or (ymin > ymax)
   public  double xmin()                           // minimum x-coordinate of rectangle
   public  double ymin()                           // minimum y-coordinate of rectangle
   public  double xmax()                           // maximum x-coordinate of rectangle
   public  double ymax()                           // maximum y-coordinate of rectangle
   public boolean contains(Point2D p)              // does this rectangle contain the point p (either inside or on boundary)?
   public boolean intersects(RectHV that)          // does this rectangle intersect that rectangle (at one or more points)?
   public  double distanceSquaredTo(Point2D p)     // square of Euclidean distance from point p to closest point in rectangle
   public boolean equals(Object that)              // does this rectangle equal that?
   public    void draw()                           // draw to standard draw
   public  String toString()                       // string representation
}
Do not modify these data types.

Brute-force implementation. Write a mutable data type PointST.java that maps Point2D objects to a generic value. Implement the following API by using a red-black BST (using either RedBlackBST from algs4.jar or java.util.TreeMap, do not try to implement your own red-black BST).

public class PointST<Value> {
   public         PointST()                               // construct an empty symbol table of points
   public           boolean isEmpty()                     // is the symbol table empty?
   public               int size()                        // number of points in the ST
   public              void insert(Point2D p, Value v)    // add the point p to the ST or if it already exists, update
   public             Value get(Point2D p)                // returns value mapped to by p   
   public           boolean contains(Point2D p)           // does the ST contain the point p?
   public              void draw()                        // draw points to standard draw
   public Iterable<Point2D> range(RectHV rect)            // all points in the ST that are inside the rectangle
   public           Point2D nearest(Point2D p)            // a nearest neighbor in the ST to p; null if set is empty
   public static void main(String[] args)                 // unit testing of the methods (not graded)
}
Your implementation should support insert(), get() and contains() in time proportional to the logarithm of the number of points in the set in the worst case; it should support nearest() and range() in time proportional to the number of points in the symbol table. You may assume that clients will not pass key or value arguments equal to null.

2d-tree implementation. Write a mutable data type KdTreeST.java that uses a 2d-tree to implement the same API (but replace PointST with KdTreeST). A 2d-tree is a generalization of a BST to two-dimensional keys. The idea is to build a BST with points in the nodes, using the x- and y-coordinates of the points as keys in strictly alternating sequence, starting with the x-coordinates.

Insert (0.7, 0.2)

insert (0.7, 0.2)
Insert (0.5, 0.4)

insert (0.5, 0.4)
Insert (0.2, 0.3)

insert (0.2, 0.3)
Insert (0.4, 0.7)

insert (0.4, 0.7)
Insert (0.9, 0.6)

insert (0.9, 0.6)
Insert (0.7, 0.2)
Insert (0.5, 0.4)
Insert (0.2, 0.3)
Insert (0.4, 0.7)
Insert (0.9, 0.6)

The prime advantage of a 2d-tree over a BST is that it supports efficient implementation of range search and nearest neighbor search. Each node corresponds to an axis-aligned rectangle, which encloses all of the points in its subtree. The root corresponds to the infinitely large square from [(-∞, -∞), (+∞, +∞ )]; the left and right children of the root correspond to the two rectangles split by the x-coordinate of the point at the root; and so forth.

Clients.  You may use the following interactive client programs to test and debug your code.

Analysis of running time and memory usage. Analyze the effectiveness of your approach to this problem by giving estimates of its time and space requirements.

Submission.  Submit only PointST.java and and KdTreeST.java. Each of the two data types should include their own main() that thoroughly tests the associated operations. We will supply Point2D.java, RectHV.java, stdlib.jar, and algs4.jar. You may not call any library functions other than those in java.lang, java.util, stdlib.jar, and algs4.jar. Finally, submit a readme.txt file and answer the questions.

Extra credit.  For one point of extra credit, create a new class KdTreePlusKdTreeEnhanced that contains one additional method:

public Iterable<Point2D> nearest(Point2D p, int k)
This method should return the k points that are closest to the query point. It should do this in an efficient manner, i.e. using the technique from KdTree, not brute force. Once you've completed this class, you'll be able to run the boid simulator. Behold their flocking majesty.

Challenge for the bored.  Make an interesting improvement to my initial attempt at a boid simulator. This can involve interactivity, improvement of flocking behavior, ability to zoom in or out, camera tracking (e.g. right now the boids tend to fly off to distant lands before returning to the middle), creation of a better physics model (e.g. right now the hawk has a fixed amount of directional thrust). Alternately, I'd also be interesting in hearing about how the behavior of the simulator depends on the parameters provided. Extensions for this challenge will be provided upon request. Josh is the only one who will provide support for this optional challenge. Particularly good submissions will be worth up to 2 bonus points (on top of the 1 point for extra credit).

This assignment was developed by Kevin Wayne, with Boid simulation by Josh Hug.