Question: longest shortest path using dfs in java programming: I am halfway through this coding problem, it solves some arrays, but the 2 D array in
longest shortest path using dfs in java programming: I am halfway through this coding problem, it solves some arrays, but the D array in the picture return an error of out of bounds. help me fix it please. here is my code:
abstract class:
public abstract class LSD
public abstract int distance int array;
Rest of the code:
public class pathlengthcalculation extends LSD
public int array;
public pathlengthcalculation
Constructor implementation if needed
public pathlengthcalculationint array
this.array array;
@Override
public int distanceint array
int longestDistance ;
for int i ; i array.length; i
for int j ; j arrayilength; j
int startVertex arrayij;
int distance bfsShortestPatharray startVertex;
if distance longestDistance
longestDistance distance;
return longestDistance;
private int bfsShortestPathint array, int start
int queue new intarraylength;
boolean visited new booleanarraylength;
int distance new intarraylength;
int front ;
int rear ;
queuerear start;
visitedstart true;
distancestart;
while front rear
int current queuefront;
for int neighbor : arraycurrent
if visitedneighbor
visitedneighbor true;
queuerear neighbor;
distanceneighbor distancecurrent;
int maxDistance ;
for int d : distance
if d maxDistance
maxDistance d;
return maxDistance;
public static void mainString args
int array ;
int array ;
pathlengthcalculation instance new pathlengthcalculationarray;
int longestShortestPath instance.distancearray;
System.out.printlnlongest shortest path: longestShortestPath;
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
