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.
- Dead nodes i.e. they have no edge connections
- 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?