#!/usr/bin/env python
# coding: utf-8

# Travelling Salesman Problem (TSP) : Given a set of cities and distances between every pair of cities, the problem is to find the shortest possible route that visits every city exactly once and returns to the starting point. 
# Note the difference between Hamiltonian Cycle and TSP. The Hamiltonian cycle problem is to find if there exists a tour that visits every city exactly once. Here we know that Hamiltonian Tour exists (because the graph is complete) and in fact, many such tours exist, the problem is to find a minimum weight Hamiltonian Cycle. 
# For example, consider the graph shown in the figure on the right side. A TSP tour in the graph is 1-2-4-3-1. The cost of the tour is 10+25+30+15 which is 80.
# The problem is a famous NP-hard problem. There is no polynomial-time known solution for this problem. 
#  
#  
#  
#  
#  
# Examples: 
#  
# Output of Given Graph:
# minimum weight Hamiltonian Cycle :
# 10 + 25 + 30 + 15 := 80

# Consider city 1 as the starting and ending point. Since the route is cyclic, we can consider any point as a starting point.
# Generate all (n-1)! permutations of cities.
# Calculate the cost of every permutation and keep track of minimum cost permutation.
# Return the permutation with minimum cost.

# In[1]:


# Python3 program to implement traveling salesman 
# problem using naive approach. 
 
from sys import maxsize 
 
from itertools import permutations
 
V = 4
 
# implementation of traveling Salesman Problem 
 
def travellingSalesmanProblem(graph, s): 
 
 
    # store all vertex apart from source vertex 
 
    vertex = [] 
 
    for i in range(V): 
 
        if i != s: 
 
            vertex.append(i) 
 
 
    # store minimum weight Hamiltonian Cycle 
 
    min_path = maxsize 
 
    next_permutation=permutations(vertex)
 
    for i in next_permutation:
 
 
        # store current Path weight(cost) 
 
        current_pathweight = 0
 
 
        # compute current path weight 
 
        k = s 
 
        for j in i: 
 
            current_pathweight += graph[k][j] 
 
            k = j 
 
        current_pathweight += graph[k][s] 
 
 
        # update minimum 
 
        min_path = min(min_path, current_pathweight) 
 
         
 
    return min_path 
 
 
# Driver Code 
 
if __name__ == "__main__": 
 
 
    # matrix representation of graph 
 
    graph = [[0, 10, 15, 20], [10, 0, 35, 25], 
 
            [15, 35, 0, 30], [20, 25, 30, 0]] 
 
    s = 0
 
    print(travellingSalesmanProblem(graph, s))

