473,473 Members | 1,489 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

What heuristic is the traveling salesman problem using?

6 New Member
can any one plz help me and tell wat heuristic this program of travelling salesman problem is using???


Expand|Select|Wrap|Line Numbers
  1.  
  2. /* C program to demonstrate travelling salesperson problem. */
  3.  
  4. /* About this algorithm:
  5. * Here we use dynamic programming to find a solution to the
  6. * travelling salesperson problem. The problem consists of finding
  7. * the least-cost cycle in a given set of nodes. */
  8.  
  9. #include <stdio.h>
  10. #define MAX 100
  11. #define INFINITY 999
  12.  
  13. int tsp_dp (int c[][MAX], int tour[], int start, int n);
  14.  
  15. int main()
  16. {
  17. int n; /* Number of cities. */
  18. int i, j; /* Loop counters. */
  19. int c[MAX][MAX]; /* Cost matrix. */
  20. int tour[MAX]; /* Tour matrix. */
  21. int cost; /* Least cost. */
  22.  
  23. printf ("This program demonstrates the TSP problem.");
  24. printf ("\nHow many cities to traverse? ");
  25. scanf ("%d", &n);
  26. printf ("Enter the cost matrix: (999: no connection)\n");
  27. for (i=0; i<n; i++)
  28. for (j=0; j<n; j++)
  29. scanf ("%d", &c[i][j]);
  30.  
  31. for (i=0; i<n; i++)
  32. tour[i] = i;
  33.  
  34. cost = tsp_dp (c, tour, 0, n);
  35.  
  36. printf ("Minimum cost: %d.\nTour: ", cost);
  37. for (i=0; i<n; i++)
  38. printf ("%d ", tour[i]+1);
  39. printf ("1\n");
  40. }
  41.  
  42. int tsp_dp (int c[][MAX], int tour[], int start, int n)
  43. {
  44. int i, j, k; /* Loop counters. */
  45. int temp[MAX]; /* Temporary during calculations. */
  46. int mintour[MAX]; /* Minimal tour array. */
  47. int mincost; /* Minimal cost. */
  48. int ccost; /* Current cost. */
  49.  
  50. /* End of recursion condition. */
  51. if (start == n - 2)
  52. return c[tour[n-2]][tour[n-1]] + c[tour[n-1]][0];
  53.  
  54. /* Compute the tour starting from the current city. */
  55. mincost = INFINITY;
  56. for (i = start+1; i<n; i++)
  57. { for (j=0; j<n; j++)
  58. temp[j] = tour[j];
  59.  
  60. /* Adjust positions. */
  61. temp[start+1] = tour[i];
  62. temp[i] = tour[start+1];
  63.  
  64. /* Found a better cycle? (Recurrence derivable.) */
  65. if (c[tour[start]][tour[i]] +
  66. (ccost = tsp_dp (c, temp, start+1, n)) < mincost) {
  67. mincost = c[tour[start]][tour[i]] + ccost;
  68. for (k=0; k<n; k++)
  69. mintour[k] = temp[k];
  70. }
  71. }
  72.  
  73. /* Set the minimum-tour array. */
  74. for (i=0; i<n; i++)
  75. tour[i] = mintour[i];
  76.  
  77. return mincost;
  78. }
Mar 25 '07 #1
19 15959
horace1
1,510 Recognized Expert Top Contributor
can you run the program with sample data to determine the algorithm?
Mar 25 '07 #2
rahulforfriends
6 New Member
can you run the program with sample data to determine the algorithm?
well yah i can run the program wid a sample data....
wat i am not able to determine is the heuristic of this tsp programme

i m not sure which heuristic it is following
cud u plz help me out
Mar 28 '07 #3
JosAH
11,448 Recognized Expert MVP
This recursive TSP solving implementation is not a heuristic, it's an exhaustive
search through the entire graph. Basically it runs like this:

1) given a cheapest route so far, find the cheapest path from the current
starting point (the endpoint of the route so far) to the starting point.

Although this method always finds a cheapest TSP solution, it'll take a lot
of time for substantial graphs/networks.

kind regards,

Jos
Mar 28 '07 #4
r035198x
13,262 MVP
can any one plz help me and tell wat heuristic this program of travelling salesman problem is using???


Expand|Select|Wrap|Line Numbers
  1.  
  2. /* C program to demonstrate travelling salesperson problem. */
  3.  
  4. /* About this algorithm:
  5. * Here we use dynamic programming to find a solution to the
  6. * travelling salesperson problem. The problem consists of finding
  7. * the least-cost cycle in a given set of nodes. */
  8.  
  9. #include <stdio.h>
  10. #define MAX 100
  11. #define INFINITY 999
  12.  
  13. int tsp_dp (int c[][MAX], int tour[], int start, int n);
  14.  
  15. int main()
  16. {
  17. int n; /* Number of cities. */
  18. int i, j; /* Loop counters. */
  19. int c[MAX][MAX]; /* Cost matrix. */
  20. int tour[MAX]; /* Tour matrix. */
  21. int cost; /* Least cost. */
  22.  
  23. printf ("This program demonstrates the TSP problem.");
  24. printf ("\nHow many cities to traverse? ");
  25. scanf ("%d", &n);
  26. printf ("Enter the cost matrix: (999: no connection)\n");
  27. for (i=0; i<n; i++)
  28. for (j=0; j<n; j++)
  29. scanf ("%d", &c[i][j]);
  30.  
  31. for (i=0; i<n; i++)
  32. tour[i] = i;
  33.  
  34. cost = tsp_dp (c, tour, 0, n);
  35.  
  36. printf ("Minimum cost: %d.\nTour: ", cost);
  37. for (i=0; i<n; i++)
  38. printf ("%d ", tour[i]+1);
  39. printf ("1\n");
  40. }
  41.  
  42. int tsp_dp (int c[][MAX], int tour[], int start, int n)
  43. {
  44. int i, j, k; /* Loop counters. */
  45. int temp[MAX]; /* Temporary during calculations. */
  46. int mintour[MAX]; /* Minimal tour array. */
  47. int mincost; /* Minimal cost. */
  48. int ccost; /* Current cost. */
  49.  
  50. /* End of recursion condition. */
  51. if (start == n - 2)
  52. return c[tour[n-2]][tour[n-1]] + c[tour[n-1]][0];
  53.  
  54. /* Compute the tour starting from the current city. */
  55. mincost = INFINITY;
  56. for (i = start+1; i<n; i++)
  57. { for (j=0; j<n; j++)
  58. temp[j] = tour[j];
  59.  
  60. /* Adjust positions. */
  61. temp[start+1] = tour[i];
  62. temp[i] = tour[start+1];
  63.  
  64. /* Found a better cycle? (Recurrence derivable.) */
  65. if (c[tour[start]][tour[i]] +
  66. (ccost = tsp_dp (c, temp, start+1, n)) < mincost) {
  67. mincost = c[tour[start]][tour[i]] + ccost;
  68. for (k=0; k<n; k++)
  69. mintour[k] = temp[k];
  70. }
  71. }
  72.  
  73. /* Set the minimum-tour array. */
  74. for (i=0; i<n; i++)
  75. tour[i] = mintour[i];
  76.  
  77. return mincost;
  78. }
I don't see any heuristic here. Isn't it that for the algorithm to be heuristic, it must be possible for it to run and follow two different paths given the same input parameters (follows a non-deterministic path)?
Mar 28 '07 #5
JosAH
11,448 Recognized Expert MVP
I don't see any heuristic here. Isn't it that for the algorithm to be heuristic, it must be possible for it to run and follow two different paths given the same input parameters (follows a non-deterministic path)?
Not really: a heuristic can be (and most of the time is) deterministic. The
characteristic of a heuristic is that it is fast and, more important, it finds
a solution not worse than so many percent from an optimal solution.

Some definitions are even weaker: the heuristic should just produce an
'acceptable' solution. For a TSP a 2-opt or 3-opt are cheap heuristics.
A better heuristic is the Lin-Kernighan opt method. All methods can fail
miserably for some graphs (but that's why they're called a heuristic ;-)

kind regards,

Jos
Mar 28 '07 #6
r035198x
13,262 MVP
Not really: a heuristic can be (and most of the time is) deterministic. The
characteristic of a heuristic is that it is fast and, more important, it finds
a solution not worse than so many percent from an optimal solution.

Some definitions are even weaker: the heuristic should just produce an
'acceptable' solution. For a TSP a 2-opt or 3-opt are cheap heuristics.
A better heuristic is the Lin-Kernighan opt method. All methods can fail
miserably for some graphs (but that's why they're called a heuristic ;-)

kind regards,

Jos
Qoute=Wiki
..there is no proof the solutions could not get arbitrarily bad; or it usually runs reasonably quickly, but there is no argument that this will always be the case.


So the non-determinism is in the frequency of getting a solution efficiently or am I still missing it?
Mar 28 '07 #7
JosAH
11,448 Recognized Expert MVP
Qoute=Wiki
..there is no proof the solutions could not get arbitrarily bad; or it usually runs reasonably quickly, but there is no argument that this will always be the case.


So the non-determinism is in the frequency of getting a solution efficiently or am I still missing it?
You wrote: for identical input different results might be obtained, which is not
true per se; i.e. heuristics can be, and most of the time are deterministic
processes.

A heuristic simply applies a (slightly) incorrect rule and tries to find a "good"
(mind the quotes) solution to a problem quickly. Of course it's beneficial
of the heuristic itself could signal that it failed somehow. What wiki said:
"no guarantee that a good solution was found" is true for most heuristics.

kind regards,

Jos
Mar 28 '07 #8
r035198x
13,262 MVP
You wrote: for identical input different results might be obtained, which is not
true per se; i.e. heuristics can be, and most of the time are deterministic
processes.

...
Ah, now here is something.

I actually said a different path could be taken meaning path taken at arriving at the result. But in this case the path taken is actually the result! So I guess I could have clouded things a bit. I do get your explanation though its quite nice.

Here is another dfn


Quote = What is
As an adjective, heuristic (pronounced hyu-RIS-tik and from the Greek "heuriskein" meaning "to discover") pertains to the process of gaining knowledge or some desired result by intelligent guesswork rather than by following some preestablished formula. (Heuristic can be contrasted with algorithmic.)


Which is where I got the "different path at arriving at the solution" idea since I saw that guesswork can be used for determining the next step.
Mar 28 '07 #9
JosAH
11,448 Recognized Expert MVP
Which is where I got the "different path at arriving at the solution" idea since I saw that guesswork can be used for determining the next step.
Ah, yes; but most of the time that "guesswork" will be a deterministic process.
There *do* exist (pseudo) non-deterministic search algorithms though;
simulated annealing comes to mind where a current solution is just messed up
a bit in order to be able to continue the search for an optimum from another
"starting point". The worse (or more complicated) those search spaces are,
the more those non-deterministic heuristics come in and people find fairly
good solutions just by accident and a lot of brute force ;-)

kind regards,

Jos
Mar 28 '07 #10
r035198x
13,262 MVP
Ah, yes; but most of the time that "guesswork" will be a deterministic process.
There *do* exist (pseudo) non-deterministic search algorithms though;
simulated annealing comes to mind where a current solution is just messed up
a bit in order to be able to continue the search for an optimum from another
"starting point". The worse (or more complicated) those search spaces are,
the more those non-deterministic heuristics come in and people find fairly
good solutions just by accident and a lot of brute force ;-)

kind regards,

Jos
I see. I never really grasped the concept behind simulated annealing and its cooling metal analogy. Maybe one day I'll ask you about it.
Mar 28 '07 #11
rahulforfriends
6 New Member
so cud u plz give me a program i mean a c++ or c program on tsp tat uses 2-opt or 3-opt or ne other better heuristic to solve the tsp
Mar 29 '07 #12
r035198x
13,262 MVP
so cud u plz give me a program i mean a c++ or c program on tsp tat uses 2-opt or 3-opt or ne other better heuristic to solve the tsp
We don't give out code. We assist you to write correct code.
Mar 29 '07 #13
rahulforfriends
6 New Member
ok thn plz help me out to write the code
i mean i m just not getting hoiw to start the code
i hv found the cost matrix now how shall i proceed further
Mar 29 '07 #14
JosAH
11,448 Recognized Expert MVP
I see. I never really grasped the concept behind simulated annealing and its cooling metal analogy. Maybe one day I'll ask you about it.
Wiki has a nice page about simulated annealing.

kind regards,

Jos
Mar 29 '07 #15
JosAH
11,448 Recognized Expert MVP
ok thn plz help me out to write the code
i mean i m just not getting hoiw to start the code
i hv found the cost matrix now how shall i proceed further
Start off easy, build a 2Opt heuristic. Given a route, make two cuts in it and
reconnect the two pieces the other way around. This can only be done if for
an edge e_ij == v_i ---> v_j the edge v_j ---> v_i is also a possibility.

Conceptually you can get around this restriction by giving an (impossible)
edge v_j ---> v_i an absurdly high cost factor in your cost matrix.

Only take 'downhill' steps, i.e. the new route must be cheaper than the previous
one. If no cheaper route can be found, stop.

There are O(n^2) different possibilities per iteration where n is the number of
vertexes in the route . Good luck and

kind regards,

Jos
Mar 29 '07 #16
rahulforfriends
6 New Member
oh its a little complicated..i wish u cud give me the code....
i mean the heuristic is the initial step for my program..plz help me to get obver it...or else just tel me wat exactly is happeneing in the program tat i wrote...the one using dynamic algortihm
atleast u can help me tat way plzzz
Mar 29 '07 #17
JosAH
11,448 Recognized Expert MVP
oh its a little complicated..i wish u cud give me the code....
i mean the heuristic is the initial step for my program..plz help me to get obver it...or else just tel me wat exactly is happeneing in the program tat i wrote...the one using dynamic algortihm
atleast u can help me tat way plzzz
Your program isn't a heuristic method; it's a full search through the entire
graph/network. The search is implemented recursively. Basically the recurrence
relation is as follows:

c_0i + cost_of_route(v_i, v_0)

where c_0i is the cost for traveling from node 0 (the start node) to node i
in one step. The cost_of_route() method finds the minimum cost to travel
to all nodes, starting at node i and ending at node 0 again.

Try it by hand on a small (say four vertexes) graph and you'll see how the
recursive mechanism works. (hint: it takes six steps in total so its doable
to find the best route by hand).

Let the nodes/vertexes be named 0, 1, 2 and 3. Starting at 0 you can travel
to 1, 2 or 3. So the first step is either:

01
02
03

given the first step you have to find the minimum routes for all the possibilites

for 01:

1230
1320

for 02:

2130
2310

for 03:

3120
3210

kind regards,

Jos
Mar 29 '07 #18
rahulforfriends
6 New Member
hi,
my guide is not accepting this program he want me to use 3-opt or 2-opt to write this program plz send me the program i dn't hv time or else i wud had tried to write the program on my own
help me out plz
Mar 31 '07 #19
r035198x
13,262 MVP
hi,
my guide is not accepting this program he want me to use 3-opt or 2-opt to write this program plz send me the program i dn't hv time or else i wud had tried to write the program on my own
help me out plz
We don't do student's assignments.

See the guidelines
Mar 31 '07 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Mike Curry | last post by:
Just thought I'd post a few links to what I've done to share with others (not that its anything great, but I think starting a thread like this would be a good way for others to see what most of us...
21
by: Alvin Bruney | last post by:
I'm looking at this guy's resume to call him in for an interview and he lists VisualStudio.Net 2003 5 years. C'mon. Is that really possible? He hasn't worked or consulted for MS either.
4
by: Mike Curry | last post by:
Just thought I'd post a few links to what I've done to share with others (not that its anything great, but I think starting a thread like this would be a good way for others to see what most of us...
8
by: werner | last post by:
Hi! I don't want to use eval() in order to parse a user-supplied formula. What alternatives do I have? PHP has no standard functionality for tokenizing or parsing expressions in this regard. ...
5
by: garyusenet | last post by:
Hi all, Im in the process of writing a simple application. I have a form which has on it five text boxes. I would like to store information that is entered into these text boxes in a small...
4
by: N.RATNAKAR | last post by:
hai, what is abstract class and abstract method
2
by: PoN | last post by:
Hello everyone.. i have to write codes in VB to solve the travelling salesman problem but i dun know how to start. So if ANYONE knows the codes, pls post it here. Or if there is a previous post on...
3
by: 9966 | last post by:
Well previously I've asked a lot of question regarding Travelling Salesman Problem and I got a lot of helpful feedback from here. Thanks again for helping me. Now I've completed the full code....
4
amitpatel66
by: amitpatel66 | last post by:
In real time applications, there is always a requirement to generate the data selected from database in XML format. using Oracle, generating XML Structured data becomes easier using the inbuilt...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.