Week 5 Assignment - Princeton-Algorithms-PartI

这篇博客详细介绍了如何使用2d树(KdTree)来实现单位正方形内的点集,支持范围搜索和最近邻搜索。文章分析了Point2D、RectHV、PointSET以及KdTree的数据类型,提供了搜索和插入的算法,并探讨了2d树相对于BST的优势。博主分享了KdTree的插入、范围搜索和最近邻搜索的实现细节,以及如何利用递归和修剪规则提高搜索效率。此外,还讨论了Princeton课程中的编程作业和教学资源,指出国内计算机教学与先进国家的差距。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题注

最近实在是太忙了,主要是又进入了论文周期,技术方案刚刚确定,估计后面的3周要一直写论文… 本来打算这一阵子不更新技术博客了,没想到优快云的朋友们有的还等着我写Week 5的Assignment呢!我这人就怕让别人等… 于是就花了整个一中午的时间把这个东西写完了。为什么这么快呢?第一个原因是以前Stanford在Coursera中也开过一门算法课,叫做《Algorithm: Design and Analysis I》,那里面也用到了KdTree,原始的代码我还留着。第二个原因是,KdTree搜索最近Point2D是一个很模块化的东西,网上实际上是有现成的代码的,甚至国外的友人们也有用Princeton的Stdlib做的。所以,Week5的Assignment我基本上结合了上面的两套代码,组合成了自己的代码,其主体框架用的是国外友人的,因为代码结构更清晰~

国外友人针对这一问题答案的链接为:https://gist.github.com/agjacome/5246693。有兴趣的朋友们可以看一下,不过其没有太多的分析。也难怪啦,Assignment题目中对于算法的描述已经非常清楚了,结合课程本身里面KdTree章节的讲解,相信大家只要认真写,是肯定可以写出来的。

题目

Write a data typeto represent a set of points in the unit square (all points have x- and y-coordinates between 0 and 1)using a 2d-tree to supportefficient range search (find all of the points containedin a query rectangle) and nearest neighbor search (find aclosest point to a query point).2d-trees have numerous applications, ranging from classifying astronomical objectsto 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 andaxis-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 implements Comparable<Point2D> {
   public Point2D(double x, double y)              // construct the point (x, y)
   public double x()                               // x-coordinate
   public double y()                               // y-coordinate
   public double distanceTo(Point2D that)          // Euclidean distance between two points
   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 distanceTo(Point2D p)             // Euclidean distance from point p to the closest point in rectangle
   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 PointSET.java that represents a set ofpoints in the unit square. Implement the following API by using ared-black BST (using either SET from algs4.jar or java.util.TreeSet).

public class PointSET {
   public PointSET()                               // construct an empty set of points
   public boolean isEmpty()                        // is the set empty?
   public int size()                               // number of points in the set
   public void insert(Point2D p)                   // add the point p to the set (if it is not already in the set)
   public boolean contains(Point2D p)              // does the set contain the point p?
   public void draw()                              // draw all of the points to standard draw
   public Iterable<Point2D> range(RectHV rect)     // all points in the set that are inside the rectangle
   public Point2D nearest(Point2D p)               // a nearest neighbor in the set to p; null if set is empty
}
Your implementation should support insert() and contains() in timeproportional 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 set.

2d-tree implementation.Write a mutable data type KdTree.java that uses a 2d-tree to implement the same API (but replace PointSET with KdTree).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 pointsas keys in strictly alternating sequence.

  • Search and insert. The algorithms for search and insert are similar to those forBSTs, but at the root we use the x-coordinate(if the point to be inserted has a smaller x-coordinatethan the point at the root, go left; otherwise go right);then at the next level, we use the y-coordinate(if the point to be inserted has a smaller y-coordinatethan the point in the node, go left; otherwise go right);then at the next level the x-coordinate, and so forth.

Insert (0.7, 0.2)

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

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值