-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTSP.py
More file actions
91 lines (73 loc) · 3.49 KB
/
TSP.py
File metadata and controls
91 lines (73 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Define a large value for 'inf'
INF = float('inf')
# Function to initialize a 2D matrix for distances with both directions
def initialize_distance_matrix(selected_spots):
n = len(selected_spots)
distance_matrix = [[INF] * n for _ in range(n)]
print("Enter the distances between spots (Enter 'inf' if no direct path):")
for i in range(n):
for j in range(n):
if i != j:
distance = input(f"Distance from {selected_spots[i]} to {selected_spots[j]}: ")
if distance.lower() == 'inf':
distance_matrix[i][j] = INF
else:
distance_matrix[i][j] = int(distance)
return distance_matrix
# Function to get the index of a spot in selected_spots
def get_index(spot, selected_spots):
return selected_spots.index(spot)
# TSP using dynamic programming with memoization, storing the path
def tsp_dp(mask, pos, dp, next_node, distance_matrix, selected_spots, destination_index):
# If all spots are visited and we are at the destination, return 0 distance
if mask == (1 << len(selected_spots)) - 1 and pos == destination_index:
return 0
# If already computed, return saved result
if dp[mask][pos] != -1:
return dp[mask][pos]
min_distance = INF
for next in range(len(selected_spots)):
if mask & (1 << next) == 0 and distance_matrix[pos][next] != INF:
# Recursive call
new_distance = distance_matrix[pos][next] + tsp_dp(mask | (1 << next), next, dp, next_node, distance_matrix, selected_spots, destination_index)
if new_distance < min_distance:
min_distance = new_distance
next_node[mask][pos] = next # Store next node in path
dp[mask][pos] = min_distance
return min_distance
# Function to reconstruct the optimal path from stored information
def reconstruct_path(next_node, selected_spots, source_index, destination_index):
path = [selected_spots[source_index]]
mask = 1 << source_index
pos = source_index
while pos != destination_index:
pos = next_node[mask][pos]
if pos == -1: # If there is no valid next step
print("No valid path found.")
return []
path.append(selected_spots[pos])
mask |= 1 << pos
return path
# Function to find and print the optimal TSP path and distance
def find_optimal_path(selected_spots, source, destination):
# Initialize distance matrix
distance_matrix = initialize_distance_matrix(selected_spots)
n = len(selected_spots)
# Create DP and next_node tables with -1
dp = [[-1] * n for _ in range(1 << n)]
next_node = [[-1] * n for _ in range(1 << n)]
# Get the index of source and destination
source_index = get_index(source, selected_spots)
destination_index = get_index(destination, selected_spots)
# Find minimum distance
optimal_distance = tsp_dp(1 << source_index, source_index, dp, next_node, distance_matrix, selected_spots, destination_index)
# Retrieve the sequence of optimal path
optimal_path = reconstruct_path(next_node, selected_spots, source_index, destination_index)
# Display results
print(f"Total distance covered: {optimal_distance}")
print("Optimal path sequence:", " -> ".join(map(str, optimal_path)))
# Example usage:
selected_spots = [4, 3, 5, 2, 6]
source = int(input("Enter the source spot: "))
destination = int(input("Enter the destination spot: "))
find_optimal_path(selected_spots, source, destination)