I have been developing a Python code to compute all the Hamilton cycles for a system but excluding those that have a maximum distance d between vertices. Hence, for d=1 i would only have a single permutation, but for d>1 it becomes more size-dependant. However, I lack a bit on mathematical background. Is there a way to know how many cycles will the system have for a given d? Separately, I made a similar code that does not take into account those cycles with an even relation between two vertices. Can any variation of the previous formula be derived to this case? Lastly, is there a way to know the sole contribution of only those cycles with a vertix relation of d?
The code I have developed for the computation of all the cycles until a distance d is as follows:
def hamilton_cycles_dfs(arr, d):
n = len(arr)
def dfs(path):
if len(path) == n and (min(abs(path[0] - path[-1]), n - abs(path[0] - path[-1])) <= d):
cycles.append(path)
for v in range(n):
if v not in path and (min(abs(v - path[-1]), n - abs(v - path[-1])) <= d):
dfs(path + [v])
cycles = []
dfs([0])
return cycles
And the adaptation to exclude those that contain even distances between vertices is as follows:
def hamilton_cycles_dfs(n, d):
import math
def dfs(path):
if len(path) == n :
val=min(abs(path[0] - path[-1]), n - abs(path[0] - path[-1]))
if val <=d and val%2 !=0:
cycles.append(path)
for v in range(n):
if v not in path :
val=min(abs(v - path[-1]), n - abs(v - path[-1]))
if val <=d and val%2 !=0 :
dfs(path + [v])
cycles = []
dfs([0])
return cycles
Thank you so much.