|
| 1 | +from itertools import permutations |
| 2 | + |
| 3 | + |
| 4 | +def calculate_total_distance(path, distances): |
| 5 | + """ |
| 6 | + Calculate the total distance of a path through cities. |
| 7 | +
|
| 8 | + Args: |
| 9 | + path (list of int): A list representing the order of cities to visit. |
| 10 | + distances (list of lists of int): A distance matrix between cities. |
| 11 | +
|
| 12 | + Returns: |
| 13 | + int: The total distance of the path. |
| 14 | +
|
| 15 | + Example: |
| 16 | + >>> distances = [[0, 2, 9, 10], [1, 0, 6, 4], [15, 7, 0, 8], [6, 3, 12, 0]] |
| 17 | + >>> calculate_total_distance([0, 1, 2, 3, 0], distances) |
| 18 | + 21 |
| 19 | + """ |
| 20 | + total_distance = 0 |
| 21 | + for i in range(len(path) - 1): |
| 22 | + total_distance += distances[path[i]][path[i + 1]] |
| 23 | + total_distance += distances[path[-1]][path[0]] |
| 24 | + return total_distance |
| 25 | + |
| 26 | + |
| 27 | +def traveling_salesman_bruteforce(distances): |
| 28 | + """ |
| 29 | + Find the shortest path through cities using brute force. |
| 30 | +
|
| 31 | + Args: |
| 32 | + distances (list of lists of int): A distance matrix between cities. |
| 33 | +
|
| 34 | + Returns: |
| 35 | + tuple: A tuple containing the shortest path (list of int) and the shortest distance (int). |
| 36 | +
|
| 37 | + Example: |
| 38 | + >>> distances = [[0, 2, 9, 10], [1, 0, 6, 4], [15, 7, 0, 8], [6, 3, 12, 0]] |
| 39 | + >>> traveling_salesman_bruteforce(distances) |
| 40 | + ([0, 1, 3, 2], 21) |
| 41 | + """ |
| 42 | + num_cities = len(distances) |
| 43 | + all_cities = list(range(num_cities)) |
| 44 | + shortest_path = None |
| 45 | + shortest_distance = float("inf") |
| 46 | + |
| 47 | + for path in permutations(all_cities): |
| 48 | + distance = calculate_total_distance(path, distances) |
| 49 | + if distance < shortest_distance: |
| 50 | + shortest_distance = distance |
| 51 | + shortest_path = path |
| 52 | + |
| 53 | + return shortest_path, shortest_distance |
| 54 | + |
| 55 | + |
| 56 | +def create_distance_matrix(num_cities): |
| 57 | + """ |
| 58 | + Create a distance matrix for a given number of cities. |
| 59 | +
|
| 60 | + Args: |
| 61 | + num_cities (int): The number of cities. |
| 62 | +
|
| 63 | + Returns: |
| 64 | + list of lists of int: A distance matrix between cities. |
| 65 | +
|
| 66 | + Example: |
| 67 | + >>> create_distance_matrix(4) |
| 68 | + [[0, 2, 9, 10], [2, 0, 6, 4], [9, 6, 0, 8], [10, 4, 8, 0]] |
| 69 | + """ |
| 70 | + distances = [] |
| 71 | + for i in range(num_cities): |
| 72 | + row = [] |
| 73 | + for j in range(num_cities): |
| 74 | + if i == j: |
| 75 | + row.append(0) |
| 76 | + elif j > i: |
| 77 | + distance = int( |
| 78 | + input(f"Enter distance between City {i + 1} and City {j + 1}: ") |
| 79 | + ) |
| 80 | + row.append(distance) |
| 81 | + else: |
| 82 | + row.append(distances[j][i]) |
| 83 | + distances.append(row) |
| 84 | + return distances |
| 85 | + |
| 86 | + |
| 87 | +def print_distance_matrix(distances): |
| 88 | + """ |
| 89 | + Print the distance matrix. |
| 90 | +
|
| 91 | + Args: |
| 92 | + distances (list of lists of int): A distance matrix between cities. |
| 93 | +
|
| 94 | + Example: |
| 95 | + >>> distances = [[0, 2, 9, 10], [1, 0, 6, 4], [15, 7, 0, 8], [6, 3, 12, 0]] |
| 96 | + >>> print_distance_matrix(distances) |
| 97 | + Distance Matrix: |
| 98 | + [0, 2, 9, 10] |
| 99 | + [2, 0, 6, 4] |
| 100 | + [9, 6, 0, 8] |
| 101 | + [10, 4, 8, 0] |
| 102 | + """ |
| 103 | + print("Distance Matrix:") |
| 104 | + for row in distances: |
| 105 | + print(row) |
| 106 | + |
| 107 | + |
| 108 | +def print_city_path(path): |
| 109 | + """ |
| 110 | + Print the shortest path through cities. |
| 111 | +
|
| 112 | + Args: |
| 113 | + path (list of int): A list representing the order of cities to visit. |
| 114 | +
|
| 115 | + Example: |
| 116 | + >>> print_city_path([0, 1, 3, 2]) |
| 117 | + Shortest Path: City 1 -> City 2 -> City 4 -> City 3 |
| 118 | + """ |
| 119 | + city_path = [f"City {city + 1}" for city in path] |
| 120 | + print("Shortest Path:", " -> ".join(city_path)) |
| 121 | + |
| 122 | + |
| 123 | +def main_menu(): |
| 124 | + """ |
| 125 | + Display a menu for the traveling salesman problem and handle user input. |
| 126 | +
|
| 127 | + Example: |
| 128 | + >>> # This function interacts with the user, so no specific doctests are provided. |
| 129 | + >>> # You can run this function manually to test the menu functionality. |
| 130 | + >>> main_menu() |
| 131 | + """ |
| 132 | + while True: |
| 133 | + print("\nMenu:") |
| 134 | + print("1. Create a distance matrix") |
| 135 | + print("2. Find the shortest path") |
| 136 | + print("3. Exit") |
| 137 | + |
| 138 | + choice = input("Enter your choice: ") |
| 139 | + |
| 140 | + if choice == "1": |
| 141 | + num_cities = int(input("Enter the number of cities: ")) |
| 142 | + distances = create_distance_matrix(num_cities) |
| 143 | + print_distance_matrix(distances) |
| 144 | + elif choice == "2": |
| 145 | + if "distances" in locals(): |
| 146 | + shortest_path, shortest_distance = traveling_salesman_bruteforce( |
| 147 | + distances |
| 148 | + ) |
| 149 | + print_city_path(shortest_path) |
| 150 | + print("Shortest Distance:", shortest_distance) |
| 151 | + else: |
| 152 | + print("Please create a distance matrix first.") |
| 153 | + elif choice == "3": |
| 154 | + print("Goodbye!") |
| 155 | + break |
| 156 | + else: |
| 157 | + print("Invalid choice. Please select a valid option.") |
| 158 | + |
| 159 | + |
| 160 | +if __name__ == "__main__": |
| 161 | + main_menu() |
0 commit comments