Dijkstra的算法问题[转贴]

Dijkstra's Algorithm issue [repost]

本文关键字:转贴 问题 算法 Dijkstra      更新时间:2023-10-16

我意识到我不能回答我自己的问题,因为我的低代表或什么,所以我删除了我的旧问题,并重新问它。我改变了一些东西,但仍然得不到我想要的东西。

下面是大部分代码我省略了一些更简单的实现,比如pathFinder类的部分,因为我确信它们是有效的,这就是为什么你会看到playerVertex和time随机出现的原因。在例子中,他们使用了一个减少关键功能,我不确定如果这是我错过了什么?我是新手,所以欢迎建设性的批评。(希望尽可能礼貌)哈哈。我的问题是打印路径,我得到一个循环的相同的两个值一遍又一遍。

class Heap 
{
public: Heap();
    ~Heap();
    void insert(double element);
    double  deletemin();
    void print();
    int size(){return heap.size();}

private:
 int currentIndex;
 int left(int parent);
 int right(int parent);
 int parent(int child);
 void heapifyup(int index);
 void heapifydown(int index);
private:
 vector<double> heap;
};
Heap::Heap()
{
 currentIndex = 0;
}
Heap::~Heap()
{}
void Heap::insert(double element)
{
heap.push_back(element);
currentIndex++;
heapifyup(heap.size() - 1);
}
double Heap::deletemin()
{
double min = heap.front();
heap[0] = heap.at(heap.size()-1);
heap.pop_back();
heapifydown(0);
currentIndex--;
return min;
}
void Heap::print()
{
vector<double>::iterator pos = heap.begin();
cout << "Heap = ";
while ( pos != heap.end() ) 
{
    cout << *pos;
    ++pos;
    cout << endl;
 }
}
void Heap::heapifyup(int index)
{

while((index>0) && (parent(index) >=0) && (heap[parent(index)] > heap[index]))
{
 double tmp = heap[parent(index)];
 heap[parent(index)] = heap[index];
 heap[index] = tmp;
 index = parent(index);

}
}
void Heap::heapifydown(int index)
{

int child = left(index);
if((child > 0) && (right(index) > 0) && (heap[child]>heap[right(index)]))
{
 child = right(index);
}
if(child > 0)
{
double tmp = heap[index];
heap[index] = heap[child];
heap[child] = tmp;
heapifydown(child);
}
}
int Heap::left(int parent)
{
int i = ( parent <<1) + 1; 
return(i<heap.size()) ? i : - 1;
}
int Heap::right(int parent)
{
int i = ( parent <<1) + 2; 
return(i<heap.size()) ? i : - 1;
}
int Heap::parent(int child)
{
if(child != 0)
{
 int i = (child - 1) >>1;
 return i;
}
return -1;
}

class pathFinder : public weightedGraph
{
private:
vertex* playerVertex;
double time;

public:
string source;
pathFinder()
{
    playerVertex = NULL;
    time = 0;
}

  void Dijkstra(int s,int t)
{
    vertex *verts = findVertex(grid[s][t]);
    Heap H;
    for each(vertex *v in vertexList)
    {
        if(v->data == verts->data)
        {
            verts->distance = 0;
            verts->pred = NULL;
        }
        v->distance = INFINITY;
        v->pred = NULL;
        H.insert(v->data);
    }
    while(H.size() != 0)
    {
        vertex *x = findVertex(H.deletemin());
        for each(edge *v in x->adjacencyList)
        {
            if(v->end->visited != true)
            {    
            relax(x,v->end);
            v->end->visited = true;
            }
            else
                break;
        }
    }
}




void relax(vertex *a, vertex *b)
{
    if(a->distance + weightFrom(a,b) > b->distance)
        {
            b->distance = a->distance + weightFrom(a,b);
            b->pred = a;
        }

}

void printPath(double dest,double dest1)
{
    vertex *verta = findVertex(dest);
    while(verta->pred->data != dest1)
    {
    cout<<verta->data<<endl;
    verta = verta->pred;
    }
}

,我不确定打印路径是这样的。我只是使用了我之前实现的BFS算法的打印路径。

在您的printPath函数中您正在寻找列表的末尾吗?

继续执行verta = verta->pred,直到数据不等于某个值。

顺便说一下,不要比较双精度对象是否相等,因为这是不会发生的。请参阅每个计算机科学家都应该知道的浮点数。

当您使用调试器执行单步操作时会发生什么?
(试着画出链接以及你如何遍历它们)