from collections import defaultdict def calculate_distance(graph, start): visited = set() distance = defaultdict(lambda: float('inf')) distance[start] = 0 queue = [start] while queue: node = queue.pop(0) if node in visited: continue visited.add(node) for neighbor in graph[node]: if distance[neighbor] > distance[node] + 1: distance[neighbor] = distance[node] + 1 queue.append(neighbor) return distance def average_distance(W, E): distances = [] for i in range(W): for j in range(E): # Connect station i on the west side to station j on the east side graph = defaultdict(list) for k in range(W): if k != i: graph['W'+str(k)].append('W'+str(k+1)) graph['W'+str(k+1)].append('W'+str(k)) for k in range(E): if k != j: graph['E'+str(k)].append('E'+str(k+1)) graph['E'+str(k+1)].append('E'+str(k)) graph['W'+str(i)].append('E'+str(j)) graph['E'+str(j)].append('W'+str(i)) # Calculate distances between all pairs of stations total_distance = 0 for node in graph: distance = calculate_distance(graph, node) for other_node in graph: if node != other_node: total_distance += distance[other_node] distances.append(total_distance / ((W+E)*(W+E-1)/2)) return distances # Example usage W = 2 E = 3 distances = average_distance(W, E) print(distances) # prints [1.5, 1.8333, 1.8333, 1.5]