Question
[Java] Using the Dijkstra algorithm, solve for the shortest path. QUESTION: I need to add two more methods into the main class below, which should
[Java] Using the Dijkstra algorithm, solve for the shortest path. QUESTION: I need to add two more methods into the main class below, which should also include " for(Vertex v : vertices.values()) " to iterate over the vertices in the vertices map. The result should be like: 1. Typing City A and City B 2. Then it should print out the list of cities between A and B, through which you can go from A to B with the shortest path i.e., If you type Boston and Atlanta, then the result should show the cities you drop by when going from Boston to Atlanta so that the path between Boston and Atlanta could be minimized as possible.
I'm done with other two classes that are invoked by the main class.
package graph;
import java.io.File;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.HashSet;
public class Graph {
private TreeMap vertices;
public Graph() {
vertices = new TreeMap();
}
public void readFrom(String fileName) {
Scanner input = null;
try {
input = new Scanner(new File(fileName));
} catch (Exception ex) {
ex.printStackTrace();
System.exit(-1);
}
while (input.hasNext()) {
String from = input.next();
String to = input.next();
int distance = input.nextInt();
Vertex fromV = null;
if (!vertices.containsKey(from)) {
fromV = new Vertex(from);
vertices.put(from, fromV);
} else {
fromV = vertices.get(from);
}
Vertex toV = null;
if (!vertices.containsKey(to)) {
toV = new Vertex(to);
vertices.put(to, toV);
} else {
toV = vertices.get(to);
}
fromV.addEdge(toV, distance);
toV.addEdge(fromV, distance);
}
}
public Edge[] getEdgesFrom(String name) {
Vertex v = vertices.get(name);
if (v != null) {
return v.getEdges();
} else {
return new Edge[0];
}
}
public static void main(String[] args) {
Graph G = new Graph();
G.readFrom("highways.txt");
Scanner input = new Scanner(System.in);
System.out.print("Enter the name of a start city: ");
String source = input.next();
while(!G.cityInGraph(source)) {
System.out.println(source+" is not a valid city.");
System.out.print("Enter the name of a start city: ");
source = input.next();
}
System.out.print("Enter the name of a destination city: ");
String destination = input.next();
while(!G.cityInGraph(destination)) {
System.out.println(destination+" is not a valid city.");
System.out.print("Enter the name of a destination city: ");
destination = input.next();
}
G.runDijkstra(source);
System.out.println("Here is the path from "+source+" to "+destination+":");
G.printPath(source,destination);
}
}
private Vertex destination; private int weight; public Edge(Vertex destination,int weight) { this.destination = destination; this.weight = weight; } public Vertex getDestination() { return destination; } public int getWeight() { return weight; } }
private String city; private ArrayList edges; public Vertex(String name) { this.city = name; edges = new ArrayList(); } public void addEdge(Vertex destination,int weight) { Edge newEdge = new Edge(destination,weight); edges.add(newEdge); } public String getName() { return city; } public Edge[] getEdges() { Edge[] result = new Edge[edges.size()]; return edges.toArray(result); } }
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