java图dijkstras算法

jq6vz3qz  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(237)

所以我尝试用java实现一个图,我可以做bfs和dfs,但是我的dijkstras算法有一些问题。基本上这是我的图表。。。
图表
所以当我运行dijkstras算法时,我得到以下结果

Shortest path cost between ST A: -1, Path: [ST, B, A]
Shortest path cost between ST B: -1, Path: [ST, B]
Shortest path cost between ST C: -1, Path: [ST, B, D, C]
Shortest path cost between ST D: -1, Path: [ST, B, D]
Shortest path cost between ST M: -1, Path: [None]
Shortest path cost between ST ST: 0, Path: []

结果应该是。。。

Shortest path cost between ST A: 10, Path: [ST, B, A]
Shortest path cost between ST B: 6, Path: [ST, B]
Shortest path cost between ST C: 12, Path: [ST, B, A, C]
Shortest path cost between ST D: 9, Path: [ST, B, D]
Shortest path cost between ST M: -1, Path: [None]
Shortest path cost between ST ST: 0, Path: [ST]

如你所见,我无法得到路径成本,对于st到c,我得到的是st,b,d,c,而不是st,b,a,c,这是较短的路径。
我的方法包含开始节点、结束节点和最短路径的arraylist。附件如下。。。

private boolean alpha;
private int temp;

public int doDijkstras(String startVertex, String endVertex,
        ArrayList<String> shortestPath) {

    if (!dataMap.containsKey(startVertex) || !dataMap.containsKey(endVertex)
            || getAdjacentVertices(startVertex) == null
            || getAdjacentVertices(endVertex) == null) {
        shortestPath.add("None");
        return -1;
    }
    if (startVertex.equals(endVertex)) {
        return 0;
    }

    ArrayList<String> currentPath = new ArrayList<String>();
    Stack<Node> visited = new Stack<Node>();

    Node currentNode;
    Node previousNode = new Node();

    int lowestPath = -1;

    currentNode = new Node(startVertex);
    visited.push(currentNode);
    currentPath.add(currentNode.name);

    while (visited.size() != 0) {
        alpha = false;
        djAux(startVertex, endVertex, previousNode, currentNode, visited,
                shortestPath, currentPath, lowestPath);
        previousNode = visited.peek();
        visited.pop();
        currentPath.clear();
        if (visited.size() != 0) {
            visited.peek().previous.add(previousNode.name);
            currentNode = visited.peek();

            for (Node n : visited) {
                currentPath.add(n.name);
            }
            temp = 0;
            for (int i = 0; i < currentPath.size() - 2; i++) {
                for (int j = i + 1; j < currentPath.size() - 1; j++) {
                    temp += getCost(currentPath.get(i), currentPath.get(j));
                }
            }
        }
    }
            Object[] shortPath = shortestPath.toArray();
    int totalDistance = 0;
    for (int i = 0; i < shortPath.length; i++) {
        if (i + 1 == shortPath.length) {
            break;
        }
        totalDistance += getCost((String) shortPath[i],
                (String) shortPath[i + 1]);
    }

    return totalDistance;
}

我还有一个助手方法。。。

private void djAux(String startVertex, String endVertex, Node previousNode,
        Node currentNode, Stack<Node> visited,
        ArrayList<String> shortestPath, ArrayList<String> currentPath,
        int lowestPath) {

    Object[] map = getAdjacentVertices(currentNode.name).keySet().toArray();
    for (int i = 0; i < map.length; i++) {
        if (!currentPath.contains(map[i])
                && !currentNode.previous.contains(map[i])) {
            currentNode = new Node((String) map[i]);
            temp += getCost(visited.peek().name, currentNode.name);
            visited.push(currentNode);
            currentPath.add(currentNode.name);
            djAux(startVertex, endVertex, previousNode, currentNode,
                    visited, shortestPath, currentPath, lowestPath);
        } else if (i == map.length - 1) {
            if (shortestPath.size() == 0) {
                shortestPath.add("None");
                temp = -1;
            }
        }

    }

    if (currentNode.name.equals(endVertex)) {
        if ((lowestPath > 0 && temp < lowestPath && temp != -1)
                || lowestPath < 0) {
            shortestPath.clear();
            lowestPath = new Integer(temp);
            alpha = true;
            for (String s : currentPath) {
                shortestPath.add(s);
            }
        }
    }
}

以及节点类

public static class Node {
    ArrayList<String> previous;
    String name;

    public Node() {
        name = null;
        previous = new ArrayList<String>();
    }

    public Node(String name) {
        this.name = name;
        previous = new ArrayList<String>();
    }
}

下面是getcost()方法

public int getCost(String startVertexName, String endVertexName) {
    if (!getAdjacentVertices(startVertexName).containsKey(endVertexName)) {
        return 0;
    }
    return getAdjacentVertices(startVertexName).get(endVertexName);

}

我试着调试这个,但是找不到为什么一些值没有打印出来。
有人能帮忙吗?
先谢谢你。
编辑:我知道如何更新成本。见上文。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题