3

I have been playing with Hamiltonian paths for some time and have found some cool uses of it. One being a sort of puzzle where the goal is to connect all the nodes to form a hamiltonian path.

So, being a novice programmer I am, I created a very basic brute force graph generator that creates graphs having a hamiltonian path. But the problem arises when I try to increase the graph size to 10x10. Needless to say, by brute force does not works there.

I understand Hamiltonian path is an NP-Complete problem, but is there a method to optimize the graph generation process. Any sort of trick that can create 15x15 grids in reasonable time.

Thank you very much.

  • So having a path (say, `3 -> 1 -> 2 -> 4`) you want to create a graph (e.g. `3 -> 1`, `1 -> 2`, `2 -> 4`, `3 -> 4`, the last edge is not mandatory one)? If it is, why not take vertexes in arbitrary order (shuffle them), declare them to be on the path, create corresponding edges (path) and then add arbitrary edges (that are on on the path)? – Dmitry Bychenko Jan 20 '17 at 10:19
  • Generating graphs that contain a Hamiltonian path is trivial and not at all related to the NP-Completeness of taking an arbitrary graph and determining whether or not it has a Hamiltonian path. – harold Jan 20 '17 at 10:36
  • 1
    @harold: S/he presumably wants to generate a *random-looking* HP *in a grid*. If we drop either "random-looking" requirement or the "in a grid" requirement, yes, it's trivial, but with both in place I don't see an obvious algorithm. – j_random_hacker Jan 20 '17 at 14:16
  • I think this is a good question. I don't see the reason for close votes. It is not homework like question. – Saeed Amiri Jan 20 '17 at 23:20
  • @j_random_hacker well it looks like you have a point but that whole grid thing means absolutely nothing to me. – harold Jan 21 '17 at 09:53
  • @harold: Do you mean it's not clear? – j_random_hacker Jan 21 '17 at 11:50
  • @SaeedAmiri Thank you. I actually developed my own naive algorithm to generate path. I take a random seed and place few block nodes on the grid and then run a brute force dfs on the graph to detect if a valid path which traverses all the connectable nodes exists. Problem arose when the grid size was increased to 10x10. To overcome the large size limitation, I created small grid with valid path and then stitch them together. I only ask if there is more clever way of doing this. – Abhinav Bhadoria Jan 24 '17 at 10:06
  • I think you are already doing well and this is a somehow technique which is based on hamiltonian separators – Saeed Amiri Jan 24 '17 at 13:51

1 Answers1

3

You're looking for what's called the "Backbite algorithm" - start with any Hamiltonian path, a trivial one will do (zig-zag back and forth across your grid, or have a spiral).

Then loop a random number of times:

  1. Select either end point

  2. There are at least two and at most four neighboring vertices; randomly select one that is not already the Hamiltonian neighbor of the end point you started with

  3. If the second point was not the other endpoint

    a. connect the two points you picked - this will produce a graph that looks like a loop with a tail

    b. Find the edge that causes the loop (not the edge you just added, rather one of its neighbors), and delete it (this is the 'backbite' step)

  4. If in step 3 the second point was the other endpoint, join the two (you will now have a Hamiltonian cycle), and delete any other random edge

At each step, the new graph is still a Hamiltonian path.