def printmatrix(matrix, iteration, matrixname): print(f"After iteration {iteration},…
- User
def print_matrix(matrix, iteration, matrix_name): print(f"After iteration {iteration}, {matrix_name}:") for row in matrix: formatted_row = [' ∞' if x == float('inf') else f'{x:3}' for x in row] print('[{}]'.format(', '.join(formatted_row))) print()
def initialize_matrices(num_vertices, edges): inf = float('inf') D = [[inf if i != j else 0 for i in range(num_vertices)] for j in range(num_vertices)] P = [[0 if i != j else 0 for i in range(num_vertices)] for j in range(num_vertices)]
for start, end, weight in edges: D[start][end] = weight P[start][end] = start + 1 return D, Pdef floyd_warshall_with_printing(num_vertices, edges): D, P = initialize_matrices(num_vertices, edges)
print_matrix(D, 0, "D0") print_matrix(P, 0, "P0") for k in range(num_vertices): for i in range(num_vertices): for j in range(num_vertices): if D[i][k] + D[k][j] < D[i][j]: D[i][j] = D[i][k] + D[k][j] P[i][j] = P[k][j] print_matrix(D, k + 1, f"D{k + 1}") print_matrix(P, k + 1, f"P{k + 1}") return D, Pedges = [ (0, 1, 4), (0, 5, 10), (1, 0, 3), (1, 3, 18), (2, 1, 6), (3, 1, 5), (3, 2, 15), (3, 4, 2), (3, 5, 19), (3, 6, 5), (4, 2, 12), (4, 3, 1), (5, 6, 10), (6, 3, 8) ]
num_vertices = 7
final_D, final_P = floyd_warshall_with_printing(num_vertices, edges)
print("Final Distance Matrix:") for row in final_D: formatted_row = [' ∞' if x == float('inf') else f'{x:3}' for x in row]
print('[{}]'.format(', '.join(formatted_row)))print("\nFinal Predecessor Matrix:") for row in final_P: formatted_row = [f'{x:3}' for x in row]
print('[{}]'.format(', '.join(formatted_row))) After iteration 0, D0: [ 0, 4, ∞, ∞, ∞, 10, ∞] [ 3, 0, ∞, 18, ∞, ∞, ∞] [ ∞, 6, 0, ∞, ∞, ∞, ∞] [ ∞, 5, 15, 0, 2, 19, 5] [ ∞, ∞, 12, 1, 0, ∞, ∞] [ ∞, ∞, ∞, ∞, ∞, 0, 10] [ ∞, ∞, ∞, 8, ∞, ∞, 0]After iteration 0, P0: [ 0, 1, 0, 0, 0, 1, 0] [ 2, 0, 0, 2, 0, 0, 0] [ 0, 3, 0, 0, 0, 0, 0] [ 0, 4, 4, 0, 4, 4, 4] [ 0, 0, 5, 5, 0, 0, 0] [ 0, 0, 0, 0, 0, 0, 6] [ 0, 0, 0, 7, 0, 0, 0]
After iteration 1, D1: [ 0, 4, ∞, ∞, ∞, 10, ∞] [ 3, 0, ∞, 18, ∞, 13, ∞] [ ∞, 6, 0, ∞, ∞, ∞, ∞] [ ∞, 5, 15, 0, 2, 19, 5] [ ∞, ∞, 12, 1, 0, ∞, ∞] [ ∞, ∞, ∞, ∞, ∞, 0, 10] [ ∞, ∞, ∞, 8, ∞, ∞, 0]
After iteration 1, P1: [ 0, 1, 0, 0, 0, 1, 0] [ 2, 0, 0, 2, 0, 1, 0] [ 0, 3, 0, 0, 0, 0, 0] [ 0, 4, 4, 0, 4, 4, 4] [ 0, 0, 5, 5, 0, 0, 0] [ 0, 0, 0, 0, 0, 0, 6] [ 0, 0, 0, 7, 0, 0, 0]
After iteration 2, D2: [ 0, 4, ∞, 22, ∞, 10, ∞] [ 3, 0, ∞, 18, ∞, 13, ∞] [ 9, 6, 0, 24, ∞, 19, ∞] [ 8, 5, 15, 0, 2, 18, 5] [ ∞, ∞, 12, 1, 0, ∞, ∞] [ ∞, ∞, ∞, ∞, ∞, 0, 10] [ ∞, ∞, ∞, 8, ∞, ∞, 0]
- Assistant
To modify the predecessor matrix to display a dash ('-') where there is a direct link between two vertices, you can adjust the
initialize_matricesfunction. When setting the initial values of the matrix, instead of directly assigning the starting vertex (plus one for human-readable format), you can assign a dash ('-') to indicate a direct link. Since the dash is a string and the rest of your values are integers, ensure to handle this change appropriately in the printing function as well, to accommodate both string and integer values. Here's how you can implement these changes: