I would suggest using constraint satisfiability toolkit to solve this problem. I'll stick with Boolean satisfiability framework, but you can look at other options, e.g. SMT, integer programming.
Let's introduce a set of Boolean variables representing each vertex in a graph:
v_0, v_1, ..., v_N
Next, let's introduce a set of Boolean variables to represent each possible edge (N^2, obviously):
e_0, e_1, ..., e_{N^2}
Check this link for details.
A Boolean variable X is True if and only if corresponding vertex/edge are present in a graph. In your case, we are talking about edges.
At this moment, you can try introducing your edge-choosing algorithm as a set of Boolean constraints:
- if vertex v_i has degree D then edge E must be present
- if an edge between v_i and v_j is present then edge E must be absent
- etc
You can rely on pseudo-boolean encodings to introduce such constraints.
If there is such assignment to input variables that resulting Boolean formula returns True (it's is satisfied) then you can iterpret this assignment as a graph that contains Hamiltonian path AND it was built by given set of rules (constraints). You can use SAT solvers (e.g. Glucose to find such assignments.
I used similar approach to solve real-world problems with 10^5 variables and 10^6 constraints. It could be pretty time and memory consuming, but it works much better than brute-force and much more flexible: you can add/remove additional constraints easily without modifying your code.
I see a few drawbacks:
- This approach might be not so scalable in your case. Results heavily depend on a problem itself, so it's difficult to predict
- It's non-trivial task to encode your graph building procedure a set of Boolean constraints.
- Essentially, SAT solvers return random assignment. If you would like to see other solution/graph, you have to add new constraints to your problem.
My algorithm might be not perfect, but if you don't know exactly how to build a result (so there is no clear algorithm to do it, except for brute-force) constraint satisfiability toolkit is very efficient approach.
EDIT:
If a graph is weighted, you still can use SAT to solve this problem, but it will be much harder: weights should be within a given range and should be discrete. Still, you can consider mixed integer programming