1
1
from itertools import permutations
2
2
3
+
3
4
def calculate_total_distance (path , distances ):
4
5
"""
5
6
Calculate the total distance of a path through cities.
@@ -22,6 +23,7 @@ def calculate_total_distance(path, distances):
22
23
total_distance += distances [path [- 1 ]][path [0 ]]
23
24
return total_distance
24
25
26
+
25
27
def traveling_salesman_bruteforce (distances ):
26
28
"""
27
29
Find the shortest path through cities using brute force.
@@ -40,7 +42,7 @@ def traveling_salesman_bruteforce(distances):
40
42
num_cities = len (distances )
41
43
all_cities = list (range (num_cities ))
42
44
shortest_path = None
43
- shortest_distance = float (' inf' )
45
+ shortest_distance = float (" inf" )
44
46
45
47
for path in permutations (all_cities ):
46
48
distance = calculate_total_distance (path , distances )
@@ -50,6 +52,7 @@ def traveling_salesman_bruteforce(distances):
50
52
51
53
return shortest_path , shortest_distance
52
54
55
+
53
56
def create_distance_matrix (num_cities ):
54
57
"""
55
58
Create a distance matrix for a given number of cities.
@@ -71,13 +74,16 @@ def create_distance_matrix(num_cities):
71
74
if i == j :
72
75
row .append (0 )
73
76
elif j > i :
74
- distance = int (input (f"Enter distance between City { i + 1 } and City { j + 1 } : " ))
77
+ distance = int (
78
+ input (f"Enter distance between City { i + 1 } and City { j + 1 } : " )
79
+ )
75
80
row .append (distance )
76
81
else :
77
82
row .append (distances [j ][i ])
78
83
distances .append (row )
79
84
return distances
80
85
86
+
81
87
def print_distance_matrix (distances ):
82
88
"""
83
89
Print the distance matrix.
@@ -98,6 +104,7 @@ def print_distance_matrix(distances):
98
104
for row in distances :
99
105
print (row )
100
106
107
+
101
108
def print_city_path (path ):
102
109
"""
103
110
Print the shortest path through cities.
@@ -112,6 +119,7 @@ def print_city_path(path):
112
119
city_path = [f"City { city + 1 } " for city in path ]
113
120
print ("Shortest Path:" , " -> " .join (city_path ))
114
121
122
+
115
123
def main_menu ():
116
124
"""
117
125
Display a menu for the traveling salesman problem and handle user input.
@@ -129,22 +137,25 @@ def main_menu():
129
137
130
138
choice = input ("Enter your choice: " )
131
139
132
- if choice == '1' :
140
+ if choice == "1" :
133
141
num_cities = int (input ("Enter the number of cities: " ))
134
142
distances = create_distance_matrix (num_cities )
135
143
print_distance_matrix (distances )
136
- elif choice == '2' :
144
+ elif choice == "2" :
137
145
if 'distances' in locals ():
138
- shortest_path , shortest_distance = traveling_salesman_bruteforce (distances )
146
+ shortest_path , shortest_distance = traveling_salesman_bruteforce (
147
+ distances
148
+ )
139
149
print_city_path (shortest_path )
140
150
print ("Shortest Distance:" , shortest_distance )
141
151
else :
142
152
print ("Please create a distance matrix first." )
143
- elif choice == '3' :
153
+ elif choice == "3" :
144
154
print ("Goodbye!" )
145
155
break
146
156
else :
147
157
print ("Invalid choice. Please select a valid option." )
148
158
159
+
149
160
if __name__ == "__main__" :
150
161
main_menu ()
0 commit comments