Professional Documents
Culture Documents
AI Lab Assignment 2 TEB57 1
AI Lab Assignment 2 TEB57 1
int weight;
node *next;
}; public:
node *head[20];
int Heuristic[20], no, fn, gn;
head[v1] = h;
}
else
{
while(f->next)
f = f->next;
f->next = n;
}
} void graph :: display()
{
int i;
node *f;
for(i = 0 ; i < no; i++)
{
cout<<endl;
f = head[i];
while(f)
{
cout<<"\t"<<f->data;
f = f->next;
}
}
}
void graph :: Astar(int src, int goal){
node *f; gn = 0;
f = head[src]; cout<<"\n
Shortest Path is :";
cout<<src<<"\t"; while(src
!= goal){
f = head[src];
f = f->next; fn =
Max; while(f){
if(fn > (gn + f->weight + Heuristic[f->data])){
gn = gn + f->weight;
fn = gn + Heuristic[f->data];
src = f->data;
}
f = f->next;
}
cout<<src<<"\t";
}
} int main()
{
graph g;
int i, v1, v2, w;
char ch;
{
g.head[i] = NULL;
cout<<"\n Heuristic["<<i<<"]=";
cin>>g.Heuristic[i];
}
cout<<"\n Now you have to enter vertices of an edge"
<<"\n The vertices label as 0 to "<<g.no-1<<endl;
do{
cout<<"\n Enter edge & weight(V1, V2, W)";
cin>>v1>>v2>>w;
g.edge_v1_v2(v1, v2, w);
//g.edge_v2_v1(v1, v2);
cout<<"\n Do u want more edge(y/n)"; cin>>ch;
}while(ch == 'y');
return 0;
}
Output :