图遍历与A*算法
Graph traversal with A* algorithm
嘿,我是AI学生,我要尝试我的作业是实现A*算法来遍历一个图。我使用c++代码,我现在所做的是下面的代码,这只是一个图形类+ insertedge和顶点函数。但现在我很困惑如何定义成本函数(f= h(n) + g(n))…
也任何代码参考或解释如何A*的工作图将帮助我。我在谷歌上找到的是关于通过a*寻路的,它没有遍历图。
#include <iostream>
using namespace std;
class Graph;
class node {
friend class Graph;
private:
char data;
int cost;
node *next;
node *vlist;
bool goal;
bool visited;
public:
node(char d,int c, bool g){
cost = c;
goal = g;
next = NULL;
data = d;
vlist = NULL;
visited = false;
}
};
class Graph {
private:
node *Headnodes;
char n;
public:
Graph ()
{
Headnodes = NULL;
}
void InsertVertex(char n, int c, bool g);
void InsertEdge(char n, char m, int c);
void PrintVertices();
void Expand(char n);
};
/////////////////
// INSERTION //
/////////////////
void Graph::InsertVertex(char n, int c, bool g){
node *temp = new node(n,c,g);
if(Headnodes==NULL)
{
Headnodes=temp;
return;
}
node *t=Headnodes;
while(t->vlist!=NULL)
t=t->vlist;
t->vlist=temp;
}
void Graph::InsertEdge(char n, char m, int c){
int temp_cost = 0;
if(Headnodes==NULL)
return;
node *x=Headnodes;
while(x!=NULL){
if(x->data==m)
temp_cost = (x->cost+c);
x = x->vlist;
}
node *t=Headnodes;
node *temp=new node(m,temp_cost,false);
while(t!=NULL){
if(t->data==n){
node *s=t;
while(s->next!=NULL)
s=s->next;
s->next=temp;
}
t=t->vlist;
}
}
int min_cost = 1000;
bool enough = false;
void Graph::PrintVertices(){
node *t=Headnodes;
while(t!=NULL){
cout<<t->data<<"t";
t=t->vlist;
}
}
void Graph::Expand(char n){
cout << n << "t";
char temp_min;
node *t=Headnodes;
while(t!=NULL){
if(t->data==n && t->visited == false){
t->visited = true;
if (t->goal)
return;
while(t->next!=NULL){
if (t->next->cost <= min_cost){
min_cost=t->next->cost;
temp_min = t->next->data;
}
t = t->next;
}
}
t=t->vlist;
}
Expand(temp_min);
}
int main(){
Graph g;
g.InsertVertex('A',5,false);
g.InsertVertex('B',1,false);
g.InsertVertex('C',9,false);
g.InsertVertex('D',5,false);
g.InsertVertex('E',1,false);
g.InsertVertex('F',3,false);
g.InsertVertex('G',2,false);
g.InsertVertex('J',1,false);
g.InsertVertex('K',0,true);
g.InsertEdge('A','B',2);
g.InsertEdge('A','C',1);
g.InsertEdge('B','A',2);
g.InsertEdge('B','D',1);
g.InsertEdge('B','E',1);
g.InsertEdge('C','A',1);
g.InsertEdge('C','F',1);
g.InsertEdge('C','G',1);
g.InsertEdge('E','J',3);
g.InsertEdge('E','K',3);
g.PrintVertices();
cout<<"nn";
g.Expand('A');
return 0;
}
你所拥有的只是一个图搜索算法。
你忘记了A*算法的本质,那就是h(n)代价,它来自于启发式计算。
你必须实现一个方法,h(n),它将根据你的启发式计算从实际路径到最终路径的可能成本,这个值将用于计算步行成本:
f (n) = g (n) + h ' (n) , 的g (n) 已经知道成本,在你的情况下,x →。
g(n)是它的总距离成本从一开始就被拿走了位置到当前位置。
h'(n)是到到目标的当前位置目的地/状态。一种启发式函数用于创建此估计一下要走多远达到目标状态
f'(n)是g(n)和h'(n)的和。这是目前估计的最短时间路径。F (n)是真正的最短路径直到A*才被发现算法完成
那么,你要做的是:
- 实现一个方法heuristic_cost(actual_node, final_node);
- 将此值与实际成本一起使用,如前面的公式所示,例如:min_cost=t->next->cost + heuristic_cost(t->next, final_node);
我真的很喜欢这里的解释:链接,比维基百科的解释更清晰。
相关文章:
- 有什么方法可以遍历结构吗
- 在循环中按顺序遍历成员变量
- 遍历模板参数
- 在遍历处理程序的向量时注册和注销处理程序
- C++RapidXml-使用first_node()遍历以修改XML文件中节点的值
- 遍历并行数组以确定C++中的最大数字
- 遍历顺序由 std::文件系统directory_iterator给出
- 遍历链表时的无限循环
- 遍历unordered_map向量
- 从预序遍历构造 bst 的 c++ 和 python 解决方案之间的区别
- C++声明双链表,使用两个 for 循环双向遍历列表并打印
- 如何正确地推回然后遍历堆中对象的向量?
- 遍历二维数组的所有子数组
- 如何在可变参数模板函数中遍历可变参数元组?
- 避免在遍历 IShellItemArray 时出现代码重复
- 执行策略如何用于C++中的顺序遍历算法
- 二叉树之字形水平阶遍历的这种算法的时间复杂度是多少?
- 输出或遍历集合的所有对组合的标准算法
- 图遍历与A*算法
- 寻找遍历图中所有顶点的路径的更好算法是什么?