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.put("Westside", new Station("Westside",0)); // Set distance to Westside as 0
// Set distance to other stations as infinity (\infty )
for (String city : stations.getKeys()){
if (!city.equals("Westside")){
shortestRoutes.put(city, new Station("Unknown", Integer.MAX_VALUE)); // Set source station to "Unknown"
}
}
// Initialize stack for stations to visit
Stack toVisit = new LinkedListStack<>();
toVisit.push("Westside");
// 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 (visited.isMember(currentStation)){
continue;
}
// Mark current station as visited
visited.add(currentStation);
System.out.println("Visiting station: "+ currentStation);
// Get neighbors of the current station
List neighbors = stations.get(currentStation);
// Sanity check for neighbors
if (neighbors == null || neighbors.isEmpty()){
System.out.println("No neighbors found for station: "+ currentStation);
continue;
}
// Loop through neighbors
for (Station neighbor : neighbors){
int currentDistance = shortestRoutes.get(currentStation).getDistance();
int distanceToNeighbor = neighbor.getDistance();
int newDistance = currentDistance + distanceToNeighbor;
// Check if the new distance is shorter
if (newDistance < shortestRoutes.get(neighbor.getCityName()).getDistance()){
// Update shortest route with the correct source station
shortestRoutes.put(neighbor.getCityName(), new Station(currentStation, newDistance));
// Add neighbor to stack if it hasn't been visited
if (!visited.isMember(neighbor.getCityName())){
toVisit.push(neighbor.getCityName());
}
// Debugging: Print shortest route update
System.out.println("Shortest route to "+ neighbor.getCityName()+" updated: "+ shortestRoutes.get(neighbor.getCityName()));
}
}
// Debugging: Print progress
System.out.println("Shortest routes so far: "+ shortestRoutes);
System.out.println();
}
// Debugging: Print final result
System.out.println("Final shortest routes: "+ shortestRoutes);
} these are the test cases @DisplayName("Testing the Travel Time")
@Nested
public class TestingTravelTime {
TrainStationManager tsm = new TrainStationManager("stations.csv");
@Test
@DisplayName("Testing Travel Time for Westside")
public void travelTimeWestside(){
Map times = tsm.getTravelTimes();
assertEquals(0, times.get("Westside"), "Failed to give corrrect travel tome for Westside.");
}
@Test
@DisplayName("Testing Travel Time for Dome")
public void travelTimeTestDome(){
Map times = tsm.getTravelTimes();
assertEquals(355.0, times.get("Dome"), "Failed to give correct travel time for Dome.");
}
@Test
@DisplayName("Testing Travel Time for Bostin")
public void travelTimeTestBostin(){
Map times = tsm.getTravelTimes();
assertEquals(192.5, times.get("Bostin"), "Failed to give correct travel time for Bostin.");
}
@Test
@DisplayName("Testing Travel Time for Bugapest")
public void travelTimeTestBugapest(){
Map times = tsm.getTravelTimes();
assertEquals(137.5, times.get("Bugapest"), "Failed to give correct travel time for Bugapest.");
}
@Test
@DisplayName("Testing Travel Time for Loondun")
public void travelTimeTestLoondun(){
Map times = tsm.getTravelTimes();
assertEquals(315.0, times.get("Loondun"), "Failed to give correct travel time for Loondun.");
}
@Test
@DisplayName("Testing Travel Time for Los Angelos")
public void travelTimeTestLosAngelos(){
Map times = tsm.getTravelTimes();
assertEquals(325.0, times.get("Los Angelos"), "Failed to give cor

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 Programming Questions!