Click here to Skip to main content
6,595,444 members and growing! (20,221 online)
Email Password   helpLost your password?
General Programming » Algorithms & Recipes » Algorithms     Intermediate

Solution to Travelling Salesman Problem

By omkar joshi

Solution to a Travelling Salesman problem using Hamiltonian circuit, the efficieny is O(n^4) and I think it gives the optimal solution.
C++/CLI, VC6, .NET, Win2K, WinXP, Dev
Posted:22 Jan 2005
Views:50,122
Bookmarked:26 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
15 votes for this article.
Popularity: 2.15 Rating: 1.83 out of 5
8 votes, 53.3%
1
1 vote, 6.7%
2
2 votes, 13.3%
3
1 vote, 6.7%
4
3 votes, 20.0%
5

Introduction

Hereby, I am giving a program to find a solution to a Traveling Salesman Problem using Hamiltonian circuit, the efficiency is O (n^4) and I think it gives the optimal solution. Though I have provided enough comments in the code itself so that one can understand the algorithm that I m following, here I give the pseudocode.

Problem Statement

Find the order of cities in which a salesman should travel in order to start from a city, reaching back the same city by visiting all rest of the cities each only once and traveling minimum distance for the same.

ALT statement: Find a Hamiltonian circuit with minimum circuit length for the given graph.

Notations

G: Symmetric weighted undirectional graph with,

  • Number of vertices = No. of cities
  • Number of links = Number of paths
  • Weight of the link = Distance of the path.

Algorithm

MIN_CKT_LENGTH=INFINITY
for each vertex V in the graph G
  for each vertex V_N in the graph G such that V and V_N are different
   mark all vertices unvisited
   mark V as visited 
     for staring vertex as V and succeeding vertex as V_N, find circuit 
          such that path staring from V_N in that circut yeilds minimum 
          pathlength from V_N for all unvisited vertices by 
          visiting each vertex.( this path is obtained by greedy method).
       if currently obtained circuit length <= MIN_CKT_LENGTH then
         set MIN_CKT_LENGTH=newly obtained value
         copy the new circuit as hamiltonion circuit
       end if
     end for V_N
   end for V

code segment:

//for each vertex, S_V as a staring node


for(int S_V_id=0;S_V_id<n;S_V_id++)

{ 
      //for each and non start vertex as i

      for(i=0;i<n;i++)
      { 
          //set all to unvisited

          set_visited(ALL,FALSE);
          // set staring vertex as visited

          set_visited(S_V_id,TRUE);
          //reset/init minimum circuit

          reset_min_circuit(S_V_id);
          // obtain circuit for combination of S_V and i

          new_circuit_length=get_valid_circuit(S_V_id,i);
          // if newer length is less than the previously

          //calculated min then set it as min and set the

          //current circuit in hamiltonion circuit

          if(new_circuit_length<=min_circuit_length)
                SET_HAM_CKT(min_circuit_length=new_circuit_length);
       }

}

Example

Inputs

infinity: 999
no. of cities: 4
no. of paths: 6

 S D Dist
path0: 0 1 2
path1: 0 2 4
path2: 0 3 3
path3: 1 2 3
path4: 1 3 6
path5: 2 3 1

Algorithm proceeds as follows:

V V_N V_N-path ckt_length, ckt
(started with INFI,-)
MIN_CKT_LENGTH, HAM_CKT
(started with INFI,-)
0 1 1-2-3 9,0-1-2-3-0* 9,0-1-2-3-0
  2 2-3-1 13,0-2-3-1-0 9,0-1-2-3-0
  3 3-2-1 9,0-3-2-1-0* 9,0-3-2-1-0
1 0 0-3-2 9,1-0-3-2-1* 9,1-0-3-2-1
  2 2-3-0 9,1-2-3-0-1* 9,1-2-3-0-1
  3 3-2-0 13,1-3-2-0-1 9,1-2-3-0-1
2 0 0-1-3 13,2-0-1-3-2 9,1-2-3-0-1
  1 1-0-3 9,2-1-0-3-2* 9,2-1-0-3-2
  3 3-0-1 9,2-3-0-1-2* 9,2-3-0-1-2
3 0 0-1-2 9,3-0-1-2-3* 9,3-0-1-2-3
  1 1-0-2 13,3-1-0-2-3 9,3-0-1-2-3
  2 2-1-0 9,3-2-1-0-3* 9,3-2-1-0-3

Comments

This might be the overrun for lower values of vertex count but for large values of n, it's surely less than the exhaustive search as n increases n^4 < n! for n>6.

The optimality is the issue with this problem and as far as I am concerned, I think this algorithm yields the optimal Hamiltonian circuit if it exists at all.

Hereby I ask you people to evaluate my algorithm for different sets of test cases. In case of failure, please inform me with the set of input values, answer found by my code and the actual optimal answer.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

omkar joshi


Member
Engg Student
Occupation: Engineer
Location: India India

Other popular Algorithms & Recipes articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
GeneralAlgorithm is not optimal PinmemberKHDev4u13:20 10 Mar '06  
GeneralRe: Algorithm is not optimal Pinmemberomkar joshi1:00 11 Mar '06  
GeneralP = NP? PinmemberCap'n Code7:21 22 Jan '05  
GeneralRe: P = NP? PinmemberRui A. Rebelo8:39 22 Jan '05  
GeneralRe: P = NP? Pinmemberomkar joshi22:03 22 Jan '05  
GeneralRe: P = NP? Pinmemberomkar joshi22:03 22 Jan '05  
GeneralRe: P = NP? Pinmemberjbstjohn6:43 24 Jun '05  
GeneralRe: P = NP? Pinmembercomputerguru923828:23 1 Jul '05  
GeneralRe: P = NP? PinsussAnonymous11:21 29 Aug '05  
GeneralRe: P = NP? Pinmemberomkar joshi2:54 24 Jan '06  
GeneralRe: P = NP? Pinmemberomkar joshi2:27 24 Jan '06  
GeneralRe: P = NP? Pinmemberomkar joshi2:43 24 Jan '06  
GeneralRe: P = NP? Pinmemberjbstjohn5:16 9 Feb '06  
GeneralRe: P = NP? Pinmembervishal_chauhan4:26 8 May '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 Jan 2005
Editor: Sumalatha K.R.
Copyright 2005 by omkar joshi
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project