On the algorithm--brute force method--recent pair and convex hull problems

Source: Internet
Author: User

One, the most recent problem: the two-dimensional or multi-bit space to find the closest point in the distance

1. Steps

A, calculate the distance between each pair of points separately

B. Find the closest pair

(to avoid repeated calculations, consider only those pairs of i<j)

2. JavaScript implementation

1<! DOCTYPE html>234<meta charset= "UTF-8" >5<title> Recent Issues </title>67<body>8     9</body>Ten<script type= "Text/javascript" > One     varPoint =function(x, y) { A         var_arr =NewArray (); -_arr =[x, y]; -_arr.x =x; the_ARR.Y =y; -         return_arr; -     } -      +     varCaculate =function(P1, p2) { -         return(p1.x-p2.x) * (p1.x-p2.x) + (P1.Y-P2.Y) * (P1.Y-p2.y); +     } A  at     functionSearch (arr) { -         vardis =number.positive_infinity; -         varIndexx =NULL; -         varIndexy =NULL; -          for(vari = 0; i < arr.length-1; i++){ -              for(varj = i+1; J < Arr.length; J + +){ in                 varD =caculate (Arr[i], arr[j]); -                 if(D <dis) { todis =D; +Indexx =i; -Indexy =J; the                 } *             } $         }Panax Notoginseng         return"The distance between the" +indexx+ "point and the" +indexy+ "point is Nearest"; -     } the  +     //Experimental Phase A     //Four point coordinates were created (0,0), (2,0), (0,3), (2,1) the     varP1 =NewPoint (0,0); +     varP2 =NewPoint (2,0); -     varP3 =NewPoint (0,3); $     varP4 =NewPoint (2,1); $ Console.log (Search ([P1, P2, p3, P4] ) -</script> -

3. Algorithm Analysis

can be used (XI-XJ) 2 + (YI-YJ) 2 instead of sqrt ((XI-XJ) 2 + (YI-YJ) 2), as far as possible to avoid the root; so the basic operation of this algorithm is to calculate the square, the number of executions C (n) =σ[i=1 to N-1]σ[j=i+1 to n] 2 = n ( n-1), belonging to θ (N2)

One, convex hull problem

1, Convex set concept: For a set of points on a plane, if the set of any two points P and Q are not power cut line segments, the collection is convex

2. Convex hull: Convex hull is the smallest convex set containing all points

3. General Judgment method: Judge whether all other points are in the same side of the line connecting P1 and P2

4. General time Efficiency: it belongs to θ (n3); for each n (n-1)/2 of different points, compare the symbols of the other n-2 points

On the algorithm--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.