Introduction to algorithms study Questions 15-1 dual-tune Euclid travel Quotient problem (Dynamic planning)

Source: Internet
Author: User

  Euclid's Travel quotient problem is the problem of determining the shortest closed journey of a connected point to a given n point on a plane. Figure A gives the solution of 7 point problem, the general form of this problem is NP-complete, so its solution needs more time than polynomial.

J.k.bentley recommends simplifying the problem by considering only the two-tone journey, which starts from the leftmost point, strictly from left to right, and then strictly from the right-most point to the leftmost point. Figure B shows the same 7-point problem of the shortest double-tuned route , in which case the polynomial time algorithm is possible.

Describes an algorithm for determining the O (n^2) time of the optimal two-tone route. you can assume that any two points have different x-coordinates .

Solution:

read a lot of this question, but also read the online a few on the issue of the blog, there are many parts is wrong but there is no correction, so his practice to collate a solution to the problem. First of all, the biggest doubt is to understand what is called double tone (Bitonic), read a lot of explanation of the terms of the use of the double tune, I understand the double tune in this problem is the idea of the whole closed route to expand a point, because the problem is required from the left to the right side of the scan, and then from the right side of the You might as well start with the left-most point as the starting point (exactly the right end, no longer repeat).

We need an auxiliary matrix a[ilen+1][ilen+1], like all dynamic programming problems, the size of the matrix is the scale of the problem +1, the entire calculation process is from the left end to the right end, that is, we calculate the shortest path from the left side to the right end of the process of growth, auxiliary matrix A[i][j] refers to p[ I] to p[1] and p[1] to p[i-1] "common" shortest path, the calculation process only takes advantage of the matrix of the lower triangular part, so the previous index is small and the latter index. First, the Ilen points are stored in a struct/class array to hold the coordinates of all points, and the distance between Count group P,p1,p2 is recorded as Dist (P1,P2).

Core idea:

1) (premise) when we calculate the first point or merge it into the shortest path, the first 1.2.3...i-1 Point has formed a shortest path.

2) to calculate A[i][j], the selected branch of the newly added point P[j] has three, that is, the route planning "growth" case has only three kinds:

(a) When j-1〉i, according to article (1) We need to do is the sub-shortest path formed in the auxiliary matrix plus the new edge (by definition must be added in the longer part of the course)

There is a[i][j]=a[i][j-1]+dist (J-1,J);

(b) When j-1=i, it is time to summarize the previous sub-shortest path to generate a longer new sub-shortest path.

expressed as a[i][j]=min{(A[k][j-1]+dist (K,j)), (A[k-1][j-1]) +dist (k-1,j) ....} K is the traversal value

(iii) When j=i, the entire graph is closed to require the last two edges, essentially a branch of (ii) process

namely a[i][j]=min{(A[k][j-1]+dist (k,j) +dist (J-1.J)) ...} K is the traversal value

#include <iostream> #include <vector> #include <cstdlib> #include <cmath> #define Max_value 99999class point{public:point (Double px=0,double py=0): X (px), Y (py) {}////////constructor & Default to be zerodouble X;double y;}; Double Dist (Point p1,point p2) {return sqrt ((p2.x-p1.x) * (p2.x-p1.x) + (P2.Y-P1.Y) * (P2.Y-P1.Y));} int main () {const int ilen=7; Point p[ilen+1];////Pointer listp[1]=point (0,6);p [2]=point (1,0);p [3]=point (2,3);p [4]=point (5,4);p [5]=point (6,1); P[6]=point (7,5);p [7]=point (8,2);//////////////to minimize the time of debugging We get parameters initializedouble A[ile n+1][ilen+1]={}; null-filledfor (int j=3;j<=ilen;j++) {for (int i=1;i<=j-2;i++) {a[i][j]=a[i][j-1]+dist (p[j-1],p[j]);} a[j-1][j]=max_value;for (int i=1;i<=j-2;i++) {a[j-1][j]= (A[i][j-1]+dist (P[i],p[j])) <a[j-1][j]? ( A[i][j-1]+dist (P[i],p[j]): A[j-1][j];}} Double dmin=max_value;for (int i=2;i<=ilen-1;i++) {double tmp=a[i][ilen]+dist (P[i],p[ilen]) +dist (p[ilen-1],p[ Ilen]); if (tmp<dmin) dmin=tmp;} /DMin records the Answerstd::cout<<dmin<<std::endl;return 0;} 

  

Introduction to algorithms study Questions 15-1 dual-tune Euclid travel Quotient problem (Dynamic planning)

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.