Question: import java.io . * ; import java.util.Scanner; public class ShortestPath { private int [ ] [ ] adjacencyMatrix; private int size; private int [ ]

import java.io.*;
import java.util.Scanner;
public class ShortestPath {
private int[][] adjacencyMatrix;
private int size;
private int[][] distanceMatrix;
private int[][] pathMatrix;
public void readFile(String filePath){
try {
Scanner scanner = new Scanner(new File(filePath));
size = Integer.parseInt(scanner.nextLine().trim());
adjacencyMatrix = new int[size][size];
for (int i =0; i < size; i++){
String[] parts = scanner.nextLine().trim().split("\\s+");
if (parts.length != size){
System.err.println("Error in input file. Row "+ i +" does not have the expected number of elements.");
return;
}
for (int k =0; k < size; k++){
adjacencyMatrix[i][k]= Integer.parseInt(parts[k].trim());
}
}
FloydWarshall();
int nums = Integer.parseInt(scanner.nextLine().trim());
for (int i =0; i < nums; i++){
String[] parts = scanner.nextLine().trim().split("\\s+");
int pathFrom = Integer.parseInt(parts[0].trim())-1;
int pathTo = Integer.parseInt(parts[1].trim())-1;
System.out.println(possiblePath(pathFrom, pathTo));
}
writeToFile("out.txt");
} catch (FileNotFoundException e){
System.out.println("File not found");
e.printStackTrace();
}
}
private void writeToFile(String filePath){
try (FileWriter fileWriter = new FileWriter(filePath)){
for (int i =0; i < size; i++){
for (int j =0; j < size; j++){
fileWriter.write(distanceMatrix[i][j]+"");
}
fileWriter.write("
");
}
fileWriter.write("
");
for (int i =0; i < size; i++){
for (int j =0; j < size; j++){
fileWriter.write(pathMatrix[i][j]+"");
}
fileWriter.write("
");
}
} catch (IOException e){
e.printStackTrace();
}
}
private void FloydWarshall(){
distanceMatrix = new int[size][size];
pathMatrix = new int[size][size];
for (int i =0; i < size; i++){
System.arraycopy(adjacencyMatrix[i],0, distanceMatrix[i],0, size);
}
for (int k =0; k < size; k++){
for (int i =0; i < size; i++){
for (int j =0; j < size; j++){
if (i != j && distanceMatrix[i][k]!=-1 && distanceMatrix[k][j]!=-1 &&
(distanceMatrix[i][j]==-1|| distanceMatrix[i][k]+ distanceMatrix[k][j]< distanceMatrix[i][j])){
distanceMatrix[i][j]= distanceMatrix[i][k]+ distanceMatrix[k][j];
pathMatrix[i][j]= k +1;
}
}
}
}
}
private String possiblePath(int pathFrom, int pathTo){
StringBuilder output = new StringBuilder();
output.append(pathFrom +1);
if (distanceMatrix[pathFrom][pathTo]==-1){
return "NO PATH EXISTS between "+(pathFrom +1)+" and "+(pathTo +1)+".";
}
int midValue = pathMatrix[pathFrom][pathTo];
while (midValue !=0){
output.append(",").append(midValue);
midValue = pathMatrix[midValue -1][pathTo];
}
output.append(",").append(pathTo +1);
return output.toString();
}
This is the code and following is the question to the code . Can you write the same code in different way by changing some methods, variables or logics.
Problem Description: Write a well-documented and well-structured JAVA program that will
determine the shortest path to a requested set of edges. Your program must use the dynamic
programming approach defined in Floyds Algorithm to solve this problem. The program will be
given the number of vertices, an adjacency matrix, a number of requested paths and a sequence
of vertex pairs that define the required terminal vertices of a path. This input will be given in a
text file chosen at runtime. The input file will be selected using command line arguments not
hard coded or read from user responses. The program must also determine if no path exists
between the given vertex pairs. Output will be to both to standard output and to a file named
out.txt. The output file will contain the distance and path matrix determined by your program
with a single blank line between them. Note, the value of -1 will be used in the adjacency
matrix for edges that have no adjacent edge. All edges will contain positive whole number
weights.
Due date: Saturday, March 16th.
Example Input: sample.txt
6
01-115-1
9032-1-1
-1-104-1-1
-1-1203-1
3-1-1-10-1
-1109-1-10
3
24
42
16
Example Output (Standard Output)
2,4
4,5,1,2
NO PATH EXSIST between 1 and 6.
Example Output (out.txt)
01314-1
80325-1
1011047-1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!