Algorithm--The recent problem and convex hull problem of brute force method

Source: Internet
Author: User
Tags square root

Last blog wrote half of the dormitory power outage .... However, today I think of adding up to find that the blog park is not automatically save Oh, smile.

First of all, the recent problem, the most recent description of the problem is found in the N-side of the collection of the two closest points, of course, the problem can be defined in the multidimensional space, but here just follow the book's ideas to achieve the two-dimensional situation of the recent problem. Assuming that all the points discussed are given in the form of standard Cartesian coordinates (x, y), the distance between the two points pi= (Xi,yi) and pj= (XJ,YJ) is the standard Euclidean distance:

D (PI,PJ) =sqrt ((x1-x2) (Y1-Y2) 2)

The idea of brute force method is to calculate the distance between all the points, and then find the one with the smallest distance, the way to increase efficiency here is to calculate only the distance between the points of the point subscript i<j, so as to avoid the repetition of calculating the distance between the same pair of points. Here is the brute force method to solve the most recent problem-solving algorithm:

Using brute force method to find the nearest two points in the plane

Bruteforceclosetpoints (P)

Input: A list of points of N (n≥2) p,pi= (Xi,Yi)

Output: Subscript index1 and Index2 from the nearest two points

DMin <-∞

For I <-1 to N-1 do

For J <-i+1 to N do

D <-sqrt ((Xi-xi)2+ (YJ-yJ)2 )

If d<dmin;

Dmin=d;  Index1=i; Index2=j;

Return INDEX1,INDEX2

The key step of the algorithm is that the basic operation is to calculate Euclidean distance between two points, but the square root is not as simple as the addition multiplication. In the algorithm above, the derivative of the open square function is constant greater than 0, it is strictly incremented, so we can directly only calculate (Xi-xi)2+ (YJ-y-J)2, Compare the size of the D2, so the basic operation becomes the square. The number of executions of the squared operation is:

N (n-1) ∈θ (n2)

Therefore, the brute force method solves the most recent problem with the average time complexity of θ (n2)

  

Here is the C + + code implementation part of the algorithm, when implementing this algorithm, I encountered three problems:

One is: How to represent a point set, because the final return of the subscript is the index of the midpoint of the set, the data structure to be used is a one-dimensional array, but the point of XY coordinates and how to express, here I in the header file to create a struct type point structure, The structure has a member variable that x represents the horizontal axis and y represents the ordinate, so you can directly create an array of the structure to calculate.

The second is: the bruteforceclosetpoints (P) function returns the value of two most recent pairs, but returns only one value, so the variable of the reference type is added here in the Bruteforceclosetpoints function's parameter table & Index1,&index2.

Three: Before calculating the distance between points, the maximum value needs to be paid to the variable that stores the current minimum distance dmin, but I do not remember how to represent the maximum value of the float type, after the search found that the C + + <float.h> header file defined in the Flt_ Max This constant represents the maximum value of the float type (which also records the highest value of the double type, and the int type is recorded in the <limits.h> header file).

In addition to the implementation of a more consistent thinking, I did not use the array of space No. 0, but directly from the 1th space to start using.

Here is the source code section:

//Header File Section#include <float.h>//The top value of the float type and double type is stored in the header file.typedefstructpoint{floatx; floaty;};voidBruteforceclosetpoints (Point p[],intNint&index1,int&index2);//Source Files Section#include <iostream>#include"recent issues with the header file. h"using namespacestd;intMain () {point p[4];  for(inti =1; I <4; i++) {cin>> p[i].x >>p[i].y;    } getchar (); /*cout << "the coordinates of the point you entered are:" << Endl;    for (int i =1; I <4; i++) {cout<< "(" <<p[i].x << "," << p[i].y<< ")"; }    */       intindex1, Index2; Bruteforceclosetpoints (P,4, Index1, INDEX2); cout<<"The subscript for the two most recent pairs of points is:"<< index1 <<"    "<< index2<<Endl;    GetChar (); return 1;}voidBruteforceclosetpoints (Point p[],intNint&index1,int&AMP;INDEX2) {//because you want to return the subscript for both points at the same time, set the parameter as a reference variable    floatDMin = Flt_max;//constant Flt_max from float.h header file    inti =1, j=0; floatD;  for(; i < n-1; i++){         for(j = i +1; J < N; J + +) {D= (p[i].x-p[j].x) * (p[i].x-p[j].x) + (P[I].Y-P[J].Y) * (P[I].Y-p[j].y); if(D <DMin) {DMin=D; Index1=i; Index2=J; }        }    }}

Algorithm--The recent problem and convex hull problem of brute force method

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.