Answered step by step
Verified Expert Solution
Link Copied!

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.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
class Graph {
private Map> adjacencyList;
public Graph(){
this.adjacencyList = new HashMap<>();
}
public void addCity(String city){
adjacencyList.put(city, new HashMap<>());
}
public void addConnection(String city1, String city2, int distance){
adjacencyList.get(city1).put(city2, distance);
adjacencyList.get(city2).put(city1, distance);
}
public List depthFirstSearch(String startCity, String endCity){
if (!adjacencyList.containsKey(startCity)||!adjacencyList.containsKey(endCity)){
System.out.println("Invalid city names");
return Collections.emptyList();
}
Map parents = new HashMap<>();
Set visited = new HashSet<>();
Stack stack = new Stack<>();
stack.push(startCity);
while (!stack.isEmpty()){
String currentCity = stack.pop();
if (!visited.contains(currentCity)){
visited.add(currentCity);
for (Map.Entry neighbor : adjacencyList.get(currentCity).entrySet()){
String neighborCity = neighbor.getKey();
if (!visited.contains(neighborCity)){
stack.push(neighborCity);
parents.put(neighborCity, currentCity);
}
}
}
}
return reconstructPath(startCity, endCity, parents);
}
public List getShortestPath(String startCity, String endCity, String algorithm){
if (!adjacencyList.containsKey(startCity)||!adjacencyList.containsKey(endCity)){
System.out.println("Invalid city names");
return Collections.emptyList();
}
Map distances = new HashMap<>();
Map parents = new HashMap<>();
Set visited = new HashSet<>();
Queue queue = new LinkedList<>();
distances.put(startCity,0);
queue.add(startCity);
while (!queue.isEmpty()){
String currentCity = queue.poll();
for (Map.Entry neighbor : adjacencyList.get(currentCity).entrySet()){
String neighborCity = neighbor.getKey();
int newDistance = distances.get(currentCity)+ neighbor.getValue();
if (!distances.containsKey(neighborCity)|| newDistance < distances.get(neighborCity)){
distances.put(neighborCity, newDistance);
parents.put(neighborCity, currentCity);
if (!visited.contains(neighborCity)){
queue.add(neighborCity);
}
}
}
visited.add(currentCity);
}
return reconstructPath(startCity, endCity, parents);
}
private List reconstructPath(String startCity, String endCity, Map parents){
List path = new ArrayList<>();
String currentCity = endCity;
while (currentCity != null){
path.add(currentCity);
currentCity = parents.get(currentCity);
}
Collections.reverse(path);
return path;
}
public int getTotalDistance(List path){
int totalDistance =0;
for (int i =0; i < path.size()-1; i++){
String currentCity = path.get(i);
String nextCity = path.get(i +1);
totalDistance += adjacencyList.get(currentCity).get(nextCity);
}
return totalDistance;
}
}
public class Main {
public static void main(String[] args){
String filePath ="C:\\Users\\kadir\\OneDrive\\Masast\\Turkish cities.csv";
Graph graph = new Graph();
try (BufferedReader br = new BufferedReader(new FileReader(filePath))){
String line;
String[] cities = br.readLine().split(",");
for (int i =1; i < cities.length; i++){
graph.addCity(cities[i]);
}
int row =0;
while ((line = br.readLine())!= null){
String[] distances = line.split(",");
String currentCity = cities[row +1];
for (int col =1; col < distances.length; col++){
String otherCity = cities[col];
int distance = Integer.parseInt(distances[col]);
if (distance !=99999){
graph.addConnection(currentCity, otherCity, distance);
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security

Authors: Alfred Basta, Melissa Zgola

1st Edition

1435453905, 978-1435453906

More Books

Students also viewed these Databases questions