import java.io.*;
import java.util.Scanner;
import java.util.LinkedList;


public class floyd {
    private static int[][] adjMatrix;
    private static int[][] pMatrix;
    private static final String OUTPUT_FILE = "output.txt";

    public static void main(String[] args) throws IOException {
        if (args.length != 1) {
            System.err.println("Usage: java Floyd <graph-file>");
            return;
        }

        try (Scanner scanner = new Scanner(new BufferedReader(new FileReader(args[0])))) {
            createFile();

            int problemCount = 0;

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();

                if (line.startsWith("Problem")) {
                    problemCount++;
                    int n = extractNumberOfVertices(line);

                    if (n < 5 || n > 10) {
                        throw new IllegalArgumentException("Invalid number of vertices.");
                    }

                    // Read adjacency matrix for the current problem
                    initializeMatrices(n, scanner);

                    // Compute shortest paths and print results
                    calculateShortestPaths();
                    printResult(problemCount, n);
                }
            }
        }
    }

    private static void createFile() throws FileNotFoundException {
        PrintWriter writer = new PrintWriter(OUTPUT_FILE);
        writer.close();
    }

    private static int extractNumberOfVertices(String line) {
        // Extracts the number of vertices from the line
        String[] parts = line.split("n = ");
        return Integer.parseInt(parts[1].trim());
    }

    private static void initializeMatrices(int n, Scanner scanner) {
        adjMatrix = new int[n][n];
        pMatrix = new int[n][n];
    
        for (int i = 0; i < n; i++) {
            String[] values = scanner.nextLine().trim().split("\s+");
            for (int j = 0; j < n; j++) {
                if (values[j].equals("INF")) {
                    adjMatrix[i][j] = Integer.MAX_VALUE;
                    pMatrix[i][j] = -1; // No direct path
                } else {
                    int weight = Integer.parseInt(values[j]);
                    adjMatrix[i][j] = weight;
                    if (i != j && adjMatrix[i][j] < Integer.MAX_VALUE) {
                        pMatrix[i][j] = j; // Direct path from i to j, so predecessor is j itself
                    } else if (i == j) {
                        pMatrix[i][j] = -1; // Optional: Self loops don’t need a predecessor
                        adjMatrix[i][j] = 0; // Distance from a node to itself is 0
                    }
                }
            }
        }
    }
    

    private static void calculateShortestPaths() {
        int n = adjMatrix.length;
        // Initialize pMatrix
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (adjMatrix[i][j] == Integer.MAX_VALUE || i == j) {
                    pMatrix[i][j] = -1; // No path or self-loop
                } else {
                    pMatrix[i][j] = i; // Predecessor initially set to source node
                }
            }
        }
        
        // Calculate shortest paths and update pMatrix
        for (int k = 0; k < n; k++) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (adjMatrix[i][k] != Integer.MAX_VALUE && adjMatrix[k][j] != Integer.MAX_VALUE) {
                        int newDistance = adjMatrix[i][k] + adjMatrix[k][j];
                        if (newDistance < adjMatrix[i][j]) {
                            adjMatrix[i][j] = newDistance;
                            // Update pMatrix to store the predecessor node in the path
                            pMatrix[i][j] = pMatrix[k][j];
                        }
                    }
                }
            }
        }
    }

    private static void printResult(int problemCount, int n) throws FileNotFoundException {
        try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(OUTPUT_FILE, true)))) {
            out.println("Problem" + problemCount + ": n = " + n);
            out.println("Pmatrix:");
            
            // Output the Pmatrix for the problem
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    out.print(String.format("%2d ", pMatrix[i][j]));
                }
                out.println();
            }
            out.println();
    
            // Output shortest paths for each city to all other cities
            for (int source = 0; source < n; source++) {
                out.println("V" + (source + 1) + "-Vj: shortest path and length");
                for (int dest = 0; dest < n; dest++) {
                    // Get the shortest path and distance between the source and destination
                    String path = getShortestPath(source, dest);
                    int distance = adjMatrix[source][dest];
    
                    out.println(path + ": " + distance);
                }
                out.println();
            }
            out.println(); // Separate problems visually
        } catch (IOException ex) {
            System.err.println("An error occurred while writing to the file: " + ex.getMessage());
        }
    }
    

    private static String getShortestPath(int source, int dest) {
        if (adjMatrix[source][dest] == Integer.MAX_VALUE) {
            return "No path";
        }
    
        // Use a list to construct the path in reverse
        LinkedList<String> pathList = new LinkedList<>();
        int currentNode = dest;
    
        while (currentNode != source) {
            pathList.addFirst("V" + (currentNode + 1));
            currentNode = pMatrix[source][currentNode];
        }
        // Add the source node at the beginning
        pathList.addFirst("V" + (source + 1));
        
        // Join the path list with spaces
        return String.join(" ", pathList);
    }
    

    private static void appendToFile(String output) throws FileNotFoundException {
        try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(OUTPUT_FILE, true)))) {
            out.print(output);
        } catch (IOException ex) {
            System.err.println("An error occurred while writing to the file: " + ex.getMessage());
        }
    }
}
 The program is giving output as Problem1: n = 7
Pmatrix:
-1  0  0  0  0  0  0 
 1 -1  1  1  1  1  1 
 2  2 -1  2  2  2  2 
 3  3  3 -1  3  3  3 
 4  4  4  4 -1  4  4 
 5  5  5  5  5 -1  5 
 6  6  6  6  6  6 -1 

V1-Vj: shortest path and length
V1: 0
V1 V2: 6
V1 V3: 5
V1 V4: 4
V1 V5: 6
V1 V6: 3
V1 V7: 6

V2-Vj: shortest path and length
V2 V1: 6
V2: 0
V2 V3: 6
V2 V4: 4
V2 V5: 5
V2 V6: 5
V2 V7: 3

V3-Vj: shortest path and length
V3 V1: 5
V3 V2: 6
V3: 0
V3 V4: 3
V3 V5: 1
V3 V6: 4
V3 V7: 6

V4-Vj: shortest path and length
V4 V1: 4
V4 V2: 4
V4 V3: 3
V4: 0
V4 V5: 4
V4 V6: 1
V4 V7: 4

V5-Vj: shortest path and length
V5 V1: 6
V5 V2: 5
V5 V3: 1
V5 V4: 4
V5: 0
V5 V6: 5
V5 V7: 5

V6-Vj: shortest path and length
V6 V1: 3
V6 V2: 5
V6 V3: 4
V6 V4: 1
V6 V5: 5
V6: 0
V6 V7: 3

V7-Vj: shortest path and length
V7 V1: 6
V7 V2: 3
V7 V3: 6
V7 V4: 4
V7 V5: 5
V7 V6: 3
V7: 0


Problem2: n = 6
Pmatrix:
-1  0  0  0  0  0 
 1 -1  1  1  1  1 
 2  2 -1  2  2  2 
 3  3  3 -1  3  3 
 4  4  4  4 -1  4 
 5  5  5  5  5 -1 

V1-Vj: shortest path and length
V1: 0
V1 V2: 1
V1 V3: 2
V1 V4: 1
V1 V5: 3
V1 V6: 4

V2-Vj: shortest path and length
V2 V1: 1
V2: 0
V2 V3: 3
V2 V4: 2
V2 V5: 2
V2 V6: 3

V3-Vj: shortest path and length
V3 V1: 2
V3 V2: 3
V3: 0
V3 V4: 3
V3 V5: 3
V3 V6: 6

V4-Vj: shortest path and length
V4 V1: 1
V4 V2: 2
V4 V3: 3
V4: 0
V4 V5: 3
V4 V6: 5

V5-Vj: shortest path and length
V5 V1: 3
V5 V2: 2
V5 V3: 3
V5 V4: 3
V5: 0
V5 V6: 5

V6-Vj: shortest path and length
V6 V1: 4
V6 V2: 3
V6 V3: 6
V6 V4: 5
V6 V5: 5
V6: 0

The correct and expected output is Problem1: n = 7
Pmatrix: 
0  0  0  6  3  0  6  
0  0  5  0  0  4  0  
0  5  0  0  0  0  5  
6  0  0  0  3  0  6  
3  0  0  3  0  3  0  
0  4  0  0  3  0  0  
6  0  5  6  0  0  0 

V1-Vj: shortest path and length
V1 V1: 0
V1 V2: 6
V1 V3: 5
V1 V6 V4: 4
V1 V3 V5: 6
V1 V6: 3
V1 V6 V7: 6

V2-Vj: shortest path and length
V2 V1: 6
V2 V2: 0
V2 V5 V3: 6
V2 V4: 4
V2 V5: 5
V2 V4 V6: 5
V2 V7: 3

V3-Vj: shortest path and length
V3 V1: 5
V3 V5 V2: 6
V3 V3: 0
V3 V4: 3
V3 V5: 1
V3 V6: 4
V3 V5 V7: 6

V4-Vj: shortest path and length
V4 V6 V1: 4
V4 V2: 4
V4 V3: 3
V4 V4: 0
V4 V3 V5: 4
V4 V6: 1
V4 V6 V7: 4

V5-Vj: shortest path and length
V5 V3 V1: 6
V5 V2: 5
V5 V3: 1
V5 V3 V4: 4
V5 V5: 0
V5 V3 V6: 5
V5 V7: 5

V6-Vj: shortest path and length
V6 V1: 3
V6 V4 V2: 5
V6 V3: 4
V6 V4: 1
V6 V3 V5: 5
V6 V6: 0
V6 V7: 3

V7-Vj: shortest path and length
V7 V6 V1: 6
V7 V2: 3
V7 V5 V3: 6
V7 V6 V4: 4
V7 V5: 5
V7 V6: 3
V7 V7: 0

Problem2: n = 6
Pmatrix:
0  0  0  0  2  2  
0  0  1  1  0  0  
0  1  0  1  0  2  
0  1  1  0  0  2  
2  0  0  0  0  2  
2  0  2  2  2  0  

V1-Vj: shortest path and length
V1 V1: 0
V1 V2: 1
V1 V3: 2
V1 V4: 1
V1 V2 V5: 3
V1 V2 V6: 4

V2-Vj: shortest path and length
V2 V1: 1
V2 V2: 0
V2 V1 V3: 3
V2 V1 V4: 2
V2 V5: 2
V2 V6: 3

V3-Vj: shortest path and length
V3 V1: 2
V3 V1 V2: 3
V3 V3: 0
V3 V1 V4: 3
V3 V5: 3
V3 V1 V2 V6: 6

V4-Vj: shortest path and length
V4 V1: 1
V4 V1 V2: 2
V4 V1 V3: 3
V4 V4: 0
V4 V5: 3
V4 V1 V2 V6: 5

V5-Vj: shortest path and length
V5 V2 V1: 3
V5 V2: 2
V5 V3: 3
V5 V4: 3
V5 V5: 0
V5 V2 V6: 5

V6-Vj: shortest path and length
V6 V2 V1: 4
V6 V2: 3
V6 V2 V1 V3: 6
V6 V2 V1 V4: 5
V6 V2 V5: 5
V6 V6: 0

The Pmatrix that the current output is giving is incorrect. The Pmatrix is the Predecessor matrix. Make necessary changes to the code to get the correct Pmatrix as shown in the expected output