dijkstra algorithm in c

6
#include<stdio.h> #include<conio.h> #define SIZE 20 // STRUCTURE DECLARATION FOR GRAPH typedef struct Graph { int vindex[SIZE][SIZE]; char vname[SIZE][SIZE]; }Graph; void create(Graph *,int); void djk(int [][SIZE],int,int); void main() { // VARIABLE DECLARATION Graph g; //stack s; int v,ch,u,visited[SIZE]={0}; clrscr(); do { // DISPLAY MENU printf("\n\n\t\t MENU"); printf("\n\n\t 1. Create"); printf("\n\n\t 2. Djkstras Algorithm"); printf("\n\n\t 3. Exit"); // ACCEPT CHOICE FROM USER printf("\n\n\t Enter your choice:"); scanf("%d",&ch); switch(ch) { case 1: clrscr();

description

good

Transcript of dijkstra algorithm in c

Page 1: dijkstra algorithm in c

#include<stdio.h>#include<conio.h>#define SIZE 20

// STRUCTURE DECLARATION FOR GRAPHtypedef struct Graph{ int vindex[SIZE][SIZE]; char vname[SIZE][SIZE];}Graph;

void create(Graph *,int);void djk(int [][SIZE],int,int);

void main(){ // VARIABLE DECLARATION Graph g; //stack s; int v,ch,u,visited[SIZE]={0};

clrscr(); do { // DISPLAY MENU printf("\n\n\t\t MENU"); printf("\n\n\t 1. Create"); printf("\n\n\t 2. Djkstras Algorithm"); printf("\n\n\t 3. Exit");

// ACCEPT CHOICE FROM USER printf("\n\n\t Enter your choice:"); scanf("%d",&ch);

switch(ch) { case 1: clrscr(); // ACCEPT NO. OF VERTICES FROM USER printf("\n\n Enter the No. of vertices:"); scanf("%d",&v);

// CHECK FOR VALID INPUT if(v>=SIZE-1||v<=0) {

Page 2: dijkstra algorithm in c

printf("\n\n You have entered invalid input..."); } else // CREATE FUNCTION CALL create(&g,v); break;

case 2: clrscr();

printf("\n\n Enter the starting node:"); scanf("%d",&u); // BFS FUNCTION CALL// printf("\n\n BFS Traversal of graph is:\n"); djk(g.vindex,v,u);

break;

case 3:

exit(0);

default: printf("\n\n You have entered invalid choice..."); break;

} }while(ch!=3);

getch();

}

void create(Graph *g,int v){ // VARIABLE DECLARATION int i,j,flag; char ans;

clrscr();

Page 3: dijkstra algorithm in c

for(i=0;i<v;i++) { // ACCEPT NAME OF VERTICES FROM USER printf("\n\n Enter the name vertex %d:",i+1); scanf("%s",g->vname[i]); }

for(i=0;i<v;i++) { for(j=0;j<v;j++) { // MAKE ALL INDEX OF VERTICES TO 0 g->vindex[i][j]=32767; }// END OF LOOP j }// END OF LOOP i

// ACCEPT VALUE FOR CONNECTED VERTEX for(i=0;i<v;i++) { for(j=0;j<v;j++) { if(g->vindex[i][j]==32767) { flag=0;

while(flag!=1) { flushall(); clrscr(); printf("\n\n Is vertex %s is connected to vertex %s (y/n):",g->vname[i],g->vname[j]); scanf("%c",&ans);

// CHECK FOR VALID INPUT if(ans=='y'|| ans=='n' || ans=='Y' || ans=='N') { flag=1; if(ans=='y' || ans=='Y') { printf("\n\n Enter the weight of %s to %s",g->vname[i],g->vname[j]); scanf("%d",&g->vindex[i][j]); g->vindex[j][i]=g->vindex[i][j]; }

} else

Page 4: dijkstra algorithm in c

{ printf("\n\n You have entered invalid choice... Enter it again..."); flag=0; } } } } }}

void djk(int G[SIZE][SIZE],int n, int startnode){ int cost[SIZE][SIZE],distance[SIZE],pred[SIZE]; int visited[SIZE],count,mindistance,nextnode,i,j; /*pred[] stores the predecessor of each node count gives the number of nodes seen so far*/

//create the cost matrix for(i=0;i<n;i++) for(j=0;j<n;j++) if(G[i][j]==32767) cost[i][j]=32767; else cost[i][j]=G[i][j]; //initialize for(i=0;i<n;i++) { distance[i]=cost[startnode][i]; pred[i]=startnode;visited[i]=0; } distance[startnode]=0;visited[startnode]=1; count=1; while(count<n-1) { mindistance=32767;// nextnode is the node at minimum distance for(i=0;i<n;i++) if(distance[i] < mindistance && !visited[i]) { mindistance=distance[i]; nextnode=i; }//check if a better path exist through nextnode visited[nextnode]=1;

Page 5: dijkstra algorithm in c

for(i=0;i<n;i++) if(!visited[i]) if(mindistance+cost[nextnode][i]<distance[i]) { distance[i]=mindistance+cost[nextnode][i]; pred[i]=nextnode; } count++; }

 //print the path and distance of each node for(i=0;i<n;i++) if(i!=startnode) { printf("\n Distance of %d = %d ",i,distance[i]); printf("\nPath = %d ",i); j=i; do { j=pred[j]; printf("<- %d ",j); }while(j!=startnode); }

}