1

I am looking for some advice /guidance in the right direction.

My requirement is to generate a graph rather than solving one for solution.

I am looking to implement an algorithm to generates a graph(NxN grid) with only 1 hamiltonian cycle. Note that only one unique solution is of key importance. The graph will be a NxN grid of nodes with each having just 4 neighbouring nodes i.e top, right, bottom, left. Nodes can be visited only once. Apart of this, there can be some special nodes.

  1. Dead nodes i.e. they have no edge connections
  2. Fixed entry and exit node i.e entry and exit nodes are already defined and no other node can connect with the given node. It can be a. adjacent nodes b. straight nodes

Some examples:

@ - means that nodes can be connected with adjacent nodes (top,right,bottom,left)
$ - indicated dead end (no connections with adjacent nodes)

Graph 1 =>
@-@-@
@-$-@
@-@-@

Solution 1 =>
1-2-3
8-$-4
7-6-5

here the solution of the graph is 1->2->3->4->5->6->7->8->1. Notice how the $ node was not included in the final solution.

My Approach:

I take a n*n grid and start by placing random dead nodes on the graph. After this, I place random special nodes. I then run a dfs search that traverses the entire grid to see is a valid cycle presents that fulfills the special node criteria and visits every node only once (except for the start node) and end at the start node, making it a complete loop.

My Questions:

What I am asking here is how do I ensure that the graph has only 1 valid cycle. One thing I can do it iterate on the level recursively by adding more dead nodes and special nodes after every cycle, till the number of valid Hamiltonian cycle decreases to one. This is what I am planning to implement.

How would you ideally approach this problem?

  • Could you add an example of a grid? I don't get it from your description. – CaptainTrunky Jun 22 '17 at 09:18
  • @CaptainTrunky added a small example to just show the basic graph with regular and dead nodes. Special nodes can be added later (all they do is restrict the connection with adjacent nodes). So 1 special node can force that it can only be connected from top and left node, leaving the left and bottom out of consideration. This will help make sure graph has unique solution. – Abhinav Bhadoria Jun 22 '17 at 10:19

2 Answers2

0

First I'd like to mention that I've not yet found a complete solution.

What I would do is generate a cycle in the grid and store this "solution", then add a dead end on all missed square which makes the generated cycle hamiltonian. then I would follow your approuch by iteratively adding "forced edges" and checking if there exists a second (read: "more than one") hamiltonian cycle.

this check can be formulated as the folowing question: "how do you check if a given planar graph contains more than one hamiltonian cycle if you know one?".

the reason why I use "planar" is easily explained. Your starting grid is planar and removing nodes or forced edges doesn't make it nonplanar. This is because a forced edge A-B can be transformed in A-X-B where X is a new node and thus needs to be visited by any hamiltonian cycle which results in the forced edge being visited.

One way I tried to transform one hamiltonian cycle in another is listed below:

If you take two planar hamiltonian cycles and take all the edges where they don't match, these form a cycle (which may visit nodes more than once). This cycle has the property that the edges alternate between being in one hamiltonian cycle and being in the other. I couldn't find a way to reverse this process.

0

Why not just connect the outer nodes to form a circle, and label all inside nodes as "dead nodes"?

For example, borrowing your notation from above...

3x3 Graph

@@@
@$@
@@@

4x4 Graph

@@@@
@$$@
@$$@
@@@@

5x5 Graph

@@@@@
@$$$@
@$$$@
@$$$@
@@@@@

There will always be exactly one solution, and it's easy to generate graphs of arbitrary size.

TimD1
  • 982
  • 15
  • 26