Salesman Program
Salesman Program
def tsp_dp(graph):
"""
Solves the Traveling Salesman Problem using Dynamic Programming (Held-Karp Algorithm).
:param graph: 2D list (adjacency matrix) representing distances between cities.
:return: Tuple (minimum tour cost, optimal tour path).
"""
n = len(graph)
memo = {}
parent = {} # Dictionary to store the best path choices
min_cost = sys.maxsize
best_city = -1
# Solve TSP
min_cost, optimal_path = tsp_dp(graph)
# Output results