Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This is my Java code so far: import java.io . BufferedReader; import java.io . FileReader; import java.io . IOException; import java.util. * ; class Graph
This is my Java code so far:
import java.ioBufferedReader;
import java.ioFileReader;
import java.ioIOException;
import java.util.;
class Graph
private Map adjacencyList;
public Graph
this.adjacencyList new HashMap;
public void addCityString city
adjacencyList.putcity new HashMap;
public void addConnectionString city String city int distance
adjacencyList.getcityputcity distance;
adjacencyList.getcityputcity distance;
public List depthFirstSearchString startCity, String endCity
if adjacencyList.containsKeystartCityadjacencyList.containsKeyendCity
System.out.printlnInvalid city names";
return Collections.emptyList;
Map parents new HashMap;
Set visited new HashSet;
Stack stack new Stack;
stack.pushstartCity;
while stack.isEmpty
String currentCity stack.pop;
if visited.containscurrentCity
visited.addcurrentCity;
for MapEntry neighbor : adjacencyList.getcurrentCityentrySet
String neighborCity neighbor.getKey;
if visited.containsneighborCity
stack.pushneighborCity;
parents.putneighborCity currentCity;
return reconstructPathstartCity endCity, parents;
public List getShortestPathString startCity, String endCity, String algorithm
if adjacencyList.containsKeystartCityadjacencyList.containsKeyendCity
System.out.printlnInvalid city names";
return Collections.emptyList;
Map distances new HashMap;
Map parents new HashMap;
Set visited new HashSet;
Queue queue new LinkedList;
distances.putstartCity;
queue.addstartCity;
while queue.isEmpty
String currentCity queue.poll;
for MapEntry neighbor : adjacencyList.getcurrentCityentrySet
String neighborCity neighbor.getKey;
int newDistance distances.getcurrentCity neighbor.getValue;
if distances.containsKeyneighborCity newDistance distances.getneighborCity
distances.putneighborCity newDistance;
parents.putneighborCity currentCity;
if visited.containsneighborCity
queue.addneighborCity;
visited.addcurrentCity;
return reconstructPathstartCity endCity, parents;
private List reconstructPathString startCity, String endCity, Map parents
List path new ArrayList;
String currentCity endCity;
while currentCity null
path.addcurrentCity;
currentCity parents.getcurrentCity;
Collections.reversepath;
return path;
public int getTotalDistanceList path
int totalDistance ;
for int i ; i path.size; i
String currentCity path.geti;
String nextCity path.geti ;
totalDistance adjacencyList.getcurrentCitygetnextCity;
return totalDistance;
public class Main
public static void mainString args
String filePath C:UserskadirOneDriveMasastTurkish cities.csv;
Graph graph new Graph;
try BufferedReader br new BufferedReadernew FileReaderfilePath
String line;
String cities brreadLinesplit;
for int i ; i cities.length; i
graph.addCitycitiesi;
int row ;
while line brreadLine null
String distances line.split;
String currentCity citiesrow ;
for int col ; col distances.length; col
String otherCity citiescol;
int distance Integer.parseIntdistancescol;
if distance
graph.addConnectioncurrentCity otherCity, distance;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started