"Fundamentals of Algorithmic Design and analysis" 15, recent questions

Source: Internet
Author: User
Tags pow square root

1, because Java does not hold a single key-value pair of type is not very convenient to use

Package cn.xf.util;/** *  * function: Equivalent to a key value * @author Xiaofeng * @date June 18, 2017 * @fileName Genericpair.java * */PU Blic class Genericpair<e extends Object, F extends object> {private E first;private F second;public Genericpair () {} Public Genericpair (E first, F second) {This.first = First;this.second = Second;} @Overridepublic string toString () {string result = "[" + this.first.toString () + "," + this.second.toString () + "]"; return Result;} Public E GetFirst () {return first;} public void Setfirst (E first) {This.first = first;} Public F Getsecond () {return second;} public void Setsecond (F second) {this.second = second;}}

  

Ask for a recent key-value pair problem

Package Cn.xf.algorithm.ch05;import Java.util.arraylist;import Java.util.list;import org.junit.test;import cn.xf.util.genericpair;/** * Features: Recent questions * @author Xiaofeng * @date June 18, 2017 * @fileName Minpath.java * */public class M Inpath {/** * * * @param the points array points stores n >= 2 points on the plane and sorts them in ascending order by the x-axis coordinates of those points seqxlist Seqylist * @param qpoints data qpoint The same point is stored with the points, except that it is sorted in ascending order of the y-axis coordinates of this point * output: Euclidean distance between nearest point pair */public double Efficientclosestpair (list<genericpair<        Double, double>> points, list<genericpair<double, double>> qpoints) {if (Points.size () <= 3) {    If less than 3 direct brute force comparison, the nearest distance to find a few points double countmin = double.max_value;    for (int i = 0; i < points.size (), ++i) {for (int j = 0; J < points.size (); ++j) {if (i = = j) {continue;    } Double temp = dist (Points.get (i), Points.get (j));    if (Temp < countmin) {countmin = temp;    }}} return countmin;    } else {int midindex = Points.size ()/2; Well, points, the front half pulls out POINTSL L.Ist<genericpair<double, double>> POINTSL = new arraylist<genericpair<double,double>> ();    for (int i = 0; i < Midindex; ++i) {Pointsl.add (Points.get (i)); }//Pull out the front half of the qpoints qpointsl//list<genericpair<double, double>> QPOINTSL = new Arraylist<genericpair <Double,Double>> ();//for (int i = 0; i < Midindex; ++i) {//Qpointsl.add (Qpoints.get (i));/}//PO INTSL's Y-sorted version as the new QPOINTSL list<genericpair<double, double>> QPOINTSL = new arraylist<genericpair<    Double,double>> ();    for (int i = 0; i < Midindex; ++i) {Qpointsl.add (Points.get (i));        } seqylist (QPOINTSL, 0, Qpointsl.size ()-1); Pull out the back half of the points. POINTSR list<genericpair<double, double>> pointsr = new arraylist<genericpair<    Double,double>> ();    for (int i = Midindex; I < points.size (); ++i) {Pointsr.add (Points.get (i)); }//Bar qpoints back half extracted qpointsr//list<genericpair<Double, double>> QPOINTSR = new arraylist<genericpair<double,double>> ();//for (int i = midindex; I &l T Qpoints.size (); ++i) {//Qpointsr.add (Qpoints.get (i)),//} list<genericpair<double, double>> QPOINTSR = new Array    List<genericpair<double,double>> ();    for (int i = Midindex; I < points.size (); ++i) {Qpointsr.add (Points.get (i));        } seqylist (QPOINTSR, 0, Qpointsr.size ()-1);    Enter recursive double dl = Efficientclosestpair (POINTSL, QPOINTSL) with the new collection point;        Double dr = Efficientclosestpair (POINTSR, QPOINTSR);    Two distances take small double dmin = minValue (DL, DR);    Double dmindist = Math.pow (DMin, 2);    Take X is worth the intermediate segment double midpointx = Points.get (Midindex). GetFirst (); All of these points x-midpointx the distance is smaller than the DMin list<genericpair<double, double>> pointsdmin = new arraylist<    Genericpair<double,double>> (); for (int i = 0; i < qpoints.size (); ++i) {//absolute value is smaller than the minimum distance if (Math.Abs (Qpoints.get (i). GETFirst ()-Midpointx) < DMin) {//the minimum distance from both sides, a smaller value, this is the data pointsdmin.add in ascending order of Y (Qpoints.get (i)); }}//Traverse all data in this collection, get the minimum distance for (int i = 0; i < pointsdmin.size (); ++i) {///if K is out of range, don't forget int k = i + 1    ;    if (k >= pointsdmin.size ()) {break;    }//squared difference Double minY2 = Math.pow (Pointsdmin.get (k). Getsecond ()-Pointsdmin.get (i). Getsecond (), 2); while (K < pointsdmin.size () && minY2 < dmindist) {//within the data range, then Y has a smaller squared difference than the smallest, then you can compare double tempmin = Math.pow (Pointsdmin.get (k). GetFirst ()-Pointsdmin.get (i). GetFirst (), 2) + Math.pow (Pointsdmin.get (k). Getsecond ()-    Pointsdmin.get (i). Getsecond (), 2);    Dmindist = MinValue (Tempmin, dmindist);    ++k;    }} return Math.sqrt (dmindist); }}public double MinValue (double DL, double dr) {if (DL < DR) {return DL;} else {return dr;}} public static Double Dist (genericpair<double, double> point1, genericpair<double, double> point2) {//Find out two points The distance between//squared difference Double DISTX = Math.Abs (Point1.getfirst ()-Point2.getfirst ());    Double disty = Math.Abs (Point1.getsecond ()-Point2.getsecond ()); The square root of the sum of the squared deviations is math.sqrt (DISTX * distx + disty * disty);} @Testpublic void Quiksorttest () {list<genericpair<double, double>> points = new Arraylist<genericpair <Double,Double>> (); List<genericpair<double, double>> qpoints = new arraylist<genericpair<double,double>> ();// 5,3,1,9,8,2,4,7,8//4,1,8,2,11,9,7,15,11genericpair<double, double> el1 = new genericpair<double, Double> (5d, 4d); Genericpair<double, double> el2 = new genericpair<double, double> (3d, 1d); Genericpair<double, double> el3 = new genericpair<double, double> (1d, 8d); Genericpair<double, double> el4 = new genericpair<double, Double> (9d, 2d); Genericpair<double, double> el5 = new genericpair<double, double> (8d, 11d); Genericpair<double, double> el6 = new genericpair<double, double> (2D, 9d); Genericpair<double, double> el7 = new genericpair<double, double> (4d, 7d); Genericpair<double, double> El8 = new genericpair<double, double> (7d, 15d);//genericpair<double, Double > el9 = new genericpair<double, double> (8d, 11d);p Oints.add (EL1);p oints.add (EL2);p oints.add (el3);p Oints.add (EL4);p oints.add (el5);p oints.add (el6);p oints.add (EL7);p oints.add (El8);//points.add (EL9); Qpoints.add (EL1); Qpoints.add (EL2); Qpoints.add (EL3); Qpoints.add (EL4); Qpoints.add (EL5); Qpoints.add (EL6); Qpoints.add (EL7); Qpoints.add (El8);//qpoints.add (EL9); Minpath MP = new Minpath () mp.seqxlist (points, 0, points.size ()-1); Mp.seqylist (qpoints, 0, Qpoints.size ()-1);//for (Gen Ericpair<double, double> temp:points) {//system.out.print (temp.tostring () + "\ t");//}double result = Mp.efficientclosestpair (points, qpoints); SYSTEM.OUT.PRINTLN (result);} /** * According to X Ascending * @param points * @return */public static void Seqxlist (List<genericpair<double, double>> points, int start, int end) {//Gets the middle point position and then divides it in half, traversing recursively if (start < end) {int mid = Hoarepartition (points, false, start, end); Seqxlist (points , start, mid-1); Seqxlist (points, Mid + 1, end);}} /** * in ascending order of Y * @param points * @return */public static void Seqylist (List<genericpair<double, double>> points, int start, int end) {//Gets the middle point position and then divides it in half, traversing recursively if (start < end) {int mid = Hoarepartition (points, true, start, end); seqyl IST (points, start, mid-1), Seqylist (points, Mid + 1, end);}} /** * Search for split points, and structured data, xory is used to determine whether the two sides are sorted by x-false or y-true sort * @param points * @return */public static int hoarepartition (list< Genericpair<double, Double>> points, Boolean xory, int start, int end) {if (start >= end) {return start;} Split Double temp = GetValue (Points.get (start), xory) on the first number of pairs, and//traverse until two stagger, or equal int leftindex = start + 1;int Rightindex = End;while (Leftindex < Rightindex) {//if traversing to the left is larger than the first, the right is smaller than the first number, exchanging data, in order to avoid unnecessary swapping while (GetValue ( Points.get (Leftindex), xory) < temp) {++leftindex;} while (GetvalUE (Points.get (Rightindex), xory) > Temp) {--rightindex;} If it is the last step, dislocation, and then swap, to avoid the extra swap swap (points, leftindex, rightindex);} From the last pair of misplaced data exchange back, but if this cycle has not been in, then there should be no swap swap (points, leftindex, rightindex);//Finally, the data place of J is switched to the data location starting as the intermediate point// If it is smaller than the middle value to be arranged, then do not change, change the sort if (GetValue (Points.get (start), xory) > GetValue (Points.get (Rightindex), xory)) swap (Points, start, rightindex); return rightindex;} Exchange data public static void Swap (list<genericpair<double, double>> points, int i, int j) {//Take out the I position data, I data is modified to J, Temp is data for I, Bar J is replaced with tempgenericpair tempi = Points.get (i); Genericpair TEMPJ = Points.get (j);p Oints.set (i, TEMPJ);p Oints.set (J, tempi);} /** * Xory is used to determine whether the two sides are sorted by x-false or y-true sort * @param pair * @param xory * @return */public static double GetValue (genericpair& Lt;double, double> pair, Boolean xory) {Double temp = 0;if (!xory) {///If True, then it is the first y//= Xtemp () to be judged Pair.getfirst);} E LSE {TEMP = Pair.getsecond ();} return temp;}}

  

Nearest point distance

Questions, answers, online what " pigeon Nest Principle " is not very understand, to seek the interpretation of popular points ...

"Fundamentals of Algorithmic Design and analysis" 15, recent questions

Related Article

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.