【问题标题】:Shortest path of Dijkstra algorithmDijkstra算法的最短路径
【发布时间】:2016-12-16 23:03:49
【问题描述】:

我想为我的边加权图中的最短路径实现一个迭代器。

public class Graph<T> implements GraphADT<T> {

    protected final int DEFAULT_CAPACITY = 10;
    protected int numVertices; // number of vertices in the graph
    protected boolean[][] adjMatrix; // adjacency matrix
    protected T[] vertices; // values of vertices
    protected double weight[][];

我尝试了很多实现,甚至尝试了自己的实现,但我想我还不了解 Dijkstra 算法的逻辑,谁能给我解释一下或给出他们在 Java 上的实现

这是我的尝试实现

public Iterator<T> iteratorShortestPathW1(int startIndex, int targetIndex) {
    ArrayUnorderedList<T> resultList = new ArrayUnorderedList<T>();
    if (!indexIsValid(startIndex) || !indexIsValid(targetIndex)) {
        return resultList.iterator();
    }

    Iterator<T> it = iteratorShortestPathW2(startIndex,
            targetIndex);
    while (it.hasNext()) {
        resultList.addToRear(vertices[((Integer) it.next()).intValue()]);
    }
    return resultList.iterator();
}

public Iterator<T> iteratorShortestPathW(T startVertex, T targetVertex) {
    return iteratorShortestPathW1(getIndex(startVertex),
            getIndex(targetVertex));
}

protected Iterator<T> iteratorShortestPathW2(int startIndex, int targetIndex) {
    int index = startIndex;
    int[] tamcaminho = new int[numVertices];
    int[] predecessor = new int[numVertices];
    LinkedQueue<Integer> traversalQueue = new LinkedQueue<Integer>();
    ArrayUnorderedList<Integer> resultList = new ArrayUnorderedList<Integer>();

    if (!indexIsValid(startIndex) || !indexIsValid(targetIndex) || (startIndex == targetIndex)) {
        return (Iterator<T>) resultList.iterator();
    }

    boolean[] visited = new boolean[numVertices];
    for (int i = 0; i < numVertices; i++) {
        visited[i] = false;
    }

    traversalQueue.enqueue(startIndex);
    visited[startIndex] = true;
    tamcaminho[startIndex] = 0;
    predecessor[startIndex] = -1;

    while (!traversalQueue.isEmpty() && (index != targetIndex)) {
        System.out.print("acima");
        index = traversalQueue.dequeue();
        int menor = 999;
        for (int i = 0; i < numVertices; i++) {
            if (adjMatrix[index][i] && !visited[i]) {
                tamcaminho[i] = (int) weight[index][i];

                if (menor > tamcaminho[i]) {
                    menor = tamcaminho[i];
                }
            }
        }

        for (int i = 0; i < numVertices ; i++) {
            if (tamcaminho[i] == menor) {                  
                System.out.print("abaixo");
                visited[index] = true;
                traversalQueue.enqueue(i);
                predecessor[i] = index;

            }
        }
    }
    if (index != targetIndex) {
        return (Iterator<T>) resultList.iterator();
    }

    LinkedStack<Integer> stack = new LinkedStack<Integer>();
    index = targetIndex;
    stack.push(new Integer(index));
    do {
        index = predecessor[index];
        stack.push(new Integer(index));
    } while (index != startIndex);

    while (!stack.isEmpty()) {
        resultList.addToRear(((Integer) stack.pop()));
    }

    return (Iterator<T>) resultList.iterator();
}

【问题讨论】:

  • 分享你自己的实现,你可能会在你的具体问题上得到一些帮助。
  • @Atreys 我添加了我的实施尝试

标签: algorithm double dijkstra protected


【解决方案1】:

来自 Cormen 算法简介。 Dijkstra 算法维护一组顶点 S,其最终最短路径 源 s 的权重已经确定。算法反复 选择具有最小最短路径估计的顶点 u 2 V S,添加 u 到 S,并放松所有离开 u 的边。在下面的实现中,我们使用 顶点的最小优先级队列 Q,由它们的 d 值键控。

DIJKSTRA.G;w; s/
1 INITIALIZE-SINGLE-SOURCE.G; s/
2 S D ;
3 Q D G:V
4 while Q ¤ ;
5 u D EXTRACT-MIN.Q/
6 S D S [ fug
7 for each vertex  2 G:AdjOEu
8 RELAX.u; ;w/DIJKSTRA.G;w; s/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多