Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created TSL.py #12163

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions dynamic_programming/TSL.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from itertools import combinations

Check failure on line 1 in dynamic_programming/TSL.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

dynamic_programming/TSL.py:1:1: N999 Invalid module name: 'TSL'


def tsp_dynamic_programming(dist_matrix):
n = len(dist_matrix) # number of cities

# Initialize a dictionary to store the costs of visiting a subset of cities ending at city i

Check failure on line 7 in dynamic_programming/TSL.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dynamic_programming/TSL.py:7:89: E501 Line too long (96 > 88)
# dp[subset][i] will store the minimum cost to visit all cities in 'subset' and end at city i

Check failure on line 8 in dynamic_programming/TSL.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dynamic_programming/TSL.py:8:89: E501 Line too long (97 > 88)
dp = {}

# Base case: Starting from the first city, i.e., only city 0 is visited
for i in range(1, n):
dp[(1 << i, i)] = dist_matrix[0][i] # Subset contains city 0 and city i

# Iterate through subsets of cities (of increasing size)
for subset_size in range(2, n):
for subset in combinations(range(1, n), subset_size):
# Create the subset bitmask
subset_mask = 0
for city in subset:
subset_mask |= 1 << city

# Try ending at each city 'j' in the subset
for j in subset:
prev_mask = subset_mask & ~(1 << j) # Remove city j from the subset
dp[(subset_mask, j)] = min(
dp[(prev_mask, k)] + dist_matrix[k][j] for k in subset if k != j
)

# Final step: Consider returning to the start (city 0) from each possible city
final_mask = (1 << n) - 2 # All cities visited except city 0
return min(dp[(final_mask, i)] + dist_matrix[i][0] for i in range(1, n))


# Example usage
if __name__ == "__main__":
# Example distance matrix (symmetric matrix where dist_matrix[i][j] is the distance between city i and city j)

Check failure on line 37 in dynamic_programming/TSL.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dynamic_programming/TSL.py:37:89: E501 Line too long (114 > 88)
dist_matrix = [[0, 10, 15, 20], [10, 0, 35, 25], [15, 35, 0, 30], [20, 25, 30, 0]]

result = tsp_dynamic_programming(dist_matrix)
print(f"The minimum travel cost is: {result}")
Loading