DIZNR INTERNATIONAL

Traveling Salesman Problem Using Dynamic Programming With Practical Example

Traveling Salesman Problem Using Dynamic Programming With Practical Example

https://www.gyanodhan.com/video/7B1.%20GATE%20CSEIT/Algorith%20Analysis%20and%20Design/616.%20AAD-%20Traveling%20Salesman%20Problem%20%20%20Using%20Dynamic%20Programming%20%20%20With%20Practical%20Example%2045%20HD.mp4

 Traveling Salesman Problem (TSP) Using Dynamic Programming

The Traveling Salesman Problem (TSP) is one of the most famous optimization problems in computer science and operations research. The goal is to find the shortest possible route that visits each city exactly once and returns to the starting point.

 Problem Statement

Given a set of N cities and the cost to travel between each pair of cities, find the shortest possible route that visits every city exactly once and returns to the starting city.

 Why Dynamic Programming?

Brute force solution (O(N!) complexity) is too slow for large N
Dynamic Programming (DP) approach reduces complexity to O(2^N * N)

 Approach – Dynamic Programming with Bitmasking

 Example

Let’s take 4 cities: A, B, C, D

Distance Matrix (Graph Representation)

A B C D
A 0 10 15 20
B 10 0 35 25
C 15 35 0 30
D 20 25 30 0

We start from city A (0th city) and find the shortest route covering all cities.

 Dynamic Programming Solution

import sys

# Number of cities
N = 4
# Distance matrix
dist = [
[0, 10, 15, 20],
[10, 0, 35, 25],
[15, 35, 0, 30],
[20, 25, 30, 0]
]

# Memoization table (stores results of subproblems)
dp = [[-1] * (1 << N) for _ in range(N)]

# Function to find the shortest route using DP + Bitmasking
def tsp(mask, pos):
# Base case: If all cities are visited, return cost to return to starting city
if mask == (1 << N) - 1:
return dist[pos][0] # Return to start city

# If already computed, return stored result
if dp[pos][mask] != -1:
return dp[pos][mask]

# Try visiting the next unvisited city
min_cost = sys.maxsize
for city in range(N):
if (mask & (1 << city)) == 0: # If city not visited
new_cost = dist[pos][city] + tsp(mask | (1 << city), city)
min_cost = min(min_cost, new_cost)

# Store result in DP table
dp[pos][mask] = min_cost
return min_cost

# Start from city 0 with only city 0 visited (mask = 1)
min_route_cost = tsp(1, 0)
print("Minimum Traveling Cost:", min_route_cost)

 Explanation of the Code

mask keeps track of visited cities using bitmasking.
pos represents the last visited city.
Base Case: If all cities are visited, return cost to return to the start.
Recursive Case: Try visiting all unvisited cities and take the minimum cost.
Memoization (dp table) avoids recomputing subproblems.

 Output

Minimum Traveling Cost: 80

Optimal Path: A -> B -> D -> C -> A with cost 80

 Time Complexity

O(2^N * N) (Much better than O(N!))

 Applications of TSP

Logistics & Delivery Optimization 
Route Planning for Salespersons 
Network Routing & Data Communication

Would you like a Graph Visualization or another approach (e.g., Genetic Algorithm)?

Traveling Salesman Problem Using Dynamic Programming With Practical Example

travelsalesman problem using dynamic programming