Você está na página 1de 7

#include <stdio.

h>

#include<malloc.h>

#include<string.h>

#define N 100

struct Node

int fval;

int id;

int g;

};

struct Node *minHeap[N],*closedList[N];

int i,n,size=0,closedsize=0,opensize=0;

adjustpos(int i,int val)

while((minHeap[i/2]->fval)>val)

swap(i,i/2);

i/=2;

insertHeap(int s_id,int s_g,int s_f)

printf("\nsize= %d :",size);

struct Node* insrtnode=(struct Node*)malloc(sizeof(struct Node*));

insrtnode->id=s_id;

insrtnode->fval=s_f;

insrtnode->g=s_g;

minHeap[size]=insrtnode;

printf("id of new node %d",insrtnode->id);

size=size+1;
printf("\n heap elemts before heapify are :");

for(i=0;i<size;i++)

printf(" \n%d fcost =%d ",minHeap[i]->id,minHeap[i]->fval);

adjustpos(size-1,s_f);

printf("\nsize= %d heap gval=%d, elemts are :",size,minHeap[0]->g);

for(i=0;i<size;i++)

printf(" %d ",minHeap[i]->id);

swap(int a, int b)

int t1=minHeap[a]->id;

int t2=minHeap[a]->g;

int t3=minHeap[a]->fval;

minHeap[a]->id=minHeap[b]->id;

minHeap[a]->g=minHeap[b]->g;

minHeap[a]->fval=minHeap[b]->fval;

minHeap[b]->id=t1;

minHeap[b]->g=t2;

minHeap[b]->fval=t3;

printpath(int parent[n],int i) {

if(parent[i]!=-1) printpath(parent,parent[i]);

printf("->%d ",i);

void heapify(int i)

{
int smallest = i; // Initialize smallest as root

int l = 2*i + 1; // left = 2*i + 1

int r = 2*i + 2; // right = 2*i + 2

if (l < size && minHeap[l]->fval < minHeap[smallest]->fval)

smallest = l;

if (r < size && minHeap[r]->fval < minHeap[smallest]->fval)

smallest = r;

if (smallest != i)

swap(i, smallest);

heapify(smallest);

struct Node* extractmin()

struct Node* min=minHeap[0];

minHeap[0]=minHeap[size-1];

size--;

heapify(0);

return min;

a_star(int st,int goal, int adjMat[][n],int h[])

int parent[n];

int j;

for(i=0;i<n;i++) parent[i]=-1;
struct Node* strtnode=(struct Node*)malloc(sizeof(struct Node*));

strtnode->id=st;

strtnode->fval=h[0];

strtnode->g=0;

insertHeap(0,0,10);

while(size!=0)

struct Node *current;

current=extractmin();

printf("\ntraversing node : %d gval =%d\n\n",current->id,current->g);

int curr_id=current->id;

int curr_g=current->g;

if(current->id==goal)

printf("\nGoal node found !\n");

printf("Solution path is :\n");

printpath(parent,current->id);

return;

printf("\nGenerating successors of %d\n",current->id);

for(j=0;j<n;j++)

printf("\n ");

printf("\n%d- value of j , %d - g cost, condition =


%d",j,adjMat[curr_id][j],adjMat[curr_id][j]!=0);

if ( adjMat[curr_id][j]!=0 )

printf("\ngfdf,");
printf("\n current gval for id: %d , is %d(gval) ",curr_id,curr_g);

int s_id=j;

int s_g=curr_g+adjMat[curr_id][s_id];

int s_f=s_g+h[s_id];

printf("\n succesor fval for id: %d , is %d(gval) ",s_id,s_g);

if(parent[s_id]==-1)

parent[s_id]=curr_id;

insertHeap(s_id,s_g,s_f);

printf("\n%d inserted",s_id);

closedList[closedsize++]=current;

int main() {

printf("Enter the number of nodes : ");

scanf("%d",&n);

int adjMat[n][n];

int x,y;

printf("Enter the cost matrix\n ");

for(x=0;x<n;x++)
{

printf("\n%dth row :",x+1 );

for(y=0;y<n;y++)

scanf("%d",&adjMat[x][y]);

int h[n];

printf("\nEnter the %d numbers of h values of each node:",n);

for(x=0;x<n;x++)

scanf("%d",&h[x]);

int st,goal;

printf("\nEnter the start node :");

scanf("%d",&st);

printf("\nEnter the goal node :");

scanf("%d",&goal);

// an example input

/* int size=0;

n=8;

int
adjMat[8][8]={{0,1,2,0,0,0,0,0},{0,0,0,0,0,4,7,0},{0,0,0,7,1,0,0,0},{0,0,0,0,0,0,0,5},{0,0,0,0,0,0,0,12},{0,
0,0,0,0,0,0,2},{0,0,0,0,0,0,0,3},{0,0,0,0,0,0,0,0}};

int h[8]={10,5,6,4,15,5,8,0};

int st=0,goal=7;*/

a_star(st,goal,adjMat,h);
return 0;

Você também pode gostar