home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!spool.mu.edu!olivea!charnel!sifon!thunder.mcrcim.mcgill.edu!mouse
- From: mouse@thunder.mcrcim.mcgill.edu (der Mouse)
- Newsgroups: comp.lang.c
- Subject: Re: travelling salesman
- Message-ID: <1992Dec12.231813.1257@thunder.mcrcim.mcgill.edu>
- Date: 12 Dec 92 23:18:13 GMT
- References: <FARID.92Dec11140244@gradient.cis.upenn.edu>
- Organization: McGill Research Centre for Intelligent Machines
- Lines: 39
-
- In article <FARID.92Dec11140244@gradient.cis.upenn.edu>, farid@gradient.cis.upenn.edu (Hany Farid) writes:
-
- > I am looking for code that enumerates the (N-1)!/2 possible paths of
- > the travelling salesman for a map with N cities.
-
- This really isn't a C question; it's a general algorithms question.
- However, since the code is trivial and it's likely to be the easiest
- and quickest way to get this thread off of comp.lang.c,
-
- int cities[N]; /* holds city numbers, 0..N-1 */
- int taken[N]; /* holds booleans */
-
- void enumerate(int depth)
- {
- if (depth < 0)
- { /* we have a new path in cities, do something with it */
- }
- else
- { for (cities[depth]=0;cities[depth]<N;cities[depth]++)
- { if (!taken[cities[depth]])
- { taken[citites[depth]] = 1;
- enumerate(depth-1);
- taken[citites[depth]] = 0;
- }
- }
- }
- }
- ....
- ...enumerate(N-1)...
-
- You can of course eliminate the recursion, but there's not much point;
- you recurse only N levels, and if N is large enough that this is likely
- to cause problems, you aren't going to finish the list before your
- computer crumbles into dust from age anyway.
-
- der Mouse
-
- mouse@larry.mcrcim.mcgill.edu
-