You are on page 1of 5

#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(); // 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) {

// BFS FUNCTION CALL // printf("\n\n BFS Traversal of graph is:\n"). break. } }while(ch!=3).printf("\n\n You have entered invalid input.u).. } void create(Graph *g. .").v). char ans. printf("\n\n Enter the starting node:").j..").int v) { // VARIABLE DECLARATION int i.&u). scanf("%d". case 2: clrscr().flag. break.. djk(g.vindex. case 3: exit(0). getch().v. break.. default: printf("\n\n You have entered invalid choice. clrscr(). } else // CREATE FUNCTION CALL create(&g.

&ans).j<v.i<v.i++) { for(j=0. scanf("%c". scanf("%s".g->vname[i]). scanf("%d".i+1). clrscr().g->vname[i].g->vname[j]).i++) { for(j=0. if(ans=='y' || ans=='Y') { printf("\n\n Enter the weight of %s to %s".j++) { // MAKE ALL INDEX OF VERTICES TO 0 g->vindex[i][j]=32767.i<v. }// END OF LOOP j }// END OF LOOP i // ACCEPT VALUE FOR CONNECTED VERTEX for(i=0.i<v. } for(i=0. while(flag!=1) { flushall().&g->vindex[i][j]). g->vindex[j][i]=g->vindex[i][j].i++) { // ACCEPT NAME OF VERTICES FROM USER printf("\n\n Enter the name vertex %d:".j<v.for(i=0.j++) { if(g->vindex[i][j]==32767) { flag=0.g->vname[i]. // CHECK FOR VALID INPUT if(ans=='y'|| ans=='n' || ans=='Y' || ans=='N') { flag=1.g->vname[j]). printf("\n\n Is vertex %s is connected to vertex %s (y/n):". } } else .

distance[SIZE].count. .int n. else cost[i][j]=G[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. } //check if a better path exist through nextnode visited[nextnode]=1.{ printf("\n\n You have entered invalid choice..i. //initialize for(i=0."). int visited[SIZE].i++) if(distance[i] < mindistance && !visited[i]) { mindistance=distance[i]. count=1. } distance[startnode]=0. nextnode=i.pred[SIZE].nextnode. } } } } } } void djk(int G[SIZE][SIZE]..mindistance.j<n. pred[i]=startnode.i<n..i<n.. int startnode) { int cost[SIZE][SIZE]. Enter it again. flag=0.i++) { distance[i]=cost[startnode][i].visited[i]=0.j.j++) if(G[i][j]==32767) cost[i][j]=32767.i<n.i++) for(j=0. while(count<n-1) { mindistance=32767.visited[startnode]=1. // nextnode is the node at minimum distance for(i=0.

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