[Introduction to algorithms] dual-tuning Euclidean Traveling Salesman Problem

Source: Internet
Author: User
First, celebrate your solution (understanding the legendary niubility traveling salesman problem)

Second, we are about to see the famous greedy algorithm problem! I am very excited.

Traveling Salesman Problem description: N points on the plane, determine the shortest closed journey connecting each point. This solution is generally in the form of Np (which can be obtained within the polynomial time)

J. l. bentley suggests simplifying the problem by only considering bitonic tour, which is a journey from the leftmost point to the rightmost point, strictly from left to right, then strictly from right to left to start point. (B) shows the shortest double-adjustment routes of the same seven points. In this case, polynomial algorithms are possible. In fact, there is an O (N * n) Time Algorithm for determining the optimal dual-tuning route.

  

PS: seven points on the plane displayed on a grid. A) The shortest closed route. The length is about 24.89. This route is not dual-tuned. B) The shortest two-way closure route on the set with the same points. It is about 25.58 in length.

Solution: adopt Dynamic Planning

1) First, number the seven points from left to right ()

2) define an array: Double M [8] [8]; // the shortest path length.

And a method for finding the two-point distance: Double D (del X [], int I, Int J)

3) M [I] [J] stores the shortest distance between vertex I and vertex J. The first statement is: M [I] [J] = m [J] [I];

Therefore, M matrix is a symmetric matrix. That is, the distance between I and j is equal to the matrix between J and I. So here we only need to ask for the Triangle Matrix M.

4 ),

  

For any point I, there are two connection methods, one is (a), I is connected to the I-1; the other is (B ), I is not connected to the I-1. (The key to thinking here)

5) the recursive formula obtained from the above ideas: (I> J is used to calculate the lower triangle)

(I = J): m [I] [J] = m [I] [J-1] + d [I] [J-1] = 0
(I> J + 1 ): M [I] [J] = m [I-1] [J] + d [I-1] [I] through the I-1 of the previous regulating point that has been obtained
(I = J + 1): m [I] [J] = min (1 <= k <j) (M [k] [J] + d [k] [I]) // select whether to connect directly or not.

// Directly connect M [I] [J] = 0 + d [I] [J];

// Otherwise: M [I] [J] = m [k] [J] + d [k] [I ];

// Adjust the point to connect

6), find the lower triangle, the node of the I-1 number is the key node.

7). The source code is as follows:
# Include <stdio. h>
# Include <math. h>
# Deprecision Max 65535

Double M [8] [8]; // Shortest Path Length
// The I j Represents the numbers of the seven vertices respectively.

Typedef struct {
Double X, Y; // y coordinate
} Del;
Double D (del X [], int I, Int J)
{
// Calculate the distance between two points. If I and j are both greater than 0, the distance between two points is returned. Otherwise, 0 is returned.
If (I> 0 & J> 0)
Return SQRT (X [I]. x-X [J]. x) * (X [I]. x-X [J]. x) + (X [I]. y-X [J]. y) * (X [I]. y-X [J]. Y ));
Else
Return 0;
}

Void short_way (del X []) // The passed coordinate Array
{
// Calculate the shortest path length
Int I, J, K;
Double W;
For (j = 0; j <= 7; j ++)
M [0] [J] = 0;
For (I = 0; I <= 7; I ++)
M [I] [0] = 0;

For (I = 1; I <= 7; I ++) // from left to right
{
For (j = 1; j <= I; j ++) // pay attention to J from bottom up <= I
{
If (I = J)
M [I] [J] = m [I] [J-1] + d (x, I, J-1 ); // M [I] [J] depends on the obtained M [I] [J-1]
// At initialization, M [1] [1] = 0; because M [1] [0] + d (x)
If (I> J + 1)
M [I] [J] = m [I-1] [J] + d (x, I-1, J ); // M [I] [J] depends on the m [I-1] [J]?

If (I = J + 1)
{
M [I] [J] = max;
If (j = 1) // I = 2
M [I] [J] = d (x, J, I );

For (k = 1; k <j; k ++) // calculates whether two adjacent points are near or after other points.
{
W = m [k] [J] + d (x, K, I );
If (W <m [I] [J])
M [I] [J] = W;
}
}
M [J] [I] = m [I] [J]; // symmetric to the upper triangle
}
}
}
Int main (){
Del X [8];
X [1]. x = 0.0;
X [1]. Y = 6.0;
X [2]. x = 1.0;
X [2]. Y = 0.0;
X [3]. x = 2.0;
X [3]. Y = 3.0;
X [4]. x = 5.0;
X [4]. Y = 4.0;
X [5]. x = 6.0;
X [5]. Y = 1.0;
X [6]. x = 7.0;
X [6]. Y = 5.0;
X [7]. x = 8.0;
X [7]. Y = 2.0;
Short_way (X );
Printf ("% F", M [7] [7]);
Return 0;
}

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.