Dijkstra的算法问题[转贴]
Dijkstra's Algorithm issue [repost]
我意识到我不能回答我自己的问题,因为我的低代表或什么,所以我删除了我的旧问题,并重新问它。我改变了一些东西,但仍然得不到我想要的东西。
下面是大部分代码我省略了一些更简单的实现,比如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
,直到数据不等于某个值。
当您使用调试器执行单步操作时会发生什么?
(试着画出链接以及你如何遍历它们)
相关文章:
- 警告处理为错误这里有什么问题
- 分段故障(堆芯转储)矢量
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- 运行我的代码时出现分段错误(核心转储)问题
- 跳转到if(false)块有问题吗
- 我的转置矩阵代码有什么问题?
- 子数组核心转储问题
- 我的转置函数有什么问题
- 核心转储?我已经通过我的程序很多次不知道出了什么问题
- GLSL法线贴图着色器问题
- OpenGL贴图问题
- Dijkstra的算法问题[转贴]
- 数组列表和使用插入和检索函数的问题(段核心转储)
- gcc转储类层次结构问题
- C 语言中的另一个核心转储问题