【问题标题】:Dijkstra's Algorithm issue [repost]Dijkstra 的算法问题 [repost]
【发布时间】:2013-04-15 22:37:44
【问题描述】:

我意识到我无法发布我自己的问题的答案,因为我的代表率低或其他原因,所以我删除了我的旧问题并重新提出。我改变了一些东西,但仍然无法得到我正在寻找的东西。

这里是大部分代码 我省略了一些更简单的实现,例如 pathFinder 类的一部分,因为我确信它们可以工作,这就是为什么你会在那里随机看到 playerVertex 和 time。 在他们使用 reductionKey 函数的示例中,我不确定这是否是我所缺少的?我是这里的初学者,所以欢迎提出建设性的批评。 (希望尽可能礼貌)大声笑。我的问题是打印路径,我一遍又一遍地得到相同的两个值的循环。

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 算法的打印路径。

【问题讨论】:

  • 只是需要什么!
  • for each 不是标准 c++,是否在某处定义了宏 each
  • @DavidBrown 我不这么认为。它必须是我的 Visual Studio 的一部分、编译器扩展或其他东西。
  • @DavidBrown OP是对的,它是specific to the MSVC compiler。我建议使用BOOST_FOREACH 来获得更好的可移植性,或者如果您使用的是 C++11(并且只需要您的代码可移植到其他 C++11 编译器),则使用 use this
  • 我不认为你确保堆条件得到维持。放松节点后,您需要确保对堆进行重新排序以尊重新成本。不这样做可能意味着您在路径中生成循环。

标签: c++ dijkstra


【解决方案1】:

您在printPath 函数中的哪个位置查找列表的末尾?

您继续verta = verta-&gt;pred,直到数据不等于某个值。

顺便说一句,不要比较双打是否相等,因为它不会发生。见What Every Computer Scientist Should Know About Floating Point.

当您使用调试器单步执行时会发生什么?
(尝试绘制链接以及如何遍历它们。)

【讨论】:

  • 是的,就像我说的,我使用了 BFS 的 printPath 函数,所以我不确定它是否正确,哈哈。我正在尝试在网格中说 [0][0] 上的 Dijkstra。然后我从 [3][3] 打印路径从 pred 到 pred 直到它到达 [0][0]。这是 BFS 打印出来的。我不确定这是否是您在 Dijkstra 中打印出来的方式。
猜你喜欢
  • 2014-06-20
  • 2021-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多