当前位置: 首页 > 知识库问答 >
问题:

使用优先级队列实现Dijkstra算法

孔理
2023-03-14

我正在使用优先级队列实现Dijkstra的算法,我想要一个函数从堆中删除一个元素,但我只能从Dijkstra的主节点索引中向它发送顶点索引,我找不到它在堆上的位置,我负担不起进行二进制搜索。有什么想法吗?

public class MinHeap {
Vertex[] Heap = null; // Vertex array
int Lenght;
int Size;
int[] elementsPostion; // Array of Index of Vertices

private int parent(int i) {
    if (i % 2 == 0)
        return (i / 2) - 1;
    else
        return i / 2;
}

private int leftChild(int i) {
    return (2 * i) + 1;
}

private int rightChild(int i) {
    return (2 * i) + 2;
}

// Initialize PQ
public MinHeap(int len) {
    Lenght = len;
    Size = 0;
    Heap = new Vertex[Lenght];
    elementsPostion = new int[Lenght];
}

// Extract Min
public Vertex ExtractMin() {

    Vertex v;
    v = Heap[0]; // min = index of min
    elementsPostion[Heap[0].index] = -1;
    Heap[0] = Heap[Size - 1];
    elementsPostion[Heap[0].index] = 0;
    Size = Size - 1;
    minHeapify(0);
    return v;
}

// ----------------------------
// Sort Inside PQ
public void minHeapify(int pos) {
    int L;
    int R;
    L = leftChild(pos);
    R = rightChild(pos);
    while (pos < Size
            && (Heap[L].minDistance < Heap[pos].minDistance || Heap[R].minDistance < Heap[pos].minDistance)) {
        Vertex tmp;
        if (Heap[L].minDistance < Heap[R].minDistance) {
            elementsPostion[Heap[L].index] = pos;
            elementsPostion[Heap[pos].index] = L;

            tmp = Heap[L];
            Heap[L] = Heap[pos];
            Heap[pos] = tmp;
            pos = L;
        } else {
            elementsPostion[Heap[R].index] = pos;
            elementsPostion[Heap[pos].index] = R;

            tmp = Heap[R];
            Heap[R] = Heap[pos];
            Heap[pos] = tmp;
            pos = R;
        }
        L = leftChild(pos);
        R = rightChild(pos);
        /*
         * if(pos< Size && Heap[L].minDistance <Heap[pos].minDistance)
         * min=L.index; else min=pos; if(R.index<=Size &&Heap[R]<Heap[pos])
         * min=R.index; if(min !=pos) { int tmp = Heap[pos]; Heap[pos] =
         * Heap[min]; Heap[min] = tmp; minHeapify(min); }
         */
    }

    // swap in P.Q with Swapping in arrayofVertNum
}


// insert vertex
public void insertVertex(Vertex element) {
    Heap[Size] = element; // size = number of verticies
    HeapDecreaseKey(Size, element); //
    Size++;
}

// Compare when insert with Parents
public void HeapDecreaseKey(int index, Vertex key) // bta5od el element ele hy3mlo insert ,,
{ 
    // index=size , key=element // add in last
    // Heap[index]=key; //add in last
    Vertex v = new Vertex(key.index, key.xPos, key.yPos, key.minDistance);

    //int swap;
    boolean b = false;
    while (index > 0
            && Heap[parent(index)].minDistance > Heap[index].minDistance) {
        b = true;
        elementsPostion[Heap[parent(index)].index] = index;
        elementsPostion[Heap[index].index] = parent(index);

        Vertex tmp = Heap[parent(index)];
        Heap[parent(index)] = Heap[index];
        Heap[index] = tmp;

        index = parent(index);
    }
    if (b == false)
        elementsPostion[key.index] = index;

    // Swap in array
}

// check if PQ is empty
public boolean isEmpty() {
    return Heap == null;
}

public void display() {



    for (int i = 0; i < Size; i++) {
        System.out.print(Heap[i].minDistance);
    }
    System.out.println();
}
}

共有1个答案

连正信
2023-03-14

使用简单索引数组< code>Positions[Vertex]跟踪堆中的顶点,并将< code >(顶点,距离)记录为堆数组中的元素。但是仅仅实现这一点是不够的,因为每次在任何例程中对堆进行交换操作时,都需要更新顶点的位置。

 类似资料:
  • 我正在为Dikjstra算法做一个优先级队列。我目前在插入方法上有麻烦。我包含了整个类的代码,以防你需要更好地了解我想完成的事情。我将堆索引放在一个数组列表(heapIndex)中,堆放在另一个数组列表中。 那是我运行程序后的输出(值,优先级,堆索引)。*(-1)表示heapIndex中的空单元格。

  • 我的问题是:每个节点的优先级是什么?我认为它是最小值的传入边缘的权重,但我不确定。这是真的吗? 第二个问题,当我提取队列的根时,如果这个节点不与任何一个被访问的节点邻接,它将如何工作?

  • 有人能帮我找到我的PQ的问题吗?

  • 这是我写的Dijkstra算法的代码: 在这方面我不能理解的工作 这涉及到: < code>()运算符在这里有什么用?我是说它在这段代码中是如何运作的? 还有为什么我们使用

  • 在我实现Dijkstra算法的过程中,我有1个数组(包含所有节点)和1个优先级队列(包含所有节点)。每当一个节点排队时,我都会用新的距离和它来自哪里来更新所有相邻的节点,这样我就可以回溯路径。 优先级队列中的节点更新为新距离,数组中的节点更新为它来自的位置和新距离。当节点出列时,数组中的最终距离会更新: 用前一个节点的信息更新数组和用距离更新优先级队列是否可以接受? 只要找到更好的距离,就会发生这

  • 我需要使用Java中的优先级队列实现Dijkstra的算法。这是我到目前为止的代码: 我需要填写最短路径方法并返回节点数组。但是,我不确定如何做到这一点。我知道我需要在某个时候进行优先排队,但有人可以向我解释一下如何吗?我已经制作了startNode,我知道我需要为它分配一个距离值0,其余节点的距离值为无穷大。另外,可比性从何而来?