Java 努力正确实施Dijkstra';具有2个短数组和1个布尔数组的s最短路径算法
我正在尝试实现Dijkstra的最短路径算法 我很难理解如何在while循环中测试每一条可能的路径 我已经寻找了正确实现的具体例子,但我找不到任何我能找到的 用于使我的代码正常工作:\ 任何指点都非常感谢 这是我的密码Java 努力正确实施Dijkstra';具有2个短数组和1个布尔数组的s最短路径算法,java,graph,dijkstra,Java,Graph,Dijkstra,我正在尝试实现Dijkstra的最短路径算法 我很难理解如何在while循环中测试每一条可能的路径 我已经寻找了正确实现的具体例子,但我找不到任何我能找到的 用于使我的代码正常工作:\ 任何指点都非常感谢 这是我的密码 package UserApplication; `enter code here`import java.util.ArrayList; public class ShortestPath { private UI ui; p
package UserApplication;
`enter code here`import java.util.ArrayList;
public class ShortestPath {
private UI ui;
private Map map;
private boolean included[];
private short distance[];
private short path[];
private ArrayList<Short> trace;
private short N;
boolean flag = false;
// ***************************************************************//
public ShortestPath(UI ui, Map map, short N) {
initialize(ui, map, N);
}
// ***************************************************************//
// ***************************************************************//
private void initialize(UI ui, Map map, short N) {
this.N = N;
initializeResources(ui, map);
}
// ***************************************************************//
// ***************************************************************//
private void initializeResources(UI ui, Map map) {
this.ui = ui;
this.map = map;
}
// ***************************************************************//
// ***************************************************************//
private void initializeStorage(short current) {
included = new boolean[N];
path = new short[N];
distance = new short[N];
trace = new ArrayList<Short>();
for (short i = 0; i < N - 1; i++) {
distance[i] = map.getRoadDistance(current, i);
if ((distance[i] != Short.MAX_VALUE) && (distance[i] > 0)) {
path[i] = current;
} else {
path[i] = -1;
}
}
distance[current] = 0;
included[current] = true;
}
// ***************************************************************//
// ***************************************************************//
public short search(short start, short destination) {
initializeStorage(start);
int edge = 0;
int target = 0;
short total = 0;
// trace.add(start);
while (!included(destination)) {
for (int i = 0; i < N; i++) {
if (!trace.contains(destination)&!included((short)i)) {
target = i;
included[target] = true;
trace.add((short) i);
for (int r = 0; r < N; r++) {
if (!included[i]) {
edge = map.getRoadDistance(target, destination);
if (edge > -1 && edge != Short.MAX_VALUE) {
if (distance[target] + edge < distance[i]) {
distance[i] = (short) (distance[target] + edge);
path[i] = (short) target;
}
}
}
}
}
}
}
return total;
}
// ***************************************************************//
// ***************************************************************//
public void findPath(short start, short end) {
short distance = 0;
if ((start > -1) && (end > -1)) {
ui.writeThis("# # # # # # # # # # # # # # # # # # # # # # # \n");
ui.writeThis(map.whatsCityName(start) + " (" + start + ")" + " TO "
+ map.whatsCityName(end).trim() + "(" + end + ")\n");
if (start == end) {
initializeStorage(start);
trace.add(start);
path[start] = 0;
included[start] = true;
distance = 0;
} else {
distance = search(start, end);
}
reportAnswer(distance);
reportTraceOfTargets(distance);
} else {
ui.writeThis("# # # # # # # # # # # # # # # # # # # # # # # \n");
ui.writeThis(ui.getStartCityName() + " (" + start + ")" + " TO "
+ ui.getDestinationCityName().trim() + " (" + end + ")\n");
ui.writeThis("ERROR one of the cities is not on this map\n\n");
}
}
// ***************************************************************//
// ***************************************************************//
public void reportAnswer(short distance) {
if (distance > 0) {
ui.writeThis("DISTANCE : " + distance + "\n");
} else {
ui.writeThis("DISTANCE : ?\n");
}
}
// ***************************************************************//
// ***************************************************************//
public void reportTraceOfTargets(short distance) {
if (distance > -1) {
ui.writeThis("PATH: ");
for (int i = N - 1; i >= 0; i--) {
if ((path[i] != -1) && (included[i])) {
ui.writeThis(" > " + (map.whatsCityName((short) i)));
}
}
} else {
ui.writeThis("PATH: SORRY - cant reach destination city from start city\n");
}
ui.writeThis("\nTRACE OF TARGETS: ");
for (int i = 0; i < trace.size(); i++) {
ui.writeThis(" > " + (map.whatsCityName(trace.get(i))));
}
ui.writeThis("\n" + "# TARGETS: " + trace.size() + "\n\n");
}
// ***************************************************************//
// ***************************************************************//
public boolean included(short destination) {
if (trace.contains(destination)) {
return true;
}
return false;
}
}
包用户应用;
`在此处输入代码`import java.util.ArrayList;
公共类最短路径{
私有用户界面;
私人地图;
包含私有布尔值[];
私人短途[];
专用短路径[];
私有数组列表跟踪;
私有短N;
布尔标志=假;
// ***************************************************************//
公共最短路径(UI、地图、短N){
初始化(ui,map,N);
}
// ***************************************************************//
// ***************************************************************//
私有void初始化(UI,映射,短N){
这个,N=N;
初始化资源(ui、地图);
}
// ***************************************************************//
// ***************************************************************//
私有无效初始值设定项资源(UI、映射){
this.ui=ui;
this.map=map;
}
// ***************************************************************//
// ***************************************************************//
专用无效初始化存储空间(短电流){
包含=新布尔值[N];
路径=新短[N];
距离=新短距离[N];
trace=newarraylist();
对于(短i=0;i0)){
路径[i]=电流;
}否则{
路径[i]=-1;
}
}
距离[电流]=0;
包含[当前]=真;
}
// ***************************************************************//
// ***************************************************************//
公共短搜索(短起点、短终点){
初始化存储(开始);
int-edge=0;
int目标=0;
短总数=0;
//trace.add(开始);
而(!包括(目的地)){
对于(int i=0;i-1&&edge!=Short.MAX_值){
if(距离[目标]+边缘<距离[i]){
距离[i]=(短)(距离[目标]+边缘);
路径[i]=(短)目标;
}
}
}
}
}
}
}
返回总数;
}
// ***************************************************************//
// ***************************************************************//
公共void findPath(短开始、短结束){
短距离=0;
如果((开始>-1)和&(结束>-1)){
ui.writeThis(“###########################;
ui.writeThis(map.whatsCityName(start)+“(“+start+”)”+“TO”
+map.whatsCityName(end.trim()+“(“+end+”)”)\n“;
如果(开始==结束){
初始化存储(开始);
trace.add(开始);
路径[开始]=0;
包括[开始]=真;
距离=0;
}否则{
距离=搜索(开始、结束);
}
报告答案(距离);
ReportTraceFTargets(距离);
}否则{
ui.writeThis(“###########################;
ui.writeThis(ui.getStartChityName()+“(“+start+”)”+“TO”
+ui.getDestinationCityName().trim()+“(“+end+”)\n”);
ui.writeThis(“错误一个城市不在此地图上\n\n”);
}
}
// ***************************************************************//
// ***************************************************************//
公共应答(短距离){
如果(距离>0){
ui.writeThis(“距离:“+DISTANCE+”\n”);
}否则{
ui.writeThis(“距离:?\n”);
}
}
// ***************************************************************//
// ***************************************************************//
公共void ReportTraceOffTargets(短距离){
如果(距离>-1){
ui.writeThis(“路径:”);
对于(int i=N-1;i>=0;i--){
if((路径[i]!=-1)和&(包含[i])){
ui.writeThis(“>”+(map.whatsCityName((short)i));
}
}
}否则{
ui.writeThis(