Generalized salesman stretcher problem (TSP)-permitted Directed Graph

Source: Internet
Author: User

We all know the famous goods man stretcher problem. Now we need to solve it. There are two ways
Method 1: The brute-force enumeration method is used to list all paths. This method is the simplest, but N is required! When N is large, it is completely non-computational. Of course, n is generated! It is easy to arrange and does not require any high-end skills. This solution is not explained here

Method 2: Dynamic Planning. t (Vi, v) indicates the shortest path value from V1 to all nodes in V, so we have the following transfer equation.

T (Vi, v) = min {d (K, I) + T (VK, V \ {VK} Where VK is an element in V, where D (K, I) the distance from the k-th node to the I-th node (an infinite number is allowed ). The problem to be solved is T (V1, V \ {v1 }). ---- that is, we get out of V1, traverse the nodes except V1 in V, and return to V1. according to this principle, we use binary bits to represent the fetching and not obtaining of each element. The Code is as follows:

// Author: sy1206321 # include <iostream> Using STD: Endl; Using STD: CIN; Using STD: cout; unsignedintinodecount; // number of nodes unsignedint ** lpdparray; // The array unsignedint ** lpgraph during dynamic planning; // bool ** bhascaculate for storing graph data; // whether the unsignedint ** lppath is accessed by storing a path; // stores the Shortest Path boolfreeresource (); // free memoryboolinitdata (); // init graph datavoidsearch (); // searches for the shortest path vertex (unsignedint inode, unsigned intinodeset ); voidshowpath ();/ /Display one of the paths int main () {initdata (); search (); cout <lpdparray [1] [(1 <inodecount)-2] <Endl; showpath (); freeresource (); System ("pause");} boolfreeresource () {// free memoryfor (INT iindex = 0; iindex <= inodecount; ++ iindex) {Delete [] lpdparray [iindex]; Delete [] bhascaculate [iindex]; Delete [] lpgraph [iindex]; Delete [] lppath [iindex];} Delete [] lpdparray; delete [] bhascaculate; Delete [] lpgraph; Delete [] lppath; returntrue;} boolin Itdata () {// allocate memory and init datacout <"Enter the number of nodes:" <Endl; CIN> inodecount; lpdparray = new unsigned int * [inodecount + 1]; lpdparray [0] = NULL; bhascaculate = newbool * [inodecount + 1]; bhascaculate [0] = NULL; lpgraph = newunsigned int * [inodecount + 1]; lpgraph [0] = NULL; lppath = newunsigned int * [inodecount + 1]; lppath [0] = NULL; for (INT iindex = 1; iindex <= inodecount; ++ iindex) {lpdparray [iindex] = newunsigned int [1 <inode Count]; bhascaculate [iindex] = newbool [1 <inodecount]; lpgraph [iindex] = newunsigned int [inodecount + 1]; lppath [iindex] = newunsigned int [1 <inodecount];} cout <"Enter the graph data. If not, enter 0" <Endl; // read the graph data. If the data does not exist, it is expressed infinitely (static_cast <int> (-1) for (unsigned int irow = 1; irow <= inodecount; ++ irow) {for (unsigned int icol = 1; icol <= inodecount; ++ icol) {CIN> lpgraph [irow] [icol]; If (! Lpgraph [irow] [icol]) {lpgraph [irow] [icol] = static_cast <unsigned int> (-1) ;}}// put bhascaculate, lpdparray, all lppath arrays are cleared for (unsigned int irow = 1; irow <= inodecount; ++ irow) {for (unsigned int icol = 1; icol <(1 <inodecount ); ++ icol) {bhascaculate [irow] [icol] = false; lpdparray [irow] [icol] = static_cast <unsigned int> (-1 ); lppath [irow] [icol] = 0 ;}} returntrue ;} // ================================================ ================================ ====================/// Lpdparray [inode] [inodeset] indicates from node (1) to node (inode) the minimum value of the vertex in the inodeset set /// ============================== ========================================================== === voidsearch () {// apparently, when inodeset is 0, it indicates an empty set for (INT inode = 1; inode <= inodecount; ++ inode) {lpdparray [inode] [0] = lpgraph [1] [inode]; lppath [inode] [0] = 1; bhascaculate [inode] [0] = true ;} lpdparray [1] [(1 <inodecount)-2] = getminpath (1, (1 <inodecount)-2);} unsig Nedintgetminpath (unsignedint inode, unsigned intinodeset) {If (bhascaculate [inode] [inodeset]) {returnlpdparray [inode] [inodeset];} unsigned int iminvalue = static_cast <int> (-1); For (INT iprenode = 1; iprenode <= inodecount; ++ iprenode) {If (1 <(iPreNode-1) & inodeset) & (lpgraph [iprenode] [inode]! =-1) {// iprenode is a ELEM of inodesetunsigned int iprevalue = getminpath (iprenode, inodeset &(~ (1 <(iPreNode-1); If (iprevalue! =-1) & iprevalue + lpgraph [iprenode] [inode] <iminvalue) {// update valuelppath [inode] [inodeset] = iprenode; iminvalue = iprevalue + lpgraph [iprenode] [inode] ;}} lpdparray [inode] [inodeset] = iminvalue; bhascaculate [inode] [inodeset] = true; returnlpdparray [inode] [inodeset];} voidshowpath () {int * Path = newint [inodecount + 1]; intivalue = (1 <inodecount)-1; intiprenode = 1; intinodeindex = 0; while (ivalue) {ivalue-= (1 <(iPreNode-1) ); Path [inodecount-inodeindex] = iprenode; ++ inodeindex; iprenode = lppath [iprenode] [ivalue];} path [0] = 1; for (inodeindex = 0; inodeindex <= inodecount; ++ inodeindex) {cout <path [inodeindex] <(inodeindex = inodecount? "\ N": "->");} Delete [] path ;}

We did not fully adopt dynamic planning, but adopted a variant of it-Memorandum, which records small results that have been obtained and will be reused, the next time you solve this small problem, you can directly look up the table, instead of asking all the small problems to be solved when solving a big problem like dynamic planning.

It is easy to find that the time complexity of the algorithm is O (n * 2 ^ N) space. Obviously, the time is higher than n! Faster than n! We know n from the sterling formula! = (N/E) ^ N * (2 * pI * n) ^ 0.5, obviously much larger than 2 ^ N * n

Here, the group of input values is:

6 (6 vertices)

0 10 20 30 40 50
12 0 18 30 25 21
23 19 0 5 10 15
34 32 4 0 8 16
45 27 11 10 0 18
56 22 16 20 12 0

The preceding is the edge weight (directed graph)

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.