Week3 Assignment - Princeton-Algorithms-PartI

本文介绍了Princeton算法课程Week 3的Assignment,内容涉及线段模式识别问题,包括Point类的实现、蛮力搜索与快速算法的实现。作业要求编写程序,从一组点中找出所有连接4个或更多点的线段。解决方案包括一个N^4复杂度的初始程序和一个更高效的N^2 log N复杂度排序算法。
摘要由CSDN通过智能技术生成

题注

Princeton Algorithm Part I中Week 3的Assignment实际上也是很早前就做完了,不过因为懒,一直没放到网上来… 正好新周期的Algorithm课也督促我复习一遍算法知识,另一方面也有机会把所有的Assignment做完吧。上个周期中,Week 4的Assignment没有拿到满分,就萌生退意了。这回卷土重来,希望能拿到一个Perfect的结果(当然了,Princeton的公开课是没有Accomplishment的,做这个只是学习和课外消遣而已)。

Week 3的题目是图论中的一个很典型的问题,在LeetCode中也有相应的算法题,也算是为刷LeetCode图论的题打点基础吧!

题目

Programming Assignment 3: Pattern Recognition

Write a program to recognize line patterns in a given set of points.

Computer vision involves analyzing patterns in visual images andreconstructing the real-world objects that produced them. The processin often broken up into two phases: feature detection andpattern recognition. Feature detection involves selectingimportant features of the image; pattern recognition involvesdiscovering patterns in the features. We will investigate aparticularly clean pattern recognition problem involving points andline segments. This kind of pattern recognition arises in many otherapplications such as statistical data analysis.

The problem.Given a set of N distinct points in the plane, draw every (maximal) line segment that connects a subset of 4 or more of the points.

Points and lines

Point data type.Create an immutable data type Point that represents a point in the planeby implementing the following API:

public class Point implements Comparable<Point> {
   public final Comparator<Point> SLOPE_ORDER;        // compare points by slope to this point

   public Point(int x, int y)                         // construct the point (x, y)

   public   void draw()                               // draw this point
   public   void drawTo(Point that)                   // draw the line segment from this point to that point
   public String toString()                           // string representation

   public    int compareTo(Point that)                // is this point lexicographically smaller than that point?
   public double slopeTo(Point that)                  // the slope between this point and that point
}
To get started, use the data type Point.java,which implements the constructor and the draw(), drawTo(), and toString() methods.Your job is to add the following components.
  • The compareTo() method should compare points by their y-coordinates,breaking ties by their x-coordinates.Formally, the invoking point(x0, y0)is less than the argument point(x1, y1)if and only if either y0 < y1 or ify0 = y1 and x0 < x1.
  • The slopeTo() method should return the slope between the invoking point(x0, y0) and the argument point(x1, y1), which is given by the formula(y1y0) / (x1x0).Treat the slope of a horizontal line segment as positive zero [added 7/29];treat the slope of a vertical line segment as positive infinity;treat the slope of a degenerate line segment (between a point and itself) as negative infinity.
  • The SLOPE_ORDER comparator should compare points by the slopes theymake with the invoking point (x0, y0).Formally, the point (x1, y1) is less thanthe point (x2, y2) if and only if the slope(y1y0) / (x1x0) is less than the slope(y2y0) / (x2x0).Treat horizontal, vertical, and degenerate line segments as in the slopeTo() method.

Brute force.Write a program Brute.java that examines 4 points at a time and checks whetherthey all lie on the same line segment, printing out any

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值