Complete the code to initialize the distance matrix with the given graph weights.
int[][] dist = new int[V][V]; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (i == j) { dist[i][j] = 0; } else if (graph[i][j] != 0) { dist[i][j] = graph[i][j]; } else { dist[i][j] = [1]; } } }
We use Integer.MAX_VALUE to represent no direct edge between nodes, meaning infinite distance.
Complete the code to update the distance matrix inside the triple nested loops.
for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (dist[i][k] != Integer.MAX_VALUE && dist[k][j] != Integer.MAX_VALUE && dist[i][k] + dist[k][j] < [1]) { dist[i][j] = dist[i][k] + dist[k][j]; } } } }
The condition checks if the new path through k is shorter than the current distance dist[i][j].
Fix the error in the code that prints the final distance matrix after Floyd Warshall completes.
for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (dist[i][j] == [1]) { System.out.print("INF "); } else { System.out.print(dist[i][j] + " "); } } System.out.println(); }
Distances equal to Integer.MAX_VALUE mean no path exists, so print "INF".
Fill both blanks to complete the Floyd Warshall triple nested loops correctly.
for (int [1] = 0; [2] < V; [1]++) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (dist[i][[1]] != Integer.MAX_VALUE && dist[[1]][j] != Integer.MAX_VALUE && dist[i][[1]] + dist[[1]][j] < dist[i][j]) { dist[i][j] = dist[i][[1]] + dist[[1]][j]; } } } }
The outer loop variable is k, which is used as the intermediate vertex index.
Fill all three blanks to create a dictionary comprehension-like structure in Java to print shortest paths with conditions.
Map<Integer, Integer> shortestPaths = new HashMap<>(); for (int [1] = 0; [1] < V; [1]++) { if (dist[[1]][[2]] != Integer.MAX_VALUE && [3] != [1]) { shortestPaths.put([1], dist[[1]][[2]]); } }
We iterate over i as source nodes, check distances to j as destination nodes, and ensure source and destination are not the same.
