Brute force Method-recent pair and convex hull problems

Source: Internet
Author: User

3.3.1 Recent questions about

Problem Description: Requires finding the closest two points in a set containing n points. The original thought is that we should compare Euclidean distances between two points, and in fact, we can compare their squares, so that we can avoid the approximate solution of the square root.

Code implementation:
/*** Brute force method to solve recent problems *@authorXiaofeig *@since2015.9.16 *@paramPoints Object Point set *@returnreturns the subscript containing the smallest pair **/     Public Static int[] bruteforceclosestpoints (point[] points) {intdmin= (points[0].x-points[1].x) * (points[0].x-points[1].x) + (POINTS[0].Y-POINTS[1].Y) * (points[0].y-points[1].y); intIndex1=0; intIndex2=1;  for(inti=0;i<points.length-1;i++){             for(intj=i+1;j<points.length;j++){                intd= (points[i].x-points[j].x) * (points[i].x-points[j].x) + (POINTS[I].Y-POINTS[J].Y) * (points[i].y-points[j].y); if(d<DMin) {index1=i; Index2=J; DMin=D; }            }        }        return New int[]{index1,index2}; }
Algorithm Analysis:

The basic operation of the algorithm is to calculate the square, each time the square value of the distance, the operation should be done two times. The algorithm belongs to Θ (N2).

3.3.2 Convex packet problem

Convex collection: For a set of points on a plane (finite or infinite), segments that are endpoints of any two points in the collection belong to that collection.

Convex hull: The smallest convex set that contains a collection of points.

Theorem: A convex hull of any set containing n>2 points (not collinear) is a convex polygon with vertices of some points in the set.

The vertex of a polygon is called the "Pole".

The convex hull problem is to construct a convex hull for a set with n points.

To solve this problem, we need to consider some geometrical knowledge, if we want to build the convex hull, we must first find the pole, if the P1,P2 is the two poles of the polygon, the point of this concentration must be located on the line P1P2 side or the line p1p2. So in judging the pole, the first request out through the two points of the line, set through the point P1 (x1,y1), p2 (x2,y2) of the line for Ax+by=c (a=y2-y1,b=x1-x2,c=x1y2-x2y1), we just need to determine whether all points in the concentration of the point is all satisfied Ax+by <=c or Ax+by>=c, if satisfied, then this two points is the pole, if not satisfied, you can try other point combinations, until the completion of all the two points of a combination.

Code implementation:
/*** Brute force method to solve convex hull problem *@authorXiaofeig *@since2015.9.16 *@parampoints All points set *@returnreturns an ordered set of Poles **/     Public StaticList<point>Bruteforceconvexhull (point[] points) {List<Point> list=NewLinkedlist<point>(); intI=0;  while(i<points.length) {            intJ=0;  while(j<points.length) {                if(Points[i].equals (Points[j])) {J++; Continue;//Filtering coincident points                }                inta=points[j].y-points[i].y; intb=points[i].x-points[j].x; intc=points[i].x*points[j].y-points[i].y*points[j].x; intcount=0;//The positive or negative representation of a value on which side of the line                intK=0;  while(k<points.length) {                    intresult=a*points[k].x+b*points[k].y-C; if(result!=0&&count==0) {Count=result; }                    if(count*result<0) {//determine if there is a point that is not on the same side of the line                         Break; } k++; }                if(k==points.length&& (!list.contains (points[j))) {//to determine whether to find a new pole                    if(!list.contains (Points[i]))                    {List.add (points[i]);                    } list.add (Points[j]); I=j;//start with the new pole and look for the next pole                     Break; } J++; }            if(j==points.length) I++; }        returnlist; }
Algorithm Analysis:

It belongs to Theta (N3), perhaps the above code efficiency is slightly higher than this, because I used the new pole to find the next pole, so the middle skipped part of the iterative process, in fact, according to this idea can be further optimized, such as finding the last pole, you can directly return to the collection, But I never thought how to better judge whether the new pole found is the last pole, because there may be a situation, that is, many points in a straight line, although these points may not be the pole, but the idea of satisfying the above algorithm, this may be the algorithm is not too complete.

Brute force Method-recent pair and convex hull problems

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.