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