Java 实施Dijkstra';基于优先级队列的s算法
我正在使用优先级队列实现Dijkstra的算法,我想要一个函数从堆中删除一个元素,但我只能从Dijkstra的main中发送顶点索引,我找不到它在堆中的位置,我无法进行二进制搜索。有什么想法吗Java 实施Dijkstra';基于优先级队列的s算法,java,algorithm,heap,priority-queue,dijkstra,Java,Algorithm,Heap,Priority Queue,Dijkstra,我正在使用优先级队列实现Dijkstra的算法,我想要一个函数从堆中删除一个元素,但我只能从Dijkstra的main中发送顶点索引,我找不到它在堆中的位置,我无法进行二进制搜索。有什么想法吗 public class MinHeap { Vertex[] Heap = null; // Vertex array int Lenght; int Size; int[] elementsPostion; // Array of Index of Vertices private int pare
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();
}
}
公共类MinHeap{
顶点[]堆=null;//顶点数组
内部长度;
整数大小;
int[]elementspstion;//顶点索引数组
私有整数父级(整数i){
如果(i%2==0)
返回(i/2)-1;
其他的
返回i/2;
}
私有int leftChild(int i){
返回(2*i)+1;
}
私人int rightChild(int i){
返回(2*i)+2;
}
//初始化PQ
公共小堆(整数){
长度=长度;
尺寸=0;
堆=新顶点[长度];
elementsPostion=新的整数[长度];
}
//提取Min
公共顶点提取min(){
顶点v;
v=堆[0];//min=最小值的索引
elementsPostion[Heap[0]。索引]=-1;
堆[0]=堆[Size-1];
elementsPostion[Heap[0]。索引]=0;
尺寸=尺寸-1;
minHeapify(0);
返回v;
}
// ----------------------------
//在PQ内排序
公共无效minHeapify(内部位置){
int L;
INTR;
L=左子女(pos);
R=右子女(pos);
而(pos *if(posPositions[vertex]
跟踪堆中的顶点,并记录(顶点,距离)
作为堆数组中的元素。但仅实现这一点是不够的,因为每次在任何例程中对堆执行交换操作时,都需要更新顶点的位置