Double-tune Euclid's Travel quotient problem

Source: Internet
Author: User
Tags gety

Euclid Travel Quotient problem is a NP problem, the problem description: A plane of n points, determine a connection between the shortest closed journey.

So it is usually simplified to double-tune Euclidean problem to find an approximate solution, borrow a diagram of the introduction of the algorithm, as shown below

A) is an optimal Euclidean

b) is a double-tone route, starting from the leftmost point strictly to the right

With dynamic programming, you need to analyze sub-problems first:

dual path is i0-j; Span lang= "ZH-CN" >i J n-10 <= i < N; 0 <= J < n;

Due to the existence of symmetry (bitonic[i][j] = = Bitonic[j][i]), consider only The case I is less than J 0 <= i < J < N; for the left point,J is the right end of the adjustment;

There are three scenarios to consider when solving

1) When I < j-1, since I is the starting point of the left adjustment curve, so j-1, J are on the right curve

can draw bitonic[i][j] = Bitonic[i][j-1] + distance[j-1][j]

2) when i = = j-1 need to use dynamic to obtain the minimum distance

foreach (0 < K < i)

Bitonic[i][j] = min (Bitonic[k][i] + distance[k][j])

3) When i = = J due to line p[j-1][j] must be on the left or right line,

So the optimal line bitonic[j-1][j] or bitonic[j][j -1] is the best bitonic[j][j] of the predecessor Route

because Bitonic[j-1][j] and bitonic[j][j -1] are symmetrical so there are:

BITONIC[J][J] = Bitonic[j-1][j] + distance[j-1][j]

After analyzing it, the code is simple:

 PackageCom.feinno.algorithmic.travelingsalesman;Importjava.util.Arrays;/*** Double Euclidean algorithm * BITONIC[I][J] * 1) i is the starting point of the left, J for the right end point * 2) 0 <= i <= J <= N * When I < j-1, because I is the start of the left-tuning curve, so J- 1, J all on the right curve * can be drawn bitonic[i][j] = Bitonic[i][j-1] + distance[j-1][j] * when i = = j-1 need to use to dynamically get the minimum distance * for Each (0 < K < i) * Bitonic[i][j] = min (Bitonic[k][i] + distance[k][j]) * when i = = J due to segment P[j-1][j] must be left Adjustment or right line, * so the optimal bitonic[j-1][j] is the best bitonic[j][j], the forward route * bitonic[j][j] = Bitonic[j-1][j] + distance[j-1][j ] *  * @authorRenzhaolong **/ Public classBITONICTSP {/*** Distance between two nodes*/    Private Double[] distance; /*** I is the starting point of the left, J is the right end of the double-adjustable distance*/    Private Double[] bitonic; /*** Pre-node with optimal dual tuning*/    Privatepoint[][] preposition; /*** Node collection, sorted by x-coordinate increment*/    Privatepoint[] points; /*** Number of collections*/    intLenth = 0; /*** Calculate the double Euclidean distance according to the agreement *@return     */     Public DoubleGetbitonicresult () {bitonic[0][1] = getdistance (0, 1);  for(intj = 2; J < Lenth; J + +) {            //first case I < j-1             for(inti = 0; i < j-1; i++) {Bitonic[i][j]= Bitonic[i][j-1] + getdistance (j-1, J); }                        //second case i = J-1             for(intk = 0; K < j-1; k++) {                Doubletemp = Bitonic[k][j-1] +Getdistance (k, J); if(Temp < Bitonic[j-1][j]) {bitonic[j-1][J] =temp; Preposition[j-1][J] =NewPoint (K, j-1); } }} bitonic[lenth-1][lenth-1] = Bitonic[lenth-2][lenth-1] + getdistance (lenth-2, lenth-1); returnBitonic[lenth-1][lenth-1]; }        /*** Get two-node distance * If data is returned directly from the cache, there is no calculation and cache *@paramX1 *@paramX2 *@return     */    Private DoubleGetdistance (intX1,intx2) {        if(X1 >x2) {X1= x1 +x2; X2= x1-x2; X1= x1-x2; }        if(DISTANCE[X1][X2] = =-1) {distance[x1][x2]=points[x1].getdistance (points[x2]); }        returnDISTANCE[X1][X2]; }         Publicbitonictsp (point[] points) {Lenth=points.length;        Arrays.sort (points);  This. Points =points; Distance=New Double[Lenth][lenth]; Bitonic=New Double[Lenth][lenth]; preposition=NewPoint[lenth][lenth];  for(inti = 0; i < lenth; i++) {             for(intj = 0; J < Lenth; J + +) {Distance[i][j]=-1; BITONIC[I][J]=Double.max_value; }        }    }         Public Static voidMain (string[] args) {bitonictsp tsp=NewBITONICTSP (Newpoint[]{NewPoint (1, 1),                        NewPoint (2, 7),                        NewPoint (3, 4),                         NewPoint (6, 3),                        NewPoint (7, 6),                         NewPoint (8, 2),                         NewPoint (9, 5)});    System.out.println (Tsp.getbitonicresult ()); }}
 PackageCom.feinno.algorithmic.travelingsalesman; Public classPointImplementsComparable<point> {    Private intx; Private inty;  PublicPoint (intXinty) { This. x =x;  This. y =y; }         Public DoubleGetdistance (Point other) {returnMath.sqrt (Math.pow (GetX ()-Other.getx (), 2) + Math.pow (GetY ()-Other.gety (), 2)); }         Public intGetX () {returnx; }     Public intGetY () {returny; } @Override Public intcompareTo (Point O) {returnGetX ()-O.getx (); }}

Double-tune Euclid's Travel quotient problem

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.