I am trying write Dijkstras Algorithm into the code I have written below. But I am unsure how to start doing this. I did review it a bit from online sources, but I am still unsure how to make it work really. I prefer to place it into the evaluate paths method. Then have the menu option call this method and it perform the sorting algorithm.
FYI. I am sorting the shortest path from City A to City B by miles and price.
Below is the code I have.
import java.io.*;
import java.util.*;
public class CityCalcultor {
static LinkedList<String> cities = new LinkedList<String>();
static LinkedList<Integer> distance = new LinkedList<Integer>();
static LinkedList<Integer> price = new LinkedList<Integer>();
public static void main(String[] args)throws IOException
{
Scanner input = new Scanner(System.in);
String text;
int option = 0;
while (true)
{
System.out.println("\nWhat would you like to do:\n" +
"1. Add a city to the system\n" +
"2. Add a path to the system\n" +
"3. Evalute paths\n" +
"4. Exit\n" + "Your option: ");
text = input.nextLine();
option = Integer.parseInt(text);
switch (option)
{
case 1: EnterCity(); break;
case 2: EnterPath(); break;
case 3: EvalutePaths(); break;
case 4: return;
default: System.out.println("ERROR INVALID INPUT");
}
}
}
public static void EnterCity(){
String c = "";
LinkedList<String> cities = new LinkedList<String>(Arrays.asList(c));
Scanner City = new Scanner(System.in);
System.out.println("Please enter the city name ");
c = City.nextLine();
cities.add(c);
System.out.println("City " + c + " has been added ");
}
public static void EnterPath(){
Scanner Path = new Scanner(System.in);
int d = 0; int p = 0;
System.out.println("Enter the starting city ");
System.out.println();
System.out.println(Path.nextLine());
System.out.println("Enter the ending city ");
System.out.println(Path.nextLine());
System.out.println("Enter the distance between the two cities ");
d= Path.nextInt();
distance.add(d);
System.out.println("Enter the price between the two cities ");
p = Path.nextInt();
price.add(p);
System.out.println("The route was sucessfully added ");
}
private static void EvalutePaths(){
}
}
Output should look like::
Shortest Route from Seattle to San Francisco is 1290 miles.
here is some pseudo code for Dijkstra’s algorithm, perhaps it will help..
this will set the shortest distance to each city from the starting city..