需要帮助编写Dijkstra算法

时间:2011-06-29 00:17:03

标签: java dijkstra shortest-path

我正在尝试将Dijkstras算法写入我在下面编写的代码中。但我不确定如何开始这样做。我确实从在线资料中查了一下,但我仍然不确定如何让它真正起作用。我更喜欢将它放在评估路径方法中。然后让菜单选项调用此方法并执行排序算法。

FYI。我按照里程和价格排序从A市到B市的最短路径。

以下是我的代码。

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(){


}
}

输出应该看起来像::

从西雅图到旧金山的最短路线是1290英里。

2 个答案:

答案 0 :(得分:2)

这是Dijkstra算法的一些代码,也许它会有所帮助..

这将设置从起始城市到每个城市的最短距离..

for each city
{
    settled = false
    distance = infinity
}

startingCity.distance = 0

currentCity = startingCity

while not all cities are settled
{
    for each city adjacent to the current city
    {
        newDist = distance from adjacentCity to currentCity

        if newDist < adjacentCity.distance
        {
            adjacentCity.distance = newDist
        }  
    }

    currentCity.settled = true

    currentCity = city closest to currentCity
}

答案 1 :(得分:1)

如果我可以提出一个可能使编码变得更容易的适度建议: 尝试创建City和Link类,然后创建节点图,而不是仅使用列表。

Dijkstra的算法是一种图遍历算法,如果你只是使用一个数组来尝试这个,你会遇到一些语义问题,将哪些值分开代表哪些路径。 (看看路径输入法,看起来你已经是)

也许您想要创建一些类:

public class City {
  String name;
  List<Road> connectingRoads;

}

public class Road {
  List<City> connectingCities;
  Float distance;
  Float price;

  // Technically this COULD be for more than two cities... mainly I wrote it this way simply to make coding and use easier.
  Road(Float distance, Float price, City... connectingCities) {
    this.distance = distance;
    this.price = price;
    connectingCities = new ArrayList(connectingCities);
    for (City city : connectingCities) {
       city.connectingRoads.add(this);
    }
  }
}

这将为您提供一个实际的图形结构来遍历,并使语义输入问题更少问题,因为您可以从数组中查找城市,然后根据给定的输入值添加道路。然后通过查看每个城市记录上的连接路径列表来完成图形遍历。

您还需要一个类来跟踪在图遍历期间找到的路径和成本,因为这是Dijkstra算法的一部分。我发现在我在大学写的迷宫运行程序的情况下找到最短的路径非常有帮助,我发现保留了这些数据。我们用它来显示迷宫中当前点的最快路径,算法运行一次后无需任何额外的计算。虽然公平,我们将算法从目标向后运行到迷宫中的所有点 - 以确定最远点 - 这样我们就可以在那里启动玩家&gt;&lt;