AI Lab Program 10
AI Lab Program 10
Code –
V=4
vertex = []
for i in range(V):
if i != s:
vertex.append(i)
min_path = maxsize
while True:
current_pathweight = 0
k=s
for i in range(len(vertex)):
current_pathweight += graph[k][vertex[i]]
k = vertex[i]
current_pathweight += graph[k][s]
if not next_permutation(vertex):
break
return min_path
def next_permutation(L):
n = len(L)
i=n-2
i -= 1
if i == -1:
return False
j=i+1
j += 1
j -= 1
left = i + 1
right = n - 1
left += 1
right -= 1
return True
if __name__ == "__main__":
s=0
print("the graph is : ", graph)
print(travellingSalesmanProblem(graph, s))
Output –
The graph is: [[0, 10, 15, 20], [10, 0, 35, 25], [15, 35, 0, 30], [20, 25, 30, 0]]