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 readFileString filePath
try
Scanner scanner new Scannernew FilefilePath;
size Integer.parseIntscannernextLinetrim;
adjacencyMatrix new intsizesize;
for int i ; i size; i
String parts scanner.nextLinetrimsplits;
if partslength size
System.err.printlnError in input file. Row i does not have the expected number of elements.";
return;
for int k ; k size; k
adjacencyMatrixik Integer.parseIntpartsktrim;
FloydWarshall;
int nums Integer.parseIntscannernextLinetrim;
for int i ; i nums; i
String parts scanner.nextLinetrimsplits;
int pathFrom Integer.parseIntpartstrim;
int pathTo Integer.parseIntpartstrim;
System.out.printlnpossiblePathpathFrom pathTo;
writeToFileouttxt;
catch FileNotFoundException e
System.out.printlnFile not found";
eprintStackTrace;
private void writeToFileString filePath
try FileWriter fileWriter new FileWriterfilePath
for int i ; i size; i
for int j ; j size; j
fileWriter.writedistanceMatrixij;
fileWriter.write
;
fileWriter.write
;
for int i ; i size; i
for int j ; j size; j
fileWriter.writepathMatrixij;
fileWriter.write
;
catch IOException e
eprintStackTrace;
private void FloydWarshall
distanceMatrix new intsizesize;
pathMatrix new intsizesize;
for int i ; i size; i
System.arraycopyadjacencyMatrixi distanceMatrixi size;
for int k ; k size; k
for int i ; i size; i
for int j ; j size; j
if i j && distanceMatrixik && distanceMatrixkj &&
distanceMatrixij distanceMatrixik distanceMatrixkj distanceMatrixij
distanceMatrixij distanceMatrixik distanceMatrixkj;
pathMatrixij k ;
private String possiblePathint pathFrom, int pathTo
StringBuilder output new StringBuilder;
output.appendpathFrom ;
if distanceMatrixpathFrompathTo
return NO PATH EXISTS between pathFrom and pathTo ;
int midValue pathMatrixpathFrompathTo;
while midValue
output.appendappendmidValue;
midValue pathMatrixmidValue pathTo;
output.appendappendpathTo ;
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 welldocumented and wellstructured 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
outtxt 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 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 th
Example Input: sampletxt
Example Output Standard Output
NO PATH EXSIST between and
Example Output outtxt
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
