Blog # 05 : Solving Traveling Salesman Problem
Program to solve Traveling Salesman Problem
/*
+-------Program to solve traveling salesman problem-----------------------+
| |
| efficiency: O(n^4) |
| optimality: I think yes!!!!!! |
| but still under test for different graphs |
| so PLEASE:::send me the test cases along with the optimal solution |
| where my algorithm fails to give the optimal solution. |
| Free to use and/or distribute for non-commercial purposes. |
+------------------------------------------------------------------------------------+
*/
/*
example:
2
[0]-------[1]
| \4 /|
| \ / |
3| /\ |3
| 6/ \ |
[3]-------[2]
1
inputs:
infinity:999
no. of cities: 4
no. of paths:6
S D Dist
path0:0 1 2
path0:0 2 4
path0:0 3 3
path0:1 2 3
path0:1 3 6
path0:2 3 1
*/
#include<stdio.h>
#include<conio.h>
#define ALL -1
#define MAXCITIES 10
enum BOOL{FALSE,TRUE};
long*visited;//visited nodes set here
long*min_circuit;//min inner circuit for given node as start node at position indexed 0
long*ham_circuit;//optimal circuit with length stored at position indexed 0
long min_circuit_length;//min circuit length for given start node
int n;//city count
long matrix[MAXCITIES][MAXCITIES];//non directional n*n symmetric matrix
//to store path distances as source*destination
long INFI; // INFINITY value to be defined by user
// function resets minimum circuit for a given start node
//with setting its id at index 0 and setting further node ids to -1
void reset_min_circuit(int s_v_id)
{
min_circuit[0]=s_v_id;
for(int i=1;i<n;i++) min_circuit[i]=-1;
}
// marks given node id with given flag
// if id==ALL it marks all nodes with given flag
void set_visited(int v_id,BOOL flag)
{
if(v_id==ALL) for(int i=0;i<n;i++) visited[i]=flag;
else visited[v_id]=flag;
}
// function sets Hamiltonian circuit for a given path length
//with setting it at index 0 and setting further nodes from current min_circuit
void SET_HAM_CKT(long pl)
{
ham_circuit[0]=pl;
for(int i=0;i<n;i++) ham_circuit[i+1]=min_circuit[i];
ham_circuit[n+1]=min_circuit[0];
}
+-------Program to solve traveling salesman problem-----------------------+
| |
| efficiency: O(n^4) |
| optimality: I think yes!!!!!! |
| but still under test for different graphs |
| so PLEASE:::send me the test cases along with the optimal solution |
| where my algorithm fails to give the optimal solution. |
| Free to use and/or distribute for non-commercial purposes. |
+------------------------------------------------------------------------------------+
*/
/*
example:
2
[0]-------[1]
| \4 /|
| \ / |
3| /\ |3
| 6/ \ |
[3]-------[2]
1
inputs:
infinity:999
no. of cities: 4
no. of paths:6
S D Dist
path0:0 1 2
path0:0 2 4
path0:0 3 3
path0:1 2 3
path0:1 3 6
path0:2 3 1
*/
#include<stdio.h>
#include<conio.h>
#define ALL -1
#define MAXCITIES 10
enum BOOL{FALSE,TRUE};
long*visited;//visited nodes set here
long*min_circuit;//min inner circuit for given node as start node at position indexed 0
long*ham_circuit;//optimal circuit with length stored at position indexed 0
long min_circuit_length;//min circuit length for given start node
int n;//city count
long matrix[MAXCITIES][MAXCITIES];//non directional n*n symmetric matrix
//to store path distances as source*destination
long INFI; // INFINITY value to be defined by user
// function resets minimum circuit for a given start node
//with setting its id at index 0 and setting further node ids to -1
void reset_min_circuit(int s_v_id)
{
min_circuit[0]=s_v_id;
for(int i=1;i<n;i++) min_circuit[i]=-1;
}
// marks given node id with given flag
// if id==ALL it marks all nodes with given flag
void set_visited(int v_id,BOOL flag)
{
if(v_id==ALL) for(int i=0;i<n;i++) visited[i]=flag;
else visited[v_id]=flag;
}
// function sets Hamiltonian circuit for a given path length
//with setting it at index 0 and setting further nodes from current min_circuit
void SET_HAM_CKT(long pl)
{
ham_circuit[0]=pl;
for(int i=0;i<n;i++) ham_circuit[i+1]=min_circuit[i];
ham_circuit[n+1]=min_circuit[0];
}