尝试使用2个短数组和1个布尔数组正确实现Dijkstra的最短路径算法

时间:2014-04-10 05:49:28

标签: java graph dijkstra

我正在尝试实现Dijkstra的最短路径算法。

我很难理解如何在while循环中测试每条可能的路径。

我已经找到了正确实施的具体示例,但我无法找到任何可以实现的内容 用来让我的代码工作:\

任何指针都非常受欢迎

这是我的代码

    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;

        }

    }

0 个答案:

没有答案