0

Hi there Im working on a project which needs to solve the TSP problem. The thing i need here is that how i can find the Hamiltonian circuits in the graph. In fact I know how to do this in the real world. But in the implementation and on the source code I do not know how this can be done. I have read articles on the internet which use some nested loops but i did not get what each for does and how the whole story goes on. I would be appreciating if someone can help me on this. And give me a simple example on how to implement this. I do not need a working model. Just assume that we have an array of vertices and an array of paths (by path I mean the start and end vertices of the path). How we can solve this problem.

user435245
  • 859
  • 4
  • 16
  • 28
  • I'm not sure to understand what you want but to solve the TSP in the fastest way you need to combine different algorithms like: nearest neighbor, 2-opt, 3-opt, ant algorithm, ... The problem is to combine them in the best way more then programming. – Pietro Dec 16 '10 at 13:21
  • No my problem is not finding the fast way, it is just about implementation of it. I mean by which steps I can find a cycle in a graph? Then I want to compare the cycles and see which one visits all nodes and then I want to find the minimum tour. I know it is not a good algorithm but I want to implement just this. And I do not know how the programming algorithm for finding a cycle in graph. – user435245 Dec 16 '10 at 13:25
  • You might find a graph library makes things a bit easier, so you don't have to mess with raw vertex/edge/subgraph/path representations yourself. I use http://jgrapht.org/ – andersoj Dec 16 '10 at 14:29
  • Suggest you give some example code for your "array of vertices" and "array of paths" and a small example graph. A "normal" way to represent a graph in this setting would be an adjacency matrix. If this is really a question about how to find hamiltonian cycles in a specific representation, show us the specific representation. – andersoj Dec 16 '10 at 14:33
  • @andersoj I'm assuming that by 'path', he means edge. And the data structure he has is an adjacency list. – Ian Bishop Dec 16 '10 at 16:17

1 Answers1

1

One of the more efficient ways to find an exact solution to TSP is using a dynamic programming algorithm which runs in O(n^2*2^n). It is rather simple in comparison to some of the linear programming alternatives. Search "TSP dynamic programming" and you'll surely find a lot of examples.

There are more naive approaches, such as brute force which run in O(n!). If you saw a lot of for loops (ie: more than two) this is likely the type of algorithm that you have seen before. These will get the job done (maybe not in this lifetime, depending on the size of your graph).

Ian Bishop
  • 5,185
  • 3
  • 26
  • 37
  • I think something useful shouldn't be more then O(n³) – Pietro Dec 16 '10 at 14:52
  • @Pietro He's trying to solve (possibly the most famous) NP-hard problem in Computer Science. I'm interested in how you think it should run in no more than O(n^3). – Ian Bishop Dec 16 '10 at 16:18
  • @Ian Bishop. DP may be one way to solve the TSP (an NP Hard problem). I doubt if any research has been done to establish its "best (most efficient)"-ness over other approaches...I am fairly certain branch-and-bound/cut generation for subtour elimination is likely to be competitive. – Tryer Dec 16 '10 at 17:42
  • "In general, finding a Hamiltonian cycle is NP Complete" http://mathworld.wolfram.com/HamiltonianCycle.html – andersoj Dec 16 '10 at 17:57
  • @Tryer I know a lot of exact solutions have been found recently using cutting-plane methods, I'm not sure that there are bounds for these though. Regardless, I would make the argument that given the simplicity of the algorithm, dynamic programming would likely fit better in this context. I've modified my original post to reflect your point. – Ian Bishop Dec 16 '10 at 20:04
  • @Ian Bishop, I wasn't thinking he was meaning really to solve the problem but with a pretty good approximation solution you can get n³ cost. with pretty good I mean like <1% error from the best with like 100points – Pietro Dec 17 '10 at 08:34
  • hello friends, i am trying to solve a TSP using cutting plane method for O(n^4) algorithm, but there are some results must be included and they are: -No load balancing is invoked: Traverse the tree without cutting. Thus perfect (almost)load balancing is known in advance. Verify that the efficiency tends to 100% when the size of the problem increases. -Testing the load balancing implementation: Cut off part of the tree in a controlled manner. (That is, do not cut the path of the solution). How efficient is your load balancer? – visanio_learner Mar 09 '12 at 13:16