import java.io.File; import java.io.PrintWriter; import java.util.Scanner; public class…
- User
import java.io.File; import java.io.PrintWriter; import java.util.Scanner;
public class floyd {
public static void main(String[] args) { if (args.length != 1) { System.out.println("Usage: java floyd <graphfile>"); return; } String inputFile = args[0]; try { File file = new File(inputFile); Scanner scanner = new Scanner(file); PrintWriter pw = new PrintWriter("output.txt"); while (scanner.hasNextLine()) { String line = scanner.nextLine().trim(); Scanner lineScanner = new Scanner(line); // Try to find an integer n in the line if (lineScanner.hasNextInt()) { int n = lineScanner.nextInt(); // Skip to next line for the matrix int[][] graph = new int[n][n]; int[][] path = new int[n][n]; // Ensure we are reading correctly formatted matrices for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (!scanner.hasNextInt()) { System.out.println("Error: Matrix data is incorrectly formatted or incomplete."); return; // Exit due to formatting error } graph[i][j] = scanner.nextInt(); if (i != j && graph[i][j] == 0) graph[i][j] = Integer.MAX_VALUE; path[i][j] = -1; } } // Performing Floyd’s algorithm floydsAlgorithm(graph, path, n); // Writing results to file writeOutput(pw, graph, path, n); } lineScanner.close(); } scanner.close(); pw.close(); } catch (Exception e) { e.printStackTrace(); } } private static void floydsAlgorithm(int[][] graph, int[][] path, int n) { for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (graph[i][k] == Integer.MAX_VALUE || graph[k][j] == Integer.MAX_VALUE) continue; if (graph[i][j] > graph[i][k] + graph[k][j]) { graph[i][j] = graph[i][k] + graph[k][j]; path[i][j] = k; } } } } } private static void writeOutput(PrintWriter pw, int[][] dist, int[][] path, int n) { pw.println("Problem: n = " + n); pw.println("Pmatrix:"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { pw.print((path[i][j] == -1 ? 0 : path[i][j] + 1) + " "); } pw.println(); } // Generating paths from the path matrix for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j) { String pathStr = constructPath(i, j, path) + ": " + dist[i][j]; pw.println("V" + (i + 1) + "-V" + (j + 1) + ": " + pathStr); } } } pw.println(); // Blank line between problems } private static String constructPath(int i, int j, int[][] path) { if (path[i][j] == -1) { return "V" + (i + 1) + " V" + (j + 1); } else { return constructPath(i, path[i][j], path) + " " + constructPath(path[i][j], j, path).substring(3); } } - Assistant
- User
The corrections made is giving error as java floyd graphfile.txt Error: Matrix data is incorrectly formatted or incomplete.
- Assistant
- User
It is again showing the same error after making above changes to the code. I want the program to read input file Problem1 Amatrix: n = 7 0 6 5 4 6 3 6
6 0 6 4 5 5 3
5 6 0 3 1 4 6
4 4 3 0 4 1 4
6 5 1 4 0 5 5
3 5 4 1 5 0 3
6 3 6 4 5 3 0Problem2 Amatrix: n = 6 0 1 2 1 3 4
1 0 3 2 2 3
2 3 0 3 3 6
1 2 3 0 3 5
3 2 3 3 0 5
4 3 6 5 5 0 as then give an output file - Assistant