Question: I keep failing test cases for a method that finds the shortest distance using dijkstras algorithm can someone fix it . private void findShortestDistance (
I keep failing test cases for a method that finds the shortest distance using dijkstras algorithm can someone fix it private void findShortestDistance
Initialize the shortest distance map
shortestRoutes.putWestside new StationWestside; Set distance to Westside as
Set distance to other stations as infinity infty
for String city : stations.getKeys
if city.equalsWestside
shortestRoutes.putcity new StationUnknown Integer.MAXVALUE; Set source station to "Unknown"
Initialize stack for stations to visit
Stack toVisit new LinkedListStack;
toVisit.pushWestside;
Set to keep track of visited stations
Set visited new HashSet;
Main loop for Dijkstra's algorithm
while toVisit.isEmpty
Get the current station from the stack
String currentStation toVisit.pop;
Skip if station has already been visited
if visitedisMembercurrentStation
continue;
Mark current station as visited
visited.addcurrentStation;
System.out.printlnVisiting station: currentStation;
Get neighbors of the current station
List neighbors stations.getcurrentStation;
Sanity check for neighbors
if neighbors null neighbors.isEmpty
System.out.printlnNo neighbors found for station: currentStation;
continue;
Loop through neighbors
for Station neighbor : neighbors
int currentDistance shortestRoutes.getcurrentStationgetDistance;
int distanceToNeighbor neighbor.getDistance;
int newDistance currentDistance distanceToNeighbor;
Check if the new distance is shorter
if newDistance shortestRoutes.getneighborgetCityNamegetDistance
Update shortest route with the correct source station
shortestRoutes.putneighborgetCityName new StationcurrentStation newDistance;
Add neighbor to stack if it hasn't been visited
if visited.isMemberneighborgetCityName
toVisit.pushneighborgetCityName;
Debugging: Print shortest route update
System.out.printlnShortest route to neighbor.getCityName updated: shortestRoutes.getneighborgetCityName;
Debugging: Print progress
System.out.printlnShortest routes so far: shortestRoutes;
System.out.println;
Debugging: Print final result
System.out.printlnFinal shortest routes: shortestRoutes;
these are the test cases @DisplayNameTesting the Travel Time"
@Nested
public class TestingTravelTime
TrainStationManager tsm new TrainStationManagerstationscsv;
@Test
@DisplayNameTesting Travel Time for Westside"
public void travelTimeWestside
Map times tsmgetTravelTimes;
assertEquals times.getWestside "Failed to give corrrect travel tome for Westside.";
@Test
@DisplayNameTesting Travel Time for Dome"
public void travelTimeTestDome
Map times tsmgetTravelTimes;
assertEquals times.getDome "Failed to give correct travel time for Dome.";
@Test
@DisplayNameTesting Travel Time for Bostin"
public void travelTimeTestBostin
Map times tsmgetTravelTimes;
assertEquals times.getBostin "Failed to give correct travel time for Bostin.";
@Test
@DisplayNameTesting Travel Time for Bugapest"
public void travelTimeTestBugapest
Map times tsmgetTravelTimes;
assertEquals times.getBugapest "Failed to give correct travel time for Bugapest.";
@Test
@DisplayNameTesting Travel Time for Loondun"
public void travelTimeTestLoondun
Map times tsmgetTravelTimes;
assertEquals times.getLoondun "Failed to give correct travel time for Loondun.";
@Test
@DisplayNameTesting Travel Time for Los Angelos"
public void travelTimeTestLosAngelos
Map times tsmgetTravelTimes;
assertEquals times.getLos Angelos" "Failed to give cor
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
